Improve generic ipv4+ipv6 support in libbb.

Convert telnet to it. Now this works:
telnetd -b [::1]:1234 - bind to IPv6 non-standard port
telnet [::1]:1234 - connect to IPv6 non-standard port
telnet ::1 1234 - same
This does not require ANY ipv6-specific code in applets
(no struct sockaddr_in6. In fact, no sockaddr_in, too).
This commit is contained in:
Denis Vlasenko 2007-01-10 09:28:01 +00:00
parent 62a6983a81
commit 9de420c27c
4 changed files with 204 additions and 148 deletions

View File

@ -293,18 +293,17 @@ extern void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen);
extern int xconnect_tcp_v4(struct sockaddr_in *s_addr); extern int xconnect_tcp_v4(struct sockaddr_in *s_addr);
extern struct hostent *xgethostbyname(const char *name); extern struct hostent *xgethostbyname(const char *name);
extern struct hostent *xgethostbyname2(const char *name, int af); extern struct hostent *xgethostbyname2(const char *name, int af);
extern int xsocket_stream_ip4or6(sa_family_t *fp);
typedef union {
struct sockaddr sa;
struct sockaddr_in sin;
#if ENABLE_FEATURE_IPV6
struct sockaddr_in6 sin6;
#endif
} sockaddr_inet;
extern int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen);
extern int create_and_bind_socket_ip4or6(const char *hostaddr, int port);
extern int setsockopt_reuseaddr(int fd); extern int setsockopt_reuseaddr(int fd);
extern int setsockopt_broadcast(int fd); extern int setsockopt_broadcast(int fd);
/* Create server TCP socket bound to bindaddr:port. bindaddr can be NULL,
* numeric IP ("N.N.N.N") or numeric IPv6 address,
* and can have ":PORT" suffix. If no suffix trere, second argument is used */
extern int create_and_bind_stream_or_die(const char *bindaddr, int port);
/* Create client TCP socket connected to peer:port. Peer cannot be NULL.
* Peer can be numeric IP ("N.N.N.N"), numeric IPv6 address or hostname,
* and can have ":PORT" suffix. If no suffix trere, second argument is used */
extern int create_and_connect_stream_or_die(const char *peer, int def_port);
extern char *xstrdup(const char *s); extern char *xstrdup(const char *s);
@ -506,7 +505,7 @@ USE_DESKTOP(long long) int uncompress(int fd_in, int fd_out);
int inflate(int in, int out); int inflate(int in, int out);
unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port); unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port);
void bb_lookup_host(struct sockaddr_in *s_in, const char *host); void bb_lookup_host(struct sockaddr_in *s_in, const char *host);
int bb_make_directory(char *path, long mode, int flags); int bb_make_directory(char *path, long mode, int flags);

View File

@ -6,68 +6,9 @@
* *
*/ */
#include <netinet/in.h>
#include "libbb.h" #include "libbb.h"
/* Return network byte ordered port number for a service.
* If "port" is a number use it as the port.
* If "port" is a name it is looked up in /etc/services, if it isnt found return
* default_port
*/
unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port)
{
unsigned short port_nr = htons(default_port);
if (port) {
char *endptr;
int old_errno;
long port_long;
/* Since this is a lib function, we're not allowed to reset errno to 0.
* Doing so could break an app that is deferring checking of errno. */
old_errno = errno;
errno = 0;
port_long = strtol(port, &endptr, 10);
if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
struct servent *tserv = getservbyname(port, protocol);
if (tserv) {
port_nr = tserv->s_port;
}
} else {
port_nr = htons(port_long);
}
errno = old_errno;
}
return port_nr;
}
void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
{
struct hostent *he;
memset(s_in, 0, sizeof(struct sockaddr_in));
s_in->sin_family = AF_INET;
he = xgethostbyname(host);
memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
}
void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
{
if (connect(s, s_addr, addrlen) < 0) {
if (ENABLE_FEATURE_CLEAN_UP) close(s);
if (s_addr->sa_family == AF_INET)
bb_perror_msg_and_die("%s (%s)",
"cannot connect to remote host",
inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
bb_perror_msg_and_die("cannot connect to remote host");
}
}
int xconnect_tcp_v4(struct sockaddr_in *s_addr)
{
int s = xsocket(AF_INET, SOCK_STREAM, 0);
xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
return s;
}
static const int one = 1; static const int one = 1;
int setsockopt_reuseaddr(int fd) int setsockopt_reuseaddr(int fd)
{ {
@ -78,77 +19,204 @@ int setsockopt_broadcast(int fd)
return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)); return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
} }
int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen) void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
{ {
if (connect(s, s_addr, addrlen) < 0) {
if (ENABLE_FEATURE_CLEAN_UP)
close(s);
if (s_addr->sa_family == AF_INET)
bb_perror_msg_and_die("%s (%s)",
"cannot connect to remote host",
inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
bb_perror_msg_and_die("cannot connect to remote host");
}
}
/* Return network byte ordered port number for a service.
* If "port" is a number use it as the port.
* If "port" is a name it is looked up in /etc/services, if it isnt found return
* default_port */
unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
{
unsigned port_nr = htons(default_port);
if (port) {
int old_errno;
/* Since this is a lib function, we're not allowed to reset errno to 0.
* Doing so could break an app that is deferring checking of errno. */
old_errno = errno;
port_nr = bb_strtou(port, NULL, 10);
if (errno || port_nr > 65535) {
struct servent *tserv = getservbyname(port, protocol);
if (tserv)
port_nr = tserv->s_port;
} else {
port_nr = htons(port_nr);
}
errno = old_errno;
}
return port_nr;
}
/* "Old" networking API - only IPv4 */
void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
{
struct hostent *he;
memset(s_in, 0, sizeof(struct sockaddr_in));
s_in->sin_family = AF_INET;
he = xgethostbyname(host);
memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
}
int xconnect_tcp_v4(struct sockaddr_in *s_addr)
{
int s = xsocket(AF_INET, SOCK_STREAM, 0);
xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
return s;
}
/* "New" networking API */
/* So far we do not expose struct and helpers to libbb */
typedef struct len_and_sockaddr {
int len;
union { union {
struct in_addr a4; struct sockaddr sa;
struct sockaddr_in sin;
#if ENABLE_FEATURE_IPV6 #if ENABLE_FEATURE_IPV6
struct in6_addr a6; struct sockaddr_in6 sin6;
#endif #endif
} a; };
} len_and_sockaddr;
//extern int xsocket_stream_ip4or6(sa_family_t *fp);
//extern len_and_sockaddr* host2sockaddr(const char *host, int def_port);
//extern len_and_sockaddr* dotted2sockaddr(const char *dotted, int def_port);
/* TODO maybe: port spec? like n.n.n.n:nn */ /* peer: "1.2.3.4[:port]", "www.google.com[:port]"
* def_port: if neither of above specifies port #
*/
static len_and_sockaddr* str2sockaddr(const char *host, int def_port, int ai_flags)
{
int rc;
len_and_sockaddr *r; // = NULL;
struct addrinfo *result = NULL;
const char *org_host = host; /* only for error msg */
const char *cp;
char service[sizeof(int)*3 + 1];
struct addrinfo hint;
#if ENABLE_FEATURE_IPV6 /* Ugly parsing of host:addr */
if (socklen >= sizeof(struct sockaddr_in6) if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
&& inet_pton(AF_INET6, dotted, &a.a6) > 0 host++;
) { cp = strchr(host, ']');
((struct sockaddr_in6*)sp)->sin6_family = AF_INET6; if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */
((struct sockaddr_in6*)sp)->sin6_addr = a.a6; bb_error_msg_and_die("bad address '%s'", org_host);
/* ((struct sockaddr_in6*)sp)->sin6_port = */ //return r; /* return NULL */
return 0; /* success */ } else {
cp = strrchr(host, ':');
if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
/* There is more than one ':' (e.g. "::1") */
cp = NULL; /* it's not a port spec */
} }
}
if (cp) {
host = safe_strncpy(alloca(cp - host + 1), host, cp - host);
if (ENABLE_FEATURE_IPV6 && *cp != ':')
cp++; /* skip ']' */
cp++; /* skip ':' */
} else {
utoa_to_buf(def_port, service, sizeof(service));
cp = service;
}
memset(&hint, 0 , sizeof(hint));
/* hint.ai_family = AF_UNSPEC; - zero anyway */
#if !ENABLE_FEATURE_IPV6
hint.ai_family = AF_INET; /* do not try to find IPv6 */
#endif #endif
if (socklen >= sizeof(struct sockaddr_in) /* Needed. Or else we will get each address thrice (or more)
&& inet_pton(AF_INET, dotted, &a.a4) > 0 * for each possible socket type (tcp,udp,raw...): */
) { hint.ai_socktype = SOCK_STREAM;
((struct sockaddr_in*)sp)->sin_family = AF_INET; hint.ai_flags = ai_flags | AI_NUMERICSERV;
((struct sockaddr_in*)sp)->sin_addr = a.a4; rc = getaddrinfo(host, cp, &hint, &result);
/* ((struct sockaddr_in*)sp)->sin_port = */ if (rc || !result)
return 0; /* success */ bb_error_msg_and_die("bad address '%s'", org_host);
} r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
return 1; r->len = result->ai_addrlen;
memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
freeaddrinfo(result);
return r;
} }
int xsocket_stream_ip4or6(sa_family_t *fp) static len_and_sockaddr* host2sockaddr(const char *host, int def_port)
{
return str2sockaddr(host, def_port, 0);
}
static len_and_sockaddr* dotted2sockaddr(const char *host, int def_port)
{
return str2sockaddr(host, def_port, NI_NUMERICHOST);
}
static int xsocket_stream_ip4or6(len_and_sockaddr *lsa)
{ {
int fd; int fd;
#if ENABLE_FEATURE_IPV6 #if ENABLE_FEATURE_IPV6
fd = socket(AF_INET6, SOCK_STREAM, 0); fd = socket(AF_INET6, SOCK_STREAM, 0);
if (fp) *fp = AF_INET6; lsa->sa.sa_family = AF_INET6;
if (fd < 0) lsa->len = sizeof(struct sockaddr_in6);
if (fd >= 0)
return fd;
#endif #endif
{
fd = xsocket(AF_INET, SOCK_STREAM, 0); fd = xsocket(AF_INET, SOCK_STREAM, 0);
if (fp) *fp = AF_INET; lsa->sa.sa_family = AF_INET;
} lsa->len = sizeof(struct sockaddr_in);
return fd; return fd;
} }
int create_and_bind_socket_ip4or6(const char *hostaddr, int port) int create_and_bind_stream_or_die(const char *bindaddr, int port)
{ {
int fd; int fd;
sockaddr_inet sa; len_and_sockaddr *lsa;
memset(&sa, 0, sizeof(sa)); if (bindaddr) {
if (hostaddr) { lsa = dotted2sockaddr(bindaddr, port);
if (dotted2sockaddr(hostaddr, &sa.sa, sizeof(sa))) /* currently NULL check is in str2sockaddr */
bb_error_msg_and_die("bad address '%s'", hostaddr); //if (!lsa)
// bb_error_msg_and_die("bad address '%s'", bindaddr);
/* user specified bind addr dictates family */ /* user specified bind addr dictates family */
fd = xsocket(sa.sa.sa_family, SOCK_STREAM, 0); fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
} else } else {
fd = xsocket_stream_ip4or6(&sa.sa.sa_family); lsa = xzalloc(offsetof(len_and_sockaddr, sa) +
USE_FEATURE_IPV6(sizeof(struct sockaddr_in6))
SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in))
);
fd = xsocket_stream_ip4or6(lsa);
}
setsockopt_reuseaddr(fd); setsockopt_reuseaddr(fd);
xbind(fd, &lsa->sa, lsa->len);
/* if (port >= 0) { */ free(lsa);
#if ENABLE_FEATURE_IPV6 return fd;
if (sa.sa.sa_family == AF_INET6 /* && !sa.sin6.sin6_port */) }
sa.sin6.sin6_port = htons(port);
#endif int create_and_connect_stream_or_die(const char *peer, int port)
if (sa.sa.sa_family == AF_INET /* && !sa.sin.sin_port */) {
sa.sin.sin_port = htons(port); int fd;
/* } */ len_and_sockaddr *lsa;
xbind(fd, &sa.sa, sizeof(sa)); lsa = host2sockaddr(peer, port);
/* currently NULL check is in str2sockaddr */
//if (!lsa)
// bb_error_msg_and_die("bad address '%s'", peer);
fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
setsockopt_reuseaddr(fd);
xconnect(fd, &lsa->sa, lsa->len);
free(lsa);
return fd; return fd;
} }

View File

@ -22,15 +22,7 @@
*/ */
#include <termios.h> #include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <arpa/telnet.h> #include <arpa/telnet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include "busybox.h" #include "busybox.h"
@ -593,8 +585,9 @@ static void cookmode(void)
int telnet_main(int argc, char** argv) int telnet_main(int argc, char** argv)
{ {
char *host;
int port;
int len; int len;
struct sockaddr_in s_in;
#ifdef USE_POLL #ifdef USE_POLL
struct pollfd ufds[2]; struct pollfd ufds[2];
#else #else
@ -610,11 +603,10 @@ int telnet_main(int argc, char** argv)
ttype = getenv("TERM"); ttype = getenv("TERM");
#endif #endif
memset(&G, 0, sizeof G); /* memset(&G, 0, sizeof G); - already is */
if (tcgetattr(0, &G.termios_def) >= 0) { if (tcgetattr(0, &G.termios_def) >= 0) {
G.do_termios = 1; G.do_termios = 1;
G.termios_raw = G.termios_def; G.termios_raw = G.termios_def;
cfmakeraw(&G.termios_raw); cfmakeraw(&G.termios_raw);
} }
@ -627,19 +619,18 @@ int telnet_main(int argc, char** argv)
autologin = getenv("USER"); autologin = getenv("USER");
if (optind < argc) { if (optind < argc) {
bb_lookup_host(&s_in, argv[optind++]); host = argv[optind++];
s_in.sin_port = bb_lookup_port((optind < argc) ? argv[optind++] : port = bb_lookup_port((optind < argc) ? argv[optind++] :
"telnet", "tcp", 23); "telnet", "tcp", 23);
if (optind < argc) if (optind < argc)
bb_show_usage(); bb_show_usage();
} else } else
bb_show_usage(); bb_show_usage();
#else #else
bb_lookup_host(&s_in, argv[1]); host = argv[1];
s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23); port = bb_lookup_port((argc > 2) ? argv[2] : "telnet", "tcp", 23);
#endif #endif
G.netfd = create_and_connect_stream_or_die(host, port);
G.netfd = xconnect_tcp_v4(&s_in);
setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one); setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
@ -655,8 +646,7 @@ int telnet_main(int argc, char** argv)
maxfd = G.netfd + 1; maxfd = G.netfd + 1;
#endif #endif
while (1) while (1) {
{
#ifndef USE_POLL #ifndef USE_POLL
fd_set rfds = readfds; fd_set rfds = readfds;
@ -700,8 +690,7 @@ int telnet_main(int argc, char** argv)
{ {
len = read(G.netfd, G.buf, DATABUFSIZE); len = read(G.netfd, G.buf, DATABUFSIZE);
if (len <= 0) if (len <= 0) {
{
WriteCS(1, "Connection closed by foreign host.\r\n"); WriteCS(1, "Connection closed by foreign host.\r\n");
doexit(1); doexit(1);
} }

View File

@ -414,7 +414,7 @@ telnetd_main(int argc, char **argv)
if (IS_INETD) { if (IS_INETD) {
sessions = make_new_session(0, 1); sessions = make_new_session(0, 1);
} else { } else {
master_fd = create_and_bind_socket_ip4or6(opt_bindaddr, portnbr); master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
xlisten(master_fd, 1); xlisten(master_fd, 1);
if (!(opt & OPT_FOREGROUND)) if (!(opt & OPT_FOREGROUND))
xdaemon(0, 0); xdaemon(0, 0);