Blame SOURCES/netkit-telnet-0.17-ipv6.diff

055396
diff -uNr netkit-telnet-0.17/telnetd/telnetd.c netkit-telnet-0.17.ipv6/telnetd/telnetd.c
055396
--- netkit-telnet-0.17/telnetd/telnetd.c	2006-07-13 08:37:18.000000000 +0200
055396
+++ netkit-telnet-0.17.ipv6/telnetd/telnetd.c	2006-07-14 08:36:11.000000000 +0200
055396
@@ -49,6 +49,7 @@
055396
 /* #include <netinet/ip.h> */ /* Don't think this is used at all here */
055396
 #include <arpa/inet.h>
055396
 #include <assert.h>
055396
+#include <sys/poll.h>
055396
 #include "telnetd.h"
055396
 #include "pathnames.h"
055396
 #include "setproctitle.h"
055396
@@ -68,7 +69,7 @@
055396
 #define HAS_IPPROTO_IP
055396
 #endif
055396
 
055396
-static void doit(struct sockaddr_in *who);
055396
+static void doit(struct sockaddr *who, socklen_t wholen);
055396
 static int terminaltypeok(const char *s);
055396
 
055396
 /*
055396
@@ -90,7 +91,7 @@
055396
 int
055396
 main(int argc, char *argv[], char *env[])
055396
 {
055396
-	struct sockaddr_in from;
055396
+	struct sockaddr from;
055396
 	int on = 1;
055396
 	socklen_t fromlen;
055396
 	register int ch;
055396
@@ -248,64 +249,89 @@
055396
 	argc -= optind;
055396
 	argv += optind;
055396
 
055396
-	if (debug) {
055396
-	    int s, ns;
055396
-	    socklen_t foo;
055396
-	    struct servent *sp;
055396
-	    struct sockaddr_in sn;
055396
+	int s = 0;
055396
 
055396
-	    memset(&sn, 0, sizeof(sn));
055396
-	    sn.sin_family = AF_INET;
055396
+	if (debug) {
055396
+	    struct addrinfo *ai;
055396
+	    unsigned int nfds = 0;
055396
+	    struct pollfd fds[2];
055396
 
055396
 	    if (argc > 1) {
055396
-		usage();
055396
-		/* NOTREACHED */
055396
-	    } else if (argc == 1) {
055396
-		    if ((sp = getservbyname(*argv, "tcp"))!=NULL) {
055396
-			sn.sin_port = sp->s_port;
055396
-		    } 
055396
-		    else {
055396
-			int pt = atoi(*argv);
055396
-			if (pt <= 0) {
055396
-			    fprintf(stderr, "telnetd: %s: bad port number\n",
055396
-				    *argv);
055396
-			    usage();
055396
-			    /* NOTREACHED */
055396
-			}
055396
-			sn.sin_port = htons(pt);
055396
-		   }
055396
+	        usage();
055396
+	        /* NOTREACHED */
055396
 	    } else {
055396
-		sp = getservbyname("telnet", "tcp");
055396
-		if (sp == 0) {
055396
-		    fprintf(stderr, "telnetd: tcp/telnet: unknown service\n");
055396
-		    exit(1);
055396
-		}
055396
-		sn.sin_port = sp->s_port;
055396
-	    }
055396
+	        struct addrinfo hints;
055396
+
055396
+	        memset (&hints, '\0', sizeof (hints));
055396
+	        hints.ai_socktype = SOCK_STREAM;
055396
+	        hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
055396
+	        hints.ai_protocol = IPPROTO_TCP;
055396
+
055396
+	        if (argc == 0) {
055396
+	            if (getaddrinfo(NULL, "telnet", &hints, &ai) != 0) {
055396
+	               fprintf(stderr, "telnetd: %s: bad port number\n", *argv);
055396
+	                usage();
055396
+	                /* NOTREACHED */
055396
+	            }
055396
+	        } else {
055396
+	            if (getaddrinfo(NULL, *argv, &hints, &ai) != 0) {
055396
+		        fprintf(stderr, "telnetd: %s: bad port number\n", *argv);
055396
+		        usage();
055396
+		        /* NOTREACHED */
055396
+		    }
055396
+                }
055396
+	    }	
055396
 
055396
-	    s = socket(AF_INET, SOCK_STREAM, 0);
055396
-	    if (s < 0) {
055396
+	    struct addrinfo *runp;
055396
+	    int b = 0;
055396
+	    for (runp = ai; ((runp != NULL) && (nfds < sizeof (fds) / sizeof (fds[0]))); runp = runp->ai_next) {
055396
+	        fds[nfds].fd = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
055396
+	        if (fds[nfds].fd < 0) {
055396
 		    perror("telnetd: socket");;
055396
-		    exit(1);
055396
+	            exit(1);
055396
+	        }
055396
+	        fds[nfds].events = POLLIN;
055396
+	        (void) setsockopt(fds[nfds].fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
055396
+
055396
+	        if (bind(fds[nfds].fd, runp->ai_addr, runp->ai_addrlen) != 0) {
055396
+	            // Unable to bind to given port. One of the reason can be
055396
+	            // that we can't bind to both IPv4 and IPv6
055396
+	            break;
055396
+	        } else {			
055396
+	            b++;
055396
+	        }
055396
+
055396
+	        if (listen(fds[nfds].fd, 1) < 0) {
055396
+	            perror("listen");
055396
+	            exit(1);
055396
+	        }
055396
+                nfds++;
055396
 	    }
055396
-	    (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
055396
-	    if (bind(s, (struct sockaddr *)&sn, sizeof(sn)) < 0) {
055396
-		perror("bind");
055396
-		exit(1);
055396
-	    }
055396
-	    if (listen(s, 1) < 0) {
055396
-		perror("listen");
055396
-		exit(1);
055396
+	    freeaddrinfo(ai);
055396
+
055396
+	    if (b == 0) {
055396
+	        perror("bind");
055396
+	        exit(1);
055396
 	    }
055396
-	    foo = sizeof(sn);
055396
-	    ns = accept(s, (struct sockaddr *)&sn, &foo;;
055396
-	    if (ns < 0) {
055396
-		perror("accept");
055396
-		exit(1);
055396
+
055396
+	    int n = poll (fds, nfds, -1);
055396
+	    if (n > 0) {
055396
+	        unsigned int i;
055396
+	        for (i = 0; i < nfds; i++) {
055396
+	            if (fds[i].revents & POLLIN) {
055396
+	                struct sockaddr_storage rem;
055396
+	                socklen_t remlen = sizeof(rem);
055396
+	                int fd = accept(fds[i].fd, (struct sockaddr *) &rem, &remlen);
055396
+
055396
+	                if (fd < 0) {
055396
+	                    perror("accept");
055396
+	                    exit(1);
055396
+	                }
055396
+
055396
+	                s = fd;
055396
+	            }
055396
+	        }
055396
 	    }
055396
-	    (void) dup2(ns, 0);
055396
-	    (void) close(ns);
055396
-	    (void) close(s);
055396
 	} else if (argc > 0) {
055396
 		usage();
055396
 		/* NOT REACHED */
055396
@@ -313,13 +339,13 @@
055396
 
055396
 	openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
055396
 	fromlen = sizeof (from);
055396
-	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
055396
+	if (getpeername(s, &from, &fromlen) < 0) {
055396
 		fprintf(stderr, "%s: ", progname);
055396
 		perror("getpeername");
055396
 		_exit(1);
055396
 	}
055396
 	if (keepalive &&
055396
-	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
055396
+	    setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
055396
 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
055396
 	}
055396
 
055396
@@ -333,13 +359,13 @@
055396
 		if (tos < 0)
055396
 			tos = 020;	/* Low Delay bit */
055396
 		if (tos
055396
-		   && (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
055396
+		   && (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
055396
 		   && (errno != ENOPROTOOPT) )
055396
 			syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
055396
 	}
055396
 #endif	/* defined(HAS_IPPROTO_IP) && defined(IP_TOS) */
055396
-	net = 0;
055396
-	doit(&from;;
055396
+	net = s;
055396
+	doit(&from, fromlen);
055396
 	/* NOTREACHED */
055396
 	return 0;
055396
 }  /* end of main */
055396
@@ -608,10 +634,9 @@
055396
  * Get a pty, scan input lines.
055396
  */
055396
 static void
055396
-doit(struct sockaddr_in *who)
055396
+doit(struct sockaddr *who, socklen_t wholen)
055396
 {
055396
 	const char *host;
055396
-	struct hostent *hp;
055396
 	int level;
055396
 	char user_name[256];
055396
 
055396
@@ -623,12 +648,18 @@
055396
 		fatal(net, "All network ports in use");
055396
 
055396
 	/* get name of connected client */
055396
-	hp = gethostbyaddr((char *)&who->sin_addr, sizeof (struct in_addr),
055396
-		who->sin_family);
055396
-	if (hp)
055396
-		host = hp->h_name;
055396
-	else
055396
-		host = inet_ntoa(who->sin_addr);
055396
+	int error = -1;
055396
+	char namebuf[255];
055396
+
055396
+	error = getnameinfo(who, wholen, namebuf, sizeof(namebuf), NULL, 0, 0);
055396
+	
055396
+	if (error) {
055396
+		perror("getnameinfo: localhost");
055396
+		perror(gai_strerror(error));
055396
+		exit(1);		
055396
+	}
055396
+	
055396
+	host = namebuf;
055396
 
055396
 	/*
055396
 	 * We must make a copy because Kerberos is probably going
055396
@@ -649,13 +680,21 @@
055396
 
055396
 	/* Get local host name */
055396
 	{
055396
-		struct hostent *h;
055396
+		struct addrinfo hints;
055396
+		struct addrinfo *res;
055396
+		int e;
055396
+
055396
+		memset(&hints, '\0', sizeof(hints));
055396
+		hints.ai_socktype = SOCK_STREAM;
055396
+		hints.ai_flags = AI_ADDRCONFIG;
055396
+
055396
 		gethostname(host_name, sizeof(host_name));
055396
-		h = gethostbyname(host_name);
055396
-		if (h) {
055396
-		    strncpy(host_name, h->h_name, sizeof(host_name));
055396
-		    host_name[sizeof(host_name)-1] = 0;
055396
+		if ((e = getaddrinfo(host_name, NULL, &hints, &res)) != 0) {
055396
+			perror("getaddrinfo: localhost");
055396
+			perror(gai_strerror(e));
055396
+			exit(1);
055396
 		}
055396
+		freeaddrinfo(res);
055396
 	}
055396
 
055396
 #if	defined(AUTHENTICATE) || defined(ENCRYPT)