[asterisk-commits] mmichelson: branch mmichelson/conversion_script r398279 - /team/mmichelson/co...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Sep 4 15:11:20 CDT 2013


Author: mmichelson
Date: Wed Sep  4 15:11:18 2013
New Revision: 398279

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=398279
Log:
Set externaddr on transports.


Modified:
    team/mmichelson/conversion_script/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py

Modified: team/mmichelson/conversion_script/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/conversion_script/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py?view=diff&rev=398279&r1=398278&r2=398279
==============================================================================
--- team/mmichelson/conversion_script/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py (original)
+++ team/mmichelson/conversion_script/contrib/scripts/sip_to_pjsip/sip_to_pjsip.py Wed Sep  4 15:11:18 2013
@@ -28,7 +28,6 @@
     except LookupError:
         # section for type doesn't exist, so add
         sect = pjsip.add_section(section)
-        print("Created new section called %s" % section)
         sect['type'] = type
         return sect
 
@@ -53,22 +52,16 @@
     def _merge_value(k, v, s, r, n):
         merge_value(key if key else k, v, s, r, n, type, section_to)
 
-    print ("Starting merge_value for %s: %s" % (key, val))
-
     # if no value or section return the merge_value
     # function with the enclosed key and type
     if not val and not section:
         return _merge_value
 
-    print("Made it past early return for %s: %s" % (key, val))
-
     # should return a single value section list
     try:
         sect = sip.section(section)[0]
     except LookupError:
         sect = sip.default(section)[0]
-
-    print("Got here for %s: %s" % (key, val))
     # for each merged value add it to pjsip.conf
     for i in sect.get_merged(key):
         set_value(key, i, section_to if section_to else section,
@@ -181,7 +174,7 @@
         val = 'no'
     set_value('inband_progress', val, section, pjsip, nmapped)
 
-def get_host(config, host, section, port_key):
+def build_host(config, host, section, port_key):
     """
     Returns a string composed of a host:port. This assumes that the host
     may have a port as part of the initial value. The port_key is only used
@@ -240,7 +233,7 @@
         # It's fine if there's no user name
         pass
 
-    result += get_host(sip, val, section, 'port')
+    result += build_host(sip, val, section, 'port')
 
     set_value('contact', result, section, pjsip, nmapped, 'aor')
 
@@ -384,6 +377,61 @@
         # No localnet options configured. No biggie!
         pass
 
+def set_transport_common(section, pjsip, nmapped):
+    """
+    sip.conf has several global settings that in pjsip.conf apply to individual
+    transports. This function adds these global settings to each individual
+    transport.
+
+    The settings included are:
+    localnet
+    tos_sip
+    cos_sip
+    """
+
+    try:
+        merge_value('localnet', sip.get('general', 'localnet')[0], 'general',
+                pjsip, nmapped, 'transport', section)
+    except LookupError:
+        # No localnet options configured. Move on.
+        pass
+
+    try:
+        set_value('tos', sip.get('general', 'sip_tos')[0], 'general', pjsip,
+            nmapped, 'transport', section)
+    except LookupError:
+        pass
+
+    try:
+        set_value('cos', sip.get('general', 'sip_cos')[0], 'general', pjsip,
+                nmapped, 'transport', section)
+    except LookupError:
+        pass
+
+def split_hostport(addr):
+    try:
+        socket.inet_pton(socket.AF_INET6, addr)
+        if not host.startswith('['):
+            return (addr, None)
+        else:
+            # If brackets are present, there may be a port as well
+            port = re.match('\[.*\]:(\d+)', host)
+            if port:
+                return (host, port.group(1))
+            else:
+                return (host, None)
+    except socket.error:
+        pass
+
+    # IPv4 address or hostname
+    host, sep, port = addr.rpartition(':')
+
+    if not host and not sep:
+        return port
+    else:
+        return (host, port)
+
+
 def create_udp(sip, pjsip, nmapped):
     """
     Creates a 'transport-udp' section in the pjsip.conf file based
@@ -396,13 +444,23 @@
     """
 
     bind = sip.multi_get('general', ['udpbindaddr', 'bindaddr'])[0]
-    bind = get_host(sip, bind, 'general', 'bindport')
+    bind = build_host(sip, bind, 'general', 'bindport')
+
+    try:
+        extern_addr = sip.multi_get('general', ['externaddr', 'externip',
+            'externhost'])[0]
+        host, port = split_hostport(extern_addr)
+        set_value('external_signaling_address', host, 'transport-udp', pjsip,
+                nmapped, 'transport')
+        if port:
+            set_value('external_signaling_port', port, 'transport-udp', pjsip,
+                    nmapped, 'transport')
+    except LookupError:
+        pass
 
     set_value('protocol', 'udp', 'transport-udp', pjsip, nmapped, 'transport')
     set_value('bind', bind, 'transport-udp', pjsip, nmapped, 'transport')
-    add_localnet('transport-udp', pjsip, nmapped)
-
-    #XXX Add externaddr, externip, externhost...
+    set_transport_common('transport-udp', pjsip, nmapped)
 
 def create_tcp(sip, pjsip, nmapped):
     """
@@ -425,14 +483,30 @@
 
     try:
         bind = sip.get('general', 'tcpbindaddr')[0]
-        bind = get_host(sip, bind, 'general', 'bindport')
+        bind = build_host(sip, bind, 'general', 'bindport')
     except LookupError:
         # No tcpbindaddr means to default to the udpbindaddr
         bind = pjsip.get('transport-udp', 'bind')[0]
 
+    try:
+        extern_addr = sip.multi_get('general', ['externaddr', 'externip',
+            'externhost'])[0]
+        host, port = split_hostport(extern_addr)
+        try:
+            tcpport = sip.get('general', 'externtcpport')
+        except:
+            tcpport = port
+        set_value('external_signaling_address', host, 'transport-tcp', pjsip,
+                nmapped, 'transport')
+        if tcpport:
+            set_value('external_signaling_port', tcpport, 'transport-tcp', pjsip,
+                    nmapped, 'transport')
+    except LookupError:
+        pass
+
     set_value('protocol', 'tcp', 'transport-tcp', pjsip, nmapped, 'transport')
     set_value('bind', bind, 'transport-tcp', pjsip, nmapped, 'transport')
-    add_localnet('transport-tcp', pjsip, nmapped)
+    set_transport_common('transport-tcp', pjsip, nmapped)
 
     #XXX Add externtcpport...
 
@@ -531,9 +605,26 @@
 
     for i in tls_map:
         try:
-            i[1](sip.multi_get('general', i[0]), pjsip, nmapped)
+            i[1](sip.multi_get('general', i[0])[0], pjsip, nmapped)
         except LookupError:
             pass
+
+    set_transport_common('transport-tls', pjsip, nmapped)
+    try:
+        extern_addr = sip.multi_get('general', ['externaddr', 'externip',
+            'externhost'])[0]
+        host, port = split_hostport(extern_addr)
+        try:
+            tlsport = sip.get('general', 'externtlsport')
+        except:
+            tlsport = port
+        set_value('external_signaling_address', host, 'transport-tls', pjsip,
+                nmapped, 'transport')
+        if tlsport:
+            set_value('external_signaling_port', tlsport, 'transport-tls', pjsip,
+                    nmapped, 'transport')
+    except LookupError:
+        pass
 
 
 def map_transports(sip, pjsip, nmapped):




More information about the asterisk-commits mailing list