The From header only has parameters, no headers. Parameters of from ...;name=value;name2=value2. Specific parameters: tag.
Пример #1
0
        private void Initialise(List<SIPContactHeader> contact, SIPFromHeader from, SIPToHeader to, int cseq, string callId)
        {
            if (from == null)
            {
                throw new ApplicationException("The From header cannot be empty when creating a new SIP header.");
            }

            if (to == null)
            {
                throw new ApplicationException("The To header cannot be empty when creating a new SIP header.");
            }

            if (callId == null || callId.Trim().Length == 0)
            {
                throw new ApplicationException("The CallId header cannot be empty when creating a new SIP header.");
            }

            From = from;
            To = to;
            Contact = contact;
            CallId = callId;

            if (cseq > 0 && cseq < Int32.MaxValue)
            {
                CSeq = cseq;
            }
            else
            {
                CSeq = DEFAULT_CSEQ;
            }
        }
Пример #2
0
        public static SIPFromHeader ParseFromHeader(string fromHeaderStr)
        {
            try
            {
                SIPFromHeader fromHeader = new SIPFromHeader();

                fromHeader.m_userField = SIPUserField.ParseSIPUserField(fromHeaderStr);

                return fromHeader;
            }
            catch (ArgumentException argExcp)
            {
                throw new SIPValidationException(SIPValidationFieldsEnum.FromHeader, argExcp.Message);
            }
            catch
            {
                throw new SIPValidationException(SIPValidationFieldsEnum.FromHeader, "The SIP From header was invalid.");
            }
        }
Пример #3
0
        public SIPHeader(SIPContactHeader contact, SIPFromHeader from, SIPToHeader to, int cseq, string callId)
        {
            List<SIPContactHeader> contactList = new List<SIPContactHeader>();
            if (contact != null)
            {
                contactList.Add(contact);
            }

            Initialise(contactList, from, to, cseq, callId);
        }
Пример #4
0
 public SIPHeader(List<SIPContactHeader> contactList, SIPFromHeader from, SIPToHeader to, int cseq, string callId)
 {
     Initialise(contactList, from, to, cseq, callId);
 }
Пример #5
0
        /// <summary>
        /// Used to create a SIP response when it was not possible to parse the incoming SIP request.
        /// </summary>
        public SIPResponse GetResponse(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPResponseStatusCodesEnum responseCode, string reasonPhrase)
        {
            try
            {
                if (localSIPEndPoint == null)
                {
                    localSIPEndPoint = GetDefaultSIPEndPoint();
                }

                SIPResponse response = new SIPResponse(responseCode, reasonPhrase, localSIPEndPoint);
                SIPSchemesEnum sipScheme = (localSIPEndPoint.Protocol == SIPProtocolsEnum.tls) ? SIPSchemesEnum.sips : SIPSchemesEnum.sip;
                SIPFromHeader from = new SIPFromHeader(null, new SIPURI(sipScheme, localSIPEndPoint), null);
                SIPToHeader to = new SIPToHeader(null, new SIPURI(sipScheme, localSIPEndPoint), null);
                int cSeq = 1;
                string callId = CallProperties.CreateNewCallId();
                response.Header = new SIPHeader(from, to, cSeq, callId);
                response.Header.CSeqMethod = SIPMethodsEnum.NONE;
                response.Header.Vias.PushViaHeader(new SIPViaHeader(new SIPEndPoint(localSIPEndPoint.Protocol, remoteEndPoint.GetIPEndPoint()), CallProperties.CreateBranchId()));
                response.Header.MaxForwards = Int32.MinValue;
                response.Header.Allow = ALLOWED_SIP_METHODS;

                return response;
            }
            catch (Exception excp)
            {
                logger.Error("Exception SIPTransport GetResponse. " + excp.Message);
                throw;
            }
        }
Пример #6
0
 public SIPHeader(SIPFromHeader from, SIPToHeader to, int cseq, string callId)
 {
     Initialise(null, from, to, cseq, callId);
 }
Пример #7
0
        public SIPCDR(
            SIPCallDirection callDirection,
            SIPURI destination,
            SIPFromHeader from,
            string callId,
            SIPEndPoint localSIPEndPoint,
            SIPEndPoint remoteEndPoint)
        {
            CDRId = Guid.NewGuid();
            Created = DateTimeOffset.UtcNow;
            CallDirection = callDirection;
            Destination = destination;
            From = from;
            CallId = callId;
            LocalSIPEndPoint = localSIPEndPoint;
            RemoteEndPoint = remoteEndPoint;
            InProgress = false;
            IsAnswered = false;
            IsHungup = false;

            CDRCreated(this);
        }
Пример #8
0
        public SIPRequest GetRequest(SIPMethodsEnum method, SIPURI uri, SIPToHeader to, SIPEndPoint localSIPEndPoint)
        {
            if (localSIPEndPoint == null)
            {
                localSIPEndPoint = GetDefaultSIPEndPoint();
            }

            SIPRequest request = new SIPRequest(method, uri);
            request.LocalSIPEndPoint = localSIPEndPoint;

            SIPContactHeader contactHeader = new SIPContactHeader(null, new SIPURI(SIPSchemesEnum.sip, localSIPEndPoint));
            SIPFromHeader fromHeader = new SIPFromHeader(null, contactHeader.ContactURI, CallProperties.CreateNewTag());
            SIPHeader header = new SIPHeader(contactHeader, fromHeader, to, 1, CallProperties.CreateNewCallId());
            request.Header = header;
            header.CSeqMethod = method;
            header.Allow = ALLOWED_SIP_METHODS;

            SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint, CallProperties.CreateBranchId());
            header.Vias.PushViaHeader(viaHeader);

            return request;
        }
        private SIPRequest GetRegistrationRequest(SIPProvider sipProvider, SIPProviderBinding binding, SIPEndPoint localSIPEndPoint, int expiry, SIPEndPoint registrarEndPoint)
        {
            try
            {
                if (!binding.BindingSIPURI.Parameters.Has(m_regAgentContactId))
                {
                    binding.BindingSIPURI.Parameters.Set(m_regAgentContactId, Crypto.GetRandomString(6));
                }

                string realm = binding.RegistrarRealm;

                SIPURI registerURI = SIPURI.ParseSIPURIRelaxed(realm);
                SIPURI regUserURI = SIPURI.ParseSIPURIRelaxed(sipProvider.ProviderUsername + "@" + realm);

                SIPFromHeader fromHeader = new SIPFromHeader(null, regUserURI, CallProperties.CreateNewTag());
                SIPToHeader toHeader = new SIPToHeader(null, regUserURI, null);
                SIPContactHeader contactHeader = new SIPContactHeader(null, binding.BindingSIPURI);
                //contactHeader.Expires = binding.BindingExpiry;
                string callId = binding.Id.ToString();
                int cseq = ++binding.CSeq;

                SIPRequest registerRequest = new SIPRequest(SIPMethodsEnum.REGISTER, registerURI);
                registerRequest.LocalSIPEndPoint = localSIPEndPoint;
                SIPHeader header = new SIPHeader(contactHeader, fromHeader, toHeader, cseq, callId);
                header.CSeqMethod = SIPMethodsEnum.REGISTER;
                header.UserAgent = m_userAgentString;
                header.Expires = binding.BindingExpiry;

                SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint, CallProperties.CreateBranchId());
                header.Vias.PushViaHeader(viaHeader);

                SIPRoute registrarRoute = new SIPRoute(new SIPURI(binding.RegistrarServer.Scheme, registrarEndPoint), true);
                header.Routes.PushRoute(registrarRoute);

                if (sipProvider != null && !sipProvider.CustomHeaders.IsNullOrBlank())
                {
                    string[] customerHeadersList = sipProvider.CustomHeaders.Split(SIPProvider.CUSTOM_HEADERS_SEPARATOR);

                    if (customerHeadersList != null && customerHeadersList.Length > 0)
                    {
                        foreach (string customHeader in customerHeadersList)
                        {
                            if (customHeader.IndexOf(':') == -1)
                            {
                                logger.Debug("Skipping custom header due to missing colon, " + customHeader + ".");
                                continue;
                            }
                            else
                            {
                                string headerName = customHeader.Substring(0, customHeader.IndexOf(':'));
                                if (headerName != null && Regex.Match(headerName.Trim(), "(Via|From|To|Contact|CSeq|Call-ID|Max-Forwards|Content)", RegexOptions.IgnoreCase).Success)
                                {
                                    logger.Debug("Skipping custom header due to an non-permitted string in header name, " + customHeader + ".");
                                    continue;
                                }
                                else
                                {
                                    if (headerName == SIPConstants.SIP_USERAGENT_STRING)
                                    {
                                        header.UserAgent = customHeader.Substring(customHeader.IndexOf(':') + 1);
                                    }
                                    else
                                    {
                                        header.UnknownHeaders.Add(customHeader.Trim());
                                    }
                                }
                            }
                        }
                    }
                }

                registerRequest.Header = header;
                return registerRequest;
            }
            catch (Exception excp)
            {
                logger.Error("Exception GetRegistrationRequest. " + excp.Message);
                throw excp;
            }
        }
            private SIPRequest GetOptionsRequest(SIPURI serverURI, int cseq, IPEndPoint contact)
            {
                SIPRequest optionsRequest = new SIPRequest(SIPMethodsEnum.OPTIONS, serverURI);

                SIPFromHeader fromHeader = new SIPFromHeader(null, SIPURI.ParseSIPURI("sip:" + contact.ToString()), null);
                SIPToHeader toHeader = new SIPToHeader(null, serverURI, null);

                string callId = CallProperties.CreateNewCallId();
                string branchId = CallProperties.CreateBranchId();

                SIPHeader header = new SIPHeader(fromHeader, toHeader, cseq, callId);
                header.CSeqMethod = SIPMethodsEnum.OPTIONS;
                header.MaxForwards = 0;

                SIPViaHeader viaHeader = new SIPViaHeader(contact.Address.ToString(), contact.Port, branchId);
                header.Vias.PushViaHeader(viaHeader);

                optionsRequest.Header = header;

                return optionsRequest;
            }
Пример #11
0
        /// <summary>
        /// Looks up a person contact in the 37 Signals Highrise application.
        /// </summary>
        /// <param name="url">The URL of the Highrise account to attempt the lookup on.</param>
        /// <param name="authToken">The auth token for the Highrise account to attempt the lookup with.</param>
        /// <param name="name">The name of the person to attempt a match on.</param>
        /// <param name="addCallNote">If true it indicates a Highrise note should be created if a matching contact is found.</param>
        //public CRMHeaders LookupHighriseContact(string url, string authToken, string name, bool addCallNote, bool async)
        //{
        //    LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Looking up Highrise contact on " + url + " for " + name + ".", m_context.Owner));
        //    if (async)
        //    {
        //        m_context.SetCallerDetails(new CRMHeaders() { Pending = true });
        //        ThreadPool.QueueUserWorkItem(delegate { DoLookup(url, authToken, name, addCallNote, (result) => { m_context.SetCallerDetails(result); }); });
        //        return null;
        //    }
        //    else
        //    {
        //        return DoLookup(url, authToken, name, addCallNote, null);
        //    }
        //}
        private CRMHeaders DoLookup(string url, string authToken, SIPFromHeader from, bool addCallNote, Action<CRMHeaders> callback)
        {
            try
            {
                string searchString = null;
                string lookupType = null;

                if (from.FromName != null && Regex.Match(from.FromName, @"\D").Success)
                {
                    // The From display name has a non-digit character do a name lookup.
                    lookupType = "name";
                    searchString = from.FromName.Trim();
                }
                else if (from.FromName != null)
                {
                    // The From display name is all digits do a phone number lookup.
                    lookupType = "phonenumber";
                    searchString = from.FromName.Trim();
                }
                else if (!Regex.Match(from.FromURI.User, @"\D").Success)
                {
                    // The From URI user is all digits do a phone number lookup.
                    lookupType = "phonenumber";
                    searchString = from.FromURI.User.Trim();
                }
                else
                {
                    // Last resort is to do a SIP URI lookup.
                    lookupType = "sipaddress";
                    searchString = from.FromURI.ToAOR();
                }

                LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Highrise contact lookup type " + lookupType + " commencing for " + searchString + ".", m_context.Owner));

                CRMHeaders result = null;
                DateTime startLookup = DateTime.Now;

                PersonRequest personRequest = new PersonRequest(url, authToken);
                People people = null;

                if (lookupType == "name")
                {
                    people = personRequest.GetByName(searchString);
                }
                else if (lookupType == "phonenumber")
                {
                    people = personRequest.GetByPhoneNumber(searchString);
                }
                else if (lookupType == "sipaddress")
                {
                    people = personRequest.GetByCustomField("sip_address", searchString);
                }

                if (people != null && people.PersonList != null && people.PersonList.Count > 0)
                {
                    Person person = people.PersonList[0];
                    string companyName = null;

                    if (person.CompanyID != null)
                    {
                        CompanyRequest companyRequest = new CompanyRequest(url, authToken);
                        Company company = companyRequest.GetByID(person.CompanyID.Value);

                        if (company != null)
                        {
                            companyName = company.Name;
                        }
                    }

                    double secondsDuration = DateTime.Now.Subtract(startLookup).TotalSeconds;

                    if (companyName != null)
                    {
                        LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Highrise contact match " + person.FirstName + " " + person.LastName + " of " + companyName + ", time taken " + secondsDuration.ToString("0.##") + "s.", m_context.Owner));
                    }
                    else
                    {
                        LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Highrise contact match " + person.FirstName + " " + person.LastName + ", time taken " + secondsDuration.ToString("0.##") + "s.", m_context.Owner));
                    }
                    //m_context.SetCallerDetails(new CRMHeaders(person.FirstName + " " + person.LastName, companyName, person.AvatarURL));
                    string personName = (!person.LastName.IsNullOrBlank()) ? person.FirstName + " " + person.LastName : person.FirstName;
                    result = new CRMHeaders(personName, companyName, person.AvatarURL);

                    if (addCallNote)
                    {
                        ThreadPool.QueueUserWorkItem(delegate { AddHighriseCallNote(url, authToken, from, person); });
                    }
                }
                else
                {
                    double secondsDuration = DateTime.Now.Subtract(startLookup).TotalSeconds;

                    LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "No Highrise contact match, time taken " + secondsDuration.ToString("0.##") + "s.", m_context.Owner));
                    result = new CRMHeaders() { Pending = false, LookupError = "No Highrise contact match." };
                }

                if (callback != null)
                {
                    callback(result);
                }

                return result;
            }
            catch (Exception excp)
            {
                logger.Error("Exception LookupHighriseContact. " + excp.Message);
                LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Error looking up Highrise contact.", m_context.Owner));

                var errorResult = new CRMHeaders() { Pending = false, LookupError = "Error looking up Highrise contact." };

                if (callback != null)
                {
                    callback(errorResult);
                }

                return errorResult;
            }
        }
Пример #12
0
        private void AddHighriseCallNote(string url, string authToken, SIPFromHeader caller, Person person)
        {
            try
            {
                NoteRequest request = new NoteRequest(url, authToken);
                string result = request.CreateNoteForPerson(person.ID, HttpUtility.HtmlEncode("Incoming SIP call as " + caller.FromUserField.ToParameterlessString() + " at " + DateTime.UtcNow.ToString("dd MMM yyyy HH:mm:ss") + " UTC."));
                string personName = person.FirstName + " " + person.LastName;

                if(result != null)
                {
                    LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Highrise call received note added for " + personName + ".", m_context.Owner));
                }
                else
                {
                    LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Failed to add Highrise call received note for " + personName + ".", m_context.Owner));
                }
            }
            catch (Exception excp)
            {
                LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Exception attempting to add Highrise call received note. " + excp.Message, m_context.Owner));
            }
        }
Пример #13
0
        /// <summary>
        /// Attempts to retrieve the CRM Account details for a dial plan.
        /// </summary>
        //public CRMAccount GetCRMAccount(string dialPlanName)
        //{
        //    try
        //    {
        //        using (SIPSorceryEntities entities = new SIPSorceryEntities())
        //        {
        //            return (from crmAcc in entities.CRMAccounts1
        //                    join dialplan in entities.SIPDialPlans on crmAcc.Owner equals dialplan.Owner
        //                    where dialplan.DialPlanName == dialPlanName && crmAcc.Owner == m_context.Owner
        //                    select crmAcc).FirstOrDefault();
        //        }
        //    }
        //    catch (Exception excp)
        //    {
        //        logger.Error("Exception GetCRMAccount. " + excp.Message);
        //        LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Error retrieving CRM Account details for dialplan.", m_context.Owner));
        //        return null;
        //    }
        //}
        /// <summary>
        /// Looks up a person contact in the 37 Signals Highrise application.
        /// </summary>
        /// <param name="url">The URL of the Highrise account to attempt the lookup on.</param>
        /// <param name="authToken">The auth token for the Highrise account to attempt the lookup with.</param>
        /// <param name="from">The SIP from header of the incoming call to attempt to match on.</param>
        /// <param name="addCallNote">If true it indicates a Highrise note should be created if a matching contact is found.</param>
        public CRMHeaders LookupHighriseContact(string url, string authToken, SIPFromHeader from, bool addCallNote, bool async)
        {
            LogToMonitor(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Looking up Highrise contact on " + url + ".", m_context.Owner));

            if (async)
            {
                m_context.SetCallerDetails(new CRMHeaders() { Pending = true });
                ThreadPool.QueueUserWorkItem(delegate { DoLookup(url, authToken, from, addCallNote, (result) => { m_context.SetCallerDetails(result); }); });
                return null;
            }
            else
            {
                return DoLookup(url, authToken, from, addCallNote, null);
            }
        }
 /// <summary>
 /// Looks up a person contact in the 37 Signals Highrise application.
 /// </summary>
 /// <param name="url">The URL of the Highrise account to attempt the lookup on.</param>
 /// <param name="authToken">The auth token for the Highrise account to attempt the lookup with.</param>
 /// <param name="from">The SIP from header of the incoming call to attempt to match on.</param>
 /// <param name="addCallNote">If true it indicates a Highrise note should be created if a matching contact is found.</param>
 public void LookupHighriseContact(string url, string authToken, SIPFromHeader from, bool addCallNote)
 {
     LookupHighriseContact(url, authToken, from.FromName, addCallNote);
 }
Пример #15
0
        public SIPRequest GetRequest(SIPMethodsEnum method, SIPURI uri, SIPToHeader to, SIPEndPoint localSIPEndPoint)
        {
            if (localSIPEndPoint == null)
            {
                localSIPEndPoint = GetDefaultSIPEndPoint();
            }

            SIPRequest request = new SIPRequest(method, uri);
            request.LocalSIPEndPoint = localSIPEndPoint;

            SIPContactHeader contactHeader = new SIPContactHeader(null, new SIPURI(SIPSchemesEnum.sip, localSIPEndPoint));
            if (!String.IsNullOrWhiteSpace(m_contactGruu))
            {
                contactHeader.ContactURI.Parameters.Set("gr", m_contactGruu);
            }

            SIPFromHeader fromHeader = new SIPFromHeader(null, contactHeader.ContactURI, CallProperties.CreateNewTag());
            SIPHeader header = new SIPHeader(contactHeader, fromHeader, to, 1, CallProperties.CreateNewCallId());
            request.Header = header;
            header.CSeqMethod = method;
            header.Allow = ALLOWED_SIP_METHODS;

            if (m_serviceRoute != null)
                header.Routes.AddBottomRoute(m_serviceRoute);

            SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint, CallProperties.CreateBranchId());
            header.Vias.PushViaHeader(viaHeader);

            return request;
        }