udp_server.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 (setsockopt(sock_info->socket, IPPROTO_IP, IP_TOS, (void*)&optval,
  289. sizeof(optval)) ==-1){
  290. LOG(L_WARN, "WARNING: udp_init: setsockopt tos: %s\n", strerror(errno));
  291. /* continue since this is not critical */
  292. }
  293. #if defined (__OS_linux) && defined(UDP_ERRORS)
  294. optval=1;
  295. /* enable error receiving on unconnected sockets */
  296. if(setsockopt(sock_info->socket, SOL_IP, IP_RECVERR,
  297. (void*)&optval, sizeof(optval)) ==-1){
  298. LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno));
  299. goto error;
  300. }
  301. #endif
  302. #if defined (__OS_linux)
  303. /* if pmtu_discovery=1 then set DF bit and do Path MTU discovery
  304. * disabled by default */
  305. optval= (pmtu_discovery) ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
  306. if(setsockopt(sock_info->socket, IPPROTO_IP, IP_MTU_DISCOVER,
  307. (void*)&optval, sizeof(optval)) ==-1){
  308. LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno));
  309. goto error;
  310. }
  311. #endif
  312. #ifdef USE_MCAST
  313. if ((sock_info->flags & SI_IS_MCAST)
  314. && (setup_mcast_rcvr(sock_info->socket, addr)<0)){
  315. goto error;
  316. }
  317. /* set the multicast options */
  318. if (addr->s.sa_family==AF_INET){
  319. m_loop=mcast_loopback;
  320. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_MULTICAST_LOOP,
  321. &m_loop, sizeof(m_loop))==-1){
  322. LOG(L_WARN, "WARNING: udp_init: setsockopt(IP_MULTICAST_LOOP):"
  323. " %s\n", strerror(errno));
  324. /* it's only a warning because we might get this error if the
  325. network interface doesn't support multicasting -- andrei */
  326. }
  327. if (mcast_ttl>=0){
  328. m_ttl=mcast_ttl;
  329. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_MULTICAST_TTL,
  330. &m_ttl, sizeof(m_ttl))==-1){
  331. LOG(L_WARN, "WARNING: udp_init: setsockopt (IP_MULTICAST_TTL):"
  332. " %s\n", strerror(errno));
  333. }
  334. }
  335. #ifdef USE_IPV6
  336. } else if (addr->s.sa_family==AF_INET6){
  337. if (setsockopt(sock_info->socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  338. &mcast_loopback, sizeof(mcast_loopback))==-1){
  339. LOG(L_WARN, "WARNING: udp_init: setsockopt (IPV6_MULTICAST_LOOP):"
  340. " %s\n", strerror(errno));
  341. }
  342. if (mcast_ttl>=0){
  343. if (setsockopt(sock_info->socket, IPPROTO_IP, IPV6_MULTICAST_HOPS,
  344. &mcast_ttl, sizeof(mcast_ttl))==-1){
  345. LOG(L_WARN, "WARNING: udp_init: setssckopt "
  346. "(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
  347. }
  348. }
  349. #endif /* USE_IPV6*/
  350. } else {
  351. LOG(L_ERR, "ERROR: udp_init: Unsupported protocol family %d\n",
  352. addr->s.sa_family);
  353. goto error;
  354. }
  355. #endif /* USE_MCAST */
  356. if ( probe_max_receive_buffer(sock_info->socket)==-1) goto error;
  357. if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
  358. LOG(L_ERR, "ERROR: udp_init: bind(%x, %p, %d) on %s: %s\n",
  359. sock_info->socket, &addr->s,
  360. (unsigned)sockaddru_len(*addr),
  361. sock_info->address_str.s,
  362. strerror(errno));
  363. #ifdef USE_IPV6
  364. if (addr->s.sa_family==AF_INET6)
  365. LOG(L_ERR, "ERROR: udp_init: might be caused by using a link "
  366. " local address, try site local or global\n");
  367. #endif
  368. goto error;
  369. }
  370. /* pkg_free(addr);*/
  371. return 0;
  372. error:
  373. /* if (addr) pkg_free(addr);*/
  374. return -1;
  375. }
  376. int udp_rcv_loop()
  377. {
  378. unsigned len;
  379. #ifdef DYN_BUF
  380. char* buf;
  381. #else
  382. static char buf [BUF_SIZE+1];
  383. #endif
  384. char *tmp;
  385. union sockaddr_union* from;
  386. unsigned int fromlen;
  387. struct receive_info ri;
  388. from=(union sockaddr_union*) pkg_malloc(sizeof(union sockaddr_union));
  389. if (from==0){
  390. LOG(L_ERR, "ERROR: udp_rcv_loop: out of memory\n");
  391. goto error;
  392. }
  393. memset(from, 0 , sizeof(union sockaddr_union));
  394. ri.bind_address=bind_address; /* this will not change, we do it only once*/
  395. ri.dst_port=bind_address->port_no;
  396. ri.dst_ip=bind_address->address;
  397. ri.proto=PROTO_UDP;
  398. ri.proto_reserved1=ri.proto_reserved2=0;
  399. /* initialize the config framework */
  400. if (cfg_child_init()) goto error;
  401. for(;;){
  402. #ifdef DYN_BUF
  403. buf=pkg_malloc(BUF_SIZE+1);
  404. if (buf==0){
  405. LOG(L_ERR, "ERROR: udp_rcv_loop: could not allocate receive"
  406. " buffer\n");
  407. goto error;
  408. }
  409. #endif
  410. fromlen=sockaddru_len(bind_address->su);
  411. len=recvfrom(bind_address->socket, buf, BUF_SIZE, 0, &from->s,
  412. &fromlen);
  413. if (len==-1){
  414. if (errno==EAGAIN){
  415. DBG("udp_rcv_loop: packet with bad checksum received\n");
  416. continue;
  417. }
  418. LOG(L_ERR, "ERROR: udp_rcv_loop:recvfrom:[%d] %s\n",
  419. errno, strerror(errno));
  420. if ((errno==EINTR)||(errno==EWOULDBLOCK)|| (errno==ECONNREFUSED))
  421. continue; /* goto skip;*/
  422. else goto error;
  423. }
  424. /* we must 0-term the messages, receive_msg expects it */
  425. buf[len]=0; /* no need to save the previous char */
  426. ri.src_su=*from;
  427. su2ip_addr(&ri.src_ip, from);
  428. ri.src_port=su_getport(from);
  429. if(unlikely(sr_event_enabled(SREV_NET_DGRAM_IN)))
  430. {
  431. void *sredp[3];
  432. sredp[0] = (void*)buf;
  433. sredp[1] = (void*)(&len);
  434. sredp[2] = (void*)(&ri);
  435. if(sr_event_exec(SREV_NET_DGRAM_IN, (void*)sredp)<0) {
  436. /* data handled by callback - continue to next packet */
  437. continue;
  438. }
  439. }
  440. #ifndef NO_ZERO_CHECKS
  441. #ifdef USE_STUN
  442. /* STUN support can be switched off even if it's compiled */
  443. if (stun_allow_stun == 0 || (unsigned char)*buf != 0x00) {
  444. #endif
  445. if (len<MIN_UDP_PACKET) {
  446. tmp=ip_addr2a(&ri.src_ip);
  447. DBG("udp_rcv_loop: probing packet received from %s %d\n",
  448. tmp, htons(ri.src_port));
  449. continue;
  450. }
  451. #ifdef USE_STUN
  452. }
  453. #endif
  454. /* historically, zero-terminated packets indicated a bug in clients
  455. * that calculated wrongly packet length and included string-terminating
  456. * zero; today clients exist with legitimate binary payloads and we
  457. * shall not check for zero-terminated payloads
  458. */
  459. #ifdef TRASH_ZEROTERMINATED_PACKETS
  460. if (buf[len-1]==0) {
  461. tmp=ip_addr2a(&ri.src_ip);
  462. LOG(L_WARN, "WARNING: udp_rcv_loop: "
  463. "upstream bug - 0-terminated packet from %s %d\n",
  464. tmp, htons(ri.src_port));
  465. len--;
  466. }
  467. #endif
  468. #endif
  469. #ifdef DBG_MSG_QA
  470. if (!dbg_msg_qa(buf, len)) {
  471. LOG(L_WARN, "WARNING: an incoming message didn't pass test,"
  472. " drop it: %.*s\n", len, buf );
  473. continue;
  474. }
  475. #endif
  476. if (ri.src_port==0){
  477. tmp=ip_addr2a(&ri.src_ip);
  478. LOG(L_INFO, "udp_rcv_loop: dropping 0 port packet from %s\n", tmp);
  479. continue;
  480. }
  481. /* update the local config */
  482. cfg_update();
  483. #ifdef USE_STUN
  484. /* STUN support can be switched off even if it's compiled */
  485. if (stun_allow_stun && (unsigned char)*buf == 0x00) {
  486. /* stun_process_msg releases buf memory if necessary */
  487. if ((stun_process_msg(buf, len, &ri)) != 0) {
  488. continue; /* some error occurred */
  489. }
  490. } else
  491. #endif
  492. /* receive_msg must free buf too!*/
  493. receive_msg(buf, len, &ri);
  494. /* skip: do other stuff */
  495. }
  496. /*
  497. if (from) pkg_free(from);
  498. return 0;
  499. */
  500. error:
  501. if (from) pkg_free(from);
  502. return -1;
  503. }
  504. /* send buf:len over udp to dst (uses only the to and send_sock dst members)
  505. * returns the numbers of bytes sent on success (>=0) and -1 on error
  506. */
  507. int udp_send(struct dest_info* dst, char *buf, unsigned len)
  508. {
  509. int n;
  510. int tolen;
  511. struct ip_addr ip; /* used only on error, for debugging */
  512. #ifdef USE_RAW_SOCKS
  513. int mtu;
  514. #endif /* USE_RAW_SOCKS */
  515. #ifdef DBG_MSG_QA
  516. /* aborts on error, does nothing otherwise */
  517. if (!dbg_msg_qa( buf, len )) {
  518. LOG(L_ERR, "ERROR: udp_send: dbg_msg_qa failed\n");
  519. abort();
  520. }
  521. #endif
  522. #ifdef USE_RAW_SOCKS
  523. if (likely( ! (raw_udp4_send_sock >= 0 &&
  524. cfg_get(core, core_cfg, udp4_raw) &&
  525. dst->send_sock->address.af == AF_INET) )) {
  526. #endif /* USE_RAW_SOCKS */
  527. /* normal send over udp socket */
  528. tolen=sockaddru_len(dst->to);
  529. again:
  530. n=sendto(dst->send_sock->socket, buf, len, 0, &dst->to.s, tolen);
  531. #ifdef XL_DEBUG
  532. LOG(L_INFO, "INFO: send status: %d\n", n);
  533. #endif
  534. if (unlikely(n==-1)){
  535. su2ip_addr(&ip, &dst->to);
  536. LOG(L_ERR, "ERROR: udp_send: sendto(sock,%p,%u,0,%s:%d,%d):"
  537. " %s(%d)\n", buf,len, ip_addr2a(&ip),
  538. su_getport(&dst->to), tolen, strerror(errno), errno);
  539. if (errno==EINTR) goto again;
  540. if (errno==EINVAL) {
  541. LOG(L_CRIT,"CRITICAL: invalid sendtoparameters\n"
  542. "one possible reason is the server is bound to localhost and\n"
  543. "attempts to send to the net\n");
  544. }
  545. }
  546. #ifdef USE_RAW_SOCKS
  547. } else {
  548. /* send over a raw socket */
  549. mtu = cfg_get(core, core_cfg, udp4_raw_mtu);
  550. raw_again:
  551. n=raw_iphdr_udp4_send(raw_udp4_send_sock, buf, len,
  552. &dst->send_sock->su,
  553. &dst->to,
  554. mtu);
  555. if (unlikely(n==-1)){
  556. su2ip_addr(&ip, &dst->to);
  557. LOG(L_ERR, "ERROR: raw_iphdr_udp4_send(%d,%p,%u,...,%s:%d,%d):"
  558. " %s(%d)\n", raw_udp4_send_sock, buf,len, ip_addr2a(&ip),
  559. su_getport(&dst->to), mtu, strerror(errno), errno);
  560. if (errno==EINTR) goto raw_again;
  561. }
  562. }
  563. #endif /* USE_RAW_SOCKS */
  564. return n;
  565. }