msg_translator.c 81 KB

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