forward.c 26 KB

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