msg_translator.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. *
  28. *
  29. * History:
  30. * --------
  31. * 2003-01-20 bug_fix: use of return value of snprintf aligned to C99 (jiri)
  32. * 2003-01-23 added rport patches, contributed by
  33. * Maxim Sobolev <[email protected]> and heavily modified by me
  34. * (andrei)
  35. * 2003-01-24 added i param to via of outgoing requests (used by tcp),
  36. * modified via_builder params (andrei)
  37. * 2003-01-27 more rport fixes (make use of new via_param->start) (andrei)
  38. * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
  39. * 2003-01-29 scratchpad removed (jiri)
  40. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  41. * 2003-03-01 VOICE_MAIL defs removed (jiri)
  42. * 2003-03-06 totags in outgoing replies bookmarked to enable
  43. * ACK/200 tag matching (andrei)
  44. * 2003-03-18 killed the build_warning snprintf (andrei)
  45. * 2003-03-31 added subst lump support (andrei)
  46. * 2003-04-01 added opt (conditional) lump support (andrei)
  47. * 2003-04-02 added more subst lumps: SUBST_{SND,RCV}_ALL
  48. * => ip:port;transport=proto (andrei)
  49. * 2003-04-12 added FL_FORCE_RPORT support (andrei)
  50. * 2003-04-13 updated warning builder -- fixed (andrei)
  51. * 2003-07-10 check_via_address knows now how to compare with ipv6 address
  52. * references (e.g [::1]) (andrei)
  53. * build_req_fomr_sip_req no longer adds 1 for ipv6 via parameter
  54. * position calculations ([] are part of host.s now) (andrei)
  55. * 2003-10-02 via+lump dst address/port can be set to preset values (andrei)
  56. * 2003-10-08 receive_test function-alized (jiri)
  57. * 2003-10-20 added body_lump list (sip_msg), adjust_clen (andrei & jan)
  58. * 2003-11-11 type of rpl_lumps replaced by flags (bogdan)
  59. * 2006-04-20 build_req_from_sip_req, via_builder and lump_* functions
  60. * use now struct dest_info; lumps & via comp param support
  61. * (rfc3486) (andrei)
  62. * 2007-08-31 id_builder() and via_builder() are grouped into one function:
  63. * create_via_hf() -- tm module needs them as well (Miklos)
  64. * 2008-12-17 build_req_from_sip_req() will now fallback to tcp, tls or sctp
  65. * if packet size > udp_mtu and fallback is enabled
  66. * build_req_from_sip_req() uses now global_req_flags along
  67. * msg->msg_flags (andrei)
  68. *
  69. */
  70. /* Via special params:
  71. * requests:
  72. * - if the address in via is different from the src_ip or an existing
  73. * received=something is found, received=src_ip is added (and any preexisting
  74. * received is deleted). received is added as the first via parameter if no
  75. * receive is previously present or over the old receive.
  76. * - if the original via contains rport / rport=something or msg->msg_flags
  77. * FL_FORCE_RPORT is set (e.g. script force_rport() cmd) rport=src_port
  78. * is added (over previous rport / as first via param or after received
  79. * if no received was present and received is added too)
  80. * local replies:
  81. * (see also sl_send_reply)
  82. * - rport and received are added in mostly the same way as for requests, but
  83. * in the reverse order (first rport and then received). See also
  84. * limitations.
  85. * - if reply_to_via is set (default off) the local reply will be sent to
  86. * the address in via (received is ignored since it was not set by us). The
  87. * destination port is either the message source port if via contains rport
  88. * or the FL_FORCE_RPORT flag is set or the port from the via. If either
  89. * port or rport are present a normal dns lookup (instead of a srv lookup)
  90. * is performed on the address. If no port is present and a srv lookup is
  91. * performed the port is taken from the srv lookup. If the srv lookup failed
  92. * or it was not performed, the port is set to the default sip port (5060).
  93. * - if reply_to_via is off (default) the local reply is sent to the message
  94. * source ip address. The destination port is set to the source port if
  95. * rport is present or FL_FORCE_RPORT flag is set, to the via port or to
  96. * the default sip port (5060) if neither rport or via port are present.
  97. * "normal" replies:
  98. * - if received is present the message is sent to the received address else
  99. * if no port is present (neither a normal via port or rport) a dns srv
  100. * lookup is performed on the host part and the reply is sent to the
  101. * resulting ip. If a port is present or the host part is an ip address
  102. * the dns lookup will be a "normal" one (A or AAAA).
  103. * - if rport is present, it's value will be used as the destination port
  104. * (and this will also disable srv lookups)
  105. * - if no port is present the destination port will be taken from the srv
  106. * lookup. If the srv lookup fails or is not performed (e.g. ip address
  107. * in host) the destination port will be set to the default sip port (5060).
  108. *
  109. * Known limitations:
  110. * - when locally replying to a message, rport and received will be appended to
  111. * the via header parameters (for forwarded requests they are inserted at the
  112. * beginning).
  113. * - a locally generated reply might get two received via parameters if a
  114. * received is already present in the original message (this should not
  115. * happen though, but ...)
  116. *
  117. *--andrei
  118. */
  119. /*!
  120. * \file
  121. * \brief SIP-router core ::
  122. * \ingroup core
  123. * Module: \ref core
  124. */
  125. #include <sys/types.h>
  126. #include <sys/socket.h>
  127. #include <netdb.h>
  128. #include <string.h>
  129. #include <stdio.h>
  130. #include <stdlib.h>
  131. #include "comp_defs.h"
  132. #include "msg_translator.h"
  133. #include "globals.h"
  134. #include "error.h"
  135. #include "mem/mem.h"
  136. #include "dprint.h"
  137. #include "config.h"
  138. #include "md5utils.h"
  139. #include "data_lump.h"
  140. #include "data_lump_rpl.h"
  141. #include "ip_addr.h"
  142. #include "resolve.h"
  143. #include "ut.h"
  144. #include "pt.h"
  145. #include "cfg/cfg.h"
  146. #include "parser/parse_to.h"
  147. #include "parser/parse_param.h"
  148. #include "forward.h"
  149. #include "str_list.h"
  150. #define append_str_trans(_dest,_src,_len,_msg) \
  151. append_str( (_dest), (_src), (_len) );
  152. extern char version[];
  153. extern int version_len;
  154. /** per process fixup function for global_req_flags.
  155. * It should be called from the configuration framework.
  156. */
  157. void fix_global_req_flags(str* gname, str* name)
  158. {
  159. global_req_flags=0;
  160. switch(cfg_get(core, core_cfg, udp_mtu_try_proto)){
  161. case PROTO_NONE:
  162. case PROTO_UDP:
  163. /* do nothing */
  164. break;
  165. case PROTO_TCP:
  166. global_req_flags|=FL_MTU_TCP_FB;
  167. break;
  168. case PROTO_TLS:
  169. global_req_flags|=FL_MTU_TLS_FB;
  170. break;
  171. case PROTO_SCTP:
  172. global_req_flags|=FL_MTU_SCTP_FB;
  173. break;
  174. }
  175. if (cfg_get(core, core_cfg, force_rport))
  176. global_req_flags|=FL_FORCE_RPORT;
  177. }
  178. /* checks if ip is in host(name) and ?host(ip)=name?
  179. * ip must be in network byte order!
  180. * resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
  181. * return 0 if equal */
  182. static int check_via_address(struct ip_addr* ip, str *name,
  183. unsigned short port, int resolver)
  184. {
  185. struct hostent* he;
  186. int i;
  187. char* s;
  188. int len;
  189. char lproto;
  190. /* maybe we are lucky and name it's an ip */
  191. s=ip_addr2a(ip);
  192. if (s){
  193. DBG("check_via_address(%s, %.*s, %d)\n",
  194. s, name->len, name->s, resolver);
  195. len=strlen(s);
  196. /* check if name->s is an ipv6 address or an ipv6 address ref. */
  197. if ((ip->af==AF_INET6) &&
  198. ( ((len==name->len)&&(strncasecmp(name->s, s, name->len)==0))
  199. ||
  200. ((len==(name->len-2))&&(name->s[0]=='[')&&
  201. (name->s[name->len-1]==']')&&
  202. (strncasecmp(name->s+1, s, len)==0))
  203. )
  204. )
  205. return 0;
  206. else
  207. if (strncmp(name->s, s, name->len)==0)
  208. return 0;
  209. }else{
  210. LM_CRIT("could not convert ip address\n");
  211. return -1;
  212. }
  213. if (port==0) port=SIP_PORT;
  214. if (resolver&DO_DNS){
  215. DBG("check_via_address: doing dns lookup\n");
  216. /* try all names ips */
  217. lproto = PROTO_NONE;
  218. he=sip_resolvehost(name, &port, &lproto); /* don't use naptr */
  219. if (he && ip->af==he->h_addrtype){
  220. for(i=0;he && he->h_addr_list[i];i++){
  221. if ( memcmp(&he->h_addr_list[i], ip->u.addr, ip->len)==0)
  222. return 0;
  223. }
  224. }
  225. }
  226. if (resolver&DO_REV_DNS){
  227. DBG("check_via_address: doing rev. dns lookup\n");
  228. /* try reverse dns */
  229. he=rev_resolvehost(ip);
  230. if (he && (strncmp(he->h_name, name->s, name->len)==0))
  231. return 0;
  232. for (i=0; he && he->h_aliases[i];i++){
  233. if (strncmp(he->h_aliases[i],name->s, name->len)==0)
  234. return 0;
  235. }
  236. }
  237. return -1;
  238. }
  239. /* check if IP address in Via != source IP address of signaling,
  240. * or the sender requires adding rport or received values */
  241. int received_test( struct sip_msg *msg )
  242. {
  243. int rcvd;
  244. rcvd=msg->via1->received || msg->via1->rport
  245. || check_via_address(&msg->rcv.src_ip, &msg->via1->host,
  246. msg->via1->port, received_dns);
  247. return rcvd;
  248. }
  249. /* check if IP address in Via != source IP address of signaling */
  250. int received_via_test( struct sip_msg *msg )
  251. {
  252. int rcvd;
  253. rcvd = (check_via_address(&msg->rcv.src_ip, &msg->via1->host,
  254. msg->via1->port, received_dns)!=0);
  255. return rcvd;
  256. }
  257. static char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
  258. {
  259. static char buf[MAX_WARNING_LEN];
  260. str *foo;
  261. int print_len, l;
  262. int clen;
  263. char* t;
  264. #define str_print(string, string_len) \
  265. do{ \
  266. l=(string_len); \
  267. if ((clen+l)>MAX_WARNING_LEN) \
  268. goto error_overflow; \
  269. memcpy(buf+clen, (string), l); \
  270. clen+=l; \
  271. }while(0)
  272. #define str_lenpair_print(string, string_len, string2, string2_len) \
  273. do{ \
  274. str_print(string, string_len); \
  275. str_print(string2, string2_len);\
  276. }while(0)
  277. #define str_pair_print( string, string2, string2_len) \
  278. str_lenpair_print((string), strlen((string)), (string2), (string2_len))
  279. #define str_int_print(string, intval)\
  280. do{\
  281. t=int2str((intval), &print_len); \
  282. str_pair_print(string, t, print_len);\
  283. } while(0)
  284. #define str_su_print(sockaddr)\
  285. do{\
  286. t=su2a(&sockaddr, sizeof(sockaddr)); \
  287. print_len=strlen(t); \
  288. str_print(t, print_len); \
  289. } while(0)
  290. #define str_ipaddr_print(string, ipaddr_val)\
  291. do{\
  292. t=ip_addr2a((ipaddr_val)); \
  293. print_len=strlen(t); \
  294. str_pair_print(string, t, print_len);\
  295. } while(0)
  296. clen=0;
  297. str_print(WARNING, WARNING_LEN);
  298. str_su_print(msg->rcv.bind_address->su);
  299. str_print(WARNING_PHRASE, WARNING_PHRASE_LEN);
  300. /*adding out_uri*/
  301. if (msg->new_uri.s)
  302. foo=&(msg->new_uri);
  303. else
  304. foo=&(msg->first_line.u.request.uri);
  305. /* pid= */
  306. str_int_print(" pid=", my_pid());
  307. /* req_src_ip= */
  308. str_ipaddr_print(" req_src_ip=", &msg->rcv.src_ip);
  309. str_int_print(" req_src_port=", msg->rcv.src_port);
  310. str_pair_print(" in_uri=", msg->first_line.u.request.uri.s,
  311. msg->first_line.u.request.uri.len);
  312. str_pair_print(" out_uri=", foo->s, foo->len);
  313. str_pair_print(" via_cnt",
  314. (msg->parsed_flag & HDR_EOH_F)==HDR_EOH_F ? "=" : ">", 1);
  315. str_int_print("=", via_cnt);
  316. if (clen<MAX_WARNING_LEN){ buf[clen]='"'; clen++; }
  317. else goto error_overflow;
  318. *returned_len=clen;
  319. return buf;
  320. error_overflow:
  321. LM_NOTICE("buffer size exceeded (probably too long URI)\n");
  322. *returned_len=0;
  323. return 0;
  324. }
  325. char* received_builder(struct sip_msg *msg, unsigned int *received_len)
  326. {
  327. char *buf;
  328. int len;
  329. struct ip_addr *source_ip;
  330. char *tmp;
  331. int tmp_len;
  332. source_ip=&msg->rcv.src_ip;
  333. buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
  334. if (buf==0){
  335. ser_error=E_OUT_OF_MEM;
  336. LM_ERR("out of memory\n");
  337. return 0;
  338. }
  339. memcpy(buf, RECEIVED, RECEIVED_LEN);
  340. if ( (tmp=ip_addr2a(source_ip))==0) {
  341. pkg_free(buf);
  342. return 0; /* error*/
  343. }
  344. tmp_len=strlen(tmp);
  345. len=RECEIVED_LEN+tmp_len;
  346. memcpy(buf+RECEIVED_LEN, tmp, tmp_len);
  347. buf[len]=0; /*null terminate it */
  348. *received_len = len;
  349. return buf;
  350. }
  351. char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
  352. {
  353. char* buf;
  354. char* tmp;
  355. int tmp_len;
  356. int len;
  357. tmp_len=0;
  358. tmp=int2str(msg->rcv.src_port, &tmp_len);
  359. len=RPORT_LEN+tmp_len;
  360. buf=pkg_malloc(sizeof(char)*(len+1));/* space for null term */
  361. if (buf==0){
  362. ser_error=E_OUT_OF_MEM;
  363. LM_ERR("out of memory\n");
  364. return 0;
  365. }
  366. memcpy(buf, RPORT, RPORT_LEN);
  367. memcpy(buf+RPORT_LEN, tmp, tmp_len);
  368. buf[len]=0; /*null terminate it*/
  369. *rport_len=len;
  370. return buf;
  371. }
  372. char* id_builder(struct sip_msg* msg, unsigned int *id_len)
  373. {
  374. char* buf;
  375. int len, value_len;
  376. char revhex[sizeof(int)*2];
  377. char* p;
  378. int size;
  379. size=sizeof(int)*2;
  380. p=&revhex[0];
  381. if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
  382. LM_CRIT("not enough space for id\n");
  383. return 0;
  384. }
  385. value_len=p-&revhex[0];
  386. len=ID_PARAM_LEN+value_len;
  387. buf=pkg_malloc(sizeof(char)*(len+1));/* place for ending \0 */
  388. if (buf==0){
  389. ser_error=E_OUT_OF_MEM;
  390. LM_ERR("out of memory\n");
  391. return 0;
  392. }
  393. memcpy(buf, ID_PARAM, ID_PARAM_LEN);
  394. memcpy(buf+ID_PARAM_LEN, revhex, value_len);
  395. buf[len]=0; /* null terminate it */
  396. *id_len=len;
  397. return buf;
  398. }
  399. char* clen_builder( struct sip_msg* msg, int *clen_len, int diff,
  400. int body_only)
  401. {
  402. char* buf;
  403. int len;
  404. int value;
  405. char* value_s;
  406. int value_len;
  407. char* body;
  408. body=get_body(msg);
  409. if (body==0){
  410. ser_error=E_BAD_REQ;
  411. LM_ERR("no message body found (missing crlf?)");
  412. return 0;
  413. }
  414. value=msg->len-(int)(body-msg->buf)+diff;
  415. value_s=int2str(value, &value_len);
  416. DBG("clen_builder: content-length: %d (%s)\n", value, value_s);
  417. if (body_only) {
  418. len=value_len;
  419. }
  420. else {
  421. len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
  422. }
  423. buf=pkg_malloc(sizeof(char)*(len+1));
  424. if (buf==0){
  425. ser_error=E_OUT_OF_MEM;
  426. LM_ERR("out of memory\n");
  427. return 0;
  428. }
  429. if (body_only) {
  430. memcpy(buf, value_s, value_len);
  431. }
  432. else {
  433. memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
  434. memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
  435. memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
  436. }
  437. buf[len]=0; /* null terminate it */
  438. *clen_len=len;
  439. return buf;
  440. }
  441. /* checks if a lump opt condition
  442. * returns 1 if cond is true, 0 if false */
  443. static inline int lump_check_opt( struct lump *l,
  444. struct sip_msg* msg,
  445. struct dest_info* snd_i
  446. )
  447. {
  448. struct ip_addr* ip;
  449. unsigned short port;
  450. int proto;
  451. #define get_ip_port_proto \
  452. if ((snd_i==0) || (snd_i->send_sock==0)){ \
  453. LM_CRIT("null send socket\n"); \
  454. return 1; /* we presume they are different :-) */ \
  455. } \
  456. if (msg->rcv.bind_address){ \
  457. ip=&msg->rcv.bind_address->address; \
  458. port=msg->rcv.bind_address->port_no; \
  459. proto=msg->rcv.bind_address->proto; \
  460. }else{ \
  461. ip=&msg->rcv.dst_ip; \
  462. port=msg->rcv.dst_port; \
  463. proto=msg->rcv.proto; \
  464. } \
  465. switch(l->u.cond){
  466. case COND_FALSE:
  467. return 0;
  468. case COND_TRUE:
  469. LUMP_SET_COND_TRUE(l);
  470. return 1;
  471. case COND_IF_DIFF_REALMS:
  472. get_ip_port_proto;
  473. /* faster tests first */
  474. if ((port==snd_i->send_sock->port_no) &&
  475. (proto==snd_i->send_sock->proto) &&
  476. #ifdef USE_COMP
  477. (msg->rcv.comp==snd_i->comp) &&
  478. #endif
  479. (ip_addr_cmp(ip, &snd_i->send_sock->address)))
  480. return 0;
  481. else {
  482. LUMP_SET_COND_TRUE(l);
  483. return 1;
  484. }
  485. case COND_IF_DIFF_AF:
  486. get_ip_port_proto;
  487. if (ip->af!=snd_i->send_sock->address.af) {
  488. LUMP_SET_COND_TRUE(l);
  489. return 1;
  490. } else return 0;
  491. case COND_IF_DIFF_PROTO:
  492. get_ip_port_proto;
  493. if (proto!=snd_i->send_sock->proto) {
  494. LUMP_SET_COND_TRUE(l);
  495. return 1;
  496. } else return 0;
  497. case COND_IF_DIFF_PORT:
  498. get_ip_port_proto;
  499. if (port!=snd_i->send_sock->port_no) {
  500. LUMP_SET_COND_TRUE(l);
  501. return 1;
  502. } else return 0;
  503. case COND_IF_DIFF_IP:
  504. get_ip_port_proto;
  505. if (ip_addr_cmp(ip, &snd_i->send_sock->address)) return 0;
  506. else {
  507. LUMP_SET_COND_TRUE(l);
  508. return 1;
  509. }
  510. case COND_IF_RAND:
  511. if(rand()>=RAND_MAX/2) {
  512. LUMP_SET_COND_TRUE(l);
  513. return 1;
  514. } else return 0;
  515. default:
  516. LM_CRIT("unknown lump condition %d\n", l->u.cond);
  517. }
  518. return 0; /* false */
  519. }
  520. /* computes the "unpacked" len of a lump list,
  521. code moved from build_req_from_req */
  522. static inline int lumps_len(struct sip_msg* msg, struct lump* lumps,
  523. struct dest_info* send_info)
  524. {
  525. int s_offset;
  526. int new_len;
  527. struct lump* t;
  528. struct lump* r;
  529. str* send_address_str;
  530. str* send_port_str;
  531. str* recv_address_str = NULL;
  532. str* recv_port_str = NULL;
  533. int recv_port_no = 0;
  534. struct socket_info* send_sock;
  535. #ifdef USE_COMP
  536. #define RCVCOMP_LUMP_LEN \
  537. /* add;comp=xxx */ \
  538. switch(msg->rcv.comp){ \
  539. case COMP_NONE: \
  540. break; \
  541. case COMP_SIGCOMP: \
  542. new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
  543. break; \
  544. case COMP_SERGZ: \
  545. new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
  546. break; \
  547. default: \
  548. LM_CRIT("unknown comp %d\n", msg->rcv.comp); \
  549. }
  550. #define SENDCOMP_LUMP_LEN \
  551. /* add;comp=xxx */ \
  552. switch(send_info->comp){ \
  553. case COMP_NONE: \
  554. break; \
  555. case COMP_SIGCOMP: \
  556. new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
  557. break; \
  558. case COMP_SERGZ: \
  559. new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
  560. break; \
  561. default: \
  562. LM_CRIT("unknown comp %d\n", send_info->comp); \
  563. }
  564. #else
  565. #define RCVCOMP_LUMP_LEN
  566. #define SENDCOMP_LUMP_LEN
  567. #endif /*USE_COMP */
  568. #define SUBST_LUMP_LEN(subst_l) \
  569. switch((subst_l)->u.subst){ \
  570. case SUBST_RCV_IP: \
  571. if (msg->rcv.bind_address){ \
  572. new_len+=recv_address_str->len; \
  573. if (msg->rcv.bind_address->address.af!=AF_INET) \
  574. new_len+=2; \
  575. }else{ \
  576. /* FIXME */ \
  577. LM_CRIT("FIXME: null bind_address\n"); \
  578. }; \
  579. break; \
  580. case SUBST_RCV_PORT: \
  581. if (msg->rcv.bind_address){ \
  582. new_len+=recv_port_str->len; \
  583. }else{ \
  584. /* FIXME */ \
  585. LM_CRIT("FIXME: null bind_address\n"); \
  586. }; \
  587. break; \
  588. case SUBST_RCV_PROTO: \
  589. if (msg->rcv.bind_address){ \
  590. switch(msg->rcv.bind_address->proto){ \
  591. case PROTO_NONE: \
  592. case PROTO_UDP: \
  593. new_len+=3; \
  594. break; \
  595. case PROTO_TCP: \
  596. case PROTO_TLS: \
  597. switch(msg->rcv.proto){ \
  598. case PROTO_WS: \
  599. case PROTO_WSS: \
  600. new_len+=2; \
  601. break; \
  602. default: \
  603. new_len+=3; \
  604. break; \
  605. } \
  606. break; \
  607. case PROTO_SCTP: \
  608. new_len+=4; \
  609. break; \
  610. default: \
  611. LM_CRIT("unknown proto %d\n", msg->rcv.bind_address->proto); \
  612. }\
  613. }else{ \
  614. /* FIXME */ \
  615. LM_CRIT("FIXME: null bind_address\n"); \
  616. }; \
  617. break; \
  618. case SUBST_RCV_ALL: \
  619. if (msg->rcv.bind_address){ \
  620. new_len+=recv_address_str->len; \
  621. if (msg->rcv.bind_address->address.af!=AF_INET) \
  622. new_len+=2; \
  623. if (recv_port_no!=SIP_PORT){ \
  624. /* add :port_no */ \
  625. new_len+=1+recv_port_str->len; \
  626. }\
  627. /*add;transport=xxx*/ \
  628. switch(msg->rcv.bind_address->proto){ \
  629. case PROTO_NONE: \
  630. case PROTO_UDP: \
  631. break; /* udp is the default */ \
  632. case PROTO_TCP: \
  633. case PROTO_TLS: \
  634. switch(msg->rcv.proto){ \
  635. case PROTO_WS: \
  636. case PROTO_WSS: \
  637. new_len+=TRANSPORT_PARAM_LEN+2; \
  638. break; \
  639. default: \
  640. new_len+=TRANSPORT_PARAM_LEN+3; \
  641. break; \
  642. } \
  643. break; \
  644. case PROTO_SCTP: \
  645. new_len+=TRANSPORT_PARAM_LEN+4; \
  646. break; \
  647. default: \
  648. LM_CRIT("unknown proto %d\n", \
  649. msg->rcv.bind_address->proto); \
  650. }\
  651. RCVCOMP_LUMP_LEN \
  652. }else{ \
  653. /* FIXME */ \
  654. LM_CRIT("FIXME: null bind_address\n"); \
  655. }; \
  656. break; \
  657. case SUBST_SND_IP: \
  658. if (send_sock){ \
  659. new_len+=send_address_str->len; \
  660. if (send_sock->address.af!=AF_INET && \
  661. send_address_str==&(send_sock->address_str)) \
  662. new_len+=2; \
  663. }else{ \
  664. LM_CRIT("FIXME: null send_sock\n"); \
  665. }; \
  666. break; \
  667. case SUBST_SND_PORT: \
  668. if (send_sock){ \
  669. new_len+=send_port_str->len; \
  670. }else{ \
  671. LM_CRIT("FIXME: null send_sock\n"); \
  672. }; \
  673. break; \
  674. case SUBST_SND_PROTO: \
  675. if (send_sock){ \
  676. switch(send_sock->proto){ \
  677. case PROTO_NONE: \
  678. case PROTO_UDP: \
  679. new_len+=3; \
  680. break; \
  681. case PROTO_TCP: \
  682. case PROTO_TLS: \
  683. switch(send_info->proto){ \
  684. case PROTO_WS: \
  685. case PROTO_WSS: \
  686. new_len+=2; \
  687. break; \
  688. default: \
  689. new_len+=3; \
  690. break; \
  691. } \
  692. break; \
  693. case PROTO_SCTP: \
  694. new_len+=4; \
  695. break; \
  696. default: \
  697. LM_CRIT("unknown proto %d\n", send_sock->proto); \
  698. }\
  699. }else{ \
  700. LM_CRIT("FIXME: null send_sock\n"); \
  701. }; \
  702. break; \
  703. case SUBST_SND_ALL: \
  704. if (send_sock){ \
  705. new_len+=send_address_str->len; \
  706. if ((send_sock->address.af!=AF_INET) && \
  707. (send_address_str==&(send_sock->address_str))) \
  708. new_len+=2; \
  709. if ((send_sock->port_no!=SIP_PORT) || \
  710. (send_port_str!=&(send_sock->port_no_str))){ \
  711. /* add :port_no */ \
  712. new_len+=1+send_port_str->len; \
  713. }\
  714. /*add;transport=xxx*/ \
  715. switch(send_sock->proto){ \
  716. case PROTO_NONE: \
  717. case PROTO_UDP: \
  718. break; /* udp is the default */ \
  719. case PROTO_TCP: \
  720. case PROTO_TLS: \
  721. switch(send_info->proto){ \
  722. case PROTO_WS: \
  723. case PROTO_WSS: \
  724. new_len+=TRANSPORT_PARAM_LEN+2; \
  725. break; \
  726. default: \
  727. new_len+=TRANSPORT_PARAM_LEN+3; \
  728. break; \
  729. } \
  730. break; \
  731. case PROTO_SCTP: \
  732. new_len+=TRANSPORT_PARAM_LEN+4; \
  733. break; \
  734. default: \
  735. LM_CRIT("unknown proto %d\n", send_sock->proto); \
  736. }\
  737. SENDCOMP_LUMP_LEN \
  738. }else{ \
  739. /* FIXME */ \
  740. LM_CRIT("FIXME: null send_sock\n"); \
  741. }; \
  742. break; \
  743. case SUBST_NOP: /* do nothing */ \
  744. break; \
  745. default: \
  746. LM_CRIT("unknown subst type %d\n", (subst_l)->u.subst); \
  747. }
  748. if (send_info){
  749. send_sock=send_info->send_sock;
  750. }else{
  751. send_sock=0;
  752. };
  753. s_offset=0;
  754. new_len=0;
  755. /* init send_address_str & send_port_str */
  756. if(send_sock && send_sock->useinfo.name.len>0)
  757. send_address_str=&(send_sock->useinfo.name);
  758. else if (msg->set_global_address.len)
  759. send_address_str=&(msg->set_global_address);
  760. else
  761. send_address_str=&(send_sock->address_str);
  762. if(send_sock && send_sock->useinfo.port_no>0)
  763. send_port_str=&(send_sock->useinfo.port_no_str);
  764. else if (msg->set_global_port.len)
  765. send_port_str=&(msg->set_global_port);
  766. else
  767. send_port_str=&(send_sock->port_no_str);
  768. /* init recv_address_str, recv_port_str & recv_port_no */
  769. if(msg->rcv.bind_address) {
  770. if(msg->rcv.bind_address->useinfo.name.len>0)
  771. recv_address_str=&(msg->rcv.bind_address->useinfo.name);
  772. else
  773. recv_address_str=&(msg->rcv.bind_address->address_str);
  774. if(msg->rcv.bind_address->useinfo.port_no>0) {
  775. recv_port_str=&(msg->rcv.bind_address->useinfo.port_no_str);
  776. recv_port_no = msg->rcv.bind_address->useinfo.port_no;
  777. } else {
  778. recv_port_str=&(msg->rcv.bind_address->port_no_str);
  779. recv_port_no = msg->rcv.bind_address->port_no;
  780. }
  781. }
  782. for(t=lumps;t;t=t->next){
  783. /* skip if this is an OPT lump and the condition is not satisfied */
  784. if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t, msg, send_info))
  785. continue;
  786. for(r=t->before;r;r=r->before){
  787. switch(r->op){
  788. case LUMP_ADD:
  789. new_len+=r->len;
  790. break;
  791. case LUMP_ADD_SUBST:
  792. SUBST_LUMP_LEN(r);
  793. break;
  794. case LUMP_ADD_OPT:
  795. /* skip if this is an OPT lump and the condition is
  796. * not satisfied */
  797. if (!lump_check_opt(r, msg, send_info))
  798. goto skip_before;
  799. break;
  800. default:
  801. /* only ADD allowed for before/after */
  802. LM_CRIT("invalid op for data lump (%x)\n", r->op);
  803. }
  804. }
  805. skip_before:
  806. switch(t->op){
  807. case LUMP_ADD:
  808. new_len+=t->len;
  809. break;
  810. case LUMP_ADD_SUBST:
  811. SUBST_LUMP_LEN(t);
  812. break;
  813. case LUMP_ADD_OPT:
  814. /* we don't do anything here, it's only a condition for
  815. * before & after */
  816. break;
  817. case LUMP_DEL:
  818. /* fix overlapping deleted zones */
  819. if (t->u.offset < s_offset){
  820. /* change len */
  821. if (t->len>s_offset-t->u.offset)
  822. t->len-=s_offset-t->u.offset;
  823. else t->len=0;
  824. t->u.offset=s_offset;
  825. }
  826. s_offset=t->u.offset+t->len;
  827. new_len-=t->len;
  828. break;
  829. case LUMP_NOP:
  830. /* fix offset if overlapping on a deleted zone */
  831. if (t->u.offset < s_offset){
  832. t->u.offset=s_offset;
  833. }else
  834. s_offset=t->u.offset;
  835. /* do nothing */
  836. break;
  837. default:
  838. LM_CRIT("invalid op for data lump (%x)\n", r->op);
  839. }
  840. for (r=t->after;r;r=r->after){
  841. switch(r->op){
  842. case LUMP_ADD:
  843. new_len+=r->len;
  844. break;
  845. case LUMP_ADD_SUBST:
  846. SUBST_LUMP_LEN(r);
  847. break;
  848. case LUMP_ADD_OPT:
  849. /* skip if this is an OPT lump and the condition is
  850. * not satisfied */
  851. if (!lump_check_opt(r, msg, send_info))
  852. goto skip_after;
  853. break;
  854. default:
  855. /* only ADD allowed for before/after */
  856. LM_CRIT("invalid op for data lump (%x)\n", r->op);
  857. }
  858. }
  859. skip_after:
  860. ; /* to make gcc 3.* happy */
  861. }
  862. return new_len;
  863. #undef RCVCOMP_LUMP_LEN
  864. #undef SENDCOMP_LUMP_LEN
  865. }
  866. /* another helper functions, adds/Removes the lump,
  867. code moved form build_req_from_req */
  868. static inline void process_lumps( struct sip_msg* msg,
  869. struct lump* lumps,
  870. char* new_buf,
  871. unsigned int* new_buf_offs,
  872. unsigned int* orig_offs,
  873. struct dest_info* send_info)
  874. {
  875. struct lump *t;
  876. struct lump *r;
  877. char* orig;
  878. int size;
  879. int offset;
  880. int s_offset;
  881. str* send_address_str;
  882. str* send_port_str;
  883. str* recv_address_str = NULL;
  884. str* recv_port_str = NULL;
  885. int recv_port_no = 0;
  886. struct socket_info* send_sock;
  887. #ifdef USE_COMP
  888. #define RCVCOMP_PARAM_ADD \
  889. /* add ;comp=xxxx */ \
  890. switch(msg->rcv.comp){ \
  891. case COMP_NONE: \
  892. break; \
  893. case COMP_SIGCOMP: \
  894. memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
  895. offset+=COMP_PARAM_LEN; \
  896. memcpy(new_buf+offset, SIGCOMP_NAME, \
  897. SIGCOMP_NAME_LEN); \
  898. offset+=SIGCOMP_NAME_LEN; \
  899. break; \
  900. case COMP_SERGZ: \
  901. memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
  902. offset+=COMP_PARAM_LEN; \
  903. memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
  904. offset+=SERGZ_NAME_LEN; \
  905. break;\
  906. default:\
  907. LM_CRIT("unknown comp %d\n", msg->rcv.comp); \
  908. }
  909. #define SENDCOMP_PARAM_ADD \
  910. /* add ;comp=xxxx */ \
  911. switch(send_info->comp){ \
  912. case COMP_NONE: \
  913. break; \
  914. case COMP_SIGCOMP: \
  915. memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
  916. offset+=COMP_PARAM_LEN; \
  917. memcpy(new_buf+offset, SIGCOMP_NAME, \
  918. SIGCOMP_NAME_LEN); \
  919. offset+=SIGCOMP_NAME_LEN; \
  920. break; \
  921. case COMP_SERGZ: \
  922. memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
  923. offset+=COMP_PARAM_LEN; \
  924. memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
  925. offset+=SERGZ_NAME_LEN; \
  926. break;\
  927. default:\
  928. LM_CRIT("unknown comp %d\n", msg->rcv.comp); \
  929. }
  930. #else
  931. #define RCVCOMP_PARAM_ADD
  932. #define SENDCOMP_PARAM_ADD
  933. #endif /* USE_COMP */
  934. #define SUBST_LUMP(subst_l) \
  935. switch((subst_l)->u.subst){ \
  936. case SUBST_RCV_IP: \
  937. if (msg->rcv.bind_address){ \
  938. if (msg->rcv.bind_address->address.af!=AF_INET){\
  939. new_buf[offset]='['; offset++; \
  940. }\
  941. memcpy(new_buf+offset, recv_address_str->s, \
  942. recv_address_str->len); \
  943. offset+=recv_address_str->len; \
  944. if (msg->rcv.bind_address->address.af!=AF_INET){\
  945. new_buf[offset]=']'; offset++; \
  946. }\
  947. }else{ \
  948. /*FIXME*/ \
  949. LM_CRIT("FIXME: null bind_address\n"); \
  950. }; \
  951. break; \
  952. case SUBST_RCV_PORT: \
  953. if (msg->rcv.bind_address){ \
  954. memcpy(new_buf+offset, recv_port_str->s, \
  955. recv_port_str->len); \
  956. offset+=recv_port_str->len; \
  957. }else{ \
  958. /*FIXME*/ \
  959. LM_CRIT("FIXME: null bind_address\n"); \
  960. }; \
  961. break; \
  962. case SUBST_RCV_ALL: \
  963. if (msg->rcv.bind_address){ \
  964. /* address */ \
  965. if (msg->rcv.bind_address->address.af!=AF_INET){\
  966. new_buf[offset]='['; offset++; \
  967. }\
  968. memcpy(new_buf+offset, recv_address_str->s, \
  969. recv_address_str->len); \
  970. offset+=recv_address_str->len; \
  971. if (msg->rcv.bind_address->address.af!=AF_INET){\
  972. new_buf[offset]=']'; offset++; \
  973. }\
  974. /* :port */ \
  975. if (recv_port_no!=SIP_PORT){ \
  976. new_buf[offset]=':'; offset++; \
  977. memcpy(new_buf+offset, \
  978. recv_port_str->s, \
  979. recv_port_str->len); \
  980. offset+=recv_port_str->len; \
  981. }\
  982. switch(msg->rcv.bind_address->proto){ \
  983. case PROTO_NONE: \
  984. case PROTO_UDP: \
  985. break; /* nothing to do, udp is default*/ \
  986. case PROTO_TCP: \
  987. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  988. TRANSPORT_PARAM_LEN); \
  989. offset+=TRANSPORT_PARAM_LEN; \
  990. if (msg->rcv.proto == PROTO_WS) { \
  991. memcpy(new_buf+offset, "ws", 2); \
  992. offset+=2; \
  993. } else { \
  994. memcpy(new_buf+offset, "tcp", 3); \
  995. offset+=3; \
  996. } \
  997. break; \
  998. case PROTO_TLS: \
  999. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  1000. TRANSPORT_PARAM_LEN); \
  1001. offset+=TRANSPORT_PARAM_LEN; \
  1002. if (msg->rcv.proto == PROTO_WS || msg->rcv.proto == PROTO_WSS) { \
  1003. memcpy(new_buf+offset, "ws", 2); \
  1004. offset+=2; \
  1005. } else { \
  1006. memcpy(new_buf+offset, "tls", 3); \
  1007. offset+=3; \
  1008. } \
  1009. break; \
  1010. case PROTO_SCTP: \
  1011. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  1012. TRANSPORT_PARAM_LEN); \
  1013. offset+=TRANSPORT_PARAM_LEN; \
  1014. memcpy(new_buf+offset, "sctp", 4); \
  1015. offset+=4; \
  1016. break; \
  1017. default: \
  1018. LM_CRIT("unknown proto %d\n", msg->rcv.bind_address->proto); \
  1019. } \
  1020. RCVCOMP_PARAM_ADD \
  1021. }else{ \
  1022. /*FIXME*/ \
  1023. LM_CRIT("FIXME: null bind_address\n"); \
  1024. }; \
  1025. break; \
  1026. case SUBST_SND_IP: \
  1027. if (send_sock){ \
  1028. if ((send_sock->address.af!=AF_INET) && \
  1029. (send_address_str==&(send_sock->address_str))){\
  1030. new_buf[offset]='['; offset++; \
  1031. }\
  1032. memcpy(new_buf+offset, send_address_str->s, \
  1033. send_address_str->len); \
  1034. offset+=send_address_str->len; \
  1035. if ((send_sock->address.af!=AF_INET) && \
  1036. (send_address_str==&(send_sock->address_str))){\
  1037. new_buf[offset]=']'; offset++; \
  1038. }\
  1039. }else{ \
  1040. /*FIXME*/ \
  1041. LM_CRIT("FIXME: null send_sock\n"); \
  1042. }; \
  1043. break; \
  1044. case SUBST_SND_PORT: \
  1045. if (send_sock){ \
  1046. memcpy(new_buf+offset, send_port_str->s, \
  1047. send_port_str->len); \
  1048. offset+=send_port_str->len; \
  1049. }else{ \
  1050. /*FIXME*/ \
  1051. LM_CRIT("FIXME: null send_sock\n"); \
  1052. }; \
  1053. break; \
  1054. case SUBST_SND_ALL: \
  1055. if (send_sock){ \
  1056. /* address */ \
  1057. if ((send_sock->address.af!=AF_INET) && \
  1058. (send_address_str==&(send_sock->address_str))){\
  1059. new_buf[offset]='['; offset++; \
  1060. }\
  1061. memcpy(new_buf+offset, send_address_str->s, \
  1062. send_address_str->len); \
  1063. offset+=send_address_str->len; \
  1064. if ((send_sock->address.af!=AF_INET) && \
  1065. (send_address_str==&(send_sock->address_str))){\
  1066. new_buf[offset]=']'; offset++; \
  1067. }\
  1068. /* :port */ \
  1069. if ((send_sock->port_no!=SIP_PORT) || \
  1070. (send_port_str!=&(send_sock->port_no_str))){ \
  1071. new_buf[offset]=':'; offset++; \
  1072. memcpy(new_buf+offset, send_port_str->s, \
  1073. send_port_str->len); \
  1074. offset+=send_port_str->len; \
  1075. }\
  1076. switch(send_sock->proto){ \
  1077. case PROTO_NONE: \
  1078. case PROTO_UDP: \
  1079. break; /* nothing to do, udp is default*/ \
  1080. case PROTO_TCP: \
  1081. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  1082. TRANSPORT_PARAM_LEN); \
  1083. offset+=TRANSPORT_PARAM_LEN; \
  1084. if (send_info->proto == PROTO_WS) { \
  1085. memcpy(new_buf+offset, "ws", 2); \
  1086. offset+=2; \
  1087. } else { \
  1088. memcpy(new_buf+offset, "tcp", 3); \
  1089. offset+=3; \
  1090. } \
  1091. break; \
  1092. case PROTO_TLS: \
  1093. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  1094. TRANSPORT_PARAM_LEN); \
  1095. offset+=TRANSPORT_PARAM_LEN; \
  1096. if (send_info->proto == PROTO_WS || send_info->proto == PROTO_WSS) { \
  1097. memcpy(new_buf+offset, "ws", 2); \
  1098. offset+=2; \
  1099. } else { \
  1100. memcpy(new_buf+offset, "tls", 3); \
  1101. offset+=3; \
  1102. } \
  1103. break; \
  1104. case PROTO_SCTP: \
  1105. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  1106. TRANSPORT_PARAM_LEN); \
  1107. offset+=TRANSPORT_PARAM_LEN; \
  1108. memcpy(new_buf+offset, "sctp", 4); \
  1109. offset+=4; \
  1110. break; \
  1111. default: \
  1112. LM_CRIT("unknown proto %d\n", send_sock->proto); \
  1113. } \
  1114. SENDCOMP_PARAM_ADD \
  1115. }else{ \
  1116. /*FIXME*/ \
  1117. LM_CRIT("FIXME: null bind_address\n"); \
  1118. }; \
  1119. break; \
  1120. case SUBST_RCV_PROTO: \
  1121. if (msg->rcv.bind_address){ \
  1122. switch(msg->rcv.bind_address->proto){ \
  1123. case PROTO_NONE: \
  1124. case PROTO_UDP: \
  1125. memcpy(new_buf+offset, "udp", 3); \
  1126. offset+=3; \
  1127. break; \
  1128. case PROTO_TCP: \
  1129. if (msg->rcv.proto == PROTO_WS) { \
  1130. memcpy(new_buf+offset, "ws", 2); \
  1131. offset+=2; \
  1132. } else { \
  1133. memcpy(new_buf+offset, "tcp", 3); \
  1134. offset+=3; \
  1135. } \
  1136. break; \
  1137. case PROTO_TLS: \
  1138. if (msg->rcv.proto == PROTO_WS || msg->rcv.proto == PROTO_WSS) { \
  1139. memcpy(new_buf+offset, "ws", 2); \
  1140. offset+=2; \
  1141. } else { \
  1142. memcpy(new_buf+offset, "tls", 3); \
  1143. offset+=3; \
  1144. } \
  1145. break; \
  1146. case PROTO_SCTP: \
  1147. memcpy(new_buf+offset, "sctp", 4); \
  1148. offset+=4; \
  1149. break; \
  1150. default: \
  1151. LM_CRIT("unknown proto %d\n", msg->rcv.bind_address->proto); \
  1152. } \
  1153. }else{ \
  1154. /*FIXME*/ \
  1155. LM_CRIT("FIXME: null send_sock \n"); \
  1156. }; \
  1157. break; \
  1158. case SUBST_SND_PROTO: \
  1159. if (send_sock){ \
  1160. switch(send_sock->proto){ \
  1161. case PROTO_NONE: \
  1162. case PROTO_UDP: \
  1163. memcpy(new_buf+offset, "udp", 3); \
  1164. offset+=3; \
  1165. break; \
  1166. case PROTO_TCP: \
  1167. if (send_info->proto == PROTO_WS) { \
  1168. memcpy(new_buf+offset, "ws", 2); \
  1169. offset+=2; \
  1170. } else { \
  1171. memcpy(new_buf+offset, "tcp", 3); \
  1172. offset+=3; \
  1173. } \
  1174. break; \
  1175. case PROTO_TLS: \
  1176. if (send_info->proto == PROTO_WS || send_info->proto == PROTO_WSS) { \
  1177. memcpy(new_buf+offset, "ws", 2); \
  1178. offset+=2; \
  1179. } else { \
  1180. memcpy(new_buf+offset, "tls", 3); \
  1181. offset+=3; \
  1182. } \
  1183. break; \
  1184. case PROTO_SCTP: \
  1185. memcpy(new_buf+offset, "sctp", 4); \
  1186. offset+=4; \
  1187. break; \
  1188. default: \
  1189. LM_CRIT("unknown proto %d\n", send_sock->proto); \
  1190. } \
  1191. }else{ \
  1192. /*FIXME*/ \
  1193. LM_CRIT("FIXME: null send_sock \n"); \
  1194. }; \
  1195. break; \
  1196. default: \
  1197. LM_CRIT("unknown subst type %d\n", (subst_l)->u.subst); \
  1198. }
  1199. if (send_info){
  1200. send_sock=send_info->send_sock;
  1201. }else{
  1202. send_sock=0;
  1203. }
  1204. /* init send_address_str & send_port_str */
  1205. if (msg->set_global_address.len)
  1206. send_address_str=&(msg->set_global_address);
  1207. else
  1208. send_address_str=&(send_sock->address_str);
  1209. if (msg->set_global_port.len)
  1210. send_port_str=&(msg->set_global_port);
  1211. else
  1212. send_port_str=&(send_sock->port_no_str);
  1213. /* init send_address_str & send_port_str */
  1214. if(send_sock && send_sock->useinfo.name.len>0)
  1215. send_address_str=&(send_sock->useinfo.name);
  1216. else if (msg->set_global_address.len)
  1217. send_address_str=&(msg->set_global_address);
  1218. else
  1219. send_address_str=&(send_sock->address_str);
  1220. if(send_sock && send_sock->useinfo.port_no>0)
  1221. send_port_str=&(send_sock->useinfo.port_no_str);
  1222. else if (msg->set_global_port.len)
  1223. send_port_str=&(msg->set_global_port);
  1224. else
  1225. send_port_str=&(send_sock->port_no_str);
  1226. /* init recv_address_str, recv_port_str & recv_port_no */
  1227. if(msg->rcv.bind_address) {
  1228. if(msg->rcv.bind_address->useinfo.name.len>0)
  1229. recv_address_str=&(msg->rcv.bind_address->useinfo.name);
  1230. else
  1231. recv_address_str=&(msg->rcv.bind_address->address_str);
  1232. if(msg->rcv.bind_address->useinfo.port_no>0) {
  1233. recv_port_str=&(msg->rcv.bind_address->useinfo.port_no_str);
  1234. recv_port_no = msg->rcv.bind_address->useinfo.port_no;
  1235. } else {
  1236. recv_port_str=&(msg->rcv.bind_address->port_no_str);
  1237. recv_port_no = msg->rcv.bind_address->port_no;
  1238. }
  1239. }
  1240. orig=msg->buf;
  1241. offset=*new_buf_offs;
  1242. s_offset=*orig_offs;
  1243. for (t=lumps;t;t=t->next){
  1244. switch(t->op){
  1245. case LUMP_ADD:
  1246. case LUMP_ADD_SUBST:
  1247. case LUMP_ADD_OPT:
  1248. /* skip if this is an OPT lump and the condition is
  1249. * not satisfied */
  1250. if ((t->op==LUMP_ADD_OPT) &&
  1251. (!lump_check_opt(t, msg, send_info)))
  1252. continue;
  1253. /* just add it here! */
  1254. /* process before */
  1255. for(r=t->before;r;r=r->before){
  1256. switch (r->op){
  1257. case LUMP_ADD:
  1258. /*just add it here*/
  1259. memcpy(new_buf+offset, r->u.value, r->len);
  1260. offset+=r->len;
  1261. break;
  1262. case LUMP_ADD_SUBST:
  1263. SUBST_LUMP(r);
  1264. break;
  1265. case LUMP_ADD_OPT:
  1266. /* skip if this is an OPT lump and the condition is
  1267. * not satisfied */
  1268. if (!lump_check_opt(r, msg, send_info))
  1269. goto skip_before;
  1270. break;
  1271. default:
  1272. /* only ADD allowed for before/after */
  1273. LM_CRIT("invalid op for data lump (%x)\n", r->op);
  1274. }
  1275. }
  1276. skip_before:
  1277. /* copy "main" part */
  1278. switch(t->op){
  1279. case LUMP_ADD:
  1280. memcpy(new_buf+offset, t->u.value, t->len);
  1281. offset+=t->len;
  1282. break;
  1283. case LUMP_ADD_SUBST:
  1284. SUBST_LUMP(t);
  1285. break;
  1286. case LUMP_ADD_OPT:
  1287. /* do nothing, it's only a condition */
  1288. break;
  1289. default:
  1290. /* should not ever get here */
  1291. LM_CRIT("unhandled data lump op %d\n", t->op);
  1292. }
  1293. /* process after */
  1294. for(r=t->after;r;r=r->after){
  1295. switch (r->op){
  1296. case LUMP_ADD:
  1297. /*just add it here*/
  1298. memcpy(new_buf+offset, r->u.value, r->len);
  1299. offset+=r->len;
  1300. break;
  1301. case LUMP_ADD_SUBST:
  1302. SUBST_LUMP(r);
  1303. break;
  1304. case LUMP_ADD_OPT:
  1305. /* skip if this is an OPT lump and the condition is
  1306. * not satisfied */
  1307. if (!lump_check_opt(r, msg, send_info))
  1308. goto skip_after;
  1309. break;
  1310. default:
  1311. /* only ADD allowed for before/after */
  1312. LM_CRIT("invalid op for data lump (%x)\n", r->op);
  1313. }
  1314. }
  1315. skip_after:
  1316. break;
  1317. case LUMP_NOP:
  1318. case LUMP_DEL:
  1319. /* copy till offset */
  1320. if (s_offset>t->u.offset){
  1321. DBG("Warning: (%d) overlapped lumps offsets,"
  1322. " ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);
  1323. /* this should've been fixed above (when computing len) */
  1324. /* just ignore it*/
  1325. break;
  1326. }
  1327. size=t->u.offset-s_offset;
  1328. if (size){
  1329. memcpy(new_buf+offset, orig+s_offset,size);
  1330. offset+=size;
  1331. s_offset+=size;
  1332. }
  1333. /* process before */
  1334. for(r=t->before;r;r=r->before){
  1335. switch (r->op){
  1336. case LUMP_ADD:
  1337. /*just add it here*/
  1338. memcpy(new_buf+offset, r->u.value, r->len);
  1339. offset+=r->len;
  1340. break;
  1341. case LUMP_ADD_SUBST:
  1342. SUBST_LUMP(r);
  1343. break;
  1344. case LUMP_ADD_OPT:
  1345. /* skip if this is an OPT lump and the condition is
  1346. * not satisfied */
  1347. if (!lump_check_opt(r, msg, send_info))
  1348. goto skip_nop_before;
  1349. break;
  1350. default:
  1351. /* only ADD allowed for before/after */
  1352. LM_CRIT("invalid op for data lump (%x)\n",r->op);
  1353. }
  1354. }
  1355. skip_nop_before:
  1356. /* process main (del only) */
  1357. if (t->op==LUMP_DEL){
  1358. /* skip len bytes from orig msg */
  1359. s_offset+=t->len;
  1360. }
  1361. /* process after */
  1362. for(r=t->after;r;r=r->after){
  1363. switch (r->op){
  1364. case LUMP_ADD:
  1365. /*just add it here*/
  1366. memcpy(new_buf+offset, r->u.value, r->len);
  1367. offset+=r->len;
  1368. break;
  1369. case LUMP_ADD_SUBST:
  1370. SUBST_LUMP(r);
  1371. break;
  1372. case LUMP_ADD_OPT:
  1373. /* skip if this is an OPT lump and the condition is
  1374. * not satisfied */
  1375. if (!lump_check_opt(r, msg, send_info))
  1376. goto skip_nop_after;
  1377. break;
  1378. default:
  1379. /* only ADD allowed for before/after */
  1380. LM_CRIT("invalid op for data lump (%x)\n", r->op);
  1381. }
  1382. }
  1383. skip_nop_after:
  1384. break;
  1385. default:
  1386. LM_CRIT("unknown op (%x)\n", t->op);
  1387. }
  1388. }
  1389. *new_buf_offs=offset;
  1390. *orig_offs=s_offset;
  1391. #undef RCVCOMP_PARAM_ADD
  1392. #undef SENDCOMP_PARAM_ADD
  1393. }
  1394. /*
  1395. * Adjust/insert Content-Length if necessary
  1396. */
  1397. static inline int adjust_clen(struct sip_msg* msg, int body_delta, int proto)
  1398. {
  1399. struct lump* anchor;
  1400. char* clen_buf;
  1401. int clen_len, body_only;
  1402. #ifdef USE_TCP
  1403. char* body;
  1404. int comp_clen;
  1405. #endif /* USE_TCP */
  1406. /* Calculate message length difference caused by lumps modifying message
  1407. * body, from this point on the message body must not be modified. Zero
  1408. * value indicates that the body hasn't been modified
  1409. */
  1410. clen_buf = 0;
  1411. anchor=0;
  1412. body_only=1;
  1413. /* check to see if we need to add clen */
  1414. #ifdef USE_TCP
  1415. if (proto == PROTO_TCP
  1416. #ifdef USE_TLS
  1417. || proto == PROTO_TLS
  1418. #endif
  1419. ) {
  1420. if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0)==-1){
  1421. LM_ERR("error parsing content-length\n");
  1422. goto error;
  1423. }
  1424. if (unlikely(msg->content_length==0)){
  1425. /* not present, we need to add it */
  1426. /* msg->unparsed should point just before the final crlf
  1427. * - whole message was parsed by the above parse_headers
  1428. * which did not find content-length */
  1429. anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
  1430. HDR_CONTENTLENGTH_T);
  1431. if (anchor==0){
  1432. LM_ERR("cannot set clen anchor\n");
  1433. goto error;
  1434. }
  1435. body_only=0;
  1436. }else{
  1437. /* compute current content length and compare it with the
  1438. one in the message */
  1439. body=get_body(msg);
  1440. if (unlikely(body==0)){
  1441. ser_error=E_BAD_REQ;
  1442. LM_ERR("no message body found (missing crlf?)");
  1443. goto error;
  1444. }
  1445. comp_clen=msg->len-(int)(body-msg->buf)+body_delta;
  1446. if (comp_clen!=(int)(long)msg->content_length->parsed){
  1447. /* note: we don't distinguish here between received with
  1448. wrong content-length and content-length changed, we just
  1449. fix it automatically in both cases (the reason being
  1450. that an error message telling we have received a msg-
  1451. with wrong content-length is of very little use) */
  1452. anchor = del_lump(msg, msg->content_length->body.s-msg->buf,
  1453. msg->content_length->body.len,
  1454. HDR_CONTENTLENGTH_T);
  1455. if (anchor==0) {
  1456. LM_ERR("Can't remove original Content-Length\n");
  1457. goto error;
  1458. }
  1459. body_only=1;
  1460. }
  1461. }
  1462. }else
  1463. #endif /* USE_TCP */
  1464. if (body_delta){
  1465. if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1) {
  1466. LM_ERR("Error parsing Content-Length\n");
  1467. goto error;
  1468. }
  1469. /* The body has been changed, try to find
  1470. * existing Content-Length
  1471. */
  1472. /* no need for Content-Length if it's and UDP packet and
  1473. * it hasn't Content-Length already */
  1474. if (msg->content_length==0){
  1475. /* content-length doesn't exist, append it */
  1476. /* msg->unparsed should point just before the final crlf
  1477. * - whole message was parsed by the above parse_headers
  1478. * which did not find content-length */
  1479. if (proto!=PROTO_UDP){
  1480. anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
  1481. HDR_CONTENTLENGTH_T);
  1482. if (anchor==0){
  1483. LM_ERR("cannot set clen anchor\n");
  1484. goto error;
  1485. }
  1486. body_only=0;
  1487. } /* else
  1488. DBG("adjust_clen: UDP packet with no clen => "
  1489. "not adding one \n"); */
  1490. }else{
  1491. /* Content-Length has been found, remove it */
  1492. anchor = del_lump( msg, msg->content_length->body.s - msg->buf,
  1493. msg->content_length->body.len,
  1494. HDR_CONTENTLENGTH_T);
  1495. if (anchor==0) {
  1496. LM_ERR("Can't remove original Content-Length\n");
  1497. goto error;
  1498. }
  1499. }
  1500. }
  1501. if (anchor){
  1502. clen_buf = clen_builder(msg, &clen_len, body_delta, body_only);
  1503. if (!clen_buf) goto error;
  1504. if (insert_new_lump_after(anchor, clen_buf, clen_len,
  1505. HDR_CONTENTLENGTH_T) == 0)
  1506. goto error;
  1507. }
  1508. return 0;
  1509. error:
  1510. if (clen_buf) pkg_free(clen_buf);
  1511. return -1;
  1512. }
  1513. static inline int find_line_start(char *text, unsigned int text_len,
  1514. char **buf, unsigned int *buf_len)
  1515. {
  1516. char *ch, *start;
  1517. unsigned int len;
  1518. start = *buf;
  1519. len = *buf_len;
  1520. while (text_len <= len) {
  1521. if (strncmp(text, start, text_len) == 0) {
  1522. *buf = start;
  1523. *buf_len = len;
  1524. return 1;
  1525. }
  1526. if ((ch = memchr(start, 13, len - 1))) {
  1527. if (*(ch + 1) != 10) {
  1528. LM_ERR("No LF after CR\n");
  1529. return 0;
  1530. }
  1531. len = len - (ch - start + 2);
  1532. start = ch + 2;
  1533. } else {
  1534. LM_ERR("No CRLF found\n");
  1535. return 0;
  1536. }
  1537. }
  1538. return 0;
  1539. }
  1540. static inline int get_line(str s)
  1541. {
  1542. char *ch;
  1543. if ((ch = memchr(s.s, 13, s.len))) {
  1544. if (*(ch + 1) != 10) {
  1545. LM_ERR("No LF after CR\n");
  1546. return 0;
  1547. }
  1548. return ch - s.s + 2;
  1549. } else {
  1550. LM_ERR("No CRLF found\n");
  1551. return s.len;
  1552. }
  1553. return 0;
  1554. }
  1555. int replace_body(struct sip_msg *msg, str txt)
  1556. {
  1557. struct lump *anchor;
  1558. char *buf;
  1559. str body = {0,0};
  1560. body.s = get_body(msg);
  1561. if(body.s==0)
  1562. {
  1563. LM_ERR("malformed sip message\n");
  1564. return 0;
  1565. }
  1566. body.len = msg->len -(int)(body.s-msg->buf);
  1567. LM_DBG("old size body[%d] actual[%d]\n", body.len, txt.len);
  1568. if(body.s+body.len>msg->buf+msg->len)
  1569. {
  1570. LM_ERR("invalid content length: %d\n", body.len);
  1571. return 0;
  1572. }
  1573. del_nonshm_lump( &(msg->body_lumps) );
  1574. msg->body_lumps = NULL;
  1575. if(del_lump(msg, body.s-msg->buf, body.len, 0) == 0)
  1576. {
  1577. LM_ERR("cannot delete existing body");
  1578. return 0;
  1579. }
  1580. anchor = anchor_lump(msg, body.s - msg->buf, 0, 0);
  1581. if(anchor==0)
  1582. {
  1583. LM_ERR("failed to get anchor\n");
  1584. return 0;
  1585. }
  1586. buf=pkg_malloc(sizeof(char)*txt.len);
  1587. if(buf==0)
  1588. {
  1589. PKG_MEM_ERROR;
  1590. return 0;
  1591. }
  1592. memcpy(buf, txt.s, txt.len);
  1593. if(insert_new_lump_after(anchor, buf, txt.len, 0)==0)
  1594. {
  1595. LM_ERR("failed to insert body lump\n");
  1596. pkg_free(buf);
  1597. return 0;
  1598. }
  1599. return 1;
  1600. }
  1601. /**
  1602. * returns the boundary defined by the Content-Type
  1603. * header
  1604. */
  1605. int get_boundary(struct sip_msg* msg, str* boundary)
  1606. {
  1607. str params;
  1608. param_t *p, *list;
  1609. param_hooks_t hooks;
  1610. params.s = memchr(msg->content_type->body.s, ';',
  1611. msg->content_type->body.len);
  1612. if (params.s == NULL)
  1613. {
  1614. LM_ERR("Content-Type hdr has no params\n");
  1615. return -1;
  1616. }
  1617. params.len = msg->content_type->body.len -
  1618. (params.s - msg->content_type->body.s);
  1619. if (parse_params(&params, CLASS_ANY, &hooks, &list) < 0)
  1620. {
  1621. LM_ERR("while parsing Content-Type params\n");
  1622. return -1;
  1623. }
  1624. boundary->s = NULL;
  1625. boundary->len = 0;
  1626. for (p = list; p; p = p->next) {
  1627. if ((p->name.len == 8)
  1628. && (strncasecmp(p->name.s, "boundary", 8) == 0))
  1629. {
  1630. boundary->s = pkg_malloc(p->body.len + 2);
  1631. if (boundary->s == NULL)
  1632. {
  1633. free_params(list);
  1634. LM_ERR("no memory for boundary string\n");
  1635. return -1;
  1636. }
  1637. *(boundary->s) = '-';
  1638. *(boundary->s + 1) = '-';
  1639. memcpy(boundary->s + 2, p->body.s, p->body.len);
  1640. boundary->len = 2 + p->body.len;
  1641. LM_DBG("boundary is <%.*s>\n", boundary->len, boundary->s);
  1642. break;
  1643. }
  1644. }
  1645. free_params(list);
  1646. return 0;
  1647. }
  1648. int check_boundaries(struct sip_msg *msg, struct dest_info *send_info)
  1649. {
  1650. str b = {0,0};
  1651. str fb = {0,0};
  1652. str ob = {0,0};
  1653. str bsuffix = {"\r\n", 2};
  1654. str fsuffix = {"--\r\n", 4};
  1655. str body = {0,0};
  1656. str buf = {0,0};
  1657. str tmp = {0,0};
  1658. struct str_list* lb = NULL;
  1659. struct str_list* lb_t = NULL;
  1660. int lb_found = 0;
  1661. int t, ret, lb_size;
  1662. char *pb;
  1663. if(!(msg->msg_flags&FL_BODY_MULTIPART)) return 0;
  1664. else
  1665. {
  1666. buf.s = build_body(msg, (unsigned int *)&buf.len, &ret, send_info);
  1667. if(ret) {
  1668. LM_ERR("Can't get body\n");
  1669. return -1;
  1670. }
  1671. tmp.s = buf.s;
  1672. t = tmp.len = buf.len;
  1673. if(get_boundary(msg, &ob)!=0) return -1;
  1674. if(str_append(&ob, &bsuffix, &b)!=0) {
  1675. LM_ERR("Can't append suffix to boundary\n");
  1676. goto error;
  1677. }
  1678. if(str_append(&ob, &fsuffix,&fb)!=0) {
  1679. LM_ERR("Can't append suffix to final boundary\n");
  1680. goto error;
  1681. }
  1682. ret = b.len-2;
  1683. while(t>0)
  1684. {
  1685. if(find_line_start(b.s, ret, &tmp.s,
  1686. (unsigned int *)&tmp.len))
  1687. {
  1688. /*LM_DBG("found t[%d] tmp.len[%d]:[%.*s]\n",
  1689. t, tmp.len, tmp.len, tmp.s);*/
  1690. if(!lb)
  1691. {
  1692. lb = pkg_malloc(sizeof(struct str_list));
  1693. if (!lb) {
  1694. PKG_MEM_ERROR;
  1695. goto error;
  1696. }
  1697. lb->s.s = tmp.s;
  1698. lb->s.len = tmp.len;
  1699. lb->next = 0;
  1700. lb_t = lb;
  1701. }
  1702. else
  1703. {
  1704. lb_t = append_str_list(tmp.s, tmp.len, &lb_t, &lb_size);
  1705. }
  1706. lb_found = lb_found + 1;
  1707. tmp.s = tmp.s + ret;
  1708. t = t - ret;
  1709. tmp.len = tmp.len - ret;
  1710. }
  1711. else { t=0; }
  1712. }
  1713. if(lb_found<2)
  1714. {
  1715. LM_ERR("found[%d] wrong number of boundaries\n", lb_found);
  1716. goto error;
  1717. }
  1718. /* adding 2 chars in advance */
  1719. body.len = buf.len + 2;
  1720. body.s = pkg_malloc(sizeof(char)*body.len);
  1721. if (!body.s) {
  1722. PKG_MEM_ERROR;
  1723. goto error;
  1724. }
  1725. pb = body.s; body.len = 0;
  1726. lb_t = lb;
  1727. while(lb_t)
  1728. {
  1729. tmp.s = lb_t->s.s; tmp.len = lb_t->s.len;
  1730. tmp.len = get_line(lb_t->s);
  1731. if(tmp.len!=b.len || strncmp(b.s, tmp.s, b.len)!=0)
  1732. {
  1733. LM_DBG("malformed bondary in the middle\n");
  1734. memcpy(pb, b.s, b.len); body.len = body.len + b.len;
  1735. pb = pb + b.len;
  1736. t = lb_t->s.s - (lb_t->s.s + tmp.len);
  1737. memcpy(pb, lb_t->s.s+tmp.len, t); pb = pb + t;
  1738. /*LM_DBG("new chunk[%d][%.*s]\n", t, t, pb-t);*/
  1739. }
  1740. else {
  1741. t = lb_t->next->s.s - lb_t->s.s;
  1742. memcpy(pb, lb_t->s.s, t);
  1743. /*LM_DBG("copy[%d][%.*s]\n", t, t, pb);*/
  1744. pb = pb + t;
  1745. }
  1746. body.len = body.len + t;
  1747. /*LM_DBG("body[%d][%.*s]\n", body.len, body.len, body.s);*/
  1748. lb_t = lb_t->next;
  1749. if(!lb_t->next) lb_t = NULL;
  1750. }
  1751. /* last boundary */
  1752. tmp.s = lb->s.s; tmp.len = lb->s.len;
  1753. tmp.len = get_line(lb->s);
  1754. if(tmp.len!=fb.len || strncmp(fb.s, tmp.s, fb.len)!=0)
  1755. {
  1756. LM_DBG("last bondary without -- at the end\n");
  1757. memcpy(pb, fb.s, fb.len);
  1758. /*LM_DBG("new chunk[%d][%.*s]\n", fb.len, fb.len, pb);*/
  1759. pb = pb + fb.len;
  1760. body.len = body.len + fb.len;
  1761. }
  1762. else {
  1763. memcpy(pb, lb->s.s, lb->s.len); pb = pb + lb->s.len;
  1764. body.len = body.len + lb->s.len;
  1765. /*LM_DBG("copy[%d][%.*s]\n", lb->s.len, lb->s.len, pb - lb->s.len);*/
  1766. }
  1767. /*LM_DBG("body[%d][%.*s] expected[%ld]\n",
  1768. body.len, body.len, body.s, pb-body.s); */
  1769. if(!replace_body(msg, body))
  1770. {
  1771. LM_ERR("Can't replace body\n");
  1772. goto error;
  1773. }
  1774. msg->msg_flags &= ~FL_BODY_MULTIPART;
  1775. ret = 1;
  1776. goto clean;
  1777. }
  1778. error:
  1779. ret = -1;
  1780. clean:
  1781. if(ob.s) pkg_free(ob.s);
  1782. if(b.s) pkg_free(b.s);
  1783. if(fb.s) pkg_free(fb.s);
  1784. if(body.s) pkg_free(body.s);
  1785. if(buf.s) pkg_free(buf.s);
  1786. while(lb)
  1787. {
  1788. lb_t = lb->next;
  1789. pkg_free(lb);
  1790. lb = lb_t;
  1791. }
  1792. return ret;
  1793. }
  1794. /** builds a request in memory from another sip request.
  1795. *
  1796. * Side-effects: - it adds lumps to the msg which are _not_ cleaned.
  1797. * The added lumps are HDR_VIA_T (almost always added), HDR_CONTENLENGTH_T
  1798. * and HDR_ROUTE_T (when a Route: header is added as a result of a non-null
  1799. * msg->path_vec).
  1800. * - it might change send_info->proto and send_info->send_socket
  1801. * if proto fallback is enabled (see below).
  1802. *
  1803. * Uses also global_req_flags ( OR'ed with msg->msg_flags, see send_info
  1804. * below).
  1805. *
  1806. * @param msg - sip message structure, complete with lumps
  1807. * @param returned_len - result length (filled in)
  1808. * @param send_info - dest_info structure (value/result), contains where the
  1809. * packet will be sent to (it's needed for building a
  1810. * correct via, fill RR lumps a.s.o.). If MTU based
  1811. * protocol fall-back is enabled (see flags below),
  1812. * send_info->proto might be updated with the new
  1813. * protocol.
  1814. * msg->msg_flags used:
  1815. * - FL_TCP_MTU_FB, FL_TLS_MTU_FB and FL_SCTP_MTU_FB -
  1816. * fallback to the corresp. proto if the built
  1817. * message > mtu and send_info->proto==PROTO_UDP.
  1818. * It will also update send_info->proto.
  1819. * - FL_FORCE_RPORT: add rport to via
  1820. * @param mode - flags for building the message, can be a combination of:
  1821. * * BUILD_NO_LOCAL_VIA - don't add a local via
  1822. * * BUILD_NO_VIA1_UPDATE - don't update first via (rport,
  1823. * received a.s.o)
  1824. * * BUILD_NO_PATH - don't add a Route: header with the
  1825. * msg->path_vec content.
  1826. * * BUILD_IN_SHM - build the result in shm memory
  1827. *
  1828. * @return pointer to the new request (pkg_malloc'ed or shm_malloc'ed,
  1829. * depending on the presence of the BUILD_IN_SHM flag, needs freeing when
  1830. * done) and sets returned_len or 0 on error.
  1831. */
  1832. char * build_req_buf_from_sip_req( struct sip_msg* msg,
  1833. unsigned int *returned_len,
  1834. struct dest_info* send_info,
  1835. unsigned int mode)
  1836. {
  1837. unsigned int len, new_len, received_len, rport_len, uri_len, via_len,
  1838. body_delta;
  1839. char* line_buf;
  1840. char* received_buf;
  1841. char* rport_buf;
  1842. char* new_buf;
  1843. char* buf;
  1844. str path_buf;
  1845. unsigned int offset, s_offset, size;
  1846. struct lump* via_anchor;
  1847. struct lump* via_lump;
  1848. struct lump* via_insert_param;
  1849. struct lump* path_anchor;
  1850. struct lump* path_lump;
  1851. str branch;
  1852. unsigned int flags;
  1853. unsigned int udp_mtu;
  1854. struct dest_info di;
  1855. via_insert_param=0;
  1856. uri_len=0;
  1857. buf=msg->buf;
  1858. len=msg->len;
  1859. received_len=0;
  1860. rport_len=0;
  1861. new_buf=0;
  1862. received_buf=0;
  1863. rport_buf=0;
  1864. via_anchor=0;
  1865. line_buf=0;
  1866. via_len=0;
  1867. path_buf.s=0;
  1868. path_buf.len=0;
  1869. flags=msg->msg_flags|global_req_flags;
  1870. if(check_boundaries(msg, send_info)<0){
  1871. LM_WARN("check_boundaries error\n");
  1872. }
  1873. /* Calculate message body difference and adjust Content-Length */
  1874. body_delta = lumps_len(msg, msg->body_lumps, send_info);
  1875. if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
  1876. LM_ERR("Error while adjusting Content-Length\n");
  1877. goto error00;
  1878. }
  1879. if(unlikely(mode&BUILD_NO_LOCAL_VIA))
  1880. goto after_local_via;
  1881. /* create the via header */
  1882. branch.s=msg->add_to_branch_s;
  1883. branch.len=msg->add_to_branch_len;
  1884. via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
  1885. if (unlikely(via_anchor==0)) goto error00;
  1886. line_buf = create_via_hf( &via_len, msg, send_info, &branch);
  1887. if (unlikely(!line_buf)){
  1888. LM_ERR("could not create Via header\n");
  1889. goto error00;
  1890. }
  1891. after_local_via:
  1892. if(unlikely(mode&BUILD_NO_VIA1_UPDATE))
  1893. goto after_update_via1;
  1894. /* check if received needs to be added */
  1895. if ( received_test(msg) ) {
  1896. if ((received_buf=received_builder(msg,&received_len))==0){
  1897. LM_ERR("received_builder failed\n");
  1898. goto error01; /* free also line_buf */
  1899. }
  1900. }
  1901. /* check if rport needs to be updated:
  1902. * - if FL_FORCE_RPORT is set add it (and del. any previous version)
  1903. * - if via already contains an rport add it and overwrite the previous
  1904. * rport value if present (if you don't want to overwrite the previous
  1905. * version remove the comments) */
  1906. if ((flags&FL_FORCE_RPORT)||
  1907. (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
  1908. if ((rport_buf=rport_builder(msg, &rport_len))==0){
  1909. LM_ERR("rport_builder failed\n");
  1910. goto error01; /* free everything */
  1911. }
  1912. }
  1913. /* find out where the offset of the first parameter that should be added
  1914. * (after host:port), needed by add receive & maybe rport */
  1915. if (msg->via1->params.s){
  1916. size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
  1917. for ';' */
  1918. }else{
  1919. size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
  1920. if (msg->via1->port!=0){
  1921. /*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
  1922. size += msg->via1->port_str.len + 1; /* +1 for ':'*/
  1923. }
  1924. #if 0
  1925. /* no longer necessary, now hots.s contains [] */
  1926. if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
  1927. #endif
  1928. }
  1929. /* if received needs to be added, add anchor after host and add it, or
  1930. * overwrite the previous one if already present */
  1931. if (received_len){
  1932. if (msg->via1->received){ /* received already present => overwrite it*/
  1933. via_insert_param=del_lump(msg,
  1934. msg->via1->received->start-buf-1, /*;*/
  1935. msg->via1->received->size+1, /*;*/ HDR_VIA_T);
  1936. }else if (via_insert_param==0){ /* receive not present, ok */
  1937. via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
  1938. HDR_VIA_T);
  1939. }
  1940. if (via_insert_param==0) goto error02; /* free received_buf */
  1941. if (insert_new_lump_after(via_insert_param, received_buf, received_len,
  1942. HDR_VIA_T) ==0 ) goto error02; /* free received_buf */
  1943. }
  1944. /* if rport needs to be updated, delete it if present and add it's value */
  1945. if (rport_len){
  1946. if (msg->via1->rport){ /* rport already present */
  1947. via_insert_param=del_lump(msg,
  1948. msg->via1->rport->start-buf-1, /*';'*/
  1949. msg->via1->rport->size+1 /* ; */, HDR_VIA_T);
  1950. }else if (via_insert_param==0){ /*force rport, no rport present */
  1951. /* no rport, add it */
  1952. via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
  1953. HDR_VIA_T);
  1954. }
  1955. if (via_insert_param==0) goto error03; /* free rport_buf */
  1956. if (insert_new_lump_after(via_insert_param, rport_buf, rport_len,
  1957. HDR_VIA_T) ==0 )
  1958. goto error03; /* free rport_buf */
  1959. }
  1960. after_update_via1:
  1961. /* add route with path content */
  1962. if(unlikely(!(mode&BUILD_NO_PATH) && msg->path_vec.s &&
  1963. msg->path_vec.len)){
  1964. path_buf.len=ROUTE_PREFIX_LEN+msg->path_vec.len+CRLF_LEN;
  1965. path_buf.s=pkg_malloc(path_buf.len+1);
  1966. if (unlikely(path_buf.s==0)){
  1967. LM_ERR("out of memory\n");
  1968. ser_error=E_OUT_OF_MEM;
  1969. goto error00;
  1970. }
  1971. memcpy(path_buf.s, ROUTE_PREFIX, ROUTE_PREFIX_LEN);
  1972. memcpy(path_buf.s+ROUTE_PREFIX_LEN, msg->path_vec.s,
  1973. msg->path_vec.len);
  1974. memcpy(path_buf.s+ROUTE_PREFIX_LEN+msg->path_vec.len, CRLF, CRLF_LEN);
  1975. path_buf.s[path_buf.len]=0;
  1976. /* insert Route header either before the other routes
  1977. (if present & parsed), after the local via or after in front of
  1978. the first via if we don't add a local via*/
  1979. if (msg->route){
  1980. path_anchor=anchor_lump(msg, msg->route->name.s-buf, 0,
  1981. HDR_ROUTE_T);
  1982. }else if (likely(via_anchor)){
  1983. path_anchor=via_anchor;
  1984. }else if (likely(msg->via1)){
  1985. path_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0,
  1986. HDR_ROUTE_T);
  1987. }else{
  1988. /* if no via1 (theoretically possible for non-sip messages,
  1989. e.g. http xmlrpc) */
  1990. path_anchor=anchor_lump(msg, msg->headers->name.s-buf, 0,
  1991. HDR_ROUTE_T);
  1992. }
  1993. if (unlikely(path_anchor==0))
  1994. goto error05;
  1995. if (unlikely((path_lump=insert_new_lump_after(path_anchor, path_buf.s,
  1996. path_buf.len,
  1997. HDR_ROUTE_T))==0))
  1998. goto error05;
  1999. }
  2000. /* compute new msg len and fix overlapping zones*/
  2001. new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len;
  2002. #ifdef XL_DEBUG
  2003. LM_ERR("new_len(%d)=len(%d)+lumps_len\n", new_len, len);
  2004. #endif
  2005. udp_mtu=cfg_get(core, core_cfg, udp_mtu);
  2006. di.proto=PROTO_NONE;
  2007. if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu &&
  2008. (flags & FL_MTU_FB_MASK) && (new_len>udp_mtu)
  2009. && (!(mode&BUILD_NO_LOCAL_VIA)))){
  2010. di=*send_info; /* copy whole struct - will be used in the Via builder */
  2011. di.proto=PROTO_NONE; /* except the proto */
  2012. #ifdef USE_TCP
  2013. if (!tcp_disable && (flags & FL_MTU_TCP_FB) &&
  2014. (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TCP))){
  2015. di.proto=PROTO_TCP;
  2016. }
  2017. #ifdef USE_TLS
  2018. else if (!tls_disable && (flags & FL_MTU_TLS_FB) &&
  2019. (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TLS))){
  2020. di.proto=PROTO_TLS;
  2021. }
  2022. #endif /* USE_TLS */
  2023. #endif /* USE_TCP */
  2024. #ifdef USE_SCTP
  2025. #ifdef USE_TCP
  2026. else
  2027. #endif /* USE_TCP */
  2028. if (!sctp_disable && (flags & FL_MTU_SCTP_FB) &&
  2029. (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_SCTP))){
  2030. di.proto=PROTO_SCTP;
  2031. }
  2032. #endif /* USE_SCTP */
  2033. if (di.proto!=PROTO_NONE){
  2034. new_len-=via_len;
  2035. if(likely(line_buf)) pkg_free(line_buf);
  2036. line_buf = create_via_hf( &via_len, msg, &di, &branch);
  2037. if (!line_buf){
  2038. LM_ERR("memory allocation failure!\n");
  2039. goto error00;
  2040. }
  2041. new_len+=via_len;
  2042. }
  2043. }
  2044. /* add via header to the list */
  2045. /* try to add it before msg. 1st via */
  2046. /* add first via, as an anchor for second via*/
  2047. if(likely(line_buf)) {
  2048. if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
  2049. HDR_VIA_T))==0)
  2050. goto error04;
  2051. }
  2052. if (msg->new_uri.s){
  2053. uri_len=msg->new_uri.len;
  2054. new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
  2055. }
  2056. if(unlikely(mode&BUILD_IN_SHM))
  2057. new_buf=(char*)shm_malloc(new_len+1);
  2058. else
  2059. new_buf=(char*)pkg_malloc(new_len+1);
  2060. if (new_buf==0){
  2061. ser_error=E_OUT_OF_MEM;
  2062. LM_ERR("out of memory\n");
  2063. goto error00;
  2064. }
  2065. offset=s_offset=0;
  2066. if (msg->new_uri.s){
  2067. /* copy message up to uri */
  2068. size=msg->first_line.u.request.uri.s-buf;
  2069. memcpy(new_buf, buf, size);
  2070. offset+=size;
  2071. s_offset+=size;
  2072. /* add our uri */
  2073. memcpy(new_buf+offset, msg->new_uri.s, uri_len);
  2074. offset+=uri_len;
  2075. s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
  2076. }
  2077. new_buf[new_len]=0;
  2078. /* copy msg adding/removing lumps */
  2079. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
  2080. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset,send_info);
  2081. /* copy the rest of the message */
  2082. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  2083. new_buf[new_len]=0;
  2084. /* update the send_info if udp_mtu affected */
  2085. if (di.proto!=PROTO_NONE) {
  2086. send_info->proto=di.proto;
  2087. send_info->send_sock=di.send_sock;
  2088. }
  2089. #ifdef DBG_MSG_QA
  2090. if (new_buf[new_len-1]==0) {
  2091. LM_ERR("0 in the end\n");
  2092. abort();
  2093. }
  2094. #endif
  2095. *returned_len=new_len;
  2096. return new_buf;
  2097. error01:
  2098. error02:
  2099. if (received_buf) pkg_free(received_buf);
  2100. error03:
  2101. if (rport_buf) pkg_free(rport_buf);
  2102. error04:
  2103. if (line_buf) pkg_free(line_buf);
  2104. error05:
  2105. if (path_buf.s) pkg_free(path_buf.s);
  2106. error00:
  2107. *returned_len=0;
  2108. return 0;
  2109. }
  2110. char * generate_res_buf_from_sip_res( struct sip_msg* msg,
  2111. unsigned int *returned_len, unsigned int mode)
  2112. {
  2113. unsigned int new_len, via_len, body_delta;
  2114. char* new_buf;
  2115. unsigned offset, s_offset, via_offset;
  2116. char* buf;
  2117. unsigned int len;
  2118. buf=msg->buf;
  2119. len=msg->len;
  2120. new_buf=0;
  2121. if(unlikely(mode&BUILD_NO_VIA1_UPDATE)) {
  2122. via_len = 0;
  2123. via_offset = 0;
  2124. } else {
  2125. /* we must remove the first via */
  2126. if (msg->via1->next) {
  2127. via_len=msg->via1->bsize;
  2128. via_offset=msg->h_via1->body.s-buf;
  2129. } else {
  2130. via_len=msg->h_via1->len;
  2131. via_offset=msg->h_via1->name.s-buf;
  2132. }
  2133. }
  2134. /* Calculate message body difference and adjust
  2135. * Content-Length
  2136. */
  2137. body_delta = lumps_len(msg, msg->body_lumps, 0);
  2138. if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
  2139. < 0) {
  2140. LM_ERR("error while adjusting Content-Length\n");
  2141. goto error;
  2142. }
  2143. if(likely(!(mode&BUILD_NO_VIA1_UPDATE))) {
  2144. /* remove the first via*/
  2145. if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
  2146. LM_ERR("error trying to remove first via\n");
  2147. goto error;
  2148. }
  2149. }
  2150. new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
  2151. know the send sock */
  2152. DBG(" old size: %d, new size: %d\n", len, new_len);
  2153. new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
  2154. (\0 to print it )*/
  2155. if (new_buf==0){
  2156. LM_ERR("out of mem\n");
  2157. goto error;
  2158. }
  2159. new_buf[new_len]=0; /* debug: print the message */
  2160. offset=s_offset=0;
  2161. /*FIXME: no send sock*/
  2162. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, 0);/*FIXME:*/
  2163. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, 0);
  2164. /* copy the rest of the message */
  2165. memcpy(new_buf+offset,
  2166. buf+s_offset,
  2167. len-s_offset);
  2168. /* send it! */
  2169. DBG("copied size: orig:%d, new: %d, rest: %d"
  2170. " msg=\n%s\n", s_offset, offset, len-s_offset, new_buf);
  2171. *returned_len=new_len;
  2172. return new_buf;
  2173. error:
  2174. *returned_len=0;
  2175. return 0;
  2176. }
  2177. char * build_res_buf_from_sip_res( struct sip_msg* msg,
  2178. unsigned int *returned_len)
  2179. {
  2180. return generate_res_buf_from_sip_res(msg, returned_len, 0);
  2181. }
  2182. char * build_res_buf_from_sip_req( unsigned int code, str *text ,str *new_tag,
  2183. struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
  2184. {
  2185. char *buf, *p;
  2186. unsigned int len,foo;
  2187. struct hdr_field *hdr;
  2188. struct lump_rpl *lump;
  2189. struct lump_rpl *body;
  2190. int i;
  2191. char* received_buf;
  2192. unsigned int received_len;
  2193. char* rport_buf;
  2194. unsigned int rport_len;
  2195. char* warning_buf;
  2196. unsigned int warning_len;
  2197. char* content_len_buf;
  2198. unsigned int content_len_len;
  2199. char *after_body;
  2200. str to_tag;
  2201. char *totags;
  2202. int httpreq;
  2203. char *pvia;
  2204. body = 0;
  2205. buf=0;
  2206. received_buf=rport_buf=warning_buf=content_len_buf=0;
  2207. received_len=rport_len=warning_len=content_len_len=0;
  2208. to_tag.s=0; /* fixes gcc 4.0 warning */
  2209. to_tag.len=0;
  2210. /* force parsing all headers -- we want to return all
  2211. Via's in the reply and they may be scattered down to the
  2212. end of header (non-block Vias are a really poor property
  2213. of SIP :( ) */
  2214. if (parse_headers( msg, HDR_EOH_F, 0 )==-1) {
  2215. LM_ERR("alas, parse_headers failed\n");
  2216. goto error00;
  2217. }
  2218. /*computes the length of the new response buffer*/
  2219. len = 0;
  2220. httpreq = IS_HTTP(msg);
  2221. /* check if received needs to be added */
  2222. if (received_test(msg)) {
  2223. if ((received_buf=received_builder(msg,&received_len))==0) {
  2224. LM_ERR("alas, received_builder failed\n");
  2225. goto error00;
  2226. }
  2227. }
  2228. /* check if rport needs to be updated */
  2229. if ( ((msg->msg_flags|global_req_flags)&FL_FORCE_RPORT)||
  2230. (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
  2231. if ((rport_buf=rport_builder(msg, &rport_len))==0){
  2232. LM_ERR("rport_builder failed\n");
  2233. goto error01; /* free everything */
  2234. }
  2235. if (msg->via1->rport)
  2236. len -= msg->via1->rport->size+1; /* include ';' */
  2237. }
  2238. /* first line */
  2239. len += msg->first_line.u.request.version.len + 1/*space*/ + 3/*code*/ + 1/*space*/ +
  2240. text->len + CRLF_LEN/*new line*/;
  2241. /*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
  2242. for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
  2243. switch (hdr->type) {
  2244. case HDR_TO_T:
  2245. if (new_tag && new_tag->len) {
  2246. to_tag=get_to(msg)->tag_value;
  2247. if ( to_tag.len || to_tag.s )
  2248. len+=new_tag->len-to_tag.len;
  2249. else
  2250. len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
  2251. }
  2252. len += hdr->len;
  2253. break;
  2254. case HDR_VIA_T:
  2255. /* we always add CRLF to via*/
  2256. len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
  2257. if (hdr==msg->h_via1) len += received_len+rport_len;
  2258. break;
  2259. case HDR_RECORDROUTE_T:
  2260. /* RR only for 1xx and 2xx replies */
  2261. if (code<180 || code>=300)
  2262. break;
  2263. case HDR_FROM_T:
  2264. case HDR_CALLID_T:
  2265. case HDR_CSEQ_T:
  2266. /* we keep the original termination for these headers*/
  2267. len += hdr->len;
  2268. break;
  2269. default:
  2270. /* do nothing, we are interested only in the above headers */
  2271. ;
  2272. }
  2273. }
  2274. /* lumps length */
  2275. for(lump=msg->reply_lump;lump;lump=lump->next) {
  2276. len += lump->text.len;
  2277. if (lump->flags&LUMP_RPL_BODY)
  2278. body = lump;
  2279. }
  2280. /* server header */
  2281. if (server_signature && server_hdr.len)
  2282. len += server_hdr.len + CRLF_LEN;
  2283. /* warning hdr */
  2284. if (sip_warning) {
  2285. warning_buf = warning_builder(msg,&warning_len);
  2286. if (warning_buf) len += warning_len + CRLF_LEN;
  2287. else LM_WARN("warning skipped -- too big\n");
  2288. }
  2289. /* content length hdr */
  2290. if (body) {
  2291. content_len_buf = int2str(body->text.len, (int*)&content_len_len);
  2292. len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
  2293. } else {
  2294. len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
  2295. }
  2296. /* end of message */
  2297. len += CRLF_LEN; /*new line*/
  2298. /*allocating mem*/
  2299. buf = (char*) pkg_malloc( len+1 );
  2300. if (!buf)
  2301. {
  2302. LM_ERR("out of memory; needs %d\n",len);
  2303. goto error01;
  2304. }
  2305. /* filling the buffer*/
  2306. p=buf;
  2307. /* first line */
  2308. memcpy( p , msg->first_line.u.request.version.s ,
  2309. msg->first_line.u.request.version.len);
  2310. p += msg->first_line.u.request.version.len;
  2311. *(p++) = ' ' ;
  2312. /*code*/
  2313. for ( i=2 , foo = code ; i>=0 ; i-- , foo=foo/10 )
  2314. *(p+i) = '0' + foo - ( foo/10 )*10;
  2315. p += 3;
  2316. *(p++) = ' ' ;
  2317. memcpy( p , text->s , text->len );
  2318. p += text->len;
  2319. memcpy( p, CRLF, CRLF_LEN );
  2320. p+=CRLF_LEN;
  2321. /* headers*/
  2322. for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
  2323. switch (hdr->type)
  2324. {
  2325. case HDR_VIA_T:
  2326. /* if is HTTP, backup start of Via header in response */
  2327. if(unlikely(httpreq)) pvia = p;
  2328. if (hdr==msg->h_via1){
  2329. if (rport_buf){
  2330. if (msg->via1->rport){ /* delete the old one */
  2331. /* copy until rport */
  2332. append_str_trans( p, hdr->name.s ,
  2333. msg->via1->rport->start-hdr->name.s-1,msg);
  2334. /* copy new rport */
  2335. append_str(p, rport_buf, rport_len);
  2336. /* copy the rest of the via */
  2337. append_str_trans(p, msg->via1->rport->start+
  2338. msg->via1->rport->size,
  2339. hdr->body.s+hdr->body.len-
  2340. msg->via1->rport->start-
  2341. msg->via1->rport->size, msg);
  2342. }else{ /* just append the new one */
  2343. /* normal whole via copy */
  2344. append_str_trans( p, hdr->name.s ,
  2345. (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
  2346. append_str(p, rport_buf, rport_len);
  2347. }
  2348. }else{
  2349. /* normal whole via copy */
  2350. append_str_trans( p, hdr->name.s ,
  2351. (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
  2352. }
  2353. if (received_buf)
  2354. append_str( p, received_buf, received_len);
  2355. }else{
  2356. /* normal whole via copy */
  2357. append_str_trans( p, hdr->name.s,
  2358. (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
  2359. }
  2360. append_str( p, CRLF,CRLF_LEN);
  2361. /* if is HTTP, replace Via with Sia
  2362. * - HTTP Via format is different than SIP Via
  2363. */
  2364. if(unlikely(httpreq)) *pvia = 'S';
  2365. break;
  2366. case HDR_RECORDROUTE_T:
  2367. /* RR only for 1xx and 2xx replies */
  2368. if (code<180 || code>=300) break;
  2369. append_str(p, hdr->name.s, hdr->len);
  2370. break;
  2371. case HDR_TO_T:
  2372. if (new_tag && new_tag->len){
  2373. if (to_tag.s ) { /* replacement */
  2374. /* before to-tag */
  2375. append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
  2376. /* to tag replacement */
  2377. bmark->to_tag_val.s=p;
  2378. bmark->to_tag_val.len=new_tag->len;
  2379. append_str( p, new_tag->s,new_tag->len);
  2380. /* the rest after to-tag */
  2381. append_str( p, to_tag.s+to_tag.len,
  2382. hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
  2383. }else{ /* adding a new to-tag */
  2384. after_body=hdr->body.s+hdr->body.len;
  2385. append_str( p, hdr->name.s, after_body-hdr->name.s);
  2386. append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
  2387. bmark->to_tag_val.s=p;
  2388. bmark->to_tag_val.len=new_tag->len;
  2389. append_str( p, new_tag->s,new_tag->len);
  2390. append_str( p, after_body,
  2391. hdr->name.s+hdr->len-after_body);
  2392. }
  2393. break;
  2394. } /* no new to-tag -- proceed to 1:1 copying */
  2395. totags=((struct to_body*)(hdr->parsed))->tag_value.s;
  2396. if (totags) {
  2397. bmark->to_tag_val.s=p+(totags-hdr->name.s);
  2398. bmark->to_tag_val.len=
  2399. ((struct to_body*)(hdr->parsed))->tag_value.len;
  2400. }
  2401. else {
  2402. bmark->to_tag_val.len = 0;
  2403. bmark->to_tag_val.s = p+(hdr->body.s+hdr->body.len-hdr->name.s);
  2404. }
  2405. /* no break */
  2406. case HDR_FROM_T:
  2407. case HDR_CALLID_T:
  2408. case HDR_CSEQ_T:
  2409. append_str(p, hdr->name.s, hdr->len);
  2410. break;
  2411. default:
  2412. /* do nothing, we are interested only in the above headers */
  2413. ;
  2414. } /* end switch */
  2415. } /* end for */
  2416. /* lumps */
  2417. for(lump=msg->reply_lump;lump;lump=lump->next)
  2418. if (lump->flags&LUMP_RPL_HDR){
  2419. memcpy(p,lump->text.s,lump->text.len);
  2420. p += lump->text.len;
  2421. }
  2422. /* server header */
  2423. if (server_signature && server_hdr.len>0) {
  2424. memcpy( p, server_hdr.s, server_hdr.len );
  2425. p+=server_hdr.len;
  2426. memcpy( p, CRLF, CRLF_LEN );
  2427. p+=CRLF_LEN;
  2428. }
  2429. /* content_length hdr */
  2430. if (content_len_len) {
  2431. append_str( p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
  2432. append_str( p, content_len_buf, content_len_len );
  2433. append_str( p, CRLF, CRLF_LEN );
  2434. } else {
  2435. append_str( p, CONTENT_LENGTH"0"CRLF,CONTENT_LENGTH_LEN+1+CRLF_LEN);
  2436. }
  2437. /* warning header */
  2438. if (warning_buf) {
  2439. memcpy( p, warning_buf, warning_len);
  2440. p+=warning_len;
  2441. memcpy( p, CRLF, CRLF_LEN);
  2442. p+=CRLF_LEN;
  2443. }
  2444. /*end of message*/
  2445. memcpy( p, CRLF, CRLF_LEN );
  2446. p+=CRLF_LEN;
  2447. /* body */
  2448. if (body) {
  2449. memcpy ( p, body->text.s, body->text.len );
  2450. p+=body->text.len;
  2451. }
  2452. if (len!=p-buf)
  2453. LM_CRIT("diff len=%d p-buf=%d\n", len, (int)(p-buf));
  2454. *(p) = 0;
  2455. *returned_len = len;
  2456. /* in req2reply, received_buf is not introduced to lumps and
  2457. needs to be deleted here
  2458. */
  2459. if (received_buf) pkg_free(received_buf);
  2460. if (rport_buf) pkg_free(rport_buf);
  2461. return buf;
  2462. error01:
  2463. if (received_buf) pkg_free(received_buf);
  2464. if (rport_buf) pkg_free(rport_buf);
  2465. error00:
  2466. *returned_len=0;
  2467. return 0;
  2468. }
  2469. /* return number of chars printed or 0 if space exceeded;
  2470. assumes buffer size of at least MAX_BRANCH_PARAM_LEN
  2471. */
  2472. int branch_builder( unsigned int hash_index,
  2473. /* only either parameter useful */
  2474. unsigned int label, char * char_v,
  2475. int branch,
  2476. char *branch_str, int *len )
  2477. {
  2478. char *begin;
  2479. int size;
  2480. /* hash id provided ... start with it */
  2481. size=MAX_BRANCH_PARAM_LEN;
  2482. begin=branch_str;
  2483. *len=0;
  2484. memcpy(begin, MCOOKIE, MCOOKIE_LEN );
  2485. size-=MCOOKIE_LEN;begin+=MCOOKIE_LEN;
  2486. if (int2reverse_hex( &begin, &size, hash_index)==-1)
  2487. return 0;
  2488. if (size) {
  2489. *begin=BRANCH_SEPARATOR;
  2490. begin++; size--;
  2491. } else return 0;
  2492. /* string with request's characteristic value ... use it ... */
  2493. if (char_v) {
  2494. if (memcpy(begin,char_v,MD5_LEN)) {
  2495. begin+=MD5_LEN; size-=MD5_LEN;
  2496. } else return 0;
  2497. } else { /* ... use the "label" value otherwise */
  2498. if (int2reverse_hex( &begin, &size, label )==-1)
  2499. return 0;
  2500. }
  2501. if (size) {
  2502. *begin=BRANCH_SEPARATOR;
  2503. begin++; size--;
  2504. } else return 0;
  2505. if (int2reverse_hex( &begin, &size, branch)==-1)
  2506. return 0;
  2507. *len=MAX_BRANCH_PARAM_LEN-size;
  2508. return size;
  2509. }
  2510. /* uses only the send_info->send_socket, send_info->proto and
  2511. * send_info->comp (so that a send_info used for sending can be passed
  2512. * to this function w/o changes and the correct via will be built) */
  2513. char* via_builder( unsigned int *len,
  2514. struct dest_info* send_info /* where to send the reply */,
  2515. str* branch, str* extra_params, struct hostport* hp)
  2516. {
  2517. unsigned int via_len, extra_len;
  2518. char *line_buf;
  2519. int max_len;
  2520. int via_prefix_len;
  2521. str* address_str; /* address displayed in via */
  2522. str* port_str; /* port no displayed in via */
  2523. struct socket_info* send_sock;
  2524. int comp_len, comp_name_len;
  2525. #ifdef USE_COMP
  2526. char* comp_name;
  2527. #endif /* USE_COMP */
  2528. int port;
  2529. struct ip_addr ip;
  2530. union sockaddr_union *from = NULL;
  2531. union sockaddr_union local_addr;
  2532. struct tcp_connection *con = NULL;
  2533. send_sock=send_info->send_sock;
  2534. /* use pre-set address in via, the outbound socket alias or address one */
  2535. if (hp && hp->host->len)
  2536. address_str=hp->host;
  2537. else if(send_sock->useinfo.name.len>0)
  2538. address_str=&(send_sock->useinfo.name);
  2539. else
  2540. address_str=&(send_sock->address_str);
  2541. if (hp && hp->port->len)
  2542. port_str=hp->port;
  2543. else if(send_sock->useinfo.port_no>0)
  2544. port_str=&(send_sock->useinfo.port_no_str);
  2545. else
  2546. port_str=&(send_sock->port_no_str);
  2547. comp_len=comp_name_len=0;
  2548. #ifdef USE_COMP
  2549. comp_name=0;
  2550. switch(send_info->comp){
  2551. case COMP_NONE:
  2552. break;
  2553. case COMP_SIGCOMP:
  2554. comp_len=COMP_PARAM_LEN;
  2555. comp_name_len=SIGCOMP_NAME_LEN;
  2556. comp_name=SIGCOMP_NAME;
  2557. break;
  2558. case COMP_SERGZ:
  2559. comp_len=COMP_PARAM_LEN;
  2560. comp_name_len=SERGZ_NAME_LEN;
  2561. comp_name=SERGZ_NAME;
  2562. break;
  2563. default:
  2564. LM_CRIT("unknown comp %d\n", send_info->comp);
  2565. /* continue, we'll just ignore comp */
  2566. }
  2567. #endif /* USE_COMP */
  2568. via_prefix_len=MY_VIA_LEN+(send_info->proto==PROTO_SCTP);
  2569. max_len=via_prefix_len +address_str->len /* space in MY_VIA */
  2570. +2 /* just in case it is a v6 address ... [ ] */
  2571. +1 /*':'*/+port_str->len
  2572. +(branch?(MY_BRANCH_LEN+branch->len):0)
  2573. +(extra_params?extra_params->len:0)
  2574. +comp_len+comp_name_len
  2575. +CRLF_LEN+1;
  2576. line_buf=pkg_malloc( max_len );
  2577. if (line_buf==0){
  2578. ser_error=E_OUT_OF_MEM;
  2579. LM_ERR("out of memory\n");
  2580. return 0;
  2581. }
  2582. extra_len=0;
  2583. via_len=via_prefix_len+address_str->len; /*space included in MY_VIA*/
  2584. memcpy(line_buf, MY_VIA, MY_VIA_LEN);
  2585. if (send_info->proto==PROTO_UDP){
  2586. /* do nothing */
  2587. }else if (send_info->proto==PROTO_TCP){
  2588. memcpy(line_buf+MY_VIA_LEN-4, "TCP ", 4);
  2589. }else if (send_info->proto==PROTO_TLS){
  2590. memcpy(line_buf+MY_VIA_LEN-4, "TLS ", 4);
  2591. }else if (send_info->proto==PROTO_SCTP){
  2592. memcpy(line_buf+MY_VIA_LEN-4, "SCTP ", 5);
  2593. }else if (send_info->proto==PROTO_WS){
  2594. if (unlikely(send_info->send_flags.f & SND_F_FORCE_SOCKET
  2595. && send_info->send_sock)) {
  2596. local_addr = send_info->send_sock->su;
  2597. su_setport(&local_addr, 0); /* any local port will do */
  2598. from = &local_addr;
  2599. }
  2600. port = su_getport(&send_info->to);
  2601. if (likely(port)) {
  2602. su2ip_addr(&ip, &send_info->to);
  2603. con = tcpconn_get(send_info->id, &ip, port, from, 0);
  2604. }
  2605. else if (likely(send_info->id))
  2606. con = tcpconn_get(send_info->id, 0, 0, 0, 0);
  2607. else {
  2608. LM_CRIT("null_id & to\n"); pkg_free(line_buf);
  2609. return 0;
  2610. }
  2611. if (con == NULL) {
  2612. LM_WARN("TCP/TLS connection (id: %d) for WebSocket could not be found\n",
  2613. send_info->id);
  2614. pkg_free(line_buf);
  2615. return 0;
  2616. }
  2617. if (con->rcv.proto==PROTO_WS) {
  2618. memcpy(line_buf+MY_VIA_LEN-4, "WS ", 3);
  2619. } else if (con->rcv.proto==PROTO_WSS) {
  2620. memcpy(line_buf+MY_VIA_LEN-4, "WSS ", 4);
  2621. } else {
  2622. tcpconn_put(con);
  2623. LM_CRIT("unknown proto %d\n", con->rcv.proto);
  2624. pkg_free(line_buf);
  2625. return 0;
  2626. }
  2627. tcpconn_put(con);
  2628. }else if (send_info->proto==PROTO_WSS){
  2629. memcpy(line_buf+MY_VIA_LEN-4, "WSS ", 4);
  2630. }else{
  2631. LM_CRIT("unknown proto %d\n", send_info->proto);
  2632. pkg_free(line_buf);
  2633. return 0;
  2634. }
  2635. /* add [] only if ipv6 and outbound socket address is used;
  2636. * if using pre-set no check is made */
  2637. if ((send_sock->address.af==AF_INET6) &&
  2638. (address_str==&(send_sock->address_str))) {
  2639. line_buf[via_prefix_len]='[';
  2640. line_buf[via_prefix_len+1+address_str->len]=']';
  2641. extra_len=1;
  2642. via_len+=2; /* [ ]*/
  2643. }
  2644. memcpy(line_buf+via_prefix_len+extra_len, address_str->s,
  2645. address_str->len);
  2646. if ((send_sock->port_no!=SIP_PORT) ||
  2647. (port_str!=&send_sock->port_no_str)){
  2648. line_buf[via_len]=':'; via_len++;
  2649. memcpy(line_buf+via_len, port_str->s, port_str->len);
  2650. via_len+=port_str->len;
  2651. }
  2652. /* branch parameter */
  2653. if (branch){
  2654. memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
  2655. via_len+=MY_BRANCH_LEN;
  2656. memcpy(line_buf+via_len, branch->s, branch->len );
  2657. via_len+=branch->len;
  2658. }
  2659. /* extra params */
  2660. if (extra_params){
  2661. memcpy(line_buf+via_len, extra_params->s, extra_params->len);
  2662. via_len+=extra_params->len;
  2663. }
  2664. #ifdef USE_COMP
  2665. /* comp */
  2666. if (comp_len){
  2667. memcpy(line_buf+via_len, COMP_PARAM, COMP_PARAM_LEN);
  2668. via_len+=COMP_PARAM_LEN;
  2669. memcpy(line_buf+via_len, comp_name, comp_name_len);
  2670. via_len+=comp_name_len;
  2671. }
  2672. #endif
  2673. memcpy(line_buf+via_len, CRLF, CRLF_LEN);
  2674. via_len+=CRLF_LEN;
  2675. line_buf[via_len]=0; /* null terminate the string*/
  2676. *len = via_len;
  2677. return line_buf;
  2678. }
  2679. /* creates a via header honoring the protocol of the incomming socket
  2680. * msg is an optional parameter */
  2681. char* create_via_hf( unsigned int *len,
  2682. struct sip_msg *msg,
  2683. struct dest_info* send_info /* where to send the reply */,
  2684. str* branch)
  2685. {
  2686. char* via;
  2687. str extra_params;
  2688. struct hostport hp;
  2689. #if defined USE_TCP || defined USE_SCTP
  2690. char* id_buf;
  2691. unsigned int id_len;
  2692. id_buf=0;
  2693. id_len=0;
  2694. #endif
  2695. extra_params.len=0;
  2696. extra_params.s=0;
  2697. #if defined USE_TCP || defined USE_SCTP
  2698. /* add id if tcp */
  2699. if (msg && (
  2700. #ifdef USE_TCP
  2701. (msg->rcv.proto==PROTO_TCP)
  2702. #ifdef USE_TLS
  2703. || (msg->rcv.proto==PROTO_TLS)
  2704. #endif
  2705. #ifdef USE_SCTP
  2706. ||
  2707. #endif /* USE_SCTP */
  2708. #endif /* USE_TCP */
  2709. #ifdef USE_SCTP
  2710. (msg->rcv.proto==PROTO_SCTP)
  2711. #endif /* USE_SCTP */
  2712. )){
  2713. if ((id_buf=id_builder(msg, &id_len))==0){
  2714. LM_ERR("id_builder failed\n");
  2715. return 0; /* we don't need to free anything,
  2716. nothing alloc'ed yet*/
  2717. }
  2718. DBG("create_via_hf: id added: <%.*s>, rcv proto=%d\n",
  2719. (int)id_len, id_buf, msg->rcv.proto);
  2720. extra_params.s=id_buf;
  2721. extra_params.len=id_len;
  2722. }
  2723. #endif /* USE_TCP || USE_SCTP */
  2724. /* test and add rport parameter to local via - rfc3581 */
  2725. if(msg && msg->msg_flags&FL_ADD_LOCAL_RPORT) {
  2726. /* params so far + ';rport' + '\0' */
  2727. via = (char*)pkg_malloc(extra_params.len+RPORT_LEN);
  2728. if(via==0) {
  2729. LM_ERR("building local rport via param failed\n");
  2730. if (extra_params.s) pkg_free(extra_params.s);
  2731. return 0;
  2732. }
  2733. if(extra_params.len!=0) {
  2734. memcpy(via, extra_params.s, extra_params.len);
  2735. pkg_free(extra_params.s);
  2736. }
  2737. memcpy(via + extra_params.len, RPORT, RPORT_LEN-1);
  2738. extra_params.s = via;
  2739. extra_params.len += RPORT_LEN-1;
  2740. extra_params.s[extra_params.len]='\0';
  2741. }
  2742. set_hostport(&hp, msg);
  2743. via = via_builder( len, send_info, branch,
  2744. extra_params.len?&extra_params:0, &hp);
  2745. /* we do not need extra_params any more, already in the new via header */
  2746. if (extra_params.s) pkg_free(extra_params.s);
  2747. return via;
  2748. }
  2749. /* builds a char* buffer from message headers without body
  2750. * first line is excluded in case of skip_first_line=1
  2751. * error is set -1 if the memory allocation failes
  2752. */
  2753. char * build_only_headers( struct sip_msg* msg, int skip_first_line,
  2754. unsigned int *returned_len,
  2755. int *error,
  2756. struct dest_info* send_info)
  2757. {
  2758. char *buf, *new_buf;
  2759. unsigned int offset, s_offset, len, new_len;
  2760. *error = 0;
  2761. buf = msg->buf;
  2762. if (skip_first_line)
  2763. s_offset = msg->headers->name.s - buf;
  2764. else
  2765. s_offset = 0;
  2766. /* original length without body, and without final \r\n */
  2767. len = msg->unparsed - buf;
  2768. /* new msg length */
  2769. new_len = len - /* original length */
  2770. s_offset + /* skipped first line */
  2771. lumps_len(msg, msg->add_rm, send_info); /* lumps */
  2772. if (new_len == 0) {
  2773. *returned_len = 0;
  2774. return 0;
  2775. }
  2776. new_buf = (char *)pkg_malloc(new_len+1);
  2777. if (!new_buf) {
  2778. LM_ERR("Not enough memory\n");
  2779. *error = -1;
  2780. return 0;
  2781. }
  2782. new_buf[0] = 0;
  2783. offset = 0;
  2784. /* copy message lumps */
  2785. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
  2786. /* copy the rest of the message without body */
  2787. if (len > s_offset) {
  2788. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  2789. offset += (len-s_offset);
  2790. }
  2791. new_buf[offset] = 0;
  2792. *returned_len = offset;
  2793. return new_buf;
  2794. }
  2795. /* builds a char* buffer from message body
  2796. * error is set -1 if the memory allocation failes
  2797. */
  2798. char * build_body( struct sip_msg* msg,
  2799. unsigned int *returned_len,
  2800. int *error,
  2801. struct dest_info* send_info)
  2802. {
  2803. char *buf, *new_buf, *body;
  2804. unsigned int offset, s_offset, len, new_len;
  2805. *error = 0;
  2806. body = get_body(msg);
  2807. if (!body || (body[0] == 0)) {
  2808. *returned_len = 0;
  2809. return 0;
  2810. }
  2811. buf = msg->buf;
  2812. s_offset = body - buf;
  2813. /* original length of msg with body */
  2814. len = msg->len;
  2815. /* new body length */
  2816. new_len = len - /* original length */
  2817. s_offset + /* msg without body */
  2818. lumps_len(msg, msg->body_lumps, send_info); /* lumps */
  2819. new_buf = (char *)pkg_malloc(new_len+1);
  2820. if (!new_buf) {
  2821. LM_ERR("Not enough memory\n");
  2822. *error = -1;
  2823. return 0;
  2824. }
  2825. new_buf[0] = 0;
  2826. offset = 0;
  2827. /* copy body lumps */
  2828. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
  2829. /* copy the rest of the message without body */
  2830. if (len > s_offset) {
  2831. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  2832. offset += (len-s_offset);
  2833. }
  2834. new_buf[offset] = 0;
  2835. *returned_len = offset;
  2836. return new_buf;
  2837. }
  2838. /* builds a char* buffer from SIP message including body
  2839. * The function adjusts the Content-Length HF according
  2840. * to body lumps in case of touch_clen=1.
  2841. */
  2842. char * build_all( struct sip_msg* msg, int touch_clen,
  2843. unsigned int *returned_len,
  2844. int *error,
  2845. struct dest_info* send_info)
  2846. {
  2847. char *buf, *new_buf;
  2848. unsigned int offset, s_offset, len, new_len;
  2849. unsigned int body_delta;
  2850. *error = 0;
  2851. /* Calculate message body difference */
  2852. body_delta = lumps_len(msg, msg->body_lumps, send_info);
  2853. if (touch_clen) {
  2854. /* adjust Content-Length */
  2855. if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
  2856. LM_ERR("Error while adjusting Content-Length\n");
  2857. *error = -1;
  2858. return 0;
  2859. }
  2860. }
  2861. buf = msg->buf;
  2862. /* original msg length */
  2863. len = msg->len;
  2864. /* new msg length */
  2865. new_len = len + /* original length */
  2866. lumps_len(msg, msg->add_rm, send_info) + /* hdr lumps */
  2867. body_delta; /* body lumps */
  2868. if (new_len == 0) {
  2869. returned_len = 0;
  2870. return 0;
  2871. }
  2872. new_buf = (char *)pkg_malloc(new_len+1);
  2873. if (!new_buf) {
  2874. LM_ERR("Not enough memory\n");
  2875. *error = -1;
  2876. return 0;
  2877. }
  2878. new_buf[0] = 0;
  2879. offset = s_offset = 0;
  2880. /* copy message lumps */
  2881. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
  2882. /* copy body lumps */
  2883. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
  2884. /* copy the rest of the message */
  2885. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  2886. offset += (len-s_offset);
  2887. new_buf[offset] = 0;
  2888. *returned_len = offset;
  2889. return new_buf;
  2890. }
  2891. /**
  2892. * parse buf in msg and fill several fields
  2893. */
  2894. int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len,
  2895. unsigned int id)
  2896. {
  2897. if(msg==0 || buf==0)
  2898. return -1;
  2899. memset(msg, 0, sizeof(sip_msg_t));
  2900. msg->id = id;
  2901. msg->buf = buf;
  2902. msg->len = len;
  2903. if (parse_msg(buf, len, msg)!=0) {
  2904. LM_ERR("parsing failed");
  2905. return -1;
  2906. }
  2907. msg->set_global_address=default_global_address;
  2908. msg->set_global_port=default_global_port;
  2909. return 0;
  2910. }