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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Sep 9 17:03:01 CDT 2013


Author: mmichelson
Date: Mon Sep  9 17:02:59 2013
New Revision: 398696

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=398696
Log:
Map registrations to pjsip configuration.


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=398696&r1=398695&r2=398696
==============================================================================
--- 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 Mon Sep  9 17:02:59 2013
@@ -712,6 +712,148 @@
             set_value('md5_cred', md5, section, pjsip, nmapped, 'auth')
             set_value('auth_type', 'md5', section, pjsip, nmapped, 'auth')
 
+class Registration:
+    def __init__(self, line, retry_interval, max_attempts):
+        self.retry_interval = retry_interval
+        self.max_attempts = max_attempts
+        self.parse(line)
+
+    def parse(self, line):
+        # register =>
+        # [peer?][transport://]user[@domain][:secret[:authuser]]@host[:port][/extension][~expiry]
+
+        prehost, at, host_part = line.rpartition('@')
+        if not prehost:
+            raise
+
+        self.parse_host_part(host_part)
+        self.parse_user_part(prehost)
+
+    def parse_host_part(self, host_part):
+       pre_expiry, sep, expiry = host_part.partition('~')
+       pre_extension, sep, self.extension = pre_expiry.partition('/')
+       self.host, sep, self.port = pre_extension.partition(':')
+
+       self.expiry = expiry if expiry else '120'
+
+    def parse_user_part(self, user_part):
+        colons = user_part.count(':')
+        if (colons == 3):
+            # :domainport:secret:authuser
+            pre_auth, sep, port_auth = user_part.partition(':')
+            self.domainport, sep, auth = port_auth.partition(':')
+            self.secret, sep, self.authuser = auth.partition(':')
+        elif (colons == 2):
+            # :secret:authuser
+            pre_auth, sep, auth = user_part.partition(':')
+            self.secret, sep, self.authuser = auth.partition(':')
+        elif (colons == 1):
+            # :secret
+            pre_auth, sep, self.secret = user_part.partition(':')
+        elif (colons == 0):
+            # No port, secret, or authuser
+            pre_auth = user_part
+        else:
+            # Invalid setting
+            raise
+
+        pre_domain, sep, self.domain = pre_auth.partition('@')
+        self.peer, sep, post_peer = pre_domain.rpartition('?')
+        transport, sep, self.user = post_peer.rpartition('://')
+
+        self.protocol = transport if transport else 'udp'
+
+    def write(self, pjsip, nmapped):
+        # Most of the data in self will get written to a registration section.
+        # However, there will also need to be an auth section created if a
+        # secret or authuser is present.
+
+        # General mapping of values:
+        # A combination of self.host and self.port is server_uri
+        # A combination of self.user, self.domain, and self.domainport is
+        #   client_uri
+        # self.expiry is expiration
+        # self.extension is contact_user
+        # self.protocol will map to one of the mapped transports
+        # self.secret and self.authuser will result in a new auth section, and
+        #   outbound_auth will point to that section.
+        # XXX self.peer really doesn't map to anything :(
+
+        section = 'reg_' + self.host
+
+        set_value('retry_interval', self.retry_interval, section, pjsip,
+                  nmapped, 'registration')
+        set_value('max_retries', self.max_attempts, section, pjsip, nmapped,
+                  'registration')
+        if self.extension:
+            set_value('contact_user', self.extension, section, pjsip, nmapped,
+                      'registration')
+        
+        set_value('expiration', self.expiry, section, pjsip, nmapped,
+                  'registration')
+
+        if self.protocol == 'udp':
+            set_value('transport', 'transport-udp', section, pjsip, nmapped,
+                      'registration')
+        elif self.protocol == 'tcp':
+            set_value('transport', 'transport-tcp', section, pjsip, nmapped,
+                      'registration')
+        elif self.protocol == 'tls':
+            set_value('transport', 'transport-tls', section, pjsip, nmapped,
+                      'registration')
+
+        auth_section = 'auth_reg_' + self.host
+
+        if self.secret:
+            set_value('password', self.secret, auth_section, pjsip, nmapped,
+                      'auth')
+            set_value('username', self.authuser or self.user, auth_section,
+                      pjsip, nmapped, 'auth')
+            set_value('outbound_auth', auth_section, section, pjsip, nmapped,
+                      'registration')
+
+        client_uri = "sip:%s@" % self.user
+        if self.domain:
+            client_uri += self.domain
+        else:
+            client_uri += self.host
+
+        if self.domainport:
+            client_uri += ":" + self.domainport
+        elif self.port:
+            client_uri += ":" + self.port
+
+        set_value('client_uri', client_uri, section, pjsip, nmapped,
+                  'registration')
+
+        server_uri = "sip:%s" % self.host
+        if self.port:
+            server_uri += ":" + self.port
+
+        set_value('server_uri', server_uri, section, pjsip, nmapped,
+                  'registration')
+
+
+def map_registrations(sip, pjsip, nmapped):
+    try:
+        regs = sip.get('general', 'register')
+    except LookupError:
+        return
+
+    try:
+        retry_interval = sip.get('general', 'registertimeout')[0]
+    except LookupError:
+        retry_interval = '20'
+
+    try:
+        max_attempts = sip.get('general', 'registerattempts')[0]
+    except LookupError:
+        max_attempts = '10'
+
+    for i in regs:
+        reg = Registration(i, retry_interval, max_attempts)
+        reg.write(pjsip, nmapped)
+
 def map_peer(sip, section, pjsip, nmapped):
     for i in peer_map:
         try:
@@ -745,6 +887,7 @@
     # First thing we need to do is establish transports.
     map_transports(sip, pjsip, nmapped)
     map_auth(sip, pjsip, nmapped)
+    map_registrations(sip, pjsip, nmapped)
     for section in sip.sections():
         if section == 'authentication':
             pass




More information about the asterisk-commits mailing list