Przeglądaj źródła

modules/rtpproxy: Add database support to rtpproxy

- If db_url modparam is set rtpproxy sets will be loaded from database
- If the instance is marked as disabled in database it will be loaded as disabled

- Currently no persistence (enable/disable MI command does not affect DB)
- Loaded during mod_init - cannot be reloaded at runtime (as before)
Hugh Waite 12 lat temu
rodzic
commit
9a0e58667f

+ 1 - 0
modules/rtpproxy/Makefile

@@ -15,5 +15,6 @@ DEFS+=-DKAMAILIO_MOD_INTERFACE
 SERLIBPATH=../../lib
 SER_LIBS+=$(SERLIBPATH)/kmi/kmi
 SER_LIBS+=$(SERLIBPATH)/kcore/kcore
+SER_LIBS+=$(SERLIBPATH)/srdb1/srdb1
 include ../../Makefile.modules
 

+ 285 - 243
modules/rtpproxy/README

@@ -1,3 +1,4 @@
+
 rtpproxy Module
 
 Maxim Sobolev
@@ -30,14 +31,14 @@ Carsten Bock
 
    ng-voice GmbH
 
-   Copyright © 2003-2008 Sippy Software, Inc.
+   Copyright © 2003-2008 Sippy Software, Inc.
 
-   Copyright © 2005 Voice Sistem SRL
+   Copyright © 2005 Voice Sistem SRL
 
-   Copyright © 2009-2012 TuTPro Inc.
+   Copyright © 2009-2012 TuTPro Inc.
 
-   Copyright © 2010 VoIPEmbedded Inc.
-     __________________________________________________________________
+   Copyright © 2010 VoIPEmbedded Inc.
+     _________________________________________________________________
 
    Table of Contents
 
@@ -60,20 +61,22 @@ Carsten Bock
               4.6. timeout_socket (string)
               4.7. ice_candidate_priority_avp (string)
               4.8. extra_id_pv (string)
+              4.9. db_url (string)
+              4.10. table_name (string)
 
         5. Functions
 
-              5.1. set_rtp_proxy_set(setid)
-              5.2. rtpproxy_offer([flags [, ip_address]])
-              5.3. rtpproxy_answer([flags [, ip_address]])
-              5.4. rtpproxy_destroy([flags])
-              5.5. unforce_rtp_proxy()
-              5.6. rtpproxy_manage([flags [, ip_address]])
-              5.7. rtpproxy_stream2uac(prompt_name, count),
-              5.8. rtpproxy_stream2uas(prompt_name, count)
-              5.9. rtpproxy_stop_stream2uac(),
-              5.10. start_recording()
-              5.11. rtpproxy_stop_stream2uas(prompt_name, count)
+              5.1. set_rtp_proxy_set(setid) 
+              5.2. rtpproxy_offer([flags [, ip_address]]) 
+              5.3. rtpproxy_answer([flags [, ip_address]]) 
+              5.4. rtpproxy_destroy([flags]) 
+              5.5. unforce_rtp_proxy() 
+              5.6. rtpproxy_manage([flags [, ip_address]]) 
+              5.7. rtpproxy_stream2uac(prompt_name, count), 
+              5.8. rtpproxy_stream2uas(prompt_name, count) 
+              5.9. rtpproxy_stop_stream2uac(), 
+              5.10. start_recording() 
+              5.11. rtpproxy_stop_stream2uas(prompt_name, count) 
 
         6. Exported Pseudo Variables
 
@@ -96,16 +99,18 @@ Carsten Bock
    1.6. Set timeout_socket parameter
    1.7. Set ice_candidate_priority_avp parameter
    1.8. Set extra_id_pv parameter
-   1.9. set_rtp_proxy_set usage
-   1.10. rtpproxy_offer usage
-   1.11. rtpproxy_answer usage
-   1.12. rtpproxy_destroy usage
-   1.13. rtpproxy_manage usage
-   1.14. rtpproxy_stream2xxx usage
-   1.15. start_recording usage
-   1.16. $rtpstat-Usage
-   1.17. nh_enable_rtpp usage
-   1.18. nh_show_rtpp usage
+   1.9. Set db_url parameter
+   1.10. Set table_name parameter
+   1.11. set_rtp_proxy_set usage
+   1.12. rtpproxy_offer usage
+   1.13. rtpproxy_answer usage
+   1.14. rtpproxy_destroy usage
+   1.15. rtpproxy_manage usage
+   1.16. rtpproxy_stream2xxx usage
+   1.17. start_recording usage
+   1.18. $rtpstat-Usage
+   1.19. nh_enable_rtpp usage
+   1.20. nh_show_rtpp usage
 
 Chapter 1. Admin Guide
 
@@ -128,20 +133,22 @@ Chapter 1. Admin Guide
         4.6. timeout_socket (string)
         4.7. ice_candidate_priority_avp (string)
         4.8. extra_id_pv (string)
+        4.9. db_url (string)
+        4.10. table_name (string)
 
    5. Functions
 
-        5.1. set_rtp_proxy_set(setid)
-        5.2. rtpproxy_offer([flags [, ip_address]])
-        5.3. rtpproxy_answer([flags [, ip_address]])
-        5.4. rtpproxy_destroy([flags])
-        5.5. unforce_rtp_proxy()
-        5.6. rtpproxy_manage([flags [, ip_address]])
-        5.7. rtpproxy_stream2uac(prompt_name, count),
-        5.8. rtpproxy_stream2uas(prompt_name, count)
-        5.9. rtpproxy_stop_stream2uac(),
-        5.10. start_recording()
-        5.11. rtpproxy_stop_stream2uas(prompt_name, count)
+        5.1. set_rtp_proxy_set(setid) 
+        5.2. rtpproxy_offer([flags [, ip_address]]) 
+        5.3. rtpproxy_answer([flags [, ip_address]]) 
+        5.4. rtpproxy_destroy([flags]) 
+        5.5. unforce_rtp_proxy() 
+        5.6. rtpproxy_manage([flags [, ip_address]]) 
+        5.7. rtpproxy_stream2uac(prompt_name, count), 
+        5.8. rtpproxy_stream2uas(prompt_name, count) 
+        5.9. rtpproxy_stop_stream2uac(), 
+        5.10. start_recording() 
+        5.11. rtpproxy_stop_stream2uas(prompt_name, count) 
 
    6. Exported Pseudo Variables
 
@@ -154,37 +161,37 @@ Chapter 1. Admin Guide
 
 1. Overview
 
-   This is a module that enables media streams to be proxied via an
-   rtpproxy. Rtpproxies know to work with this module are Sippy RTPproxy
+   This  is  a  module  that  enables  media streams to be proxied via an
+   rtpproxy.  Rtpproxies know to work with this module are Sippy RTPproxy
    http://www.rtpproxy.org and ngcp-rtpproxy-ng
-   http://deb.sipwise.com/spce/2.6/pool/main/n/ngcp-mediaproxy-ng. Some
-   features of the rtpproxy module apply only to one of the two
+   http://deb.sipwise.com/spce/2.6/pool/main/n/ngcp-mediaproxy-ng.   Some
+   features  of  the  rtpproxy  module  apply  only  to  one  of  the two
    rtpproxies.
 
 2. Multiple RTPProxy usage
 
-   The rtpproxy module can support multiple rtpproxies for
+   The    rtpproxy   module   can   support   multiple   rtpproxies   for
    balancing/distribution and control/selection purposes.
 
-   The module allows definition of several sets of rtpproxies.
-   Load-balancing will be performed over a set and the admin has the
+   The   module   allows   definition  of  several  sets  of  rtpproxies.
+   Load-balancing  will  be  performed  over  a set and the admin has the
    ability to choose what set should be used. The set is selected via its
-   id - the id being defined with the set. Refer to the “rtpproxy_sock�
+   id  -  the id being defined with the set. Refer to the "rtpproxy_sock"
    module parameter definition for syntax description.
 
-   The balancing inside a set is done automatically by the module based on
-   the weight of each rtpproxy from the set.
+   The  balancing  inside a set is done automatically by the module based
+   on the weight of each rtpproxy from the set.
 
-   The selection of the set is done from script prior using
+   The   selection   of   the   set  is  done  from  script  prior  using
    unforce_rtp_proxy(), rtpproxy_offer() or rtpproxy_answer() functions -
    see the set_rtp_proxy_set() function.
 
-   For backward compatibility reasons, a set with no id take by default
+   For  backward  compatibility reasons, a set with no id take by default
    the id 0. Also if no set is explicitly set before unforce_rtp_proxy(),
    rtpproxy_offer() or rtpproxy_answer() the 0 id set will be used.
 
-   IMPORTANT: if you use multiple sets, take care and use the same set for
-   both rtpproxy_offer()/rtpproxy_answer() and unforce_rtpproxy()!!
+   IMPORTANT:  if  you  use multiple sets, take care and use the same set
+   for both rtpproxy_offer()/rtpproxy_answer() and unforce_rtpproxy()!!
 
 3. Dependencies
 
@@ -199,7 +206,7 @@ Chapter 1. Admin Guide
 
 3.2. External Libraries or Applications
 
-   The following libraries or applications must be installed before
+   The  following  libraries  or  applications  must  be installed before
    running Kamailio with this module loaded:
      * None.
 
@@ -213,21 +220,31 @@ Chapter 1. Admin Guide
    4.6. timeout_socket (string)
    4.7. ice_candidate_priority_avp (string)
    4.8. extra_id_pv (string)
+   4.9. db_url (string)
+   4.10. table_name (string)
 
 4.1. rtpproxy_sock (string)
 
-   Definition of socket(s) used to connect to (a set) RTPProxy. It may
-   specify a UNIX socket or an IPv4/IPv6 UDP socket.
+   Used to define the list of RTPPRoxy instances to connect to. These can
+   be  UNIX  sockets  or  IPv4/IPv6 UDP sockets. Each modparam entry will
+   insert  sockets  into a single set. If no set ID is given, the default
+   set ID '0' will be used. To define multiple sets add the set number at
+   the  beginning  of  each  parameter  followed  by '=='. Sockets can be
+   weighted  by  adding  '=#' to a socket where # is an integer. A socket
+   with  a weight of 2 will be chosen twice as often as one with a weight
+   of 1.
 
-   Default value is “NONE� (disabled).
+   Default value is "NONE" (disabled). 
 
    Example 1.1. Set rtpproxy_sock parameter
 ...
 # single rtproxy
 modparam("rtpproxy", "rtpproxy_sock", "udp:localhost:12221")
+
 # multiple rtproxies for LB
 modparam("rtpproxy", "rtpproxy_sock",
         "udp:localhost:12221 udp:localhost:12222")
+
 # multiple sets of multiple rtproxies
 modparam("rtpproxy", "rtpproxy_sock",
         "1 == udp:localhost:12221 udp:localhost:12222")
@@ -237,11 +254,11 @@ modparam("rtpproxy", "rtpproxy_sock",
 
 4.2. rtpproxy_disable_tout (integer)
 
-   Once RTPProxy was found unreachable and marked as disabled, the
-   rtpproxy module will not attempt to establish communication to RTPProxy
-   for rtpproxy_disable_tout seconds.
+   Once  RTPProxy  was  found  unreachable  and  marked  as disabled, the
+   rtpproxy  module  will  not  attempt  to  establish  communication  to
+   RTPProxy for rtpproxy_disable_tout seconds.
 
-   Default value is “60�.
+   Default value is "60". 
 
    Example 1.2. Set rtpproxy_disable_tout parameter
 ...
@@ -252,7 +269,7 @@ modparam("rtpproxy", "rtpproxy_disable_tout", 20)
 
    Timeout value in waiting for reply from RTPProxy.
 
-   Default value is “1�.
+   Default value is "1". 
 
    Example 1.3. Set rtpproxy_tout parameter
 ...
@@ -261,10 +278,10 @@ modparam("rtpproxy", "rtpproxy_tout", 2)
 
 4.4. rtpproxy_retr (integer)
 
-   How many times the module should retry to send and receive after
+   How  many  times  the  module  should  retry to send and receive after
    timeout was generated.
 
-   Default value is “5�.
+   Default value is "5". 
 
    Example 1.4. Set rtpproxy_retr parameter
 ...
@@ -273,8 +290,8 @@ modparam("rtpproxy", "rtpproxy_retr", 2)
 
 4.5. nortpproxy_str (string)
 
-   This parameter sets the SDP attribute used by rtpproxy to mark the
-   message's SDP attachemnt with information that it have already been
+   This  parameter  sets  the  SDP attribute used by rtpproxy to mark the
+   message's  SDP  attachemnt  with information that it have already been
    changed.
 
    If empty string, no marker will be added or checked.
@@ -283,7 +300,7 @@ Note
 
    The string must be a complete SDP line, including the EOH (\r\n).
 
-   Default value is “a=nortpproxy:yes\r\n�.
+   Default value is "a=nortpproxy:yes\r\n". 
 
    Example 1.5. Set nortpproxy_str parameter
 ...
@@ -293,30 +310,30 @@ modparam("rtpproxy", "nortpproxy_str", "a=sdpmangled:yes\r\n")
 4.6. timeout_socket (string)
 
    The parameter sets the RTP timeout socket, which is transmitted to the
-   RTP-Proxy. It will be used by the RTP proxy to signal back that a media
-   stream timed out.
+   RTP-Proxy.  It  will  be  used  by the RTP proxy to signal back that a
+   media stream timed out.
 
    If it is an empty string, no timeout socket will be transmitted to the
    RTP-Proxy.
 
-   Default value is “� (nothing).
+   Default value is "" (nothing). 
 
    Example 1.6. Set timeout_socket parameter
 ...
-modparam("nathelper", "timeout_socket", "xmlrpc:http://127.0.0.1:8000/RPC2")
+modparam("rtpproxy", "timeout_socket", "xmlrpc:http://127.0.0.1:8000/RPC2")
 ...
 
 4.7. ice_candidate_priority_avp (string)
 
-   If specified and if value of the avp value is not 0, rtpproxy_manage
-   function adds ICE relay candidate attributes to sdp stream(s)
+   If  specified  and if value of the avp value is not 0, rtpproxy_manage
+   function   adds  ICE  relay  candidate  attributes  to  sdp  stream(s)
    containing ICE candidate attributes.
 
-   If value of the avp is 1, added candidates have high priority. If value
-   of the avp is 2 (default), added candidates have low priority.
+   If  value  of  the  avp  is 1, added candidates have high priority. If
+   value of the avp is 2 (default), added candidates have low priority.
 
-   There is no default value meaning that no ICE relay candidates are
-   added in any circumstance.
+   There  is  no  default  value meaning that no ICE relay candidates are
+   added in any circumstance. 
 
    Example 1.7. Set ice_candidate_priority_avp parameter
 ...
@@ -325,42 +342,66 @@ modparam("rtpproxy", "ice_candidate_priority_avp", "$avp(ice_priority)")
 
 4.8. extra_id_pv (string)
 
-   The parameter sets the PV defination to use when the “b� parameter is
-   used on unforce_rtp_proxy(), rtpproxy_offer(), rtpproxy_answer() or
+   The  parameter sets the PV defination to use when the "b" parameter is
+   used  on  unforce_rtp_proxy(),  rtpproxy_offer(), rtpproxy_answer() or
    rtpproxy_manage() command.
 
-   Default is empty, the “b� parameter may not be used then.
+   Default is empty, the "b" parameter may not be used then.
 
    Example 1.8. Set extra_id_pv parameter
 ...
 modparam("rtpproxy", "extra_id_pv", "$avp(extra_id)")
 ...
 
+4.9. db_url (string)
+
+   The  database  URL  to  load rtp_proxy sets from. If this parameter is
+   set,  the  module  will  attempt  to  load  the rtpproxy sets from the
+   specified database and will ignore any 'rtpproxy_sock' modparams.
+
+   Default is empty, a database will not be used.
+
+   Example 1.9. Set db_url parameter
+...
+modparam("rtpproxy", "db_url", "mysql://user:passwb@localhost/database")
+...
+
+4.10. table_name (string)
+
+   The name of the table containing the rtpproxy sets.
+
+   Default value is "rtpproxy".
+
+   Example 1.10. Set table_name parameter
+...
+modparam("rtpproxy", "table_name", "my_rtpp_sets")
+...
+
 5. Functions
 
-   5.1. set_rtp_proxy_set(setid)
-   5.2. rtpproxy_offer([flags [, ip_address]])
-   5.3. rtpproxy_answer([flags [, ip_address]])
-   5.4. rtpproxy_destroy([flags])
-   5.5. unforce_rtp_proxy()
-   5.6. rtpproxy_manage([flags [, ip_address]])
-   5.7. rtpproxy_stream2uac(prompt_name, count),
-   5.8. rtpproxy_stream2uas(prompt_name, count)
-   5.9. rtpproxy_stop_stream2uac(),
-   5.10. start_recording()
-   5.11. rtpproxy_stop_stream2uas(prompt_name, count)
+   5.1. set_rtp_proxy_set(setid) 
+   5.2. rtpproxy_offer([flags [, ip_address]]) 
+   5.3. rtpproxy_answer([flags [, ip_address]]) 
+   5.4. rtpproxy_destroy([flags]) 
+   5.5. unforce_rtp_proxy() 
+   5.6. rtpproxy_manage([flags [, ip_address]]) 
+   5.7. rtpproxy_stream2uac(prompt_name, count), 
+   5.8. rtpproxy_stream2uas(prompt_name, count) 
+   5.9. rtpproxy_stop_stream2uac(), 
+   5.10. start_recording() 
+   5.11. rtpproxy_stop_stream2uas(prompt_name, count) 
 
 5.1.  set_rtp_proxy_set(setid)
 
-   Sets the Id of the rtpproxy set to be used for the next
-   unforce_rtp_proxy(), rtpproxy_offer(), rtpproxy_answer() or
+   Sets   the   Id   of  the  rtpproxy  set  to  be  used  for  the  next
+   unforce_rtp_proxy(),     rtpproxy_offer(),     rtpproxy_answer()    or
    rtpproxy_manage() command. The parameter can be an integer or a config
    variable holding an integer.
 
-   This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
+   This   function   can   be  used  from  REQUEST_ROUTE,  ONREPLY_ROUTE,
    BRANCH_ROUTE.
 
-   Example 1.9. set_rtp_proxy_set usage
+   Example 1.11. set_rtp_proxy_set usage
 ...
 set_rtp_proxy_set("2");
 rtpproxy_offer();
@@ -369,103 +410,104 @@ rtpproxy_offer();
 5.2.  rtpproxy_offer([flags [, ip_address]])
 
    Rewrites SDP body to ensure that media is passed through an RTP proxy.
-   To be invoked on INVITE for the cases the SDPs are in INVITE and 200 OK
-   and on 200 OK when SDPs are in 200 OK and ACK.
+   To  be  invoked on INVITE for the cases the SDPs are in INVITE and 200
+   OK and on 200 OK when SDPs are in 200 OK and ACK.
 
    Meaning of the parameters is as follows:
      * flags - flags to turn on some features.
-          + 1 - append first Via branch to Call-ID when sending command to
-            rtpproxy. This can be used to create one media session per
-            branch on the rtpproxy. When sending a subsequent “delete�
-            command to the rtpproxy, you can then stop just the session
+          + 1  -  append first Via branch to Call-ID when sending command
+            to rtpproxy. This can be used to create one media session per
+            branch  on  the  rtpproxy. When sending a subsequent "delete"
+            command  to  the rtpproxy, you can then stop just the session
             for a specific branch when passing the flag '1' or '2' in the
-            “unforce_rtpproxy�, or stop all sessions for a call when not
-            passing one of those two flags there. This is especially
-            useful if you have serially forked call scenarios where
-            rtpproxy gets an “update� command for a new branch, and then a
-            “delete� command for the previous branch, which would
-            otherwise delete the full call, breaking the subsequent
-            “lookup� for the new branch. This flag is only supported by
+            "unforce_rtpproxy",  or stop all sessions for a call when not
+            passing  one  of  those  two  flags there. This is especially
+            useful  if  you  have  serially  forked  call scenarios where
+            rtpproxy  gets an "update" command for a new branch, and then
+            a  "delete"  command  for  the  previous  branch, which would
+            otherwise  delete  the  full  call,  breaking  the subsequent
+            "lookup"  for  the new branch. This flag is only supported by
             the ngcp-mediaproxy-ng rtpproxy at the moment!
-          + 2 - append second Via branch to Call-ID when sending command
+          + 2  - append second Via branch to Call-ID when sending command
             to rtpproxy. See flag '1' for its meaning.
-          + 3 - behave like flag 1 is set for a request and like flag 2 is
-            set for a reply.
-          + a - flags that UA from which message is received doesn't
+          + 3  -  behave like flag 1 is set for a request and like flag 2
+            is set for a reply.
+          + a  -  flags  that  UA  from which message is received doesn't
             support symmetric RTP. (automatically sets the 'r' flag)
-          + b - append branch specific variable to Call-ID when sending
-            command to rtpproxy. This creates one rtpproxy session per
-            unique variable. Works similar to the 1, 2 and 3 parameter,
-            but is usefull when forking to multiple destinations on
-            different address families or network segments, requiring
-            different rtpproxy parameters. The variable value is taken
-            from the “extra_id_pv�. When used, it must be used in every
-            call to rtpproxy_manage(), rtpproxy_offer(), rtpproxy_answer()
-            and rtpproxy_destroy() with the same contents of the PV. The b
-            parameter may not be used in conjunction with the 1, 2 or 3
-            parameter to use the Via branch in the Call-ID.
-          + l - force “lookup�, that is, only rewrite SDP when
-            corresponding session already exists in the RTP proxy. By
+          + b  -  append branch specific variable to Call-ID when sending
+            command  to  rtpproxy.  This creates one rtpproxy session per
+            unique  variable.  Works similar to the 1, 2 and 3 parameter,
+            but  is  usefull  when  forking  to  multiple destinations on
+            different  address  families  or  network segments, requiring
+            different  rtpproxy  parameters.  The variable value is taken
+            from  the  "extra_id_pv". When used, it must be used in every
+            call       to       rtpproxy_manage(),      rtpproxy_offer(),
+            rtpproxy_answer()   and   rtpproxy_destroy()  with  the  same
+            contents  of  the  PV.  The  b  parameter  may not be used in
+            conjunction  with  the  1,  2  or  3 parameter to use the Via
+            branch in the Call-ID.
+          + l   -   force  "lookup",  that  is,  only  rewrite  SDP  when
+            corresponding  session  already  exists  in the RTP proxy. By
             default is on when the session is to be completed.
-          + i, e - these flags specify the direction of the SIP message.
-            These flags only make sense when rtpproxy is running in bridge
-            mode. 'i' means internal network (LAN), 'e' means external
-            network (WAN). 'i' corresponds to rtpproxy's first interface,
-            'e' corresponds to rtpproxy's second interface. You always
-            have to specify two flags to define the incoming network and
-            the outgoing network. For example, 'ie' should be used for SIP
-            message received from the local interface and sent out on the
-            external interface, and 'ei' vice versa. Other options are
-            'ii' and 'ee'. So, for example if a SIP requests is processed
-            with 'ie' flags, the corresponding response must be processed
-            with 'ie' flags.
-            Note: As rtpproxy in bridge mode s per default asymmetric, you
-            have to specify the 'w' flag for clients behind NAT! See also
-            above notes!
-          + x - this flag a shortcut for using the "ie" or "ei"-flags of
-            RTP-Proxy, in order to do automatic bridging between IPv4 on
-            the "internal network" and IPv6 on the "external network". The
-            distinction is done by the given IP in the SDP, e.g. a IPv4
-            Address will always call "ie" to the RTPProxy (IPv4(i) to
-            IPv6(e)) and an IPv6Address will always call "ei" to the
+          + i,  e - these flags specify the direction of the SIP message.
+            These  flags  only  make  sense  when  rtpproxy is running in
+            bridge  mode.  'i'  means  internal  network (LAN), 'e' means
+            external  network  (WAN). 'i' corresponds to rtpproxy's first
+            interface,  'e'  corresponds  to rtpproxy's second interface.
+            You  always  have to specify two flags to define the incoming
+            network and the outgoing network. For example, 'ie' should be
+            used  for  SIP  message received from the local interface and
+            sent  out  on  the  external  interface, and 'ei' vice versa.
+            Other  options  are  'ii'  and 'ee'. So, for example if a SIP
+            requests  is  processed  with  'ie'  flags, the corresponding
+            response must be processed with 'ie' flags.
+            Note:  As  rtpproxy  in bridge mode s per default asymmetric,
+            you  have to specify the 'w' flag for clients behind NAT! See
+            also above notes!
+          + x  - this flag a shortcut for using the "ie" or "ei"-flags of
+            RTP-Proxy,  in order to do automatic bridging between IPv4 on
+            the  "internal  network"  and IPv6 on the "external network".
+            The  distinction  is  done by the given IP in the SDP, e.g. a
+            IPv4  Address  will always call "ie" to the RTPProxy (IPv4(i)
+            to  IPv6(e))  and an IPv6Address will always call "ei" to the
             RTPProxy (IPv6(e) to IPv4(i)).
-            Note: Please note, that this will only work properly with
-            non-dual-stack user-agents or with dual-stack clients
-            according to RFC6157 (which suggest ICE for Dual-Stack
-            implementations). This short-cut will not work properly with
-            RFC4091 (ANAT) compatible clients, which suggests having
-            different m-lines with different IP-protocols grouped
+            Note:  Please  note,  that  this will only work properly with
+            non-dual-stack   user-agents   or   with  dual-stack  clients
+            according  to  RFC6157  (which  suggest  ICE  for  Dual-Stack
+            implementations).  This short-cut will not work properly with
+            RFC4091  (ANAT)  compatible  clients,  which  suggests having
+            different   m-lines   with   different  IP-protocols  grouped
             together.
-          + f - instructs rtpproxy to ignore marks inserted by another
-            rtpproxy in transit to indicate that the session is already
-            goes through another proxy. Allows creating a chain of
+          + f  -  instructs  rtpproxy to ignore marks inserted by another
+            rtpproxy  in  transit to indicate that the session is already
+            goes  through  another  proxy.  Allows  creating  a  chain of
             proxies.
-          + r - flags that IP address in SDP should be trusted. Without
-            this flag, rtpproxy ignores address in the SDP and uses source
-            address of the SIP message as media address which is passed to
-            the RTP proxy.
-          + o - flags that IP from the origin description (o=) should be
+          + r  -  flags that IP address in SDP should be trusted. Without
+            this  flag,  rtpproxy  ignores  address  in  the SDP and uses
+            source  address  of the SIP message as media address which is
+            passed to the RTP proxy.
+          + o  - flags that IP from the origin description (o=) should be
             also changed.
-          + c - flags to change the session-level SDP connection (c=) IP
+          + c  - flags to change the session-level SDP connection (c=) IP
             if media-description also includes connection information.
-          + w - flags that for the UA from which message is received,
+          + w  -  flags  that  for the UA from which message is received,
             support symmetric RTP must be forced.
-          + zNN - requests the RTPproxy to perform re-packetization of RTP
-            traffic coming from the UA which has sent the current message
-            to increase or decrease payload size per each RTP packet
-            forwarded if possible. The NN is the target payload size in
-            ms, for the most codecs its value should be in 10ms
-            increments, however for some codecs the increment could differ
-            (e.g. 30ms for GSM or 20ms for G.723). The RTPproxy would
-            select the closest value supported by the codec. This feature
-            could be used for significantly reducing bandwith overhead for
-            low bitrate codecs, for example with G.729 going from 10ms to
-            100ms saves two thirds of the network bandwith.
+          + zNN  -  requests  the RTPproxy to perform re-packetization of
+            RTP  traffic  coming  from  the UA which has sent the current
+            message  to  increase  or  decrease payload size per each RTP
+            packet  forwarded  if  possible. The NN is the target payload
+            size  in  ms, for the most codecs its value should be in 10ms
+            increments,  however  for  some  codecs  the  increment could
+            differ  (e.g.  30ms  for GSM or 20ms for G.723). The RTPproxy
+            would  select  the closest value supported by the codec. This
+            feature  could  be  used  for significantly reducing bandwith
+            overhead for low bitrate codecs, for example with G.729 going
+            from 10ms to 100ms saves two thirds of the network bandwith.
      * ip_address - new SDP IP address.
 
    This function can be used from ANY_ROUTE.
 
-   Example 1.10. rtpproxy_offer usage
+   Example 1.12. rtpproxy_offer usage
 route {
 ...
     if (is_method("INVITE")) {
@@ -500,16 +542,16 @@ onreply_route[2]
 5.3.  rtpproxy_answer([flags [, ip_address]])
 
    Rewrites SDP body to ensure that media is passed through an RTP proxy.
-   To be invoked on 200 OK for the cases the SDPs are in INVITE and 200 OK
-   and on ACK when SDPs are in 200 OK and ACK.
+   To  be  invoked on 200 OK for the cases the SDPs are in INVITE and 200
+   OK and on ACK when SDPs are in 200 OK and ACK.
 
-   See rtpproxy_answer() function description above for the meaning of the
-   parameters.
+   See  rtpproxy_answer()  function  description above for the meaning of
+   the parameters.
 
-   This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
+   This   function   can   be  used  from  REQUEST_ROUTE,  ONREPLY_ROUTE,
    FAILURE_ROUTE, BRANCH_ROUTE.
 
-   Example 1.11. rtpproxy_answer usage
+   Example 1.13. rtpproxy_answer usage
 
    See rtpproxy_offer() function example above for example.
 
@@ -521,31 +563,31 @@ onreply_route[2]
 
    Meaning of the parameters is as follows:
      * flags - flags to turn on some features.
-          + 1 - append first Via branch to Call-ID when sending command to
-            rtpproxy. This can be used to create one media session per
-            branch on the rtpproxy. When sending a subsequent “delete�
-            command to the rtpproxy, you can then stop just the session
+          + 1  -  append first Via branch to Call-ID when sending command
+            to rtpproxy. This can be used to create one media session per
+            branch  on  the  rtpproxy. When sending a subsequent "delete"
+            command  to  the rtpproxy, you can then stop just the session
             for a specific branch when passing the flag '1' or '2' in the
-            “unforce_rtpproxy�, or stop all sessions for a call when not
-            passing one of those two flags there. This is especially
-            useful if you have serially forked call scenarios where
-            rtpproxy gets an “update� command for a new branch, and then a
-            “delete� command for the previous branch, which would
-            otherwise delete the full call, breaking the subsequent
-            “lookup� for the new branch. This flag is only supported by
+            "unforce_rtpproxy",  or stop all sessions for a call when not
+            passing  one  of  those  two  flags there. This is especially
+            useful  if  you  have  serially  forked  call scenarios where
+            rtpproxy  gets an "update" command for a new branch, and then
+            a  "delete"  command  for  the  previous  branch, which would
+            otherwise  delete  the  full  call,  breaking  the subsequent
+            "lookup"  for  the new branch. This flag is only supported by
             the ngcp-mediaproxy-ng rtpproxy at the moment!
-          + 2 - append second Via branch to Call-ID when sending command
+          + 2  - append second Via branch to Call-ID when sending command
             to rtpproxy. See flag '1' for its meaning.
-          + b - append branch specific variable to Call-ID when sending
-            command to rtpproxy. See rtpproxy_offer() for details.
+          + b  -  append branch specific variable to Call-ID when sending
+            command   to  rtpproxy.  See  rtpproxy_offer()  for  details.
             <listitem>
             </listitem>
-            t - do not include To tag to “delete� command to rtpproxy thus
-            causing full call to be deleted. Useful for deleting unused
-            rtpproxy call when 200 OK is received on a branch, where
-            rtpproxy is not needed.
+            t  -  do  not  include To tag to "delete" command to rtpproxy
+            thus  causing  full  call  to be deleted. Useful for deleting
+            unused  rtpproxy  call  when  200 OK is received on a branch,
+            where rtpproxy is not needed.
 
-   Example 1.12. rtpproxy_destroy usage
+   Example 1.14. rtpproxy_destroy usage
 ...
 rtpproxy_destroy();
 ...
@@ -556,66 +598,66 @@ rtpproxy_destroy();
 
 5.6.  rtpproxy_manage([flags [, ip_address]])
 
-   Manage the RTPProxy session - it combines the functionality of
-   rtpproxy_offer(), rtpproxy_answer() and unforce_rtpproxy(), detecting
+   Manage  the  RTPProxy  session  -  it  combines  the  functionality of
+   rtpproxy_offer(),  rtpproxy_answer() and unforce_rtpproxy(), detecting
    internally based on message type and method which one to execute.
 
-   It can take the same parameters as rtpproxy_offer(). The flags
-   parameter to rtpproxy_manage() can be a configuration variable
+   It  can  take  the  same  parameters  as  rtpproxy_offer().  The flags
+   parameter   to  rtpproxy_manage()  can  be  a  configuration  variable
    containing the flags as a string.
 
    Functionality:
      * If INVITE with SDP, then do rtpproxy_offer()
      * If INVITE with SDP, when the tm module is loaded, mark transaction
-       with internal flag FL_SDP_BODY to know that the 1xx and 2xx are for
-       rtpproxy_answer()
+       with  internal  flag  FL_SDP_BODY to know that the 1xx and 2xx are
+       for rtpproxy_answer()
      * If ACK with SDP, then do rtpproxy_answer()
-     * If BYE or CANCEL, or called within a FAILURE_ROUTE[], then do
+     * If  BYE  or  CANCEL,  or  called within a FAILURE_ROUTE[], then do
        unforce_rtpproxy()
      * If reply to INVITE with code >= 300 do unforce_rtpproxy()
-     * If reply with SDP to INVITE having code 1xx and 2xx, then do
-       rtpproxy_answer() if the request had SDP or tm is not loaded,
+     * If  reply  with  SDP  to  INVITE  having code 1xx and 2xx, then do
+       rtpproxy_answer()  if  the  request  had  SDP or tm is not loaded,
        otherwise do rtpproxy_offer()
 
    This function can be used from ANY_ROUTE.
 
-   Example 1.13. rtpproxy_manage usage
+   Example 1.15. rtpproxy_manage usage
 ...
 rtpproxy_manage();
 ...
 
 5.7.  rtpproxy_stream2uac(prompt_name, count),
 
-   Instruct the RTPproxy to stream prompt/announcement pre-encoded with
+   Instruct  the  RTPproxy to stream prompt/announcement pre-encoded with
    the makeann command from the RTPproxy distribution. The uac/uas suffix
-   selects who will hear the announcement relatively to the current
+   selects  who  will  hear  the  announcement  relatively to the current
    transaction - UAC or UAS. For example invoking the rtpproxy_stream2uac
-   in the request processing block on ACK transaction will play the prompt
-   to the UA that has generated original INVITE and ACK while
-   rtpproxy_stop_stream2uas on 183 in reply processing block will play the
-   prompt to the UA that has generated 183.
-
-   Apart from generating announcements, another possible application of
-   this function is implementing music on hold (MOH) functionality. When
-   count is -1, the streaming will be in loop indefinitely until the
+   in  the  request  processing  block  on  ACK transaction will play the
+   prompt  to  the  UA  that  has generated original INVITE and ACK while
+   rtpproxy_stop_stream2uas  on  183  in reply processing block will play
+   the prompt to the UA that has generated 183.
+
+   Apart  from  generating announcements, another possible application of
+   this  function is implementing music on hold (MOH) functionality. When
+   count  is  -1,  the  streaming  will be in loop indefinitely until the
    appropriate rtpproxy_stop_stream2xxx is issued.
 
-   In order to work correctly, these functions require that a session in
+   In  order to work correctly, these functions require that a session in
    the RTPproxy already exists. Also those functions don't alter the SDP,
-   so that they are not a substitute for calling rtpproxy_offer or
+   so  that  they  are  not  a  substitute  for calling rtpproxy_offer or
    rtpproxy_answer.
 
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE.
 
    Meaning of the parameters is as follows:
-     * prompt_name - name of the prompt to stream. Should be either
-       absolute pathname or pathname relative to the directory where
+     * prompt_name  -  name  of  the  prompt  to stream. Should be either
+       absolute  pathname  or  pathname  relative  to the directory where
        RTPproxy runs.
-     * count - number of times the prompt should be repeated. A value of
-       -1 means that it will be streaming in a loop indefinitely, until
+     * count  - number of times the prompt should be repeated. A value of
+       -1  means  that it will be streaming in a loop indefinitely, until
        the appropriate rtpproxy_stop_stream2xxx is issued.
 
-   Example 1.14. rtpproxy_stream2xxx usage
+   Example 1.16. rtpproxy_stream2xxx usage
 ...
     if (is_method("INVITE")) {
         rtpproxy_offer();
@@ -633,8 +675,8 @@ rtpproxy_manage();
 
 5.9.  rtpproxy_stop_stream2uac(),
 
-   Stop streaming of announcement/prompt/MOH started previously by the
-   respective rtpproxy_stream2xxx. The uac/uas suffix selects whose
+   Stop  streaming  of  announcement/prompt/MOH started previously by the
+   respective  rtpproxy_stream2xxx.  The  uac/uas  suffix  selects  whose
    announcement relatively to tha current transaction should be stopped -
    UAC or UAS.
 
@@ -642,13 +684,13 @@ rtpproxy_manage();
 
 5.10.  start_recording()
 
-   This function will send a signal to the RTP-Proxy to record the RTP
-   stream on the RTP-Proxy. This function is only supported by Sippy
+   This  function  will  send a signal to the RTP-Proxy to record the RTP
+   stream  on  the  RTP-Proxy.  This  function is only supported by Sippy
    RTPproxy at the moment!
 
    This function can be used from REQUEST_ROUTE and ONREPLY_ROUTE.
 
-   Example 1.15. start_recording usage
+   Example 1.17. start_recording usage
 ...
 start_recording();
 ...
@@ -664,11 +706,11 @@ start_recording();
 6.1. $rtpstat
 
    Returns the RTP-Statistics from the RTP-Proxy. The RTP-Statistics from
-   the RTP-Proxy are provided as a string and it does contain several
-   packet-counters. The statistics must be retrieved before the session is
-   deleted (before unforce_rtpproxy()).
+   the  RTP-Proxy  are  provided  as a string and it does contain several
+   packet-counters.  The  statistics must be retrieved before the session
+   is deleted (before unforce_rtpproxy()).
 
-   Example 1.16. $rtpstat-Usage
+   Example 1.18. $rtpstat-Usage
 ...
     append_hf("X-RTP-Statistics: $rtpstat\r\n");
 ...
@@ -680,46 +722,46 @@ start_recording();
 
 7.1. nh_enable_rtpp
 
-   Enables a rtp proxy if parameter value is greater than 0. Disables it
+   Enables  a rtp proxy if parameter value is greater than 0. Disables it
    if a zero value is given.
 
-   The first parameter is the rtp proxy url (exactly as defined in the
+   The  first  parameter  is the rtp proxy url (exactly as defined in the
    config file).
 
    The second parameter value must be a number in decimal.
 
-   NOTE: if a rtpproxy is defined multiple times (in the same or diferente
-   sete), all of its instances will be enables/disabled.
+   NOTE:  if  a  rtpproxy  is  defined  multiple  times  (in  the same or
+   diferente sete), all of its instances will be enables/disabled.
 
-   Example 1.17.  nh_enable_rtpp usage
+   Example 1.19.  nh_enable_rtpp usage
 ...
 $ kamctl fifo nh_enable_rtpp udp:192.168.2.133:8081 0
 ...
 
 7.2. nh_show_rtpp
 
-   Displays all the rtp proxies and their information: set and status
+   Displays  all  the  rtp  proxies and their information: set and status
    (disabled or not, weight and recheck_ticks).
 
    No parameter.
 
-   Example 1.18.  nh_show_rtpp usage
+   Example 1.20.  nh_show_rtpp usage
 ...
 $ kamctl fifo nh_show_rtpp
 ...
 
 Chapter 2. Frequently Asked Questions
 
-   2.1. What happend with “rtpproxy_disable� parameter?
+   2.1. What happend with "rtpproxy_disable" parameter?
    2.2. Where can I find more about Kamailio?
    2.3. Where can I post a question about this module?
    2.4. How can I report a bug?
 
    2.1.
 
-   What happend with “rtpproxy_disable� parameter?
+   What happend with "rtpproxy_disable" parameter?
 
-   It was removed as it became obsolete - now “rtpproxy_sock� can take
+   It was removed as it became obsolete - now "rtpproxy_sock" can take
    empty value to disable the rtpproxy functionality.
 
    2.2.

+ 43 - 3
modules/rtpproxy/doc/rtpproxy_admin.xml

@@ -98,8 +98,11 @@
 	<section>
 		<title><varname>rtpproxy_sock</varname> (string)</title>
 		<para>
-		Definition of socket(s) used to connect to (a set) RTPProxy. It may
-		specify a UNIX socket or an IPv4/IPv6 UDP socket.
+		Used to define the list of RTPPRoxy instances to connect to.
+		These can be UNIX sockets or IPv4/IPv6 UDP sockets.
+
+		Each modparam entry will insert sockets into a single set. If no set ID is given, the default set ID '0' will be used. To define multiple sets add the set number at the beginning of each parameter followed by '=='.
+		Sockets can be weighted by adding '=#' to a socket where # is an integer. A socket with a weight of 2 will be chosen twice as often as one with a weight of 1.
 		</para>
 		<para>
 		<emphasis>
@@ -112,9 +115,11 @@
 ...
 # single rtproxy
 modparam("rtpproxy", "rtpproxy_sock", "udp:localhost:12221")
+
 # multiple rtproxies for LB
 modparam("rtpproxy", "rtpproxy_sock",
 	"udp:localhost:12221 udp:localhost:12222")
+
 # multiple sets of multiple rtproxies
 modparam("rtpproxy", "rtpproxy_sock",
 	"1 == udp:localhost:12221 udp:localhost:12222")
@@ -230,7 +235,7 @@ modparam("rtpproxy", "nortpproxy_str", "a=sdpmangled:yes\r\n")
 		<title>Set <varname>timeout_socket</varname> parameter</title>
 		<programlisting format="linespecific">
 ...
-modparam("nathelper", "timeout_socket", "xmlrpc:http://127.0.0.1:8000/RPC2")
+modparam("rtpproxy", "timeout_socket", "xmlrpc:http://127.0.0.1:8000/RPC2")
 ...
 </programlisting>
 		</example>
@@ -278,6 +283,41 @@ modparam("rtpproxy", "ice_candidate_priority_avp", "$avp(ice_priority)")
 ...
 modparam("rtpproxy", "extra_id_pv", "$avp(extra_id)")
 ...
+</programlisting>
+		</example>
+	</section>
+	<section>
+		<title><varname>db_url</varname> (string)</title>
+		<para>
+			The database URL to load rtp_proxy sets from.
+			If this parameter is set, the module will attempt to load the rtpproxy sets from the specified database and will ignore any 'rtpproxy_sock' modparams.
+		</para>
+		<para>
+			Default is empty, a database will not be used.
+		</para>
+		<example>
+		<title>Set <varname>db_url</varname> parameter</title>
+		<programlisting format="linespecific">
+...
+modparam("rtpproxy", "db_url", "mysql://user:passwb@localhost/database")
+...
+</programlisting>
+		</example>
+	</section>
+	<section>
+		<title><varname>table_name</varname> (string)</title>
+		<para>
+			The name of the table containing the rtpproxy sets.
+		</para>
+		<para>
+			Default value is <quote>rtpproxy</quote>.
+		</para>
+		<example>
+		<title>Set <varname>table_name</varname> parameter</title>
+		<programlisting format="linespecific">
+...
+modparam("rtpproxy", "table_name", "my_rtpp_sets")
+...
 </programlisting>
 		</example>
 	</section>

+ 134 - 107
modules/rtpproxy/rtpproxy.c

@@ -241,6 +241,7 @@ MODULE_VERSION
 
 
 #define DEFAULT_RTPP_SET_ID		0
+static str DEFAULT_RTPP_SET_ID_STR = str_init("0");
 
 #define MI_SET_NATPING_STATE		"nh_enable_ping"
 #define MI_DEFAULT_NATPING_STATE	1
@@ -434,6 +435,8 @@ static param_export_t params[] = {
 	{"ice_candidate_priority_avp", STR_PARAM,
 	 &ice_candidate_priority_avp_param},
 	{"extra_id_pv",           STR_PARAM, &extra_id_pv_param.s },
+	{"db_url",                STR_PARAM, &rtpp_db_url.s },
+	{"table_name",            STR_PARAM, &rtpp_table_name.s },
 	{0, 0, 0}
 };
 
@@ -502,13 +505,107 @@ static int rtpproxy_set_store(modparam_t type, void * val){
 	return 0;
 }
 
+struct rtpp_set *get_rtpp_set(str *const set_name)
+{
+	unsigned int this_set_id;
+	struct rtpp_set *rtpp_list;
+	if (rtpp_set_list == NULL)
+	{
+		LM_ERR("rtpp set list not configured\n");
+		return NULL;
+	}
+	/* Only integer set_names are valid at the moment */
+	if ((set_name->s == NULL) || (set_name->len == 0))
+	{
+		LM_ERR("Invalid set name '%.*s'\n", set_name->len, set_name->s);
+		return NULL;
+	}
+	if (str2int(set_name, &this_set_id) < 0)
+	{
+		LM_ERR("Invalid set name '%.*s' - must be integer\n", set_name->len, set_name->s);
+		return NULL;
+	}
+
+	rtpp_list = select_rtpp_set(this_set_id);
+
+	if(rtpp_list==NULL){	/*if a new id_set : add a new set of rtpp*/
+		rtpp_list = shm_malloc(sizeof(struct rtpp_set));
+		if(!rtpp_list){
+			LM_ERR("no shm memory left\n");
+			return NULL;
+		}
+		memset(rtpp_list, 0, sizeof(struct rtpp_set));
+		rtpp_list->id_set = this_set_id;
+		if (rtpp_set_list->rset_first == NULL)
+		{
+			rtpp_set_list->rset_first = rtpp_list;
+		} else {
+			rtpp_set_list->rset_last->rset_next = rtpp_list;
+		}
+		rtpp_set_list->rset_last = rtpp_list;
+		rtpp_set_count++;
+
+		if (this_set_id == DEFAULT_RTPP_SET_ID)
+		{
+			default_rtpp_set = rtpp_list;
+		}
+	}
+	return rtpp_list;
+}
+
+int insert_rtpp_node(struct rtpp_set *const rtpp_list, const str *const url, const int weight, const int disabled)
+{
+	struct rtpp_node *pnode;
+
+	if ((pnode = shm_malloc(sizeof(struct rtpp_node) + url->len + 1)) == NULL)
+	{
+		LM_ERR("out of shm memory\n");
+		return -1;
+	}
+	memset(pnode, 0, sizeof(struct rtpp_node) + url->len + 1);
+	pnode->idx = rtpp_no++;
+	pnode->rn_weight = weight;
+	pnode->rn_umode = 0;
+	pnode->rn_disabled = disabled;
+	/* Permanently disable if marked as disabled */
+	pnode->rn_recheck_ticks = disabled ? MI_MAX_RECHECK_TICKS : 0;
+	pnode->rn_url.s = (char*)(pnode + 1);
+	memcpy(pnode->rn_url.s, url->s, url->len);
+	pnode->rn_url.len = url->len;
+
+	LM_DBG("url is '%.*s'\n", pnode->rn_url.len, pnode->rn_url.s);
+
+	/* Find protocol and store address */
+	pnode->rn_address = pnode->rn_url.s;
+	if (strncasecmp(pnode->rn_address, "udp:", 4) == 0) {
+		pnode->rn_umode = 1;
+		pnode->rn_address += 4;
+	} else if (strncasecmp(pnode->rn_address, "udp6:", 5) == 0) {
+		pnode->rn_umode = 6;
+		pnode->rn_address += 5;
+	} else if (strncasecmp(pnode->rn_address, "unix:", 5) == 0) {
+		pnode->rn_umode = 0;
+		pnode->rn_address += 5;
+	}
+
+	if (rtpp_list->rn_first == NULL)
+	{
+		rtpp_list->rn_first = pnode;
+	} else {
+		rtpp_list->rn_last->rn_next = pnode;
+	}
+	rtpp_list->rn_last = pnode;
+	rtpp_list->rtpp_node_count++;
+
+	return 0;
+}
 
 static int add_rtpproxy_socks(struct rtpp_set * rtpp_list, 
 										char * rtpproxy){
 	/* Make rtp proxies list. */
 	char *p, *p1, *p2, *plim;
-	struct rtpp_node *pnode;
 	int weight;
+	str url;
 
 	p = rtpproxy;
 	plim = p + strlen(p);
@@ -531,49 +628,10 @@ static int add_rtpproxy_socks(struct rtpp_set * rtpp_list,
 		} else {
 			p2 = p;
 		}
-		pnode = shm_malloc(sizeof(struct rtpp_node));
-		if (pnode == NULL) {
-			LM_ERR("no shm memory left\n");
-			return -1;
-		}
-		memset(pnode, 0, sizeof(*pnode));
-		pnode->idx = rtpp_no++;
-		pnode->rn_recheck_ticks = 0;
-		pnode->rn_weight = weight;
-		pnode->rn_umode = 0;
-		pnode->rn_disabled = 0;
-		pnode->rn_url.s = shm_malloc(p2 - p1 + 1);
-		if (pnode->rn_url.s == NULL) {
-			shm_free(pnode);
-			LM_ERR("no shm memory left\n");
-			return -1;
-		}
-		memmove(pnode->rn_url.s, p1, p2 - p1);
-		pnode->rn_url.s[p2 - p1] 	= 0;
-		pnode->rn_url.len 			= p2-p1;
-
-		LM_DBG("url is %s, len is %i\n", pnode->rn_url.s, pnode->rn_url.len);
-		/* Leave only address in rn_address */
-		pnode->rn_address = pnode->rn_url.s;
-		if (strncasecmp(pnode->rn_address, "udp:", 4) == 0) {
-			pnode->rn_umode = 1;
-			pnode->rn_address += 4;
-		} else if (strncasecmp(pnode->rn_address, "udp6:", 5) == 0) {
-			pnode->rn_umode = 6;
-			pnode->rn_address += 5;
-		} else if (strncasecmp(pnode->rn_address, "unix:", 5) == 0) {
-			pnode->rn_umode = 0;
-			pnode->rn_address += 5;
-		}
 
-		if (rtpp_list->rn_first == NULL) {
-			rtpp_list->rn_first = pnode;
-		} else {
-			rtpp_list->rn_last->rn_next = pnode;
-		}
-
-		rtpp_list->rn_last = pnode;
-		rtpp_list->rtpp_node_count++;
+		url.s = p1;
+		url.len = (p2-p1);
+		insert_rtpp_node(rtpp_list, &url, weight, 0);
 	}
 	return 0;
 }
@@ -586,9 +644,7 @@ static int rtpproxy_add_rtpproxy_set( char * rtp_proxies)
 {
 	char *p,*p2;
 	struct rtpp_set * rtpp_list;
-	unsigned int my_current_id;
 	str id_set;
-	int new_list;
 
 	/* empty definition? */
 	p= rtp_proxies;
@@ -613,7 +669,7 @@ static int rtpproxy_add_rtpproxy_set( char * rtp_proxies)
 		for(;isspace(*p2); *p2 = '\0',p2--);
 		id_set.s = p;	id_set.len = p2 - p+1;
 			
-		if(id_set.len <= 0 ||str2int(&id_set, &my_current_id)<0 ){
+		if(id_set.len <= 0){
 		LM_ERR("script error -invalid set_id value!\n");
 			return -1;
 		}
@@ -621,7 +677,7 @@ static int rtpproxy_add_rtpproxy_set( char * rtp_proxies)
 		rtp_proxies+=2;
 	}else{
 		rtp_proxies = p;
-		my_current_id = DEFAULT_RTPP_SET_ID;
+		id_set = DEFAULT_RTPP_SET_ID_STR;
 	}
 
 	for(;*rtp_proxies && isspace(*rtp_proxies);rtp_proxies++);
@@ -631,57 +687,18 @@ static int rtpproxy_add_rtpproxy_set( char * rtp_proxies)
 		return -1;;
 	}
 
-	/*search for the current_id*/
-	rtpp_list = rtpp_set_list ? rtpp_set_list->rset_first : 0;
-	while( rtpp_list != 0 && rtpp_list->id_set!=my_current_id)
-		rtpp_list = rtpp_list->rset_next;
-
-	if(rtpp_list==NULL){	/*if a new id_set : add a new set of rtpp*/
-		rtpp_list = shm_malloc(sizeof(struct rtpp_set));
-		if(!rtpp_list){
-			LM_ERR("no shm memory left\n");
-			return -1;
-		}
-		memset(rtpp_list, 0, sizeof(struct rtpp_set));
-		rtpp_list->id_set = my_current_id;
-		new_list = 1;
-	} else {
-		new_list = 0;
+	rtpp_list = get_rtpp_set(&id_set);
+	if (rtpp_list == NULL)
+	{
+		LM_ERR("Failed to get or create rtpp_list for '%.*s'\n", id_set.len, id_set.s);
+		return -1;
 	}
 
 	if(add_rtpproxy_socks(rtpp_list, rtp_proxies)!= 0){
-		/*if this list will not be inserted, clean it up*/
-		goto error;
-	}
-
-	if (new_list) {
-		if(!rtpp_set_list){/*initialize the list of set*/
-			rtpp_set_list = shm_malloc(sizeof(struct rtpp_set_head));
-			if(!rtpp_set_list){
-				LM_ERR("no shm memory left\n");
-				return -1;
-			}
-			memset(rtpp_set_list, 0, sizeof(struct rtpp_set_head));
-		}
-
-		/*update the list of set info*/
-		if(!rtpp_set_list->rset_first){
-			rtpp_set_list->rset_first = rtpp_list;
-		}else{
-			rtpp_set_list->rset_last->rset_next = rtpp_list;
-		}
-
-		rtpp_set_list->rset_last = rtpp_list;
-		rtpp_set_count++;
-
-		if(my_current_id == DEFAULT_RTPP_SET_ID){
-			default_rtpp_set = rtpp_list;
-		}
+		return -1;
 	}
 
 	return 0;
-error:
-	return -1;
 }
 
 
@@ -887,9 +904,13 @@ mod_init(void)
 		return -1;
 	}
 
-	/* any rtpproxy configured? */
-	if(rtpp_set_list)
-		default_rtpp_set = select_rtpp_set(DEFAULT_RTPP_SET_ID);
+	/* Configure the head of the rtpp_set_list */
+	rtpp_set_list = shm_malloc(sizeof(struct rtpp_set_head));
+	if (rtpp_set_list == NULL)
+	{
+		LM_ERR("no shm memory for rtpp_set_list\n");
+		return -1;
+	}
 
 	if (nortpproxy_str.s==NULL || nortpproxy_str.s[0]==0) {
 		nortpproxy_str.len = 0;
@@ -903,9 +924,19 @@ mod_init(void)
 			nortpproxy_str.s = NULL;
 	}
 
+	if (rtpp_db_url.s != NULL)
+	{
+		rtpp_db_url.len = strlen(rtpp_db_url.s);
+		init_rtpproxy_db();
+		if (rtpp_sets > 0)
+		{
+			LM_WARN("rtpproxy db url configured - ignoring modparam sets\n");
+		}
+	}
 	/* storing the list of rtp proxy sets in shared memory*/
 	for(i=0;i<rtpp_sets;i++){
-		if(rtpproxy_add_rtpproxy_set(rtpp_strings[i]) !=0){
+		if ((rtpp_db_url.s == NULL) &&
+		    (rtpproxy_add_rtpproxy_set(rtpp_strings[i]) != 0)) {
 			for(;i<rtpp_sets;i++)
 				if(rtpp_strings[i])
 					pkg_free(rtpp_strings[i]);
@@ -915,6 +946,7 @@ mod_init(void)
 		if(rtpp_strings[i])
 			pkg_free(rtpp_strings[i]);
 	}
+
 	if (timeout_socket_str.s==NULL || timeout_socket_str.s[0]==0) {
 		timeout_socket_str.len = 0;
 		timeout_socket_str.s = NULL;
@@ -1038,7 +1070,7 @@ child_init(int rank)
 			}
 			freeaddrinfo(res);
 rptest:
-			pnode->rn_disabled = rtpp_test(pnode, 0, 1);
+			pnode->rn_disabled = rtpp_test(pnode, pnode->rn_disabled, 1);
 		}
 	}
 
@@ -1062,9 +1094,6 @@ static void mod_destroy(void)
 
 		for(crt_rtpp = crt_list->rn_first; crt_rtpp != NULL;  ){
 
-			if(crt_rtpp->rn_url.s)
-				shm_free(crt_rtpp->rn_url.s);
-
 			last_rtpp = crt_rtpp;
 			crt_rtpp = last_rtpp->rn_next;
 			shm_free(last_rtpp);
@@ -1696,16 +1725,14 @@ static struct rtpp_set * select_rtpp_set(int id_set ){
 	struct rtpp_set * rtpp_list;
 	/*is it a valid set_id?*/
 	
-	if(!rtpp_set_list || !rtpp_set_list->rset_first){
-		LM_ERR("no rtp_proxy configured\n");
-		return 0;
+	if(!rtpp_set_list)
+	{
+		LM_ERR("rtpproxy set list not initialised\n");
+		return NULL;
 	}
 
-	for(rtpp_list=rtpp_set_list->rset_first; rtpp_list!=0 && 
+	for(rtpp_list=rtpp_set_list->rset_first; rtpp_list!=NULL && 
 		rtpp_list->id_set!=id_set; rtpp_list=rtpp_list->rset_next);
-	if(!rtpp_list){
-		LM_ERR(" script error-invalid id_set to be selected\n");
-	}
 
 	return rtpp_list;
 }

+ 9 - 0
modules/rtpproxy/rtpproxy.h

@@ -27,6 +27,7 @@
 #ifndef _RTPPROXY_H
 #define _RTPPROXY_H
 
+#include <sys/uio.h>
 #include "../../str.h"
 
 /* Handy macros */
@@ -68,4 +69,12 @@ struct rtpp_set_head{
 struct rtpp_node *select_rtpp_node(str, int);
 char *send_rtpp_command(struct rtpp_node *, struct iovec *, int);
 
+struct rtpp_set *get_rtpp_set(str *set_name);
+int insert_rtpp_node(struct rtpp_set *const rtpp_list, const str *const url, const int weight, const int disabled);
+
+int init_rtpproxy_db(void);
+
+extern str rtpp_db_url;
+extern str rtpp_table_name;
+
 #endif

+ 169 - 0
modules/rtpproxy/rtpproxy_db.c

@@ -0,0 +1,169 @@
+/*
+ * rtpproxy module
+ *
+ * Copyright (c) 2013 Crocodile RCS Ltd
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Kamailio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include "../../lib/srdb1/db.h"
+#include "../../lib/srdb1/db_res.h"
+
+#include "rtpproxy.h"
+
+#define RTPP_TABLE_VERSION 1
+
+static db_func_t rtpp_dbf;
+static db1_con_t *rtpp_db_handle = NULL;
+
+str rtpp_db_url = {NULL, 0};
+str rtpp_table_name = str_init("rtpproxy");
+str rtpp_set_name_col = str_init("set_name");
+str rtpp_url_col = str_init("url");
+str rtpp_weight_col = str_init("weight");
+str rtpp_flags_col = str_init("flags");
+
+static int rtpp_connect_db(void)
+{
+	if ((rtpp_db_url.s == NULL) || (rtpp_db_url.len == 0))
+		return -1;
+	if ((rtpp_db_handle = rtpp_dbf.init(&rtpp_db_url)) == NULL)
+	{
+		LM_ERR("Cannot initialize db connection\n");
+		return -1;
+	}
+	return 0;
+}
+
+static void rtpp_disconnect_db(void)
+{
+	if (rtpp_db_handle)
+	{
+		rtpp_dbf.close(rtpp_db_handle);
+		rtpp_db_handle = NULL;
+	}
+}
+
+static int rtpp_load_db(void)
+{
+	int i;
+	struct rtpp_set *rtpp_list = NULL;
+	db1_res_t *res = NULL;
+	db_val_t *values = NULL;
+	db_row_t *rows = NULL;
+	db_key_t query_cols[] = {&rtpp_set_name_col, &rtpp_url_col, &rtpp_weight_col, &rtpp_flags_col};
+
+	str set, url;
+	int weight, flags;
+	int n_rows = 0;
+	int n_cols = 4;
+
+	if (rtpp_db_handle == NULL)
+	{
+		LM_ERR("invalid db handle\n");
+		return -1;
+	}
+	if (rtpp_dbf.use_table(rtpp_db_handle, &rtpp_table_name) < 0)
+	{
+		LM_ERR("unable to use table '%.*s'\n", rtpp_table_name.len, rtpp_table_name.s);
+		return -1;
+	}
+	if (rtpp_dbf.query(rtpp_db_handle, 0, 0, 0, query_cols, 0, n_cols, 0, &res) < 0)
+	{
+		LM_ERR("error while running db query\n");
+		return -1;
+	}
+
+	n_rows = RES_ROW_N(res);
+	rows = RES_ROWS(res);
+	if (n_rows == 0)
+	{
+		LM_WARN("No rtpproxy instances in database\n");
+		return 0;
+	}
+	for (i=0; i<n_rows; i++)
+	{
+		values = ROW_VALUES(rows + i);
+
+		set.s = VAL_STR(values).s;
+		set.len = strlen(set.s);
+		url.s = VAL_STR(values+1).s;
+		url.len = strlen(url.s);
+		weight = VAL_INT(values+2);
+		flags = VAL_INT(values+3);
+
+		if ((rtpp_list = get_rtpp_set(&set)) == NULL)
+		{
+			LM_ERR("error getting rtpp_list for set '%.*s'\n", set.len, set.s);
+			continue;
+		}
+		if (insert_rtpp_node(rtpp_list, &url, weight, flags) < 0)
+		{
+			LM_ERR("error inserting '%.*s' into set '%.*s'\n", url.len, url.s, set.len, set.s);
+		}
+	}
+
+	rtpp_dbf.free_result(rtpp_db_handle, res);
+	return 0;
+}
+
+int init_rtpproxy_db(void)
+{
+	int ret;
+	int rtpp_table_version;
+	if (rtpp_db_url.s == NULL)
+		/* Database not configured */
+		return 0;
+
+	rtpp_db_url.len = strlen(rtpp_db_url.s);
+	rtpp_table_name.len = strlen(rtpp_table_name.s);
+
+	if (db_bind_mod(&rtpp_db_url, &rtpp_dbf) < 0)
+	{
+		LM_ERR("Unable to bind to db driver - %.*s\n", rtpp_db_url.len, rtpp_db_url.s);
+		return -1;
+	}
+	if (rtpp_connect_db() != 0)
+	{
+		LM_ERR("Unable to connect to db\n");
+		return -1;
+	}
+
+	rtpp_table_version = db_table_version(&rtpp_dbf, rtpp_db_handle, &rtpp_table_name);
+	if (rtpp_table_version < 0)
+	{
+		LM_ERR("failed to get rtpp table version\n");
+		ret = -1;
+		goto done;
+	}
+	switch (rtpp_table_version) {
+		case RTPP_TABLE_VERSION:
+			break;
+		default:
+			LM_ERR("invalid table version (found %d, require %d)\n",
+			  rtpp_table_version, RTPP_TABLE_VERSION);
+			ret = -1;
+			goto done;
+	}
+	ret = rtpp_load_db();
+
+done:
+	rtpp_disconnect_db();
+
+	return ret;
+}