msg_translator.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504
  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* 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. /* Calculate message length difference caused by lumps modifying message
  1279. * body, from this point on the message body must not be modified. Zero
  1280. * value indicates that the body hasn't been modified
  1281. */
  1282. clen_buf = 0;
  1283. anchor=0;
  1284. body_only=1;
  1285. /* check to see if we need to add clen */
  1286. #ifdef USE_TCP
  1287. if (proto == PROTO_TCP
  1288. #ifdef USE_TLS
  1289. || proto == PROTO_TLS
  1290. #endif
  1291. ) {
  1292. if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0)==-1){
  1293. LOG(L_ERR, "adjust_clen: error parsing content-length\n");
  1294. goto error;
  1295. }
  1296. if (msg->content_length==0){
  1297. /* not present, we need to add it */
  1298. /* msg->unparsed should point just before the final crlf
  1299. * - whole message was parsed by the above parse_headers
  1300. * which did not find content-length */
  1301. anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
  1302. HDR_CONTENTLENGTH_T);
  1303. if (anchor==0){
  1304. LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
  1305. goto error;
  1306. }
  1307. body_only=0;
  1308. }
  1309. }
  1310. #endif
  1311. if ((anchor==0) && body_delta){
  1312. if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1) {
  1313. LOG(L_ERR, "adjust_clen: Error parsing Content-Length\n");
  1314. goto error;
  1315. }
  1316. /* The body has been changed, try to find
  1317. * existing Content-Length
  1318. */
  1319. /* no need for Content-Length if it's and UDP packet and
  1320. * it hasn't Content-Length already */
  1321. if ((msg->content_length==0)){
  1322. /* content-length doesn't exist, append it */
  1323. /* msg->unparsed should point just before the final crlf
  1324. * - whole message was parsed by the above parse_headers
  1325. * which did not find content-length */
  1326. if (proto!=PROTO_UDP){
  1327. anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
  1328. HDR_CONTENTLENGTH_T);
  1329. if (anchor==0){
  1330. LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
  1331. goto error;
  1332. }
  1333. body_only=0;
  1334. }else{
  1335. DBG("adjust_clen: UDP packet with no clen => not adding one \n");
  1336. }
  1337. }else{
  1338. /* Content-Length has been found, remove it */
  1339. anchor = del_lump( msg, msg->content_length->body.s - msg->buf,
  1340. msg->content_length->body.len, HDR_CONTENTLENGTH_T);
  1341. if (anchor==0) {
  1342. LOG(L_ERR, "adjust_clen: Can't remove original"
  1343. " Content-Length\n");
  1344. goto error;
  1345. }
  1346. }
  1347. }
  1348. if (anchor){
  1349. clen_buf = clen_builder(msg, &clen_len, body_delta, body_only);
  1350. if (!clen_buf) goto error;
  1351. if (insert_new_lump_after(anchor, clen_buf, clen_len,
  1352. HDR_CONTENTLENGTH_T) == 0)
  1353. goto error;
  1354. }
  1355. return 0;
  1356. error:
  1357. if (clen_buf) pkg_free(clen_buf);
  1358. return -1;
  1359. }
  1360. /** builds a request in memory from another sip request.
  1361. *
  1362. * Side-effects: - it adds lumps to the msg which are _not_ cleaned.
  1363. * All the added lumps are HDR_VIA_T.
  1364. * - it might change send_info->proto and send_info->send_socket
  1365. * if proto fallback is enabled (see below).
  1366. *
  1367. * Uses also global_req_flags ( OR'ed with msg->msg_flags, see send_info
  1368. * below).
  1369. *
  1370. * @param msg - sip message structure, complete with lumps
  1371. * @param returned_len - result length (filled in)
  1372. * @param send_info - dest_info structure (value/result), contains where the
  1373. * packet will be sent to (it's needed for building a
  1374. * correct via, fill RR lumps a.s.o.). If MTU based
  1375. * protocol fall-back is enabled (see flags below),
  1376. * send_info->proto might be updated with the new
  1377. * protocol.
  1378. * msg->msg_flags used:
  1379. * - FL_TCP_MTU_FB, FL_TLS_MTU_FB and FL_SCTP_MTU_FB -
  1380. * fallback to the corresp. proto if the built
  1381. * message > mtu and send_info->proto==PROTO_UDP.
  1382. * It will also update send_info->proto.
  1383. * - FL_FORCE_RPORT: add rport to via
  1384. *
  1385. * @return pointer to the new request (pkg_malloc'ed, needs freeing when
  1386. * done) and sets returned_len or 0 on error.
  1387. */
  1388. char * build_req_buf_from_sip_req( struct sip_msg* msg,
  1389. unsigned int *returned_len,
  1390. struct dest_info* send_info
  1391. )
  1392. {
  1393. unsigned int len, new_len, received_len, rport_len, uri_len, via_len,
  1394. body_delta;
  1395. char* line_buf;
  1396. char* received_buf;
  1397. char* rport_buf;
  1398. char* new_buf;
  1399. char* buf;
  1400. unsigned int offset, s_offset, size;
  1401. struct lump* via_anchor;
  1402. struct lump* via_lump;
  1403. struct lump* via_insert_param;
  1404. str branch;
  1405. unsigned int flags;
  1406. unsigned int udp_mtu;
  1407. struct dest_info di;
  1408. via_insert_param=0;
  1409. uri_len=0;
  1410. buf=msg->buf;
  1411. len=msg->len;
  1412. received_len=0;
  1413. rport_len=0;
  1414. new_buf=0;
  1415. received_buf=0;
  1416. rport_buf=0;
  1417. line_buf=0;
  1418. flags=msg->msg_flags|global_req_flags;
  1419. /* Calculate message body difference and adjust Content-Length */
  1420. body_delta = lumps_len(msg, msg->body_lumps, send_info);
  1421. if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
  1422. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
  1423. " Content-Length\n");
  1424. goto error00;
  1425. }
  1426. /* create the via header */
  1427. branch.s=msg->add_to_branch_s;
  1428. branch.len=msg->add_to_branch_len;
  1429. line_buf = create_via_hf( &via_len, msg, send_info, &branch);
  1430. if (!line_buf){
  1431. LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
  1432. "memory allocation failure\n");
  1433. goto error00;
  1434. }
  1435. /* check if received needs to be added */
  1436. if ( received_test(msg) ) {
  1437. if ((received_buf=received_builder(msg,&received_len))==0){
  1438. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
  1439. " received_builder failed\n");
  1440. goto error01; /* free also line_buf */
  1441. }
  1442. }
  1443. /* check if rport needs to be updated:
  1444. * - if FL_FORCE_RPORT is set add it (and del. any previous version)
  1445. * - if via already contains an rport add it and overwrite the previous
  1446. * rport value if present (if you don't want to overwrite the previous
  1447. * version remove the comments) */
  1448. if ((flags&FL_FORCE_RPORT)||
  1449. (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
  1450. if ((rport_buf=rport_builder(msg, &rport_len))==0){
  1451. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
  1452. " rport_builder failed\n");
  1453. goto error01; /* free everything */
  1454. }
  1455. }
  1456. /* find out where the offset of the first parameter that should be added
  1457. * (after host:port), needed by add receive & maybe rport */
  1458. if (msg->via1->params.s){
  1459. size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
  1460. for ';' */
  1461. }else{
  1462. size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
  1463. if (msg->via1->port!=0){
  1464. /*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
  1465. size += msg->via1->port_str.len + 1; /* +1 for ':'*/
  1466. }
  1467. #if 0
  1468. /* no longer necessary, now hots.s contains [] */
  1469. #ifdef USE_IPV6
  1470. if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
  1471. #endif
  1472. #endif
  1473. }
  1474. /* if received needs to be added, add anchor after host and add it, or
  1475. * overwrite the previous one if already present */
  1476. if (received_len){
  1477. if (msg->via1->received){ /* received already present => overwrite it*/
  1478. via_insert_param=del_lump(msg,
  1479. msg->via1->received->start-buf-1, /*;*/
  1480. msg->via1->received->size+1, /*;*/ HDR_VIA_T);
  1481. }else if (via_insert_param==0){ /* receive not present, ok */
  1482. via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
  1483. HDR_VIA_T);
  1484. }
  1485. if (via_insert_param==0) goto error02; /* free received_buf */
  1486. if (insert_new_lump_after(via_insert_param, received_buf, received_len,
  1487. HDR_VIA_T) ==0 ) goto error02; /* free received_buf */
  1488. }
  1489. /* if rport needs to be updated, delete it if present and add it's value */
  1490. if (rport_len){
  1491. if (msg->via1->rport){ /* rport already present */
  1492. via_insert_param=del_lump(msg,
  1493. msg->via1->rport->start-buf-1, /*';'*/
  1494. msg->via1->rport->size+1 /* ; */, HDR_VIA_T);
  1495. }else if (via_insert_param==0){ /*force rport, no rport present */
  1496. /* no rport, add it */
  1497. via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
  1498. HDR_VIA_T);
  1499. }
  1500. if (via_insert_param==0) goto error03; /* free rport_buf */
  1501. if (insert_new_lump_after(via_insert_param, rport_buf, rport_len,
  1502. HDR_VIA_T) ==0 )
  1503. goto error03; /* free rport_buf */
  1504. }
  1505. /* compute new msg len and fix overlapping zones*/
  1506. new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len;
  1507. #ifdef XL_DEBUG
  1508. LOG(L_ERR, "DEBUG: new_len(%d)=len(%d)+lumps_len\n", new_len, len);
  1509. #endif
  1510. udp_mtu=cfg_get(core, core_cfg, udp_mtu);
  1511. di.proto=PROTO_NONE;
  1512. if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu &&
  1513. (flags & FL_MTU_FB_MASK) && (new_len>udp_mtu))){
  1514. di=*send_info; /* copy whole struct - will be used in the Via builder */
  1515. di.proto=PROTO_NONE; /* except the proto */
  1516. #ifdef USE_TCP
  1517. if (!tcp_disable && (flags & FL_MTU_TCP_FB) &&
  1518. (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TCP))){
  1519. di.proto=PROTO_TCP;
  1520. }
  1521. #ifdef USE_TLS
  1522. else if (!tls_disable && (flags & FL_MTU_TLS_FB) &&
  1523. (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TLS))){
  1524. di.proto=PROTO_TLS;
  1525. }
  1526. #endif /* USE_TLS */
  1527. #endif /* USE_TCP */
  1528. #ifdef USE_SCTP
  1529. #ifdef USE_TCP
  1530. else
  1531. #endif /* USE_TCP */
  1532. if (!sctp_disable && (flags & FL_MTU_SCTP_FB) &&
  1533. (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_SCTP))){
  1534. di.proto=PROTO_SCTP;
  1535. }
  1536. #endif /* USE_SCTP */
  1537. if (di.proto!=PROTO_NONE){
  1538. new_len-=via_len;
  1539. pkg_free(line_buf);
  1540. line_buf = create_via_hf( &via_len, msg, &di, &branch);
  1541. if (!line_buf){
  1542. LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
  1543. "memory allocation failure!\n");
  1544. goto error00;
  1545. }
  1546. new_len+=via_len;
  1547. }
  1548. }
  1549. /* add via header to the list */
  1550. /* try to add it before msg. 1st via */
  1551. /* add first via, as an anchor for second via*/
  1552. via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
  1553. if (via_anchor==0) goto error04;
  1554. if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
  1555. HDR_VIA_T))==0)
  1556. goto error04;
  1557. if (msg->new_uri.s){
  1558. uri_len=msg->new_uri.len;
  1559. new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
  1560. }
  1561. new_buf=(char*)pkg_malloc(new_len+1);
  1562. if (new_buf==0){
  1563. ser_error=E_OUT_OF_MEM;
  1564. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
  1565. goto error00;
  1566. }
  1567. offset=s_offset=0;
  1568. if (msg->new_uri.s){
  1569. /* copy message up to uri */
  1570. size=msg->first_line.u.request.uri.s-buf;
  1571. memcpy(new_buf, buf, size);
  1572. offset+=size;
  1573. s_offset+=size;
  1574. /* add our uri */
  1575. memcpy(new_buf+offset, msg->new_uri.s, uri_len);
  1576. offset+=uri_len;
  1577. s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
  1578. }
  1579. new_buf[new_len]=0;
  1580. /* copy msg adding/removing lumps */
  1581. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
  1582. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset,send_info);
  1583. /* copy the rest of the message */
  1584. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  1585. new_buf[new_len]=0;
  1586. /* update the send_info if udp_mtu affected */
  1587. if (di.proto!=PROTO_NONE) {
  1588. send_info->proto=di.proto;
  1589. send_info->send_sock=di.send_sock;
  1590. }
  1591. #ifdef DBG_MSG_QA
  1592. if (new_buf[new_len-1]==0) {
  1593. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: 0 in the end\n");
  1594. abort();
  1595. }
  1596. #endif
  1597. *returned_len=new_len;
  1598. return new_buf;
  1599. error01:
  1600. error02:
  1601. if (received_buf) pkg_free(received_buf);
  1602. error03:
  1603. if (rport_buf) pkg_free(rport_buf);
  1604. error04:
  1605. if (line_buf) pkg_free(line_buf);
  1606. error00:
  1607. *returned_len=0;
  1608. return 0;
  1609. }
  1610. char * build_res_buf_from_sip_res( struct sip_msg* msg,
  1611. unsigned int *returned_len)
  1612. {
  1613. unsigned int new_len, via_len, body_delta;
  1614. char* new_buf;
  1615. unsigned offset, s_offset, via_offset;
  1616. char* buf;
  1617. unsigned int len;
  1618. buf=msg->buf;
  1619. len=msg->len;
  1620. new_buf=0;
  1621. /* we must remove the first via */
  1622. if (msg->via1->next) {
  1623. via_len=msg->via1->bsize;
  1624. via_offset=msg->h_via1->body.s-buf;
  1625. } else {
  1626. via_len=msg->h_via1->len;
  1627. via_offset=msg->h_via1->name.s-buf;
  1628. }
  1629. /* Calculate message body difference and adjust
  1630. * Content-Length
  1631. */
  1632. body_delta = lumps_len(msg, msg->body_lumps, 0);
  1633. if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
  1634. < 0) {
  1635. LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
  1636. " Content-Length\n");
  1637. goto error;
  1638. }
  1639. /* remove the first via*/
  1640. if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
  1641. LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
  1642. "via\n");
  1643. goto error;
  1644. }
  1645. new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
  1646. know the send sock */
  1647. DBG(" old size: %d, new size: %d\n", len, new_len);
  1648. new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
  1649. (\0 to print it )*/
  1650. if (new_buf==0){
  1651. LOG(L_ERR, "ERROR: build_res_buf_from_sip_res: out of mem\n");
  1652. goto error;
  1653. }
  1654. new_buf[new_len]=0; /* debug: print the message */
  1655. offset=s_offset=0;
  1656. /*FIXME: no send sock*/
  1657. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, 0);/*FIXME:*/
  1658. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, 0);
  1659. /* copy the rest of the message */
  1660. memcpy(new_buf+offset,
  1661. buf+s_offset,
  1662. len-s_offset);
  1663. /* send it! */
  1664. DBG("build_res_from_sip_res: copied size: orig:%d, new: %d, rest: %d"
  1665. " msg=\n%s\n", s_offset, offset, len-s_offset, new_buf);
  1666. *returned_len=new_len;
  1667. return new_buf;
  1668. error:
  1669. *returned_len=0;
  1670. return 0;
  1671. }
  1672. char * build_res_buf_from_sip_req( unsigned int code, char *text ,str *new_tag,
  1673. struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
  1674. {
  1675. char *buf, *p;
  1676. unsigned int len,foo;
  1677. struct hdr_field *hdr;
  1678. struct lump_rpl *lump;
  1679. struct lump_rpl *body;
  1680. int i;
  1681. char* received_buf;
  1682. unsigned int received_len;
  1683. char* rport_buf;
  1684. unsigned int rport_len;
  1685. char* warning_buf;
  1686. unsigned int warning_len;
  1687. char* content_len_buf;
  1688. unsigned int content_len_len;
  1689. unsigned int text_len;
  1690. char *after_body;
  1691. str to_tag;
  1692. char *totags;
  1693. body = 0;
  1694. buf=0;
  1695. received_buf=rport_buf=warning_buf=content_len_buf=0;
  1696. received_len=rport_len=warning_len=content_len_len=0;
  1697. text_len=strlen(text);
  1698. to_tag.s=0; /* fixes gcc 4.0 warning */
  1699. to_tag.len=0;
  1700. /* force parsing all headers -- we want to return all
  1701. Via's in the reply and they may be scattered down to the
  1702. end of header (non-block Vias are a really poor property
  1703. of SIP :( ) */
  1704. if (parse_headers( msg, HDR_EOH_F, 0 )==-1) {
  1705. LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
  1706. "alas, parse_headers failed\n");
  1707. goto error00;
  1708. }
  1709. /*computes the length of the new response buffer*/
  1710. len = 0;
  1711. /* check if received needs to be added */
  1712. if (received_test(msg)) {
  1713. if ((received_buf=received_builder(msg,&received_len))==0) {
  1714. LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
  1715. "alas, received_builder failed\n");
  1716. goto error00;
  1717. }
  1718. }
  1719. /* check if rport needs to be updated */
  1720. if ( ((msg->msg_flags|global_req_flags)&FL_FORCE_RPORT)||
  1721. (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
  1722. if ((rport_buf=rport_builder(msg, &rport_len))==0){
  1723. LOG(L_ERR, "ERROR: build_res_buf_from_sip_req:"
  1724. " rport_builder failed\n");
  1725. goto error01; /* free everything */
  1726. }
  1727. if (msg->via1->rport)
  1728. len -= msg->via1->rport->size+1; /* include ';' */
  1729. }
  1730. /* first line */
  1731. len += msg->first_line.u.request.version.len + 1/*space*/ + 3/*code*/ + 1/*space*/ +
  1732. text_len + CRLF_LEN/*new line*/;
  1733. /*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
  1734. for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
  1735. switch (hdr->type) {
  1736. case HDR_TO_T:
  1737. if (new_tag && new_tag->len) {
  1738. to_tag=get_to(msg)->tag_value;
  1739. if (to_tag.len )
  1740. len+=new_tag->len-to_tag.len;
  1741. else
  1742. len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
  1743. }
  1744. len += hdr->len;
  1745. break;
  1746. case HDR_VIA_T:
  1747. /* we always add CRLF to via*/
  1748. len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
  1749. if (hdr==msg->h_via1) len += received_len+rport_len;
  1750. break;
  1751. case HDR_RECORDROUTE_T:
  1752. /* RR only for 1xx and 2xx replies */
  1753. if (code<180 || code>=300)
  1754. break;
  1755. case HDR_FROM_T:
  1756. case HDR_CALLID_T:
  1757. case HDR_CSEQ_T:
  1758. /* we keep the original termination for these headers*/
  1759. len += hdr->len;
  1760. break;
  1761. default:
  1762. /* do nothing, we are interested only in the above headers */
  1763. ;
  1764. }
  1765. }
  1766. /* lumps length */
  1767. for(lump=msg->reply_lump;lump;lump=lump->next) {
  1768. len += lump->text.len;
  1769. if (lump->flags&LUMP_RPL_BODY)
  1770. body = lump;
  1771. }
  1772. /* server header */
  1773. if (server_signature)
  1774. len += SERVER_HDR_LEN + CRLF_LEN;
  1775. /* warning hdr */
  1776. if (sip_warning) {
  1777. warning_buf = warning_builder(msg,&warning_len);
  1778. if (warning_buf) len += warning_len + CRLF_LEN;
  1779. else LOG(L_WARN, "WARNING: warning skipped -- too big\n");
  1780. }
  1781. /* content length hdr */
  1782. if (body) {
  1783. content_len_buf = int2str(body->text.len, (int*)&content_len_len);
  1784. len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
  1785. } else {
  1786. len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
  1787. }
  1788. /* end of message */
  1789. len += CRLF_LEN; /*new line*/
  1790. /*allocating mem*/
  1791. buf = (char*) pkg_malloc( len+1 );
  1792. if (!buf)
  1793. {
  1794. LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: out of memory "
  1795. " ; needs %d\n",len);
  1796. goto error01;
  1797. }
  1798. /* filling the buffer*/
  1799. p=buf;
  1800. /* first line */
  1801. memcpy( p , msg->first_line.u.request.version.s ,
  1802. msg->first_line.u.request.version.len);
  1803. p += msg->first_line.u.request.version.len;
  1804. *(p++) = ' ' ;
  1805. /*code*/
  1806. for ( i=2 , foo = code ; i>=0 ; i-- , foo=foo/10 )
  1807. *(p+i) = '0' + foo - ( foo/10 )*10;
  1808. p += 3;
  1809. *(p++) = ' ' ;
  1810. memcpy( p , text , text_len );
  1811. p += text_len;
  1812. memcpy( p, CRLF, CRLF_LEN );
  1813. p+=CRLF_LEN;
  1814. /* headers*/
  1815. for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
  1816. switch (hdr->type)
  1817. {
  1818. case HDR_VIA_T:
  1819. if (hdr==msg->h_via1){
  1820. if (rport_buf){
  1821. if (msg->via1->rport){ /* delete the old one */
  1822. /* copy until rport */
  1823. append_str_trans( p, hdr->name.s ,
  1824. msg->via1->rport->start-hdr->name.s-1,msg);
  1825. /* copy new rport */
  1826. append_str(p, rport_buf, rport_len);
  1827. /* copy the rest of the via */
  1828. append_str_trans(p, msg->via1->rport->start+
  1829. msg->via1->rport->size,
  1830. hdr->body.s+hdr->body.len-
  1831. msg->via1->rport->start-
  1832. msg->via1->rport->size, msg);
  1833. }else{ /* just append the new one */
  1834. /* normal whole via copy */
  1835. append_str_trans( p, hdr->name.s ,
  1836. (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
  1837. append_str(p, rport_buf, rport_len);
  1838. }
  1839. }else{
  1840. /* normal whole via copy */
  1841. append_str_trans( p, hdr->name.s ,
  1842. (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
  1843. }
  1844. if (received_buf)
  1845. append_str( p, received_buf, received_len);
  1846. }else{
  1847. /* normal whole via copy */
  1848. append_str_trans( p, hdr->name.s,
  1849. (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
  1850. }
  1851. append_str( p, CRLF,CRLF_LEN);
  1852. break;
  1853. case HDR_RECORDROUTE_T:
  1854. /* RR only for 1xx and 2xx replies */
  1855. if (code<180 || code>=300) break;
  1856. append_str(p, hdr->name.s, hdr->len);
  1857. break;
  1858. case HDR_TO_T:
  1859. if (new_tag && new_tag->len){
  1860. if (to_tag.s ) { /* replacement */
  1861. /* before to-tag */
  1862. append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
  1863. /* to tag replacement */
  1864. bmark->to_tag_val.s=p;
  1865. bmark->to_tag_val.len=new_tag->len;
  1866. append_str( p, new_tag->s,new_tag->len);
  1867. /* the rest after to-tag */
  1868. append_str( p, to_tag.s+to_tag.len,
  1869. hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
  1870. }else{ /* adding a new to-tag */
  1871. after_body=hdr->body.s+hdr->body.len;
  1872. append_str( p, hdr->name.s, after_body-hdr->name.s);
  1873. append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
  1874. bmark->to_tag_val.s=p;
  1875. bmark->to_tag_val.len=new_tag->len;
  1876. append_str( p, new_tag->s,new_tag->len);
  1877. append_str( p, after_body,
  1878. hdr->name.s+hdr->len-after_body);
  1879. }
  1880. break;
  1881. } /* no new to-tag -- proceed to 1:1 copying */
  1882. totags=((struct to_body*)(hdr->parsed))->tag_value.s;
  1883. if (totags) {
  1884. bmark->to_tag_val.s=p+(totags-hdr->name.s);
  1885. bmark->to_tag_val.len=
  1886. ((struct to_body*)(hdr->parsed))->tag_value.len;
  1887. }
  1888. else {
  1889. bmark->to_tag_val.len = 0;
  1890. bmark->to_tag_val.s = p+(hdr->body.s+hdr->body.len-hdr->name.s);
  1891. }
  1892. /* no break */
  1893. case HDR_FROM_T:
  1894. case HDR_CALLID_T:
  1895. case HDR_CSEQ_T:
  1896. append_str(p, hdr->name.s, hdr->len);
  1897. break;
  1898. default:
  1899. /* do nothing, we are interested only in the above headers */
  1900. ;
  1901. } /* end switch */
  1902. } /* end for */
  1903. /* lumps */
  1904. for(lump=msg->reply_lump;lump;lump=lump->next)
  1905. if (lump->flags&LUMP_RPL_HDR){
  1906. memcpy(p,lump->text.s,lump->text.len);
  1907. p += lump->text.len;
  1908. }
  1909. /* server header */
  1910. if (server_signature) {
  1911. memcpy( p, SERVER_HDR , SERVER_HDR_LEN );
  1912. p+=SERVER_HDR_LEN;
  1913. memcpy( p, CRLF, CRLF_LEN );
  1914. p+=CRLF_LEN;
  1915. }
  1916. /* content_length hdr */
  1917. if (content_len_len) {
  1918. append_str( p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
  1919. append_str( p, content_len_buf, content_len_len );
  1920. append_str( p, CRLF, CRLF_LEN );
  1921. } else {
  1922. append_str( p, CONTENT_LENGTH"0"CRLF,CONTENT_LENGTH_LEN+1+CRLF_LEN);
  1923. }
  1924. /* warning header */
  1925. if (warning_buf) {
  1926. memcpy( p, warning_buf, warning_len);
  1927. p+=warning_len;
  1928. memcpy( p, CRLF, CRLF_LEN);
  1929. p+=CRLF_LEN;
  1930. }
  1931. /*end of message*/
  1932. memcpy( p, CRLF, CRLF_LEN );
  1933. p+=CRLF_LEN;
  1934. /* body */
  1935. if (body) {
  1936. memcpy ( p, body->text.s, body->text.len );
  1937. p+=body->text.len;
  1938. }
  1939. if (len!=p-buf)
  1940. LOG(L_CRIT,"BUG:build_res_from_sip_req: diff len=%d p-buf=%d\n",
  1941. len, (int)(p-buf));
  1942. *(p) = 0;
  1943. *returned_len = len;
  1944. /* in req2reply, received_buf is not introduced to lumps and
  1945. needs to be deleted here
  1946. */
  1947. if (received_buf) pkg_free(received_buf);
  1948. if (rport_buf) pkg_free(rport_buf);
  1949. return buf;
  1950. error01:
  1951. if (received_buf) pkg_free(received_buf);
  1952. if (rport_buf) pkg_free(rport_buf);
  1953. error00:
  1954. *returned_len=0;
  1955. return 0;
  1956. }
  1957. /* return number of chars printed or 0 if space exceeded;
  1958. assumes buffer size of at least MAX_BRANCH_PARAM_LEN
  1959. */
  1960. int branch_builder( unsigned int hash_index,
  1961. /* only either parameter useful */
  1962. unsigned int label, char * char_v,
  1963. int branch,
  1964. char *branch_str, int *len )
  1965. {
  1966. char *begin;
  1967. int size;
  1968. /* hash id provided ... start with it */
  1969. size=MAX_BRANCH_PARAM_LEN;
  1970. begin=branch_str;
  1971. *len=0;
  1972. memcpy(begin, MCOOKIE, MCOOKIE_LEN );
  1973. size-=MCOOKIE_LEN;begin+=MCOOKIE_LEN;
  1974. if (int2reverse_hex( &begin, &size, hash_index)==-1)
  1975. return 0;
  1976. if (size) {
  1977. *begin=BRANCH_SEPARATOR;
  1978. begin++; size--;
  1979. } else return 0;
  1980. /* string with request's characteristic value ... use it ... */
  1981. if (char_v) {
  1982. if (memcpy(begin,char_v,MD5_LEN)) {
  1983. begin+=MD5_LEN; size-=MD5_LEN;
  1984. } else return 0;
  1985. } else { /* ... use the "label" value otherwise */
  1986. if (int2reverse_hex( &begin, &size, label )==-1)
  1987. return 0;
  1988. }
  1989. if (size) {
  1990. *begin=BRANCH_SEPARATOR;
  1991. begin++; size--;
  1992. } else return 0;
  1993. if (int2reverse_hex( &begin, &size, branch)==-1)
  1994. return 0;
  1995. *len=MAX_BRANCH_PARAM_LEN-size;
  1996. return size;
  1997. }
  1998. /* uses only the send_info->send_socket, send_info->proto and
  1999. * send_info->comp (so that a send_info used for sending can be passed
  2000. * to this function w/o changes and the correct via will be built) */
  2001. char* via_builder( unsigned int *len,
  2002. struct dest_info* send_info /* where to send the reply */,
  2003. str* branch, str* extra_params, struct hostport* hp)
  2004. {
  2005. unsigned int via_len, extra_len;
  2006. char *line_buf;
  2007. int max_len;
  2008. int via_prefix_len;
  2009. str* address_str; /* address displayed in via */
  2010. str* port_str; /* port no displayed in via */
  2011. struct socket_info* send_sock;
  2012. int comp_len, comp_name_len;
  2013. char* comp_name;
  2014. send_sock=send_info->send_sock;
  2015. /* use pre-set address in via or the outbound socket one */
  2016. if ( hp && hp->host->len)
  2017. address_str=hp->host;
  2018. else
  2019. address_str=&(send_sock->address_str);
  2020. if (hp && hp->port->len)
  2021. port_str=hp->port;
  2022. else
  2023. port_str=&(send_sock->port_no_str);
  2024. comp_len=comp_name_len=0;
  2025. comp_name=0;
  2026. #ifdef USE_COMP
  2027. switch(send_info->comp){
  2028. case COMP_NONE:
  2029. break;
  2030. case COMP_SIGCOMP:
  2031. comp_len=COMP_PARAM_LEN;
  2032. comp_name_len=SIGCOMP_NAME_LEN;
  2033. comp_name=SIGCOMP_NAME;
  2034. break;
  2035. case COMP_SERGZ:
  2036. comp_len=COMP_PARAM_LEN;
  2037. comp_name_len=SERGZ_NAME_LEN;
  2038. comp_name=SERGZ_NAME;
  2039. break;
  2040. default:
  2041. LOG(L_CRIT, "BUG: via_builder: unknown comp %d\n",
  2042. send_info->comp);
  2043. /* continue, we'll just ignore comp */
  2044. }
  2045. #endif /* USE_COMP */
  2046. via_prefix_len=MY_VIA_LEN+(send_info->proto==PROTO_SCTP);
  2047. max_len=via_prefix_len +address_str->len /* space in MY_VIA */
  2048. +2 /* just in case it is a v6 address ... [ ] */
  2049. +1 /*':'*/+port_str->len
  2050. +(branch?(MY_BRANCH_LEN+branch->len):0)
  2051. +(extra_params?extra_params->len:0)
  2052. +comp_len+comp_name_len
  2053. +CRLF_LEN+1;
  2054. line_buf=pkg_malloc( max_len );
  2055. if (line_buf==0){
  2056. ser_error=E_OUT_OF_MEM;
  2057. LOG(L_ERR, "ERROR: via_builder: out of memory\n");
  2058. return 0;
  2059. }
  2060. extra_len=0;
  2061. via_len=via_prefix_len+address_str->len; /*space included in MY_VIA*/
  2062. memcpy(line_buf, MY_VIA, MY_VIA_LEN);
  2063. if (send_info->proto==PROTO_UDP){
  2064. /* do nothing */
  2065. }else if (send_info->proto==PROTO_TCP){
  2066. memcpy(line_buf+MY_VIA_LEN-4, "TCP ", 4);
  2067. }else if (send_info->proto==PROTO_TLS){
  2068. memcpy(line_buf+MY_VIA_LEN-4, "TLS ", 4);
  2069. }else if (send_info->proto==PROTO_SCTP){
  2070. memcpy(line_buf+MY_VIA_LEN-4, "SCTP ", 5);
  2071. }else{
  2072. LOG(L_CRIT, "BUG: via_builder: unknown proto %d\n", send_info->proto);
  2073. return 0;
  2074. }
  2075. # ifdef USE_IPV6
  2076. /* add [] only if ipv6 and outbound socket address is used;
  2077. * if using pre-set no check is made */
  2078. if ((send_sock->address.af==AF_INET6) &&
  2079. (address_str==&(send_sock->address_str))) {
  2080. line_buf[via_prefix_len]='[';
  2081. line_buf[via_prefix_len+1+address_str->len]=']';
  2082. extra_len=1;
  2083. via_len+=2; /* [ ]*/
  2084. }
  2085. # endif
  2086. memcpy(line_buf+via_prefix_len+extra_len, address_str->s,
  2087. address_str->len);
  2088. if ((send_sock->port_no!=SIP_PORT) ||
  2089. (port_str!=&send_sock->port_no_str)){
  2090. line_buf[via_len]=':'; via_len++;
  2091. memcpy(line_buf+via_len, port_str->s, port_str->len);
  2092. via_len+=port_str->len;
  2093. }
  2094. /* branch parameter */
  2095. if (branch){
  2096. memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
  2097. via_len+=MY_BRANCH_LEN;
  2098. memcpy(line_buf+via_len, branch->s, branch->len );
  2099. via_len+=branch->len;
  2100. }
  2101. /* extra params */
  2102. if (extra_params){
  2103. memcpy(line_buf+via_len, extra_params->s, extra_params->len);
  2104. via_len+=extra_params->len;
  2105. }
  2106. #ifdef USE_COMP
  2107. /* comp */
  2108. if (comp_len){
  2109. memcpy(line_buf+via_len, COMP_PARAM, COMP_PARAM_LEN);
  2110. via_len+=COMP_PARAM_LEN;
  2111. memcpy(line_buf+via_len, comp_name, comp_name_len);
  2112. via_len+=comp_name_len;
  2113. }
  2114. #endif
  2115. memcpy(line_buf+via_len, CRLF, CRLF_LEN);
  2116. via_len+=CRLF_LEN;
  2117. line_buf[via_len]=0; /* null terminate the string*/
  2118. *len = via_len;
  2119. return line_buf;
  2120. }
  2121. /* creates a via header honoring the protocol of the incomming socket
  2122. * msg is an optional parameter */
  2123. char* create_via_hf( unsigned int *len,
  2124. struct sip_msg *msg,
  2125. struct dest_info* send_info /* where to send the reply */,
  2126. str* branch)
  2127. {
  2128. char* via;
  2129. str extra_params;
  2130. struct hostport hp;
  2131. #if defined USE_TCP || defined USE_SCTP
  2132. char* id_buf;
  2133. unsigned int id_len;
  2134. id_buf=0;
  2135. id_len=0;
  2136. #endif
  2137. extra_params.len=0;
  2138. extra_params.s=0;
  2139. #if defined USE_TCP || defined USE_SCTP
  2140. /* add id if tcp */
  2141. if (msg && (
  2142. #ifdef USE_TCP
  2143. (msg->rcv.proto==PROTO_TCP)
  2144. #ifdef USE_TLS
  2145. || (msg->rcv.proto==PROTO_TLS)
  2146. #endif
  2147. #ifdef USE_SCTP
  2148. ||
  2149. #endif /* USE_SCTP */
  2150. #endif /* USE_TCP */
  2151. #ifdef USE_SCTP
  2152. (msg->rcv.proto==PROTO_SCTP)
  2153. #endif /* USE_SCTP */
  2154. )){
  2155. if ((id_buf=id_builder(msg, &id_len))==0){
  2156. LOG(L_ERR, "ERROR: create_via_hf:"
  2157. " id_builder failed\n");
  2158. return 0; /* we don't need to free anything,
  2159. nothing alloc'ed yet*/
  2160. }
  2161. DBG("create_via_hf: id added: <%.*s>, rcv proto=%d\n",
  2162. (int)id_len, id_buf, msg->rcv.proto);
  2163. extra_params.s=id_buf;
  2164. extra_params.len=id_len;
  2165. }
  2166. #endif /* USE_TCP || USE_SCTP */
  2167. set_hostport(&hp, msg);
  2168. via = via_builder( len, send_info, branch,
  2169. extra_params.len?&extra_params:0, &hp);
  2170. #if defined USE_TCP || defined USE_SCTP
  2171. /* we do not need id_buf any more, the id is already in the new via header */
  2172. if (id_buf) pkg_free(id_buf);
  2173. #endif
  2174. return via;
  2175. }
  2176. /* builds a char* buffer from message headers without body
  2177. * first line is excluded in case of skip_first_line=1
  2178. * error is set -1 if the memory allocation failes
  2179. */
  2180. char * build_only_headers( struct sip_msg* msg, int skip_first_line,
  2181. unsigned int *returned_len,
  2182. int *error,
  2183. struct dest_info* send_info)
  2184. {
  2185. char *buf, *new_buf;
  2186. unsigned int offset, s_offset, len, new_len;
  2187. *error = 0;
  2188. buf = msg->buf;
  2189. if (skip_first_line)
  2190. s_offset = msg->headers->name.s - buf;
  2191. else
  2192. s_offset = 0;
  2193. /* original length without body, and without final \r\n */
  2194. len = msg->unparsed - buf;
  2195. /* new msg length */
  2196. new_len = len - /* original length */
  2197. s_offset + /* skipped first line */
  2198. lumps_len(msg, msg->add_rm, send_info); /* lumps */
  2199. if (new_len == 0) {
  2200. *returned_len = 0;
  2201. return 0;
  2202. }
  2203. new_buf = (char *)pkg_malloc(new_len+1);
  2204. if (!new_buf) {
  2205. LOG(L_ERR, "ERROR: build_only_headers: Not enough memory\n");
  2206. *error = -1;
  2207. return 0;
  2208. }
  2209. new_buf[0] = 0;
  2210. offset = 0;
  2211. /* copy message lumps */
  2212. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
  2213. /* copy the rest of the message without body */
  2214. if (len > s_offset) {
  2215. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  2216. offset += (len-s_offset);
  2217. }
  2218. new_buf[offset] = 0;
  2219. *returned_len = offset;
  2220. return new_buf;
  2221. }
  2222. /* builds a char* buffer from message body
  2223. * error is set -1 if the memory allocation failes
  2224. */
  2225. char * build_body( struct sip_msg* msg,
  2226. unsigned int *returned_len,
  2227. int *error,
  2228. struct dest_info* send_info)
  2229. {
  2230. char *buf, *new_buf, *body;
  2231. unsigned int offset, s_offset, len, new_len;
  2232. *error = 0;
  2233. body = get_body(msg);
  2234. if (!body || (body[0] == 0)) {
  2235. *returned_len = 0;
  2236. return 0;
  2237. }
  2238. buf = msg->buf;
  2239. s_offset = body - buf;
  2240. /* original length of msg with body */
  2241. len = msg->len;
  2242. /* new body length */
  2243. new_len = len - /* original length */
  2244. s_offset + /* msg without body */
  2245. lumps_len(msg, msg->body_lumps, send_info); /* lumps */
  2246. new_buf = (char *)pkg_malloc(new_len+1);
  2247. if (!new_buf) {
  2248. LOG(L_ERR, "ERROR: build_body: Not enough memory\n");
  2249. *error = -1;
  2250. return 0;
  2251. }
  2252. new_buf[0] = 0;
  2253. offset = 0;
  2254. /* copy body lumps */
  2255. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
  2256. /* copy the rest of the message without body */
  2257. if (len > s_offset) {
  2258. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  2259. offset += (len-s_offset);
  2260. }
  2261. new_buf[offset] = 0;
  2262. *returned_len = offset;
  2263. return new_buf;
  2264. }
  2265. /* builds a char* buffer from SIP message including body
  2266. * The function adjusts the Content-Length HF according
  2267. * to body lumps in case of touch_clen=1.
  2268. */
  2269. char * build_all( struct sip_msg* msg, int touch_clen,
  2270. unsigned int *returned_len,
  2271. int *error,
  2272. struct dest_info* send_info)
  2273. {
  2274. char *buf, *new_buf;
  2275. unsigned int offset, s_offset, len, new_len;
  2276. unsigned int body_delta;
  2277. *error = 0;
  2278. /* Calculate message body difference */
  2279. body_delta = lumps_len(msg, msg->body_lumps, send_info);
  2280. if (touch_clen) {
  2281. /* adjust Content-Length */
  2282. if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
  2283. LOG(L_ERR, "ERROR: build_all: Error while adjusting"
  2284. " Content-Length\n");
  2285. *error = -1;
  2286. return 0;
  2287. }
  2288. }
  2289. buf = msg->buf;
  2290. /* original msg length */
  2291. len = msg->len;
  2292. /* new msg length */
  2293. new_len = len + /* original length */
  2294. lumps_len(msg, msg->add_rm, send_info) + /* hdr lumps */
  2295. body_delta; /* body lumps */
  2296. if (new_len == 0) {
  2297. returned_len = 0;
  2298. return 0;
  2299. }
  2300. new_buf = (char *)pkg_malloc(new_len+1);
  2301. if (!new_buf) {
  2302. LOG(L_ERR, "ERROR: build_all: Not enough memory\n");
  2303. *error = -1;
  2304. return 0;
  2305. }
  2306. new_buf[0] = 0;
  2307. offset = s_offset = 0;
  2308. /* copy message lumps */
  2309. process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
  2310. /* copy body lumps */
  2311. process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
  2312. /* copy the rest of the message */
  2313. memcpy(new_buf+offset, buf+s_offset, len-s_offset);
  2314. offset += (len-s_offset);
  2315. new_buf[offset] = 0;
  2316. *returned_len = offset;
  2317. return new_buf;
  2318. }