2002-07-03 17:21:44 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-04-14 23:21:38 +05:30
|
|
|
* Connect to host at port using address resolution from getaddrinfo
|
2002-07-03 17:21:44 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2002-07-03 17:21:44 +05:30
|
|
|
*/
|
|
|
|
|
2010-06-19 11:07:42 +05:30
|
|
|
#include <sys/types.h>
|
2009-09-06 16:17:55 +05:30
|
|
|
#include <sys/socket.h> /* netinet/in.h needs it */
|
2007-01-10 14:58:01 +05:30
|
|
|
#include <netinet/in.h>
|
2008-12-10 16:42:16 +05:30
|
|
|
#include <net/if.h>
|
2009-04-25 18:46:53 +05:30
|
|
|
#include <sys/un.h>
|
2002-07-03 17:21:44 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC setsockopt_reuseaddr(int fd)
|
2007-01-10 14:58:01 +05:30
|
|
|
{
|
2007-08-18 19:46:39 +05:30
|
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
|
2007-01-10 14:58:01 +05:30
|
|
|
}
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC setsockopt_broadcast(int fd)
|
2007-01-10 14:58:01 +05:30
|
|
|
{
|
2007-01-22 19:42:08 +05:30
|
|
|
return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
|
2007-01-10 14:58:01 +05:30
|
|
|
}
|
2010-03-26 23:38:53 +05:30
|
|
|
|
|
|
|
#ifdef SO_BINDTODEVICE
|
2008-12-10 16:42:16 +05:30
|
|
|
int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
struct ifreq ifr;
|
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, iface);
|
2008-12-10 16:54:33 +05:30
|
|
|
/* NB: passing (iface, strlen(iface) + 1) does not work!
|
|
|
|
* (maybe it works on _some_ kernels, but not on 2.6.26)
|
|
|
|
* Actually, ifr_name is at offset 0, and in practice
|
2008-12-10 16:42:16 +05:30
|
|
|
* just giving char[IFNAMSIZ] instead of struct ifreq works too.
|
|
|
|
* But just in case it's not true on some obscure arch... */
|
|
|
|
r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
|
|
|
|
if (r)
|
|
|
|
bb_perror_msg("can't bind to interface %s", iface);
|
|
|
|
return r;
|
|
|
|
}
|
2010-03-26 23:38:53 +05:30
|
|
|
#else
|
|
|
|
int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM,
|
|
|
|
const char *iface UNUSED_PARAM)
|
|
|
|
{
|
|
|
|
bb_error_msg("SO_BINDTODEVICE is not supported on this system");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-10 16:42:16 +05:30
|
|
|
|
2010-04-06 21:13:29 +05:30
|
|
|
static len_and_sockaddr* get_lsa(int fd, int (*get_name)(int fd, struct sockaddr *addr, socklen_t *addrlen))
|
2009-03-09 05:42:37 +05:30
|
|
|
{
|
2009-04-22 05:18:38 +05:30
|
|
|
len_and_sockaddr lsa;
|
|
|
|
len_and_sockaddr *lsa_ptr;
|
2009-03-09 05:42:37 +05:30
|
|
|
|
2009-04-22 05:18:38 +05:30
|
|
|
lsa.len = LSA_SIZEOF_SA;
|
2010-04-06 21:13:29 +05:30
|
|
|
if (get_name(fd, &lsa.u.sa, &lsa.len) != 0)
|
2009-03-09 05:42:37 +05:30
|
|
|
return NULL;
|
2009-04-22 05:18:38 +05:30
|
|
|
|
|
|
|
lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
|
|
|
|
if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
|
|
|
|
lsa_ptr->len = lsa.len;
|
2010-04-06 21:13:29 +05:30
|
|
|
get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
|
2009-04-22 05:18:38 +05:30
|
|
|
} else {
|
|
|
|
memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
|
|
|
|
}
|
|
|
|
return lsa_ptr;
|
2009-03-09 05:42:37 +05:30
|
|
|
}
|
2007-01-10 14:58:01 +05:30
|
|
|
|
2010-04-06 21:13:29 +05:30
|
|
|
len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
|
|
|
|
{
|
|
|
|
return get_lsa(fd, getsockname);
|
|
|
|
}
|
|
|
|
|
|
|
|
len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd)
|
|
|
|
{
|
|
|
|
return get_lsa(fd, getpeername);
|
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
|
2007-01-10 14:58:01 +05:30
|
|
|
{
|
|
|
|
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)",
|
2009-11-13 13:38:27 +05:30
|
|
|
"can't connect to remote host",
|
2007-01-10 14:58:01 +05:30
|
|
|
inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_perror_msg_and_die("can't connect to remote host");
|
2007-01-10 14:58:01 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-13 02:29:31 +05:30
|
|
|
/* Return port number for a service.
|
2003-12-20 07:17:18 +05:30
|
|
|
* If "port" is a number use it as the port.
|
2009-03-09 05:42:37 +05:30
|
|
|
* If "port" is a name it is looked up in /etc/services,
|
|
|
|
* if it isnt found return default_port
|
|
|
|
*/
|
2008-06-27 08:22:20 +05:30
|
|
|
unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
|
2002-07-03 17:21:44 +05:30
|
|
|
{
|
2007-01-12 16:05:23 +05:30
|
|
|
unsigned port_nr = default_port;
|
2003-12-20 07:17:18 +05:30
|
|
|
if (port) {
|
2003-12-24 02:07:23 +05:30
|
|
|
int old_errno;
|
2002-07-03 17:21:44 +05:30
|
|
|
|
2003-12-24 02:07:23 +05:30
|
|
|
/* 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 14:58:01 +05:30
|
|
|
port_nr = bb_strtou(port, NULL, 10);
|
|
|
|
if (errno || port_nr > 65535) {
|
2004-01-17 10:33:31 +05:30
|
|
|
struct servent *tserv = getservbyname(port, protocol);
|
2007-01-14 02:35:38 +05:30
|
|
|
port_nr = default_port;
|
2007-01-10 14:58:01 +05:30
|
|
|
if (tserv)
|
2007-01-12 16:05:23 +05:30
|
|
|
port_nr = ntohs(tserv->s_port);
|
2003-12-20 07:17:18 +05:30
|
|
|
}
|
2003-12-24 02:07:23 +05:30
|
|
|
errno = old_errno;
|
2002-07-03 17:21:44 +05:30
|
|
|
}
|
2007-01-12 16:05:23 +05:30
|
|
|
return (uint16_t)port_nr;
|
2003-10-31 15:01:46 +05:30
|
|
|
}
|
2002-07-03 17:21:44 +05:30
|
|
|
|
2007-01-10 14:58:01 +05:30
|
|
|
|
|
|
|
/* "New" networking API */
|
2006-11-22 21:40:39 +05:30
|
|
|
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC get_nport(const struct sockaddr *sa)
|
2007-01-13 02:29:31 +05:30
|
|
|
{
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
2007-04-01 06:48:20 +05:30
|
|
|
if (sa->sa_family == AF_INET6) {
|
|
|
|
return ((struct sockaddr_in6*)sa)->sin6_port;
|
2007-01-13 02:29:31 +05:30
|
|
|
}
|
|
|
|
#endif
|
2007-04-01 06:48:20 +05:30
|
|
|
if (sa->sa_family == AF_INET) {
|
|
|
|
return ((struct sockaddr_in*)sa)->sin_port;
|
2007-01-13 02:29:31 +05:30
|
|
|
}
|
|
|
|
/* What? UNIX socket? IPX?? :) */
|
2007-01-22 15:06:41 +05:30
|
|
|
return -1;
|
2007-01-13 02:29:31 +05:30
|
|
|
}
|
|
|
|
|
2011-04-07 21:22:20 +05:30
|
|
|
void FAST_FUNC set_nport(struct sockaddr *sa, unsigned port)
|
2007-01-11 22:20:23 +05:30
|
|
|
{
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
2011-04-07 21:22:20 +05:30
|
|
|
if (sa->sa_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *sin6 = (void*) sa;
|
|
|
|
sin6->sin6_port = port;
|
2007-01-11 22:20:23 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2011-04-07 21:22:20 +05:30
|
|
|
if (sa->sa_family == AF_INET) {
|
|
|
|
struct sockaddr_in *sin = (void*) sa;
|
|
|
|
sin->sin_port = port;
|
2007-01-11 22:20:23 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* What? UNIX socket? IPX?? :) */
|
|
|
|
}
|
2007-01-10 14:58:01 +05:30
|
|
|
|
2007-02-04 08:09:08 +05:30
|
|
|
/* 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 15:06:41 +05:30
|
|
|
/* host: "1.2.3.4[:port]", "www.google.com[:port]"
|
2007-02-04 08:09:08 +05:30
|
|
|
* port: if neither of above specifies port # */
|
2007-01-23 04:15:27 +05:30
|
|
|
static len_and_sockaddr* str2sockaddr(
|
|
|
|
const char *host, int port,
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_FEATURE_IPV6(sa_family_t af,)
|
2007-01-23 04:15:27 +05:30
|
|
|
int ai_flags)
|
2007-01-10 14:58:01 +05:30
|
|
|
{
|
2009-11-24 11:37:42 +05:30
|
|
|
IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
|
2007-01-10 14:58:01 +05:30
|
|
|
int rc;
|
2009-04-25 18:46:53 +05:30
|
|
|
len_and_sockaddr *r;
|
2007-01-10 14:58:01 +05:30
|
|
|
struct addrinfo *result = NULL;
|
2007-11-14 15:48:33 +05:30
|
|
|
struct addrinfo *used_res;
|
2007-01-10 14:58:01 +05:30
|
|
|
const char *org_host = host; /* only for error msg */
|
|
|
|
const char *cp;
|
|
|
|
struct addrinfo hint;
|
|
|
|
|
2009-04-25 18:46:53 +05:30
|
|
|
if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
|
|
|
|
struct sockaddr_un *sun;
|
|
|
|
|
|
|
|
r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
|
|
|
|
r->len = sizeof(struct sockaddr_un);
|
|
|
|
r->u.sa.sa_family = AF_UNIX;
|
|
|
|
sun = (struct sockaddr_un *)&r->u.sa;
|
|
|
|
safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = NULL;
|
|
|
|
|
2007-01-10 14:58:01 +05:30
|
|
|
/* Ugly parsing of host:addr */
|
|
|
|
if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
|
2008-03-13 03:44:34 +05:30
|
|
|
/* Even uglier parsing of [xx]:nn */
|
2007-01-10 14:58:01 +05:30
|
|
|
host++;
|
|
|
|
cp = strchr(host, ']');
|
2009-03-09 18:31:08 +05:30
|
|
|
if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
|
|
|
|
/* Malformed: must be [xx]:nn or [xx] */
|
2008-03-13 03:44:34 +05:30
|
|
|
bb_error_msg("bad address '%s'", org_host);
|
|
|
|
if (ai_flags & DIE_ON_ERROR)
|
|
|
|
xfunc_die();
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-01-10 14:58:01 +05:30
|
|
|
} 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 21:40:39 +05:30
|
|
|
}
|
2008-03-13 03:44:34 +05:30
|
|
|
if (cp) { /* points to ":" or "]:" */
|
2007-01-11 01:49:56 +05:30
|
|
|
int sz = cp - host + 1;
|
2009-04-25 18:46:53 +05:30
|
|
|
|
2007-01-11 01:49:56 +05:30
|
|
|
host = safe_strncpy(alloca(sz), host, sz);
|
2009-03-09 18:31:08 +05:30
|
|
|
if (ENABLE_FEATURE_IPV6 && *cp != ':') {
|
2007-01-10 14:58:01 +05:30
|
|
|
cp++; /* skip ']' */
|
2009-03-09 18:31:08 +05:30
|
|
|
if (*cp == '\0') /* [xx] without port */
|
|
|
|
goto skip;
|
|
|
|
}
|
2007-01-10 14:58:01 +05:30
|
|
|
cp++; /* skip ':' */
|
2008-03-13 03:44:34 +05:30
|
|
|
port = bb_strtou(cp, NULL, 10);
|
|
|
|
if (errno || (unsigned)port > 0xffff) {
|
|
|
|
bb_error_msg("bad port spec '%s'", org_host);
|
|
|
|
if (ai_flags & DIE_ON_ERROR)
|
|
|
|
xfunc_die();
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-03-09 18:31:08 +05:30
|
|
|
skip: ;
|
2007-01-10 14:58:01 +05:30
|
|
|
}
|
|
|
|
|
2009-11-24 11:37:42 +05:30
|
|
|
/* Next two if blocks allow to skip getaddrinfo()
|
2009-11-24 18:36:04 +05:30
|
|
|
* in case host name is a numeric IP(v6) address.
|
2009-11-24 11:37:42 +05:30
|
|
|
* getaddrinfo() initializes DNS resolution machinery,
|
|
|
|
* scans network config and such - tens of syscalls.
|
|
|
|
*/
|
|
|
|
/* If we were not asked specifically for IPv6,
|
|
|
|
* check whether this is a numeric IPv4 */
|
|
|
|
IF_FEATURE_IPV6(if(af != AF_INET6)) {
|
|
|
|
struct in_addr in4;
|
|
|
|
if (inet_aton(host, &in4) != 0) {
|
|
|
|
r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in));
|
|
|
|
r->len = sizeof(struct sockaddr_in);
|
|
|
|
r->u.sa.sa_family = AF_INET;
|
|
|
|
r->u.sin.sin_addr = in4;
|
|
|
|
goto set_port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
/* If we were not asked specifically for IPv4,
|
|
|
|
* check whether this is a numeric IPv6 */
|
|
|
|
if (af != AF_INET) {
|
|
|
|
struct in6_addr in6;
|
|
|
|
if (inet_pton(AF_INET6, host, &in6) > 0) {
|
|
|
|
r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6));
|
|
|
|
r->len = sizeof(struct sockaddr_in6);
|
|
|
|
r->u.sa.sa_family = AF_INET6;
|
|
|
|
r->u.sin6.sin6_addr = in6;
|
|
|
|
goto set_port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-01-10 14:58:01 +05:30
|
|
|
memset(&hint, 0 , sizeof(hint));
|
2007-01-23 04:15:27 +05:30
|
|
|
hint.ai_family = af;
|
2011-03-23 00:44:26 +05:30
|
|
|
/* Need SOCK_STREAM, or else we get each address thrice (or more)
|
2007-01-10 14:58:01 +05:30
|
|
|
* for each possible socket type (tcp,udp,raw...): */
|
|
|
|
hint.ai_socktype = SOCK_STREAM;
|
2007-02-04 08:09:08 +05:30
|
|
|
hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
|
2007-01-11 22:20:23 +05:30
|
|
|
rc = getaddrinfo(host, NULL, &hint, &result);
|
2007-02-04 08:09:08 +05:30
|
|
|
if (rc || !result) {
|
2007-02-04 08:09:55 +05:30
|
|
|
bb_error_msg("bad address '%s'", org_host);
|
2007-02-04 08:09:08 +05:30
|
|
|
if (ai_flags & DIE_ON_ERROR)
|
2007-04-11 03:08:30 +05:30
|
|
|
xfunc_die();
|
2007-02-04 08:09:08 +05:30
|
|
|
goto ret;
|
|
|
|
}
|
2007-11-14 15:48:33 +05:30
|
|
|
used_res = result;
|
|
|
|
#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
|
|
|
|
while (1) {
|
|
|
|
if (used_res->ai_family == AF_INET)
|
|
|
|
break;
|
|
|
|
used_res = used_res->ai_next;
|
|
|
|
if (!used_res) {
|
|
|
|
used_res = result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-11-24 11:37:42 +05:30
|
|
|
r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen);
|
2007-11-14 15:48:33 +05:30
|
|
|
r->len = used_res->ai_addrlen;
|
2008-01-29 16:03:34 +05:30
|
|
|
memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
|
2009-11-24 11:37:42 +05:30
|
|
|
|
2009-11-24 18:36:04 +05:30
|
|
|
set_port:
|
2011-04-07 21:22:20 +05:30
|
|
|
set_nport(&r->u.sa, htons(port));
|
2007-02-04 08:09:08 +05:30
|
|
|
ret:
|
2011-03-23 00:44:26 +05:30
|
|
|
if (result)
|
|
|
|
freeaddrinfo(result);
|
2007-01-10 14:58:01 +05:30
|
|
|
return r;
|
|
|
|
}
|
2007-01-23 04:15:27 +05:30
|
|
|
#if !ENABLE_FEATURE_IPV6
|
|
|
|
#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
2008-06-27 08:22:20 +05:30
|
|
|
len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
|
2007-02-04 08:11:57 +05:30
|
|
|
{
|
|
|
|
return str2sockaddr(host, port, af, 0);
|
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
|
2007-01-23 04:15:27 +05:30
|
|
|
{
|
2007-02-04 08:09:08 +05:30
|
|
|
return str2sockaddr(host, port, af, DIE_ON_ERROR);
|
2007-01-23 04:15:27 +05:30
|
|
|
}
|
|
|
|
#endif
|
2007-01-10 14:58:01 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
|
2007-02-04 08:09:55 +05:30
|
|
|
{
|
|
|
|
return str2sockaddr(host, port, AF_UNSPEC, 0);
|
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
|
2007-01-10 14:58:01 +05:30
|
|
|
{
|
2007-02-04 08:09:08 +05:30
|
|
|
return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
|
2007-01-10 14:58:01 +05:30
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
|
2007-01-10 14:58:01 +05:30
|
|
|
{
|
2007-02-04 08:09:08 +05:30
|
|
|
return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
|
2006-11-22 21:40:39 +05:30
|
|
|
}
|
|
|
|
|
2012-02-09 19:44:33 +05:30
|
|
|
int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, int family, int sock_type)
|
2006-11-22 21:40:39 +05:30
|
|
|
{
|
2007-01-13 02:29:31 +05:30
|
|
|
len_and_sockaddr *lsa;
|
2006-11-22 21:40:39 +05:30
|
|
|
int fd;
|
2007-06-06 01:38:11 +05:30
|
|
|
int len;
|
2007-01-13 02:29:31 +05:30
|
|
|
|
2007-06-06 01:38:11 +05:30
|
|
|
if (family == AF_UNSPEC) {
|
2012-02-09 19:44:33 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2007-06-06 01:38:11 +05:30
|
|
|
fd = socket(AF_INET6, sock_type, 0);
|
|
|
|
if (fd >= 0) {
|
|
|
|
family = AF_INET6;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-02-09 19:44:33 +05:30
|
|
|
#endif
|
2007-06-06 01:38:11 +05:30
|
|
|
family = AF_INET;
|
|
|
|
}
|
2012-02-09 19:44:33 +05:30
|
|
|
|
2007-06-06 01:38:11 +05:30
|
|
|
fd = xsocket(family, sock_type, 0);
|
2012-02-09 19:44:33 +05:30
|
|
|
|
2007-06-06 01:38:11 +05:30
|
|
|
len = sizeof(struct sockaddr_in);
|
2012-02-09 19:44:33 +05:30
|
|
|
if (family == AF_UNIX)
|
|
|
|
len = sizeof(struct sockaddr_un);
|
2007-06-06 01:38:11 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
if (family == AF_INET6) {
|
|
|
|
done:
|
|
|
|
len = sizeof(struct sockaddr_in6);
|
2007-01-13 02:29:31 +05:30
|
|
|
}
|
2007-06-06 01:38:11 +05:30
|
|
|
#endif
|
2009-11-24 20:33:47 +05:30
|
|
|
lsa = xzalloc(LSA_LEN_SIZE + len);
|
2007-01-13 02:29:31 +05:30
|
|
|
lsa->len = len;
|
2008-01-29 16:03:34 +05:30
|
|
|
lsa->u.sa.sa_family = family;
|
2007-01-13 02:29:31 +05:30
|
|
|
*lsap = lsa;
|
2006-11-22 21:40:39 +05:30
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
|
2007-04-02 18:07:28 +05:30
|
|
|
{
|
2012-02-09 19:44:33 +05:30
|
|
|
return xsocket_type(lsap, AF_UNSPEC, SOCK_STREAM);
|
2007-04-02 18:07:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
|
2006-11-22 21:40:39 +05:30
|
|
|
{
|
|
|
|
int fd;
|
2007-01-10 14:58:01 +05:30
|
|
|
len_and_sockaddr *lsa;
|
2006-11-22 21:40:39 +05:30
|
|
|
|
2007-01-11 01:49:56 +05:30
|
|
|
if (bindaddr && bindaddr[0]) {
|
2007-02-04 08:09:08 +05:30
|
|
|
lsa = xdotted2sockaddr(bindaddr, port);
|
2006-11-22 21:40:39 +05:30
|
|
|
/* user specified bind addr dictates family */
|
2008-01-29 16:03:34 +05:30
|
|
|
fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
|
2007-01-10 14:58:01 +05:30
|
|
|
} else {
|
2012-02-09 19:44:33 +05:30
|
|
|
fd = xsocket_type(&lsa, AF_UNSPEC, sock_type);
|
2011-04-07 21:22:20 +05:30
|
|
|
set_nport(&lsa->u.sa, htons(port));
|
2007-01-10 14:58:01 +05:30
|
|
|
}
|
2006-11-23 04:52:06 +05:30
|
|
|
setsockopt_reuseaddr(fd);
|
2008-01-29 16:03:34 +05:30
|
|
|
xbind(fd, &lsa->u.sa, lsa->len);
|
2007-01-10 14:58:01 +05:30
|
|
|
free(lsa);
|
|
|
|
return fd;
|
|
|
|
}
|
2006-11-22 21:40:39 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
|
2007-04-02 18:07:28 +05:30
|
|
|
{
|
|
|
|
return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
|
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
|
2007-04-02 18:07:28 +05:30
|
|
|
{
|
|
|
|
return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
|
2007-01-10 14:58:01 +05:30
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
len_and_sockaddr *lsa;
|
2006-11-22 21:40:39 +05:30
|
|
|
|
2007-02-04 08:09:08 +05:30
|
|
|
lsa = xhost2sockaddr(peer, port);
|
2008-01-29 16:03:34 +05:30
|
|
|
fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
|
2007-01-10 14:58:01 +05:30
|
|
|
setsockopt_reuseaddr(fd);
|
2008-01-29 16:03:34 +05:30
|
|
|
xconnect(fd, &lsa->u.sa, lsa->len);
|
2007-01-10 14:58:01 +05:30
|
|
|
free(lsa);
|
2006-11-22 21:40:39 +05:30
|
|
|
return fd;
|
|
|
|
}
|
2007-01-11 22:20:23 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
|
2007-01-11 22:20:23 +05:30
|
|
|
{
|
2008-01-29 16:03:34 +05:30
|
|
|
int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
|
|
|
|
xconnect(fd, &lsa->u.sa, lsa->len);
|
2007-01-11 22:20:23 +05:30
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2007-01-23 04:13:05 +05:30
|
|
|
/* We hijack this constant to mean something else */
|
|
|
|
/* It doesn't hurt because we will add this bit anyway */
|
|
|
|
#define IGNORE_PORT NI_NUMERICSERV
|
2008-06-27 08:22:20 +05:30
|
|
|
static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
|
2007-01-11 22:20:23 +05:30
|
|
|
{
|
|
|
|
char host[128];
|
|
|
|
char serv[16];
|
2007-08-18 19:46:39 +05:30
|
|
|
int rc;
|
|
|
|
socklen_t salen;
|
|
|
|
|
2009-04-25 18:46:53 +05:30
|
|
|
if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
|
|
|
|
struct sockaddr_un *sun = (struct sockaddr_un *)sa;
|
|
|
|
return xasprintf("local:%.*s",
|
|
|
|
(int) sizeof(sun->sun_path),
|
|
|
|
sun->sun_path);
|
|
|
|
}
|
|
|
|
|
2007-08-18 19:46:39 +05:30
|
|
|
salen = LSA_SIZEOF_SA;
|
|
|
|
#if ENABLE_FEATURE_IPV6
|
|
|
|
if (sa->sa_family == AF_INET)
|
|
|
|
salen = sizeof(struct sockaddr_in);
|
|
|
|
if (sa->sa_family == AF_INET6)
|
|
|
|
salen = sizeof(struct sockaddr_in6);
|
|
|
|
#endif
|
|
|
|
rc = getnameinfo(sa, salen,
|
2007-01-11 22:20:23 +05:30
|
|
|
host, sizeof(host),
|
2007-01-23 04:13:05 +05:30
|
|
|
/* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
|
2007-01-11 22:20:23 +05:30
|
|
|
serv, sizeof(serv),
|
2007-01-22 15:06:41 +05:30
|
|
|
/* do not resolve port# into service _name_ */
|
|
|
|
flags | NI_NUMERICSERV
|
2007-01-11 22:20:23 +05:30
|
|
|
);
|
2007-01-22 15:06:41 +05:30
|
|
|
if (rc)
|
|
|
|
return NULL;
|
2007-01-23 04:13:05 +05:30
|
|
|
if (flags & IGNORE_PORT)
|
|
|
|
return xstrdup(host);
|
2007-01-22 15:06:41 +05:30
|
|
|
#if ENABLE_FEATURE_IPV6
|
2007-01-23 04:13:05 +05:30
|
|
|
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 15:06:41 +05:30
|
|
|
#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-23 04:13:05 +05:30
|
|
|
/*return xstrdup(host);*/
|
2007-01-11 22:20:23 +05:30
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
|
2007-01-11 22:20:23 +05:30
|
|
|
{
|
2007-08-18 19:46:39 +05:30
|
|
|
return sockaddr2str(sa, 0);
|
2007-01-11 22:20:23 +05:30
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
|
2007-01-23 04:15:27 +05:30
|
|
|
{
|
2007-08-18 19:46:39 +05:30
|
|
|
return sockaddr2str(sa, IGNORE_PORT);
|
2007-01-23 04:15:27 +05:30
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
|
2007-01-23 04:13:05 +05:30
|
|
|
{
|
2007-08-18 19:46:39 +05:30
|
|
|
return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
|
2007-01-23 04:13:05 +05:30
|
|
|
}
|
2008-06-27 08:22:20 +05:30
|
|
|
char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
|
2007-01-11 22:20:23 +05:30
|
|
|
{
|
2007-08-18 19:46:39 +05:30
|
|
|
return sockaddr2str(sa, NI_NUMERICHOST);
|
2007-01-11 22:20:23 +05:30
|
|
|
}
|
2007-01-23 04:13:05 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
|
2007-01-23 04:13:05 +05:30
|
|
|
{
|
2007-08-18 19:46:39 +05:30
|
|
|
return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
|
2007-01-23 04:13:05 +05:30
|
|
|
}
|