udp_server.c 17 KB

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