udp_server.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. * 2003-01-28 packet zero-termination moved to receive_msg (jiri)
  30. * 2003-02-10 undoed the above changes (andrei)
  31. * 2003-03-19 replaced all the mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  32. * 2003-04-14 set sockopts to TOS low delay (andrei)
  33. * 2004-05-03 applied multicast support patch from janakj
  34. * added set multicast ttl support (andrei)
  35. * 2004-07-05 udp_rcv_loop: drop packets with 0 src port + error msg.
  36. * cleanups (andrei)
  37. * 2005-03-10 multicast options are now set for all the udp sockets (andrei)
  38. * 2005-06-26 failure to set mcast options is not an error anymore (andrei)
  39. * 2006-04-12 udp_send() switched to struct dest_info (andrei)
  40. */
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <netinet/in.h>
  46. #include <netinet/in_systm.h>
  47. #include <netinet/ip.h>
  48. #include <errno.h>
  49. #include <arpa/inet.h>
  50. #ifdef __linux__
  51. #include <linux/types.h>
  52. #include <linux/errqueue.h>
  53. #endif
  54. #include "udp_server.h"
  55. #include "globals.h"
  56. #include "config.h"
  57. #include "dprint.h"
  58. #include "receive.h"
  59. #include "mem/mem.h"
  60. #include "ip_addr.h"
  61. #ifdef DBG_MSG_QA
  62. /* message quality assurance -- frequently, bugs in ser have
  63. been indicated by zero characters or long whitespaces
  64. in generated messages; this debugging option aborts if
  65. any such message is sighted
  66. */
  67. static int dbg_msg_qa(char *buf, int len)
  68. {
  69. #define _DBG_WS_LEN 3
  70. #define _DBG_WS " "
  71. char *scan;
  72. int my_len;
  73. int space_cnt;
  74. enum { QA_ANY, QA_SPACE, QA_EOL1 } state;
  75. /* is there a zero character in there ? */
  76. if (memchr(buf, 0, len)) {
  77. LOG(L_CRIT, "BUG: message with 0 in it\n");
  78. return 0;
  79. }
  80. my_len=len;
  81. scan=buf;
  82. state=QA_ANY;
  83. space_cnt=0;
  84. while(my_len) {
  85. switch(*scan) {
  86. case ' ': if (state==QA_SPACE) {
  87. space_cnt++;
  88. if (space_cnt==4) {
  89. LOG(L_CRIT, "BUG(probably): DBG_MSG_QA: "
  90. "too many spaces\n");
  91. return 0;
  92. }
  93. } else space_cnt=0;
  94. state=QA_SPACE;
  95. break;
  96. case '\r': /* ignore */
  97. space_cnt=0;
  98. break;
  99. case '\n': /* don't proceed to body on EoH */
  100. if (state==QA_EOL1) goto qa_passed;
  101. space_cnt=0;
  102. state=QA_EOL1;
  103. break;
  104. default: space_cnt=0;
  105. state=QA_ANY;
  106. break;
  107. }
  108. scan++;
  109. my_len--;
  110. }
  111. qa_passed:
  112. return 1;
  113. }
  114. #endif
  115. int probe_max_receive_buffer( int udp_sock )
  116. {
  117. int optval;
  118. int ioptval;
  119. unsigned int ioptvallen;
  120. int foptval;
  121. unsigned int foptvallen;
  122. int voptval;
  123. unsigned int voptvallen;
  124. int phase=0;
  125. /* jku: try to increase buffer size as much as we can */
  126. ioptvallen=sizeof(ioptval);
  127. if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &ioptval,
  128. &ioptvallen) == -1 )
  129. {
  130. LOG(L_ERR, "ERROR: udp_init: getsockopt: %s\n", strerror(errno));
  131. return -1;
  132. }
  133. if ( ioptval==0 )
  134. {
  135. LOG(L_DBG, "DEBUG: udp_init: SO_RCVBUF initially set to 0; resetting to %d\n",
  136. BUFFER_INCREMENT );
  137. ioptval=BUFFER_INCREMENT;
  138. } else LOG(L_INFO, "INFO: udp_init: SO_RCVBUF is initially %d\n", ioptval );
  139. for (optval=ioptval; ; ) {
  140. /* increase size; double in initial phase, add linearly later */
  141. if (phase==0) optval <<= 1; else optval+=BUFFER_INCREMENT;
  142. if (optval > maxbuffer){
  143. if (phase==1) break;
  144. else { phase=1; optval >>=1; continue; }
  145. }
  146. LOG(L_DBG, "DEBUG: udp_init: trying SO_RCVBUF: %d\n", optval );
  147. if (setsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF,
  148. (void*)&optval, sizeof(optval)) ==-1){
  149. /* Solaris returns -1 if asked size too big; Linux ignores */
  150. LOG(L_DBG, "DEBUG: udp_init: SOL_SOCKET failed"
  151. " for %d, phase %d: %s\n", optval, phase, strerror(errno));
  152. /* if setting buffer size failed and still in the aggressive
  153. phase, try less aggressively; otherwise give up
  154. */
  155. if (phase==0) { phase=1; optval >>=1 ; continue; }
  156. else break;
  157. }
  158. /* verify if change has taken effect */
  159. /* Linux note -- otherwise I would never know that; funny thing: Linux
  160. doubles size for which we asked in setsockopt
  161. */
  162. voptvallen=sizeof(voptval);
  163. if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &voptval,
  164. &voptvallen) == -1 )
  165. {
  166. LOG(L_ERR, "ERROR: udp_init: getsockopt: %s\n", strerror(errno));
  167. return -1;
  168. } else {
  169. LOG(L_DBG, "DEBUG: setting SO_RCVBUF; set=%d,verify=%d\n",
  170. optval, voptval);
  171. if (voptval<optval) {
  172. LOG(L_DBG, "DEBUG: setting SO_RCVBUF has no effect\n");
  173. /* if setting buffer size failed and still in the aggressive
  174. phase, try less aggressively; otherwise give up
  175. */
  176. if (phase==0) { phase=1; optval >>=1 ; continue; }
  177. else break;
  178. }
  179. }
  180. } /* for ... */
  181. foptvallen=sizeof(foptval);
  182. if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &foptval,
  183. &foptvallen) == -1 )
  184. {
  185. LOG(L_ERR, "ERROR: udp_init: getsockopt: %s\n", strerror(errno));
  186. return -1;
  187. }
  188. LOG(L_INFO, "INFO: udp_init: SO_RCVBUF is finally %d\n", foptval );
  189. return 0;
  190. /* EoJKU */
  191. }
  192. #ifdef USE_MCAST
  193. /*
  194. * Setup multicast receiver
  195. */
  196. static int setup_mcast_rcvr(int sock, union sockaddr_union* addr)
  197. {
  198. struct ip_mreq mreq;
  199. #ifdef USE_IPV6
  200. struct ipv6_mreq mreq6;
  201. #endif /* USE_IPV6 */
  202. if (addr->s.sa_family==AF_INET){
  203. memcpy(&mreq.imr_multiaddr, &addr->sin.sin_addr,
  204. sizeof(struct in_addr));
  205. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  206. if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq,
  207. sizeof(mreq))==-1){
  208. LOG(L_ERR, "ERROR: setup_mcast_rcvr: setsockopt: %s\n",
  209. strerror(errno));
  210. return -1;
  211. }
  212. #ifdef USE_IPV6
  213. } else if (addr->s.sa_family==AF_INET6){
  214. memcpy(&mreq6.ipv6mr_multiaddr, &addr->sin6.sin6_addr,
  215. sizeof(struct in6_addr));
  216. mreq6.ipv6mr_interface = 0;
  217. #ifdef __OS_linux
  218. if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6,
  219. #else
  220. if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
  221. #endif
  222. sizeof(mreq6))==-1){
  223. LOG(L_ERR, "ERROR: setup_mcast_rcvr: setsockopt:%s\n",
  224. strerror(errno));
  225. return -1;
  226. }
  227. #endif /* USE_IPV6 */
  228. } else {
  229. LOG(L_ERR, "ERROR: setup_mcast_rcvr: Unsupported protocol family\n");
  230. return -1;
  231. }
  232. return 0;
  233. }
  234. #endif /* USE_MCAST */
  235. int udp_init(struct socket_info* sock_info)
  236. {
  237. union sockaddr_union* addr;
  238. int optval;
  239. #ifdef USE_MCAST
  240. unsigned char m_ttl, m_loop;
  241. #endif
  242. addr=&sock_info->su;
  243. /*
  244. addr=(union sockaddr_union*)pkg_malloc(sizeof(union sockaddr_union));
  245. if (addr==0){
  246. LOG(L_ERR, "ERROR: udp_init: out of memory\n");
  247. goto error;
  248. }
  249. */
  250. sock_info->proto=PROTO_UDP;
  251. if (init_su(addr, &sock_info->address, sock_info->port_no)<0){
  252. LOG(L_ERR, "ERROR: udp_init: could not init sockaddr_union\n");
  253. goto error;
  254. }
  255. sock_info->socket = socket(AF2PF(addr->s.sa_family), SOCK_DGRAM, 0);
  256. if (sock_info->socket==-1){
  257. LOG(L_ERR, "ERROR: udp_init: socket: %s\n", strerror(errno));
  258. goto error;
  259. }
  260. /* set sock opts? */
  261. optval=1;
  262. if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR ,
  263. (void*)&optval, sizeof(optval)) ==-1){
  264. LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno));
  265. goto error;
  266. }
  267. /* tos */
  268. optval = tos;
  269. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_TOS, (void*)&optval,
  270. sizeof(optval)) ==-1){
  271. LOG(L_WARN, "WARNING: udp_init: setsockopt tos: %s\n", strerror(errno));
  272. /* continue since this is not critical */
  273. }
  274. #if defined (__linux__) && defined(UDP_ERRORS)
  275. optval=1;
  276. /* enable error receiving on unconnected sockets */
  277. if(setsockopt(sock_info->socket, SOL_IP, IP_RECVERR,
  278. (void*)&optval, sizeof(optval)) ==-1){
  279. LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno));
  280. goto error;
  281. }
  282. #endif
  283. #ifdef USE_MCAST
  284. if ((sock_info->flags & SI_IS_MCAST)
  285. && (setup_mcast_rcvr(sock_info->socket, addr)<0)){
  286. goto error;
  287. }
  288. /* set the multicast options */
  289. if (addr->s.sa_family==AF_INET){
  290. m_loop=mcast_loopback;
  291. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_MULTICAST_LOOP,
  292. &m_loop, sizeof(m_loop))==-1){
  293. LOG(L_WARN, "WARNING: udp_init: setsockopt(IP_MULTICAST_LOOP):"
  294. " %s\n", strerror(errno));
  295. /* it's only a warning because we might get this error if the
  296. network interface doesn't support multicasting -- andrei */
  297. }
  298. if (mcast_ttl>=0){
  299. m_ttl=mcast_ttl;
  300. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_MULTICAST_TTL,
  301. &m_ttl, sizeof(m_ttl))==-1){
  302. LOG(L_WARN, "WARNING: udp_init: setsockopt (IP_MULTICAST_TTL):"
  303. " %s\n", strerror(errno));
  304. }
  305. }
  306. #ifdef USE_IPV6
  307. } else if (addr->s.sa_family==AF_INET6){
  308. if (setsockopt(sock_info->socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  309. &mcast_loopback, sizeof(mcast_loopback))==-1){
  310. LOG(L_WARN, "WARNING: udp_init: setsockopt (IPV6_MULTICAST_LOOP):"
  311. " %s\n", strerror(errno));
  312. }
  313. if (mcast_ttl>=0){
  314. if (setsockopt(sock_info->socket, IPPROTO_IP, IPV6_MULTICAST_HOPS,
  315. &mcast_ttl, sizeof(mcast_ttl))==-1){
  316. LOG(L_WARN, "WARNING: udp_init: setssckopt "
  317. "(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
  318. }
  319. }
  320. #endif /* USE_IPV6*/
  321. } else {
  322. LOG(L_ERR, "ERROR: udp_init: Unsupported protocol family %d\n",
  323. addr->s.sa_family);
  324. goto error;
  325. }
  326. #endif /* USE_MCAST */
  327. if ( probe_max_receive_buffer(sock_info->socket)==-1) goto error;
  328. if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
  329. LOG(L_ERR, "ERROR: udp_init: bind(%x, %p, %d) on %s: %s\n",
  330. sock_info->socket, &addr->s,
  331. (unsigned)sockaddru_len(*addr),
  332. sock_info->address_str.s,
  333. strerror(errno));
  334. #ifdef USE_IPV6
  335. if (addr->s.sa_family==AF_INET6)
  336. LOG(L_ERR, "ERROR: udp_init: might be caused by using a link "
  337. " local address, try site local or global\n");
  338. #endif
  339. goto error;
  340. }
  341. /* pkg_free(addr);*/
  342. return 0;
  343. error:
  344. /* if (addr) pkg_free(addr);*/
  345. return -1;
  346. }
  347. int udp_rcv_loop()
  348. {
  349. unsigned len;
  350. #ifdef DYN_BUF
  351. char* buf;
  352. #else
  353. static char buf [BUF_SIZE+1];
  354. #endif
  355. char *tmp;
  356. union sockaddr_union* from;
  357. unsigned int fromlen;
  358. struct receive_info ri;
  359. from=(union sockaddr_union*) pkg_malloc(sizeof(union sockaddr_union));
  360. if (from==0){
  361. LOG(L_ERR, "ERROR: udp_rcv_loop: out of memory\n");
  362. goto error;
  363. }
  364. memset(from, 0 , sizeof(union sockaddr_union));
  365. ri.bind_address=bind_address; /* this will not change, we do it only once*/
  366. ri.dst_port=bind_address->port_no;
  367. ri.dst_ip=bind_address->address;
  368. ri.proto=PROTO_UDP;
  369. ri.proto_reserved1=ri.proto_reserved2=0;
  370. for(;;){
  371. #ifdef DYN_BUF
  372. buf=pkg_malloc(BUF_SIZE+1);
  373. if (buf==0){
  374. LOG(L_ERR, "ERROR: udp_rcv_loop: could not allocate receive"
  375. " buffer\n");
  376. goto error;
  377. }
  378. #endif
  379. fromlen=sockaddru_len(bind_address->su);
  380. len=recvfrom(bind_address->socket, buf, BUF_SIZE, 0, &from->s,
  381. &fromlen);
  382. if (len==-1){
  383. if (errno==EAGAIN){
  384. DBG("udp_rcv_loop: packet with bad checksum received\n");
  385. continue;
  386. }
  387. LOG(L_ERR, "ERROR: udp_rcv_loop:recvfrom:[%d] %s\n",
  388. errno, strerror(errno));
  389. if ((errno==EINTR)||(errno==EWOULDBLOCK)|| (errno==ECONNREFUSED))
  390. continue; /* goto skip;*/
  391. else goto error;
  392. }
  393. /* we must 0-term the messages, receive_msg expects it */
  394. buf[len]=0; /* no need to save the previous char */
  395. ri.src_su=*from;
  396. su2ip_addr(&ri.src_ip, from);
  397. ri.src_port=su_getport(from);
  398. #ifndef NO_ZERO_CHECKS
  399. if (len<MIN_UDP_PACKET) {
  400. tmp=ip_addr2a(&ri.src_ip);
  401. DBG("udp_rcv_loop: probing packet received from %s %d\n",
  402. tmp, htons(ri.src_port));
  403. continue;
  404. }
  405. if (buf[len-1]==0) {
  406. tmp=ip_addr2a(&ri.src_ip);
  407. LOG(L_WARN, "WARNING: udp_rcv_loop: "
  408. "upstream bug - 0-terminated packet from %s %d\n",
  409. tmp, htons(ri.src_port));
  410. len--;
  411. }
  412. #endif
  413. #ifdef DBG_MSG_QA
  414. if (!dbg_msg_qa(buf, len)) {
  415. LOG(L_WARN, "WARNING: an incoming message didn't pass test,"
  416. " drop it: %.*s\n", len, buf );
  417. continue;
  418. }
  419. #endif
  420. if (ri.src_port==0){
  421. tmp=ip_addr2a(&ri.src_ip);
  422. LOG(L_INFO, "udp_rcv_loop: dropping 0 port packet from %s\n", tmp);
  423. continue;
  424. }
  425. /* receive_msg must free buf too!*/
  426. receive_msg(buf, len, &ri);
  427. /* skip: do other stuff */
  428. }
  429. /*
  430. if (from) pkg_free(from);
  431. return 0;
  432. */
  433. error:
  434. if (from) pkg_free(from);
  435. return -1;
  436. }
  437. /* send buf:len over udp to dst (uses only the to and send_sock dst members)
  438. * returns the numbers of bytes sent on success (>=0) and -1 on error
  439. */
  440. int udp_send(struct dest_info* dst, char *buf, unsigned len)
  441. {
  442. int n;
  443. int tolen;
  444. struct ip_addr ip; /* used only on error, for debugging */
  445. #ifdef DBG_MSG_QA
  446. /* aborts on error, does nothing otherwise */
  447. if (!dbg_msg_qa( buf, len )) {
  448. LOG(L_ERR, "ERROR: udp_send: dbg_msg_qa failed\n");
  449. abort();
  450. }
  451. #endif
  452. tolen=sockaddru_len(dst->to);
  453. again:
  454. n=sendto(dst->send_sock->socket, buf, len, 0, &dst->to.s, tolen);
  455. #ifdef XL_DEBUG
  456. LOG(L_INFO, "INFO: send status: %d\n", n);
  457. #endif
  458. if (n==-1){
  459. su2ip_addr(&ip, &dst->to);
  460. LOG(L_ERR, "ERROR: udp_send: sendto(sock,%p,%d,0,%s:%d,%d): %s(%d)\n",
  461. buf,len, ip_addr2a(&ip), su_getport(&dst->to), tolen,
  462. strerror(errno),errno);
  463. if (errno==EINTR) goto again;
  464. if (errno==EINVAL) {
  465. LOG(L_CRIT,"CRITICAL: invalid sendtoparameters\n"
  466. "one possible reason is the server is bound to localhost and\n"
  467. "attempts to send to the net\n");
  468. }
  469. }
  470. return n;
  471. }