forward.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. #include <string.h>
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <sys/types.h>
  62. #include <sys/socket.h>
  63. #include <netdb.h>
  64. #include <netinet/in.h>
  65. #include <arpa/inet.h>
  66. #include "forward.h"
  67. #include "hash_func.h"
  68. #include "config.h"
  69. #include "parser/msg_parser.h"
  70. #include "route.h"
  71. #include "dprint.h"
  72. #include "globals.h"
  73. #include "cfg_core.h"
  74. #include "data_lump.h"
  75. #include "ut.h"
  76. #include "mem/mem.h"
  77. #include "msg_translator.h"
  78. #include "sr_module.h"
  79. #include "ip_addr.h"
  80. #include "resolve.h"
  81. #include "name_alias.h"
  82. #include "socket_info.h"
  83. #include "onsend.h"
  84. #include "resolve.h"
  85. #ifdef USE_DNS_FAILOVER
  86. #include "dns_cache.h"
  87. #endif
  88. #ifdef USE_DST_BLACKLIST
  89. #include "dst_blacklist.h"
  90. #endif
  91. #include "compiler_opt.h"
  92. #ifdef DEBUG_DMALLOC
  93. #include <dmalloc.h>
  94. #endif
  95. /* return a socket_info_pointer to the sending socket; as opposed to
  96. * get_send_socket, which returns process's default socket, get_out_socket
  97. * attempts to determine the outbound interface which will be used;
  98. * it creates a temporary connected socket to determine it; it will
  99. * be very likely noticeably slower, but it can deal better with
  100. * multihomed hosts
  101. */
  102. struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
  103. {
  104. int temp_sock;
  105. socklen_t len;
  106. union sockaddr_union from;
  107. struct socket_info* si;
  108. struct ip_addr ip;
  109. if (proto!=PROTO_UDP) {
  110. LOG(L_CRIT, "BUG: get_out_socket can only be called for UDP\n");
  111. return 0;
  112. }
  113. temp_sock=socket(to->s.sa_family, SOCK_DGRAM, 0 );
  114. if (temp_sock==-1) {
  115. LOG(L_ERR, "ERROR: get_out_socket: socket() failed: %s\n",
  116. strerror(errno));
  117. return 0;
  118. }
  119. if (connect(temp_sock, &to->s, sockaddru_len(*to))==-1) {
  120. LOG(L_ERR, "ERROR: get_out_socket: connect failed: %s\n",
  121. strerror(errno));
  122. goto error;
  123. }
  124. len=sizeof(from);
  125. if (getsockname(temp_sock, &from.s, &len)==-1) {
  126. LOG(L_ERR, "ERROR: get_out_socket: getsockname failed: %s\n",
  127. strerror(errno));
  128. goto error;
  129. }
  130. su2ip_addr(&ip, &from);
  131. si=find_si(&ip, 0, proto);
  132. if (si==0) goto error;
  133. close(temp_sock);
  134. DBG("DEBUG: get_out_socket: socket determined: %p\n", si );
  135. return si;
  136. error:
  137. LOG(L_ERR, "ERROR: get_out_socket: no socket found\n");
  138. close(temp_sock);
  139. return 0;
  140. }
  141. /* returns a socket_info pointer to the sending socket or 0 on error
  142. * params: sip msg (can be null), destination socket_union pointer, protocol
  143. * if msg!=null and msg->force_send_socket, the force_send_socket will be
  144. * used
  145. */
  146. struct socket_info* get_send_socket(struct sip_msg *msg,
  147. union sockaddr_union* to, int proto)
  148. {
  149. struct socket_info* send_sock;
  150. /* check if send interface is not forced */
  151. if (unlikely(msg && msg->force_send_socket)){
  152. if (unlikely(msg->force_send_socket->proto!=proto)){
  153. DBG("get_send_socket: force_send_socket of different proto"
  154. " (%d)!\n", proto);
  155. msg->force_send_socket=find_si(&(msg->force_send_socket->address),
  156. msg->force_send_socket->port_no,
  157. proto);
  158. if (unlikely(msg->force_send_socket == 0)){
  159. LOG(L_WARN, "WARNING: get_send_socket: "
  160. "protocol/port mismatch\n");
  161. goto not_forced;
  162. }
  163. }
  164. if (unlikely(msg->force_send_socket->address.af!=to->s.sa_family)){
  165. DBG("get_send_socket: force_send_socket of different af (dst %d,"
  166. " forced %d)\n",
  167. to->s.sa_family, msg->force_send_socket->address.af);
  168. goto not_forced;
  169. }
  170. if (likely((msg->force_send_socket->socket!=-1) &&
  171. !(msg->force_send_socket->flags & SI_IS_MCAST)))
  172. return msg->force_send_socket;
  173. else{
  174. if (!(msg->force_send_socket->flags & SI_IS_MCAST))
  175. LOG(L_WARN, "WARNING: get_send_socket: not listening"
  176. " on the requested socket, no fork mode?\n");
  177. }
  178. };
  179. not_forced:
  180. if (mhomed && proto==PROTO_UDP){
  181. send_sock=get_out_socket(to, proto);
  182. if ((send_sock==0) || (send_sock->socket!=-1))
  183. return send_sock; /* found or error*/
  184. else if (send_sock->socket==-1){
  185. LOG(L_WARN, "WARNING: get_send_socket: not listening on the"
  186. " requested socket, no fork mode?\n");
  187. /* continue: try to use some socket */
  188. }
  189. }
  190. send_sock=0;
  191. /* check if we need to change the socket (different address families -
  192. * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
  193. switch(proto){
  194. #ifdef USE_TCP
  195. case PROTO_TCP:
  196. /* on tcp just use the "main address", we don't really now the
  197. * sending address (we can find it out, but we'll need also to see
  198. * if we listen on it, and if yes on which port -> too complicated*/
  199. switch(to->s.sa_family){
  200. /* FIXME */
  201. case AF_INET: send_sock=sendipv4_tcp;
  202. break;
  203. #ifdef USE_IPV6
  204. case AF_INET6: send_sock=sendipv6_tcp;
  205. break;
  206. #endif
  207. default: LOG(L_ERR, "get_send_socket: BUG: don't know how"
  208. " to forward to af %d\n", to->s.sa_family);
  209. }
  210. break;
  211. #endif
  212. #ifdef USE_TLS
  213. case PROTO_TLS:
  214. switch(to->s.sa_family){
  215. /* FIXME */
  216. case AF_INET: send_sock=sendipv4_tls;
  217. break;
  218. #ifdef USE_IPV6
  219. case AF_INET6: send_sock=sendipv6_tls;
  220. break;
  221. #endif
  222. default: LOG(L_ERR, "get_send_socket: BUG: don't know how"
  223. " to forward to af %d\n", to->s.sa_family);
  224. }
  225. break;
  226. #endif /* USE_TLS */
  227. #ifdef USE_SCTP
  228. case PROTO_SCTP:
  229. if ((bind_address==0) ||
  230. (to->s.sa_family!=bind_address->address.af) ||
  231. (bind_address->proto!=PROTO_SCTP)){
  232. switch(to->s.sa_family){
  233. case AF_INET: send_sock=sendipv4_sctp;
  234. break;
  235. #ifdef USE_IPV6
  236. case AF_INET6: send_sock=sendipv6_sctp;
  237. break;
  238. #endif
  239. default: LOG(L_ERR, "get_send_socket: BUG: don't know"
  240. " how to forward to af %d\n",
  241. to->s.sa_family);
  242. }
  243. }else send_sock=bind_address;
  244. break;
  245. #endif /* USE_SCTP */
  246. case PROTO_UDP:
  247. if ((bind_address==0) ||
  248. (to->s.sa_family!=bind_address->address.af) ||
  249. (bind_address->proto!=PROTO_UDP)){
  250. switch(to->s.sa_family){
  251. case AF_INET: send_sock=sendipv4;
  252. break;
  253. #ifdef USE_IPV6
  254. case AF_INET6: send_sock=sendipv6;
  255. break;
  256. #endif
  257. default: LOG(L_ERR, "get_send_socket: BUG: don't know"
  258. " how to forward to af %d\n",
  259. to->s.sa_family);
  260. }
  261. }else send_sock=bind_address;
  262. break;
  263. default:
  264. LOG(L_CRIT, "BUG: get_send_socket: unknown proto %d\n", proto);
  265. }
  266. return send_sock;
  267. }
  268. /* checks if the proto: host:port is one of the address we listen on;
  269. * if port==0, the port number is ignored
  270. * if proto==0 (PROTO_NONE) the protocol is ignored
  271. * returns 1 if true, 0 if false, -1 on error
  272. * WARNING: uses str2ip6 so it will overwrite any previous
  273. * unsaved result of this function (static buffer)
  274. */
  275. int check_self(str* host, unsigned short port, unsigned short proto)
  276. {
  277. if (grep_sock_info(host, port, proto)) goto found;
  278. /* try to look into the aliases*/
  279. if (grep_aliases(host->s, host->len, port, proto)==0){
  280. DBG("check_self: host != me\n");
  281. return 0;
  282. }
  283. found:
  284. return 1;
  285. }
  286. /* checks if the proto:port is one of the ports we listen on;
  287. * if proto==0 (PROTO_NONE) the protocol is ignored
  288. * returns 1 if true, 0 if false, -1 on error
  289. */
  290. int check_self_port(unsigned short port, unsigned short proto)
  291. {
  292. if (grep_sock_info_by_port(port, proto))
  293. /* as aliases do not contain different ports we can skip them */
  294. return 1;
  295. else
  296. return 0;
  297. }
  298. /* forwards a request to dst
  299. * parameters:
  300. * msg - sip msg
  301. * dst - destination name, if non-null it will be resolved and
  302. * send_info updated with the ip/port. Even if dst is non
  303. * null send_info must contain the protocol and if a non
  304. * default port or non srv. lookup is desired, the port must
  305. * be !=0
  306. * port - used only if dst!=0 (else the port in send_info->to is used)
  307. * send_info - filled dest_info structure:
  308. * if the send_socket member is null, a send_socket will be
  309. * chosen automatically
  310. * WARNING: don't forget to zero-fill all the unused members (a non-zero
  311. * random id along with proto==PROTO_TCP can have bad consequences, same for
  312. * a bogus send_socket value)
  313. */
  314. int forward_request(struct sip_msg* msg, str* dst, unsigned short port,
  315. struct dest_info* send_info)
  316. {
  317. unsigned int len;
  318. char* buf;
  319. char md5[MD5_LEN];
  320. struct socket_info* orig_send_sock; /* initial send_sock */
  321. int ret;
  322. struct ip_addr ip; /* debugging only */
  323. char proto;
  324. #ifdef USE_DNS_FAILOVER
  325. struct socket_info* prev_send_sock;
  326. int err;
  327. struct dns_srv_handle dns_srv_h;
  328. prev_send_sock=0;
  329. err=0;
  330. #endif
  331. buf=0;
  332. orig_send_sock=send_info->send_sock;
  333. proto=send_info->proto;
  334. ret=0;
  335. if(dst){
  336. #ifdef USE_DNS_FAILOVER
  337. if (cfg_get(core, core_cfg, use_dns_failover)){
  338. dns_srv_handle_init(&dns_srv_h);
  339. err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
  340. &proto, dns_flags);
  341. if (err!=0){
  342. LOG(L_ERR, "ERROR: forward_request: resolving \"%.*s\""
  343. " failed: %s [%d]\n", dst->len, ZSW(dst->s),
  344. dns_strerror(err), err);
  345. ret=E_BAD_ADDRESS;
  346. goto error;
  347. }
  348. }else
  349. #endif
  350. if (sip_hostport2su(&send_info->to, dst, port, &proto)<0){
  351. LOG(L_ERR, "ERROR: forward_request: bad host name %.*s,"
  352. " dropping packet\n", dst->len, ZSW(dst->s));
  353. ret=E_BAD_ADDRESS;
  354. goto error;
  355. }
  356. }/* dst */
  357. /* calculate branch for outbound request; if syn_branch is turned off,
  358. calculate is from transaction key, i.e., as an md5 of From/To/CallID/
  359. CSeq exactly the same way as TM does; good for reboot -- than messages
  360. belonging to transaction lost due to reboot will still be forwarded
  361. with the same branch parameter and will be match-able downstream
  362. if it is turned on, we don't care about reboot; we simply put a simple
  363. value in there; better for performance
  364. */
  365. if (syn_branch ) {
  366. *msg->add_to_branch_s='0';
  367. msg->add_to_branch_len=1;
  368. } else {
  369. if (!char_msg_val( msg, md5 )) { /* parses transaction key */
  370. LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
  371. ret=E_UNSPEC;
  372. goto error;
  373. }
  374. msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
  375. if (!branch_builder( msg->hash_index, 0, md5, 0 /* 0-th branch */,
  376. msg->add_to_branch_s, &msg->add_to_branch_len )) {
  377. LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
  378. ret=E_UNSPEC;
  379. goto error;
  380. }
  381. }
  382. /* try to send the message until success or all the ips are exhausted
  383. * (if dns lookup is peformed && the dns cache used ) */
  384. #ifdef USE_DNS_FAILOVER
  385. do{
  386. #endif
  387. if (orig_send_sock==0) /* no forced send_sock => find it **/
  388. send_info->send_sock=get_send_socket(msg, &send_info->to, proto);
  389. if (send_info->send_sock==0){
  390. LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d, proto %d "
  391. "no corresponding listening socket\n",
  392. send_info->to.s.sa_family, proto);
  393. ret=ser_error=E_NO_SOCKET;
  394. #ifdef USE_DNS_FAILOVER
  395. /* continue, maybe we find a socket for some other ip */
  396. continue;
  397. #else
  398. goto error;
  399. #endif
  400. }
  401. #ifdef USE_DNS_FAILOVER
  402. if (prev_send_sock!=send_info->send_sock){
  403. /* rebuild the message only if the send_sock changed */
  404. prev_send_sock=send_info->send_sock;
  405. #endif
  406. if (buf) pkg_free(buf);
  407. send_info->proto=proto;
  408. buf = build_req_buf_from_sip_req(msg, &len, send_info);
  409. if (!buf){
  410. LOG(L_ERR, "ERROR: forward_request: building failed\n");
  411. ret=E_OUT_OF_MEM; /* most probable */
  412. goto error;
  413. }
  414. #ifdef USE_DNS_FAILOVER
  415. }
  416. #endif
  417. /* send it! */
  418. DBG("Sending:\n%.*s.\n", (int)len, buf);
  419. DBG("orig. len=%d, new_len=%d, proto=%d\n",
  420. msg->len, len, send_info->proto );
  421. if (run_onsend(msg, send_info, buf, len)==0){
  422. su2ip_addr(&ip, &send_info->to);
  423. LOG(L_INFO, "forward_request: request to %s:%d(%d) dropped"
  424. " (onsend_route)\n", ip_addr2a(&ip),
  425. su_getport(&send_info->to), send_info->proto);
  426. ser_error=E_OK; /* no error */
  427. ret=E_ADM_PROHIBITED;
  428. #ifdef USE_DNS_FAILOVER
  429. continue; /* try another ip */
  430. #else
  431. goto error; /* error ? */
  432. #endif
  433. }
  434. #ifdef USE_DST_BLACKLIST
  435. if (cfg_get(core, core_cfg, use_dst_blacklist)){
  436. if (dst_is_blacklisted(send_info, msg)){
  437. su2ip_addr(&ip, &send_info->to);
  438. LOG(L_DBG, "DEBUG: blacklisted destination:%s:%d (%d)\n",
  439. ip_addr2a(&ip), su_getport(&send_info->to),
  440. send_info->proto);
  441. ret=ser_error=E_SEND;
  442. #ifdef USE_DNS_FAILOVER
  443. continue; /* try another ip */
  444. #else
  445. goto error;
  446. #endif
  447. }
  448. }
  449. #endif
  450. if (msg_send(send_info, buf, len)<0){
  451. ret=ser_error=E_SEND;
  452. #ifdef USE_DST_BLACKLIST
  453. if (cfg_get(core, core_cfg, use_dst_blacklist))
  454. dst_blacklist_add(BLST_ERR_SEND, send_info, msg);
  455. #endif
  456. #ifdef USE_DNS_FAILOVER
  457. continue; /* try another ip */
  458. #else
  459. goto error;
  460. #endif
  461. }else{
  462. ret=ser_error=E_OK;
  463. /* sent requests stats */
  464. STATS_TX_REQUEST( msg->first_line.u.request.method_value );
  465. /* exit succcesfully */
  466. goto end;
  467. }
  468. #ifdef USE_DNS_FAILOVER
  469. }while(dst && cfg_get(core, core_cfg, use_dns_failover) &&
  470. dns_srv_handle_next(&dns_srv_h, err) &&
  471. ((err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
  472. &proto, dns_flags))==0));
  473. if ((err!=0) && (err!=-E_DNS_EOR)){
  474. LOG(L_ERR, "ERROR: resolving %.*s host name in uri"
  475. " failed: %s [%d] (dropping packet)\n",
  476. dst->len, ZSW(dst->s),
  477. dns_strerror(err), err);
  478. ret=ser_error=E_BAD_ADDRESS;
  479. goto error;
  480. }
  481. #endif
  482. error:
  483. STATS_TX_DROPS;
  484. end:
  485. #ifdef USE_DNS_FAILOVER
  486. if (dst && cfg_get(core, core_cfg, use_dns_failover)){
  487. dns_srv_handle_put(&dns_srv_h);
  488. }
  489. #endif
  490. if (buf) pkg_free(buf);
  491. /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
  492. return ret;
  493. }
  494. int update_sock_struct_from_via( union sockaddr_union* to,
  495. struct sip_msg* msg,
  496. struct via_body* via )
  497. {
  498. struct hostent* he;
  499. str* name;
  500. int err;
  501. unsigned short port;
  502. char proto;
  503. port=0;
  504. if(via==msg->via1){
  505. /* _local_ reply, we ignore any rport or received value
  506. * (but we will send back to the original port if rport is
  507. * present) */
  508. if ((msg->msg_flags&FL_FORCE_RPORT)||(via->rport))
  509. port=msg->rcv.src_port;
  510. else port=via->port;
  511. name=&(via->host); /* received=ip in 1st via is ignored (it's
  512. not added by us so it's bad) */
  513. }else{
  514. /* "normal" reply, we use rport's & received value if present */
  515. if (via->rport && via->rport->value.s){
  516. DBG("update_sock_struct_from_via: using 'rport'\n");
  517. port=str2s(via->rport->value.s, via->rport->value.len, &err);
  518. if (err){
  519. LOG(L_NOTICE, "ERROR: update_sock_struct_from_via: bad rport value(%.*s)\n",
  520. via->rport->value.len, via->rport->value.s);
  521. port=0;
  522. }
  523. }
  524. if (via->received){
  525. DBG("update_sock_struct_from_via: using 'received'\n");
  526. name=&(via->received->value);
  527. /* making sure that we won't do SRV lookup on "received"
  528. * (possible if no DNS_IP_HACK is used)*/
  529. if (port==0) port=via->port?via->port:SIP_PORT;
  530. }else{
  531. DBG("update_sock_struct_from_via: using via host\n");
  532. name=&(via->host);
  533. if (port==0) port=via->port;
  534. }
  535. }
  536. /* we do now a malloc/memcpy because gethostbyname loves \0-terminated
  537. strings; -jiri
  538. but only if host is not null terminated
  539. (host.s[len] will always be ok for a via)
  540. BTW: when is via->host.s non null terminated? tm copy? - andrei
  541. Yes -- it happened on generating a 408 by TM; -jiri
  542. sip_resolvehost now accepts str -janakj
  543. */
  544. DBG("update_sock_struct_from_via: trying SRV lookup\n");
  545. proto=via->proto;
  546. he=sip_resolvehost(name, &port, &proto);
  547. if (he==0){
  548. LOG(L_NOTICE, "ERROR:forward_reply:resolve_host(%.*s) failure\n",
  549. name->len, name->s);
  550. return -1;
  551. }
  552. hostent2su(to, he, 0, port);
  553. return 1;
  554. }
  555. /* removes first via & sends msg to the second */
  556. int forward_reply(struct sip_msg* msg)
  557. {
  558. char* new_buf;
  559. struct dest_info dst;
  560. unsigned int new_len;
  561. int r;
  562. #ifdef USE_TCP
  563. char* s;
  564. int len;
  565. #endif
  566. init_dest_info(&dst);
  567. new_buf=0;
  568. /*check if first via host = us */
  569. if (check_via){
  570. if (check_self(&msg->via1->host,
  571. msg->via1->port?msg->via1->port:SIP_PORT,
  572. msg->via1->proto)!=1){
  573. LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
  574. " %.*s:%d\n", msg->via1->host.len, msg->via1->host.s,
  575. msg->via1->port);
  576. /* send error msg back? */
  577. goto error;
  578. }
  579. }
  580. /* check modules response_f functions */
  581. for (r=0; r<mod_response_cbk_no; r++)
  582. if (mod_response_cbks[r](msg)==0) goto skip;
  583. /* we have to forward the reply stateless, so we need second via -bogdan*/
  584. if (parse_headers( msg, HDR_VIA2_F, 0 )==-1
  585. || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
  586. {
  587. /* no second via => error */
  588. LOG(L_ERR, "ERROR: forward_reply: no 2nd via found in reply\n");
  589. goto error;
  590. }
  591. new_buf = build_res_buf_from_sip_res( msg, &new_len);
  592. if (!new_buf){
  593. LOG(L_ERR, "ERROR: forward_reply: building failed\n");
  594. goto error;
  595. }
  596. dst.proto=msg->via2->proto;
  597. if (update_sock_struct_from_via( &dst.to, msg, msg->via2 )==-1) goto error;
  598. #ifdef USE_COMP
  599. dst.comp=msg->via2->comp_no;
  600. #endif
  601. #if defined USE_TCP || defined USE_SCTP
  602. if (
  603. #ifdef USE_TCP
  604. dst.proto==PROTO_TCP
  605. #ifdef USE_TLS
  606. || dst.proto==PROTO_TLS
  607. #endif
  608. #ifdef USE_SCTP
  609. ||
  610. #endif /* USE_SCTP */
  611. #endif /* USE_TCP */
  612. #ifdef USE_SCTP
  613. dst.proto==PROTO_SCTP
  614. #endif /* USE_SCTP */
  615. ){
  616. /* find id in i param if it exists */
  617. if (msg->via1->i && msg->via1->i->value.s){
  618. s=msg->via1->i->value.s;
  619. len=msg->via1->i->value.len;
  620. DBG("forward_reply: i=%.*s\n",len, ZSW(s));
  621. if (reverse_hex2int(s, len, (unsigned int*)&dst.id)<0){
  622. LOG(L_ERR, "ERROR: forward_reply: bad via i param \"%.*s\"\n",
  623. len, ZSW(s));
  624. dst.id=0;
  625. }
  626. }
  627. }
  628. #endif
  629. if (msg_send(&dst, new_buf, new_len)<0) goto error;
  630. #ifdef STATS
  631. STATS_TX_RESPONSE( (msg->first_line.u.reply.statuscode/100) );
  632. #endif
  633. DBG(" reply forwarded to %.*s:%d\n",
  634. msg->via2->host.len, msg->via2->host.s,
  635. (unsigned short) msg->via2->port);
  636. pkg_free(new_buf);
  637. skip:
  638. return 0;
  639. error:
  640. if (new_buf) pkg_free(new_buf);
  641. return -1;
  642. }