forward.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. /*!
  22. * \file
  23. * \brief Kamailio core :: Message forwarding
  24. * \ingroup core
  25. * Module: \ref core
  26. */
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <netdb.h>
  33. #include <netinet/in.h>
  34. #include <arpa/inet.h>
  35. #include "forward.h"
  36. #include "hash_func.h"
  37. #include "config.h"
  38. #include "parser/msg_parser.h"
  39. #include "char_msg_val.h"
  40. #include "route.h"
  41. #include "events.h"
  42. #include "dprint.h"
  43. #include "globals.h"
  44. #include "cfg_core.h"
  45. #include "data_lump.h"
  46. #include "ut.h"
  47. #include "mem/mem.h"
  48. #include "msg_translator.h"
  49. #include "sr_module.h"
  50. #include "ip_addr.h"
  51. #include "resolve.h"
  52. #include "name_alias.h"
  53. #include "socket_info.h"
  54. #include "onsend.h"
  55. #include "resolve.h"
  56. #ifdef USE_DNS_FAILOVER
  57. #include "dns_cache.h"
  58. #endif
  59. #ifdef USE_DST_BLACKLIST
  60. #include "dst_blacklist.h"
  61. #endif
  62. #include "compiler_opt.h"
  63. #include "core_stats.h"
  64. #ifdef DEBUG_DMALLOC
  65. #include <dmalloc.h>
  66. #endif
  67. /* return a socket_info_pointer to the sending socket; as opposed to
  68. * get_send_socket, which returns process's default socket, get_out_socket
  69. * attempts to determine the outbound interface which will be used;
  70. * it uses a temporary connected socket to determine it; it will
  71. * be very likely noticeably slower, but it can deal better with
  72. * multihomed hosts
  73. */
  74. static int mhomed_sock_cache_disabled = 0;
  75. static int sock_inet = -1;
  76. static int sock_inet6 = -1;
  77. static int _forward_set_send_info = 0;
  78. void forward_set_send_info(int v)
  79. {
  80. _forward_set_send_info = v;
  81. }
  82. static void apply_force_send_socket(struct dest_info* dst, struct sip_msg* msg);
  83. struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
  84. {
  85. int* temp_sock;
  86. socklen_t len;
  87. union sockaddr_union from;
  88. struct socket_info* si;
  89. struct ip_addr ip;
  90. union sockaddr_union uncon;
  91. memset(&uncon, 0, sizeof(union sockaddr_union));
  92. uncon.sin.sin_family = AF_UNSPEC;
  93. if (unlikely(proto!=PROTO_UDP)) {
  94. LM_CRIT("can only be called for UDP\n");
  95. return 0;
  96. }
  97. retry:
  98. switch(to->s.sa_family){
  99. case AF_INET : {
  100. if(unlikely(sock_inet < 0)){
  101. sock_inet = socket(AF_INET, SOCK_DGRAM, 0);
  102. if (sock_inet==-1) {
  103. LM_ERR("socket() failed: %s\n", strerror(errno));
  104. return 0;
  105. }
  106. }
  107. temp_sock = &sock_inet;
  108. break;
  109. }
  110. case AF_INET6 : {
  111. if(unlikely(sock_inet6 < 0)){
  112. sock_inet6 = socket(AF_INET6, SOCK_DGRAM, 0);
  113. if (sock_inet6==-1) {
  114. LM_ERR("socket() failed: %s\n", strerror(errno));
  115. return 0;
  116. }
  117. }
  118. temp_sock = &sock_inet6;
  119. break;
  120. }
  121. default: {
  122. LM_ERR("Unknown protocol family \n");
  123. return 0;
  124. }
  125. }
  126. if( !mhomed_sock_cache_disabled ){
  127. /* some Linux kernel versions (all?) along with other UNIXes don't re-bound the sock if already bound */
  128. /* to un-bound a socket set sin_family to AF_UNSPEC and zero out the rest*/
  129. if (unlikely(connect(*temp_sock, &uncon.s, sockaddru_len(uncon)) < 0))
  130. mhomed_sock_cache_disabled = 1;
  131. }
  132. if (unlikely(connect(*temp_sock, &to->s, sockaddru_len(*to))==-1)) {
  133. if (unlikely(errno==EISCONN && !mhomed_sock_cache_disabled)){
  134. /* no multiple connects support on the same socket */
  135. mhomed_sock_cache_disabled=1;
  136. if (sock_inet>=0){
  137. close(sock_inet);
  138. sock_inet=-1;
  139. }
  140. if (sock_inet6>=0){
  141. close(sock_inet6);
  142. sock_inet6=-1;
  143. }
  144. goto retry;
  145. }
  146. LM_ERR("connect failed: %s\n", strerror(errno));
  147. goto error;
  148. }
  149. len=sizeof(from);
  150. if (unlikely(getsockname(*temp_sock, &from.s, &len)==-1)) {
  151. LM_ERR("getsockname failed: %s\n", strerror(errno));
  152. goto error;
  153. }
  154. su2ip_addr(&ip, &from);
  155. si=find_si(&ip, 0, proto);
  156. if (si==0) goto error;
  157. LM_DBG("socket determined: %p\n", si );
  158. if (unlikely(mhomed_sock_cache_disabled)){
  159. close(*temp_sock);
  160. *temp_sock=-1;
  161. }
  162. return si;
  163. error:
  164. LM_ERR("no socket found\n");
  165. ERR("no corresponding socket found for(%s:%s)\n",
  166. proto2a(proto), su2a(to, sizeof(*to)));
  167. if (unlikely(mhomed_sock_cache_disabled && *temp_sock >=0)){
  168. close(*temp_sock);
  169. *temp_sock=-1;
  170. }
  171. return 0;
  172. }
  173. /** get the sending socket for a corresponding destination.
  174. * @param force_send_socket - if !=0 and the protocol and af correspond
  175. * with the destination, it will be returned.
  176. * If the protocol or af check fail, a look-alike
  177. * socket will be searched for and mismatch will be
  178. * set. If no look-alike socket is found it will
  179. * fallback to normal resolution.
  180. * @param to - destination
  181. * @param proto - protocol
  182. * @param mismatch - result parameter, set if a force_send_socket was used, but
  183. * there was an error matching it exactly to the destination.
  184. * Possible values: 0 ok, SS_MISMATCH_PROTO,
  185. * SS_MISMATCH_ADDR, SS_MISMATCH_AF, SS_MISMATCH_MCAST.
  186. * @return a socket_info pointer to the sending socket on success (and possibly
  187. * sets mismatch) or 0 on error.
  188. */
  189. struct socket_info* get_send_socket2(struct socket_info* force_send_socket,
  190. union sockaddr_union* to, int proto,
  191. enum ss_mismatch* mismatch)
  192. {
  193. struct socket_info* send_sock;
  194. struct socket_info* orig;
  195. if (likely(mismatch)) *mismatch=0;
  196. /* check if send interface is not forced */
  197. if (unlikely(force_send_socket)){
  198. orig=force_send_socket;
  199. /* Special case here as there is no ;transport=wss - so wss connections will
  200. appear as ws ones and be sorted out in the WebSocket module */
  201. if (unlikely(orig->proto!=proto && !(orig->proto==PROTO_TLS && proto==PROTO_WS))){
  202. force_send_socket=find_si(&(force_send_socket->address),
  203. force_send_socket->port_no,
  204. proto);
  205. if (unlikely(force_send_socket == 0)){
  206. if (likely(mismatch)) *mismatch=SS_MISMATCH_ADDR;
  207. LM_WARN("protocol/port mismatch (forced %s:%s:%d, to %s:%s)\n",
  208. proto2a(orig->proto), ip_addr2a(&orig->address),
  209. orig->port_no,
  210. proto2a(proto), su2a(to, sizeof(*to)));
  211. goto not_forced;
  212. }
  213. if (likely(mismatch)) *mismatch=SS_MISMATCH_PROTO;
  214. }
  215. if (unlikely(force_send_socket->address.af!=to->s.sa_family)){
  216. LM_DBG("force_send_socket of different af"
  217. " (dst %d - %s:%s forced %d -%s:%s:%d)\n",
  218. to->s.sa_family, proto2a(proto), su2a(to, sizeof(*to)),
  219. force_send_socket->address.af,
  220. proto2a(force_send_socket->proto),
  221. ip_addr2a(&force_send_socket->address),
  222. force_send_socket->port_no);
  223. if (likely(mismatch)) *mismatch=SS_MISMATCH_AF;
  224. goto not_forced;
  225. }
  226. /* check if listening on the socket (the check does not work
  227. for TCP and TLS, for them socket==-1 on all the processes
  228. except tcp_main(), see close_extra_socks() */
  229. if (likely((force_send_socket->socket!=-1 ||
  230. force_send_socket->proto==PROTO_TCP ||
  231. force_send_socket->proto==PROTO_TLS ||
  232. force_send_socket->proto==PROTO_WS ||
  233. force_send_socket->proto==PROTO_WSS) &&
  234. !(force_send_socket->flags & SI_IS_MCAST)))
  235. return force_send_socket;
  236. else{
  237. if (!(force_send_socket->flags & SI_IS_MCAST))
  238. LM_WARN("not listening on the requested socket (%s:%s:%d),"
  239. " no fork mode?\n",
  240. proto2a(force_send_socket->proto),
  241. ip_addr2a(&force_send_socket->address),
  242. force_send_socket->port_no);
  243. else if (likely(mismatch)) *mismatch=SS_MISMATCH_MCAST;
  244. }
  245. };
  246. not_forced:
  247. if (mhomed && proto==PROTO_UDP){
  248. send_sock=get_out_socket(to, proto);
  249. if ((send_sock==0) || (send_sock->socket!=-1))
  250. return send_sock; /* found or error*/
  251. else if (send_sock->socket==-1){
  252. LM_WARN("not listening on the"
  253. " requested socket (%s:%s:%d), no fork mode?\n",
  254. proto2a(send_sock->proto), ip_addr2a(&send_sock->address),
  255. send_sock->port_no);
  256. /* continue: try to use some socket */
  257. }
  258. }
  259. send_sock=0;
  260. /* check if we need to change the socket (different address families -
  261. * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
  262. switch(proto){
  263. #ifdef USE_TCP
  264. case PROTO_WS:
  265. case PROTO_TCP:
  266. /* on tcp just use the "main address", we don't really now the
  267. * sending address (we can find it out, but we'll need also to see
  268. * if we listen on it, and if yes on which port -> too complicated*/
  269. switch(to->s.sa_family){
  270. /* FIXME */
  271. case AF_INET: send_sock=sendipv4_tcp;
  272. break;
  273. case AF_INET6: send_sock=sendipv6_tcp;
  274. break;
  275. default: LM_ERR("don't know how to forward to af %d\n",
  276. to->s.sa_family);
  277. }
  278. break;
  279. #endif
  280. #ifdef USE_TLS
  281. case PROTO_WSS:
  282. case PROTO_TLS:
  283. switch(to->s.sa_family){
  284. /* FIXME */
  285. case AF_INET: send_sock=sendipv4_tls;
  286. break;
  287. case AF_INET6: send_sock=sendipv6_tls;
  288. break;
  289. default: LM_ERR("don't know how to forward to af %d\n",
  290. to->s.sa_family);
  291. }
  292. break;
  293. #endif /* USE_TLS */
  294. #ifdef USE_SCTP
  295. case PROTO_SCTP:
  296. if ((bind_address==0) ||
  297. (to->s.sa_family!=bind_address->address.af) ||
  298. (bind_address->proto!=PROTO_SCTP)){
  299. switch(to->s.sa_family){
  300. case AF_INET: send_sock=sendipv4_sctp;
  301. break;
  302. case AF_INET6: send_sock=sendipv6_sctp;
  303. break;
  304. default: LM_ERR("don't know how to forward to af %d\n",
  305. to->s.sa_family);
  306. }
  307. }else send_sock=bind_address;
  308. break;
  309. #endif /* USE_SCTP */
  310. case PROTO_UDP:
  311. if ((bind_address==0) ||
  312. (to->s.sa_family!=bind_address->address.af) ||
  313. (bind_address->proto!=PROTO_UDP)){
  314. switch(to->s.sa_family){
  315. case AF_INET: send_sock=sendipv4;
  316. break;
  317. case AF_INET6: send_sock=sendipv6;
  318. break;
  319. default: LM_ERR("don't know how to forward to af %d\n",
  320. to->s.sa_family);
  321. }
  322. }else send_sock=bind_address;
  323. break;
  324. default:
  325. LM_CRIT("unsupported proto %d (%s)\n", proto, proto2a(proto));
  326. }
  327. return send_sock;
  328. }
  329. static struct _check_self_func {
  330. check_self_f fself;
  331. struct _check_self_func *next;
  332. } *_check_self_func_list = NULL;
  333. /** check if _check_self_func_list is set
  334. * - return 1 if yes, 0 if no
  335. */
  336. int is_check_self_func_list_set(void)
  337. {
  338. return (_check_self_func_list)?1:0;
  339. }
  340. /** register a function to be called when matching for myself
  341. * - return 0 on success, -1 on error
  342. * - f must have same prototype as check_self() and return same kind of values
  343. */
  344. int register_check_self_func(check_self_f f)
  345. {
  346. struct _check_self_func *nf = 0;
  347. nf=(struct _check_self_func*)pkg_malloc(sizeof(struct _check_self_func));
  348. if(nf==0)
  349. {
  350. LM_ERR("no more pkg\n");
  351. return -1;
  352. }
  353. nf->fself = f;
  354. nf->next = _check_self_func_list;
  355. _check_self_func_list = nf;
  356. return 0;
  357. }
  358. /** run registered check self functions
  359. * returns 1 if true, 0 if false
  360. */
  361. int run_check_self_func(str* host, unsigned short port, unsigned short proto)
  362. {
  363. struct _check_self_func *sf = 0;
  364. if(_check_self_func_list==NULL)
  365. return 0;
  366. for(sf=_check_self_func_list; sf; sf=sf->next)
  367. if(sf->fself(host, port, proto)==1)
  368. return 1;
  369. return 0;
  370. }
  371. /** checks if the proto: host:port is one of the address we listen on;
  372. *
  373. * if port==0, the port number is ignored
  374. * if proto==0 (PROTO_NONE) the protocol is ignored
  375. * returns 1 if true, 0 if false, -1 on error
  376. * WARNING: uses str2ip6 so it will overwrite any previous
  377. * unsaved result of this function (static buffer)
  378. */
  379. int check_self(str* host, unsigned short port, unsigned short proto)
  380. {
  381. if (grep_sock_info(host, port, proto)) goto found;
  382. /* try to look into the aliases*/
  383. if (grep_aliases(host->s, host->len, port, proto)==0){
  384. LM_DBG("host != me\n");
  385. return (_check_self_func_list==NULL)?0:run_check_self_func(host,
  386. port, proto);
  387. }
  388. found:
  389. return 1;
  390. }
  391. /** checks if the proto:port is one of the ports we listen on;
  392. * if proto==0 (PROTO_NONE) the protocol is ignored
  393. * returns 1 if true, 0 if false, -1 on error
  394. */
  395. int check_self_port(unsigned short port, unsigned short proto)
  396. {
  397. if (grep_sock_info_by_port(port, proto))
  398. /* as aliases do not contain different ports we can skip them */
  399. return 1;
  400. else
  401. return 0;
  402. }
  403. /** forwards a request to dst
  404. * parameters:
  405. * msg - sip msg
  406. * dst - destination name, if non-null it will be resolved and
  407. * send_info updated with the ip/port. Even if dst is non
  408. * null send_info must contain the protocol and if a non
  409. * default port or non srv. lookup is desired, the port must
  410. * be !=0
  411. * port - used only if dst!=0 (else the port in send_info->to is used)
  412. * send_info - value/result partially filled dest_info structure:
  413. * - send_info->proto and comp are used
  414. * - send_info->to will be filled (dns)
  415. * - send_info->send_flags is filled from the message
  416. * - if the send_socket member is null, a send_socket will be
  417. * chosen automatically
  418. * WARNING: don't forget to zero-fill all the unused members (a non-zero
  419. * random id along with proto==PROTO_TCP can have bad consequences, same for
  420. * a bogus send_socket value)
  421. */
  422. int forward_request(struct sip_msg* msg, str* dst, unsigned short port,
  423. struct dest_info* send_info)
  424. {
  425. unsigned int len;
  426. char* buf;
  427. char md5[MD5_LEN];
  428. struct socket_info* orig_send_sock; /* initial send_sock */
  429. int ret;
  430. struct ip_addr ip; /* debugging only */
  431. char proto;
  432. struct onsend_info onsnd_info = {0};
  433. #ifdef USE_DNS_FAILOVER
  434. struct socket_info* prev_send_sock;
  435. int err;
  436. struct dns_srv_handle dns_srv_h;
  437. prev_send_sock=0;
  438. err=0;
  439. #endif
  440. buf=0;
  441. orig_send_sock=send_info->send_sock;
  442. proto=send_info->proto;
  443. ret=0;
  444. if(dst){
  445. #ifdef USE_DNS_FAILOVER
  446. if (cfg_get(core, core_cfg, use_dns_failover)){
  447. dns_srv_handle_init(&dns_srv_h);
  448. err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
  449. &proto, dns_flags);
  450. if (err!=0){
  451. LM_ERR("resolving \"%.*s\" failed: %s [%d]\n",
  452. dst->len, ZSW(dst->s), dns_strerror(err), err);
  453. ret=E_BAD_ADDRESS;
  454. goto error;
  455. }
  456. }else
  457. #endif
  458. if (sip_hostport2su(&send_info->to, dst, port, &proto)<0){
  459. LM_ERR("bad host name %.*s, dropping packet\n", dst->len, ZSW(dst->s));
  460. ret=E_BAD_ADDRESS;
  461. goto error;
  462. }
  463. }/* dst */
  464. send_info->send_flags=msg->fwd_send_flags;
  465. /* calculate branch for outbound request;
  466. calculate is from transaction key, i.e., as an md5 of From/To/CallID/
  467. CSeq exactly the same way as TM does; good for reboot -- than messages
  468. belonging to transaction lost due to reboot will still be forwarded
  469. with the same branch parameter and will be match-able downstream
  470. */
  471. if (!char_msg_val( msg, md5 )) { /* parses transaction key */
  472. LM_ERR("char_msg_val failed\n");
  473. ret=E_UNSPEC;
  474. goto error;
  475. }
  476. msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
  477. if (!branch_builder( msg->hash_index, 0, md5, 0 /* 0-th branch */,
  478. msg->add_to_branch_s, &msg->add_to_branch_len )) {
  479. LM_ERR("branch_builder failed\n");
  480. ret=E_UNSPEC;
  481. goto error;
  482. }
  483. /* try to send the message until success or all the ips are exhausted
  484. * (if dns lookup is performed && the dns cache used ) */
  485. #ifdef USE_DNS_FAILOVER
  486. do{
  487. #endif
  488. if (orig_send_sock==0) /* no forced send_sock => find it **/
  489. send_info->send_sock=get_send_socket(msg, &send_info->to, proto);
  490. if (send_info->send_sock==0){
  491. LM_ERR("cannot forward to af %d, proto %d "
  492. "no corresponding listening socket\n",
  493. send_info->to.s.sa_family, proto);
  494. ret=ser_error=E_NO_SOCKET;
  495. #ifdef USE_DNS_FAILOVER
  496. /* continue, maybe we find a socket for some other ip */
  497. continue;
  498. #else
  499. goto error;
  500. #endif
  501. }
  502. #ifdef USE_DNS_FAILOVER
  503. if (prev_send_sock!=send_info->send_sock){
  504. /* rebuild the message only if the send_sock changed */
  505. prev_send_sock=send_info->send_sock;
  506. #endif
  507. if (buf) pkg_free(buf);
  508. send_info->proto=proto;
  509. buf = build_req_buf_from_sip_req(msg, &len, send_info, 0);
  510. if (!buf){
  511. LM_ERR("building failed\n");
  512. ret=E_OUT_OF_MEM; /* most probable */
  513. goto error;
  514. }
  515. #ifdef USE_DNS_FAILOVER
  516. }
  517. #endif
  518. /* send it! */
  519. LM_DBG("Sending:\n%.*s.\n", (int)len, buf);
  520. LM_DBG("orig. len=%d, new_len=%d, proto=%d\n",
  521. msg->len, len, send_info->proto );
  522. if (run_onsend(msg, send_info, buf, len)==0){
  523. su2ip_addr(&ip, &send_info->to);
  524. LM_INFO("request to %s:%d(%d) dropped (onsend_route)\n",
  525. ip_addr2a(&ip), su_getport(&send_info->to), send_info->proto);
  526. ser_error=E_OK; /* no error */
  527. ret=E_ADM_PROHIBITED;
  528. #ifdef USE_DNS_FAILOVER
  529. continue; /* try another ip */
  530. #else
  531. goto error; /* error ? */
  532. #endif
  533. }
  534. #ifdef USE_DST_BLACKLIST
  535. if (cfg_get(core, core_cfg, use_dst_blacklist)){
  536. if (dst_is_blacklisted(send_info, msg)){
  537. su2ip_addr(&ip, &send_info->to);
  538. LM_DBG("blacklisted destination:%s:%d (%d)\n",
  539. ip_addr2a(&ip), su_getport(&send_info->to), send_info->proto);
  540. ret=ser_error=E_SEND;
  541. #ifdef USE_DNS_FAILOVER
  542. continue; /* try another ip */
  543. #else
  544. goto error;
  545. #endif
  546. }
  547. }
  548. #endif
  549. if(unlikely(_forward_set_send_info==1)) {
  550. onsnd_info.to=&send_info->to;
  551. onsnd_info.send_sock=send_info->send_sock;
  552. onsnd_info.buf=buf;
  553. onsnd_info.len=len;
  554. onsnd_info.msg=msg;
  555. p_onsend=&onsnd_info;
  556. }
  557. if (msg_send(send_info, buf, len)<0){
  558. p_onsend=0;
  559. ret=ser_error=E_SEND;
  560. #ifdef USE_DST_BLACKLIST
  561. (void)dst_blacklist_add(BLST_ERR_SEND, send_info, msg);
  562. #endif
  563. #ifdef USE_DNS_FAILOVER
  564. continue; /* try another ip */
  565. #else
  566. goto error;
  567. #endif
  568. }else{
  569. p_onsend=0;
  570. ret=ser_error=E_OK;
  571. /* sent requests stats */
  572. STATS_TX_REQUEST( msg->first_line.u.request.method_value );
  573. /* exit succcesfully */
  574. goto end;
  575. }
  576. #ifdef USE_DNS_FAILOVER
  577. }while(dst && cfg_get(core, core_cfg, use_dns_failover) &&
  578. dns_srv_handle_next(&dns_srv_h, err) &&
  579. ((err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
  580. &proto, dns_flags))==0));
  581. if ((err!=0) && (err!=-E_DNS_EOR)){
  582. LM_ERR("resolving %.*s host name in uri failed: %s [%d] (dropping packet)\n",
  583. dst->len, ZSW(dst->s),
  584. dns_strerror(err), err);
  585. ret=ser_error=E_BAD_ADDRESS;
  586. goto error;
  587. }
  588. #endif
  589. error:
  590. STATS_TX_DROPS;
  591. end:
  592. #ifdef USE_DNS_FAILOVER
  593. if (dst && cfg_get(core, core_cfg, use_dns_failover)){
  594. dns_srv_handle_put(&dns_srv_h);
  595. }
  596. #endif
  597. if (buf) pkg_free(buf);
  598. /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
  599. #if defined STATS_REQ_FWD_OK || defined STATS_REQ_FWD_DROP
  600. if(ret==0)
  601. STATS_REQ_FWD_OK();
  602. else
  603. STATS_REQ_FWD_DROP();
  604. #endif /* STATS_REQ_FWD_* */
  605. return ret;
  606. }
  607. int update_sock_struct_from_via( union sockaddr_union* to,
  608. struct sip_msg* msg,
  609. struct via_body* via )
  610. {
  611. struct hostent* he;
  612. str* name;
  613. int err;
  614. unsigned short port;
  615. char proto;
  616. port=0;
  617. if(via==msg->via1){
  618. /* _local_ reply, we ignore any rport or received value
  619. * (but we will send back to the original port if rport is
  620. * present) */
  621. if ((msg->msg_flags&FL_FORCE_RPORT)||(via->rport))
  622. port=msg->rcv.src_port;
  623. else port=via->port;
  624. name=&(via->host); /* received=ip in 1st via is ignored (it's
  625. not added by us so it's bad) */
  626. }else{
  627. /* "normal" reply, we use rport's & received value if present */
  628. if (via->rport && via->rport->value.s){
  629. LM_DBG("using 'rport'\n");
  630. port=str2s(via->rport->value.s, via->rport->value.len, &err);
  631. if (err){
  632. LM_ERR("bad rport value(%.*s)\n",
  633. via->rport->value.len, via->rport->value.s);
  634. port=0;
  635. }
  636. }
  637. if (via->received){
  638. LM_DBG("using 'received'\n");
  639. name=&(via->received->value);
  640. /* making sure that we won't do SRV lookup on "received"
  641. * (possible if no DNS_IP_HACK is used)*/
  642. if (port==0) port=via->port?via->port:SIP_PORT;
  643. }else{
  644. LM_DBG("using via host\n");
  645. name=&(via->host);
  646. if (port==0) port=via->port;
  647. }
  648. }
  649. /* we do now a malloc/memcpy because gethostbyname loves \0-terminated
  650. strings; -jiri
  651. but only if host is not null terminated
  652. (host.s[len] will always be ok for a via)
  653. BTW: when is via->host.s non null terminated? tm copy? - andrei
  654. Yes -- it happened on generating a 408 by TM; -jiri
  655. sip_resolvehost now accepts str -janakj
  656. */
  657. LM_DBG("trying SRV lookup\n");
  658. proto=via->proto;
  659. he=sip_resolvehost(name, &port, &proto);
  660. if (he==0){
  661. LM_NOTICE("resolve_host(%.*s) failure\n", name->len, name->s);
  662. return -1;
  663. }
  664. hostent2su(to, he, 0, port);
  665. return 1;
  666. }
  667. /** removes first via & sends msg to the second
  668. * - mode param controls if modules sip response callbacks are executed */
  669. static int do_forward_reply(struct sip_msg* msg, int mode)
  670. {
  671. char* new_buf;
  672. struct dest_info dst;
  673. unsigned int new_len;
  674. int r;
  675. struct ip_addr ip;
  676. #ifdef USE_TCP
  677. char* s;
  678. int len;
  679. #endif
  680. init_dest_info(&dst);
  681. new_buf=0;
  682. /*check if first via host = us */
  683. if (check_via){
  684. if (check_self(&msg->via1->host,
  685. msg->via1->port?msg->via1->port:SIP_PORT,
  686. msg->via1->proto)!=1){
  687. LM_ERR("host in first via!=me : %.*s:%d\n",
  688. msg->via1->host.len, msg->via1->host.s, msg->via1->port);
  689. /* send error msg back? */
  690. goto error;
  691. }
  692. }
  693. /* check modules response_f functions */
  694. if(likely(mode==0)) {
  695. for (r=0; r<mod_response_cbk_no; r++)
  696. if (mod_response_cbks[r](msg)==0) goto skip;
  697. }
  698. /* we have to forward the reply stateless, so we need second via -bogdan*/
  699. if (parse_headers( msg, HDR_VIA2_F, 0 )==-1
  700. || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
  701. {
  702. /* no second via => error */
  703. LM_DBG("reply cannot be forwarded - no 2nd via\n");
  704. goto error;
  705. }
  706. new_buf = build_res_buf_from_sip_res( msg, &new_len);
  707. if (!new_buf){
  708. LM_ERR("building failed\n");
  709. goto error;
  710. }
  711. dst.proto=msg->via2->proto;
  712. SND_FLAGS_OR(&dst.send_flags, &msg->fwd_send_flags, &msg->rpl_send_flags);
  713. if (update_sock_struct_from_via( &dst.to, msg, msg->via2 )==-1) goto error;
  714. #ifdef USE_COMP
  715. dst.comp=msg->via2->comp_no;
  716. #endif
  717. #if defined USE_TCP || defined USE_SCTP
  718. if (
  719. #ifdef USE_TCP
  720. dst.proto==PROTO_TCP
  721. || dst.proto==PROTO_WS
  722. #ifdef USE_TLS
  723. || dst.proto==PROTO_TLS
  724. || dst.proto==PROTO_WSS
  725. #endif
  726. #ifdef USE_SCTP
  727. ||
  728. #endif /* USE_SCTP */
  729. #endif /* USE_TCP */
  730. #ifdef USE_SCTP
  731. dst.proto==PROTO_SCTP
  732. #endif /* USE_SCTP */
  733. ){
  734. /* find id in i param if it exists */
  735. if (msg->via1->i && msg->via1->i->value.s){
  736. s=msg->via1->i->value.s;
  737. len=msg->via1->i->value.len;
  738. LM_DBG("i=%.*s\n",len, ZSW(s));
  739. if (reverse_hex2int(s, len, (unsigned int*)&dst.id)<0){
  740. LM_ERR("bad via i param \"%.*s\"\n", len, ZSW(s));
  741. dst.id=0;
  742. }
  743. }
  744. }
  745. #endif
  746. apply_force_send_socket(&dst, msg);
  747. /* call onsend_route */
  748. if(dst.send_sock == NULL) {
  749. dst.send_sock=get_send_socket(msg, &dst.to, dst.proto);
  750. if (dst.send_sock==0){
  751. LM_ERR("cannot forward reply\n");
  752. goto done;
  753. }
  754. }
  755. if (onsend_route_enabled(SIP_REPLY)){
  756. if (run_onsend(msg, &dst, new_buf, new_len)==0){
  757. su2ip_addr(&ip, &(dst.to));
  758. LOG(L_ERR, "forward_reply: reply to %s:%d(%d) dropped"
  759. " (onsend_route)\n", ip_addr2a(&ip),
  760. su_getport(&(dst.to)), dst.proto);
  761. goto error; /* error ? */
  762. }
  763. }
  764. if (msg_send(&dst, new_buf, new_len)<0)
  765. {
  766. STATS_RPL_FWD_DROP();
  767. goto error;
  768. }
  769. done:
  770. #ifdef STATS
  771. STATS_TX_RESPONSE( (msg->first_line.u.reply.statuscode/100) );
  772. #endif
  773. LM_DBG("reply forwarded to %.*s:%d\n",
  774. msg->via2->host.len, msg->via2->host.s,
  775. (unsigned short) msg->via2->port);
  776. STATS_RPL_FWD_OK();
  777. pkg_free(new_buf);
  778. skip:
  779. return 0;
  780. error:
  781. if (new_buf) pkg_free(new_buf);
  782. return -1;
  783. }
  784. /** removes first via & sends msg to the second */
  785. int forward_reply(struct sip_msg* msg)
  786. {
  787. return do_forward_reply(msg, 0);
  788. }
  789. /** removes first via & sends msg to the second - no module callbacks */
  790. int forward_reply_nocb(struct sip_msg* msg)
  791. {
  792. return do_forward_reply(msg, 1);
  793. }
  794. static void apply_force_send_socket(struct dest_info* dst, struct sip_msg* msg)
  795. {
  796. if (msg->force_send_socket != 0) {
  797. dst->send_sock = get_send_socket(msg, &dst->to, dst->proto);
  798. }
  799. }