msg_translator.c 83 KB

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