public static SIPEndPoint ParseSIPEndPoint(string sipEndPointStr) { try { if (sipEndPointStr.IsNullOrBlank()) { return(null); } if (sipEndPointStr.StartsWith("udp") || sipEndPointStr.StartsWith("tcp") || sipEndPointStr.StartsWith("tls")) { return(ParseSerialisedSIPEndPoint(sipEndPointStr)); } string ipAddress = null; int port = 0; SIPProtocolsEnum protocol = SIPProtocolsEnum.udp; if (sipEndPointStr.StartsWith("sip:")) { sipEndPointStr = sipEndPointStr.Substring(4); } else if (sipEndPointStr.StartsWith("sips:")) { sipEndPointStr = sipEndPointStr.Substring(5); protocol = SIPProtocolsEnum.tls; } int colonIndex = sipEndPointStr.IndexOf(':'); int semiColonIndex = sipEndPointStr.IndexOf(';'); if (colonIndex == -1 && semiColonIndex == -1) { ipAddress = sipEndPointStr; } else if (colonIndex != -1 && semiColonIndex == -1) { ipAddress = sipEndPointStr.Substring(0, colonIndex); port = Convert.ToInt32(sipEndPointStr.Substring(colonIndex + 1)); } else { if (colonIndex != -1 && colonIndex < semiColonIndex) { ipAddress = sipEndPointStr.Substring(0, colonIndex); port = Convert.ToInt32(sipEndPointStr.Substring(colonIndex + 1, semiColonIndex - colonIndex - 1)); } else { ipAddress = sipEndPointStr.Substring(0, semiColonIndex); } if (protocol != SIPProtocolsEnum.tls) { sipEndPointStr = sipEndPointStr.Substring(semiColonIndex + 1); int transportIndex = sipEndPointStr.ToLower().IndexOf(m_transportParameterKey + "="); if (transportIndex != -1) { sipEndPointStr = sipEndPointStr.Substring(transportIndex + 10); semiColonIndex = sipEndPointStr.IndexOf(';'); if (semiColonIndex != -1) { protocol = SIPProtocolsType.GetProtocolType(sipEndPointStr.Substring(0, semiColonIndex)); } else { protocol = SIPProtocolsType.GetProtocolType(sipEndPointStr); } } } } if (port == 0) { port = (protocol == SIPProtocolsEnum.tls) ? m_defaultSIPTLSPort : m_defaultSIPPort; } return(new SIPEndPoint(protocol, IPAddress.Parse(ipAddress), port)); } catch //(Exception excp) { //logger.Error("Exception ParseSIPEndPoint (sipEndPointStr=" + sipEndPointStr + "). " + excp.Message); throw; } }
public static List <SIPChannel> ParseSIPChannelsNode(XmlNode sipChannelsNode, int port = 0) { var sipChannels = new List <SIPChannel>(); foreach (XmlNode sipSocketNode in sipChannelsNode.ChildNodes) { logger.Debug("Creating SIP Channel for " + sipSocketNode.OuterXml + "."); var localSocket = sipSocketNode.InnerText; var protocol = SIPProtocolsEnum.udp; if (sipSocketNode.Attributes.GetNamedItem(SIP_PROTOCOL_PARAMETER) != null) { protocol = SIPProtocolsType.GetProtocolType(sipSocketNode.Attributes.GetNamedItem(SIP_PROTOCOL_PARAMETER).Value); } var nodeSIPEndPoints = GetSIPEndPoints(localSocket, protocol, port); foreach (var sipEndPoint in nodeSIPEndPoints) { try { switch (protocol) { case SIPProtocolsEnum.udp: { logger.Debug(" attempting to create SIP UDP channel for " + sipEndPoint.GetIPEndPoint() + "."); var udpChannel = new SIPUDPChannel(sipEndPoint.GetIPEndPoint()); sipChannels.Add(udpChannel); } break; case SIPProtocolsEnum.tcp: { logger.Debug(" attempting to create SIP TCP channel for " + sipEndPoint.GetIPEndPoint() + "."); var tcpChannel = new SIPTCPChannel(sipEndPoint.GetIPEndPoint()); sipChannels.Add(tcpChannel); } break; case SIPProtocolsEnum.tls: if (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_PATH_PARAMETER) == null) { logger.Warn("Could not create SIPTLSChannel from XML configuration node as no " + CERTIFICATE_PATH_PARAMETER + " attribute was present."); } else { var certificateType = "machinestore"; if (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_TYPE_PARAMETER) != null) { certificateType = sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_TYPE_PARAMETER).Value; } var certificatePath = (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_PATH_PARAMETER) != null) ? sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_PATH_PARAMETER).Value : null; var certificateKeyPassword = (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_KEY_PASSWORD_PARAMETER) != null) ? sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_KEY_PASSWORD_PARAMETER).Value : String.Empty; logger.Debug(" attempting to create SIP TLS channel for " + sipEndPoint.GetIPEndPoint() + " and certificate type of " + certificateType + " at " + certificatePath + "."); var certificate = LoadCertificate(certificateType, certificatePath, certificateKeyPassword); if (certificate != null) { var tlsChannel = new SIPTLSChannel(certificate, sipEndPoint.GetIPEndPoint()); sipChannels.Add(tlsChannel); } else { logger.Warn("A SIP TLS channel was not created because the certificate could not be loaded."); } } break; default: logger.Warn("Could not create a SIP channel for protocol " + protocol + "."); break; } } catch (Exception excp) { logger.Warn("Exception SIPTransportConfig Adding SIP Channel for " + sipEndPoint + ". " + excp.Message); } } } return(sipChannels); }
/// <summary> /// Reverses ToString(). /// </summary> /// <param name="serialisedSIPEndPoint">The serialised SIP end point MUST be in the form protocol:socket and protocol must /// be exactly 3 characters. Valid examples are udp:10.0.0.1:5060, invalid example is 10.0.0.1:5060.</param> private static SIPEndPoint ParseSerialisedSIPEndPoint(string serialisedSIPEndPoint) { return(new SIPEndPoint(SIPProtocolsType.GetProtocolType(serialisedSIPEndPoint.Substring(0, 3)), IPSocket.ParseSocketString(serialisedSIPEndPoint.Substring(4)))); }