udp_server.c
e60a9728
 /*
  * $Id$
  */
 
3e429f5c
 #include <stdlib.h>
 #include <string.h>
e60a9728
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <errno.h>
68a3fc65
 #include <arpa/inet.h>
853c176f
 #ifdef __linux__
 	#include <linux/types.h>
 	#include <linux/errqueue.h>
 #endif
e60a9728
 
 
 #include "udp_server.h"
4e2fdd79
 #include "globals.h"
e60a9728
 #include "config.h"
 #include "dprint.h"
3e429f5c
 #include "receive.h"
dda9dab1
 #include "mem/mem.h"
4e2fdd79
 #include "ip_addr.h"
e60a9728
 
03150098
 #ifdef DEBUG_DMALLOC
dda9dab1
 #include <mem/dmalloc.h>
03150098
 #endif
e60a9728
 
c0656aad
 #ifdef DBG_MSG_QA
 /* message quality assurance -- frequently, bugs in ser have
    been indicated by zero characters or long whitespaces
    in generated messages; this debugging option aborts if
    any such message is sighted
 */
 static int dbg_msg_qa(char *buf, int len)
 {
 #define _DBG_WS_LEN 3
 #define _DBG_WS "   "
 
 	char *scan;
 	int my_len;
 	int space_cnt;
 	enum { QA_ANY, QA_SPACE, QA_EOL1 } state;
 
 
 	/* is there a zero character inthere ? */	
 	if (memchr(buf, 0, len)) {
 		LOG(L_CRIT, "BUG: message being sent with 0 in it\n");
 		return 0;
 	}
 
 	my_len=len;
 	scan=buf;
 	state=QA_ANY;
 	space_cnt=0;
 
 	while(my_len) {
 		switch(*scan) {
 			case ' ':	if (state==QA_SPACE) {
 							space_cnt++;
 							if (space_cnt==4) {
 								LOG(L_CRIT, "BUG(propably): DBG_MSG_QA: "
 									"too many spaces\n");
 								return 0;
 							}
 						} else space_cnt=0;
 						state=QA_SPACE; 
 						break;
 
 			case '\r':	/* ignore */
 						space_cnt=0;
 						break;
 
 			case '\n': /* don't proceed to body on EoH */
 						if (state==QA_EOL1) goto qa_passed;
 						space_cnt=0;
 						state=QA_EOL1;
 						break;
 
 			default:	space_cnt=0;
 						state=QA_ANY;
 						break;
 		}
 		scan++;
 		my_len--;
 	}
 
 
 qa_passed:
 	return 1;
 }
 
 #endif
 
e60a9728
 
8eddd6f6
 int probe_max_receive_buffer( int udp_sock )
e60a9728
 {
e22bbdb8
 	int optval;
9c61e9f9
 	int ioptval;
 	unsigned int ioptvallen;
 	int foptval;
 	unsigned int foptvallen;
 	int voptval;
 	unsigned int voptvallen;
d24a2059
 	int phase=0;
 
 	/* jku: try to increase buffer size as much as we can */
 	ioptvallen=sizeof(ioptval);
 	if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &ioptval,
 		    &ioptvallen) == -1 )
 	{
 		LOG(L_ERR, "ERROR: udp_init: getsockopt: %s\n", strerror(errno));
8eddd6f6
 		return -1;
d24a2059
 	}
 	if ( ioptval==0 ) 
 	{
 		LOG(L_DBG, "DEBUG: udp_init: SO_RCVBUF initialy set to 0; resetting to %d\n",
 			BUFFER_INCREMENT );
 		ioptval=BUFFER_INCREMENT;
 	} else LOG(L_INFO, "INFO: udp_init: SO_RCVBUF is initially %d\n", ioptval );
53af8794
 	for (optval=ioptval; ;  ) {
d24a2059
 		/* increase size; double in initial phase, add linearly later */
 		if (phase==0) optval <<= 1; else optval+=BUFFER_INCREMENT;
e22bbdb8
 		if (optval > maxbuffer){
 			if (phase==1) break; 
e0a6ffa6
 			else { phase=1; optval >>=1; continue; }
 		}
d24a2059
 		LOG(L_DBG, "DEBUG: udp_init: trying SO_RCVBUF: %d\n", optval );
e22bbdb8
 		if (setsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF,
 			(void*)&optval, sizeof(optval)) ==-1){
af6fb476
 			/* Solaris returns -1 if asked size too big; Linux ignores */
e22bbdb8
 			LOG(L_DBG, "DEBUG: udp_init: SOL_SOCKET failed"
 					" for %d, phase %d: %s\n", optval, phase, strerror(errno));
d24a2059
 			/* if setting buffer size failed and still in the aggressive
 			   phase, try less agressively; otherwise give up 
 			*/
 			if (phase==0) { phase=1; optval >>=1 ; continue; } 
 			else break;
e22bbdb8
 		} 
d24a2059
 		/* verify if change has taken effect */
af6fb476
 		/* Linux note -- otherwise I would never know that; funny thing: Linux
 		   doubles size for which we asked in setsockopt
 		*/
d24a2059
 		voptvallen=sizeof(voptval);
 		if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &voptval,
 		    &voptvallen) == -1 )
 		{
 			LOG(L_ERR, "ERROR: udp_init: getsockopt: %s\n", strerror(errno));
8eddd6f6
 			return -1;
d24a2059
 		} else {
 			LOG(L_DBG, "DEBUG: setting SO_RCVBUF; set=%d,verify=%d\n", 
 				optval, voptval);
 			if (voptval<optval) {
 				LOG(L_DBG, "DEBUG: setting SO_RCVBUF has no effect\n");
 				/* if setting buffer size failed and still in the aggressive
e22bbdb8
 				phase, try less agressively; otherwise give up 
d24a2059
 				*/
e22bbdb8
 				if (phase==0) { phase=1; optval >>=1 ; continue; } 
 				else break;
8eddd6f6
 			} 
d24a2059
 		}
e22bbdb8
 	
d24a2059
 	} /* for ... */
 	foptvallen=sizeof(foptval);
 	if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &foptval,
 		    &foptvallen) == -1 )
 	{
 		LOG(L_ERR, "ERROR: udp_init: getsockopt: %s\n", strerror(errno));
8eddd6f6
 		return -1;
d24a2059
 	}
e22bbdb8
 	LOG(L_INFO, "INFO: udp_init: SO_RCVBUF is finally %d\n", foptval );
d24a2059
 
8eddd6f6
 	return 0;
d24a2059
 
 	/* EoJKU */
8eddd6f6
 }
 
36ef0329
 int udp_init(struct socket_info* sock_info)
8eddd6f6
 {
4e2fdd79
 	union sockaddr_union* addr;
e22bbdb8
 	int optval;
8eddd6f6
 
741db913
 	addr=&sock_info->su;
 /*
4e2fdd79
 	addr=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
8eddd6f6
 	if (addr==0){
 		LOG(L_ERR, "ERROR: udp_init: out of memory\n");
 		goto error;
 	}
741db913
 */
36ef0329
 	if (init_su(addr, &sock_info->address, htons(sock_info->port_no))<0){
4e2fdd79
 		LOG(L_ERR, "ERROR: udp_init: could not init sockaddr_union\n");
 		goto error;
 	}
1d597ac3
 	
36ef0329
 	sock_info->socket = socket(AF2PF(addr->s.sa_family), SOCK_DGRAM, 0);
 	if (sock_info->socket==-1){
8eddd6f6
 		LOG(L_ERR, "ERROR: udp_init: socket: %s\n", strerror(errno));
 		goto error;
 	}
 	/* set sock opts? */
 	optval=1;
36ef0329
 	if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR ,
853c176f
 					(void*)&optval, sizeof(optval)) ==-1){
 		LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno));
 		goto error;
 	}
577f13b5
 #if defined (__linux__) && defined(UDP_ERRORS)
853c176f
 	optval=1;
 	/* enable error receiving on unconnected sockets */
 	if(setsockopt(sock_info->socket, SOL_IP, IP_RECVERR,
 					(void*)&optval, sizeof(optval)) ==-1){
8eddd6f6
 		LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno));
 		goto error;
 	}
853c176f
 #endif
 
8eddd6f6
 
36ef0329
 	if ( probe_max_receive_buffer(sock_info->socket)==-1) goto error;
53531528
 	
741db913
 	if (bind(sock_info->socket,  &addr->s, sockaddru_len(*addr))==-1){
36ef0329
 		LOG(L_ERR, "ERROR: udp_init: bind(%x, %p, %d) on %s: %s\n",
 				sock_info->socket, &addr->s, 
741db913
 				sockaddru_len(*addr),
36ef0329
 				sock_info->address_str.s,
 				strerror(errno));
 	#ifdef USE_IPV6
 		if (addr->s.sa_family==AF_INET6)
 			LOG(L_ERR, "ERROR: udp_init: might be caused by using a link "
 					" local address, try site local or global\n");
 	#endif
e60a9728
 		goto error;
 	}
 
741db913
 /*	free(addr);*/
e60a9728
 	return 0;
 
 error:
741db913
 /*	if (addr) free(addr);*/
e60a9728
 	return -1;
 }
 
 
 
 int udp_rcv_loop()
 {
3e429f5c
 	unsigned len;
dda9dab1
 #ifdef DYN_BUF
22d4aa5d
 	char* buf;
dda9dab1
 #else
b2dec9c6
 	static char buf [BUF_SIZE+1];
dda9dab1
 #endif
 
4e2fdd79
 	union sockaddr_union* from;
9c61e9f9
 	unsigned int fromlen;
e60a9728
 
dda9dab1
 
4e2fdd79
 	from=(union sockaddr_union*) malloc(sizeof(union sockaddr_union));
e60a9728
 	if (from==0){
efeaaf53
 		LOG(L_ERR, "ERROR: udp_rcv_loop: out of memory\n");
e60a9728
 		goto error;
 	}
741db913
 	memset(from, 0 , sizeof(union sockaddr_union));
e60a9728
 
 	for(;;){
dda9dab1
 #ifdef DYN_BUF
6bd84753
 		buf=pkg_malloc(BUF_SIZE+1);
 		if (buf==0){
 			LOG(L_ERR, "ERROR: udp_rcv_loop: could not allocate receive"
 					 " buffer\n");
 			goto error;
 		}
dda9dab1
 #endif
741db913
 		fromlen=sockaddru_len(bind_address->su);
36ef0329
 		len=recvfrom(bind_address->socket, buf, BUF_SIZE, 0, &from->s,
 											&fromlen);
e60a9728
 		if (len==-1){
6984881e
 			LOG(L_ERR, "ERROR: udp_rcv_loop:recvfrom:[%d] %s\n",
 						errno, strerror(errno));
e90526cd
 			if ((errno==EINTR)||(errno==EAGAIN)||(errno==EWOULDBLOCK)||
 					(errno==ECONNREFUSED))
6984881e
 				continue; /* goto skip;*/
e60a9728
 			else goto error;
 		}
 		/*debugging, make print* msg work */
 		buf[len+1]=0;
577f13b5
 
 #ifndef NO_ZERO_CHECKS
 		if (len==0) {
 			LOG(L_WARN, "WARNING: empty packet received\n");
 			continue;
 		}
 		if (buf[len-1]==0) {
 			LOG(L_WARN, "WARNING: upstream bug - 0-terminated packet\n");
 			len--;
 		}
 #endif
f354fb0e
 #ifdef DBG_MSG_QA
 		if (!dbg_msg_qa(buf, len)) {
 			LOG(L_WARN, "WARNING: an incoming message didn't pass test, drop it: %.*s\n",
 				len, buf );
 			continue;
 		}
 #endif
22d4aa5d
 		
 		/* receive_msg must free buf too!*/
4e2fdd79
 		receive_msg(buf, len, from);
1b1b19d8
 		
b2dec9c6
 	/* skip: do other stuff */
e60a9728
 		
 	}
51eadd0c
 	/*
1b1b19d8
 	if (from) free(from);
e60a9728
 	return 0;
51eadd0c
 	*/
e60a9728
 	
 error:
1b1b19d8
 	if (from) free(from);
e60a9728
 	return -1;
 }
 
 
 
92044adc
 
577f13b5
 /* which socket to use? main socket or new one? */
1e6e3461
 int udp_send(struct socket_info *source, char *buf, unsigned len, union sockaddr_union*  to)
577f13b5
 {
 
 	int n;
741db913
 	int tolen;
577f13b5
 
 #ifdef DBG_MSG_QA
 	/* aborts on error, does nothing otherwise */
f354fb0e
 	if (!dbg_msg_qa( buf, len )) {
 		LOG(L_ERR, "ERROR: udp_send: dbg_msg_qa failed\n");
 		abort();
 	}
577f13b5
 #endif
 
741db913
 	tolen=sockaddru_len(*to);
e60a9728
 again:
36ef0329
 	n=sendto(source->socket, buf, len, 0, &to->s, tolen);
18abd66c
 #ifdef XL_DEBUG
 	LOG(L_INFO, "INFO: send status: %d\n", n);
 #endif
e60a9728
 	if (n==-1){
e22bbdb8
 		LOG(L_ERR, "ERROR: udp_send: sendto(sock,%p,%d,0,%p,%d): %s(%d)\n",
9dfa5dc4
 				buf,len,to,tolen,
 				strerror(errno),errno);
e60a9728
 		if (errno==EINTR) goto again;
d3b31abd
 		if (errno==EINVAL) {
 			LOG(L_CRIT,"CRITICAL: invalid sendtoparameters\n"
68a3fc65
 			"one possible reason is the server is bound to localhost and\n"
 			"attempts to send to the net\n");
d3b31abd
 		}
e60a9728
 	}
 	return n;
 }