msg_translator.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. *
  29. * History:
  30. * --------
  31. * 2003-01-20 bug_fix: use of return value of snprintf aligned to C99 (jiri)
  32. * 2003-01-23 added rport patches, contributed by
  33. * Maxim Sobolev <[email protected]> and heavily modified by me
  34. * (andrei)
  35. * 2003-01-24 added i param to via of outgoing requests (used by tcp),
  36. * modified via_builder params (andrei)
  37. * 2003-01-27 more rport fixes (make use of new via_param->start) (andrei)
  38. * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
  39. * 2003-01-29 scratchpad removed (jiri)
  40. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  41. * 2003-03-01 VOICE_MAIL defs removed (jiri)
  42. * 2003-03-06 totags in outgoing replies bookmarked to enable
  43. * ACK/200 tag matching (andrei)
  44. * 2003-03-18 killed the build_warning snprintf (andrei)
  45. * 2003-03-31 added subst lump support (andrei)
  46. * 2003-04-01 added opt (conditional) lump support (andrei)
  47. * 2003-04-02 added more subst lumps: SUBST_{SND,RCV}_ALL
  48. * => ip:port;transport=proto (andrei)
  49. * 2003-04-12 added FL_FORCE_RPORT support (andrei)
  50. * 2003-04-13 updated warning builder -- fixed (andrei)
  51. * 2003-07-10 check_via_address knows now how to compare with ipv6 address
  52. * references (e.g [::1]) (andrei)
  53. * build_req_fomr_sip_req no longer adds 1 for ipv6 via parameter
  54. * position calculations ([] are part of host.s now) (andrei)
  55. * 2003-10-02 via+lump dst address/port can be set to preset values (andrei)
  56. * 2003-10-08 receive_test function-alized (jiri)
  57. * 2003-10-20 added body_lump list (sip_msg), adjust_clen (andrei & jan)
  58. * 2003-11-11 type of rpl_lumps replaced by flags (bogdan)
  59. * 2006-04-20 build_req_from_sip_req, via_builder and lump_* functions
  60. * use now struct dest_info; lumps & via comp param support
  61. * (rfc3486) (andrei)
  62. * 2007-08-31 id_builder() and via_builder() are grouped into one function:
  63. * create_via_hf() -- tm module needs them as well (Miklos)
  64. * 2008-12-17 build_req_from_sip_req() will now fallback to tcp, tls or sctp
  65. * if packet size > udp_mtu and fallback is enabled
  66. * build_req_from_sip_req() uses now global_req_flags along
  67. * msg->msg_flags (andrei)
  68. *
  69. */
  70. /* Via special params:
  71. * requests:
  72. * - if the address in via is different from the src_ip or an existing
  73. * received=something is found, received=src_ip is added (and any preexisting
  74. * received is deleted). received is added as the first via parameter if no
  75. * receive is previously present or over the old receive.
  76. * - if the original via contains rport / rport=something or msg->msg_flags
  77. * FL_FORCE_RPORT is set (e.g. script force_rport() cmd) rport=src_port
  78. * is added (over previous rport / as first via param or after received
  79. * if no received was present and received is added too)
  80. * local replies:
  81. * (see also sl_send_reply)
  82. * - rport and received are added in mostly the same way as for requests, but
  83. * in the reverse order (first rport and then received). See also
  84. * limitations.
  85. * - if reply_to_via is set (default off) the local reply will be sent to
  86. * the address in via (received is ignored since it was not set by us). The
  87. * destination port is either the message source port if via contains rport
  88. * or the FL_FORCE_RPORT flag is set or the port from the via. If either
  89. * port or rport are present a normal dns lookup (instead of a srv lookup)
  90. * is performed on the address. If no port is present and a srv lookup is
  91. * performed the port is taken from the srv lookup. If the srv lookup failed
  92. * or it was not performed, the port is set to the default sip port (5060).
  93. * - if reply_to_via is off (default) the local reply is sent to the message
  94. * source ip address. The destination port is set to the source port if
  95. * rport is present or FL_FORCE_RPORT flag is set, to the via port or to
  96. * the default sip port (5060) if neither rport or via port are present.
  97. * "normal" replies:
  98. * - if received is present the message is sent to the received address else
  99. * if no port is present (neither a normal via port or rport) a dns srv
  100. * lookup is performed on the host part and the reply is sent to the
  101. * resulting ip. If a port is present or the host part is an ip address
  102. * the dns lookup will be a "normal" one (A or AAAA).
  103. * - if rport is present, it's value will be used as the destination port
  104. * (and this will also disable srv lookups)
  105. * - if no port is present the destination port will be taken from the srv
  106. * lookup. If the srv lookup fails or is not performed (e.g. ip address
  107. * in host) the destination port will be set to the default sip port (5060).
  108. *
  109. * Known limitations:
  110. * - when locally replying to a message, rport and received will be appended to
  111. * the via header parameters (for forwarded requests they are inserted at the
  112. * beginning).
  113. * - a locally generated reply might get two received via parameters if a
  114. * received is already present in the original message (this should not
  115. * happen though, but ...)
  116. *
  117. *--andrei
  118. */
  119. #include <sys/types.h>
  120. #include <sys/socket.h>
  121. #include <netdb.h>
  122. #include <string.h>
  123. #include <stdio.h>
  124. #include <stdlib.h>
  125. #include "comp_defs.h"
  126. #include "msg_translator.h"
  127. #include "globals.h"
  128. #include "error.h"
  129. #include "mem/mem.h"
  130. #include "dprint.h"
  131. #include "config.h"
  132. #include "md5utils.h"
  133. #include "data_lump.h"
  134. #include "data_lump_rpl.h"
  135. #include "ip_addr.h"
  136. #include "resolve.h"
  137. #include "ut.h"
  138. #include "pt.h"
  139. #include "cfg/cfg.h"
  140. #include "forward.h"
  141. #define append_str(_dest,_src,_len) \
  142. do{\
  143. memcpy( (_dest) , (_src) , (_len) );\
  144. (_dest) += (_len) ;\
  145. }while(0);
  146. #define append_str_trans(_dest,_src,_len,_msg) \
  147. append_str( (_dest), (_src), (_len) );
  148. extern char version[];
  149. extern int version_len;
  150. /* global flags for build_req_from_sip_req */
  151. static unsigned int global_req_flags=0;
  152. /** per process fixup function for global_req_flags.
  153. * It should be called from the configuration framework.
  154. */
  155. void fix_global_req_flags(str* gname, str* name)
  156. {
  157. global_req_flags=0;
  158. switch(cfg_get(core, core_cfg, udp_mtu_try_proto)){
  159. case PROTO_NONE:
  160. case PROTO_UDP:
  161. /* do nothing */
  162. break;
  163. case PROTO_TCP:
  164. global_req_flags|=FL_MTU_TCP_FB;
  165. break;
  166. case PROTO_TLS:
  167. global_req_flags|=FL_MTU_TLS_FB;
  168. break;
  169. case PROTO_SCTP:
  170. global_req_flags|=FL_MTU_SCTP_FB;
  171. break;
  172. }
  173. if (cfg_get(core, core_cfg, force_rport))
  174. global_req_flags|=FL_FORCE_RPORT;
  175. }
  176. /* checks if ip is in host(name) and ?host(ip)=name?
  177. * ip must be in network byte order!
  178. * resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
  179. * return 0 if equal */
  180. static int check_via_address(struct ip_addr* ip, str *name,
  181. unsigned short port, int resolver)
  182. {
  183. struct hostent* he;
  184. int i;
  185. char* s;
  186. int len;
  187. /* maybe we are lucky and name it's an ip */
  188. s=ip_addr2a(ip);
  189. if (s){
  190. DBG("check_via_address(%s, %.*s, %d)\n",
  191. s, name->len, name->s, resolver);
  192. len=strlen(s);
  193. #ifdef USE_IPV6
  194. /* check if name->s is an ipv6 address or an ipv6 address ref. */
  195. if ((ip->af==AF_INET6) &&
  196. ( ((len==name->len)&&(strncasecmp(name->s, s, name->len)==0))
  197. ||
  198. ((len==(name->len-2))&&(name->s[0]=='[')&&
  199. (name->s[name->len-1]==']')&&
  200. (strncasecmp(name->s+1, s, len)==0))
  201. )
  202. )
  203. return 0;
  204. else
  205. #endif
  206. if (strncmp(name->s, s, name->len)==0)
  207. return 0;
  208. }else{
  209. LOG(L_CRIT, "check_via_address: BUG: 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
  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. LOG(L_NOTICE, "NOTICE: warning_builder: 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. LOG(L_ERR, "ERROR: received_builder: out of memory\n");
  326. return 0;
  327. }
  328. memcpy(buf, RECEIVED, RECEIVED_LEN);
  329. if ( (tmp=ip_addr2a(source_ip))==0)
  330. return 0; /* error*/
  331. tmp_len=strlen(tmp);
  332. len=RECEIVED_LEN+tmp_len;
  333. memcpy(buf+RECEIVED_LEN, tmp, tmp_len);
  334. buf[len]=0; /*null terminate it */
  335. *received_len = len;
  336. return buf;
  337. }
  338. char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
  339. {
  340. char* buf;
  341. char* tmp;
  342. int tmp_len;
  343. int len;
  344. tmp_len=0;
  345. tmp=int2str(msg->rcv.src_port, &tmp_len);
  346. len=RPORT_LEN+tmp_len;
  347. buf=pkg_malloc(sizeof(char)*(len+1));/* space for null term */
  348. if (buf==0){
  349. ser_error=E_OUT_OF_MEM;
  350. LOG(L_ERR, "ERROR: rport_builder: out of memory\n");
  351. return 0;
  352. }
  353. memcpy(buf, RPORT, RPORT_LEN);
  354. memcpy(buf+RPORT_LEN, tmp, tmp_len);
  355. buf[len]=0; /*null terminate it*/
  356. *rport_len=len;
  357. return buf;
  358. }
  359. char* id_builder(struct sip_msg* msg, unsigned int *id_len)
  360. {
  361. char* buf;
  362. int len, value_len;
  363. char revhex[sizeof(int)*2];
  364. char* p;
  365. int size;
  366. size=sizeof(int)*2;
  367. p=&revhex[0];
  368. if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
  369. LOG(L_CRIT, "BUG: id_builder: not enough space for id\n");
  370. return 0;
  371. }
  372. value_len=p-&revhex[0];
  373. len=ID_PARAM_LEN+value_len;
  374. buf=pkg_malloc(sizeof(char)*(len+1));/* place for ending \0 */
  375. if (buf==0){
  376. ser_error=E_OUT_OF_MEM;
  377. LOG(L_ERR, "ERROR: id_builder: out of memory\n");
  378. return 0;
  379. }
  380. memcpy(buf, ID_PARAM, ID_PARAM_LEN);
  381. memcpy(buf+ID_PARAM_LEN, revhex, value_len);
  382. buf[len]=0; /* null terminate it */
  383. *id_len=len;
  384. return buf;
  385. }
  386. char* clen_builder( struct sip_msg* msg, int *clen_len, int diff,
  387. int body_only)
  388. {
  389. char* buf;
  390. int len;
  391. int value;
  392. char* value_s;
  393. int value_len;
  394. char* body;
  395. body=get_body(msg);
  396. if (body==0){
  397. ser_error=E_BAD_REQ;
  398. LOG(L_ERR, "ERROR: clen_builder: no message body found"
  399. " (missing crlf?)");
  400. return 0;
  401. }
  402. value=msg->len-(int)(body-msg->buf)+diff;
  403. value_s=int2str(value, &value_len);
  404. DBG("clen_builder: content-length: %d (%s)\n", value, value_s);
  405. if (body_only) {
  406. len=value_len;
  407. }
  408. else {
  409. len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
  410. }
  411. buf=pkg_malloc(sizeof(char)*(len+1));
  412. if (buf==0){
  413. ser_error=E_OUT_OF_MEM;
  414. LOG(L_ERR, "ERROR: clen_builder: out of memory\n");
  415. return 0;
  416. }
  417. if (body_only) {
  418. memcpy(buf, value_s, value_len);
  419. }
  420. else {
  421. memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
  422. memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
  423. memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
  424. }
  425. buf[len]=0; /* null terminate it */
  426. *clen_len=len;
  427. return buf;
  428. }
  429. /* checks if a lump opt condition
  430. * returns 1 if cond is true, 0 if false */
  431. static inline int lump_check_opt( enum lump_conditions cond,
  432. struct sip_msg* msg,
  433. struct dest_info* snd_i
  434. )
  435. {
  436. struct ip_addr* ip;
  437. unsigned short port;
  438. int proto;
  439. #define get_ip_port_proto \
  440. if ((snd_i==0) || (snd_i->send_sock==0)){ \
  441. LOG(L_CRIT, "ERROR: lump_check_opt: null send socket\n"); \
  442. return 1; /* we presume they are different :-) */ \
  443. } \
  444. if (msg->rcv.bind_address){ \
  445. ip=&msg->rcv.bind_address->address; \
  446. port=msg->rcv.bind_address->port_no; \
  447. proto=msg->rcv.bind_address->proto; \
  448. }else{ \
  449. ip=&msg->rcv.dst_ip; \
  450. port=msg->rcv.dst_port; \
  451. proto=msg->rcv.proto; \
  452. } \
  453. switch(cond){
  454. case COND_FALSE:
  455. return 0;
  456. case COND_TRUE:
  457. return 1;
  458. case COND_IF_DIFF_REALMS:
  459. get_ip_port_proto;
  460. /* faster tests first */
  461. if ((port==snd_i->send_sock->port_no) &&
  462. (proto==snd_i->send_sock->proto) &&
  463. #ifdef USE_COMP
  464. (msg->rcv.comp==snd_i->comp) &&
  465. #endif
  466. (ip_addr_cmp(ip, &snd_i->send_sock->address)))
  467. return 0;
  468. else return 1;
  469. case COND_IF_DIFF_AF:
  470. get_ip_port_proto;
  471. if (ip->af!=snd_i->send_sock->address.af) return 1;
  472. else return 0;
  473. case COND_IF_DIFF_PROTO:
  474. get_ip_port_proto;
  475. if (proto!=snd_i->send_sock->proto) return 1;
  476. else return 0;
  477. case COND_IF_DIFF_PORT:
  478. get_ip_port_proto;
  479. if (port!=snd_i->send_sock->port_no) return 1;
  480. else return 0;
  481. case COND_IF_DIFF_IP:
  482. get_ip_port_proto;
  483. if (ip_addr_cmp(ip, &snd_i->send_sock->address)) return 0;
  484. else return 1;
  485. case COND_IF_RAND:
  486. return (rand()>=RAND_MAX/2);
  487. default:
  488. LOG(L_CRIT, "BUG: lump:w_check_opt: unknown lump condition %d\n",
  489. cond);
  490. }
  491. return 0; /* false */
  492. }
  493. /* computes the "unpacked" len of a lump list,
  494. code moved from build_req_from_req */
  495. static inline int lumps_len(struct sip_msg* msg, struct lump* lumps,
  496. struct dest_info* send_info)
  497. {
  498. int s_offset;
  499. int new_len;
  500. struct lump* t;
  501. struct lump* r;
  502. str* send_address_str;
  503. str* send_port_str;
  504. struct socket_info* send_sock;
  505. #ifdef USE_COMP
  506. #define RCVCOMP_LUMP_LEN \
  507. /* add;comp=xxx */ \
  508. switch(msg->rcv.comp){ \
  509. case COMP_NONE: \
  510. break; \
  511. case COMP_SIGCOMP: \
  512. new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
  513. break; \
  514. case COMP_SERGZ: \
  515. new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
  516. break; \
  517. default: \
  518. LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
  519. msg->rcv.comp); \
  520. }
  521. #define SENDCOMP_LUMP_LEN \
  522. /* add;comp=xxx */ \
  523. switch(send_info->comp){ \
  524. case COMP_NONE: \
  525. break; \
  526. case COMP_SIGCOMP: \
  527. new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
  528. break; \
  529. case COMP_SERGZ: \
  530. new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
  531. break; \
  532. default: \
  533. LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
  534. send_info->comp); \
  535. }
  536. #else
  537. #define RCVCOMP_LUMP_LEN
  538. #define SENDCOMP_LUMP_LEN
  539. #endif /*USE_COMP */
  540. #define SUBST_LUMP_LEN(subst_l) \
  541. switch((subst_l)->u.subst){ \
  542. case SUBST_RCV_IP: \
  543. if (msg->rcv.bind_address){ \
  544. new_len+=msg->rcv.bind_address->address_str.len; \
  545. if (msg->rcv.bind_address->address.af!=AF_INET) \
  546. new_len+=2; \
  547. }else{ \
  548. /* FIXME */ \
  549. LOG(L_CRIT, "FIXME: null bind_address\n"); \
  550. }; \
  551. break; \
  552. case SUBST_RCV_PORT: \
  553. if (msg->rcv.bind_address){ \
  554. new_len+=msg->rcv.bind_address->port_no_str.len; \
  555. }else{ \
  556. /* FIXME */ \
  557. LOG(L_CRIT, "FIXME: null bind_address\n"); \
  558. }; \
  559. break; \
  560. case SUBST_RCV_PROTO: \
  561. if (msg->rcv.bind_address){ \
  562. switch(msg->rcv.bind_address->proto){ \
  563. case PROTO_NONE: \
  564. case PROTO_UDP: \
  565. case PROTO_TCP: \
  566. case PROTO_TLS: \
  567. new_len+=3; \
  568. break; \
  569. case PROTO_SCTP: \
  570. new_len+=4; \
  571. break; \
  572. default: \
  573. LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
  574. msg->rcv.bind_address->proto); \
  575. }\
  576. }else{ \
  577. /* FIXME */ \
  578. LOG(L_CRIT, "FIXME: null bind_address\n"); \
  579. }; \
  580. break; \
  581. case SUBST_RCV_ALL: \
  582. if (msg->rcv.bind_address){ \
  583. new_len+=msg->rcv.bind_address->address_str.len; \
  584. if (msg->rcv.bind_address->address.af!=AF_INET) \
  585. new_len+=2; \
  586. if (msg->rcv.bind_address->port_no!=SIP_PORT){ \
  587. /* add :port_no */ \
  588. new_len+=1+msg->rcv.bind_address->port_no_str.len; \
  589. }\
  590. /*add;transport=xxx*/ \
  591. switch(msg->rcv.bind_address->proto){ \
  592. case PROTO_NONE: \
  593. case PROTO_UDP: \
  594. break; /* udp is the default */ \
  595. case PROTO_TCP: \
  596. case PROTO_TLS: \
  597. new_len+=TRANSPORT_PARAM_LEN+3; \
  598. break; \
  599. case PROTO_SCTP: \
  600. new_len+=TRANSPORT_PARAM_LEN+4; \
  601. break; \
  602. default: \
  603. LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
  604. msg->rcv.bind_address->proto); \
  605. }\
  606. RCVCOMP_LUMP_LEN \
  607. }else{ \
  608. /* FIXME */ \
  609. LOG(L_CRIT, "FIXME: null bind_address\n"); \
  610. }; \
  611. break; \
  612. case SUBST_SND_IP: \
  613. if (send_sock){ \
  614. new_len+=send_address_str->len; \
  615. if (send_sock->address.af!=AF_INET && \
  616. send_address_str==&(send_sock->address_str)) \
  617. new_len+=2; \
  618. }else{ \
  619. LOG(L_CRIT, "FIXME: lumps_len called with" \
  620. " null send_sock\n"); \
  621. }; \
  622. break; \
  623. case SUBST_SND_PORT: \
  624. if (send_sock){ \
  625. new_len+=send_port_str->len; \
  626. }else{ \
  627. LOG(L_CRIT, "FIXME: lumps_len called with" \
  628. " null send_sock\n"); \
  629. }; \
  630. break; \
  631. case SUBST_SND_PROTO: \
  632. if (send_sock){ \
  633. switch(send_sock->proto){ \
  634. case PROTO_NONE: \
  635. case PROTO_UDP: \
  636. case PROTO_TCP: \
  637. case PROTO_TLS: \
  638. new_len+=3; \
  639. break; \
  640. case PROTO_SCTP: \
  641. new_len+=4; \
  642. break; \
  643. default: \
  644. LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
  645. send_sock->proto); \
  646. }\
  647. }else{ \
  648. LOG(L_CRIT, "FIXME: lumps_len called with" \
  649. " null send_sock\n"); \
  650. }; \
  651. break; \
  652. case SUBST_SND_ALL: \
  653. if (send_sock){ \
  654. new_len+=send_address_str->len; \
  655. if ((send_sock->address.af!=AF_INET) && \
  656. (send_address_str==&(send_sock->address_str))) \
  657. new_len+=2; \
  658. if ((send_sock->port_no!=SIP_PORT) || \
  659. (send_port_str!=&(send_sock->port_no_str))){ \
  660. /* add :port_no */ \
  661. new_len+=1+send_port_str->len; \
  662. }\
  663. /*add;transport=xxx*/ \
  664. switch(send_sock->proto){ \
  665. case PROTO_NONE: \
  666. case PROTO_UDP: \
  667. break; /* udp is the default */ \
  668. case PROTO_TCP: \
  669. case PROTO_TLS: \
  670. new_len+=TRANSPORT_PARAM_LEN+3; \
  671. break; \
  672. case PROTO_SCTP: \
  673. new_len+=TRANSPORT_PARAM_LEN+4; \
  674. break; \
  675. default: \
  676. LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
  677. send_sock->proto); \
  678. }\
  679. SENDCOMP_LUMP_LEN \
  680. }else{ \
  681. /* FIXME */ \
  682. LOG(L_CRIT, "FIXME: lumps_len called with" \
  683. " null send_sock\n"); \
  684. }; \
  685. break; \
  686. case SUBST_NOP: /* do nothing */ \
  687. break; \
  688. default: \
  689. LOG(L_CRIT, "BUG: unknown subst type %d\n", \
  690. (subst_l)->u.subst); \
  691. }
  692. if (send_info){
  693. send_sock=send_info->send_sock;
  694. }else{
  695. send_sock=0;
  696. };
  697. s_offset=0;
  698. new_len=0;
  699. /* init send_address_str & send_port_str */
  700. if (msg->set_global_address.len)
  701. send_address_str=&(msg->set_global_address);
  702. else
  703. send_address_str=&(send_sock->address_str);
  704. if (msg->set_global_port.len)
  705. send_port_str=&(msg->set_global_port);
  706. else
  707. send_port_str=&(send_sock->port_no_str);
  708. for(t=lumps;t;t=t->next){
  709. /* skip if this is an OPT lump and the condition is not satisfied */
  710. if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t->u.cond, msg, send_info))
  711. continue;
  712. for(r=t->before;r;r=r->before){
  713. switch(r->op){
  714. case LUMP_ADD:
  715. new_len+=r->len;
  716. break;
  717. case LUMP_ADD_SUBST:
  718. SUBST_LUMP_LEN(r);
  719. break;
  720. case LUMP_ADD_OPT:
  721. /* skip if this is an OPT lump and the condition is
  722. * not satisfied */
  723. if (!lump_check_opt(r->u.cond, msg, send_info))
  724. goto skip_before;
  725. break;
  726. default:
  727. /* only ADD allowed for before/after */
  728. LOG(L_CRIT, "BUG: lumps_len: invalid op "
  729. "for data lump (%x)\n", r->op);
  730. }
  731. }
  732. skip_before:
  733. switch(t->op){
  734. case LUMP_ADD:
  735. new_len+=t->len;
  736. break;
  737. case LUMP_ADD_SUBST:
  738. SUBST_LUMP_LEN(t);
  739. break;
  740. case LUMP_ADD_OPT:
  741. /* we don't do anything here, it's only a condition for
  742. * before & after */
  743. break;
  744. case LUMP_DEL:
  745. /* fix overlapping deleted zones */
  746. if (t->u.offset < s_offset){
  747. /* change len */
  748. if (t->len>s_offset-t->u.offset)
  749. t->len-=s_offset-t->u.offset;
  750. else t->len=0;
  751. t->u.offset=s_offset;
  752. }
  753. s_offset=t->u.offset+t->len;
  754. new_len-=t->len;
  755. break;
  756. case LUMP_NOP:
  757. /* fix offset if overlapping on a deleted zone */
  758. if (t->u.offset < s_offset){
  759. t->u.offset=s_offset;
  760. }else
  761. s_offset=t->u.offset;
  762. /* do nothing */
  763. break;
  764. default:
  765. LOG(L_CRIT,"BUG:lumps_len: invalid"
  766. " op for data lump (%x)\n", r->op);
  767. }
  768. for (r=t->after;r;r=r->after){
  769. switch(r->op){
  770. case LUMP_ADD:
  771. new_len+=r->len;
  772. break;
  773. case LUMP_ADD_SUBST:
  774. SUBST_LUMP_LEN(r);
  775. break;
  776. case LUMP_ADD_OPT:
  777. /* skip if this is an OPT lump and the condition is
  778. * not satisfied */
  779. if (!lump_check_opt(r->u.cond, msg, send_info))
  780. goto skip_after;
  781. break;
  782. default:
  783. /* only ADD allowed for before/after */
  784. LOG(L_CRIT, "BUG:lumps_len: invalid"
  785. " op for data lump (%x)\n", r->op);
  786. }
  787. }
  788. skip_after:
  789. ; /* to make gcc 3.* happy */
  790. }
  791. return new_len;
  792. #undef RCVCOMP_LUMP_LEN
  793. #undef SENDCOMP_LUMP_LEN
  794. }
  795. /* another helper functions, adds/Removes the lump,
  796. code moved form build_req_from_req */
  797. static inline void process_lumps( struct sip_msg* msg,
  798. struct lump* lumps,
  799. char* new_buf,
  800. unsigned int* new_buf_offs,
  801. unsigned int* orig_offs,
  802. struct dest_info* send_info)
  803. {
  804. struct lump *t;
  805. struct lump *r;
  806. char* orig;
  807. int size;
  808. int offset;
  809. int s_offset;
  810. str* send_address_str;
  811. str* send_port_str;
  812. struct socket_info* send_sock;
  813. #ifdef USE_COMP
  814. #define RCVCOMP_PARAM_ADD \
  815. /* add ;comp=xxxx */ \
  816. switch(msg->rcv.comp){ \
  817. case COMP_NONE: \
  818. break; \
  819. case COMP_SIGCOMP: \
  820. memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
  821. offset+=COMP_PARAM_LEN; \
  822. memcpy(new_buf+offset, SIGCOMP_NAME, \
  823. SIGCOMP_NAME_LEN); \
  824. offset+=SIGCOMP_NAME_LEN; \
  825. break; \
  826. case COMP_SERGZ: \
  827. memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
  828. offset+=COMP_PARAM_LEN; \
  829. memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
  830. offset+=SERGZ_NAME_LEN; \
  831. break;\
  832. default:\
  833. LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
  834. msg->rcv.comp); \
  835. }
  836. #define SENDCOMP_PARAM_ADD \
  837. /* add ;comp=xxxx */ \
  838. switch(send_info->comp){ \
  839. case COMP_NONE: \
  840. break; \
  841. case COMP_SIGCOMP: \
  842. memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
  843. offset+=COMP_PARAM_LEN; \
  844. memcpy(new_buf+offset, SIGCOMP_NAME, \
  845. SIGCOMP_NAME_LEN); \
  846. offset+=SIGCOMP_NAME_LEN; \
  847. break; \
  848. case COMP_SERGZ: \
  849. memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
  850. offset+=COMP_PARAM_LEN; \
  851. memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
  852. offset+=SERGZ_NAME_LEN; \
  853. break;\
  854. default:\
  855. LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
  856. msg->rcv.comp); \
  857. }
  858. #else
  859. #define RCVCOMP_PARAM_ADD
  860. #define SENDCOMP_PARAM_ADD
  861. #endif /* USE_COMP */
  862. #define SUBST_LUMP(subst_l) \
  863. switch((subst_l)->u.subst){ \
  864. case SUBST_RCV_IP: \
  865. if (msg->rcv.bind_address){ \
  866. if (msg->rcv.bind_address->address.af!=AF_INET){\
  867. new_buf[offset]='['; offset++; \
  868. }\
  869. memcpy(new_buf+offset, msg->rcv.bind_address->address_str.s, \
  870. msg->rcv.bind_address->address_str.len); \
  871. offset+=msg->rcv.bind_address->address_str.len; \
  872. if (msg->rcv.bind_address->address.af!=AF_INET){\
  873. new_buf[offset]=']'; offset++; \
  874. }\
  875. }else{ \
  876. /*FIXME*/ \
  877. LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
  878. }; \
  879. break; \
  880. case SUBST_RCV_PORT: \
  881. if (msg->rcv.bind_address){ \
  882. memcpy(new_buf+offset, msg->rcv.bind_address->port_no_str.s, \
  883. msg->rcv.bind_address->port_no_str.len); \
  884. offset+=msg->rcv.bind_address->port_no_str.len; \
  885. }else{ \
  886. /*FIXME*/ \
  887. LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
  888. }; \
  889. break; \
  890. case SUBST_RCV_ALL: \
  891. if (msg->rcv.bind_address){ \
  892. /* address */ \
  893. if (msg->rcv.bind_address->address.af!=AF_INET){\
  894. new_buf[offset]='['; offset++; \
  895. }\
  896. memcpy(new_buf+offset, msg->rcv.bind_address->address_str.s, \
  897. msg->rcv.bind_address->address_str.len); \
  898. offset+=msg->rcv.bind_address->address_str.len; \
  899. if (msg->rcv.bind_address->address.af!=AF_INET){\
  900. new_buf[offset]=']'; offset++; \
  901. }\
  902. /* :port */ \
  903. if (msg->rcv.bind_address->port_no!=SIP_PORT){ \
  904. new_buf[offset]=':'; offset++; \
  905. memcpy(new_buf+offset, \
  906. msg->rcv.bind_address->port_no_str.s, \
  907. msg->rcv.bind_address->port_no_str.len); \
  908. offset+=msg->rcv.bind_address->port_no_str.len; \
  909. }\
  910. switch(msg->rcv.bind_address->proto){ \
  911. case PROTO_NONE: \
  912. case PROTO_UDP: \
  913. break; /* nothing to do, udp is default*/ \
  914. case PROTO_TCP: \
  915. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  916. TRANSPORT_PARAM_LEN); \
  917. offset+=TRANSPORT_PARAM_LEN; \
  918. memcpy(new_buf+offset, "tcp", 3); \
  919. offset+=3; \
  920. break; \
  921. case PROTO_TLS: \
  922. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  923. TRANSPORT_PARAM_LEN); \
  924. offset+=TRANSPORT_PARAM_LEN; \
  925. memcpy(new_buf+offset, "tls", 3); \
  926. offset+=3; \
  927. break; \
  928. case PROTO_SCTP: \
  929. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  930. TRANSPORT_PARAM_LEN); \
  931. offset+=TRANSPORT_PARAM_LEN; \
  932. memcpy(new_buf+offset, "sctp", 4); \
  933. offset+=4; \
  934. break; \
  935. default: \
  936. LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
  937. msg->rcv.bind_address->proto); \
  938. } \
  939. RCVCOMP_PARAM_ADD \
  940. }else{ \
  941. /*FIXME*/ \
  942. LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
  943. }; \
  944. break; \
  945. case SUBST_SND_IP: \
  946. if (send_sock){ \
  947. if ((send_sock->address.af!=AF_INET) && \
  948. (send_address_str==&(send_sock->address_str))){\
  949. new_buf[offset]='['; offset++; \
  950. }\
  951. memcpy(new_buf+offset, send_address_str->s, \
  952. send_address_str->len); \
  953. offset+=send_address_str->len; \
  954. if ((send_sock->address.af!=AF_INET) && \
  955. (send_address_str==&(send_sock->address_str))){\
  956. new_buf[offset]=']'; offset++; \
  957. }\
  958. }else{ \
  959. /*FIXME*/ \
  960. LOG(L_CRIT, "FIXME: process_lumps: called with" \
  961. " null send_sock\n"); \
  962. }; \
  963. break; \
  964. case SUBST_SND_PORT: \
  965. if (send_sock){ \
  966. memcpy(new_buf+offset, send_port_str->s, \
  967. send_port_str->len); \
  968. offset+=send_port_str->len; \
  969. }else{ \
  970. /*FIXME*/ \
  971. LOG(L_CRIT, "FIXME: process_lumps: called with" \
  972. " null send_sock\n"); \
  973. }; \
  974. break; \
  975. case SUBST_SND_ALL: \
  976. if (send_sock){ \
  977. /* address */ \
  978. if ((send_sock->address.af!=AF_INET) && \
  979. (send_address_str==&(send_sock->address_str))){\
  980. new_buf[offset]='['; offset++; \
  981. }\
  982. memcpy(new_buf+offset, send_address_str->s, \
  983. send_address_str->len); \
  984. offset+=send_address_str->len; \
  985. if ((send_sock->address.af!=AF_INET) && \
  986. (send_address_str==&(send_sock->address_str))){\
  987. new_buf[offset]=']'; offset++; \
  988. }\
  989. /* :port */ \
  990. if ((send_sock->port_no!=SIP_PORT) || \
  991. (send_port_str!=&(send_sock->port_no_str))){ \
  992. new_buf[offset]=':'; offset++; \
  993. memcpy(new_buf+offset, send_port_str->s, \
  994. send_port_str->len); \
  995. offset+=send_port_str->len; \
  996. }\
  997. switch(send_sock->proto){ \
  998. case PROTO_NONE: \
  999. case PROTO_UDP: \
  1000. break; /* nothing to do, udp is default*/ \
  1001. case PROTO_TCP: \
  1002. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  1003. TRANSPORT_PARAM_LEN); \
  1004. offset+=TRANSPORT_PARAM_LEN; \
  1005. memcpy(new_buf+offset, "tcp", 3); \
  1006. offset+=3; \
  1007. break; \
  1008. case PROTO_TLS: \
  1009. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  1010. TRANSPORT_PARAM_LEN); \
  1011. offset+=TRANSPORT_PARAM_LEN; \
  1012. memcpy(new_buf+offset, "tls", 3); \
  1013. offset+=3; \
  1014. break; \
  1015. case PROTO_SCTP: \
  1016. memcpy(new_buf+offset, TRANSPORT_PARAM, \
  1017. TRANSPORT_PARAM_LEN); \
  1018. offset+=TRANSPORT_PARAM_LEN; \
  1019. memcpy(new_buf+offset, "sctp", 4); \
  1020. offset+=4; \
  1021. break; \
  1022. default: \
  1023. LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
  1024. send_sock->proto); \
  1025. } \
  1026. SENDCOMP_PARAM_ADD \
  1027. }else{ \
  1028. /*FIXME*/ \
  1029. LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
  1030. }; \
  1031. break; \
  1032. case SUBST_RCV_PROTO: \
  1033. if (msg->rcv.bind_address){ \
  1034. switch(msg->rcv.bind_address->proto){ \
  1035. case PROTO_NONE: \
  1036. case PROTO_UDP: \
  1037. memcpy(new_buf+offset, "udp", 3); \
  1038. offset+=3; \
  1039. break; \
  1040. case PROTO_TCP: \
  1041. memcpy(new_buf+offset, "tcp", 3); \
  1042. offset+=3; \
  1043. break; \
  1044. case PROTO_TLS: \
  1045. memcpy(new_buf+offset, "tls", 3); \
  1046. offset+=3; \
  1047. break; \
  1048. case PROTO_SCTP: \
  1049. memcpy(new_buf+offset, "sctp", 4); \
  1050. offset+=4; \
  1051. break; \
  1052. default: \
  1053. LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
  1054. msg->rcv.bind_address->proto); \
  1055. } \
  1056. }else{ \
  1057. /*FIXME*/ \
  1058. LOG(L_CRIT, "FIXME: process_lumps: called with null" \
  1059. " send_sock \n"); \
  1060. }; \
  1061. break; \
  1062. case SUBST_SND_PROTO: \
  1063. if (send_sock){ \
  1064. switch(send_sock->proto){ \
  1065. case PROTO_NONE: \
  1066. case PROTO_UDP: \
  1067. memcpy(new_buf+offset, "udp", 3); \
  1068. offset+=3; \
  1069. break; \
  1070. case PROTO_TCP: \
  1071. memcpy(new_buf+offset, "tcp", 3); \
  1072. offset+=3; \
  1073. break; \
  1074. case PROTO_TLS: \
  1075. memcpy(new_buf+offset, "tls", 3); \
  1076. offset+=3; \
  1077. break; \
  1078. case PROTO_SCTP: \
  1079. memcpy(new_buf+offset, "sctp", 4); \
  1080. offset+=4; \
  1081. break; \
  1082. default: \
  1083. LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
  1084. send_sock->proto); \
  1085. } \
  1086. }else{ \
  1087. /*FIXME*/ \
  1088. LOG(L_CRIT, "FIXME: process_lumps: called with null" \
  1089. " send_sock \n"); \
  1090. }; \
  1091. break; \
  1092. default: \
  1093. LOG(L_CRIT, "BUG: process_lumps: unknown subst type %d\n", \
  1094. (subst_l)->u.subst); \
  1095. }
  1096. if (send_info){
  1097. send_sock=send_info->send_sock;
  1098. }else{
  1099. send_sock=0;
  1100. }
  1101. /* init send_address_str & send_port_str */
  1102. if (msg->set_global_address.len)
  1103. send_address_str=&(msg->set_global_address);
  1104. else
  1105. send_address_str=&(send_sock->address_str);
  1106. if (msg->set_global_port.len)
  1107. send_port_str=&(msg->set_global_port);
  1108. else
  1109. send_port_str=&(send_sock->port_no_str);
  1110. orig=msg->buf;
  1111. offset=*new_buf_offs;
  1112. s_offset=*orig_offs;
  1113. for (t=lumps;t;t=t->next){
  1114. switch(t->op){
  1115. case LUMP_ADD:
  1116. case LUMP_ADD_SUBST:
  1117. case LUMP_ADD_OPT:
  1118. /* skip if this is an OPT lump and the condition is
  1119. * not satisfied */
  1120. if ((t->op==LUMP_ADD_OPT) &&
  1121. (!lump_check_opt(t->u.cond, msg, send_info)))
  1122. continue;
  1123. /* just add it here! */
  1124. /* process before */
  1125. for(r=t->before;r;r=r->before){
  1126. switch (r->op){
  1127. case LUMP_ADD:
  1128. /*just add it here*/
  1129. memcpy(new_buf+offset, r->u.value, r->len);
  1130. offset+=r->len;
  1131. break;
  1132. case LUMP_ADD_SUBST:
  1133. SUBST_LUMP(r);
  1134. break;
  1135. case LUMP_ADD_OPT:
  1136. /* skip if this is an OPT lump and the condition is
  1137. * not satisfied */
  1138. if (!lump_check_opt(r->u.cond, msg, send_info))
  1139. goto skip_before;
  1140. break;
  1141. default:
  1142. /* only ADD allowed for before/after */
  1143. LOG(L_CRIT, "BUG:process_lumps: "
  1144. "invalid op for data lump (%x)\n", r->op);
  1145. }
  1146. }
  1147. skip_before:
  1148. /* copy "main" part */
  1149. switch(t->op){
  1150. case LUMP_ADD:
  1151. memcpy(new_buf+offset, t->u.value, t->len);
  1152. offset+=t->len;
  1153. break;
  1154. case LUMP_ADD_SUBST:
  1155. SUBST_LUMP(t);
  1156. break;
  1157. case LUMP_ADD_OPT:
  1158. /* do nothing, it's only a condition */
  1159. break;
  1160. default:
  1161. /* should not ever get here */
  1162. LOG(L_CRIT, "BUG: process_lumps: unhandled data lump "
  1163. " op %d\n", t->op);
  1164. }
  1165. /* process after */
  1166. for(r=t->after;r;r=r->after){
  1167. switch (r->op){
  1168. case LUMP_ADD:
  1169. /*just add it here*/
  1170. memcpy(new_buf+offset, r->u.value, r->len);
  1171. offset+=r->len;
  1172. break;
  1173. case LUMP_ADD_SUBST:
  1174. SUBST_LUMP(r);
  1175. break;
  1176. case LUMP_ADD_OPT:
  1177. /* skip if this is an OPT lump and the condition is
  1178. * not satisfied */
  1179. if (!lump_check_opt(r->u.cond, msg, send_info))
  1180. goto skip_after;
  1181. break;
  1182. default:
  1183. /* only ADD allowed for before/after */
  1184. LOG(L_CRIT, "BUG:process_lumps: "
  1185. "invalid op for data lump (%x)\n", r->op);
  1186. }
  1187. }
  1188. skip_after:
  1189. break;
  1190. case LUMP_NOP:
  1191. case LUMP_DEL:
  1192. /* copy till offset */
  1193. if (s_offset>t->u.offset){
  1194. DBG("Warning: (%d) overlapped lumps offsets,"
  1195. " ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);
  1196. /* this should've been fixed above (when computing len) */
  1197. /* just ignore it*/
  1198. break;
  1199. }
  1200. size=t->u.offset-s_offset;
  1201. if (size){
  1202. memcpy(new_buf+offset, orig+s_offset,size);
  1203. offset+=size;
  1204. s_offset+=size;
  1205. }
  1206. /* process before */
  1207. for(r=t->before;r;r=r->before){
  1208. switch (r->op){
  1209. case LUMP_ADD:
  1210. /*just add it here*/
  1211. memcpy(new_buf+offset, r->u.value, r->len);
  1212. offset+=r->len;
  1213. break;
  1214. case LUMP_ADD_SUBST:
  1215. SUBST_LUMP(r);
  1216. break;
  1217. case LUMP_ADD_OPT:
  1218. /* skip if this is an OPT lump and the condition is
  1219. * not satisfied */
  1220. if (!lump_check_opt(r->u.cond, msg, send_info))
  1221. goto skip_nop_before;
  1222. break;
  1223. default:
  1224. /* only ADD allowed for before/after */
  1225. LOG(L_CRIT, "BUG:process_lumps: "
  1226. "invalid op for data lump (%x)\n",r->op);
  1227. }
  1228. }
  1229. skip_nop_before:
  1230. /* process main (del only) */
  1231. if (t->op==LUMP_DEL){
  1232. /* skip len bytes from orig msg */
  1233. s_offset+=t->len;
  1234. }
  1235. /* process after */
  1236. for(r=t->after;r;r=r->after){
  1237. switch (r->op){
  1238. case LUMP_ADD:
  1239. /*just add it here*/
  1240. memcpy(new_buf+offset, r->u.value, r->len);
  1241. offset+=r->len;
  1242. break;
  1243. case LUMP_ADD_SUBST:
  1244. SUBST_LUMP(r);
  1245. break;
  1246. case LUMP_ADD_OPT:
  1247. /* skip if this is an OPT lump and the condition is
  1248. * not satisfied */
  1249. if (!lump_check_opt(r->u.cond, msg, send_info))
  1250. goto skip_nop_after;
  1251. break;
  1252. default:
  1253. /* only ADD allowed for before/after */
  1254. LOG(L_CRIT, "BUG:process_lumps: "
  1255. "invalid op for data lump (%x)\n", r->op);
  1256. }
  1257. }
  1258. skip_nop_after:
  1259. break;
  1260. default:
  1261. LOG(L_CRIT, "BUG: process_lumps: "
  1262. "unknown op (%x)\n", t->op);
  1263. }
  1264. }
  1265. *new_buf_offs=offset;
  1266. *orig_offs=s_offset;
  1267. #undef RCVCOMP_PARAM_ADD
  1268. #undef SENDCOMP_PARAM_ADD
  1269. }
  1270. /*
  1271. * Adjust/insert Content-Length if necessary
  1272. */
  1273. static inline int adjust_clen(struct sip_msg* msg, int body_delta, int proto)
  1274. {
  1275. struct lump* anchor;
  1276. char* clen_buf;
  1277. int clen_len, body_only;
  1278. #ifdef USE_TCP
  1279. char* body;
  1280. int comp_clen;
  1281. #endif /* USE_TCP */
  1282. /* Calculate message length difference caused by lumps modifying message
  1283. * body, from this point on the message body must not be modified. Zero
  1284. * value indicates that the body hasn't been modified
  1285. */
  1286. clen_buf = 0;
  1287. anchor=0;
  1288. body_only=1;
  1289. /* check to see if we need to add clen */
  1290. #ifdef USE_TCP
  1291. if (proto == PROTO_TCP
  1292. #ifdef USE_TLS
  1293. || proto == PROTO_TLS
  1294. #endif
  1295. ) {
  1296. if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0)==-1){
  1297. LOG(L_ERR, "adjust_clen: error parsing content-length\n");
  1298. goto error;
  1299. }
  1300. if (unlikely(msg->content_length==0)){
  1301. /* not present, we need to add it */
  1302. /* msg->unparsed should point just before the final crlf
  1303. * - whole message was parsed by the above parse_headers
  1304. * which did not find content-length */
  1305. anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
  1306. HDR_CONTENTLENGTH_T);
  1307. if (anchor==0){
  1308. LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
  1309. goto error;
  1310. }
  1311. body_only=0;
  1312. }else{
  1313. /* compute current content length and compare it with the
  1314. one in the message */
  1315. body=get_body(msg);
  1316. if (unlikely(body==0)){
  1317. ser_error=E_BAD_REQ;
  1318. LOG(L_ERR, "adjust_clen: no message body found"
  1319. " (missing crlf?)");
  1320. goto error;
  1321. }
  1322. comp_clen=msg->len-(int)(body-msg->buf)+body_delta;
  1323. if (comp_clen!=(int)(long)msg->content_length->parsed){
  1324. /* note: we don't distinguish here between received with
  1325. wrong content-length and content-length changed, we just
  1326. fix it automatically in both cases (the reason being
  1327. that an error message telling we have received a msg-
  1328. with wrong content-length is of very little use) */
  1329. anchor = del_lump(msg, msg->content_length->body.s-msg->buf,
  1330. msg->content_length->body.len,
  1331. HDR_CONTENTLENGTH_T);
  1332. if (anchor==0) {
  1333. LOG(L_ERR, "adjust_clen: Can't remove original"
  1334. " Content-Length\n");
  1335. goto error;
  1336. }
  1337. body_only=1;
  1338. }
  1339. }
  1340. }else
  1341. #endif /* USE_TCP */
  1342. if (body_delta){
  1343. if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1) {
  1344. LOG(L_ERR, "adjust_clen: Error parsing Content-Length\n");
  1345. goto error;
  1346. }
  1347. /* The body has been changed, try to find
  1348. * existing Content-Length
  1349. */
  1350. /* no need for Content-Length if it's and UDP packet and
  1351. * it hasn't Content-Length already */
  1352. if ((msg->content_length==0)){
  1353. /* content-length doesn't exist, append it */
  1354. /* msg->unparsed should point just before the final crlf
  1355. * - whole message was parsed by the above parse_headers
  1356. * which did not find content-length */
  1357. if (proto!=PROTO_UDP){
  1358. anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
  1359. HDR_CONTENTLENGTH_T);
  1360. if (anchor==0){
  1361. LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
  1362. goto error;
  1363. }
  1364. body_only=0;
  1365. } /* else
  1366. DBG("adjust_clen: UDP packet with no clen => "
  1367. "not adding one \n"); */
  1368. }else{
  1369. /* Content-Length has been found, remove it */
  1370. anchor = del_lump( msg, msg->content_length->body.s - msg->buf,
  1371. msg->content_length->body.len,
  1372. HDR_CONTENTLENGTH_T);
  1373. if (anchor==0) {
  1374. LOG(L_ERR, "adjust_clen: Can't remove original"
  1375. " Content-Length\n");
  1376. goto error;
  1377. }
  1378. }
  1379. }
  1380. if (anchor){
  1381. clen_buf = clen_builder(msg, &clen_len, body_delta, body_only);
  1382. if (!clen_buf) goto error;
  1383. if (insert_new_lump_after(anchor, clen_buf, clen_len,
  1384. HDR_CONTENTLENGTH_T) == 0)
  1385. goto error;
  1386. }
  1387. return 0;
  1388. error:
  1389. if (clen_buf) pkg_free(clen_buf);
  1390. return -1;
  1391. }
  1392. /** builds a request in memory from another sip request.
  1393. *
  1394. * Side-effects: - it adds lumps to the msg which are _not_ cleaned.
  1395. * All the added lumps are HDR_VIA_T.
  1396. * - it might change send_info->proto and send_info->send_socket
  1397. * if proto fallback is enabled (see below).
  1398. *
  1399. * Uses also global_req_flags ( OR'ed with msg->msg_flags, see send_info
  1400. * below).
  1401. *
  1402. * @param msg - sip message structure, complete with lumps
  1403. * @param returned_len - result length (filled in)
  1404. * @param send_info - dest_info structure (value/result), contains where the
  1405. * packet will be sent to (it's needed for building a
  1406. * correct via, fill RR lumps a.s.o.). If MTU based
  1407. * protocol fall-back is enabled (see flags below),
  1408. * send_info->proto might be updated with the new
  1409. * protocol.
  1410. * msg->msg_flags used:
  1411. * - FL_TCP_MTU_FB, FL_TLS_MTU_FB and FL_SCTP_MTU_FB -
  1412. * fallback to the corresp. proto if the built
  1413. * message > mtu and send_info->proto==PROTO_UDP.
  1414. * It will also update send_info->proto.
  1415. * - FL_FORCE_RPORT: add rport to via
  1416. *
  1417. * @return pointer to the new request (pkg_malloc'ed, needs freeing when
  1418. * done) and sets returned_len or 0 on error.
  1419. */
  1420. char * build_req_buf_from_sip_req( struct sip_msg* msg,
  1421. unsigned int *returned_len,
  1422. struct dest_info* send_info
  1423. )
  1424. {
  1425. unsigned int len, new_len, received_len, rport_len, uri_len, via_len,
  1426. body_delta;
  1427. char* line_buf;
  1428. char* received_buf;
  1429. char* rport_buf;
  1430. char* new_buf;
  1431. char* buf;
  1432. unsigned int offset, s_offset, size;
  1433. struct lump* via_anchor;
  1434. struct lump* via_lump;
  1435. struct lump* via_insert_param;
  1436. str branch;
  1437. unsigned int flags;
  1438. unsigned int udp_mtu;
  1439. struct dest_info di;
  1440. via_insert_param=0;
  1441. uri_len=0;
  1442. buf=msg->buf;
  1443. len=msg->len;
  1444. received_len=0;
  1445. rport_len=0;
  1446. new_buf=0;
  1447. received_buf=0;
  1448. rport_buf=0;
  1449. line_buf=0;
  1450. flags=msg->msg_flags|global_req_flags;
  1451. /* Calculate message body difference and adjust Content-Length */
  1452. body_delta = lumps_len(msg, msg->body_lumps, send_info);
  1453. if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
  1454. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
  1455. " Content-Length\n");
  1456. goto error00;
  1457. }
  1458. /* create the via header */
  1459. branch.s=msg->add_to_branch_s;
  1460. branch.len=msg->add_to_branch_len;
  1461. line_buf = create_via_hf( &via_len, msg, send_info, &branch);
  1462. if (!line_buf){
  1463. LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
  1464. "memory allocation failure\n");
  1465. goto error00;
  1466. }
  1467. /* check if received needs to be added */
  1468. if ( received_test(msg) ) {
  1469. if ((received_buf=received_builder(msg,&received_len))==0){
  1470. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
  1471. " received_builder failed\n");
  1472. goto error01; /* free also line_buf */
  1473. }
  1474. }
  1475. /* check if rport needs to be updated:
  1476. * - if FL_FORCE_RPORT is set add it (and del. any previous version)
  1477. * - if via already contains an rport add it and overwrite the previous
  1478. * rport value if present (if you don't want to overwrite the previous
  1479. * version remove the comments) */
  1480. if ((flags&FL_FORCE_RPORT)||
  1481. (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
  1482. if ((rport_buf=rport_builder(msg, &rport_len))==0){
  1483. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
  1484. " rport_builder failed\n");
  1485. goto error01; /* free everything */
  1486. }
  1487. }
  1488. /* find out where the offset of the first parameter that should be added
  1489. * (after host:port), needed by add receive & maybe rport */
  1490. if (msg->via1->params.s){
  1491. size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
  1492. for ';' */
  1493. }else{
  1494. size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
  1495. if (msg->via1->port!=0){
  1496. /*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
  1497. size += msg->via1->port_str.len + 1; /* +1 for ':'*/
  1498. }
  1499. #if 0
  1500. /* no longer necessary, now hots.s contains [] */
  1501. #ifdef USE_IPV6
  1502. if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
  1503. #endif
  1504. #endif
  1505. }
  1506. /* if received needs to be added, add anchor after host and add it, or
  1507. * overwrite the previous one if already present */
  1508. if (received_len){
  1509. if (msg->via1->received){ /* received already present => overwrite it*/
  1510. via_insert_param=del_lump(msg,
  1511. msg->via1->received->start-buf-1, /*;*/
  1512. msg->via1->received->size+1, /*;*/ HDR_VIA_T);
  1513. }else if (via_insert_param==0){ /* receive not present, ok */
  1514. via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
  1515. HDR_VIA_T);
  1516. }
  1517. if (via_insert_param==0) goto error02; /* free received_buf */
  1518. if (insert_new_lump_after(via_insert_param, received_buf, received_len,
  1519. HDR_VIA_T) ==0 ) goto error02; /* free received_buf */
  1520. }
  1521. /* if rport needs to be updated, delete it if present and add it's value */
  1522. if (rport_len){
  1523. if (msg->via1->rport){ /* rport already present */
  1524. via_insert_param=del_lump(msg,
  1525. msg->via1->rport->start-buf-1, /*';'*/
  1526. msg->via1->rport->size+1 /* ; */, HDR_VIA_T);
  1527. }else if (via_insert_param==0){ /*force rport, no rport present */
  1528. /* no rport, add it */
  1529. via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
  1530. HDR_VIA_T);
  1531. }
  1532. if (via_insert_param==0) goto error03; /* free rport_buf */
  1533. if (insert_new_lump_after(via_insert_param, rport_buf, rport_len,
  1534. HDR_VIA_T) ==0 )
  1535. goto error03; /* free rport_buf */
  1536. }
  1537. /* compute new msg len and fix overlapping zones*/
  1538. new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len;
  1539. #ifdef XL_DEBUG
  1540. LOG(L_ERR, "DEBUG: new_len(%d)=len(%d)+lumps_len\n", new_len, len);
  1541. #endif
  1542. udp_mtu=cfg_get(core, core_cfg, udp_mtu);
  1543. di.proto=PROTO_NONE;
  1544. if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu &&
  1545. (flags & FL_MTU_FB_MASK) && (new_len>udp_mtu))){
  1546. di=*send_info; /* copy whole struct - will be used in the Via builder */
  1547. di.proto=PROTO_NONE; /* except the proto */
  1548. #ifdef USE_TCP
  1549. if (!tcp_disable && (flags & FL_MTU_TCP_FB) &&
  1550. (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TCP))){
  1551. di.proto=PROTO_TCP;
  1552. }
  1553. #ifdef USE_TLS
  1554. else if (!tls_disable && (flags & FL_MTU_TLS_FB) &&
  1555. (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TLS))){
  1556. di.proto=PROTO_TLS;
  1557. }
  1558. #endif /* USE_TLS */
  1559. #endif /* USE_TCP */
  1560. #ifdef USE_SCTP
  1561. #ifdef USE_TCP
  1562. else
  1563. #endif /* USE_TCP */
  1564. if (!sctp_disable && (flags & FL_MTU_SCTP_FB) &&
  1565. (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_SCTP))){
  1566. di.proto=PROTO_SCTP;
  1567. }
  1568. #endif /* USE_SCTP */
  1569. if (di.proto!=PROTO_NONE){
  1570. new_len-=via_len;
  1571. pkg_free(line_buf);
  1572. line_buf = create_via_hf( &via_len, msg, &di, &branch);
  1573. if (!line_buf){
  1574. LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
  1575. "memory allocation failure!\n");
  1576. goto error00;
  1577. }
  1578. new_len+=via_len;
  1579. }
  1580. }
  1581. /* add via header to the list */
  1582. /* try to add it before msg. 1st via */
  1583. /* add first via, as an anchor for second via*/
  1584. via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
  1585. if (via_anchor==0) goto error04;
  1586. if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
  1587. HDR_VIA_T))==0)
  1588. goto error04;
  1589. if (msg->new_uri.s){
  1590. uri_len=msg->new_uri.len;
  1591. new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
  1592. }
  1593. new_buf=(char*)pkg_malloc(new_len+1);
  1594. if (new_buf==0){
  1595. ser_error=E_OUT_OF_MEM;
  1596. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
  1597. goto error00;
  1598. }
  1599. offset=s_offset=0;
  1600. if (msg->new_uri.s){
  1601. /* copy message up to uri */
  1602. size=msg->first_line.u.request.uri.s-buf;
  1603. memcpy(new_buf, buf, size);
  1604. offset+=size;
  1605. s_offset+=size;
  1606. /* add our uri */
  1607. memcpy(new_buf+offset, msg->new_uri.s, uri_len);
  1608. offset+=uri_len;
  1609. s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
  1610. }
  1611. new_buf[new_len]=0;
  1612. /* copy msg adding/removing lumps */
  1613. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
  1614. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset,send_info);
  1615. /* copy the rest of the message */
  1616. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  1617. new_buf[new_len]=0;
  1618. /* update the send_info if udp_mtu affected */
  1619. if (di.proto!=PROTO_NONE) {
  1620. send_info->proto=di.proto;
  1621. send_info->send_sock=di.send_sock;
  1622. }
  1623. #ifdef DBG_MSG_QA
  1624. if (new_buf[new_len-1]==0) {
  1625. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: 0 in the end\n");
  1626. abort();
  1627. }
  1628. #endif
  1629. *returned_len=new_len;
  1630. return new_buf;
  1631. error01:
  1632. error02:
  1633. if (received_buf) pkg_free(received_buf);
  1634. error03:
  1635. if (rport_buf) pkg_free(rport_buf);
  1636. error04:
  1637. if (line_buf) pkg_free(line_buf);
  1638. error00:
  1639. *returned_len=0;
  1640. return 0;
  1641. }
  1642. char * build_res_buf_from_sip_res( struct sip_msg* msg,
  1643. unsigned int *returned_len)
  1644. {
  1645. unsigned int new_len, via_len, body_delta;
  1646. char* new_buf;
  1647. unsigned offset, s_offset, via_offset;
  1648. char* buf;
  1649. unsigned int len;
  1650. buf=msg->buf;
  1651. len=msg->len;
  1652. new_buf=0;
  1653. /* we must remove the first via */
  1654. if (msg->via1->next) {
  1655. via_len=msg->via1->bsize;
  1656. via_offset=msg->h_via1->body.s-buf;
  1657. } else {
  1658. via_len=msg->h_via1->len;
  1659. via_offset=msg->h_via1->name.s-buf;
  1660. }
  1661. /* Calculate message body difference and adjust
  1662. * Content-Length
  1663. */
  1664. body_delta = lumps_len(msg, msg->body_lumps, 0);
  1665. if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
  1666. < 0) {
  1667. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
  1668. " Content-Length\n");
  1669. goto error;
  1670. }
  1671. /* remove the first via*/
  1672. if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
  1673. LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
  1674. "via\n");
  1675. goto error;
  1676. }
  1677. new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
  1678. know the send sock */
  1679. DBG(" old size: %d, new size: %d\n", len, new_len);
  1680. new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
  1681. (\0 to print it )*/
  1682. if (new_buf==0){
  1683. LOG(L_ERR, "ERROR: build_res_buf_from_sip_res: out of mem\n");
  1684. goto error;
  1685. }
  1686. new_buf[new_len]=0; /* debug: print the message */
  1687. offset=s_offset=0;
  1688. /*FIXME: no send sock*/
  1689. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, 0);/*FIXME:*/
  1690. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, 0);
  1691. /* copy the rest of the message */
  1692. memcpy(new_buf+offset,
  1693. buf+s_offset,
  1694. len-s_offset);
  1695. /* send it! */
  1696. DBG("build_res_from_sip_res: copied size: orig:%d, new: %d, rest: %d"
  1697. " msg=\n%s\n", s_offset, offset, len-s_offset, new_buf);
  1698. *returned_len=new_len;
  1699. return new_buf;
  1700. error:
  1701. *returned_len=0;
  1702. return 0;
  1703. }
  1704. char * build_res_buf_from_sip_req( unsigned int code, char *text ,str *new_tag,
  1705. struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
  1706. {
  1707. char *buf, *p;
  1708. unsigned int len,foo;
  1709. struct hdr_field *hdr;
  1710. struct lump_rpl *lump;
  1711. struct lump_rpl *body;
  1712. int i;
  1713. char* received_buf;
  1714. unsigned int received_len;
  1715. char* rport_buf;
  1716. unsigned int rport_len;
  1717. char* warning_buf;
  1718. unsigned int warning_len;
  1719. char* content_len_buf;
  1720. unsigned int content_len_len;
  1721. unsigned int text_len;
  1722. char *after_body;
  1723. str to_tag;
  1724. char *totags;
  1725. body = 0;
  1726. buf=0;
  1727. received_buf=rport_buf=warning_buf=content_len_buf=0;
  1728. received_len=rport_len=warning_len=content_len_len=0;
  1729. text_len=strlen(text);
  1730. to_tag.s=0; /* fixes gcc 4.0 warning */
  1731. to_tag.len=0;
  1732. /* force parsing all headers -- we want to return all
  1733. Via's in the reply and they may be scattered down to the
  1734. end of header (non-block Vias are a really poor property
  1735. of SIP :( ) */
  1736. if (parse_headers( msg, HDR_EOH_F, 0 )==-1) {
  1737. LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
  1738. "alas, parse_headers failed\n");
  1739. goto error00;
  1740. }
  1741. /*computes the length of the new response buffer*/
  1742. len = 0;
  1743. /* check if received needs to be added */
  1744. if (received_test(msg)) {
  1745. if ((received_buf=received_builder(msg,&received_len))==0) {
  1746. LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
  1747. "alas, received_builder failed\n");
  1748. goto error00;
  1749. }
  1750. }
  1751. /* check if rport needs to be updated */
  1752. if ( ((msg->msg_flags|global_req_flags)&FL_FORCE_RPORT)||
  1753. (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
  1754. if ((rport_buf=rport_builder(msg, &rport_len))==0){
  1755. LOG(L_ERR, "ERROR: build_res_buf_from_sip_req:"
  1756. " rport_builder failed\n");
  1757. goto error01; /* free everything */
  1758. }
  1759. if (msg->via1->rport)
  1760. len -= msg->via1->rport->size+1; /* include ';' */
  1761. }
  1762. /* first line */
  1763. len += msg->first_line.u.request.version.len + 1/*space*/ + 3/*code*/ + 1/*space*/ +
  1764. text_len + CRLF_LEN/*new line*/;
  1765. /*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
  1766. for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
  1767. switch (hdr->type) {
  1768. case HDR_TO_T:
  1769. if (new_tag && new_tag->len) {
  1770. to_tag=get_to(msg)->tag_value;
  1771. if (to_tag.len )
  1772. len+=new_tag->len-to_tag.len;
  1773. else
  1774. len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
  1775. }
  1776. len += hdr->len;
  1777. break;
  1778. case HDR_VIA_T:
  1779. /* we always add CRLF to via*/
  1780. len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
  1781. if (hdr==msg->h_via1) len += received_len+rport_len;
  1782. break;
  1783. case HDR_RECORDROUTE_T:
  1784. /* RR only for 1xx and 2xx replies */
  1785. if (code<180 || code>=300)
  1786. break;
  1787. case HDR_FROM_T:
  1788. case HDR_CALLID_T:
  1789. case HDR_CSEQ_T:
  1790. /* we keep the original termination for these headers*/
  1791. len += hdr->len;
  1792. break;
  1793. default:
  1794. /* do nothing, we are interested only in the above headers */
  1795. ;
  1796. }
  1797. }
  1798. /* lumps length */
  1799. for(lump=msg->reply_lump;lump;lump=lump->next) {
  1800. len += lump->text.len;
  1801. if (lump->flags&LUMP_RPL_BODY)
  1802. body = lump;
  1803. }
  1804. /* server header */
  1805. if (server_signature)
  1806. len += SERVER_HDR_LEN + CRLF_LEN;
  1807. /* warning hdr */
  1808. if (sip_warning) {
  1809. warning_buf = warning_builder(msg,&warning_len);
  1810. if (warning_buf) len += warning_len + CRLF_LEN;
  1811. else LOG(L_WARN, "WARNING: warning skipped -- too big\n");
  1812. }
  1813. /* content length hdr */
  1814. if (body) {
  1815. content_len_buf = int2str(body->text.len, (int*)&content_len_len);
  1816. len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
  1817. } else {
  1818. len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
  1819. }
  1820. /* end of message */
  1821. len += CRLF_LEN; /*new line*/
  1822. /*allocating mem*/
  1823. buf = (char*) pkg_malloc( len+1 );
  1824. if (!buf)
  1825. {
  1826. LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: out of memory "
  1827. " ; needs %d\n",len);
  1828. goto error01;
  1829. }
  1830. /* filling the buffer*/
  1831. p=buf;
  1832. /* first line */
  1833. memcpy( p , msg->first_line.u.request.version.s ,
  1834. msg->first_line.u.request.version.len);
  1835. p += msg->first_line.u.request.version.len;
  1836. *(p++) = ' ' ;
  1837. /*code*/
  1838. for ( i=2 , foo = code ; i>=0 ; i-- , foo=foo/10 )
  1839. *(p+i) = '0' + foo - ( foo/10 )*10;
  1840. p += 3;
  1841. *(p++) = ' ' ;
  1842. memcpy( p , text , text_len );
  1843. p += text_len;
  1844. memcpy( p, CRLF, CRLF_LEN );
  1845. p+=CRLF_LEN;
  1846. /* headers*/
  1847. for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
  1848. switch (hdr->type)
  1849. {
  1850. case HDR_VIA_T:
  1851. if (hdr==msg->h_via1){
  1852. if (rport_buf){
  1853. if (msg->via1->rport){ /* delete the old one */
  1854. /* copy until rport */
  1855. append_str_trans( p, hdr->name.s ,
  1856. msg->via1->rport->start-hdr->name.s-1,msg);
  1857. /* copy new rport */
  1858. append_str(p, rport_buf, rport_len);
  1859. /* copy the rest of the via */
  1860. append_str_trans(p, msg->via1->rport->start+
  1861. msg->via1->rport->size,
  1862. hdr->body.s+hdr->body.len-
  1863. msg->via1->rport->start-
  1864. msg->via1->rport->size, msg);
  1865. }else{ /* just append the new one */
  1866. /* normal whole via copy */
  1867. append_str_trans( p, hdr->name.s ,
  1868. (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
  1869. append_str(p, rport_buf, rport_len);
  1870. }
  1871. }else{
  1872. /* normal whole via copy */
  1873. append_str_trans( p, hdr->name.s ,
  1874. (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
  1875. }
  1876. if (received_buf)
  1877. append_str( p, received_buf, received_len);
  1878. }else{
  1879. /* normal whole via copy */
  1880. append_str_trans( p, hdr->name.s,
  1881. (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
  1882. }
  1883. append_str( p, CRLF,CRLF_LEN);
  1884. break;
  1885. case HDR_RECORDROUTE_T:
  1886. /* RR only for 1xx and 2xx replies */
  1887. if (code<180 || code>=300) break;
  1888. append_str(p, hdr->name.s, hdr->len);
  1889. break;
  1890. case HDR_TO_T:
  1891. if (new_tag && new_tag->len){
  1892. if (to_tag.s ) { /* replacement */
  1893. /* before to-tag */
  1894. append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
  1895. /* to tag replacement */
  1896. bmark->to_tag_val.s=p;
  1897. bmark->to_tag_val.len=new_tag->len;
  1898. append_str( p, new_tag->s,new_tag->len);
  1899. /* the rest after to-tag */
  1900. append_str( p, to_tag.s+to_tag.len,
  1901. hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
  1902. }else{ /* adding a new to-tag */
  1903. after_body=hdr->body.s+hdr->body.len;
  1904. append_str( p, hdr->name.s, after_body-hdr->name.s);
  1905. append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
  1906. bmark->to_tag_val.s=p;
  1907. bmark->to_tag_val.len=new_tag->len;
  1908. append_str( p, new_tag->s,new_tag->len);
  1909. append_str( p, after_body,
  1910. hdr->name.s+hdr->len-after_body);
  1911. }
  1912. break;
  1913. } /* no new to-tag -- proceed to 1:1 copying */
  1914. totags=((struct to_body*)(hdr->parsed))->tag_value.s;
  1915. if (totags) {
  1916. bmark->to_tag_val.s=p+(totags-hdr->name.s);
  1917. bmark->to_tag_val.len=
  1918. ((struct to_body*)(hdr->parsed))->tag_value.len;
  1919. }
  1920. else {
  1921. bmark->to_tag_val.len = 0;
  1922. bmark->to_tag_val.s = p+(hdr->body.s+hdr->body.len-hdr->name.s);
  1923. }
  1924. /* no break */
  1925. case HDR_FROM_T:
  1926. case HDR_CALLID_T:
  1927. case HDR_CSEQ_T:
  1928. append_str(p, hdr->name.s, hdr->len);
  1929. break;
  1930. default:
  1931. /* do nothing, we are interested only in the above headers */
  1932. ;
  1933. } /* end switch */
  1934. } /* end for */
  1935. /* lumps */
  1936. for(lump=msg->reply_lump;lump;lump=lump->next)
  1937. if (lump->flags&LUMP_RPL_HDR){
  1938. memcpy(p,lump->text.s,lump->text.len);
  1939. p += lump->text.len;
  1940. }
  1941. /* server header */
  1942. if (server_signature) {
  1943. memcpy( p, SERVER_HDR , SERVER_HDR_LEN );
  1944. p+=SERVER_HDR_LEN;
  1945. memcpy( p, CRLF, CRLF_LEN );
  1946. p+=CRLF_LEN;
  1947. }
  1948. /* content_length hdr */
  1949. if (content_len_len) {
  1950. append_str( p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
  1951. append_str( p, content_len_buf, content_len_len );
  1952. append_str( p, CRLF, CRLF_LEN );
  1953. } else {
  1954. append_str( p, CONTENT_LENGTH"0"CRLF,CONTENT_LENGTH_LEN+1+CRLF_LEN);
  1955. }
  1956. /* warning header */
  1957. if (warning_buf) {
  1958. memcpy( p, warning_buf, warning_len);
  1959. p+=warning_len;
  1960. memcpy( p, CRLF, CRLF_LEN);
  1961. p+=CRLF_LEN;
  1962. }
  1963. /*end of message*/
  1964. memcpy( p, CRLF, CRLF_LEN );
  1965. p+=CRLF_LEN;
  1966. /* body */
  1967. if (body) {
  1968. memcpy ( p, body->text.s, body->text.len );
  1969. p+=body->text.len;
  1970. }
  1971. if (len!=p-buf)
  1972. LOG(L_CRIT,"BUG:build_res_from_sip_req: diff len=%d p-buf=%d\n",
  1973. len, (int)(p-buf));
  1974. *(p) = 0;
  1975. *returned_len = len;
  1976. /* in req2reply, received_buf is not introduced to lumps and
  1977. needs to be deleted here
  1978. */
  1979. if (received_buf) pkg_free(received_buf);
  1980. if (rport_buf) pkg_free(rport_buf);
  1981. return buf;
  1982. error01:
  1983. if (received_buf) pkg_free(received_buf);
  1984. if (rport_buf) pkg_free(rport_buf);
  1985. error00:
  1986. *returned_len=0;
  1987. return 0;
  1988. }
  1989. /* return number of chars printed or 0 if space exceeded;
  1990. assumes buffer size of at least MAX_BRANCH_PARAM_LEN
  1991. */
  1992. int branch_builder( unsigned int hash_index,
  1993. /* only either parameter useful */
  1994. unsigned int label, char * char_v,
  1995. int branch,
  1996. char *branch_str, int *len )
  1997. {
  1998. char *begin;
  1999. int size;
  2000. /* hash id provided ... start with it */
  2001. size=MAX_BRANCH_PARAM_LEN;
  2002. begin=branch_str;
  2003. *len=0;
  2004. memcpy(begin, MCOOKIE, MCOOKIE_LEN );
  2005. size-=MCOOKIE_LEN;begin+=MCOOKIE_LEN;
  2006. if (int2reverse_hex( &begin, &size, hash_index)==-1)
  2007. return 0;
  2008. if (size) {
  2009. *begin=BRANCH_SEPARATOR;
  2010. begin++; size--;
  2011. } else return 0;
  2012. /* string with request's characteristic value ... use it ... */
  2013. if (char_v) {
  2014. if (memcpy(begin,char_v,MD5_LEN)) {
  2015. begin+=MD5_LEN; size-=MD5_LEN;
  2016. } else return 0;
  2017. } else { /* ... use the "label" value otherwise */
  2018. if (int2reverse_hex( &begin, &size, label )==-1)
  2019. return 0;
  2020. }
  2021. if (size) {
  2022. *begin=BRANCH_SEPARATOR;
  2023. begin++; size--;
  2024. } else return 0;
  2025. if (int2reverse_hex( &begin, &size, branch)==-1)
  2026. return 0;
  2027. *len=MAX_BRANCH_PARAM_LEN-size;
  2028. return size;
  2029. }
  2030. /* uses only the send_info->send_socket, send_info->proto and
  2031. * send_info->comp (so that a send_info used for sending can be passed
  2032. * to this function w/o changes and the correct via will be built) */
  2033. char* via_builder( unsigned int *len,
  2034. struct dest_info* send_info /* where to send the reply */,
  2035. str* branch, str* extra_params, struct hostport* hp)
  2036. {
  2037. unsigned int via_len, extra_len;
  2038. char *line_buf;
  2039. int max_len;
  2040. int via_prefix_len;
  2041. str* address_str; /* address displayed in via */
  2042. str* port_str; /* port no displayed in via */
  2043. struct socket_info* send_sock;
  2044. int comp_len, comp_name_len;
  2045. char* comp_name;
  2046. send_sock=send_info->send_sock;
  2047. /* use pre-set address in via or the outbound socket one */
  2048. if ( hp && hp->host->len)
  2049. address_str=hp->host;
  2050. else
  2051. address_str=&(send_sock->address_str);
  2052. if (hp && hp->port->len)
  2053. port_str=hp->port;
  2054. else
  2055. port_str=&(send_sock->port_no_str);
  2056. comp_len=comp_name_len=0;
  2057. comp_name=0;
  2058. #ifdef USE_COMP
  2059. switch(send_info->comp){
  2060. case COMP_NONE:
  2061. break;
  2062. case COMP_SIGCOMP:
  2063. comp_len=COMP_PARAM_LEN;
  2064. comp_name_len=SIGCOMP_NAME_LEN;
  2065. comp_name=SIGCOMP_NAME;
  2066. break;
  2067. case COMP_SERGZ:
  2068. comp_len=COMP_PARAM_LEN;
  2069. comp_name_len=SERGZ_NAME_LEN;
  2070. comp_name=SERGZ_NAME;
  2071. break;
  2072. default:
  2073. LOG(L_CRIT, "BUG: via_builder: unknown comp %d\n",
  2074. send_info->comp);
  2075. /* continue, we'll just ignore comp */
  2076. }
  2077. #endif /* USE_COMP */
  2078. via_prefix_len=MY_VIA_LEN+(send_info->proto==PROTO_SCTP);
  2079. max_len=via_prefix_len +address_str->len /* space in MY_VIA */
  2080. +2 /* just in case it is a v6 address ... [ ] */
  2081. +1 /*':'*/+port_str->len
  2082. +(branch?(MY_BRANCH_LEN+branch->len):0)
  2083. +(extra_params?extra_params->len:0)
  2084. +comp_len+comp_name_len
  2085. +CRLF_LEN+1;
  2086. line_buf=pkg_malloc( max_len );
  2087. if (line_buf==0){
  2088. ser_error=E_OUT_OF_MEM;
  2089. LOG(L_ERR, "ERROR: via_builder: out of memory\n");
  2090. return 0;
  2091. }
  2092. extra_len=0;
  2093. via_len=via_prefix_len+address_str->len; /*space included in MY_VIA*/
  2094. memcpy(line_buf, MY_VIA, MY_VIA_LEN);
  2095. if (send_info->proto==PROTO_UDP){
  2096. /* do nothing */
  2097. }else if (send_info->proto==PROTO_TCP){
  2098. memcpy(line_buf+MY_VIA_LEN-4, "TCP ", 4);
  2099. }else if (send_info->proto==PROTO_TLS){
  2100. memcpy(line_buf+MY_VIA_LEN-4, "TLS ", 4);
  2101. }else if (send_info->proto==PROTO_SCTP){
  2102. memcpy(line_buf+MY_VIA_LEN-4, "SCTP ", 5);
  2103. }else{
  2104. LOG(L_CRIT, "BUG: via_builder: unknown proto %d\n", send_info->proto);
  2105. return 0;
  2106. }
  2107. # ifdef USE_IPV6
  2108. /* add [] only if ipv6 and outbound socket address is used;
  2109. * if using pre-set no check is made */
  2110. if ((send_sock->address.af==AF_INET6) &&
  2111. (address_str==&(send_sock->address_str))) {
  2112. line_buf[via_prefix_len]='[';
  2113. line_buf[via_prefix_len+1+address_str->len]=']';
  2114. extra_len=1;
  2115. via_len+=2; /* [ ]*/
  2116. }
  2117. # endif
  2118. memcpy(line_buf+via_prefix_len+extra_len, address_str->s,
  2119. address_str->len);
  2120. if ((send_sock->port_no!=SIP_PORT) ||
  2121. (port_str!=&send_sock->port_no_str)){
  2122. line_buf[via_len]=':'; via_len++;
  2123. memcpy(line_buf+via_len, port_str->s, port_str->len);
  2124. via_len+=port_str->len;
  2125. }
  2126. /* branch parameter */
  2127. if (branch){
  2128. memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
  2129. via_len+=MY_BRANCH_LEN;
  2130. memcpy(line_buf+via_len, branch->s, branch->len );
  2131. via_len+=branch->len;
  2132. }
  2133. /* extra params */
  2134. if (extra_params){
  2135. memcpy(line_buf+via_len, extra_params->s, extra_params->len);
  2136. via_len+=extra_params->len;
  2137. }
  2138. #ifdef USE_COMP
  2139. /* comp */
  2140. if (comp_len){
  2141. memcpy(line_buf+via_len, COMP_PARAM, COMP_PARAM_LEN);
  2142. via_len+=COMP_PARAM_LEN;
  2143. memcpy(line_buf+via_len, comp_name, comp_name_len);
  2144. via_len+=comp_name_len;
  2145. }
  2146. #endif
  2147. memcpy(line_buf+via_len, CRLF, CRLF_LEN);
  2148. via_len+=CRLF_LEN;
  2149. line_buf[via_len]=0; /* null terminate the string*/
  2150. *len = via_len;
  2151. return line_buf;
  2152. }
  2153. /* creates a via header honoring the protocol of the incomming socket
  2154. * msg is an optional parameter */
  2155. char* create_via_hf( unsigned int *len,
  2156. struct sip_msg *msg,
  2157. struct dest_info* send_info /* where to send the reply */,
  2158. str* branch)
  2159. {
  2160. char* via;
  2161. str extra_params;
  2162. struct hostport hp;
  2163. #if defined USE_TCP || defined USE_SCTP
  2164. char* id_buf;
  2165. unsigned int id_len;
  2166. id_buf=0;
  2167. id_len=0;
  2168. #endif
  2169. extra_params.len=0;
  2170. extra_params.s=0;
  2171. #if defined USE_TCP || defined USE_SCTP
  2172. /* add id if tcp */
  2173. if (msg && (
  2174. #ifdef USE_TCP
  2175. (msg->rcv.proto==PROTO_TCP)
  2176. #ifdef USE_TLS
  2177. || (msg->rcv.proto==PROTO_TLS)
  2178. #endif
  2179. #ifdef USE_SCTP
  2180. ||
  2181. #endif /* USE_SCTP */
  2182. #endif /* USE_TCP */
  2183. #ifdef USE_SCTP
  2184. (msg->rcv.proto==PROTO_SCTP)
  2185. #endif /* USE_SCTP */
  2186. )){
  2187. if ((id_buf=id_builder(msg, &id_len))==0){
  2188. LOG(L_ERR, "ERROR: create_via_hf:"
  2189. " id_builder failed\n");
  2190. return 0; /* we don't need to free anything,
  2191. nothing alloc'ed yet*/
  2192. }
  2193. DBG("create_via_hf: id added: <%.*s>, rcv proto=%d\n",
  2194. (int)id_len, id_buf, msg->rcv.proto);
  2195. extra_params.s=id_buf;
  2196. extra_params.len=id_len;
  2197. }
  2198. #endif /* USE_TCP || USE_SCTP */
  2199. set_hostport(&hp, msg);
  2200. via = via_builder( len, send_info, branch,
  2201. extra_params.len?&extra_params:0, &hp);
  2202. #if defined USE_TCP || defined USE_SCTP
  2203. /* we do not need id_buf any more, the id is already in the new via header */
  2204. if (id_buf) pkg_free(id_buf);
  2205. #endif
  2206. return via;
  2207. }
  2208. /* builds a char* buffer from message headers without body
  2209. * first line is excluded in case of skip_first_line=1
  2210. * error is set -1 if the memory allocation failes
  2211. */
  2212. char * build_only_headers( struct sip_msg* msg, int skip_first_line,
  2213. unsigned int *returned_len,
  2214. int *error,
  2215. struct dest_info* send_info)
  2216. {
  2217. char *buf, *new_buf;
  2218. unsigned int offset, s_offset, len, new_len;
  2219. *error = 0;
  2220. buf = msg->buf;
  2221. if (skip_first_line)
  2222. s_offset = msg->headers->name.s - buf;
  2223. else
  2224. s_offset = 0;
  2225. /* original length without body, and without final \r\n */
  2226. len = msg->unparsed - buf;
  2227. /* new msg length */
  2228. new_len = len - /* original length */
  2229. s_offset + /* skipped first line */
  2230. lumps_len(msg, msg->add_rm, send_info); /* lumps */
  2231. if (new_len == 0) {
  2232. *returned_len = 0;
  2233. return 0;
  2234. }
  2235. new_buf = (char *)pkg_malloc(new_len+1);
  2236. if (!new_buf) {
  2237. LOG(L_ERR, "ERROR: build_only_headers: Not enough memory\n");
  2238. *error = -1;
  2239. return 0;
  2240. }
  2241. new_buf[0] = 0;
  2242. offset = 0;
  2243. /* copy message lumps */
  2244. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
  2245. /* copy the rest of the message without body */
  2246. if (len > s_offset) {
  2247. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  2248. offset += (len-s_offset);
  2249. }
  2250. new_buf[offset] = 0;
  2251. *returned_len = offset;
  2252. return new_buf;
  2253. }
  2254. /* builds a char* buffer from message body
  2255. * error is set -1 if the memory allocation failes
  2256. */
  2257. char * build_body( struct sip_msg* msg,
  2258. unsigned int *returned_len,
  2259. int *error,
  2260. struct dest_info* send_info)
  2261. {
  2262. char *buf, *new_buf, *body;
  2263. unsigned int offset, s_offset, len, new_len;
  2264. *error = 0;
  2265. body = get_body(msg);
  2266. if (!body || (body[0] == 0)) {
  2267. *returned_len = 0;
  2268. return 0;
  2269. }
  2270. buf = msg->buf;
  2271. s_offset = body - buf;
  2272. /* original length of msg with body */
  2273. len = msg->len;
  2274. /* new body length */
  2275. new_len = len - /* original length */
  2276. s_offset + /* msg without body */
  2277. lumps_len(msg, msg->body_lumps, send_info); /* lumps */
  2278. new_buf = (char *)pkg_malloc(new_len+1);
  2279. if (!new_buf) {
  2280. LOG(L_ERR, "ERROR: build_body: Not enough memory\n");
  2281. *error = -1;
  2282. return 0;
  2283. }
  2284. new_buf[0] = 0;
  2285. offset = 0;
  2286. /* copy body lumps */
  2287. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
  2288. /* copy the rest of the message without body */
  2289. if (len > s_offset) {
  2290. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  2291. offset += (len-s_offset);
  2292. }
  2293. new_buf[offset] = 0;
  2294. *returned_len = offset;
  2295. return new_buf;
  2296. }
  2297. /* builds a char* buffer from SIP message including body
  2298. * The function adjusts the Content-Length HF according
  2299. * to body lumps in case of touch_clen=1.
  2300. */
  2301. char * build_all( struct sip_msg* msg, int touch_clen,
  2302. unsigned int *returned_len,
  2303. int *error,
  2304. struct dest_info* send_info)
  2305. {
  2306. char *buf, *new_buf;
  2307. unsigned int offset, s_offset, len, new_len;
  2308. unsigned int body_delta;
  2309. *error = 0;
  2310. /* Calculate message body difference */
  2311. body_delta = lumps_len(msg, msg->body_lumps, send_info);
  2312. if (touch_clen) {
  2313. /* adjust Content-Length */
  2314. if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
  2315. LOG(L_ERR, "ERROR: build_all: Error while adjusting"
  2316. " Content-Length\n");
  2317. *error = -1;
  2318. return 0;
  2319. }
  2320. }
  2321. buf = msg->buf;
  2322. /* original msg length */
  2323. len = msg->len;
  2324. /* new msg length */
  2325. new_len = len + /* original length */
  2326. lumps_len(msg, msg->add_rm, send_info) + /* hdr lumps */
  2327. body_delta; /* body lumps */
  2328. if (new_len == 0) {
  2329. returned_len = 0;
  2330. return 0;
  2331. }
  2332. new_buf = (char *)pkg_malloc(new_len+1);
  2333. if (!new_buf) {
  2334. LOG(L_ERR, "ERROR: build_all: Not enough memory\n");
  2335. *error = -1;
  2336. return 0;
  2337. }
  2338. new_buf[0] = 0;
  2339. offset = s_offset = 0;
  2340. /* copy message lumps */
  2341. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
  2342. /* copy body lumps */
  2343. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
  2344. /* copy the rest of the message */
  2345. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  2346. offset += (len-s_offset);
  2347. new_buf[offset] = 0;
  2348. *returned_len = offset;
  2349. return new_buf;
  2350. }