msg_translator.c 79 KB

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