udp_server.c 17 KB

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