Versió 1 quasi per entregar. Falta molt RFC per implementar.
authorviric@llimona
Thu, 06 Apr 2006 01:41:59 +0200
changeset 16 de00e98450eb
parent 15 fea6e87812f0
child 17 589406eaaf43
Versió 1 quasi per entregar. Falta molt RFC per implementar.
Makefile
Makefile.common
syslog_in_npipe.c
syslog_in_udp.c
--- a/Makefile	Thu Apr 06 01:13:18 2006 +0200
+++ b/Makefile	Thu Apr 06 01:41:59 2006 +0200
@@ -1,10 +1,5 @@
 # GNU CC
 CC=gcc
-CODE_OPTIONS=-D_POSIX_C_SOURCE=200112L -D_ISOC99_SOURCE
-CFLAGS=-Wall -ansi -pedantic -std=c99 -g $(CODE_OPTIONS)
-
-# Tiny C
-#CC=tcc
-#CFLAGS=
+MY_CFLAGS=-Wall -ansi -pedantic -std=c99 -g $(CODE_OPTIONS)
 
 include Makefile.common
--- a/Makefile.common	Thu Apr 06 01:13:18 2006 +0200
+++ b/Makefile.common	Thu Apr 06 01:41:59 2006 +0200
@@ -1,11 +1,7 @@
-# GNU CC
-#CC=gcc
-#CODE_OPTIONS=-D_POSIX_C_SOURCE=200112L -D_ISOC99_SOURCE
-#CFLAGS=-Wall -ansi -pedantic -std=c99 -g $(CODE_OPTIONS)
+CODE_OPTIONS=-D_POSIX_C_SOURCE=200112L -D_ISOC99_SOURCE
+CFLAGS=$(MY_CFLAGS) $(CODE_OPTIONS)
 
-# Tiny C
-#CC=tcc
-#CFLAGS=
+LDFLAGS=
 
 all: syslog_in_npipe syslog_in_udp syslog_in_unix unix_writer syslog_kernel
 
--- a/syslog_in_npipe.c	Thu Apr 06 01:13:18 2006 +0200
+++ b/syslog_in_npipe.c	Thu Apr 06 01:41:59 2006 +0200
@@ -86,7 +86,7 @@
 				}
 				close (fd_entrada);
 				/* End of code duplication */
-				return 1;
+				return 0;
 			}
 		}
 
@@ -107,18 +107,19 @@
 			/* End of code duplication */
 			return 1;
 		}
+		/* Code duplication !! */
+		blocking.l_type = F_UNLCK;
+		status = fcntl(fd_entrada, F_SETLKW, &blocking);
+		if (fd_entrada < 0) {
+			fprintf(stderr,
+				"Error unlocking the named pipe: %s\n",
+				strerror(errno));
+			abort();
+		}
+		close (fd_entrada);
+		/* End of code duplication */
 	}
 
-	blocking.l_type = F_UNLCK;
-	status = fcntl(fd_entrada, F_SETLKW, &blocking);
-	if (fd_entrada < 0) {
-		fprintf(stderr,
-			"Error unlocking the named pipe: %s\n",
-			strerror(errno));
-		abort();
-	}
-
-	close (fd_entrada);
 }
 
 int main(int argn, char **argv)
--- a/syslog_in_udp.c	Thu Apr 06 01:13:18 2006 +0200
+++ b/syslog_in_udp.c	Thu Apr 06 01:41:59 2006 +0200
@@ -7,6 +7,7 @@
 #include <sys/socket.h>  // Per les macros bool,true,false
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <stdbool.h>
 #include <netdb.h> // Per protoent getprotobyname
 #include <assert.h> // Per assert()
 #include <sys/select.h> // Pel select()
@@ -15,6 +16,8 @@
 
 #define LISTEN_QUEUE = 10;
 
+bool ipv6enabled = true;
+
 void show_help(const char * restrict program)
 {
 	printf("Usage: %s <udp_port>\n", program);
@@ -142,13 +145,14 @@
 
 
 	socket_ipv4 = listen_ipv4(port);
-	socket_ipv6 = listen_ipv6(port);
+	if (ipv6enabled)
+		socket_ipv6 = listen_ipv6(port);
 
 
 	/* Mirem quin és el socket més alt pel select() */
-	high_socket = socket_ipv6;
-	if (high_socket < socket_ipv4)
-		high_socket = socket_ipv4;
+	high_socket = socket_ipv4;
+	if (ipv6enabled && (high_socket < socket_ipv6) )
+		high_socket = socket_ipv6;
 
 	high_socket += 1;
 	
@@ -157,7 +161,8 @@
 	{
 		/* Establim els FDs que volem esperar al select() */
 		FD_ZERO(&listen_sockets);
-		FD_SET(socket_ipv6, &listen_sockets);
+		if (ipv6enabled)
+			FD_SET(socket_ipv6, &listen_sockets);
 		FD_SET(socket_ipv4, &listen_sockets);
 
 		/* Establim el temporitzador pel select() */
@@ -185,7 +190,7 @@
 				break;
 		}
 
-		if (FD_ISSET(socket_ipv6, &listen_sockets))
+		if (ipv6enabled && FD_ISSET(socket_ipv6, &listen_sockets))
 		{
 			recv(socket_ipv6, message, MESSAGE_LENGTH, 0);
 			process_message(message);
@@ -194,6 +199,11 @@
 				break;
 		}
 	}
+
+	/* Closing sockets at the end */
+	close(socket_ipv4);
+	if (ipv6enabled)
+		close(socket_ipv6);
 }