Browse Source

- set IP_RECVERR on linux

Andrei Pelinescu-Onciul 23 năm trước cách đây
mục cha
commit
853c176faa
2 tập tin đã thay đổi với 29 bổ sung7 xóa
  1. 14 5
      test/udp_flood_disc.c
  2. 15 2
      udp_server.c

+ 14 - 5
test/udp_flood_disc.c

@@ -13,6 +13,10 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#ifdef __linux__
+#include <linux/types.h>
+#include <linux/errqueue.h>
+#endif
 
 
 static char *id="$Id$";
@@ -48,6 +52,9 @@ int main (int argc, char** argv)
 	char *fname;
 	char *dst;
 	int port;
+#ifdef __linux__
+	int optval;
+#endif
 	
 	/* init */
 	count=0;
@@ -164,12 +171,14 @@ int main (int argc, char** argv)
 		fprintf(stderr, "ERROR: socket: %s\n", strerror(errno));
 		goto error;
 	}
-/*
-	if (connect(sock, (struct sockaddr*) &addr, sizeof(struct sockaddr))!=0){
-		fprintf(stderr, "ERROR: connect: %s\n", strerror(errno));
-		goto error;
+#ifdef __linux__
+	/* enable error receiving on unconnected sockets*/
+	optval=1;
+	if(setsockopt(sock,SOL_IP,IP_RECVERR,(void*)&optval,sizeof(optval))==-1){
+		fprintf(stderr, "Error: setsockopt: %s\n", strerror(errno));
+		exit(1);
 	}
-*/
+#endif
 
 
 	/* flood loop */

+ 15 - 2
udp_server.c

@@ -9,6 +9,10 @@
 #include <netinet/in.h>
 #include <errno.h>
 #include <arpa/inet.h>
+#ifdef __linux__
+	#include <linux/types.h>
+	#include <linux/errqueue.h>
+#endif
 
 
 #include "udp_server.h"
@@ -137,11 +141,20 @@ int udp_init(struct socket_info* sock_info)
 	/* set sock opts? */
 	optval=1;
 	if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR ,
-					(void*)&optval, sizeof(optval)) ==-1)
-	{
+					(void*)&optval, sizeof(optval)) ==-1){
+		LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno));
+		goto error;
+	}
+#ifdef __linux__
+	optval=1;
+	/* enable error receiving on unconnected sockets */
+	if(setsockopt(sock_info->socket, SOL_IP, IP_RECVERR,
+					(void*)&optval, sizeof(optval)) ==-1){
 		LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno));
 		goto error;
 	}
+#endif
+
 
 	if ( probe_max_receive_buffer(sock_info->socket)==-1) goto error;