diff options
author | Denis Vlasenko | 2006-11-22 16:10:39 +0000 |
---|---|---|
committer | Denis Vlasenko | 2006-11-22 16:10:39 +0000 |
commit | c8717cd8571cd35d71696aab8aa847169756bd9f (patch) | |
tree | 254a736d3e761bddc4ba4b00a8f6af1d430ad18f /networking | |
parent | 0801ef28ca655a28543b28754fd804ba0616c21c (diff) | |
download | busybox-c8717cd8571cd35d71696aab8aa847169756bd9f.zip busybox-c8717cd8571cd35d71696aab8aa847169756bd9f.tar.gz |
telnetd: move generic stuff into libbb. It will make it easy
to make other server applets IPv6-aware too.
Diffstat (limited to 'networking')
-rw-r--r-- | networking/fakeidentd.c | 4 | ||||
-rw-r--r-- | networking/telnetd.c | 80 |
2 files changed, 4 insertions, 80 deletions
diff --git a/networking/fakeidentd.c b/networking/fakeidentd.c index 18e86c5..0e543e7 100644 --- a/networking/fakeidentd.c +++ b/networking/fakeidentd.c @@ -110,13 +110,13 @@ static void handlexitsigs(int signum) /* May succeed. If not, won't care. */ static void writepid(uid_t nobody, uid_t nogrp) { - char buf[24]; + char buf[sizeof(int)*3 + 2]; int fd = open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0664); if (fd < 0) return; - snprintf(buf, 23, "%d\n", getpid()); + sprintf(buf, "%d\n", getpid()); write(fd, buf, strlen(buf)); fchown(fd, nobody, nogrp); close(fd); diff --git a/networking/telnetd.c b/networking/telnetd.c index d524ac8..604f65c 100644 --- a/networking/telnetd.c +++ b/networking/telnetd.c @@ -354,87 +354,10 @@ free_session(struct tsession *ts) } } -static int -dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen) -{ - union { - struct in_addr a4; -#if ENABLE_FEATURE_IPV6 - struct in6_addr a6; -#endif - } a; - -#if ENABLE_FEATURE_IPV6 - if (socklen >= sizeof(struct sockaddr_in6) - && inet_pton(AF_INET6, dotted, &a) > 0 - ) { - ((struct sockaddr_in6*)sp)->sin6_family = AF_INET6; - ((struct sockaddr_in6*)sp)->sin6_addr = a.a6; - } else -#endif - if (socklen >= sizeof(struct sockaddr_in) - && inet_pton(AF_INET, dotted, &a) > 0 - ) { - ((struct sockaddr_in*)sp)->sin_family = AF_INET; - ((struct sockaddr_in*)sp)->sin_addr = a.a4; - } else - return 1; - - return 0; /* success */ -} - -static int -xsocket_stream_ip4or6(sa_family_t *fp) -{ - int fd = socket(AF_INET6, SOCK_STREAM, 0); - if (fp) *fp = AF_INET6; - if (fd < 0) { - fd = xsocket(AF_INET, SOCK_STREAM, 0); - if (fp) *fp = AF_INET; - } - return fd; -} - -static int -create_socket(const char *hostaddr, int port) -{ - static const int on = 1; - int fd; - union { - struct sockaddr sa; - struct sockaddr_in sin; -#if ENABLE_FEATURE_IPV6 - struct sockaddr_in6 sin6; -#endif - } sa; - - memset(&sa, 0, sizeof(sa)); - if (hostaddr && dotted2sockaddr(hostaddr, &sa.sa, sizeof(sa))) - bb_show_usage(); - - if (!sa.sa.sa_family) - fd = xsocket_stream_ip4or6(&sa.sa.sa_family); - else /* user specified -b ADDR dictates family */ - fd = xsocket(sa.sa.sa_family, SOCK_STREAM, 0); - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); - -#if ENABLE_FEATURE_IPV6 - if (sa.sa.sa_family == AF_INET6) - sa.sin6.sin6_port = htons(port); -#endif - if (sa.sa.sa_family == AF_INET) - sa.sin.sin_port = htons(port); - - xbind(fd, &sa.sa, sizeof(sa)); - xlisten(fd, 1); - return fd; -} - #else /* !FEATURE_TELNETD_STANDALONE */ /* Never actually called */ void free_session(struct tsession *ts); -int create_socket(const char *hostaddr, int port); #endif @@ -491,7 +414,8 @@ telnetd_main(int argc, char **argv) if (IS_INETD) { sessions = make_new_session(0, 1); } else { - master_fd = create_socket(opt_bindaddr, portnbr); + master_fd = create_and_bind_socket_ip4or6(opt_bindaddr, portnbr); + xlisten(master_fd, 1); if (!(opt & OPT_FOREGROUND)) xdaemon(0, 0); } |