Added ipv6-option as run parameter. Removed a dependency of shared glibc,
the getprotobyname() call for 'udp' - 0 will do just fine at a socket call.
--- a/syslog_in_udp.c Sun May 14 16:23:56 2006 +0200
+++ b/syslog_in_udp.c Sun May 14 16:39:14 2006 +0200
@@ -8,7 +8,6 @@
#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()
@@ -17,27 +16,9 @@
#define LISTEN_QUEUE = 10;
-const bool ipv6enabled = true;
-
void show_help(const char * restrict program)
{
- printf("Usage: %s <udp_port>\n", program);
-}
-
-
-int udp_proto_number()
-{
- struct protoent *protocol;
-
- /* Get the protocol udp number */
- protocol = getprotobyname("udp");
- if (protocol == NULL)
- {
- fprintf(stderr, "Cannot get the protocol number for udp.\n");
- abort();
- }
-
- return protocol->p_proto;
+ printf("Usage: %s <udp_port> (negative for enable ipv6)\n", program);
}
int listen_ipv6(int port)
@@ -49,7 +30,7 @@
- socket_ipv6 = socket(PF_INET6, SOCK_DGRAM, udp_proto_number() );
+ socket_ipv6 = socket(PF_INET6, SOCK_DGRAM, 0);
if (socket_ipv6 == -1)
{
fprintf(stderr, "IPv6 socket failed.\n");
@@ -97,7 +78,7 @@
int on=1;
- socket_ipv4 = socket(PF_INET, SOCK_DGRAM, udp_proto_number() );
+ socket_ipv4 = socket(PF_INET, SOCK_DGRAM, 0);
if (socket_ipv4 == -1)
{
fprintf(stderr, "IPv4 socket not supported.\n");
@@ -135,7 +116,7 @@
return socket_ipv4;
}
-void server_loop(int port)
+void server_loop(const int port, const bool ipv6enabled)
{
int socket_ipv6, socket_ipv4;
fd_set listen_sockets;
@@ -211,6 +192,7 @@
int main(int argn, char **argv)
{
int port;
+ bool use_ipv6 = false;
/* Processem els parĂ metres d'entrada */
if (argn != 2) {
@@ -219,6 +201,14 @@
}
port=atoi(argv[1]);
+ /* If a port number is typed as negative, it will start
+ * also the ipv6 server */
+ if(port < 0)
+ {
+ port = -port;
+ use_ipv6 = true;
+ }
+
if (port < 0 || port >65535)
{
show_help(argv[0]);
@@ -226,7 +216,8 @@
}
program_ignore_hup();
- server_loop(port);
+
+ server_loop(port, use_ipv6);
return 0;
}