forward.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. #include <string.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <netdb.h>
  33. #include <netinet/in.h>
  34. #include <arpa/inet.h>
  35. #include "forward.h"
  36. #include "hash_func.h"
  37. #include "config.h"
  38. #include "parser/msg_parser.h"
  39. #include "route.h"
  40. #include "dprint.h"
  41. #include "udp_server.h"
  42. #include "globals.h"
  43. #include "data_lump.h"
  44. #include "ut.h"
  45. #include "mem/mem.h"
  46. #include "msg_translator.h"
  47. #include "sr_module.h"
  48. #include "stats.h"
  49. #include "ip_addr.h"
  50. #include "resolve.h"
  51. #include "name_alias.h"
  52. #ifdef USE_TCP
  53. #include "tcp_server.h"
  54. #endif
  55. #ifdef DEBUG_DMALLOC
  56. #include <dmalloc.h>
  57. #endif
  58. /* returns a socket_info pointer to the sending socket or 0 on error
  59. * params: destination socket_union pointer
  60. */
  61. struct socket_info* get_send_socket(union sockaddr_union* to, int proto)
  62. {
  63. struct socket_info* send_sock;
  64. send_sock=0;
  65. /* check if we need to change the socket (different address families -
  66. * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
  67. #ifdef USE_TCP
  68. if (proto==PROTO_TCP){
  69. /* on tcp just use the "main address", we don't really now the
  70. * sending address (we can find it out, but we'll find also to see
  71. * if we listen on it, and if yes on which port -> too complicated*/
  72. switch(to->s.sa_family){
  73. case AF_INET: send_sock=sendipv4_tcp;
  74. break;
  75. #ifdef USE_IPV6
  76. case AF_INET6: send_sock=sendipv6_tcp;
  77. break;
  78. #endif
  79. default: LOG(L_ERR, "get_send_socket: BUG: don't know how"
  80. " to forward to af %d\n", to->s.sa_family);
  81. }
  82. }else
  83. #endif
  84. if ((bind_address==0) ||(to->s.sa_family!=bind_address->address.af)){
  85. switch(to->s.sa_family){
  86. case AF_INET: send_sock=sendipv4;
  87. break;
  88. #ifdef USE_IPV6
  89. case AF_INET6: send_sock=sendipv6;
  90. break;
  91. #endif
  92. default: LOG(L_ERR, "get_send_socket: BUG: don't know how"
  93. " to forward to af %d\n", to->s.sa_family);
  94. }
  95. }else send_sock=bind_address;
  96. return send_sock;
  97. }
  98. /* checks if the host:port is one of the address we listen on;
  99. * if port==0, the port number is ignored
  100. * returns 1 if true, 0 if false, -1 on error
  101. */
  102. int check_self(str* host, unsigned short port)
  103. {
  104. int r;
  105. for (r=0; r<sock_no; r++){
  106. DBG("check_self - checking if host==us: %d==%d && "
  107. " [%.*s] == [%.*s]\n",
  108. host->len,
  109. sock_info[r].name.len,
  110. host->len, host->s,
  111. sock_info[r].name.len, sock_info[r].name.s
  112. );
  113. if ((port)&&(sock_info[r].port_no!=port)) continue;
  114. if ( (host->len==sock_info[r].name.len) &&
  115. #ifdef USE_IPV6
  116. (strncasecmp(host->s, sock_info[r].name.s,
  117. sock_info[r].name.len)==0) /*slower*/
  118. #else
  119. (memcmp(host->s, sock_info[r].name.s,
  120. sock_info[r].name.len)==0)
  121. #endif
  122. )
  123. break;
  124. /* check if host == ip address */
  125. if ( (!sock_info[r].is_ip) &&
  126. (host->len==sock_info[r].address_str.len) &&
  127. #ifdef USE_IPV6
  128. (strncasecmp(host->s, sock_info[r].address_str.s,
  129. sock_info[r].address_str.len)==0) /*slower*/
  130. #else
  131. (memcmp(host->s, sock_info[r].address_str.s,
  132. sock_info[r].address_str.len)==0)
  133. #endif
  134. )
  135. break;
  136. }
  137. if (r==sock_no){
  138. /* try to look into the aliases*/
  139. if (grep_aliases(host->s, host->len, port)==0){
  140. DBG("check_self: host != me\n");
  141. return 0;
  142. }
  143. }
  144. return 1;
  145. }
  146. int forward_request( struct sip_msg* msg, struct proxy_l * p, int proto)
  147. {
  148. unsigned int len;
  149. char* buf;
  150. union sockaddr_union* to;
  151. struct socket_info* send_sock;
  152. char md5[MD5_LEN];
  153. int id; /* used as branch for tcp! */
  154. to=0;
  155. buf=0;
  156. id=0;
  157. to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
  158. if (to==0){
  159. ser_error=E_OUT_OF_MEM;
  160. LOG(L_ERR, "ERROR: forward_request: out of memory\n");
  161. goto error;
  162. }
  163. /* if error try next ip address if possible */
  164. if (p->ok==0){
  165. if (p->host.h_addr_list[p->addr_idx+1])
  166. p->addr_idx++;
  167. else p->addr_idx=0;
  168. p->ok=1;
  169. }
  170. hostent2su(to, &p->host, p->addr_idx,
  171. (p->port)?htons(p->port):htons(SIP_PORT));
  172. p->tx++;
  173. p->tx_bytes+=len;
  174. send_sock=get_send_socket(to, proto);
  175. if (send_sock==0){
  176. LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d "
  177. "no coresponding listening socket\n", to->s.sa_family);
  178. ser_error=E_NO_SOCKET;
  179. goto error1;
  180. }
  181. /* calculate branch for outbound request; if syn_branch is turned off,
  182. calculate is from transaction key, i.e., as an md5 of From/To/CallID/
  183. CSeq exactly the same way as TM does; good for reboot -- than messages
  184. belonging to transaction lost due to reboot will still be forwarded
  185. with the same branch parameter and will be match-able downstream
  186. if it is turned on, we don't care about reboot; we simply put a simple
  187. value in there; better for performance
  188. */
  189. #ifdef USE_TCP
  190. if (msg->rcv.proto==PROTO_TCP) id=msg->rcv.proto_reserved1;
  191. #endif
  192. if (syn_branch ) {
  193. *msg->add_to_branch_s='0';
  194. msg->add_to_branch_len=1;
  195. } else {
  196. if (!char_msg_val( msg, md5 )) { /* parses transaction key */
  197. LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
  198. goto error1;
  199. }
  200. msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
  201. if (!branch_builder( msg->hash_index, 0, md5, id /* 0-th branch */,
  202. msg->add_to_branch_s, &msg->add_to_branch_len )) {
  203. LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
  204. goto error1;
  205. }
  206. }
  207. buf = build_req_buf_from_sip_req( msg, &len, send_sock, proto);
  208. if (!buf){
  209. LOG(L_ERR, "ERROR: forward_request: building failed\n");
  210. goto error1;
  211. }
  212. /* send it! */
  213. DBG("Sending:\n%s.\n", buf);
  214. DBG("orig. len=%d, new_len=%d, proto=%d\n", msg->len, len, proto );
  215. if (proto==PROTO_UDP){
  216. if (udp_send(send_sock, buf, len, to)==-1){
  217. ser_error=E_SEND;
  218. p->errors++;
  219. p->ok=0;
  220. STATS_TX_DROPS;
  221. goto error1;
  222. }
  223. }
  224. #ifdef USE_TCP
  225. else if (proto==PROTO_TCP){
  226. if (tcp_send(buf, len, to, 0)==-1){
  227. ser_error=E_SEND;
  228. p->errors++;
  229. p->ok=0;
  230. STATS_TX_DROPS;
  231. goto error1;
  232. }
  233. }
  234. #endif
  235. else{
  236. LOG(L_CRIT, "BUG: forward_request: unknown proto %d\n", proto);
  237. ser_error=E_SEND;
  238. STATS_TX_DROPS;
  239. goto error1;
  240. }
  241. /* sent requests stats */
  242. STATS_TX_REQUEST( msg->first_line.u.request.method_value );
  243. pkg_free(buf);
  244. free(to);
  245. /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
  246. return 0;
  247. error1:
  248. free(to);
  249. error:
  250. if (buf) pkg_free(buf);
  251. return -1;
  252. }
  253. int update_sock_struct_from_via( union sockaddr_union* to,
  254. struct via_body* via )
  255. {
  256. struct hostent* he;
  257. str* name;
  258. unsigned short port;
  259. if (via->received){
  260. DBG("update_sock_struct_from_via: using 'received'\n");
  261. name=&(via->received->value);
  262. /* making sure that we won't do SRV lookup on "received"
  263. * (possible if no DNS_IP_HACK is used)*/
  264. port=via->port?via->port:SIP_PORT;
  265. }else{
  266. DBG("update_sock_struct_from_via: using via host\n");
  267. name=&(via->host);
  268. port=via->port;
  269. }
  270. /* we do now a malloc/memcpy because gethostbyname loves \0-terminated
  271. strings; -jiri
  272. but only if host is not null terminated
  273. (host.s[len] will always be ok for a via)
  274. BTW: when is via->host.s non null terminated? tm copy? - andrei
  275. Yes -- it happened on generating a 408 by TM; -jiri
  276. sip_resolvehost now accepts str -janakj
  277. */
  278. DBG("update_sock_struct_from_via: trying SRV lookup\n");
  279. he=sip_resolvehost(name, &port);
  280. if (he==0){
  281. LOG(L_NOTICE, "ERROR:forward_reply:resolve_host(%.*s) failure\n",
  282. name->len, name->s);
  283. return -1;
  284. }
  285. hostent2su(to, he, 0, htons(port));
  286. return 1;
  287. }
  288. /* removes first via & sends msg to the second */
  289. int forward_reply(struct sip_msg* msg)
  290. {
  291. char* new_buf;
  292. union sockaddr_union* to;
  293. struct socket_info* send_sock;
  294. unsigned int new_len;
  295. struct sr_module *mod;
  296. int proto;
  297. #ifdef USE_TCP
  298. char* s;
  299. char* p;
  300. int len;
  301. int id;
  302. #endif
  303. to=0;
  304. new_buf=0;
  305. /*check if first via host = us */
  306. if (check_via){
  307. if (check_self(&msg->via1->host,
  308. msg->via1->port?msg->via1->port:SIP_PORT)!=1){
  309. LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
  310. " %.*s:%d\n", msg->via1->host.len, msg->via1->host.s,
  311. msg->via1->port);
  312. /* send error msg back? */
  313. goto error;
  314. }
  315. }
  316. /* quick hack, slower for mutliple modules*/
  317. for (mod=modules;mod;mod=mod->next){
  318. if ((mod->exports) && (mod->exports->response_f)){
  319. DBG("forward_reply: found module %s, passing reply to it\n",
  320. mod->exports->name);
  321. if (mod->exports->response_f(msg)==0) goto skip;
  322. }
  323. }
  324. /* we have to forward the reply stateless, so we need second via -bogdan*/
  325. if (parse_headers( msg, HDR_VIA2, 0 )==-1
  326. || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
  327. {
  328. /* no second via => error */
  329. LOG(L_ERR, "ERROR: forward_msg: no 2nd via found in reply\n");
  330. goto error;
  331. }
  332. to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
  333. if (to==0){
  334. LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
  335. goto error;
  336. }
  337. new_buf = build_res_buf_from_sip_res( msg, &new_len);
  338. if (!new_buf){
  339. LOG(L_ERR, "ERROR: forward_reply: building failed\n");
  340. goto error;
  341. }
  342. if (update_sock_struct_from_via( to, msg->via2 )==-1) goto error;
  343. send_sock=get_send_socket(to, msg->rcv.proto);
  344. if (send_sock==0){
  345. LOG(L_ERR, "forward_reply: ERROR: no sending socket found\n");
  346. goto error;
  347. }
  348. proto=msg->via2->proto;
  349. if (proto==PROTO_UDP){
  350. if (udp_send(send_sock, new_buf,new_len, to)==-1)
  351. {
  352. STATS_TX_DROPS;
  353. goto error;
  354. }
  355. }
  356. #ifdef USE_TCP
  357. else if (proto==PROTO_TCP){
  358. id=0;
  359. /* find id in branch if it exists */
  360. if ((msg->via1->branch)&&(msg->via1->branch->value.len>MCOOKIE_LEN) &&
  361. (memcmp(msg->via1->branch->value.s, MCOOKIE, MCOOKIE_LEN)==0)){
  362. DBG("forward_reply: found branch\n");
  363. s=msg->via1->branch->value.s+MCOOKIE_LEN;
  364. len=msg->via1->branch->value.len-MCOOKIE_LEN;
  365. for (p=s; p<s+len && *p!=BRANCH_SEPARATOR; p++);
  366. p++;
  367. for(;p<s+len && *p!=BRANCH_SEPARATOR; p++);
  368. p++;
  369. if (p<s+len){
  370. /* we found the second BRANCH_SEPARATOR, p points after it */
  371. len-=(int)(p-s);
  372. id=reverse_hex2int(p, len);
  373. DBG("forward_reply: id= %x\n", id);
  374. }else{
  375. DBG("forward_reply: no id in branch\n");
  376. }
  377. }
  378. if (tcp_send(new_buf, new_len, to, id)==-1)
  379. {
  380. STATS_TX_DROPS;
  381. goto error;
  382. }
  383. }
  384. #endif
  385. else{
  386. LOG(L_CRIT, "BUG: forward_reply: unknown proto %d\n", proto);
  387. goto error;
  388. }
  389. #ifdef STATS
  390. STATS_TX_RESPONSE( (msg->first_line.u.reply.statuscode/100) );
  391. #endif
  392. DBG(" reply forwarded to %s:%d\n", msg->via2->host.s,
  393. (unsigned short) msg->via2->port);
  394. pkg_free(new_buf);
  395. free(to);
  396. skip:
  397. return 0;
  398. error:
  399. if (new_buf) pkg_free(new_buf);
  400. if (to) free(to);
  401. return -1;
  402. }