msg_translator.c 79 KB

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