2
0

forward.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. static int sock_inet6 = -1;
  114. static void apply_force_send_socket(struct dest_info* dst, struct sip_msg* msg);
  115. struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
  116. {
  117. int* temp_sock;
  118. socklen_t len;
  119. union sockaddr_union from;
  120. struct socket_info* si;
  121. struct ip_addr ip;
  122. union sockaddr_union uncon;
  123. memset(&uncon, 0, sizeof(union sockaddr_union));
  124. uncon.sin.sin_family = AF_UNSPEC;
  125. if (unlikely(proto!=PROTO_UDP)) {
  126. LOG(L_CRIT, "BUG: get_out_socket can only be called for UDP\n");
  127. return 0;
  128. }
  129. retry:
  130. switch(to->s.sa_family){
  131. case AF_INET : {
  132. if(unlikely(sock_inet < 0)){
  133. sock_inet = socket(AF_INET, SOCK_DGRAM, 0);
  134. if (sock_inet==-1) {
  135. LM_ERR("socket() failed: %s\n", strerror(errno));
  136. return 0;
  137. }
  138. }
  139. temp_sock = &sock_inet;
  140. break;
  141. }
  142. case AF_INET6 : {
  143. if(unlikely(sock_inet6 < 0)){
  144. sock_inet6 = socket(AF_INET6, SOCK_DGRAM, 0);
  145. if (sock_inet6==-1) {
  146. LM_ERR("socket() failed: %s\n", strerror(errno));
  147. return 0;
  148. }
  149. }
  150. temp_sock = &sock_inet6;
  151. break;
  152. }
  153. default: {
  154. LM_ERR("Unknown protocol family \n");
  155. return 0;
  156. }
  157. }
  158. if( !mhomed_sock_cache_disabled ){
  159. /* some Linux kernel versions (all?) along with other UNIXes don't re-bound the sock if already bound */
  160. /* to un-bound a socket set sin_family to AF_UNSPEC and zero out the rest*/
  161. if (unlikely(connect(*temp_sock, &uncon.s, sockaddru_len(uncon)) < 0))
  162. mhomed_sock_cache_disabled = 1;
  163. }
  164. if (unlikely(connect(*temp_sock, &to->s, sockaddru_len(*to))==-1)) {
  165. if (unlikely(errno==EISCONN && !mhomed_sock_cache_disabled)){
  166. /* no multiple connects support on the same socket */
  167. mhomed_sock_cache_disabled=1;
  168. if (sock_inet>=0){
  169. close(sock_inet);
  170. sock_inet=-1;
  171. }
  172. if (sock_inet6>=0){
  173. close(sock_inet6);
  174. sock_inet6=-1;
  175. }
  176. goto retry;
  177. }
  178. LOG(L_ERR, "ERROR: get_out_socket: connect failed: %s\n",
  179. strerror(errno));
  180. goto error;
  181. }
  182. len=sizeof(from);
  183. if (unlikely(getsockname(*temp_sock, &from.s, &len)==-1)) {
  184. LOG(L_ERR, "ERROR: get_out_socket: getsockname failed: %s\n",
  185. strerror(errno));
  186. goto error;
  187. }
  188. su2ip_addr(&ip, &from);
  189. si=find_si(&ip, 0, proto);
  190. if (si==0) goto error;
  191. DBG("DEBUG: get_out_socket: socket determined: %p\n", si );
  192. if (unlikely(mhomed_sock_cache_disabled)){
  193. close(*temp_sock);
  194. *temp_sock=-1;
  195. }
  196. return si;
  197. error:
  198. LOG(L_ERR, "ERROR: get_out_socket: no socket found\n");
  199. if (unlikely(mhomed_sock_cache_disabled && *temp_sock >=0)){
  200. close(*temp_sock);
  201. *temp_sock=-1;
  202. }
  203. return 0;
  204. }
  205. /** get the sending socket for a corresponding destination.
  206. * @param force_send_socket - if !=0 and the protocol and af correspond
  207. * with the destination, it will be returned.
  208. * If the protocol or af check fail, a look-alike
  209. * socket will be searched for and mismatch will be
  210. * set. If no look-alike socket is found it will
  211. * fallback to normal resolution.
  212. * @param to - destination
  213. * @param proto - protocol
  214. * @param mismatch - result parameter, set if a force_send_socket was used, but
  215. * there was an error matching it exactly to the destination.
  216. * Possible values: 0 ok, SS_MISMATCH_PROTO,
  217. * SS_MISMATCH_ADDR, SS_MISMATCH_AF, SS_MISMATCH_MCAST.
  218. * @return a socket_info pointer to the sending socket on success (and possibly
  219. * sets mismatch) or 0 on error.
  220. */
  221. struct socket_info* get_send_socket2(struct socket_info* force_send_socket,
  222. union sockaddr_union* to, int proto,
  223. enum ss_mismatch* mismatch)
  224. {
  225. struct socket_info* send_sock;
  226. struct socket_info* orig;
  227. if (likely(mismatch)) *mismatch=0;
  228. /* check if send interface is not forced */
  229. if (unlikely(force_send_socket)){
  230. orig=force_send_socket;
  231. /* Special case here as there is no ;transport=wss - so wss connections will
  232. appear as ws ones and be sorted out in the WebSocket module */
  233. if (unlikely(orig->proto!=proto && !(orig->proto==PROTO_TLS && proto==PROTO_WS))){
  234. force_send_socket=find_si(&(force_send_socket->address),
  235. force_send_socket->port_no,
  236. proto);
  237. if (unlikely(force_send_socket == 0)){
  238. if (likely(mismatch)) *mismatch=SS_MISMATCH_ADDR;
  239. LOG(L_WARN, "WARNING: get_send_socket: "
  240. "protocol/port mismatch (forced %s:%s:%d,"
  241. " to %s:%s)\n",
  242. proto2a(orig->proto), ip_addr2a(&orig->address),
  243. orig->port_no,
  244. proto2a(proto), su2a(to, sizeof(*to)));
  245. goto not_forced;
  246. }
  247. if (likely(mismatch)) *mismatch=SS_MISMATCH_PROTO;
  248. }
  249. if (unlikely(force_send_socket->address.af!=to->s.sa_family)){
  250. DBG("get_send_socket: force_send_socket of different af"
  251. " (dst %d - %s:%s forced %d -%s:%s:%d)\n",
  252. to->s.sa_family, proto2a(proto), su2a(to, sizeof(*to)),
  253. force_send_socket->address.af,
  254. proto2a(force_send_socket->proto),
  255. ip_addr2a(&force_send_socket->address),
  256. force_send_socket->port_no);
  257. if (likely(mismatch)) *mismatch=SS_MISMATCH_AF;
  258. goto not_forced;
  259. }
  260. /* check if listening on the socket (the check does not work
  261. for TCP and TLS, for them socket==-1 on all the processes
  262. except tcp_main(), see close_extra_socks() */
  263. if (likely((force_send_socket->socket!=-1 ||
  264. force_send_socket->proto==PROTO_TCP ||
  265. force_send_socket->proto==PROTO_TLS ||
  266. force_send_socket->proto==PROTO_WS ||
  267. force_send_socket->proto==PROTO_WSS) &&
  268. !(force_send_socket->flags & SI_IS_MCAST)))
  269. return force_send_socket;
  270. else{
  271. if (!(force_send_socket->flags & SI_IS_MCAST))
  272. LOG(L_WARN, "WARNING: get_send_socket: not listening"
  273. " on the requested socket (%s:%s:%d),"
  274. " no fork mode?\n",
  275. proto2a(force_send_socket->proto),
  276. ip_addr2a(&force_send_socket->address),
  277. force_send_socket->port_no);
  278. else if (likely(mismatch)) *mismatch=SS_MISMATCH_MCAST;
  279. }
  280. };
  281. not_forced:
  282. if (mhomed && proto==PROTO_UDP){
  283. send_sock=get_out_socket(to, proto);
  284. if ((send_sock==0) || (send_sock->socket!=-1))
  285. return send_sock; /* found or error*/
  286. else if (send_sock->socket==-1){
  287. LOG(L_WARN, "WARNING: get_send_socket: not listening on the"
  288. " requested socket (%s:%s:%d), no fork mode?\n",
  289. proto2a(send_sock->proto), ip_addr2a(&send_sock->address),
  290. send_sock->port_no);
  291. /* continue: try to use some socket */
  292. }
  293. }
  294. send_sock=0;
  295. /* check if we need to change the socket (different address families -
  296. * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
  297. switch(proto){
  298. #ifdef USE_TCP
  299. case PROTO_WS:
  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. case AF_INET6: send_sock=sendipv6_tcp;
  309. break;
  310. default: LOG(L_ERR, "get_send_socket: BUG: don't know how"
  311. " to forward to af %d\n", to->s.sa_family);
  312. }
  313. break;
  314. #endif
  315. #ifdef USE_TLS
  316. case PROTO_WSS:
  317. case PROTO_TLS:
  318. switch(to->s.sa_family){
  319. /* FIXME */
  320. case AF_INET: send_sock=sendipv4_tls;
  321. break;
  322. case AF_INET6: send_sock=sendipv6_tls;
  323. break;
  324. default: LOG(L_ERR, "get_send_socket: BUG: don't know how"
  325. " to forward to af %d\n", to->s.sa_family);
  326. }
  327. break;
  328. #endif /* USE_TLS */
  329. #ifdef USE_SCTP
  330. case PROTO_SCTP:
  331. if ((bind_address==0) ||
  332. (to->s.sa_family!=bind_address->address.af) ||
  333. (bind_address->proto!=PROTO_SCTP)){
  334. switch(to->s.sa_family){
  335. case AF_INET: send_sock=sendipv4_sctp;
  336. break;
  337. case AF_INET6: send_sock=sendipv6_sctp;
  338. break;
  339. default: LOG(L_ERR, "get_send_socket: BUG: don't know"
  340. " how to forward to af %d\n",
  341. to->s.sa_family);
  342. }
  343. }else send_sock=bind_address;
  344. break;
  345. #endif /* USE_SCTP */
  346. case PROTO_UDP:
  347. if ((bind_address==0) ||
  348. (to->s.sa_family!=bind_address->address.af) ||
  349. (bind_address->proto!=PROTO_UDP)){
  350. switch(to->s.sa_family){
  351. case AF_INET: send_sock=sendipv4;
  352. break;
  353. case AF_INET6: send_sock=sendipv6;
  354. break;
  355. default: LOG(L_ERR, "get_send_socket: BUG: don't know"
  356. " how to forward to af %d\n",
  357. to->s.sa_family);
  358. }
  359. }else send_sock=bind_address;
  360. break;
  361. default:
  362. LOG(L_CRIT, "BUG: get_send_socket: unsupported proto %d (%s)\n",
  363. proto, proto2a(proto));
  364. }
  365. return send_sock;
  366. }
  367. static struct _check_self_func {
  368. check_self_f fself;
  369. struct _check_self_func *next;
  370. } *_check_self_func_list = NULL;
  371. /* check if _check_self_func_list is set
  372. * - return 1 if yes, 0 if no
  373. */
  374. int is_check_self_func_list_set(void)
  375. {
  376. return (_check_self_func_list)?1:0;
  377. }
  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;
  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. */
  509. if (!char_msg_val( msg, md5 )) { /* parses transaction key */
  510. LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
  511. ret=E_UNSPEC;
  512. goto error;
  513. }
  514. msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
  515. if (!branch_builder( msg->hash_index, 0, md5, 0 /* 0-th branch */,
  516. msg->add_to_branch_s, &msg->add_to_branch_len )) {
  517. LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
  518. ret=E_UNSPEC;
  519. goto error;
  520. }
  521. /* try to send the message until success or all the ips are exhausted
  522. * (if dns lookup is performed && the dns cache used ) */
  523. #ifdef USE_DNS_FAILOVER
  524. do{
  525. #endif
  526. if (orig_send_sock==0) /* no forced send_sock => find it **/
  527. send_info->send_sock=get_send_socket(msg, &send_info->to, proto);
  528. if (send_info->send_sock==0){
  529. LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d, proto %d "
  530. "no corresponding listening socket\n",
  531. send_info->to.s.sa_family, proto);
  532. ret=ser_error=E_NO_SOCKET;
  533. #ifdef USE_DNS_FAILOVER
  534. /* continue, maybe we find a socket for some other ip */
  535. continue;
  536. #else
  537. goto error;
  538. #endif
  539. }
  540. #ifdef USE_DNS_FAILOVER
  541. if (prev_send_sock!=send_info->send_sock){
  542. /* rebuild the message only if the send_sock changed */
  543. prev_send_sock=send_info->send_sock;
  544. #endif
  545. if (buf) pkg_free(buf);
  546. send_info->proto=proto;
  547. buf = build_req_buf_from_sip_req(msg, &len, send_info, 0);
  548. if (!buf){
  549. LOG(L_ERR, "ERROR: forward_request: building failed\n");
  550. ret=E_OUT_OF_MEM; /* most probable */
  551. goto error;
  552. }
  553. #ifdef USE_DNS_FAILOVER
  554. }
  555. #endif
  556. /* send it! */
  557. DBG("Sending:\n%.*s.\n", (int)len, buf);
  558. DBG("orig. len=%d, new_len=%d, proto=%d\n",
  559. msg->len, len, send_info->proto );
  560. if (run_onsend(msg, send_info, buf, len)==0){
  561. su2ip_addr(&ip, &send_info->to);
  562. LOG(L_INFO, "forward_request: request to %s:%d(%d) dropped"
  563. " (onsend_route)\n", ip_addr2a(&ip),
  564. su_getport(&send_info->to), send_info->proto);
  565. ser_error=E_OK; /* no error */
  566. ret=E_ADM_PROHIBITED;
  567. #ifdef USE_DNS_FAILOVER
  568. continue; /* try another ip */
  569. #else
  570. goto error; /* error ? */
  571. #endif
  572. }
  573. #ifdef USE_DST_BLACKLIST
  574. if (cfg_get(core, core_cfg, use_dst_blacklist)){
  575. if (dst_is_blacklisted(send_info, msg)){
  576. su2ip_addr(&ip, &send_info->to);
  577. LOG(L_DBG, "DEBUG: blacklisted destination:%s:%d (%d)\n",
  578. ip_addr2a(&ip), su_getport(&send_info->to),
  579. send_info->proto);
  580. ret=ser_error=E_SEND;
  581. #ifdef USE_DNS_FAILOVER
  582. continue; /* try another ip */
  583. #else
  584. goto error;
  585. #endif
  586. }
  587. }
  588. #endif
  589. if (msg_send(send_info, buf, len)<0){
  590. ret=ser_error=E_SEND;
  591. #ifdef USE_DST_BLACKLIST
  592. (void)dst_blacklist_add(BLST_ERR_SEND, send_info, msg);
  593. #endif
  594. #ifdef USE_DNS_FAILOVER
  595. continue; /* try another ip */
  596. #else
  597. goto error;
  598. #endif
  599. }else{
  600. ret=ser_error=E_OK;
  601. /* sent requests stats */
  602. STATS_TX_REQUEST( msg->first_line.u.request.method_value );
  603. /* exit succcesfully */
  604. goto end;
  605. }
  606. #ifdef USE_DNS_FAILOVER
  607. }while(dst && cfg_get(core, core_cfg, use_dns_failover) &&
  608. dns_srv_handle_next(&dns_srv_h, err) &&
  609. ((err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
  610. &proto, dns_flags))==0));
  611. if ((err!=0) && (err!=-E_DNS_EOR)){
  612. LOG(L_ERR, "ERROR: resolving %.*s host name in uri"
  613. " failed: %s [%d] (dropping packet)\n",
  614. dst->len, ZSW(dst->s),
  615. dns_strerror(err), err);
  616. ret=ser_error=E_BAD_ADDRESS;
  617. goto error;
  618. }
  619. #endif
  620. error:
  621. STATS_TX_DROPS;
  622. end:
  623. #ifdef USE_DNS_FAILOVER
  624. if (dst && cfg_get(core, core_cfg, use_dns_failover)){
  625. dns_srv_handle_put(&dns_srv_h);
  626. }
  627. #endif
  628. if (buf) pkg_free(buf);
  629. /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
  630. #if defined STATS_REQ_FWD_OK || defined STATS_REQ_FWD_DROP
  631. if(ret==0)
  632. STATS_REQ_FWD_OK();
  633. else
  634. STATS_REQ_FWD_DROP();
  635. #endif /* STATS_REQ_FWD_* */
  636. return ret;
  637. }
  638. int update_sock_struct_from_via( union sockaddr_union* to,
  639. struct sip_msg* msg,
  640. struct via_body* via )
  641. {
  642. struct hostent* he;
  643. str* name;
  644. int err;
  645. unsigned short port;
  646. char proto;
  647. port=0;
  648. if(via==msg->via1){
  649. /* _local_ reply, we ignore any rport or received value
  650. * (but we will send back to the original port if rport is
  651. * present) */
  652. if ((msg->msg_flags&FL_FORCE_RPORT)||(via->rport))
  653. port=msg->rcv.src_port;
  654. else port=via->port;
  655. name=&(via->host); /* received=ip in 1st via is ignored (it's
  656. not added by us so it's bad) */
  657. }else{
  658. /* "normal" reply, we use rport's & received value if present */
  659. if (via->rport && via->rport->value.s){
  660. DBG("update_sock_struct_from_via: using 'rport'\n");
  661. port=str2s(via->rport->value.s, via->rport->value.len, &err);
  662. if (err){
  663. LOG(L_NOTICE, "ERROR: update_sock_struct_from_via: bad rport value(%.*s)\n",
  664. via->rport->value.len, via->rport->value.s);
  665. port=0;
  666. }
  667. }
  668. if (via->received){
  669. DBG("update_sock_struct_from_via: using 'received'\n");
  670. name=&(via->received->value);
  671. /* making sure that we won't do SRV lookup on "received"
  672. * (possible if no DNS_IP_HACK is used)*/
  673. if (port==0) port=via->port?via->port:SIP_PORT;
  674. }else{
  675. DBG("update_sock_struct_from_via: using via host\n");
  676. name=&(via->host);
  677. if (port==0) port=via->port;
  678. }
  679. }
  680. /* we do now a malloc/memcpy because gethostbyname loves \0-terminated
  681. strings; -jiri
  682. but only if host is not null terminated
  683. (host.s[len] will always be ok for a via)
  684. BTW: when is via->host.s non null terminated? tm copy? - andrei
  685. Yes -- it happened on generating a 408 by TM; -jiri
  686. sip_resolvehost now accepts str -janakj
  687. */
  688. DBG("update_sock_struct_from_via: trying SRV lookup\n");
  689. proto=via->proto;
  690. he=sip_resolvehost(name, &port, &proto);
  691. if (he==0){
  692. LOG(L_NOTICE,
  693. "update_sock_struct_from_via:resolve_host(%.*s) failure\n",
  694. name->len, name->s);
  695. return -1;
  696. }
  697. hostent2su(to, he, 0, port);
  698. return 1;
  699. }
  700. /* removes first via & sends msg to the second
  701. * - mode param controls if modules sip response callbacks are executed */
  702. static int do_forward_reply(struct sip_msg* msg, int mode)
  703. {
  704. char* new_buf;
  705. struct dest_info dst;
  706. unsigned int new_len;
  707. int r;
  708. #ifdef USE_TCP
  709. char* s;
  710. int len;
  711. #endif
  712. init_dest_info(&dst);
  713. new_buf=0;
  714. /*check if first via host = us */
  715. if (check_via){
  716. if (check_self(&msg->via1->host,
  717. msg->via1->port?msg->via1->port:SIP_PORT,
  718. msg->via1->proto)!=1){
  719. LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
  720. " %.*s:%d\n", msg->via1->host.len, msg->via1->host.s,
  721. msg->via1->port);
  722. /* send error msg back? */
  723. goto error;
  724. }
  725. }
  726. /* check modules response_f functions */
  727. if(likely(mode==0)) {
  728. for (r=0; r<mod_response_cbk_no; r++)
  729. if (mod_response_cbks[r](msg)==0) goto skip;
  730. }
  731. /* we have to forward the reply stateless, so we need second via -bogdan*/
  732. if (parse_headers( msg, HDR_VIA2_F, 0 )==-1
  733. || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
  734. {
  735. /* no second via => error */
  736. LOG(L_DBG, "reply cannot be forwarded - no 2nd via\n");
  737. goto error;
  738. }
  739. new_buf = build_res_buf_from_sip_res( msg, &new_len);
  740. if (!new_buf){
  741. LOG(L_ERR, "ERROR: forward_reply: building failed\n");
  742. goto error;
  743. }
  744. dst.proto=msg->via2->proto;
  745. SND_FLAGS_OR(&dst.send_flags, &msg->fwd_send_flags, &msg->rpl_send_flags);
  746. if (update_sock_struct_from_via( &dst.to, msg, msg->via2 )==-1) goto error;
  747. #ifdef USE_COMP
  748. dst.comp=msg->via2->comp_no;
  749. #endif
  750. #if defined USE_TCP || defined USE_SCTP
  751. if (
  752. #ifdef USE_TCP
  753. dst.proto==PROTO_TCP
  754. || dst.proto==PROTO_WS
  755. #ifdef USE_TLS
  756. || dst.proto==PROTO_TLS
  757. || dst.proto==PROTO_WSS
  758. #endif
  759. #ifdef USE_SCTP
  760. ||
  761. #endif /* USE_SCTP */
  762. #endif /* USE_TCP */
  763. #ifdef USE_SCTP
  764. dst.proto==PROTO_SCTP
  765. #endif /* USE_SCTP */
  766. ){
  767. /* find id in i param if it exists */
  768. if (msg->via1->i && msg->via1->i->value.s){
  769. s=msg->via1->i->value.s;
  770. len=msg->via1->i->value.len;
  771. DBG("forward_reply: i=%.*s\n",len, ZSW(s));
  772. if (reverse_hex2int(s, len, (unsigned int*)&dst.id)<0){
  773. LOG(L_ERR, "ERROR: forward_reply: bad via i param \"%.*s\"\n",
  774. len, ZSW(s));
  775. dst.id=0;
  776. }
  777. }
  778. }
  779. #endif
  780. apply_force_send_socket(&dst, msg);
  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. }
  800. /* removes first via & sends msg to the second */
  801. int forward_reply(struct sip_msg* msg)
  802. {
  803. return do_forward_reply(msg, 0);
  804. }
  805. /* removes first via & sends msg to the second - no module callbacks */
  806. int forward_reply_nocb(struct sip_msg* msg)
  807. {
  808. return do_forward_reply(msg, 1);
  809. }
  810. static void apply_force_send_socket(struct dest_info* dst, struct sip_msg* msg)
  811. {
  812. if (msg->force_send_socket != 0) {
  813. dst->send_sock = get_send_socket(msg, &dst->to, dst->proto);
  814. }
  815. }