rtpproxy.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017
  1. /* $Id$
  2. *
  3. * Copyright (C) 2003-2008 Sippy Software, Inc., http://www.sippysoft.com
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * History:
  22. * ---------
  23. * 2003-10-09 nat_uac_test introduced (jiri)
  24. *
  25. * 2003-11-06 nat_uac_test permitted from onreply_route (jiri)
  26. *
  27. * 2003-12-01 unforce_rtp_proxy introduced (sobomax)
  28. *
  29. * 2004-01-07 RTP proxy support updated to support new version of the
  30. * RTP proxy (20040107).
  31. *
  32. * force_rtp_proxy() now inserts a special flag
  33. * into the SDP body to indicate that this session already
  34. * proxied and ignores sessions with such flag.
  35. *
  36. * Added run-time check for version of command protocol
  37. * supported by the RTP proxy.
  38. *
  39. * 2004-01-16 Integrated slightly modified patch from Tristan Colgate,
  40. * force_rtp_proxy function with IP as a parameter (janakj)
  41. *
  42. * 2004-01-28 nat_uac_test extended to allow testing SDP body (sobomax)
  43. *
  44. * nat_uac_test extended to allow testing top Via (sobomax)
  45. *
  46. * 2004-02-21 force_rtp_proxy now accepts option argument, which
  47. * consists of string of chars, each of them turns "on"
  48. * some feature, currently supported ones are:
  49. *
  50. * `a' - flags that UA from which message is received
  51. * doesn't support symmetric RTP;
  52. * `l' - force "lookup", that is, only rewrite SDP when
  53. * corresponding session is already exists in the
  54. * RTP proxy. Only makes sense for SIP requests,
  55. * replies are always processed in "lookup" mode;
  56. * `i' - flags that message is received from UA in the
  57. * LAN. Only makes sense when RTP proxy is running
  58. * in the bridge mode.
  59. *
  60. * force_rtp_proxy can now be invoked without any arguments,
  61. * as previously, with one argument - in this case argument
  62. * is treated as option string and with two arguments, in
  63. * which case 1st argument is option string and the 2nd
  64. * one is IP address which have to be inserted into
  65. * SDP (IP address on which RTP proxy listens).
  66. *
  67. * 2004-03-12 Added support for IPv6 addresses in SDPs. Particularly,
  68. * force_rtp_proxy now can work with IPv6-aware RTP proxy,
  69. * replacing IPv4 address in SDP with IPv6 one and vice versa.
  70. * This allows creating full-fledged IPv4<->IPv6 gateway.
  71. * See 4to6.cfg file for example.
  72. *
  73. * Two new options added into force_rtp_proxy:
  74. *
  75. * `f' - instructs nathelper to ignore marks inserted
  76. * by another nathelper in transit to indicate
  77. * that the session is already goes through another
  78. * proxy. Allows creating chain of proxies.
  79. * `r' - flags that IP address in SDP should be trusted.
  80. * Without this flag, nathelper ignores address in the
  81. * SDP and uses source address of the SIP message
  82. * as media address which is passed to the RTP proxy.
  83. *
  84. * Protocol between nathelper and RTP proxy in bridge
  85. * mode has been slightly changed. Now RTP proxy expects SER
  86. * to provide 2 flags when creating or updating session
  87. * to indicate direction of this session. Each of those
  88. * flags can be either `e' or `i'. For example `ei' means
  89. * that we received INVITE from UA on the "external" network
  90. * network and will send it to the UA on "internal" one.
  91. * Also possible `ie' (internal->external), `ii'
  92. * (internal->internal) and `ee' (external->external). See
  93. * example file alg.cfg for details.
  94. *
  95. * 2004-03-15 If the rtp proxy test failed (wrong version or not started)
  96. * retry test from time to time, when some *rtpproxy* function
  97. * is invoked. Minimum interval between retries can be
  98. * configured via rtpproxy_disable_tout module parameter (default
  99. * is 60 seconds). Setting it to -1 will disable periodic
  100. * rechecks completely, setting it to 0 will force checks
  101. * for each *rtpproxy* function call. (andrei)
  102. *
  103. * 2004-03-22 Fix assignment of rtpproxy_retr and rtpproxy_tout module
  104. * parameters.
  105. *
  106. * 2004-03-22 Fix get_body position (should be called before get_callid)
  107. * (andrei)
  108. *
  109. * 2004-03-24 Fix newport for null ip address case (e.g onhold re-INVITE)
  110. * (andrei)
  111. *
  112. * 2004-09-30 added received port != via port test (andrei)
  113. *
  114. * 2004-10-10 force_socket option introduced (jiri)
  115. *
  116. * 2005-02-24 Added support for using more than one rtp proxy, in which
  117. * case traffic will be distributed evenly among them. In addition,
  118. * each such proxy can be assigned a weight, which will specify
  119. * which share of the traffic should be placed to this particular
  120. * proxy.
  121. *
  122. * Introduce failover mechanism, so that if SER detects that one
  123. * of many proxies is no longer available it temporarily decreases
  124. * its weight to 0, so that no traffic will be assigned to it.
  125. * Such "disabled" proxies are periodically checked to see if they
  126. * are back to normal in which case respective weight is restored
  127. * resulting in traffic being sent to that proxy again.
  128. *
  129. * Those features can be enabled by specifying more than one "URI"
  130. * in the rtpproxy_sock parameter, optionally followed by the weight,
  131. * which if absent is assumed to be 1, for example:
  132. *
  133. * rtpproxy_sock="unix:/foo/bar=4 udp:1.2.3.4:3456=3 udp:5.6.7.8:5432=1"
  134. *
  135. * 2005-02-25 Force for pinging the socket returned by USRLOC (bogdan)
  136. *
  137. * 2005-03-22 support for multiple media streams added (netch)
  138. *
  139. * 2005-07-11 SIP ping support added (bogdan)
  140. *
  141. * 2005-07-14 SDP origin (o=) IP may be also changed (bogdan)
  142. *
  143. * 2006-03-08 fix_nated_sdp() may take one more param to force a specific IP;
  144. * force_rtp_proxy() accepts a new flag 's' to swap creation/
  145. * confirmation between requests/replies;
  146. * add_rcv_param() may take as parameter a flag telling if the
  147. * parameter should go to the contact URI or contact header;
  148. * (bogdan)
  149. * 2006-03-28 Support for changing session-level SDP connection (c=) IP when
  150. * media-description also includes connection information (bayan)
  151. * 2007-04-13 Support multiple sets of rtpproxies and set selection added
  152. * (ancuta)
  153. * 2007-04-26 Added some MI commands:
  154. * nh_enable_ping used to enable or disable natping
  155. * nh_enable_rtpp used to enable or disable a specific rtp proxy
  156. * nh_show_rtpp used to display information for all rtp proxies
  157. * (ancuta)
  158. * 2007-05-09 New function start_recording() allowing to start recording RTP
  159. * session in the RTP proxy (Carsten Bock - ported from SER)
  160. * 2007-09-11 Separate timer process and support for multiple timer processes
  161. * (bogdan)
  162. * 2008-12-12 Support for RTCP attribute in the SDP
  163. * (Min Wang/BASIS AudioNet - ported from SER)
  164. * 2010-08-05 Core SDP parser integrated into nathelper (osas)
  165. * 2010-10-08 Removal of deprecated force_rtp_proxy and swap flag (osas)
  166. */
  167. #include <sys/types.h>
  168. #include <sys/socket.h>
  169. #include <sys/time.h>
  170. #include <netinet/in.h>
  171. #include <netinet/in_systm.h>
  172. #ifndef __USE_BSD
  173. #define __USE_BSD
  174. #endif
  175. #include <netinet/ip.h>
  176. #ifndef __FAVOR_BSD
  177. #define __FAVOR_BSD
  178. #endif
  179. #include <netinet/udp.h>
  180. #include <arpa/inet.h>
  181. #include <sys/uio.h>
  182. #include <sys/un.h>
  183. #include <ctype.h>
  184. #include <errno.h>
  185. #include <netdb.h>
  186. #include <poll.h>
  187. #include <stdio.h>
  188. #include <stdlib.h>
  189. #include <string.h>
  190. #include <unistd.h>
  191. #include "../../flags.h"
  192. #include "../../sr_module.h"
  193. #include "../../dprint.h"
  194. #include "../../data_lump.h"
  195. #include "../../data_lump_rpl.h"
  196. #include "../../error.h"
  197. #include "../../forward.h"
  198. #include "../../mem/mem.h"
  199. #include "../../parser/parse_from.h"
  200. #include "../../parser/parse_to.h"
  201. #include "../../parser/parse_uri.h"
  202. #include "../../parser/parser_f.h"
  203. #include "../../parser/sdp/sdp.h"
  204. #include "../../resolve.h"
  205. #include "../../timer.h"
  206. #include "../../trim.h"
  207. #include "../../ut.h"
  208. #include "../../pt.h"
  209. #include "../../timer_proc.h"
  210. #include "../../lib/kmi/mi.h"
  211. #include "../../pvar.h"
  212. #include "../../lvalue.h"
  213. #include "../../msg_translator.h"
  214. #include "../../usr_avp.h"
  215. #include "../../socket_info.h"
  216. #include "../../mod_fix.h"
  217. #include "../../dset.h"
  218. #include "../../route.h"
  219. #include "../../modules/tm/tm_load.h"
  220. #include "rtpproxy.h"
  221. #include "rtpproxy_funcs.h"
  222. #include "rtpproxy_stream.h"
  223. MODULE_VERSION
  224. #if !defined(AF_LOCAL)
  225. #define AF_LOCAL AF_UNIX
  226. #endif
  227. #if !defined(PF_LOCAL)
  228. #define PF_LOCAL PF_UNIX
  229. #endif
  230. /* NAT UAC test constants */
  231. #define NAT_UAC_TEST_C_1918 0x01
  232. #define NAT_UAC_TEST_RCVD 0x02
  233. #define NAT_UAC_TEST_V_1918 0x04
  234. #define NAT_UAC_TEST_S_1918 0x08
  235. #define NAT_UAC_TEST_RPORT 0x10
  236. #define DEFAULT_RTPP_SET_ID 0
  237. static str DEFAULT_RTPP_SET_ID_STR = str_init("0");
  238. #define MI_SET_NATPING_STATE "nh_enable_ping"
  239. #define MI_DEFAULT_NATPING_STATE 1
  240. #define MI_ENABLE_RTP_PROXY "nh_enable_rtpp"
  241. #define MI_MIN_RECHECK_TICKS 0
  242. #define MI_MAX_RECHECK_TICKS (unsigned int)-1
  243. #define MI_SHOW_RTP_PROXIES "nh_show_rtpp"
  244. #define MI_RTP_PROXY_NOT_FOUND "RTP proxy not found"
  245. #define MI_RTP_PROXY_NOT_FOUND_LEN (sizeof(MI_RTP_PROXY_NOT_FOUND)-1)
  246. #define MI_PING_DISABLED "NATping disabled from script"
  247. #define MI_PING_DISABLED_LEN (sizeof(MI_PING_DISABLED)-1)
  248. #define MI_SET "set"
  249. #define MI_SET_LEN (sizeof(MI_SET)-1)
  250. #define MI_INDEX "index"
  251. #define MI_INDEX_LEN (sizeof(MI_INDEX)-1)
  252. #define MI_DISABLED "disabled"
  253. #define MI_DISABLED_LEN (sizeof(MI_DISABLED)-1)
  254. #define MI_WEIGHT "weight"
  255. #define MI_WEIGHT_LEN (sizeof(MI_WEIGHT)-1)
  256. #define MI_RECHECK_TICKS "recheck_ticks"
  257. #define MI_RECHECK_T_LEN (sizeof(MI_RECHECK_TICKS)-1)
  258. /* Supported version of the RTP proxy command protocol */
  259. #define SUP_CPROTOVER 20040107
  260. /* Required additional version of the RTP proxy command protocol */
  261. #define REQ_CPROTOVER "20050322"
  262. /* Additional version necessary for re-packetization support */
  263. #define REP_CPROTOVER "20071116"
  264. #define PTL_CPROTOVER "20081102"
  265. #define CPORT "22222"
  266. static int extract_mediaip(str *, str *, int *, char *);
  267. static int alter_mediaip(struct sip_msg *, str *, str *, int, str *, int, int);
  268. static int alter_mediaport(struct sip_msg *, str *, str *, str *, int);
  269. static int alter_rtcp(struct sip_msg *msg, str *body, str *oldport, str *newport);
  270. static char *gencookie();
  271. static int rtpp_test(struct rtpp_node*, int, int);
  272. static int unforce_rtp_proxy1_f(struct sip_msg *, char *, char *);
  273. static int unforce_rtp_proxy(struct sip_msg *, char *);
  274. static int force_rtp_proxy(struct sip_msg *, char *, char *, int, int);
  275. static int start_recording_f(struct sip_msg *, char *, char *);
  276. static int rtpproxy_answer1_f(struct sip_msg *, char *, char *);
  277. static int rtpproxy_answer2_f(struct sip_msg *, char *, char *);
  278. static int rtpproxy_offer1_f(struct sip_msg *, char *, char *);
  279. static int rtpproxy_offer2_f(struct sip_msg *, char *, char *);
  280. static int rtpproxy_manage0(struct sip_msg *msg, char *flags, char *ip);
  281. static int rtpproxy_manage1(struct sip_msg *msg, char *flags, char *ip);
  282. static int rtpproxy_manage2(struct sip_msg *msg, char *flags, char *ip);
  283. static int add_rtpproxy_socks(struct rtpp_set * rtpp_list, char * rtpproxy);
  284. static int fixup_set_id(void ** param, int param_no);
  285. static int set_rtp_proxy_set_f(struct sip_msg * msg, char * str1, char * str2);
  286. static struct rtpp_set * select_rtpp_set(int id_set);
  287. static int rtpproxy_set_store(modparam_t type, void * val);
  288. static int rtpproxy_add_rtpproxy_set( char * rtp_proxies);
  289. static int mod_init(void);
  290. static int child_init(int);
  291. static void mod_destroy(void);
  292. /* Pseudo-Variables */
  293. static int pv_get_rtpstat_f(struct sip_msg *, pv_param_t *, pv_value_t *);
  294. /*mi commands*/
  295. static struct mi_root* mi_enable_rtp_proxy(struct mi_root* cmd_tree,
  296. void* param );
  297. static struct mi_root* mi_show_rtpproxies(struct mi_root* cmd_tree,
  298. void* param);
  299. static int rtpproxy_disable_tout = 60;
  300. static int rtpproxy_retr = 5;
  301. static int rtpproxy_tout = 1;
  302. static pid_t mypid;
  303. static unsigned int myseqn = 0;
  304. static str nortpproxy_str = str_init("a=nortpproxy:yes");
  305. static str extra_id_pv_param = {NULL, 0};
  306. static char ** rtpp_strings=0;
  307. static int rtpp_sets=0; /*used in rtpproxy_set_store()*/
  308. static int rtpp_set_count = 0;
  309. static unsigned int current_msg_id = (unsigned int)-1;
  310. /* RTP proxy balancing list */
  311. struct rtpp_set_head * rtpp_set_list =0;
  312. struct rtpp_set * selected_rtpp_set =0;
  313. struct rtpp_set * default_rtpp_set=0;
  314. static char *ice_candidate_priority_avp_param = NULL;
  315. static int ice_candidate_priority_avp_type;
  316. static int_str ice_candidate_priority_avp;
  317. static str rtp_inst_pv_param = {NULL, 0};
  318. static pv_spec_t *rtp_inst_pvar = NULL;
  319. /* array with the sockets used by rtpporxy (per process)*/
  320. static unsigned int rtpp_no = 0;
  321. static int *rtpp_socks = 0;
  322. typedef struct rtpp_set_link {
  323. struct rtpp_set *rset;
  324. pv_spec_t *rpv;
  325. } rtpp_set_link_t;
  326. /* tm */
  327. static struct tm_binds tmb;
  328. /*0-> disabled, 1 ->enabled*/
  329. unsigned int *natping_state=0;
  330. static str timeout_socket_str = {0, 0};
  331. static pv_elem_t *extra_id_pv = NULL;
  332. static cmd_export_t cmds[] = {
  333. {"set_rtp_proxy_set", (cmd_function)set_rtp_proxy_set_f, 1,
  334. fixup_set_id, 0,
  335. ANY_ROUTE},
  336. {"unforce_rtp_proxy", (cmd_function)unforce_rtp_proxy1_f, 0,
  337. 0, 0,
  338. ANY_ROUTE},
  339. {"rtpproxy_destroy", (cmd_function)unforce_rtp_proxy1_f, 0,
  340. 0, 0,
  341. ANY_ROUTE},
  342. {"unforce_rtp_proxy", (cmd_function)unforce_rtp_proxy1_f, 1,
  343. fixup_spve_null, 0,
  344. ANY_ROUTE},
  345. {"rtpproxy_destroy", (cmd_function)unforce_rtp_proxy1_f, 1,
  346. fixup_spve_null, 0,
  347. ANY_ROUTE},
  348. {"start_recording", (cmd_function)start_recording_f, 0,
  349. 0, 0,
  350. ANY_ROUTE },
  351. {"rtpproxy_offer", (cmd_function)rtpproxy_offer1_f, 0,
  352. 0, 0,
  353. ANY_ROUTE},
  354. {"rtpproxy_offer", (cmd_function)rtpproxy_offer1_f, 1,
  355. fixup_spve_null, 0,
  356. ANY_ROUTE},
  357. {"rtpproxy_offer", (cmd_function)rtpproxy_offer2_f, 2,
  358. fixup_spve_spve, 0,
  359. ANY_ROUTE},
  360. {"rtpproxy_answer", (cmd_function)rtpproxy_answer1_f, 0,
  361. 0, 0,
  362. ANY_ROUTE},
  363. {"rtpproxy_answer", (cmd_function)rtpproxy_answer1_f, 1,
  364. fixup_spve_null, 0,
  365. ANY_ROUTE},
  366. {"rtpproxy_answer", (cmd_function)rtpproxy_answer2_f, 2,
  367. fixup_spve_spve, 0,
  368. ANY_ROUTE},
  369. {"rtpproxy_stream2uac",(cmd_function)rtpproxy_stream2uac2_f, 2,
  370. fixup_var_str_int, 0,
  371. ANY_ROUTE },
  372. {"rtpproxy_stream2uas",(cmd_function)rtpproxy_stream2uas2_f, 2,
  373. fixup_var_str_int, 0,
  374. ANY_ROUTE },
  375. {"rtpproxy_stop_stream2uac",(cmd_function)rtpproxy_stop_stream2uac2_f,0,
  376. NULL, 0,
  377. ANY_ROUTE },
  378. {"rtpproxy_stop_stream2uas",(cmd_function)rtpproxy_stop_stream2uas2_f,0,
  379. NULL, 0,
  380. ANY_ROUTE },
  381. {"rtpproxy_manage", (cmd_function)rtpproxy_manage0, 0,
  382. 0, 0,
  383. ANY_ROUTE},
  384. {"rtpproxy_manage", (cmd_function)rtpproxy_manage1, 1,
  385. fixup_spve_null, fixup_free_spve_null,
  386. ANY_ROUTE},
  387. {"rtpproxy_manage", (cmd_function)rtpproxy_manage2, 2,
  388. fixup_spve_spve, fixup_free_spve_spve,
  389. ANY_ROUTE},
  390. {0, 0, 0, 0, 0, 0}
  391. };
  392. static pv_export_t mod_pvs[] = {
  393. {{"rtpstat", (sizeof("rtpstat")-1)}, /* RTP-Statistics */
  394. PVT_OTHER, pv_get_rtpstat_f, 0, 0, 0, 0, 0},
  395. {{0, 0}, 0, 0, 0, 0, 0, 0, 0}
  396. };
  397. static param_export_t params[] = {
  398. {"nortpproxy_str", PARAM_STR, &nortpproxy_str },
  399. {"rtpproxy_sock", PARAM_STRING|USE_FUNC_PARAM,
  400. (void*)rtpproxy_set_store },
  401. {"rtpproxy_disable_tout", INT_PARAM, &rtpproxy_disable_tout },
  402. {"rtpproxy_retr", INT_PARAM, &rtpproxy_retr },
  403. {"rtpproxy_tout", INT_PARAM, &rtpproxy_tout },
  404. {"timeout_socket", PARAM_STR, &timeout_socket_str },
  405. {"ice_candidate_priority_avp", PARAM_STRING,
  406. &ice_candidate_priority_avp_param},
  407. {"extra_id_pv", PARAM_STR, &extra_id_pv_param },
  408. {"db_url", PARAM_STR, &rtpp_db_url },
  409. {"table_name", PARAM_STR, &rtpp_table_name },
  410. {"rtp_inst_pvar", PARAM_STR, &rtp_inst_pv_param },
  411. {0, 0, 0}
  412. };
  413. static mi_export_t mi_cmds[] = {
  414. {MI_ENABLE_RTP_PROXY, mi_enable_rtp_proxy, 0, 0, 0},
  415. {MI_SHOW_RTP_PROXIES, mi_show_rtpproxies, MI_NO_INPUT_FLAG, 0, 0},
  416. { 0, 0, 0, 0, 0}
  417. };
  418. struct module_exports exports = {
  419. "rtpproxy",
  420. DEFAULT_DLFLAGS, /* dlopen flags */
  421. cmds,
  422. params,
  423. 0, /* exported statistics */
  424. mi_cmds, /* exported MI functions */
  425. mod_pvs, /* exported pseudo-variables */
  426. 0, /* extra processes */
  427. mod_init,
  428. 0, /* reply processing */
  429. mod_destroy, /* destroy function */
  430. child_init
  431. };
  432. static int rtpproxy_set_store(modparam_t type, void * val){
  433. char * p;
  434. int len;
  435. p = (char* )val;
  436. if(p==0 || *p=='\0'){
  437. return 0;
  438. }
  439. if(rtpp_sets==0){
  440. rtpp_strings = (char**)pkg_malloc(sizeof(char*));
  441. if(!rtpp_strings){
  442. LM_ERR("no pkg memory left\n");
  443. return -1;
  444. }
  445. } else {/*realloc to make room for the current set*/
  446. rtpp_strings = (char**)pkg_realloc(rtpp_strings,
  447. (rtpp_sets+1)* sizeof(char*));
  448. if(!rtpp_strings){
  449. LM_ERR("no pkg memory left\n");
  450. return -1;
  451. }
  452. }
  453. /*allocate for the current set of urls*/
  454. len = strlen(p);
  455. rtpp_strings[rtpp_sets] = (char*)pkg_malloc((len+1)*sizeof(char));
  456. if(!rtpp_strings[rtpp_sets]){
  457. LM_ERR("no pkg memory left\n");
  458. return -1;
  459. }
  460. memcpy(rtpp_strings[rtpp_sets], p, len);
  461. rtpp_strings[rtpp_sets][len] = '\0';
  462. rtpp_sets++;
  463. return 0;
  464. }
  465. struct rtpp_set *get_rtpp_set(str *const set_name)
  466. {
  467. unsigned int this_set_id;
  468. struct rtpp_set *rtpp_list;
  469. if (rtpp_set_list == NULL)
  470. {
  471. LM_ERR("rtpp set list not configured\n");
  472. return NULL;
  473. }
  474. /* Only integer set_names are valid at the moment */
  475. if ((set_name->s == NULL) || (set_name->len == 0))
  476. {
  477. LM_ERR("Invalid set name '%.*s'\n", set_name->len, set_name->s);
  478. return NULL;
  479. }
  480. if (str2int(set_name, &this_set_id) < 0)
  481. {
  482. LM_ERR("Invalid set name '%.*s' - must be integer\n", set_name->len, set_name->s);
  483. return NULL;
  484. }
  485. rtpp_list = select_rtpp_set(this_set_id);
  486. if(rtpp_list==NULL){ /*if a new id_set : add a new set of rtpp*/
  487. rtpp_list = shm_malloc(sizeof(struct rtpp_set));
  488. if(!rtpp_list){
  489. LM_ERR("no shm memory left\n");
  490. return NULL;
  491. }
  492. memset(rtpp_list, 0, sizeof(struct rtpp_set));
  493. rtpp_list->id_set = this_set_id;
  494. if (rtpp_set_list->rset_first == NULL)
  495. {
  496. rtpp_set_list->rset_first = rtpp_list;
  497. } else {
  498. rtpp_set_list->rset_last->rset_next = rtpp_list;
  499. }
  500. rtpp_set_list->rset_last = rtpp_list;
  501. rtpp_set_count++;
  502. if (this_set_id == DEFAULT_RTPP_SET_ID)
  503. {
  504. default_rtpp_set = rtpp_list;
  505. }
  506. }
  507. return rtpp_list;
  508. }
  509. int insert_rtpp_node(struct rtpp_set *const rtpp_list, const str *const url, const int weight, const int disabled)
  510. {
  511. struct rtpp_node *pnode;
  512. if ((pnode = shm_malloc(sizeof(struct rtpp_node) + url->len + 1)) == NULL)
  513. {
  514. LM_ERR("out of shm memory\n");
  515. return -1;
  516. }
  517. memset(pnode, 0, sizeof(struct rtpp_node) + url->len + 1);
  518. pnode->idx = rtpp_no++;
  519. pnode->rn_weight = weight;
  520. pnode->rn_umode = 0;
  521. pnode->rn_disabled = disabled;
  522. /* Permanently disable if marked as disabled */
  523. pnode->rn_recheck_ticks = disabled ? MI_MAX_RECHECK_TICKS : 0;
  524. pnode->rn_url.s = (char*)(pnode + 1);
  525. memcpy(pnode->rn_url.s, url->s, url->len);
  526. pnode->rn_url.len = url->len;
  527. LM_DBG("url is '%.*s'\n", pnode->rn_url.len, pnode->rn_url.s);
  528. /* Find protocol and store address */
  529. pnode->rn_address = pnode->rn_url.s;
  530. if (strncasecmp(pnode->rn_address, "udp:", 4) == 0) {
  531. pnode->rn_umode = 1;
  532. pnode->rn_address += 4;
  533. } else if (strncasecmp(pnode->rn_address, "udp6:", 5) == 0) {
  534. pnode->rn_umode = 6;
  535. pnode->rn_address += 5;
  536. } else if (strncasecmp(pnode->rn_address, "unix:", 5) == 0) {
  537. pnode->rn_umode = 0;
  538. pnode->rn_address += 5;
  539. }
  540. if (rtpp_list->rn_first == NULL)
  541. {
  542. rtpp_list->rn_first = pnode;
  543. } else {
  544. rtpp_list->rn_last->rn_next = pnode;
  545. }
  546. rtpp_list->rn_last = pnode;
  547. rtpp_list->rtpp_node_count++;
  548. return 0;
  549. }
  550. static int add_rtpproxy_socks(struct rtpp_set * rtpp_list,
  551. char * rtpproxy){
  552. /* Make rtp proxies list. */
  553. char *p, *p1, *p2, *plim;
  554. int weight;
  555. str url;
  556. p = rtpproxy;
  557. plim = p + strlen(p);
  558. for(;;) {
  559. weight = 1;
  560. while (*p && isspace((int)*p))
  561. ++p;
  562. if (p >= plim)
  563. break;
  564. p1 = p;
  565. while (*p && !isspace((int)*p))
  566. ++p;
  567. if (p <= p1)
  568. break; /* may happen??? */
  569. /* Have weight specified? If yes, scan it */
  570. p2 = memchr(p1, '=', p - p1);
  571. if (p2 != NULL) {
  572. weight = strtoul(p2 + 1, NULL, 10);
  573. } else {
  574. p2 = p;
  575. }
  576. url.s = p1;
  577. url.len = (p2-p1);
  578. insert_rtpp_node(rtpp_list, &url, weight, 0);
  579. }
  580. return 0;
  581. }
  582. /* 0-succes
  583. * -1 - erorr
  584. * */
  585. static int rtpproxy_add_rtpproxy_set( char * rtp_proxies)
  586. {
  587. char *p,*p2;
  588. struct rtpp_set * rtpp_list;
  589. str id_set;
  590. /* empty definition? */
  591. p= rtp_proxies;
  592. if(!p || *p=='\0'){
  593. return 0;
  594. }
  595. for(;*p && isspace(*p);p++);
  596. if(*p=='\0'){
  597. return 0;
  598. }
  599. rtp_proxies = strstr(p, "==");
  600. if(rtp_proxies){
  601. if(*(rtp_proxies +2)=='\0'){
  602. LM_ERR("script error -invalid rtp proxy list!\n");
  603. return -1;
  604. }
  605. *rtp_proxies = '\0';
  606. p2 = rtp_proxies-1;
  607. for(;isspace(*p2); *p2 = '\0',p2--);
  608. id_set.s = p; id_set.len = p2 - p+1;
  609. if(id_set.len <= 0){
  610. LM_ERR("script error -invalid set_id value!\n");
  611. return -1;
  612. }
  613. rtp_proxies+=2;
  614. }else{
  615. rtp_proxies = p;
  616. id_set = DEFAULT_RTPP_SET_ID_STR;
  617. }
  618. for(;*rtp_proxies && isspace(*rtp_proxies);rtp_proxies++);
  619. if(!(*rtp_proxies)){
  620. LM_ERR("script error -empty rtp_proxy list\n");
  621. return -1;;
  622. }
  623. rtpp_list = get_rtpp_set(&id_set);
  624. if (rtpp_list == NULL)
  625. {
  626. LM_ERR("Failed to get or create rtpp_list for '%.*s'\n", id_set.len, id_set.s);
  627. return -1;
  628. }
  629. if(add_rtpproxy_socks(rtpp_list, rtp_proxies)!= 0){
  630. return -1;
  631. }
  632. return 0;
  633. }
  634. static int fixup_set_id(void ** param, int param_no)
  635. {
  636. int int_val, err;
  637. struct rtpp_set* rtpp_list;
  638. rtpp_set_link_t *rtpl = NULL;
  639. str s;
  640. rtpl = (rtpp_set_link_t*)pkg_malloc(sizeof(rtpp_set_link_t));
  641. if(rtpl==NULL) {
  642. LM_ERR("no more pkg memory\n");
  643. return -1;
  644. }
  645. memset(rtpl, 0, sizeof(rtpp_set_link_t));
  646. s.s = (char*)*param;
  647. s.len = strlen(s.s);
  648. if(s.s[0] == PV_MARKER) {
  649. int_val = pv_locate_name(&s);
  650. if(int_val<0 || int_val!=s.len) {
  651. LM_ERR("invalid parameter %s\n", s.s);
  652. return -1;
  653. }
  654. rtpl->rpv = pv_cache_get(&s);
  655. if(rtpl->rpv == NULL) {
  656. LM_ERR("invalid pv parameter %s\n", s.s);
  657. return -1;
  658. }
  659. } else {
  660. int_val = str2s(*param, strlen(*param), &err);
  661. if (err == 0) {
  662. pkg_free(*param);
  663. if((rtpp_list = select_rtpp_set(int_val)) ==0){
  664. LM_ERR("rtpp_proxy set %i not configured\n", int_val);
  665. return E_CFG;
  666. }
  667. rtpl->rset = rtpp_list;
  668. } else {
  669. LM_ERR("bad number <%s>\n", (char *)(*param));
  670. return E_CFG;
  671. }
  672. }
  673. *param = (void*)rtpl;
  674. return 0;
  675. }
  676. static struct mi_root* mi_enable_rtp_proxy(struct mi_root* cmd_tree,
  677. void* param )
  678. { struct mi_node* node;
  679. str rtpp_url;
  680. unsigned int enable;
  681. struct rtpp_set * rtpp_list;
  682. struct rtpp_node * crt_rtpp;
  683. int found;
  684. found = 0;
  685. if(rtpp_set_list ==NULL)
  686. goto end;
  687. node = cmd_tree->node.kids;
  688. if(node == NULL)
  689. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  690. if(node->value.s == NULL || node->value.len ==0)
  691. return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  692. rtpp_url = node->value;
  693. node = node->next;
  694. if(node == NULL)
  695. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  696. enable = 0;
  697. if( strno2int( &node->value, &enable) <0)
  698. goto error;
  699. for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL;
  700. rtpp_list = rtpp_list->rset_next){
  701. for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
  702. crt_rtpp = crt_rtpp->rn_next){
  703. /*found a matching rtpp*/
  704. if(crt_rtpp->rn_url.len == rtpp_url.len){
  705. if(strncmp(crt_rtpp->rn_url.s, rtpp_url.s, rtpp_url.len) == 0){
  706. /*set the enabled/disabled status*/
  707. found = 1;
  708. crt_rtpp->rn_recheck_ticks =
  709. enable? MI_MIN_RECHECK_TICKS : MI_MAX_RECHECK_TICKS;
  710. crt_rtpp->rn_disabled = enable?0:1;
  711. }
  712. }
  713. }
  714. }
  715. end:
  716. if(found)
  717. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  718. return init_mi_tree(404,MI_RTP_PROXY_NOT_FOUND,MI_RTP_PROXY_NOT_FOUND_LEN);
  719. error:
  720. return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  721. }
  722. #define add_rtpp_node_int_info(_parent, _name, _name_len, _value, _child,\
  723. _len, _string, _error)\
  724. do {\
  725. (_string) = int2str((_value), &(_len));\
  726. if((_string) == 0){\
  727. LM_ERR("cannot convert int value\n");\
  728. goto _error;\
  729. }\
  730. if(((_child) = add_mi_node_child((_parent), MI_DUP_VALUE, (_name), \
  731. (_name_len), (_string), (_len)) ) == 0)\
  732. goto _error;\
  733. }while(0);
  734. static struct mi_root* mi_show_rtpproxies(struct mi_root* cmd_tree,
  735. void* param)
  736. {
  737. struct mi_node* node, *crt_node, *child;
  738. struct mi_root* root;
  739. struct mi_attr * attr;
  740. struct rtpp_set * rtpp_list;
  741. struct rtpp_node * crt_rtpp;
  742. char * string, *id;
  743. int id_len, len;
  744. string = id = 0;
  745. root = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  746. if (!root) {
  747. LM_ERR("the MI tree cannot be initialized!\n");
  748. return 0;
  749. }
  750. if(rtpp_set_list ==NULL)
  751. return root;
  752. node = &root->node;
  753. for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL;
  754. rtpp_list = rtpp_list->rset_next){
  755. for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
  756. crt_rtpp = crt_rtpp->rn_next){
  757. id = int2str(rtpp_list->id_set, &id_len);
  758. if(!id){
  759. LM_ERR("cannot convert set id\n");
  760. goto error;
  761. }
  762. if(!(crt_node = add_mi_node_child(node, 0, crt_rtpp->rn_url.s,
  763. crt_rtpp->rn_url.len, 0,0)) ) {
  764. LM_ERR("cannot add the child node to the tree\n");
  765. goto error;
  766. }
  767. LM_DBG("adding node name %s \n",crt_rtpp->rn_url.s );
  768. if((attr = add_mi_attr(crt_node, MI_DUP_VALUE, MI_SET, MI_SET_LEN,
  769. id, id_len))== 0){
  770. LM_ERR("cannot add attributes to the node\n");
  771. goto error;
  772. }
  773. add_rtpp_node_int_info(crt_node, MI_INDEX, MI_INDEX_LEN,
  774. crt_rtpp->idx, child, len,string,error);
  775. add_rtpp_node_int_info(crt_node, MI_DISABLED, MI_DISABLED_LEN,
  776. crt_rtpp->rn_disabled, child, len,string,error);
  777. add_rtpp_node_int_info(crt_node, MI_WEIGHT, MI_WEIGHT_LEN,
  778. crt_rtpp->rn_weight, child, len, string,error);
  779. add_rtpp_node_int_info(crt_node, MI_RECHECK_TICKS,MI_RECHECK_T_LEN,
  780. crt_rtpp->rn_recheck_ticks, child, len, string, error);
  781. }
  782. }
  783. return root;
  784. error:
  785. if (root)
  786. free_mi_tree(root);
  787. return 0;
  788. }
  789. static int
  790. mod_init(void)
  791. {
  792. int i;
  793. pv_spec_t avp_spec;
  794. str s;
  795. unsigned short avp_flags;
  796. if(register_mi_mod(exports.name, mi_cmds)!=0)
  797. {
  798. LM_ERR("failed to register MI commands\n");
  799. return -1;
  800. }
  801. /* Configure the head of the rtpp_set_list */
  802. rtpp_set_list = shm_malloc(sizeof(struct rtpp_set_head));
  803. if (rtpp_set_list == NULL)
  804. {
  805. LM_ERR("no shm memory for rtpp_set_list\n");
  806. return -1;
  807. }
  808. memset(rtpp_set_list, 0, sizeof(struct rtpp_set_head));
  809. if (nortpproxy_str.s==NULL || nortpproxy_str.len<=0) {
  810. nortpproxy_str.len = 0;
  811. } else {
  812. while (nortpproxy_str.len > 0 && (nortpproxy_str.s[nortpproxy_str.len - 1] == '\r' ||
  813. nortpproxy_str.s[nortpproxy_str.len - 1] == '\n'))
  814. nortpproxy_str.len--;
  815. }
  816. if (rtpp_db_url.s != NULL)
  817. {
  818. init_rtpproxy_db();
  819. if (rtpp_sets > 0)
  820. {
  821. LM_WARN("rtpproxy db url configured - ignoring modparam sets\n");
  822. }
  823. }
  824. /* storing the list of rtp proxy sets in shared memory*/
  825. for(i=0;i<rtpp_sets;i++){
  826. LM_DBG("Adding RTP-Proxy set %d/%d: %s\n", i, rtpp_sets, rtpp_strings[i]);
  827. if ((rtpp_db_url.s == NULL) &&
  828. (rtpproxy_add_rtpproxy_set(rtpp_strings[i]) != 0)) {
  829. for(;i<rtpp_sets;i++)
  830. if(rtpp_strings[i])
  831. pkg_free(rtpp_strings[i]);
  832. pkg_free(rtpp_strings);
  833. LM_ERR("Failed to add RTP-Proxy from Config!\n");
  834. return -1;
  835. }
  836. if(rtpp_strings[i])
  837. pkg_free(rtpp_strings[i]);
  838. }
  839. if (ice_candidate_priority_avp_param) {
  840. s.s = ice_candidate_priority_avp_param; s.len = strlen(s.s);
  841. if (pv_parse_spec(&s, &avp_spec) == 0 || avp_spec.type != PVT_AVP) {
  842. LM_ERR("malformed or non AVP definition <%s>\n", ice_candidate_priority_avp_param);
  843. return -1;
  844. }
  845. if (pv_get_avp_name(0, &(avp_spec.pvp), &ice_candidate_priority_avp, &avp_flags) != 0) {
  846. LM_ERR("invalid AVP definition <%s>\n", ice_candidate_priority_avp_param);
  847. return -1;
  848. }
  849. ice_candidate_priority_avp_type = avp_flags;
  850. }
  851. if (rtp_inst_pv_param.s) {
  852. rtp_inst_pvar = pv_cache_get(&rtp_inst_pv_param);
  853. if ((rtp_inst_pvar == NULL) ||
  854. ((rtp_inst_pvar->type != PVT_AVP) &&
  855. (rtp_inst_pvar->type != PVT_XAVP) &&
  856. (rtp_inst_pvar->type != PVT_SCRIPTVAR))) {
  857. LM_ERR("Invalid pvar name <%.*s>\n", rtp_inst_pv_param.len, rtp_inst_pv_param.s);
  858. return -1;
  859. }
  860. }
  861. if (extra_id_pv_param.s && *extra_id_pv_param.s) {
  862. if(pv_parse_format(&extra_id_pv_param, &extra_id_pv) < 0) {
  863. LM_ERR("malformed PV string: %s\n", extra_id_pv_param.s);
  864. return -1;
  865. }
  866. } else {
  867. extra_id_pv = NULL;
  868. }
  869. if (rtpp_strings)
  870. pkg_free(rtpp_strings);
  871. if (load_tm_api( &tmb ) < 0)
  872. {
  873. LM_DBG("could not load the TM-functions - answer-offer model"
  874. " auto-detection is disabled\n");
  875. memset(&tmb, 0, sizeof(struct tm_binds));
  876. }
  877. return 0;
  878. }
  879. static int
  880. child_init(int rank)
  881. {
  882. int n;
  883. char *cp;
  884. struct addrinfo hints, *res;
  885. struct rtpp_set *rtpp_list;
  886. struct rtpp_node *pnode;
  887. if(rtpp_set_list==NULL )
  888. return 0;
  889. /* Iterate known RTP proxies - create sockets */
  890. mypid = getpid();
  891. rtpp_socks = (int*)pkg_malloc( sizeof(int)*rtpp_no );
  892. if (rtpp_socks==NULL) {
  893. LM_ERR("no more pkg memory\n");
  894. return -1;
  895. }
  896. for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != 0;
  897. rtpp_list = rtpp_list->rset_next){
  898. for (pnode=rtpp_list->rn_first; pnode!=0; pnode = pnode->rn_next){
  899. char *hostname;
  900. if (pnode->rn_umode == 0) {
  901. rtpp_socks[pnode->idx] = -1;
  902. goto rptest;
  903. }
  904. /*
  905. * This is UDP or UDP6. Detect host and port; lookup host;
  906. * do connect() in order to specify peer address
  907. */
  908. hostname = (char*)pkg_malloc(sizeof(char) * (strlen(pnode->rn_address) + 1));
  909. if (hostname==NULL) {
  910. LM_ERR("no more pkg memory\n");
  911. return -1;
  912. }
  913. strcpy(hostname, pnode->rn_address);
  914. cp = strrchr(hostname, ':');
  915. if (cp != NULL) {
  916. *cp = '\0';
  917. cp++;
  918. }
  919. if (cp == NULL || *cp == '\0')
  920. cp = CPORT;
  921. memset(&hints, 0, sizeof(hints));
  922. hints.ai_flags = 0;
  923. hints.ai_family = (pnode->rn_umode == 6) ? AF_INET6 : AF_INET;
  924. hints.ai_socktype = SOCK_DGRAM;
  925. if ((n = getaddrinfo(hostname, cp, &hints, &res)) != 0) {
  926. LM_ERR("%s\n", gai_strerror(n));
  927. pkg_free(hostname);
  928. return -1;
  929. }
  930. pkg_free(hostname);
  931. rtpp_socks[pnode->idx] = socket((pnode->rn_umode == 6)
  932. ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
  933. if ( rtpp_socks[pnode->idx] == -1) {
  934. LM_ERR("can't create socket\n");
  935. freeaddrinfo(res);
  936. return -1;
  937. }
  938. if (connect( rtpp_socks[pnode->idx], res->ai_addr, res->ai_addrlen) == -1) {
  939. LM_ERR("can't connect to a RTP proxy\n");
  940. close( rtpp_socks[pnode->idx] );
  941. rtpp_socks[pnode->idx] = -1;
  942. freeaddrinfo(res);
  943. return -1;
  944. }
  945. freeaddrinfo(res);
  946. rptest:
  947. pnode->rn_disabled = rtpp_test(pnode, pnode->rn_disabled, 1);
  948. }
  949. }
  950. return 0;
  951. }
  952. static void mod_destroy(void)
  953. {
  954. struct rtpp_set * crt_list, * last_list;
  955. struct rtpp_node * crt_rtpp, *last_rtpp;
  956. /*free the shared memory*/
  957. if (natping_state)
  958. shm_free(natping_state);
  959. if(rtpp_set_list == NULL)
  960. return;
  961. for(crt_list = rtpp_set_list->rset_first; crt_list != NULL; ){
  962. for(crt_rtpp = crt_list->rn_first; crt_rtpp != NULL; ){
  963. last_rtpp = crt_rtpp;
  964. crt_rtpp = last_rtpp->rn_next;
  965. shm_free(last_rtpp);
  966. }
  967. last_list = crt_list;
  968. crt_list = last_list->rset_next;
  969. shm_free(last_list);
  970. }
  971. shm_free(rtpp_set_list);
  972. }
  973. static int
  974. isnulladdr(str *sx, int pf)
  975. {
  976. char *cp;
  977. if (pf == AF_INET6) {
  978. for(cp = sx->s; cp < sx->s + sx->len; cp++)
  979. if (*cp != '0' && *cp != ':')
  980. return 0;
  981. return 1;
  982. }
  983. return (sx->len == 7 && memcmp("0.0.0.0", sx->s, 7) == 0);
  984. }
  985. #define ADD_ADIRECTION 0x01
  986. #define FIX_MEDIP 0x02
  987. #define ADD_ANORTPPROXY 0x04
  988. #define FIX_ORGIP 0x08
  989. #define ADIRECTION "a=direction:active"
  990. #define ADIRECTION_LEN (sizeof(ADIRECTION) - 1)
  991. #define AOLDMEDIP "a=oldmediaip:"
  992. #define AOLDMEDIP_LEN (sizeof(AOLDMEDIP) - 1)
  993. #define AOLDMEDIP6 "a=oldmediaip6:"
  994. #define AOLDMEDIP6_LEN (sizeof(AOLDMEDIP6) - 1)
  995. #define AOLDMEDPRT "a=oldmediaport:"
  996. #define AOLDMEDPRT_LEN (sizeof(AOLDMEDPRT) - 1)
  997. static inline int
  998. replace_sdp_ip(struct sip_msg* msg, str *org_body, char *line, str *ip)
  999. {
  1000. str body1, oldip, newip;
  1001. str body = *org_body;
  1002. unsigned hasreplaced = 0;
  1003. int pf, pf1 = 0;
  1004. str body2;
  1005. char *bodylimit = body.s + body.len;
  1006. /* Iterate all lines and replace ips in them. */
  1007. if (!ip) {
  1008. newip.s = ip_addr2a(&msg->rcv.src_ip);
  1009. newip.len = strlen(newip.s);
  1010. } else {
  1011. newip = *ip;
  1012. }
  1013. body1 = body;
  1014. for(;;) {
  1015. if (extract_mediaip(&body1, &oldip, &pf,line) == -1)
  1016. break;
  1017. if (pf != AF_INET) {
  1018. LM_ERR("not an IPv4 address in '%s' SDP\n",line);
  1019. return -1;
  1020. }
  1021. if (!pf1)
  1022. pf1 = pf;
  1023. else if (pf != pf1) {
  1024. LM_ERR("mismatching address families in '%s' SDP\n",line);
  1025. return -1;
  1026. }
  1027. body2.s = oldip.s + oldip.len;
  1028. body2.len = bodylimit - body2.s;
  1029. if (alter_mediaip(msg, &body1, &oldip, pf, &newip, pf,1) == -1) {
  1030. LM_ERR("can't alter '%s' IP\n",line);
  1031. return -1;
  1032. }
  1033. hasreplaced = 1;
  1034. body1 = body2;
  1035. }
  1036. if (!hasreplaced) {
  1037. LM_ERR("can't extract '%s' IP from the SDP\n",line);
  1038. return -1;
  1039. }
  1040. return 0;
  1041. }
  1042. static int
  1043. extract_mediaip(str *body, str *mediaip, int *pf, char *line)
  1044. {
  1045. char *cp, *cp1;
  1046. int len, nextisip;
  1047. cp1 = NULL;
  1048. for (cp = body->s; (len = body->s + body->len - cp) > 0;) {
  1049. cp1 = ser_memmem(cp, line, len, 2);
  1050. if (cp1 == NULL || cp1[-1] == '\n' || cp1[-1] == '\r')
  1051. break;
  1052. cp = cp1 + 2;
  1053. }
  1054. if (cp1 == NULL)
  1055. return -1;
  1056. mediaip->s = cp1 + 2;
  1057. mediaip->len = eat_line(mediaip->s, body->s + body->len - mediaip->s) - mediaip->s;
  1058. trim_len(mediaip->len, mediaip->s, *mediaip);
  1059. nextisip = 0;
  1060. for (cp = mediaip->s; cp < mediaip->s + mediaip->len;) {
  1061. len = eat_token_end(cp, mediaip->s + mediaip->len) - cp;
  1062. if (nextisip == 1) {
  1063. mediaip->s = cp;
  1064. mediaip->len = len;
  1065. nextisip++;
  1066. break;
  1067. }
  1068. if (len == 3 && memcmp(cp, "IP", 2) == 0) {
  1069. switch (cp[2]) {
  1070. case '4':
  1071. nextisip = 1;
  1072. *pf = AF_INET;
  1073. break;
  1074. case '6':
  1075. nextisip = 1;
  1076. *pf = AF_INET6;
  1077. break;
  1078. default:
  1079. break;
  1080. }
  1081. }
  1082. cp = eat_space_end(cp + len, mediaip->s + mediaip->len);
  1083. }
  1084. if (nextisip != 2 || mediaip->len == 0) {
  1085. LM_ERR("no `IP[4|6]' in `%s' field\n",line);
  1086. return -1;
  1087. }
  1088. return 1;
  1089. }
  1090. static int
  1091. alter_mediaip(struct sip_msg *msg, str *body, str *oldip, int oldpf,
  1092. str *newip, int newpf, int preserve)
  1093. {
  1094. char *buf;
  1095. int offset;
  1096. struct lump* anchor;
  1097. str omip, nip, oip;
  1098. /* check that updating mediaip is really necessary */
  1099. if (oldpf == newpf && isnulladdr(oldip, oldpf))
  1100. return 0;
  1101. if (newip->len == oldip->len &&
  1102. memcmp(newip->s, oldip->s, newip->len) == 0)
  1103. return 0;
  1104. if (preserve != 0) {
  1105. anchor = anchor_lump(msg, body->s + body->len - msg->buf, 0, 0);
  1106. if (anchor == NULL) {
  1107. LM_ERR("anchor_lump failed\n");
  1108. return -1;
  1109. }
  1110. if (oldpf == AF_INET6) {
  1111. omip.s = AOLDMEDIP6;
  1112. omip.len = AOLDMEDIP6_LEN;
  1113. } else {
  1114. omip.s = AOLDMEDIP;
  1115. omip.len = AOLDMEDIP_LEN;
  1116. }
  1117. buf = pkg_malloc(omip.len + oldip->len + CRLF_LEN);
  1118. if (buf == NULL) {
  1119. LM_ERR("out of pkg memory\n");
  1120. return -1;
  1121. }
  1122. memcpy(buf, CRLF, CRLF_LEN);
  1123. memcpy(buf + CRLF_LEN, omip.s, omip.len);
  1124. memcpy(buf + CRLF_LEN + omip.len, oldip->s, oldip->len);
  1125. if (insert_new_lump_after(anchor, buf,
  1126. omip.len + oldip->len + CRLF_LEN, 0) == NULL) {
  1127. LM_ERR("insert_new_lump_after failed\n");
  1128. pkg_free(buf);
  1129. return -1;
  1130. }
  1131. }
  1132. if (oldpf == newpf) {
  1133. nip.len = newip->len;
  1134. nip.s = pkg_malloc(nip.len);
  1135. if (nip.s == NULL) {
  1136. LM_ERR("out of pkg memory\n");
  1137. return -1;
  1138. }
  1139. memcpy(nip.s, newip->s, newip->len);
  1140. } else {
  1141. nip.len = newip->len + 2;
  1142. nip.s = pkg_malloc(nip.len);
  1143. if (nip.s == NULL) {
  1144. LM_ERR("out of pkg memory\n");
  1145. return -1;
  1146. }
  1147. memcpy(nip.s + 2, newip->s, newip->len);
  1148. nip.s[0] = (newpf == AF_INET6) ? '6' : '4';
  1149. nip.s[1] = ' ';
  1150. }
  1151. oip = *oldip;
  1152. if (oldpf != newpf) {
  1153. do {
  1154. oip.s--;
  1155. oip.len++;
  1156. } while (*oip.s != '6' && *oip.s != '4');
  1157. }
  1158. offset = oip.s - msg->buf;
  1159. anchor = del_lump(msg, offset, oip.len, 0);
  1160. if (anchor == NULL) {
  1161. LM_ERR("del_lump failed\n");
  1162. pkg_free(nip.s);
  1163. return -1;
  1164. }
  1165. if (insert_new_lump_after(anchor, nip.s, nip.len, 0) == 0) {
  1166. LM_ERR("insert_new_lump_after failed\n");
  1167. pkg_free(nip.s);
  1168. return -1;
  1169. }
  1170. return 0;
  1171. }
  1172. static int
  1173. alter_mediaport(struct sip_msg *msg, str *body, str *oldport, str *newport,
  1174. int preserve)
  1175. {
  1176. char *buf;
  1177. int offset;
  1178. struct lump* anchor;
  1179. /* check that updating mediaport is really necessary */
  1180. if (newport->len == oldport->len &&
  1181. memcmp(newport->s, oldport->s, newport->len) == 0)
  1182. return 0;
  1183. /*
  1184. * Since rewriting the same info twice will mess SDP up,
  1185. * apply simple anti foot shooting measure - put flag on
  1186. * messages that have been altered and check it when
  1187. * another request comes.
  1188. */
  1189. #if 0
  1190. /* disabled: - it propagates to the reply and we don't want this
  1191. * -- andrei */
  1192. if (msg->msg_flags & FL_SDP_PORT_AFS) {
  1193. LM_ERR("you can't rewrite the same SDP twice, check your config!\n");
  1194. return -1;
  1195. }
  1196. #endif
  1197. if (preserve != 0) {
  1198. anchor = anchor_lump(msg, body->s + body->len - msg->buf, 0, 0);
  1199. if (anchor == NULL) {
  1200. LM_ERR("anchor_lump failed\n");
  1201. return -1;
  1202. }
  1203. buf = pkg_malloc(AOLDMEDPRT_LEN + oldport->len + CRLF_LEN);
  1204. if (buf == NULL) {
  1205. LM_ERR("out of pkg memory\n");
  1206. return -1;
  1207. }
  1208. memcpy(buf, CRLF, CRLF_LEN);
  1209. memcpy(buf + CRLF_LEN, AOLDMEDPRT, AOLDMEDPRT_LEN);
  1210. memcpy(buf + CRLF_LEN + AOLDMEDPRT_LEN, oldport->s, oldport->len);
  1211. if (insert_new_lump_after(anchor, buf,
  1212. AOLDMEDPRT_LEN + oldport->len + CRLF_LEN, 0) == NULL) {
  1213. LM_ERR("insert_new_lump_after failed\n");
  1214. pkg_free(buf);
  1215. return -1;
  1216. }
  1217. }
  1218. buf = pkg_malloc(newport->len);
  1219. if (buf == NULL) {
  1220. LM_ERR("out of pkg memory\n");
  1221. return -1;
  1222. }
  1223. offset = oldport->s - msg->buf;
  1224. anchor = del_lump(msg, offset, oldport->len, 0);
  1225. if (anchor == NULL) {
  1226. LM_ERR("del_lump failed\n");
  1227. pkg_free(buf);
  1228. return -1;
  1229. }
  1230. memcpy(buf, newport->s, newport->len);
  1231. if (insert_new_lump_after(anchor, buf, newport->len, 0) == 0) {
  1232. LM_ERR("insert_new_lump_after failed\n");
  1233. pkg_free(buf);
  1234. return -1;
  1235. }
  1236. #if 0
  1237. msg->msg_flags |= FL_SDP_PORT_AFS;
  1238. #endif
  1239. return 0;
  1240. }
  1241. /*
  1242. * this function is ported from SER
  1243. */
  1244. static int
  1245. alter_rtcp(struct sip_msg *msg, str *body, str *oldport, str *newport)
  1246. {
  1247. char *buf;
  1248. int offset;
  1249. struct lump* anchor;
  1250. /* check that updating rtcpport is really necessary */
  1251. if (newport->len == oldport->len &&
  1252. memcmp(newport->s, oldport->s, newport->len) == 0)
  1253. return 0;
  1254. buf = pkg_malloc(newport->len);
  1255. if (buf == NULL) {
  1256. LM_ERR("alter_rtcp: out of memory\n");
  1257. return -1;
  1258. }
  1259. offset = oldport->s - msg->buf;
  1260. anchor = del_lump(msg, offset, oldport->len, 0);
  1261. if (anchor == NULL) {
  1262. LM_ERR("alter_rtcp: del_lump failed\n");
  1263. pkg_free(buf);
  1264. return -1;
  1265. }
  1266. memcpy(buf, newport->s, newport->len);
  1267. if (insert_new_lump_after(anchor, buf, newport->len, 0) == 0) {
  1268. LM_ERR("alter_rtcp: insert_new_lump_after failed\n");
  1269. pkg_free(buf);
  1270. return -1;
  1271. }
  1272. return 0;
  1273. }
  1274. static char *
  1275. append_filtered_ip(char *at, str *ip)
  1276. {
  1277. int i;
  1278. for (i = 0; i < ip->len; i++) {
  1279. if (isdigit(ip->s[i])) {
  1280. append_chr(at, ip->s[i]);
  1281. }
  1282. }
  1283. return at;
  1284. }
  1285. static int
  1286. insert_candidates(struct sip_msg *msg, char *where, str *ip, unsigned int port,
  1287. int priority)
  1288. {
  1289. char *buf, *at;
  1290. struct lump* anchor;
  1291. str rtp_port;
  1292. str rtcp_port;
  1293. rtcp_port.s = int2str(port+1, &rtcp_port.len); /* beware static buffer */
  1294. buf = pkg_malloc(24 + 78 + 14 + 24 + 2*ip->len + 2 + 2*rtcp_port.len + 24);
  1295. if (buf == NULL) {
  1296. LM_ERR("insert_candidates: out of memory\n");
  1297. return -1;
  1298. }
  1299. at = buf;
  1300. append_str(at, "a=candidate:", 12);
  1301. at = append_filtered_ip(at, ip);
  1302. append_str(at, " 2 UDP ", 7);
  1303. if (priority == 2) {
  1304. append_str(at, "16777214 ", 9);
  1305. } else {
  1306. append_str(at, "2197815294 ", 11);
  1307. }
  1308. append_str(at, ip->s, ip->len);
  1309. append_chr(at, ' ');
  1310. append_str(at, rtcp_port.s, rtcp_port.len);
  1311. append_str(at, " typ relay\r\n", 12);
  1312. rtp_port.s = int2str(port, &rtp_port.len); /* beware static buffer */
  1313. append_str(at, "a=candidate:", 12);
  1314. at = append_filtered_ip(at, ip);
  1315. append_str(at, " 1 UDP ", 7);
  1316. if (priority == 2) {
  1317. append_str(at, "16777215 ", 9);
  1318. } else {
  1319. append_str(at, "2197815295 ", 11);
  1320. }
  1321. append_str(at, ip->s, ip->len);
  1322. append_chr(at, ' ');
  1323. append_str(at, rtp_port.s, rtp_port.len);
  1324. append_str(at, " typ relay\r\n", 12);
  1325. LM_DBG("inserting '%.*s'\n", (int)(at - buf), buf);
  1326. anchor = anchor_lump(msg, where - msg->buf, 0, 0);
  1327. if (anchor == 0) {
  1328. LOG(L_ERR, "insert_candidates: can't get anchor\n");
  1329. pkg_free(buf);
  1330. return -1;
  1331. }
  1332. if (insert_new_lump_before(anchor, buf, at - buf, 0) == 0) {
  1333. LM_ERR("insert_candidates: insert_new_lump_before failed\n");
  1334. pkg_free(buf);
  1335. return -1;
  1336. }
  1337. return 0;
  1338. }
  1339. static char * gencookie(void)
  1340. {
  1341. static char cook[34];
  1342. sprintf(cook, "%d_%u ", (int)mypid, myseqn);
  1343. myseqn++;
  1344. return cook;
  1345. }
  1346. static int
  1347. rtpp_checkcap(struct rtpp_node *node, char *cap, int caplen)
  1348. {
  1349. char *cp;
  1350. struct iovec vf[4] = {{NULL, 0}, {"VF", 2}, {" ", 1}, {NULL, 0}};
  1351. vf[3].iov_base = cap;
  1352. vf[3].iov_len = caplen;
  1353. cp = send_rtpp_command(node, vf, 4);
  1354. if (cp == NULL)
  1355. return -1;
  1356. if (cp[0] == 'E' || atoi(cp) != 1)
  1357. return 0;
  1358. return 1;
  1359. }
  1360. static int
  1361. rtpp_test(struct rtpp_node *node, int isdisabled, int force)
  1362. {
  1363. int rtpp_ver, rval;
  1364. char *cp;
  1365. struct iovec v[2] = {{NULL, 0}, {"V", 1}};
  1366. if(node->rn_recheck_ticks == MI_MAX_RECHECK_TICKS){
  1367. LM_DBG("rtpp %s disabled for ever\n", node->rn_url.s);
  1368. return 1;
  1369. }
  1370. if (force == 0) {
  1371. if (isdisabled == 0)
  1372. return 0;
  1373. if (node->rn_recheck_ticks > get_ticks())
  1374. return 1;
  1375. }
  1376. cp = send_rtpp_command(node, v, 2);
  1377. if (cp == NULL) {
  1378. LM_WARN("can't get version of the RTP proxy\n");
  1379. goto error;
  1380. }
  1381. rtpp_ver = atoi(cp);
  1382. if (rtpp_ver != SUP_CPROTOVER) {
  1383. LM_WARN("unsupported version of RTP proxy <%s> found: %d supported,"
  1384. "%d present\n", node->rn_url.s, SUP_CPROTOVER, rtpp_ver);
  1385. goto error;
  1386. }
  1387. rval = rtpp_checkcap(node, REQ_CPROTOVER, sizeof(REQ_CPROTOVER) - 1);
  1388. if (rval == -1) {
  1389. LM_WARN("RTP proxy went down during version query\n");
  1390. goto error;
  1391. }
  1392. if (rval == 0) {
  1393. LM_WARN("of RTP proxy <%s> doesn't support required protocol version"
  1394. "%s\n", node->rn_url.s, REQ_CPROTOVER);
  1395. goto error;
  1396. }
  1397. LM_INFO("rtp proxy <%s> found, support for it %senabled\n",
  1398. node->rn_url.s, force == 0 ? "re-" : "");
  1399. /* Check for optional capabilities */
  1400. rval = rtpp_checkcap(node, REP_CPROTOVER, sizeof(REP_CPROTOVER) - 1);
  1401. if (rval != -1) {
  1402. node->rn_rep_supported = rval;
  1403. } else {
  1404. node->rn_rep_supported = 0;
  1405. }
  1406. rval = rtpp_checkcap(node, PTL_CPROTOVER, sizeof(PTL_CPROTOVER) - 1);
  1407. if (rval != -1) {
  1408. node->rn_ptl_supported = rval;
  1409. } else {
  1410. node->rn_ptl_supported = 0;
  1411. }
  1412. return 0;
  1413. error:
  1414. LM_WARN("support for RTP proxy <%s> has been disabled%s\n", node->rn_url.s,
  1415. rtpproxy_disable_tout < 0 ? "" : " temporarily");
  1416. if (rtpproxy_disable_tout >= 0)
  1417. node->rn_recheck_ticks = get_ticks() + rtpproxy_disable_tout;
  1418. return 1;
  1419. }
  1420. char *
  1421. send_rtpp_command(struct rtpp_node *node, struct iovec *v, int vcnt)
  1422. {
  1423. struct sockaddr_un addr;
  1424. int fd, len, i;
  1425. char *cp;
  1426. static char buf[256];
  1427. struct pollfd fds[1];
  1428. len = 0;
  1429. cp = buf;
  1430. if (node->rn_umode == 0) {
  1431. memset(&addr, 0, sizeof(addr));
  1432. addr.sun_family = AF_LOCAL;
  1433. strncpy(addr.sun_path, node->rn_address,
  1434. sizeof(addr.sun_path) - 1);
  1435. #ifdef HAVE_SOCKADDR_SA_LEN
  1436. addr.sun_len = strlen(addr.sun_path);
  1437. #endif
  1438. fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  1439. if (fd < 0) {
  1440. LM_ERR("can't create socket\n");
  1441. goto badproxy;
  1442. }
  1443. if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1444. close(fd);
  1445. LM_ERR("can't connect to RTP proxy\n");
  1446. goto badproxy;
  1447. }
  1448. do {
  1449. len = writev(fd, v + 1, vcnt - 1);
  1450. } while (len == -1 && errno == EINTR);
  1451. if (len <= 0) {
  1452. close(fd);
  1453. LM_ERR("can't send command to a RTP proxy\n");
  1454. goto badproxy;
  1455. }
  1456. do {
  1457. len = read(fd, buf, sizeof(buf) - 1);
  1458. } while (len == -1 && errno == EINTR);
  1459. close(fd);
  1460. if (len <= 0) {
  1461. LM_ERR("can't read reply from a RTP proxy\n");
  1462. goto badproxy;
  1463. }
  1464. } else {
  1465. fds[0].fd = rtpp_socks[node->idx];
  1466. fds[0].events = POLLIN;
  1467. fds[0].revents = 0;
  1468. /* Drain input buffer */
  1469. while ((poll(fds, 1, 0) == 1) &&
  1470. ((fds[0].revents & POLLIN) != 0)) {
  1471. recv(rtpp_socks[node->idx], buf, sizeof(buf) - 1, 0);
  1472. fds[0].revents = 0;
  1473. }
  1474. v[0].iov_base = gencookie();
  1475. v[0].iov_len = strlen(v[0].iov_base);
  1476. for (i = 0; i < rtpproxy_retr; i++) {
  1477. do {
  1478. len = writev(rtpp_socks[node->idx], v, vcnt);
  1479. } while (len == -1 && (errno == EINTR || errno == ENOBUFS));
  1480. if (len <= 0) {
  1481. LM_ERR("can't send command to a RTP proxy\n");
  1482. goto badproxy;
  1483. }
  1484. while ((poll(fds, 1, rtpproxy_tout * 1000) == 1) &&
  1485. (fds[0].revents & POLLIN) != 0) {
  1486. do {
  1487. len = recv(rtpp_socks[node->idx], buf, sizeof(buf)-1, 0);
  1488. } while (len == -1 && errno == EINTR);
  1489. if (len <= 0) {
  1490. LM_ERR("can't read reply from a RTP proxy\n");
  1491. goto badproxy;
  1492. }
  1493. if (len >= (v[0].iov_len - 1) &&
  1494. memcmp(buf, v[0].iov_base, (v[0].iov_len - 1)) == 0) {
  1495. len -= (v[0].iov_len - 1);
  1496. cp += (v[0].iov_len - 1);
  1497. if (len != 0) {
  1498. len--;
  1499. cp++;
  1500. }
  1501. goto out;
  1502. }
  1503. fds[0].revents = 0;
  1504. }
  1505. }
  1506. if (i == rtpproxy_retr) {
  1507. LM_ERR("timeout waiting reply from a RTP proxy\n");
  1508. goto badproxy;
  1509. }
  1510. }
  1511. out:
  1512. cp[len] = '\0';
  1513. return cp;
  1514. badproxy:
  1515. LM_ERR("proxy <%s> does not respond, disable it\n", node->rn_url.s);
  1516. node->rn_disabled = 1;
  1517. node->rn_recheck_ticks = get_ticks() + rtpproxy_disable_tout;
  1518. return NULL;
  1519. }
  1520. /*
  1521. * select the set with the id_set id
  1522. */
  1523. static struct rtpp_set * select_rtpp_set(int id_set ){
  1524. struct rtpp_set * rtpp_list;
  1525. /*is it a valid set_id?*/
  1526. if(!rtpp_set_list)
  1527. {
  1528. LM_ERR("rtpproxy set list not initialised\n");
  1529. return NULL;
  1530. }
  1531. for(rtpp_list=rtpp_set_list->rset_first; rtpp_list!=NULL &&
  1532. rtpp_list->id_set!=id_set; rtpp_list=rtpp_list->rset_next);
  1533. return rtpp_list;
  1534. }
  1535. /*
  1536. * Main balancing routine. This does not try to keep the same proxy for
  1537. * the call if some proxies were disabled or enabled; proxy death considered
  1538. * too rare. Otherwise we should implement "mature" HA clustering, which is
  1539. * too expensive here.
  1540. */
  1541. struct rtpp_node *
  1542. select_rtpp_node(str callid, int do_test)
  1543. {
  1544. unsigned sum, sumcut, weight_sum;
  1545. struct rtpp_node* node;
  1546. int was_forced;
  1547. if(!selected_rtpp_set){
  1548. LM_ERR("script error -no valid set selected\n");
  1549. return NULL;
  1550. }
  1551. /* Most popular case: 1 proxy, nothing to calculate */
  1552. if (selected_rtpp_set->rtpp_node_count == 1) {
  1553. node = selected_rtpp_set->rn_first;
  1554. if (node->rn_disabled && node->rn_recheck_ticks <= get_ticks())
  1555. node->rn_disabled = rtpp_test(node, 1, 0);
  1556. return node->rn_disabled ? NULL : node;
  1557. }
  1558. /* XXX Use quick-and-dirty hashing algo */
  1559. for(sum = 0; callid.len > 0; callid.len--)
  1560. sum += callid.s[callid.len - 1];
  1561. sum &= 0xff;
  1562. was_forced = 0;
  1563. retry:
  1564. weight_sum = 0;
  1565. for (node=selected_rtpp_set->rn_first; node!=NULL; node=node->rn_next) {
  1566. if (node->rn_disabled && node->rn_recheck_ticks <= get_ticks()){
  1567. /* Try to enable if it's time to try. */
  1568. node->rn_disabled = rtpp_test(node, 1, 0);
  1569. }
  1570. if (!node->rn_disabled)
  1571. weight_sum += node->rn_weight;
  1572. }
  1573. if (weight_sum == 0) {
  1574. /* No proxies? Force all to be redetected, if not yet */
  1575. if (was_forced)
  1576. return NULL;
  1577. was_forced = 1;
  1578. for(node=selected_rtpp_set->rn_first; node!=NULL; node=node->rn_next) {
  1579. node->rn_disabled = rtpp_test(node, 1, 1);
  1580. }
  1581. goto retry;
  1582. }
  1583. sumcut = sum % weight_sum;
  1584. /*
  1585. * sumcut here lays from 0 to weight_sum-1.
  1586. * Scan proxy list and decrease until appropriate proxy is found.
  1587. */
  1588. for (node=selected_rtpp_set->rn_first; node!=NULL; node=node->rn_next) {
  1589. if (node->rn_disabled)
  1590. continue;
  1591. if (sumcut < node->rn_weight)
  1592. goto found;
  1593. sumcut -= node->rn_weight;
  1594. }
  1595. /* No node list */
  1596. return NULL;
  1597. found:
  1598. if (do_test) {
  1599. node->rn_disabled = rtpp_test(node, node->rn_disabled, 0);
  1600. if (node->rn_disabled)
  1601. goto retry;
  1602. }
  1603. return node;
  1604. }
  1605. static int
  1606. get_extra_id(struct sip_msg* msg, str *id_str) {
  1607. if(msg==NULL || extra_id_pv==NULL || id_str==NULL) {
  1608. LM_ERR("bad parameters\n");
  1609. return 0;
  1610. }
  1611. if (pv_printf_s(msg, extra_id_pv, id_str)<0) {
  1612. LM_ERR("cannot print the additional id\n");
  1613. return 0;
  1614. }
  1615. return 1;
  1616. }
  1617. static int
  1618. unforce_rtp_proxy1_f(struct sip_msg* msg, char* str1, char* str2)
  1619. {
  1620. str flags;
  1621. if (str1)
  1622. get_str_fparam(&flags, msg, (fparam_t *) str1);
  1623. else
  1624. flags.s = NULL;
  1625. return unforce_rtp_proxy(msg, flags.s);
  1626. }
  1627. static int
  1628. unforce_rtp_proxy(struct sip_msg* msg, char* flags)
  1629. {
  1630. str callid, from_tag, to_tag, viabranch;
  1631. char *cp;
  1632. int via = 0;
  1633. int to = 1;
  1634. int extra = 0;
  1635. str extra_id;
  1636. int ret;
  1637. struct rtpp_node *node;
  1638. struct iovec v[1 + 4 + 3 + 2] = {{NULL, 0}, {"D", 1}, {" ", 1}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {" ", 1}, {NULL, 0}, {" ", 1}, {NULL, 0}};
  1639. /* 1 */ /* 2 */ /* 3 */ /* 4 */ /* 5 */ /* 6 */ /* 7 */ /* 8 */ /* 9 */
  1640. for (cp = flags; cp && *cp; cp++) {
  1641. switch (*cp) {
  1642. case '1':
  1643. via = 1;
  1644. break;
  1645. case '2':
  1646. via = 2;
  1647. break;
  1648. case '3':
  1649. if(msg && msg->first_line.type == SIP_REPLY)
  1650. via = 2;
  1651. else
  1652. via = 1;
  1653. break;
  1654. case 't':
  1655. case 'T':
  1656. to = 0;
  1657. break;
  1658. case 'b':
  1659. extra = 1;
  1660. break;
  1661. case 'a':
  1662. case 'A':
  1663. case 'i':
  1664. case 'I':
  1665. case 'e':
  1666. case 'E':
  1667. case 'l':
  1668. case 'L':
  1669. case 'f':
  1670. case 'F':
  1671. case 'r':
  1672. case 'R':
  1673. case 'c':
  1674. case 'C':
  1675. case 'o':
  1676. case 'O':
  1677. case 'x':
  1678. case 'X':
  1679. case 'w':
  1680. case 'W':
  1681. case 'z':
  1682. case 'Z':
  1683. /* ignore them - they can be sent by rtpproxy_manage() */
  1684. break;
  1685. default:
  1686. LM_ERR("unknown option `%c'\n", *cp);
  1687. return -1;
  1688. }
  1689. }
  1690. if (get_callid(msg, &callid) == -1 || callid.len == 0) {
  1691. LM_ERR("can't get Call-Id field\n");
  1692. return -1;
  1693. }
  1694. to_tag.s = 0;
  1695. to_tag.len = 0;
  1696. if ((to == 1) && get_to_tag(msg, &to_tag) == -1) {
  1697. LM_ERR("can't get To tag\n");
  1698. return -1;
  1699. }
  1700. if (get_from_tag(msg, &from_tag) == -1 || from_tag.len == 0) {
  1701. LM_ERR("can't get From tag\n");
  1702. return -1;
  1703. }
  1704. if (via) {
  1705. if (via == 1)
  1706. ret = get_via_branch(msg, 1, &viabranch);
  1707. else /* (via == 2) */
  1708. ret = get_via_branch(msg, 2, &viabranch);
  1709. if (ret == -1 || viabranch.len == 0) {
  1710. LM_ERR("can't get Via branch\n");
  1711. return -1;
  1712. }
  1713. v[4].iov_base = ";";
  1714. v[4].iov_len = 1;
  1715. STR2IOVEC(viabranch, v[5]);
  1716. } else
  1717. /* Append extra id to call-id */
  1718. if (extra && extra_id_pv && get_extra_id(msg, &extra_id)) {
  1719. v[4].iov_base = ";";
  1720. v[4].iov_len = 1;
  1721. STR2IOVEC(extra_id, v[5]);
  1722. }
  1723. STR2IOVEC(callid, v[3]);
  1724. STR2IOVEC(from_tag, v[7]);
  1725. STR2IOVEC(to_tag, v[9]);
  1726. if(msg->id != current_msg_id){
  1727. selected_rtpp_set = default_rtpp_set;
  1728. }
  1729. node = select_rtpp_node(callid, 1);
  1730. if (!node) {
  1731. LM_ERR("no available proxies\n");
  1732. return -1;
  1733. }
  1734. set_rtp_inst_pvar(msg, &node->rn_url);
  1735. send_rtpp_command(node, v, (to_tag.len > 0) ? 10 : 8);
  1736. return 1;
  1737. }
  1738. /* This function assumes p points to a line of requested type. */
  1739. static int
  1740. set_rtp_proxy_set_f(struct sip_msg * msg, char * str1, char * str2)
  1741. {
  1742. rtpp_set_link_t *rtpl;
  1743. pv_value_t val;
  1744. rtpl = (rtpp_set_link_t*)str1;
  1745. current_msg_id = 0;
  1746. selected_rtpp_set = 0;
  1747. if(rtpl->rset != NULL) {
  1748. current_msg_id = msg->id;
  1749. selected_rtpp_set = rtpl->rset;
  1750. } else {
  1751. if(pv_get_spec_value(msg, rtpl->rpv, &val)<0) {
  1752. LM_ERR("cannot evaluate pv param\n");
  1753. return -1;
  1754. }
  1755. if(!(val.flags & PV_VAL_INT)) {
  1756. LM_ERR("pv param must hold an integer value\n");
  1757. return -1;
  1758. }
  1759. selected_rtpp_set = select_rtpp_set(val.ri);
  1760. if(selected_rtpp_set==NULL) {
  1761. LM_ERR("could not locate rtpproxy set %d\n", val.ri);
  1762. return -1;
  1763. }
  1764. current_msg_id = msg->id;
  1765. }
  1766. return 1;
  1767. }
  1768. static int
  1769. rtpproxy_manage(struct sip_msg *msg, char *flags, char *ip)
  1770. {
  1771. char *cp = NULL;
  1772. char newip[IP_ADDR_MAX_STR_SIZE];
  1773. int method;
  1774. int nosdp;
  1775. if(msg->cseq==NULL && ((parse_headers(msg, HDR_CSEQ_F, 0)==-1)
  1776. || (msg->cseq==NULL)))
  1777. {
  1778. LM_ERR("no CSEQ header\n");
  1779. return -1;
  1780. }
  1781. method = get_cseq(msg)->method_id;
  1782. if(!(method==METHOD_INVITE || method==METHOD_ACK || method==METHOD_CANCEL
  1783. || method==METHOD_BYE || method==METHOD_UPDATE || method==METHOD_PRACK))
  1784. return -1;
  1785. if(method==METHOD_CANCEL || method==METHOD_BYE)
  1786. return unforce_rtp_proxy(msg, flags);
  1787. if(ip==NULL)
  1788. {
  1789. cp = ip_addr2a(&msg->rcv.dst_ip);
  1790. strcpy(newip, cp);
  1791. }
  1792. if(msg->msg_flags & FL_SDP_BODY)
  1793. nosdp = 0;
  1794. else
  1795. nosdp = parse_sdp(msg);
  1796. if(msg->first_line.type == SIP_REQUEST) {
  1797. if(method==METHOD_ACK && nosdp==0)
  1798. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 0,
  1799. (ip!=NULL)?1:0);
  1800. if(method==METHOD_PRACK && nosdp==0)
  1801. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 1,
  1802. (ip!=NULL)?1:0);
  1803. if(method==METHOD_UPDATE && nosdp==0)
  1804. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 1,
  1805. (ip!=NULL)?1:0);
  1806. if(method==METHOD_INVITE && nosdp==0) {
  1807. msg->msg_flags |= FL_SDP_BODY;
  1808. if(tmb.t_gett!=NULL && tmb.t_gett()!=NULL
  1809. && tmb.t_gett()!=T_UNDEFINED)
  1810. tmb.t_gett()->uas.request->msg_flags |= FL_SDP_BODY;
  1811. if(route_type==FAILURE_ROUTE)
  1812. return unforce_rtp_proxy(msg, flags);
  1813. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 1,
  1814. (ip!=NULL)?1:0);
  1815. }
  1816. } else if(msg->first_line.type == SIP_REPLY) {
  1817. if(msg->first_line.u.reply.statuscode>=300)
  1818. return unforce_rtp_proxy(msg, flags);
  1819. if(nosdp==0) {
  1820. if(method==METHOD_PRACK)
  1821. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 0,
  1822. (ip!=NULL)?1:0);
  1823. if(method==METHOD_UPDATE)
  1824. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 0,
  1825. (ip!=NULL)?1:0);
  1826. if(tmb.t_gett==NULL || tmb.t_gett()==NULL
  1827. || tmb.t_gett()==T_UNDEFINED)
  1828. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 0,
  1829. (ip!=NULL)?1:0);
  1830. if(tmb.t_gett()->uas.request->msg_flags & FL_SDP_BODY)
  1831. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 0,
  1832. (ip!=NULL)?1:0);
  1833. return force_rtp_proxy(msg, flags, (cp!=NULL)?newip:ip, 1,
  1834. (ip!=NULL)?1:0);
  1835. }
  1836. }
  1837. return -1;
  1838. }
  1839. static int
  1840. rtpproxy_manage0(struct sip_msg *msg, char *flags, char *ip)
  1841. {
  1842. return rtpproxy_manage(msg, 0, 0);
  1843. }
  1844. static int
  1845. rtpproxy_manage1(struct sip_msg *msg, char *flags, char *ip)
  1846. {
  1847. str flag_str;
  1848. if(fixup_get_svalue(msg, (gparam_p)flags, &flag_str)<0)
  1849. {
  1850. LM_ERR("invalid flags parameter\n");
  1851. return -1;
  1852. }
  1853. return rtpproxy_manage(msg, flag_str.s, 0);
  1854. }
  1855. static int
  1856. rtpproxy_manage2(struct sip_msg *msg, char *flags, char *ip)
  1857. {
  1858. str flag_str;
  1859. str ip_str;
  1860. if(fixup_get_svalue(msg, (gparam_p)flags, &flag_str)<0)
  1861. {
  1862. LM_ERR("invalid flags parameter\n");
  1863. return -1;
  1864. }
  1865. if(fixup_get_svalue(msg, (gparam_p)ip, &ip_str)<0)
  1866. {
  1867. LM_ERR("invalid IP parameter\n");
  1868. return -1;
  1869. }
  1870. return rtpproxy_manage(msg, flag_str.s, ip_str.s);
  1871. }
  1872. static int
  1873. rtpproxy_offer1_f(struct sip_msg *msg, char *str1, char *str2)
  1874. {
  1875. char *cp;
  1876. char newip[IP_ADDR_MAX_STR_SIZE];
  1877. str flags;
  1878. cp = ip_addr2a(&msg->rcv.dst_ip);
  1879. strcpy(newip, cp);
  1880. if (str1)
  1881. get_str_fparam(&flags, msg, (fparam_t *) str1);
  1882. else
  1883. flags.s = NULL;
  1884. return force_rtp_proxy(msg, flags.s, newip, 1, 0);
  1885. }
  1886. static int
  1887. rtpproxy_offer2_f(struct sip_msg *msg, char *param1, char *param2)
  1888. {
  1889. str flags, new_ip;
  1890. get_str_fparam(&flags, msg, (fparam_t *) param1);
  1891. get_str_fparam(&new_ip, msg, (fparam_t *) param2);
  1892. return force_rtp_proxy(msg, flags.s, new_ip.s, 1, 1);
  1893. }
  1894. static int
  1895. rtpproxy_answer1_f(struct sip_msg *msg, char *str1, char *str2)
  1896. {
  1897. char *cp;
  1898. char newip[IP_ADDR_MAX_STR_SIZE];
  1899. str flags;
  1900. if (msg->first_line.type == SIP_REQUEST)
  1901. if (msg->first_line.u.request.method_value != METHOD_ACK)
  1902. return -1;
  1903. cp = ip_addr2a(&msg->rcv.dst_ip);
  1904. strcpy(newip, cp);
  1905. if (str1)
  1906. get_str_fparam(&flags, msg, (fparam_t *) str1);
  1907. else
  1908. flags.s = NULL;
  1909. return force_rtp_proxy(msg, flags.s, newip, 0, 0);
  1910. }
  1911. static int
  1912. rtpproxy_answer2_f(struct sip_msg *msg, char *param1, char *param2)
  1913. {
  1914. str flags, new_ip;
  1915. if (msg->first_line.type == SIP_REQUEST)
  1916. if (msg->first_line.u.request.method_value != METHOD_ACK)
  1917. return -1;
  1918. get_str_fparam(&flags, msg, (fparam_t *) param1);
  1919. get_str_fparam(&new_ip, msg, (fparam_t *) param2);
  1920. return force_rtp_proxy(msg, flags.s, new_ip.s, 0, 1);
  1921. }
  1922. struct options {
  1923. str s;
  1924. int oidx;
  1925. };
  1926. static int
  1927. append_opts(struct options *op, char ch)
  1928. {
  1929. void *p;
  1930. if (op->s.len <= op->oidx) {
  1931. p = pkg_realloc(op->s.s, op->oidx + 32);
  1932. if (p == NULL) {
  1933. return (-1);
  1934. }
  1935. op->s.s = p;
  1936. op->s.len = op->oidx + 32;
  1937. }
  1938. op->s.s[op->oidx++] = ch;
  1939. return (0);
  1940. }
  1941. static void
  1942. free_opts(struct options *op1, struct options *op2, struct options *op3)
  1943. {
  1944. if (op1->s.len > 0 && op1->s.s != NULL) {
  1945. pkg_free(op1->s.s);
  1946. op1->s.len = 0;
  1947. }
  1948. if (op2->s.len > 0 && op2->s.s != NULL) {
  1949. pkg_free(op2->s.s);
  1950. op2->s.len = 0;
  1951. }
  1952. if (op3->s.len > 0 && op3->s.s != NULL) {
  1953. pkg_free(op3->s.s);
  1954. op3->s.len = 0;
  1955. }
  1956. }
  1957. #define FORCE_RTP_PROXY_RET(e) \
  1958. do { \
  1959. free_opts(&opts, &rep_opts, &pt_opts); \
  1960. return (e); \
  1961. } while (0);
  1962. static int
  1963. force_rtp_proxy(struct sip_msg* msg, char* str1, char* str2, int offer, int forcedIP)
  1964. {
  1965. str body, body1, oldport, oldip, newport, newip;
  1966. str callid, from_tag, to_tag, tmp, payload_types;
  1967. str newrtcp = {0, 0};
  1968. str viabranch;
  1969. int create, port, len, flookup, argc, proxied, real, via, ret;
  1970. int orgip, commip;
  1971. int pf, pf1, force;
  1972. struct options opts, rep_opts, pt_opts;
  1973. char *cp, *cp1;
  1974. char *cpend, *next;
  1975. char **ap, *argv[10];
  1976. struct lump* anchor;
  1977. struct rtpp_node *node;
  1978. struct iovec v[] = {
  1979. {NULL, 0}, /* reserved (cookie) */
  1980. {NULL, 0}, /* command & common options */
  1981. {NULL, 0}, /* per-media/per-node options 1 */
  1982. {NULL, 0}, /* per-media/per-node options 2 */
  1983. {" ", 1}, /* separator */
  1984. {NULL, 0}, /* callid */
  1985. {NULL, 0}, /* via-branch separator ";" */
  1986. {NULL, 0}, /* via-branch */
  1987. {" ", 1}, /* separator */
  1988. {NULL, 7}, /* newip */
  1989. {" ", 1}, /* separator */
  1990. {NULL, 1}, /* oldport */
  1991. {" ", 1}, /* separator */
  1992. {NULL, 0}, /* from_tag */
  1993. {";", 1}, /* separator */
  1994. {NULL, 0}, /* medianum */
  1995. {" ", 1}, /* separator */
  1996. {NULL, 0}, /* to_tag */
  1997. {";", 1}, /* separator */
  1998. {NULL, 0}, /* medianum */
  1999. {" ", 1}, /* separator */
  2000. {NULL, 0}, /* Timeout-Socket */
  2001. };
  2002. int iovec_param_count;
  2003. int autobridge_ipv4v6;
  2004. int extra;
  2005. str extra_id;
  2006. char *c1p, *c2p, *bodylimit, *o1p;
  2007. char itoabuf_buf[20];
  2008. int medianum, media_multi;
  2009. str itoabuf_str;
  2010. int c1p_altered;
  2011. int sdp_session_num, sdp_stream_num;
  2012. sdp_session_cell_t* sdp_session;
  2013. sdp_stream_cell_t* sdp_stream;
  2014. int_str ice_candidate_priority_val;
  2015. memset(&opts, '\0', sizeof(opts));
  2016. memset(&rep_opts, '\0', sizeof(rep_opts));
  2017. memset(&pt_opts, '\0', sizeof(pt_opts));
  2018. /* Leave space for U/L prefix TBD later */
  2019. if (append_opts(&opts, '?') == -1) {
  2020. LM_ERR("out of pkg memory\n");
  2021. FORCE_RTP_PROXY_RET (-1);
  2022. }
  2023. flookup = force = real = orgip = commip = via = autobridge_ipv4v6 = extra = 0;
  2024. for (cp = str1; cp != NULL && *cp != '\0'; cp++) {
  2025. switch (*cp) {
  2026. case '1':
  2027. via = 1;
  2028. break;
  2029. case '2':
  2030. via = 2;
  2031. break;
  2032. case '3':
  2033. if(msg && msg->first_line.type == SIP_REPLY)
  2034. via = 2;
  2035. else
  2036. via = 1;
  2037. break;
  2038. case 'a':
  2039. case 'A':
  2040. if (append_opts(&opts, 'A') == -1) {
  2041. LM_ERR("out of pkg memory\n");
  2042. FORCE_RTP_PROXY_RET (-1);
  2043. }
  2044. real = 1;
  2045. break;
  2046. case 'b':
  2047. extra = 1;
  2048. break;
  2049. case 'i':
  2050. case 'I':
  2051. if (append_opts(&opts, 'I') == -1) {
  2052. LM_ERR("out of pkg memory\n");
  2053. FORCE_RTP_PROXY_RET (-1);
  2054. }
  2055. break;
  2056. case 'e':
  2057. case 'E':
  2058. if (append_opts(&opts, 'E') == -1) {
  2059. LM_ERR("out of pkg memory\n");
  2060. FORCE_RTP_PROXY_RET (-1);
  2061. }
  2062. break;
  2063. case 'l':
  2064. case 'L':
  2065. if (offer == 0) {
  2066. FORCE_RTP_PROXY_RET (-1);
  2067. }
  2068. flookup = 1;
  2069. break;
  2070. case 'f':
  2071. case 'F':
  2072. force = 1;
  2073. break;
  2074. case 'r':
  2075. case 'R':
  2076. real = 1;
  2077. break;
  2078. case 'c':
  2079. case 'C':
  2080. commip = 1;
  2081. break;
  2082. case 'o':
  2083. case 'O':
  2084. orgip = 1;
  2085. break;
  2086. case 'x':
  2087. case 'X':
  2088. autobridge_ipv4v6 = 1;
  2089. break;
  2090. case 'w':
  2091. case 'W':
  2092. if (append_opts(&opts, 'S') == -1) {
  2093. LM_ERR("out of pkg memory\n");
  2094. FORCE_RTP_PROXY_RET (-1);
  2095. }
  2096. break;
  2097. case 'z':
  2098. case 'Z':
  2099. if (append_opts(&rep_opts, 'Z') == -1) {
  2100. LM_ERR("out of pkg memory\n");
  2101. FORCE_RTP_PROXY_RET (-1);
  2102. }
  2103. /* If there are any digits following Z copy them into the command */
  2104. for (; cp[1] != '\0' && isdigit(cp[1]); cp++) {
  2105. if (append_opts(&rep_opts, cp[1]) == -1) {
  2106. LM_ERR("out of pkg memory\n");
  2107. FORCE_RTP_PROXY_RET (-1);
  2108. }
  2109. }
  2110. break;
  2111. case 't':
  2112. case 'T':
  2113. /* Only used in rtpproxy_destroy */
  2114. break;
  2115. default:
  2116. LM_ERR("unknown option `%c'\n", *cp);
  2117. FORCE_RTP_PROXY_RET (-1);
  2118. }
  2119. }
  2120. if (offer != 0) {
  2121. create = 1;
  2122. } else {
  2123. create = 0;
  2124. }
  2125. /* extract_body will also parse all the headers in the message as
  2126. * a side effect => don't move get_callid/get_to_tag in front of it
  2127. * -- andrei */
  2128. if (extract_body(msg, &body) == -1) {
  2129. LM_ERR("can't extract body from the message\n");
  2130. FORCE_RTP_PROXY_RET (-1);
  2131. }
  2132. if (get_callid(msg, &callid) == -1 || callid.len == 0) {
  2133. LM_ERR("can't get Call-Id field\n");
  2134. FORCE_RTP_PROXY_RET (-1);
  2135. }
  2136. to_tag.s = 0;
  2137. if (get_to_tag(msg, &to_tag) == -1) {
  2138. LM_ERR("can't get To tag\n");
  2139. FORCE_RTP_PROXY_RET (-1);
  2140. }
  2141. if (get_from_tag(msg, &from_tag) == -1 || from_tag.len == 0) {
  2142. LM_ERR("can't get From tag\n");
  2143. FORCE_RTP_PROXY_RET (-1);
  2144. }
  2145. if (via) {
  2146. if (via == 1)
  2147. ret = get_via_branch(msg, 1, &viabranch);
  2148. else /* (via == 2) */
  2149. ret = get_via_branch(msg, 2, &viabranch);
  2150. if (ret == -1 || viabranch.len == 0) {
  2151. LM_ERR("can't get Via branch\n");
  2152. FORCE_RTP_PROXY_RET (-1);
  2153. }
  2154. v[6].iov_base = ";";
  2155. v[6].iov_len = 1;
  2156. STR2IOVEC(viabranch, v[7]);
  2157. } else
  2158. /* Append extra id to call-id */
  2159. if (extra && extra_id_pv && get_extra_id(msg, &extra_id)) {
  2160. v[6].iov_base = ";";
  2161. v[6].iov_len = 1;
  2162. STR2IOVEC(extra_id, v[7]);
  2163. }
  2164. if (flookup != 0) {
  2165. if (to_tag.len == 0) {
  2166. FORCE_RTP_PROXY_RET (-1);
  2167. }
  2168. if (msg->first_line.type == SIP_REQUEST) {
  2169. tmp = from_tag;
  2170. from_tag = to_tag;
  2171. to_tag = tmp;
  2172. }
  2173. create = 0;
  2174. } else if ((msg->first_line.type == SIP_REPLY && offer != 0)
  2175. || (msg->first_line.type == SIP_REQUEST && offer == 0)) {
  2176. if (to_tag.len == 0) {
  2177. FORCE_RTP_PROXY_RET (-1);
  2178. }
  2179. tmp = from_tag;
  2180. from_tag = to_tag;
  2181. to_tag = tmp;
  2182. }
  2183. proxied = 0;
  2184. if (nortpproxy_str.len) {
  2185. for ( cp=body.s ; (len=body.s+body.len-cp) >= nortpproxy_str.len ; ) {
  2186. cp1 = ser_memmem(cp, nortpproxy_str.s, len, nortpproxy_str.len);
  2187. if (cp1 == NULL)
  2188. break;
  2189. if (cp1[-1] == '\n' || cp1[-1] == '\r') {
  2190. proxied = 1;
  2191. break;
  2192. }
  2193. cp = cp1 + nortpproxy_str.len;
  2194. }
  2195. }
  2196. if (proxied != 0 && force == 0) {
  2197. FORCE_RTP_PROXY_RET (-2);
  2198. }
  2199. /*
  2200. * Parsing of SDP body.
  2201. * It can contain a few session descriptions (each starts with
  2202. * v-line), and each session may contain a few media descriptions
  2203. * (each starts with m-line).
  2204. * We have to change ports in m-lines, and also change IP addresses in
  2205. * c-lines which can be placed either in session header (fallback for
  2206. * all medias) or media description.
  2207. * Ports should be allocated for any media. IPs all should be changed
  2208. * to the same value (RTP proxy IP), so we can change all c-lines
  2209. * unconditionally.
  2210. */
  2211. if(0 != parse_sdp(msg)) {
  2212. LM_ERR("Unable to parse sdp\n");
  2213. FORCE_RTP_PROXY_RET (-1);
  2214. }
  2215. #ifdef EXTRA_DEBUG
  2216. print_sdp((sdp_info_t*)msg->body, L_DBG);
  2217. #endif
  2218. bodylimit = body.s + body.len;
  2219. if(msg->id != current_msg_id){
  2220. selected_rtpp_set = default_rtpp_set;
  2221. }
  2222. opts.s.s[0] = (create == 0) ? 'L' : 'U';
  2223. v[1].iov_base = opts.s.s;
  2224. v[1].iov_len = opts.oidx;
  2225. STR2IOVEC(callid, v[5]);
  2226. STR2IOVEC(from_tag, v[13]);
  2227. STR2IOVEC(to_tag, v[17]);
  2228. if (ice_candidate_priority_avp_param) {
  2229. if (search_first_avp(ice_candidate_priority_avp_type,
  2230. ice_candidate_priority_avp,
  2231. &ice_candidate_priority_val, 0)
  2232. == NULL) {
  2233. ice_candidate_priority_val.n = 2;
  2234. } else if ((ice_candidate_priority_val.n < 1) ||
  2235. (ice_candidate_priority_val.n > 2)) {
  2236. LM_ERR("invalid ice candidate priority value %d\n",
  2237. ice_candidate_priority_val.n);
  2238. FORCE_RTP_PROXY_RET (-1);
  2239. }
  2240. } else {
  2241. ice_candidate_priority_val.n = 0;
  2242. }
  2243. /* check if this is a single or a multi stream SDP offer/answer */
  2244. sdp_stream_num = get_sdp_stream_num(msg);
  2245. switch (sdp_stream_num) {
  2246. case 0:
  2247. LM_ERR("sdp w/o streams\n");
  2248. FORCE_RTP_PROXY_RET (-1);
  2249. break;
  2250. case 1:
  2251. media_multi = 0;
  2252. break;
  2253. default:
  2254. media_multi = 1;
  2255. }
  2256. #ifdef EXTRA_DEBUG
  2257. LM_DBG("my new media_multi=%d\n", media_multi);
  2258. #endif
  2259. medianum = 0;
  2260. sdp_session_num = 0;
  2261. for(;;) {
  2262. sdp_session = get_sdp_session(msg, sdp_session_num);
  2263. if(!sdp_session) break;
  2264. sdp_stream_num = 0;
  2265. c1p_altered = 0;
  2266. o1p = sdp_session->o_ip_addr.s;
  2267. for(;;) {
  2268. sdp_stream = get_sdp_stream(msg, sdp_session_num, sdp_stream_num);
  2269. if (!sdp_stream ||
  2270. (ice_candidate_priority_val.n && sdp_stream->remote_candidates.len)) break;
  2271. if (sdp_stream->ip_addr.s && sdp_stream->ip_addr.len>0) {
  2272. oldip = sdp_stream->ip_addr;
  2273. pf = sdp_stream->pf;
  2274. } else {
  2275. oldip = sdp_session->ip_addr;
  2276. pf = sdp_session->pf;
  2277. }
  2278. oldport = sdp_stream->port;
  2279. payload_types = sdp_stream->payloads;
  2280. medianum++;
  2281. if (real != 0) {
  2282. newip = oldip;
  2283. } else {
  2284. newip.s = ip_addr2a(&msg->rcv.src_ip);
  2285. newip.len = strlen(newip.s);
  2286. }
  2287. /* XXX must compare address families in all addresses */
  2288. if (pf == AF_INET6) {
  2289. if (autobridge_ipv4v6 != 0) {
  2290. if ((append_opts(&opts, 'E') == -1) || (append_opts(&opts, 'I') == -1)) {
  2291. LM_ERR("out of pkg memory\n");
  2292. FORCE_RTP_PROXY_RET (-1);
  2293. }
  2294. /* Only execute once */
  2295. autobridge_ipv4v6 = 0;
  2296. }
  2297. if (append_opts(&opts, '6') == -1) {
  2298. LM_ERR("out of pkg memory\n");
  2299. FORCE_RTP_PROXY_RET (-1);
  2300. }
  2301. /* We need to update the pointers and the length here, it has changed. */
  2302. v[1].iov_base = opts.s.s;
  2303. v[1].iov_len = opts.oidx;
  2304. } else {
  2305. if (autobridge_ipv4v6 != 0) {
  2306. if ((append_opts(&opts, 'I') == -1) || (append_opts(&opts, 'E') == -1)) {
  2307. LM_ERR("out of pkg memory\n");
  2308. FORCE_RTP_PROXY_RET (-1);
  2309. }
  2310. /* We need to update the pointers and the length here, it has changed. */
  2311. v[1].iov_base = opts.s.s;
  2312. v[1].iov_len = opts.oidx;
  2313. /* Only execute once */
  2314. autobridge_ipv4v6 = 0;
  2315. }
  2316. }
  2317. STR2IOVEC(newip, v[9]);
  2318. STR2IOVEC(oldport, v[11]);
  2319. #ifdef EXTRA_DEBUG
  2320. LM_DBG("STR2IOVEC(newip[%.*s], v[9])", newip.len, newip.s);
  2321. LM_DBG("STR2IOVEC(oldport[%.*s], v[11])", oldport.len, oldport.s);
  2322. #endif
  2323. if (1 || media_multi) /* XXX netch: can't choose now*/
  2324. {
  2325. snprintf(itoabuf_buf, sizeof itoabuf_buf, "%d", medianum);
  2326. itoabuf_str.s = itoabuf_buf;
  2327. itoabuf_str.len = strlen(itoabuf_buf);
  2328. STR2IOVEC(itoabuf_str, v[15]);
  2329. STR2IOVEC(itoabuf_str, v[19]);
  2330. #ifdef EXTRA_DEBUG
  2331. LM_DBG("STR2IOVEC(itoabuf_str, v[15])\n");
  2332. LM_DBG("STR2IOVEC(itoabuf_str, v[19])\n");
  2333. #endif
  2334. } else {
  2335. v[14].iov_len = v[15].iov_len = 0;
  2336. v[18].iov_len = v[19].iov_len = 0;
  2337. }
  2338. do {
  2339. node = select_rtpp_node(callid, 1);
  2340. if (!node) {
  2341. LM_ERR("no available proxies\n");
  2342. FORCE_RTP_PROXY_RET (-3);
  2343. }
  2344. set_rtp_inst_pvar(msg, &node->rn_url);
  2345. if (rep_opts.oidx > 0) {
  2346. if (node->rn_rep_supported == 0) {
  2347. LM_WARN("re-packetization is requested but is not "
  2348. "supported by the selected RTP proxy node\n");
  2349. v[2].iov_len = 0;
  2350. } else {
  2351. v[2].iov_base = rep_opts.s.s;
  2352. v[2].iov_len += rep_opts.oidx;
  2353. }
  2354. }
  2355. #ifdef EXTRA_DEBUG
  2356. LM_DBG("payload_types='%.*s'\n", payload_types.len, payload_types.s);
  2357. #endif
  2358. if (sdp_stream->is_rtp && payload_types.len > 0 && node->rn_ptl_supported != 0) {
  2359. pt_opts.oidx = 0;
  2360. if (append_opts(&pt_opts, 'c') == -1) {
  2361. LM_ERR("out of pkg memory\n");
  2362. FORCE_RTP_PROXY_RET (-1);
  2363. }
  2364. /*
  2365. * Convert space-separated payload types list into
  2366. * a comma-separated list.
  2367. */
  2368. for (cp = payload_types.s;
  2369. cp < payload_types.s + payload_types.len; cp++) {
  2370. if (isdigit(*cp)) {
  2371. if (append_opts(&pt_opts, *cp) == -1) {
  2372. LM_ERR("out of pkg memory\n");
  2373. FORCE_RTP_PROXY_RET (-1);
  2374. }
  2375. continue;
  2376. }
  2377. do {
  2378. cp++;
  2379. } while (!isdigit(*cp) &&
  2380. cp < payload_types.s + payload_types.len);
  2381. /* Check EOL */
  2382. if (cp >= payload_types.s + payload_types.len)
  2383. break;
  2384. if (append_opts(&pt_opts, ',') == -1) {
  2385. LM_ERR("out of pkg memory\n");
  2386. FORCE_RTP_PROXY_RET (-1);
  2387. }
  2388. cp--;
  2389. }
  2390. v[3].iov_base = pt_opts.s.s;
  2391. v[3].iov_len = pt_opts.oidx;
  2392. } else {
  2393. v[3].iov_len = 0;
  2394. }
  2395. if (to_tag.len > 0) {
  2396. iovec_param_count = 20;
  2397. if (opts.s.s[0] == 'U' && timeout_socket_str.len > 0) {
  2398. iovec_param_count = 22;
  2399. STR2IOVEC(timeout_socket_str, v[21]);
  2400. }
  2401. } else {
  2402. iovec_param_count = 16;
  2403. }
  2404. cp = send_rtpp_command(node, v, iovec_param_count);
  2405. } while (cp == NULL);
  2406. LM_DBG("proxy reply: %s\n", cp);
  2407. /* Parse proxy reply to <argc,argv> */
  2408. argc = 0;
  2409. memset(argv, 0, sizeof(argv));
  2410. cpend=cp+strlen(cp);
  2411. next=eat_token_end(cp, cpend);
  2412. for (ap=argv; cp<cpend; cp=next+1, next=eat_token_end(cp, cpend)){
  2413. *next=0;
  2414. if (*cp != '\0') {
  2415. *ap=cp;
  2416. argc++;
  2417. if ((char*)++ap >= ((char*)argv+sizeof(argv)))
  2418. break;
  2419. }
  2420. }
  2421. if (argc < 1) {
  2422. LM_ERR("no reply from rtp proxy\n");
  2423. FORCE_RTP_PROXY_RET (-1);
  2424. }
  2425. port = atoi(argv[0]);
  2426. if (port <= 0 || port > 65535) {
  2427. if (port != 0 || flookup == 0)
  2428. LM_ERR("incorrect port %i in reply "
  2429. "from rtp proxy\n",port);
  2430. FORCE_RTP_PROXY_RET (-1);
  2431. }
  2432. pf1 = (argc >= 3 && argv[2][0] == '6') ? AF_INET6 : AF_INET;
  2433. if (isnulladdr(&oldip, pf)) {
  2434. if (pf1 == AF_INET6) {
  2435. newip.s = "::";
  2436. newip.len = 2;
  2437. } else {
  2438. newip.s = "0.0.0.0";
  2439. newip.len = 7;
  2440. }
  2441. } else {
  2442. if (forcedIP) {
  2443. newip.s = str2;
  2444. newip.len = strlen(newip.s);
  2445. #ifdef EXTRA_DEBUG
  2446. LM_DBG("forcing IP='%.*s'\n", newip.len, newip.s);
  2447. #endif
  2448. } else {
  2449. newip.s = (argc < 2) ? str2 : argv[1];
  2450. newip.len = strlen(newip.s);
  2451. }
  2452. }
  2453. /* marker to double check : newport goes: str -> int -> str ?!?! */
  2454. newport.s = int2str(port, &newport.len); /* beware static buffer */
  2455. /* Alter port. */
  2456. body1.s = sdp_stream->media.s;
  2457. body1.len = bodylimit - body1.s;
  2458. #ifdef EXTRA_DEBUG
  2459. LM_DBG("alter port body1='%.*s'\n", body1.len, body1.s);
  2460. #endif
  2461. /* do not do it if old port was 0 (means media disable)
  2462. * - check if actually should be better done in rtpptoxy,
  2463. * by returning also 0
  2464. * - or by not sending to rtpproxy the old port if 0
  2465. */
  2466. if(oldport.len!=1 || oldport.s[0]!='0')
  2467. {
  2468. if (alter_mediaport(msg, &body1, &oldport, &newport, 0) == -1) {
  2469. FORCE_RTP_PROXY_RET (-1);
  2470. }
  2471. }
  2472. /*
  2473. * Alter RTCP attribute if present. Inserting RTP port + 1 (as allocated
  2474. * by RTP proxy). No IP-address is needed in the new RTCP attribute as the
  2475. * 'c' attribute (altered below) will contain the RTP proxy IP address.
  2476. * See RFC 3605 for definition of RTCP attribute.
  2477. * ported from ser
  2478. */
  2479. if (sdp_stream->rtcp_port.s && sdp_stream->rtcp_port.len) {
  2480. newrtcp.s = int2str(port+1, &newrtcp.len); /* beware static buffer */
  2481. /* Alter port. */
  2482. body1.s = sdp_stream->rtcp_port.s;
  2483. body1.len = bodylimit - body1.s;
  2484. #ifdef EXTRA_DEBUG
  2485. LM_DBG("alter rtcp body1='%.*s'\n", body1.len, body1.s);
  2486. #endif
  2487. if (alter_rtcp(msg, &body1, &sdp_stream->rtcp_port, &newrtcp) == -1) {
  2488. FORCE_RTP_PROXY_RET (-1);
  2489. }
  2490. }
  2491. /* Add ice relay candidates */
  2492. if (ice_candidate_priority_val.n && sdp_stream->ice_attrs_num > 0) {
  2493. body1.s = sdp_stream->ice_attr->foundation.s - 12;
  2494. body1.len = bodylimit - body1.s;
  2495. if (insert_candidates(msg, sdp_stream->ice_attr->foundation.s - 12,
  2496. &newip, port, ice_candidate_priority_val.n) == -1) {
  2497. FORCE_RTP_PROXY_RET (-1);
  2498. }
  2499. }
  2500. c1p = sdp_session->ip_addr.s;
  2501. c2p = sdp_stream->ip_addr.s;
  2502. /*
  2503. * Alter IP. Don't alter IP common for the session
  2504. * more than once.
  2505. */
  2506. if (c2p != NULL || !c1p_altered) {
  2507. body1.s = c2p ? c2p : c1p;
  2508. body1.len = bodylimit - body1.s;
  2509. #ifdef EXTRA_DEBUG
  2510. LM_DBG("alter ip body1='%.*s'\n", body1.len, body1.s);
  2511. #endif
  2512. if (alter_mediaip(msg, &body1, &oldip, pf, &newip, pf1, 0)==-1) {
  2513. FORCE_RTP_PROXY_RET (-1);
  2514. }
  2515. if (!c2p)
  2516. c1p_altered = 1;
  2517. }
  2518. /*
  2519. * Alter common IP if required, but don't do it more than once.
  2520. */
  2521. if (commip && c1p && !c1p_altered) {
  2522. body1.s = c1p;
  2523. body1.len = bodylimit - body1.s;
  2524. #ifdef EXTRA_DEBUG
  2525. LM_DBG("alter common ip body1='%.*s'\n", body1.len, body1.s);
  2526. #endif
  2527. if (alter_mediaip(msg, &body1, &sdp_session->ip_addr, sdp_session->pf, &newip, pf1, 0)==-1) {
  2528. FORCE_RTP_PROXY_RET (-1);
  2529. }
  2530. c1p_altered = 1;
  2531. }
  2532. /*
  2533. * Alter the IP in "o=", but only once per session
  2534. */
  2535. if (o1p) {
  2536. body1.s = o1p;
  2537. body1.len = bodylimit - body1.s;
  2538. #ifdef EXTRA_DEBUG
  2539. LM_DBG("alter media ip body1='%.*s'\n", body1.len, body1.s);
  2540. #endif
  2541. if (alter_mediaip(msg, &body1, &sdp_session->o_ip_addr, sdp_session->o_pf, &newip, pf1, 0)==-1) {
  2542. FORCE_RTP_PROXY_RET (-1);
  2543. }
  2544. o1p = 0;
  2545. }
  2546. sdp_stream_num++;
  2547. }
  2548. sdp_session_num++;
  2549. }
  2550. free_opts(&opts, &rep_opts, &pt_opts);
  2551. if (proxied == 0 && nortpproxy_str.len) {
  2552. cp = pkg_malloc((nortpproxy_str.len + CRLF_LEN) * sizeof(char));
  2553. if (cp == NULL) {
  2554. LM_ERR("out of pkg memory\n");
  2555. return -1;
  2556. }
  2557. anchor = anchor_lump(msg, body.s + body.len - msg->buf, 0, 0);
  2558. if (anchor == NULL) {
  2559. LM_ERR("anchor_lump failed\n");
  2560. pkg_free(cp);
  2561. return -1;
  2562. }
  2563. memcpy(cp, CRLF, CRLF_LEN);
  2564. memcpy(cp + CRLF_LEN, nortpproxy_str.s, nortpproxy_str.len);
  2565. if (insert_new_lump_after(anchor, cp, nortpproxy_str.len + CRLF_LEN, 0) == NULL) {
  2566. LM_ERR("insert_new_lump_after failed\n");
  2567. pkg_free(cp);
  2568. return -1;
  2569. }
  2570. }
  2571. return 1;
  2572. }
  2573. static int start_recording_f(struct sip_msg* msg, char *foo, char *bar)
  2574. {
  2575. int nitems;
  2576. str callid = {0, 0};
  2577. str from_tag = {0, 0};
  2578. str to_tag = {0, 0};
  2579. struct rtpp_node *node;
  2580. struct iovec v[1 + 4 + 3] = {{NULL, 0}, {"R", 1}, {" ", 1}, {NULL, 0}, {" ", 1}, {NULL, 0}, {" ", 1}, {NULL, 0}};
  2581. /* 1 */ /* 2 */ /* 3 */ /* 4 */ /* 5 */ /* 6 */ /* 1 */
  2582. if (get_callid(msg, &callid) == -1 || callid.len == 0) {
  2583. LM_ERR("can't get Call-Id field\n");
  2584. return -1;
  2585. }
  2586. if (get_to_tag(msg, &to_tag) == -1) {
  2587. LM_ERR("can't get To tag\n");
  2588. return -1;
  2589. }
  2590. if (get_from_tag(msg, &from_tag) == -1 || from_tag.len == 0) {
  2591. LM_ERR("can't get From tag\n");
  2592. return -1;
  2593. }
  2594. if(msg->id != current_msg_id){
  2595. selected_rtpp_set = default_rtpp_set;
  2596. }
  2597. STR2IOVEC(callid, v[3]);
  2598. STR2IOVEC(from_tag, v[5]);
  2599. STR2IOVEC(to_tag, v[7]);
  2600. node = select_rtpp_node(callid, 1);
  2601. if (!node) {
  2602. LM_ERR("no available proxies\n");
  2603. return -1;
  2604. }
  2605. set_rtp_inst_pvar(msg, &node->rn_url);
  2606. nitems = 8;
  2607. if (msg->first_line.type == SIP_REPLY) {
  2608. if (to_tag.len == 0)
  2609. return -1;
  2610. STR2IOVEC(to_tag, v[5]);
  2611. STR2IOVEC(from_tag, v[7]);
  2612. } else {
  2613. STR2IOVEC(from_tag, v[5]);
  2614. STR2IOVEC(to_tag, v[7]);
  2615. if (to_tag.len <= 0)
  2616. nitems = 6;
  2617. }
  2618. send_rtpp_command(node, v, nitems);
  2619. return 1;
  2620. }
  2621. /*
  2622. * Returns the current RTP-Statistics from the RTP-Proxy
  2623. */
  2624. static int
  2625. pv_get_rtpstat_f(struct sip_msg *msg, pv_param_t *param,
  2626. pv_value_t *res)
  2627. {
  2628. str ret_val = {0, 0};
  2629. int nitems;
  2630. str callid = {0, 0};
  2631. str from_tag = {0, 0};
  2632. str to_tag = {0, 0};
  2633. struct rtpp_node *node;
  2634. struct iovec v[1 + 4 + 3 + 1] = {{NULL, 0}, {"Q", 1}, {" ", 1}, {NULL, 0},
  2635. {" ", 1}, {NULL, 0}, {";1 ", 3}, {";1", }, {NULL, 0}};
  2636. if (get_callid(msg, &callid) == -1 || callid.len == 0) {
  2637. LM_ERR("can't get Call-Id field\n");
  2638. return pv_get_null(msg, param, res);
  2639. }
  2640. if (get_to_tag(msg, &to_tag) == -1) {
  2641. LM_ERR("can't get To tag\n");
  2642. return pv_get_null(msg, param, res);
  2643. }
  2644. if (get_from_tag(msg, &from_tag) == -1 || from_tag.len == 0) {
  2645. LM_ERR("can't get From tag\n");
  2646. return pv_get_null(msg, param, res);
  2647. }
  2648. if(msg->id != current_msg_id){
  2649. selected_rtpp_set = default_rtpp_set;
  2650. }
  2651. STR2IOVEC(callid, v[3]);
  2652. STR2IOVEC(from_tag, v[5]);
  2653. STR2IOVEC(to_tag, v[7]);
  2654. node = select_rtpp_node(callid, 1);
  2655. if (!node) {
  2656. LM_ERR("no available proxies\n");
  2657. return -1;
  2658. }
  2659. set_rtp_inst_pvar(msg, &node->rn_url);
  2660. nitems = 8;
  2661. if (msg->first_line.type == SIP_REPLY) {
  2662. if (to_tag.len == 0)
  2663. return -1;
  2664. STR2IOVEC(to_tag, v[5]);
  2665. STR2IOVEC(from_tag, v[7]);
  2666. } else {
  2667. STR2IOVEC(from_tag, v[5]);
  2668. STR2IOVEC(to_tag, v[7]);
  2669. if (to_tag.len <= 0)
  2670. nitems = 6;
  2671. }
  2672. ret_val.s = send_rtpp_command(node, v, nitems);
  2673. if(ret_val.s==NULL)
  2674. return pv_get_null(msg, param, res);
  2675. ret_val.len = strlen(ret_val.s);
  2676. return pv_get_strval(msg, param, res, &ret_val);
  2677. }
  2678. int set_rtp_inst_pvar(struct sip_msg *msg, const str * const uri) {
  2679. pv_value_t val;
  2680. if (rtp_inst_pvar == NULL)
  2681. return 0;
  2682. memset(&val, 0, sizeof(pv_value_t));
  2683. val.flags = PV_VAL_STR;
  2684. val.rs = *uri;
  2685. if (rtp_inst_pvar->setf(msg, &rtp_inst_pvar->pvp, (int)EQ_T, &val) < 0)
  2686. {
  2687. LM_ERR("Failed to add RTPProxy URI to pvar\n");
  2688. return -1;
  2689. }
  2690. return 0;
  2691. }