udp_server.c 15 KB

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