1 diff -uNr netkit-telnet-0.17/telnetd/telnetd.c netkit-telnet-0.17.ipv6/telnetd/telnetd.c
2 --- netkit-telnet-0.17/telnetd/telnetd.c 2006-07-13 08:37:18.000000000 +0200
3 +++ netkit-telnet-0.17.ipv6/telnetd/telnetd.c 2006-07-14 08:36:11.000000000 +0200
5 /* #include <netinet/ip.h> */ /* Don't think this is used at all here */
10 #include "pathnames.h"
11 #include "setproctitle.h"
13 #define HAS_IPPROTO_IP
16 -static void doit(struct sockaddr_in *who);
17 +static void doit(struct sockaddr *who, socklen_t wholen);
18 static int terminaltypeok(const char *s);
23 main(int argc, char *argv[], char *env[])
25 - struct sockaddr_in from;
26 + struct sockaddr from;
38 - struct sockaddr_in sn;
41 - memset(&sn, 0, sizeof(sn));
42 - sn.sin_family = AF_INET;
44 + struct addrinfo *ai;
45 + unsigned int nfds = 0;
46 + struct pollfd fds[2];
51 - } else if (argc == 1) {
52 - if ((sp = getservbyname(*argv, "tcp"))!=NULL) {
53 - sn.sin_port = sp->s_port;
56 - int pt = atoi(*argv);
58 - fprintf(stderr, "telnetd: %s: bad port number\n",
63 - sn.sin_port = htons(pt);
68 - sp = getservbyname("telnet", "tcp");
70 - fprintf(stderr, "telnetd: tcp/telnet: unknown service\n");
73 - sn.sin_port = sp->s_port;
75 + struct addrinfo hints;
77 + memset (&hints, '\0', sizeof (hints));
78 + hints.ai_socktype = SOCK_STREAM;
79 + hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
80 + hints.ai_protocol = IPPROTO_TCP;
83 + if (getaddrinfo(NULL, "telnet", &hints, &ai) != 0) {
84 + fprintf(stderr, "telnetd: %s: bad port number\n", *argv);
89 + if (getaddrinfo(NULL, *argv, &hints, &ai) != 0) {
90 + fprintf(stderr, "telnetd: %s: bad port number\n", *argv);
97 - s = socket(AF_INET, SOCK_STREAM, 0);
99 + struct addrinfo *runp;
101 + for (runp = ai; ((runp != NULL) && (nfds < sizeof (fds) / sizeof (fds[0]))); runp = runp->ai_next) {
102 + fds[nfds].fd = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
103 + if (fds[nfds].fd < 0) {
104 perror("telnetd: socket");;
108 + fds[nfds].events = POLLIN;
109 + (void) setsockopt(fds[nfds].fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
111 + if (bind(fds[nfds].fd, runp->ai_addr, runp->ai_addrlen) != 0) {
112 + // Unable to bind to given port. One of the reason can be
113 + // that we can't bind to both IPv4 and IPv6
119 + if (listen(fds[nfds].fd, 1) < 0) {
125 - (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
126 - if (bind(s, (struct sockaddr *)&sn, sizeof(sn)) < 0) {
130 - if (listen(s, 1) < 0) {
140 - ns = accept(s, (struct sockaddr *)&sn, &foo);
145 + int n = poll (fds, nfds, -1);
148 + for (i = 0; i < nfds; i++) {
149 + if (fds[i].revents & POLLIN) {
150 + struct sockaddr_storage rem;
151 + socklen_t remlen = sizeof(rem);
152 + int fd = accept(fds[i].fd, (struct sockaddr *) &rem, &remlen);
163 - (void) dup2(ns, 0);
166 } else if (argc > 0) {
169 @@ -313,13 +339,13 @@
171 openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
172 fromlen = sizeof (from);
173 - if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
174 + if (getpeername(s, &from, &fromlen) < 0) {
175 fprintf(stderr, "%s: ", progname);
176 perror("getpeername");
180 - setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
181 + setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
182 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
185 @@ -333,13 +359,13 @@
187 tos = 020; /* Low Delay bit */
189 - && (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
190 + && (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
191 && (errno != ENOPROTOOPT) )
192 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
194 #endif /* defined(HAS_IPPROTO_IP) && defined(IP_TOS) */
198 + doit(&from, fromlen);
203 * Get a pty, scan input lines.
206 -doit(struct sockaddr_in *who)
207 +doit(struct sockaddr *who, socklen_t wholen)
210 - struct hostent *hp;
214 @@ -623,12 +648,18 @@
215 fatal(net, "All network ports in use");
217 /* get name of connected client */
218 - hp = gethostbyaddr((char *)&who->sin_addr, sizeof (struct in_addr),
223 - host = inet_ntoa(who->sin_addr);
227 + error = getnameinfo(who, wholen, namebuf, sizeof(namebuf), NULL, 0, 0);
230 + perror("getnameinfo: localhost");
231 + perror(gai_strerror(error));
238 * We must make a copy because Kerberos is probably going
239 @@ -649,13 +680,21 @@
241 /* Get local host name */
244 + struct addrinfo hints;
245 + struct addrinfo *res;
248 + memset(&hints, '\0', sizeof(hints));
249 + hints.ai_socktype = SOCK_STREAM;
250 + hints.ai_flags = AI_ADDRCONFIG;
252 gethostname(host_name, sizeof(host_name));
253 - h = gethostbyname(host_name);
255 - strncpy(host_name, h->h_name, sizeof(host_name));
256 - host_name[sizeof(host_name)-1] = 0;
257 + if ((e = getaddrinfo(host_name, NULL, &hints, &res)) != 0) {
258 + perror("getaddrinfo: localhost");
259 + perror(gai_strerror(e));
265 #if defined(AUTHENTICATE) || defined(ENCRYPT)