2002-07-03 11:51:44 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-04-14 17:51:38 +00:00
|
|
|
* Connect to host at port using address resolution from getaddrinfo
|
2002-07-03 11:51:44 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-01-10 09:28:01 +00:00
|
|
|
#include <netinet/in.h>
|
2002-07-03 11:51:44 +00:00
|
|
|
#include "libbb.h"
|
|
|
|
|
2007-01-10 09:28:01 +00:00
|
|
|
int setsockopt_reuseaddr(int fd)
|
|
|
|
{
|
2007-01-22 14:12:08 +00:00
|
|
|
return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
|
2007-01-10 09:28:01 +00:00
|
|
|
}
|
|
|
|
int setsockopt_broadcast(int fd)
|
|
|
|
{
|
2007-01-22 14:12:08 +00:00
|
|
|
return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
|
2007-01-10 09:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-12 20:59:31 +00:00
|
|
|
/* Return port number for a service.
|
2003-12-20 01:47:18 +00:00
|
|
|
* 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
|
2007-01-10 09:28:01 +00:00
|
|
|
* default_port */
|
|
|
|
unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
|
2002-07-03 11:51:44 +00:00
|
|
|
{
|
2007-01-12 10:35:23 +00:00
|
|
|
unsigned port_nr = default_port;
|
2003-12-20 01:47:18 +00:00
|
|
|
if (port) {
|
2003-12-23 20:37:23 +00:00
|
|
|
int old_errno;
|
2002-07-03 11:51:44 +00:00
|
|
|
|
2003-12-23 20:37:23 +00:00
|
|
|
/* 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;
|
2007-01-10 09:28:01 +00:00
|
|
|
port_nr = bb_strtou(port, NULL, 10);
|
|
|
|
if (errno || port_nr > 65535) {
|
2004-01-17 05:03:31 +00:00
|
|
|
struct servent *tserv = getservbyname(port, protocol);
|
2007-01-13 21:05:38 +00:00
|
|
|
port_nr = default_port;
|
2007-01-10 09:28:01 +00:00
|
|
|
if (tserv)
|
2007-01-12 10:35:23 +00:00
|
|
|
port_nr = ntohs(tserv->s_port);
|
2003-12-20 01:47:18 +00:00
|
|
|
}
|
2003-12-23 20:37:23 +00:00
|
|
|
errno = old_errno;
|
2002-07-03 11:51:44 +00:00
|
|
|
}
|
2007-01-12 10:35:23 +00:00
|
|
|
return (uint16_t)port_nr;
|
2003-10-31 09:31:46 +00:00
|
|
|
}
|
2002-07-03 11:51:44 +00:00
|
|
|
|
2007-01-10 09:28:01 +00:00
|
|
|
|
|
|
|
/* "Old" networking API - only IPv4 */
|
|
|
|
|
2007-01-22 09:36:41 +00:00
|
|
|
/*
|
2003-12-20 01:47:18 +00:00
|
|
|
void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
|
2003-10-31 09:31:46 +00:00
|
|
|
{
|
|
|
|
struct hostent *he;
|
2002-07-03 11:51:44 +00:00
|
|
|
|
2003-10-31 09:31:46 +00:00
|
|
|
memset(s_in, 0, sizeof(struct sockaddr_in));
|
|
|
|
s_in->sin_family = AF_INET;
|
2002-07-03 11:51:44 +00:00
|
|
|
he = xgethostbyname(host);
|
2003-10-31 09:31:46 +00:00
|
|
|
memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
|
|
|
|
}
|
|
|
|
|
2007-01-22 09:36:41 +00:00
|
|
|
|
2006-10-26 01:09:46 +00:00
|
|
|
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));
|
2002-07-03 11:51:44 +00:00
|
|
|
return s;
|
|
|
|
}
|
2007-01-22 09:36:41 +00:00
|
|
|
*/
|
2006-11-22 23:22:06 +00:00
|
|
|
|
2007-01-10 09:28:01 +00:00
|
|
|
/* "New" networking API */
|
2006-11-22 16:10:39 +00:00
|
|
|
|
|
|
|
|
2007-01-12 20:59:31 +00:00
|
|
|
int get_nport(len_and_sockaddr *lsa)
|
|
|
|
{
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
if (lsa->sa.sa_family == AF_INET6) {
|
|
|
|
return lsa->sin6.sin6_port;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (lsa->sa.sa_family == AF_INET) {
|
|
|
|
return lsa->sin.sin_port;
|
|
|
|
}
|
|
|
|
/* What? UNIX socket? IPX?? :) */
|
2007-01-22 09:36:41 +00:00
|
|
|
return -1;
|
2007-01-12 20:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_nport(len_and_sockaddr *lsa, unsigned port)
|
2007-01-11 16:50:23 +00:00
|
|
|
{
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
if (lsa->sa.sa_family == AF_INET6) {
|
|
|
|
lsa->sin6.sin6_port = port;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (lsa->sa.sa_family == AF_INET) {
|
|
|
|
lsa->sin.sin_port = port;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* What? UNIX socket? IPX?? :) */
|
|
|
|
}
|
2007-01-10 09:28:01 +00:00
|
|
|
|
2007-02-04 02:39:08 +00:00
|
|
|
/* We hijack this constant to mean something else */
|
|
|
|
/* It doesn't hurt because we will remove this bit anyway */
|
|
|
|
#define DIE_ON_ERROR AI_CANONNAME
|
|
|
|
|
2007-01-22 09:36:41 +00:00
|
|
|
/* host: "1.2.3.4[:port]", "www.google.com[:port]"
|
2007-02-04 02:39:08 +00:00
|
|
|
* port: if neither of above specifies port # */
|
2007-01-22 22:45:27 +00:00
|
|
|
static len_and_sockaddr* str2sockaddr(
|
|
|
|
const char *host, int port,
|
|
|
|
USE_FEATURE_IPV6(sa_family_t af,)
|
|
|
|
int ai_flags)
|
2007-01-10 09:28:01 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2007-02-04 02:39:08 +00:00
|
|
|
len_and_sockaddr *r = NULL;
|
2007-01-10 09:28:01 +00:00
|
|
|
struct addrinfo *result = NULL;
|
|
|
|
const char *org_host = host; /* only for error msg */
|
|
|
|
const char *cp;
|
|
|
|
struct addrinfo hint;
|
|
|
|
|
|
|
|
/* Ugly parsing of host:addr */
|
|
|
|
if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
|
|
|
|
host++;
|
|
|
|
cp = strchr(host, ']');
|
|
|
|
if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */
|
|
|
|
bb_error_msg_and_die("bad address '%s'", org_host);
|
|
|
|
//return r; /* return NULL */
|
|
|
|
} 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 */
|
|
|
|
}
|
2006-11-22 16:10:39 +00:00
|
|
|
}
|
2007-01-10 09:28:01 +00:00
|
|
|
if (cp) {
|
2007-01-10 20:19:56 +00:00
|
|
|
int sz = cp - host + 1;
|
|
|
|
host = safe_strncpy(alloca(sz), host, sz);
|
2007-01-10 09:28:01 +00:00
|
|
|
if (ENABLE_FEATURE_IPV6 && *cp != ':')
|
|
|
|
cp++; /* skip ']' */
|
|
|
|
cp++; /* skip ':' */
|
2007-01-11 16:50:23 +00:00
|
|
|
port = xatou16(cp);
|
2007-01-10 09:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&hint, 0 , sizeof(hint));
|
|
|
|
#if !ENABLE_FEATURE_IPV6
|
|
|
|
hint.ai_family = AF_INET; /* do not try to find IPv6 */
|
2007-01-22 22:45:27 +00:00
|
|
|
#else
|
|
|
|
hint.ai_family = af;
|
2007-01-10 09:28:01 +00:00
|
|
|
#endif
|
|
|
|
/* Needed. Or else we will get each address thrice (or more)
|
|
|
|
* for each possible socket type (tcp,udp,raw...): */
|
|
|
|
hint.ai_socktype = SOCK_STREAM;
|
2007-02-04 02:39:08 +00:00
|
|
|
hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
|
2007-01-11 16:50:23 +00:00
|
|
|
rc = getaddrinfo(host, NULL, &hint, &result);
|
2007-02-04 02:39:08 +00:00
|
|
|
if (rc || !result) {
|
2007-02-04 02:39:55 +00:00
|
|
|
bb_error_msg("bad address '%s'", org_host);
|
2007-02-04 02:39:08 +00:00
|
|
|
if (ai_flags & DIE_ON_ERROR)
|
2007-02-04 02:39:55 +00:00
|
|
|
sleep_and_die();
|
2007-02-04 02:39:08 +00:00
|
|
|
goto ret;
|
|
|
|
}
|
2007-01-10 09:28:01 +00:00
|
|
|
r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
|
|
|
|
r->len = result->ai_addrlen;
|
|
|
|
memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
|
2007-01-12 20:59:31 +00:00
|
|
|
set_nport(r, htons(port));
|
2007-02-04 02:39:08 +00:00
|
|
|
ret:
|
2007-01-10 09:28:01 +00:00
|
|
|
freeaddrinfo(result);
|
|
|
|
return r;
|
|
|
|
}
|
2007-01-22 22:45:27 +00:00
|
|
|
#if !ENABLE_FEATURE_IPV6
|
|
|
|
#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
2007-02-04 02:39:08 +00:00
|
|
|
len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
|
2007-01-22 22:45:27 +00:00
|
|
|
{
|
2007-02-04 02:39:08 +00:00
|
|
|
return str2sockaddr(host, port, af, DIE_ON_ERROR);
|
2007-01-22 22:45:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-01-10 09:28:01 +00:00
|
|
|
|
2007-02-04 02:39:55 +00:00
|
|
|
len_and_sockaddr* host2sockaddr(const char *host, int port)
|
|
|
|
{
|
|
|
|
return str2sockaddr(host, port, AF_UNSPEC, 0);
|
|
|
|
}
|
|
|
|
|
2007-02-04 02:39:08 +00:00
|
|
|
len_and_sockaddr* xhost2sockaddr(const char *host, int port)
|
2007-01-10 09:28:01 +00:00
|
|
|
{
|
2007-02-04 02:39:08 +00:00
|
|
|
return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
|
2007-01-10 09:28:01 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 02:39:08 +00:00
|
|
|
static len_and_sockaddr* xdotted2sockaddr(const char *host, int port)
|
2007-01-10 09:28:01 +00:00
|
|
|
{
|
2007-02-04 02:39:08 +00:00
|
|
|
return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
|
2006-11-22 16:10:39 +00:00
|
|
|
}
|
|
|
|
|
2007-01-12 20:59:31 +00:00
|
|
|
int xsocket_stream(len_and_sockaddr **lsap)
|
2006-11-22 16:10:39 +00:00
|
|
|
{
|
2007-01-12 20:59:31 +00:00
|
|
|
len_and_sockaddr *lsa;
|
2006-11-22 16:10:39 +00:00
|
|
|
int fd;
|
2007-01-12 20:59:31 +00:00
|
|
|
int len = sizeof(struct sockaddr_in);
|
|
|
|
int family = AF_INET;
|
|
|
|
|
2006-11-22 16:10:39 +00:00
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
fd = socket(AF_INET6, SOCK_STREAM, 0);
|
2007-01-12 20:59:31 +00:00
|
|
|
if (fd >= 0) {
|
|
|
|
len = sizeof(struct sockaddr_in6);
|
|
|
|
family = AF_INET6;
|
|
|
|
} else
|
2006-11-22 16:10:39 +00:00
|
|
|
#endif
|
2007-01-12 20:59:31 +00:00
|
|
|
{
|
|
|
|
fd = xsocket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
}
|
|
|
|
lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len);
|
|
|
|
lsa->len = len;
|
|
|
|
lsa->sa.sa_family = family;
|
|
|
|
*lsap = lsa;
|
2006-11-22 16:10:39 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2007-01-10 09:28:01 +00:00
|
|
|
int create_and_bind_stream_or_die(const char *bindaddr, int port)
|
2006-11-22 16:10:39 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2007-01-10 09:28:01 +00:00
|
|
|
len_and_sockaddr *lsa;
|
2006-11-22 16:10:39 +00:00
|
|
|
|
2007-01-10 20:19:56 +00:00
|
|
|
if (bindaddr && bindaddr[0]) {
|
2007-02-04 02:39:08 +00:00
|
|
|
lsa = xdotted2sockaddr(bindaddr, port);
|
2006-11-22 16:10:39 +00:00
|
|
|
/* user specified bind addr dictates family */
|
2007-01-10 09:28:01 +00:00
|
|
|
fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
|
|
|
|
} else {
|
2007-01-12 20:59:31 +00:00
|
|
|
fd = xsocket_stream(&lsa);
|
2007-01-13 21:05:38 +00:00
|
|
|
set_nport(lsa, htons(port));
|
2007-01-10 09:28:01 +00:00
|
|
|
}
|
2006-11-22 23:22:06 +00:00
|
|
|
setsockopt_reuseaddr(fd);
|
2007-01-10 09:28:01 +00:00
|
|
|
xbind(fd, &lsa->sa, lsa->len);
|
|
|
|
free(lsa);
|
|
|
|
return fd;
|
|
|
|
}
|
2006-11-22 16:10:39 +00:00
|
|
|
|
2007-01-10 09:28:01 +00:00
|
|
|
int create_and_connect_stream_or_die(const char *peer, int port)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
len_and_sockaddr *lsa;
|
2006-11-22 16:10:39 +00:00
|
|
|
|
2007-02-04 02:39:08 +00:00
|
|
|
lsa = xhost2sockaddr(peer, port);
|
2007-01-10 09:28:01 +00:00
|
|
|
fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
|
|
|
|
setsockopt_reuseaddr(fd);
|
|
|
|
xconnect(fd, &lsa->sa, lsa->len);
|
|
|
|
free(lsa);
|
2006-11-22 16:10:39 +00:00
|
|
|
return fd;
|
|
|
|
}
|
2007-01-11 16:50:23 +00:00
|
|
|
|
|
|
|
int xconnect_stream(const len_and_sockaddr *lsa)
|
|
|
|
{
|
|
|
|
int fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
|
|
|
|
xconnect(fd, &lsa->sa, lsa->len);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2007-01-22 22:43:05 +00:00
|
|
|
/* We hijack this constant to mean something else */
|
|
|
|
/* It doesn't hurt because we will add this bit anyway */
|
|
|
|
#define IGNORE_PORT NI_NUMERICSERV
|
2007-01-11 16:50:23 +00:00
|
|
|
static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags)
|
|
|
|
{
|
|
|
|
char host[128];
|
|
|
|
char serv[16];
|
|
|
|
int rc = getnameinfo(sa, salen,
|
|
|
|
host, sizeof(host),
|
2007-01-22 22:43:05 +00:00
|
|
|
/* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
|
2007-01-11 16:50:23 +00:00
|
|
|
serv, sizeof(serv),
|
2007-01-22 09:36:41 +00:00
|
|
|
/* do not resolve port# into service _name_ */
|
|
|
|
flags | NI_NUMERICSERV
|
2007-01-11 16:50:23 +00:00
|
|
|
);
|
2007-01-22 09:36:41 +00:00
|
|
|
if (rc)
|
|
|
|
return NULL;
|
2007-01-22 22:43:05 +00:00
|
|
|
if (flags & IGNORE_PORT)
|
|
|
|
return xstrdup(host);
|
2007-01-22 09:36:41 +00:00
|
|
|
#if ENABLE_FEATURE_IPV6
|
2007-01-22 22:43:05 +00:00
|
|
|
if (sa->sa_family == AF_INET6) {
|
|
|
|
if (strchr(host, ':')) /* heh, it's not a resolved hostname */
|
|
|
|
return xasprintf("[%s]:%s", host, serv);
|
|
|
|
/*return xasprintf("%s:%s", host, serv);*/
|
|
|
|
/* - fall through instead */
|
|
|
|
}
|
2007-01-22 09:36:41 +00:00
|
|
|
#endif
|
|
|
|
/* For now we don't support anything else, so it has to be INET */
|
|
|
|
/*if (sa->sa_family == AF_INET)*/
|
|
|
|
return xasprintf("%s:%s", host, serv);
|
2007-01-22 22:43:05 +00:00
|
|
|
/*return xstrdup(host);*/
|
2007-01-11 16:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen)
|
|
|
|
{
|
|
|
|
return sockaddr2str(sa, salen, 0);
|
|
|
|
}
|
|
|
|
|
2007-01-24 23:53:22 +00:00
|
|
|
/* Unused
|
2007-01-22 22:45:27 +00:00
|
|
|
char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa, socklen_t salen)
|
|
|
|
{
|
|
|
|
return sockaddr2str(sa, salen, IGNORE_PORT);
|
|
|
|
}
|
2007-01-24 23:53:22 +00:00
|
|
|
*/
|
2007-01-22 22:45:27 +00:00
|
|
|
|
2007-01-22 22:43:05 +00:00
|
|
|
char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa, socklen_t salen)
|
|
|
|
{
|
|
|
|
return sockaddr2str(sa, salen, NI_NAMEREQD | IGNORE_PORT);
|
|
|
|
}
|
2007-01-11 16:50:23 +00:00
|
|
|
char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen)
|
|
|
|
{
|
|
|
|
return sockaddr2str(sa, salen, NI_NUMERICHOST);
|
|
|
|
}
|
2007-01-22 22:43:05 +00:00
|
|
|
|
|
|
|
char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen)
|
|
|
|
{
|
|
|
|
return sockaddr2str(sa, salen, NI_NUMERICHOST | IGNORE_PORT);
|
|
|
|
}
|