forward.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. #ifdef USE_IPV6
  114. static int sock_inet6 = -1;
  115. #endif /* USE_IPV6 */
  116. struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
  117. {
  118. int* temp_sock;
  119. socklen_t len;
  120. union sockaddr_union from;
  121. struct socket_info* si;
  122. struct ip_addr ip;
  123. union sockaddr_union uncon;
  124. memset(&uncon, 0, sizeof(union sockaddr_union));
  125. uncon.sin.sin_family = AF_UNSPEC;
  126. if (unlikely(proto!=PROTO_UDP)) {
  127. LOG(L_CRIT, "BUG: get_out_socket can only be called for UDP\n");
  128. return 0;
  129. }
  130. retry:
  131. switch(to->s.sa_family){
  132. case AF_INET : {
  133. if(unlikely(sock_inet < 0)){
  134. sock_inet = socket(AF_INET, SOCK_DGRAM, 0);
  135. if (sock_inet==-1) {
  136. LM_ERR("socket() failed: %s\n", strerror(errno));
  137. return 0;
  138. }
  139. }
  140. temp_sock = &sock_inet;
  141. break;
  142. }
  143. #ifdef USE_IPV6
  144. case AF_INET6 : {
  145. if(unlikely(sock_inet6 < 0)){
  146. sock_inet6 = socket(AF_INET6, SOCK_DGRAM, 0);
  147. if (sock_inet6==-1) {
  148. LM_ERR("socket() failed: %s\n", strerror(errno));
  149. return 0;
  150. }
  151. }
  152. temp_sock = &sock_inet6;
  153. break;
  154. }
  155. #endif /* USE_IPV6 */
  156. default: {
  157. LM_ERR("Unknown protocol family \n");
  158. return 0;
  159. }
  160. }
  161. if( !mhomed_sock_cache_disabled ){
  162. /* some Linux kernel versions (all?) along with other UNIXes don't re-bound the sock if already bound */
  163. /* to un-bound a socket set sin_family to AF_UNSPEC and zero out the rest*/
  164. if (unlikely(connect(*temp_sock, &uncon.s, sockaddru_len(uncon)) < 0))
  165. mhomed_sock_cache_disabled = 1;
  166. }
  167. if (unlikely(connect(*temp_sock, &to->s, sockaddru_len(*to))==-1)) {
  168. if (unlikely(errno==EISCONN && !mhomed_sock_cache_disabled)){
  169. /* no multiple connects support on the same socket */
  170. mhomed_sock_cache_disabled=1;
  171. if (sock_inet>=0){
  172. close(sock_inet);
  173. sock_inet=-1;
  174. }
  175. #ifdef USE_IPV6
  176. if (sock_inet6>=0){
  177. close(sock_inet6);
  178. sock_inet6=-1;
  179. }
  180. #endif /* USE_IPV6 */
  181. goto retry;
  182. }
  183. LOG(L_ERR, "ERROR: get_out_socket: connect failed: %s\n",
  184. strerror(errno));
  185. goto error;
  186. }
  187. len=sizeof(from);
  188. if (unlikely(getsockname(*temp_sock, &from.s, &len)==-1)) {
  189. LOG(L_ERR, "ERROR: get_out_socket: getsockname failed: %s\n",
  190. strerror(errno));
  191. goto error;
  192. }
  193. su2ip_addr(&ip, &from);
  194. si=find_si(&ip, 0, proto);
  195. if (si==0) goto error;
  196. DBG("DEBUG: get_out_socket: socket determined: %p\n", si );
  197. if (unlikely(mhomed_sock_cache_disabled)){
  198. close(*temp_sock);
  199. *temp_sock=-1;
  200. }
  201. return si;
  202. error:
  203. LOG(L_ERR, "ERROR: get_out_socket: no socket found\n");
  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. if (unlikely(force_send_socket->proto!=proto)){
  236. orig=force_send_socket;
  237. force_send_socket=find_si(&(force_send_socket->address),
  238. force_send_socket->port_no,
  239. proto);
  240. if (unlikely(force_send_socket == 0)){
  241. if (likely(mismatch)) *mismatch=SS_MISMATCH_ADDR;
  242. LOG(L_WARN, "WARNING: get_send_socket: "
  243. "protocol/port mismatch (forced %s:%s:%d,"
  244. " 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->flags & SI_IS_MCAST)))
  270. return force_send_socket;
  271. else{
  272. if (!(force_send_socket->flags & SI_IS_MCAST))
  273. LOG(L_WARN, "WARNING: get_send_socket: not listening"
  274. " on the requested socket (%s:%s:%d),"
  275. " no fork mode?\n",
  276. proto2a(force_send_socket->proto),
  277. ip_addr2a(&force_send_socket->address),
  278. force_send_socket->port_no);
  279. else if (likely(mismatch)) *mismatch=SS_MISMATCH_MCAST;
  280. }
  281. };
  282. not_forced:
  283. if (mhomed && proto==PROTO_UDP){
  284. send_sock=get_out_socket(to, proto);
  285. if ((send_sock==0) || (send_sock->socket!=-1))
  286. return send_sock; /* found or error*/
  287. else if (send_sock->socket==-1){
  288. LOG(L_WARN, "WARNING: get_send_socket: not listening on the"
  289. " requested socket (%s:%s:%d), no fork mode?\n",
  290. proto2a(send_sock->proto), ip_addr2a(&send_sock->address),
  291. send_sock->port_no);
  292. /* continue: try to use some socket */
  293. }
  294. }
  295. send_sock=0;
  296. /* check if we need to change the socket (different address families -
  297. * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
  298. switch(proto){
  299. #ifdef USE_TCP
  300. case PROTO_TCP:
  301. /* on tcp just use the "main address", we don't really now the
  302. * sending address (we can find it out, but we'll need also to see
  303. * if we listen on it, and if yes on which port -> too complicated*/
  304. switch(to->s.sa_family){
  305. /* FIXME */
  306. case AF_INET: send_sock=sendipv4_tcp;
  307. break;
  308. #ifdef USE_IPV6
  309. case AF_INET6: send_sock=sendipv6_tcp;
  310. break;
  311. #endif
  312. default: LOG(L_ERR, "get_send_socket: BUG: don't know how"
  313. " to forward to af %d\n", to->s.sa_family);
  314. }
  315. break;
  316. #endif
  317. #ifdef USE_TLS
  318. case PROTO_TLS:
  319. switch(to->s.sa_family){
  320. /* FIXME */
  321. case AF_INET: send_sock=sendipv4_tls;
  322. break;
  323. #ifdef USE_IPV6
  324. case AF_INET6: send_sock=sendipv6_tls;
  325. break;
  326. #endif
  327. default: LOG(L_ERR, "get_send_socket: BUG: don't know how"
  328. " to forward to af %d\n", to->s.sa_family);
  329. }
  330. break;
  331. #endif /* USE_TLS */
  332. #ifdef USE_SCTP
  333. case PROTO_SCTP:
  334. if ((bind_address==0) ||
  335. (to->s.sa_family!=bind_address->address.af) ||
  336. (bind_address->proto!=PROTO_SCTP)){
  337. switch(to->s.sa_family){
  338. case AF_INET: send_sock=sendipv4_sctp;
  339. break;
  340. #ifdef USE_IPV6
  341. case AF_INET6: send_sock=sendipv6_sctp;
  342. break;
  343. #endif
  344. default: LOG(L_ERR, "get_send_socket: BUG: don't know"
  345. " how to forward to af %d\n",
  346. to->s.sa_family);
  347. }
  348. }else send_sock=bind_address;
  349. break;
  350. #endif /* USE_SCTP */
  351. case PROTO_UDP:
  352. if ((bind_address==0) ||
  353. (to->s.sa_family!=bind_address->address.af) ||
  354. (bind_address->proto!=PROTO_UDP)){
  355. switch(to->s.sa_family){
  356. case AF_INET: send_sock=sendipv4;
  357. break;
  358. #ifdef USE_IPV6
  359. case AF_INET6: send_sock=sendipv6;
  360. break;
  361. #endif
  362. default: LOG(L_ERR, "get_send_socket: BUG: don't know"
  363. " how to forward to af %d\n",
  364. to->s.sa_family);
  365. }
  366. }else send_sock=bind_address;
  367. break;
  368. default:
  369. LOG(L_CRIT, "BUG: get_send_socket: unsupported proto %d (%s)\n",
  370. proto, proto2a(proto));
  371. }
  372. return send_sock;
  373. }
  374. static struct _check_self_func {
  375. check_self_f fself;
  376. struct _check_self_func *next;
  377. } *_check_self_func_list = NULL;
  378. /* register a function to be called when matching for myself
  379. * - return 0 on success, -1 on error
  380. * - f must have same prototype as check_self() and return same kind of values
  381. */
  382. int register_check_self_func(check_self_f f)
  383. {
  384. struct _check_self_func *nf = 0;
  385. nf=(struct _check_self_func*)pkg_malloc(sizeof(struct _check_self_func));
  386. if(nf==0)
  387. {
  388. LM_ERR("no more pkg\n");
  389. return -1;
  390. }
  391. nf->fself = f;
  392. nf->next = _check_self_func_list;
  393. _check_self_func_list = nf;
  394. return 0;
  395. }
  396. /* run registered check self functions
  397. * returns 1 if true, 0 if false
  398. */
  399. int run_check_self_func(str* host, unsigned short port, unsigned short proto)
  400. {
  401. struct _check_self_func *sf = 0;
  402. if(_check_self_func_list==NULL)
  403. return 0;
  404. for(sf=_check_self_func_list; sf; sf=sf->next)
  405. if(sf->fself(host, port, proto)==1)
  406. return 1;
  407. return 0;
  408. }
  409. /* checks if the proto: host:port is one of the address we listen on;
  410. * if port==0, the port number is ignored
  411. * if proto==0 (PROTO_NONE) the protocol is ignored
  412. * returns 1 if true, 0 if false, -1 on error
  413. * WARNING: uses str2ip6 so it will overwrite any previous
  414. * unsaved result of this function (static buffer)
  415. */
  416. int check_self(str* host, unsigned short port, unsigned short proto)
  417. {
  418. if (grep_sock_info(host, port, proto)) goto found;
  419. /* try to look into the aliases*/
  420. if (grep_aliases(host->s, host->len, port, proto)==0){
  421. DBG("check_self: host != me\n");
  422. return (_check_self_func_list==NULL)?0:run_check_self_func(host,
  423. port, proto);
  424. }
  425. found:
  426. return 1;
  427. }
  428. /* checks if the proto:port is one of the ports we listen on;
  429. * if proto==0 (PROTO_NONE) the protocol is ignored
  430. * returns 1 if true, 0 if false, -1 on error
  431. */
  432. int check_self_port(unsigned short port, unsigned short proto)
  433. {
  434. if (grep_sock_info_by_port(port, proto))
  435. /* as aliases do not contain different ports we can skip them */
  436. return 1;
  437. else
  438. return 0;
  439. }
  440. /* forwards a request to dst
  441. * parameters:
  442. * msg - sip msg
  443. * dst - destination name, if non-null it will be resolved and
  444. * send_info updated with the ip/port. Even if dst is non
  445. * null send_info must contain the protocol and if a non
  446. * default port or non srv. lookup is desired, the port must
  447. * be !=0
  448. * port - used only if dst!=0 (else the port in send_info->to is used)
  449. * send_info - value/result partially filled dest_info structure:
  450. * - send_info->proto and comp are used
  451. * - send_info->to will be filled (dns)
  452. * - send_info->send_flags is filled from the message
  453. * - if the send_socket member is null, a send_socket will be
  454. * chosen automatically
  455. * WARNING: don't forget to zero-fill all the unused members (a non-zero
  456. * random id along with proto==PROTO_TCP can have bad consequences, same for
  457. * a bogus send_socket value)
  458. */
  459. int forward_request(struct sip_msg* msg, str* dst, unsigned short port,
  460. struct dest_info* send_info)
  461. {
  462. unsigned int len;
  463. char* buf;
  464. char md5[MD5_LEN];
  465. struct socket_info* orig_send_sock; /* initial send_sock */
  466. int ret;
  467. struct ip_addr ip; /* debugging only */
  468. char proto;
  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. LOG(L_ERR, "ERROR: forward_request: resolving \"%.*s\""
  488. " failed: %s [%d]\n", dst->len, ZSW(dst->s),
  489. dns_strerror(err), err);
  490. ret=E_BAD_ADDRESS;
  491. goto error;
  492. }
  493. }else
  494. #endif
  495. if (sip_hostport2su(&send_info->to, dst, port, &proto)<0){
  496. LOG(L_ERR, "ERROR: forward_request: bad host name %.*s,"
  497. " dropping packet\n", dst->len, ZSW(dst->s));
  498. ret=E_BAD_ADDRESS;
  499. goto error;
  500. }
  501. }/* dst */
  502. send_info->send_flags=msg->fwd_send_flags;
  503. /* calculate branch for outbound request; if syn_branch is turned off,
  504. calculate is from transaction key, i.e., as an md5 of From/To/CallID/
  505. CSeq exactly the same way as TM does; good for reboot -- than messages
  506. belonging to transaction lost due to reboot will still be forwarded
  507. with the same branch parameter and will be match-able downstream
  508. if it is turned on, we don't care about reboot; we simply put a simple
  509. value in there; better for performance
  510. */
  511. if (syn_branch ) {
  512. *msg->add_to_branch_s='0';
  513. msg->add_to_branch_len=1;
  514. } else {
  515. if (!char_msg_val( msg, md5 )) { /* parses transaction key */
  516. LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
  517. ret=E_UNSPEC;
  518. goto error;
  519. }
  520. msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
  521. if (!branch_builder( msg->hash_index, 0, md5, 0 /* 0-th branch */,
  522. msg->add_to_branch_s, &msg->add_to_branch_len )) {
  523. LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
  524. ret=E_UNSPEC;
  525. goto error;
  526. }
  527. }
  528. /* try to send the message until success or all the ips are exhausted
  529. * (if dns lookup is performed && the dns cache used ) */
  530. #ifdef USE_DNS_FAILOVER
  531. do{
  532. #endif
  533. if (orig_send_sock==0) /* no forced send_sock => find it **/
  534. send_info->send_sock=get_send_socket(msg, &send_info->to, proto);
  535. if (send_info->send_sock==0){
  536. LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d, proto %d "
  537. "no corresponding listening socket\n",
  538. send_info->to.s.sa_family, proto);
  539. ret=ser_error=E_NO_SOCKET;
  540. #ifdef USE_DNS_FAILOVER
  541. /* continue, maybe we find a socket for some other ip */
  542. continue;
  543. #else
  544. goto error;
  545. #endif
  546. }
  547. #ifdef USE_DNS_FAILOVER
  548. if (prev_send_sock!=send_info->send_sock){
  549. /* rebuild the message only if the send_sock changed */
  550. prev_send_sock=send_info->send_sock;
  551. #endif
  552. if (buf) pkg_free(buf);
  553. send_info->proto=proto;
  554. buf = build_req_buf_from_sip_req(msg, &len, send_info, 0);
  555. if (!buf){
  556. LOG(L_ERR, "ERROR: forward_request: building failed\n");
  557. ret=E_OUT_OF_MEM; /* most probable */
  558. goto error;
  559. }
  560. #ifdef USE_DNS_FAILOVER
  561. }
  562. #endif
  563. /* send it! */
  564. DBG("Sending:\n%.*s.\n", (int)len, buf);
  565. DBG("orig. len=%d, new_len=%d, proto=%d\n",
  566. msg->len, len, send_info->proto );
  567. if (run_onsend(msg, send_info, buf, len)==0){
  568. su2ip_addr(&ip, &send_info->to);
  569. LOG(L_INFO, "forward_request: request to %s:%d(%d) dropped"
  570. " (onsend_route)\n", ip_addr2a(&ip),
  571. su_getport(&send_info->to), send_info->proto);
  572. ser_error=E_OK; /* no error */
  573. ret=E_ADM_PROHIBITED;
  574. #ifdef USE_DNS_FAILOVER
  575. continue; /* try another ip */
  576. #else
  577. goto error; /* error ? */
  578. #endif
  579. }
  580. #ifdef USE_DST_BLACKLIST
  581. if (cfg_get(core, core_cfg, use_dst_blacklist)){
  582. if (dst_is_blacklisted(send_info, msg)){
  583. su2ip_addr(&ip, &send_info->to);
  584. LOG(L_DBG, "DEBUG: blacklisted destination:%s:%d (%d)\n",
  585. ip_addr2a(&ip), su_getport(&send_info->to),
  586. send_info->proto);
  587. ret=ser_error=E_SEND;
  588. #ifdef USE_DNS_FAILOVER
  589. continue; /* try another ip */
  590. #else
  591. goto error;
  592. #endif
  593. }
  594. }
  595. #endif
  596. if (msg_send(send_info, buf, len)<0){
  597. ret=ser_error=E_SEND;
  598. #ifdef USE_DST_BLACKLIST
  599. dst_blacklist_add(BLST_ERR_SEND, send_info, msg);
  600. #endif
  601. #ifdef USE_DNS_FAILOVER
  602. continue; /* try another ip */
  603. #else
  604. goto error;
  605. #endif
  606. }else{
  607. ret=ser_error=E_OK;
  608. /* sent requests stats */
  609. STATS_TX_REQUEST( msg->first_line.u.request.method_value );
  610. /* exit succcesfully */
  611. goto end;
  612. }
  613. #ifdef USE_DNS_FAILOVER
  614. }while(dst && cfg_get(core, core_cfg, use_dns_failover) &&
  615. dns_srv_handle_next(&dns_srv_h, err) &&
  616. ((err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
  617. &proto, dns_flags))==0));
  618. if ((err!=0) && (err!=-E_DNS_EOR)){
  619. LOG(L_ERR, "ERROR: resolving %.*s host name in uri"
  620. " failed: %s [%d] (dropping packet)\n",
  621. dst->len, ZSW(dst->s),
  622. dns_strerror(err), err);
  623. ret=ser_error=E_BAD_ADDRESS;
  624. goto error;
  625. }
  626. #endif
  627. error:
  628. STATS_TX_DROPS;
  629. end:
  630. #ifdef USE_DNS_FAILOVER
  631. if (dst && cfg_get(core, core_cfg, use_dns_failover)){
  632. dns_srv_handle_put(&dns_srv_h);
  633. }
  634. #endif
  635. if (buf) pkg_free(buf);
  636. /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
  637. #if defined STATS_REQ_FWD_OK || defined STATS_REQ_FWD_DROP
  638. if(ret==0)
  639. STATS_REQ_FWD_OK();
  640. else
  641. STATS_REQ_FWD_DROP();
  642. #endif /* STATS_REQ_FWD_* */
  643. return ret;
  644. }
  645. int update_sock_struct_from_via( union sockaddr_union* to,
  646. struct sip_msg* msg,
  647. struct via_body* via )
  648. {
  649. struct hostent* he;
  650. str* name;
  651. int err;
  652. unsigned short port;
  653. char proto;
  654. port=0;
  655. if(via==msg->via1){
  656. /* _local_ reply, we ignore any rport or received value
  657. * (but we will send back to the original port if rport is
  658. * present) */
  659. if ((msg->msg_flags&FL_FORCE_RPORT)||(via->rport))
  660. port=msg->rcv.src_port;
  661. else port=via->port;
  662. name=&(via->host); /* received=ip in 1st via is ignored (it's
  663. not added by us so it's bad) */
  664. }else{
  665. /* "normal" reply, we use rport's & received value if present */
  666. if (via->rport && via->rport->value.s){
  667. DBG("update_sock_struct_from_via: using 'rport'\n");
  668. port=str2s(via->rport->value.s, via->rport->value.len, &err);
  669. if (err){
  670. LOG(L_NOTICE, "ERROR: update_sock_struct_from_via: bad rport value(%.*s)\n",
  671. via->rport->value.len, via->rport->value.s);
  672. port=0;
  673. }
  674. }
  675. if (via->received){
  676. DBG("update_sock_struct_from_via: using 'received'\n");
  677. name=&(via->received->value);
  678. /* making sure that we won't do SRV lookup on "received"
  679. * (possible if no DNS_IP_HACK is used)*/
  680. if (port==0) port=via->port?via->port:SIP_PORT;
  681. }else{
  682. DBG("update_sock_struct_from_via: using via host\n");
  683. name=&(via->host);
  684. if (port==0) port=via->port;
  685. }
  686. }
  687. /* we do now a malloc/memcpy because gethostbyname loves \0-terminated
  688. strings; -jiri
  689. but only if host is not null terminated
  690. (host.s[len] will always be ok for a via)
  691. BTW: when is via->host.s non null terminated? tm copy? - andrei
  692. Yes -- it happened on generating a 408 by TM; -jiri
  693. sip_resolvehost now accepts str -janakj
  694. */
  695. DBG("update_sock_struct_from_via: trying SRV lookup\n");
  696. proto=via->proto;
  697. he=sip_resolvehost(name, &port, &proto);
  698. if (he==0){
  699. LOG(L_NOTICE, "ERROR:forward_reply:resolve_host(%.*s) failure\n",
  700. name->len, name->s);
  701. return -1;
  702. }
  703. hostent2su(to, he, 0, port);
  704. return 1;
  705. }
  706. /* removes first via & sends msg to the second */
  707. int forward_reply(struct sip_msg* msg)
  708. {
  709. char* new_buf;
  710. struct dest_info dst;
  711. unsigned int new_len;
  712. int r;
  713. #ifdef USE_TCP
  714. char* s;
  715. int len;
  716. #endif
  717. init_dest_info(&dst);
  718. new_buf=0;
  719. /*check if first via host = us */
  720. if (check_via){
  721. if (check_self(&msg->via1->host,
  722. msg->via1->port?msg->via1->port:SIP_PORT,
  723. msg->via1->proto)!=1){
  724. LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
  725. " %.*s:%d\n", msg->via1->host.len, msg->via1->host.s,
  726. msg->via1->port);
  727. /* send error msg back? */
  728. goto error;
  729. }
  730. }
  731. /* check modules response_f functions */
  732. for (r=0; r<mod_response_cbk_no; r++)
  733. if (mod_response_cbks[r](msg)==0) goto skip;
  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. LOG(L_DBG, "broken reply to forward - 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. LOG(L_ERR, "ERROR: forward_reply: 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. #ifdef USE_TLS
  758. || dst.proto==PROTO_TLS
  759. #endif
  760. #ifdef USE_SCTP
  761. ||
  762. #endif /* USE_SCTP */
  763. #endif /* USE_TCP */
  764. #ifdef USE_SCTP
  765. dst.proto==PROTO_SCTP
  766. #endif /* USE_SCTP */
  767. ){
  768. /* find id in i param if it exists */
  769. if (msg->via1->i && msg->via1->i->value.s){
  770. s=msg->via1->i->value.s;
  771. len=msg->via1->i->value.len;
  772. DBG("forward_reply: i=%.*s\n",len, ZSW(s));
  773. if (reverse_hex2int(s, len, (unsigned int*)&dst.id)<0){
  774. LOG(L_ERR, "ERROR: forward_reply: bad via i param \"%.*s\"\n",
  775. len, ZSW(s));
  776. dst.id=0;
  777. }
  778. }
  779. }
  780. #endif
  781. if (msg_send(&dst, new_buf, new_len)<0)
  782. {
  783. STATS_RPL_FWD_DROP();
  784. goto error;
  785. }
  786. #ifdef STATS
  787. STATS_TX_RESPONSE( (msg->first_line.u.reply.statuscode/100) );
  788. #endif
  789. DBG(" reply forwarded to %.*s:%d\n",
  790. msg->via2->host.len, msg->via2->host.s,
  791. (unsigned short) msg->via2->port);
  792. STATS_RPL_FWD_OK();
  793. pkg_free(new_buf);
  794. skip:
  795. return 0;
  796. error:
  797. if (new_buf) pkg_free(new_buf);
  798. return -1;
  799. }