Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 5 | * Connect to host at port using address resolution from getaddrinfo |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 8 | */ |
Dan Fandrich | fdd7b56 | 2010-06-18 22:37:42 -0700 | [diff] [blame] | 9 | #include <sys/types.h> |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 10 | #include <sys/socket.h> /* netinet/in.h needs it */ |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 11 | #include <netinet/in.h> |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 12 | #include <net/if.h> |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 13 | #include <sys/un.h> |
Denys Vlasenko | 45e3967 | 2019-06-03 14:16:52 +0200 | [diff] [blame] | 14 | #if ENABLE_IFPLUGD || ENABLE_FEATURE_MDEV_DAEMON || ENABLE_UEVENT |
| 15 | # include <linux/netlink.h> |
| 16 | #endif |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
| 18 | |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 19 | int FAST_FUNC setsockopt_int(int fd, int level, int optname, int optval) |
| 20 | { |
| 21 | return setsockopt(fd, level, optname, &optval, sizeof(int)); |
| 22 | } |
| 23 | int FAST_FUNC setsockopt_1(int fd, int level, int optname) |
| 24 | { |
| 25 | return setsockopt_int(fd, level, optname, 1); |
| 26 | } |
| 27 | int FAST_FUNC setsockopt_SOL_SOCKET_int(int fd, int optname, int optval) |
| 28 | { |
| 29 | return setsockopt_int(fd, SOL_SOCKET, optname, optval); |
| 30 | } |
| 31 | int FAST_FUNC setsockopt_SOL_SOCKET_1(int fd, int optname) |
| 32 | { |
| 33 | return setsockopt_SOL_SOCKET_int(fd, optname, 1); |
| 34 | } |
| 35 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 36 | void FAST_FUNC setsockopt_reuseaddr(int fd) |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 37 | { |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 38 | setsockopt_SOL_SOCKET_1(fd, SO_REUSEADDR); |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 39 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 40 | int FAST_FUNC setsockopt_broadcast(int fd) |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 41 | { |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 42 | return setsockopt_SOL_SOCKET_1(fd, SO_BROADCAST); |
| 43 | } |
| 44 | int FAST_FUNC setsockopt_keepalive(int fd) |
| 45 | { |
| 46 | return setsockopt_SOL_SOCKET_1(fd, SO_KEEPALIVE); |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 47 | } |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 48 | |
| 49 | #ifdef SO_BINDTODEVICE |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 50 | int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface) |
| 51 | { |
| 52 | int r; |
| 53 | struct ifreq ifr; |
| 54 | strncpy_IFNAMSIZ(ifr.ifr_name, iface); |
Denis Vlasenko | a3ca7cf | 2008-12-10 11:24:33 +0000 | [diff] [blame] | 55 | /* NB: passing (iface, strlen(iface) + 1) does not work! |
| 56 | * (maybe it works on _some_ kernels, but not on 2.6.26) |
| 57 | * Actually, ifr_name is at offset 0, and in practice |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 58 | * just giving char[IFNAMSIZ] instead of struct ifreq works too. |
| 59 | * But just in case it's not true on some obscure arch... */ |
| 60 | r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)); |
| 61 | if (r) |
| 62 | bb_perror_msg("can't bind to interface %s", iface); |
| 63 | return r; |
| 64 | } |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 65 | #else |
| 66 | int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM, |
| 67 | const char *iface UNUSED_PARAM) |
| 68 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 69 | bb_simple_error_msg("SO_BINDTODEVICE is not supported on this system"); |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 70 | return -1; |
| 71 | } |
| 72 | #endif |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 73 | |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 74 | static len_and_sockaddr* get_lsa(int fd, int (*get_name)(int fd, struct sockaddr *addr, socklen_t *addrlen)) |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 75 | { |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 76 | len_and_sockaddr lsa; |
| 77 | len_and_sockaddr *lsa_ptr; |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 79 | lsa.len = LSA_SIZEOF_SA; |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 80 | if (get_name(fd, &lsa.u.sa, &lsa.len) != 0) |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 81 | return NULL; |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 82 | |
| 83 | lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len); |
| 84 | if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */ |
| 85 | lsa_ptr->len = lsa.len; |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 86 | get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len); |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 87 | } else { |
| 88 | memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len); |
| 89 | } |
| 90 | return lsa_ptr; |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 91 | } |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 92 | |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 93 | len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd) |
| 94 | { |
| 95 | return get_lsa(fd, getsockname); |
| 96 | } |
| 97 | |
| 98 | len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd) |
| 99 | { |
| 100 | return get_lsa(fd, getpeername); |
| 101 | } |
| 102 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 103 | void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 104 | { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 105 | if (connect(s, s_addr, addrlen) < 0) { |
| 106 | if (ENABLE_FEATURE_CLEAN_UP) |
| 107 | close(s); |
| 108 | if (s_addr->sa_family == AF_INET) |
| 109 | bb_perror_msg_and_die("%s (%s)", |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 110 | "can't connect to remote host", |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 111 | inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr)); |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 112 | bb_simple_perror_msg_and_die("can't connect to remote host"); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 113 | } |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 116 | /* Return port number for a service. |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 117 | * If "port" is a number use it as the port. |
Denys Vlasenko | 7ab9cd2 | 2021-09-09 22:00:44 +0200 | [diff] [blame] | 118 | * If "port" is a name it is looked up in /etc/services. |
| 119 | * if NULL, return default_port |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 120 | */ |
Denys Vlasenko | 7ab9cd2 | 2021-09-09 22:00:44 +0200 | [diff] [blame] | 121 | unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned port_nr) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 122 | { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 123 | if (port) { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 124 | port_nr = bb_strtou(port, NULL, 10); |
| 125 | if (errno || port_nr > 65535) { |
| 126 | struct servent *tserv = getservbyname(port, protocol); |
Denys Vlasenko | 7ab9cd2 | 2021-09-09 22:00:44 +0200 | [diff] [blame] | 127 | if (!tserv) |
| 128 | bb_error_msg_and_die("bad port '%s'", port); |
| 129 | port_nr = ntohs(tserv->s_port); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 130 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 131 | } |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 132 | return (uint16_t)port_nr; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 136 | /* "New" networking API */ |
| 137 | |
| 138 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 139 | int FAST_FUNC get_nport(const struct sockaddr *sa) |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 140 | { |
| 141 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 142 | if (sa->sa_family == AF_INET6) { |
| 143 | return ((struct sockaddr_in6*)sa)->sin6_port; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 144 | } |
| 145 | #endif |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 146 | if (sa->sa_family == AF_INET) { |
| 147 | return ((struct sockaddr_in*)sa)->sin_port; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 148 | } |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 149 | /* What? UNIX socket? IPX?? :) */ |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 150 | return -1; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Denys Vlasenko | ca18311 | 2011-04-07 17:52:20 +0200 | [diff] [blame] | 153 | void FAST_FUNC set_nport(struct sockaddr *sa, unsigned port) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 154 | { |
| 155 | #if ENABLE_FEATURE_IPV6 |
Denys Vlasenko | ca18311 | 2011-04-07 17:52:20 +0200 | [diff] [blame] | 156 | if (sa->sa_family == AF_INET6) { |
| 157 | struct sockaddr_in6 *sin6 = (void*) sa; |
| 158 | sin6->sin6_port = port; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | #endif |
Denys Vlasenko | ca18311 | 2011-04-07 17:52:20 +0200 | [diff] [blame] | 162 | if (sa->sa_family == AF_INET) { |
| 163 | struct sockaddr_in *sin = (void*) sa; |
| 164 | sin->sin_port = port; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | /* What? UNIX socket? IPX?? :) */ |
| 168 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 169 | |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 170 | /* We hijack this constant to mean something else */ |
| 171 | /* It doesn't hurt because we will remove this bit anyway */ |
| 172 | #define DIE_ON_ERROR AI_CANONNAME |
| 173 | |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 174 | /* host: "1.2.3.4[:port]", "www.google.com[:port]" |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 175 | * port: if neither of above specifies port # */ |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 176 | static len_and_sockaddr* str2sockaddr( |
| 177 | const char *host, int port, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 178 | IF_FEATURE_IPV6(sa_family_t af,) |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 179 | int ai_flags) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 180 | { |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 181 | IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 182 | int rc; |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 183 | len_and_sockaddr *r; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 184 | struct addrinfo *result = NULL; |
Denis Vlasenko | 9d6c469 | 2007-11-14 10:18:33 +0000 | [diff] [blame] | 185 | struct addrinfo *used_res; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 186 | const char *org_host = host; /* only for error msg */ |
| 187 | const char *cp; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 188 | struct addrinfo hint; |
| 189 | |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 190 | if (ENABLE_FEATURE_UNIX_LOCAL && is_prefixed_with(host, "local:")) { |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 191 | struct sockaddr_un *sun; |
| 192 | |
| 193 | r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un)); |
| 194 | r->len = sizeof(struct sockaddr_un); |
| 195 | r->u.sa.sa_family = AF_UNIX; |
| 196 | sun = (struct sockaddr_un *)&r->u.sa; |
| 197 | safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path)); |
| 198 | return r; |
| 199 | } |
| 200 | |
| 201 | r = NULL; |
| 202 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 203 | /* Ugly parsing of host:addr */ |
| 204 | if (ENABLE_FEATURE_IPV6 && host[0] == '[') { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 205 | /* Even uglier parsing of [xx]:nn */ |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 206 | host++; |
| 207 | cp = strchr(host, ']'); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 208 | if (!cp || (cp[1] != ':' && cp[1] != '\0')) { |
| 209 | /* Malformed: must be [xx]:nn or [xx] */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 210 | bb_error_msg("bad address '%s'", org_host); |
| 211 | if (ai_flags & DIE_ON_ERROR) |
| 212 | xfunc_die(); |
| 213 | return NULL; |
| 214 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 215 | } else { |
| 216 | cp = strrchr(host, ':'); |
| 217 | if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) { |
| 218 | /* There is more than one ':' (e.g. "::1") */ |
| 219 | cp = NULL; /* it's not a port spec */ |
| 220 | } |
| 221 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 222 | if (cp) { /* points to ":" or "]:" */ |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 223 | int sz = cp - host + 1; |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 224 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 225 | host = safe_strncpy(alloca(sz), host, sz); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 226 | if (ENABLE_FEATURE_IPV6 && *cp != ':') { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 227 | cp++; /* skip ']' */ |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 228 | if (*cp == '\0') /* [xx] without port */ |
| 229 | goto skip; |
| 230 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 231 | cp++; /* skip ':' */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 232 | port = bb_strtou(cp, NULL, 10); |
| 233 | if (errno || (unsigned)port > 0xffff) { |
Denys Vlasenko | c00bcf2 | 2021-09-10 09:48:55 +0200 | [diff] [blame] | 234 | bb_error_msg("bad port '%s'", cp); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 235 | if (ai_flags & DIE_ON_ERROR) |
| 236 | xfunc_die(); |
| 237 | return NULL; |
| 238 | } |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 239 | skip: ; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 242 | /* Next two if blocks allow to skip getaddrinfo() |
Denys Vlasenko | 75b7397 | 2009-11-24 14:06:04 +0100 | [diff] [blame] | 243 | * in case host name is a numeric IP(v6) address. |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 244 | * getaddrinfo() initializes DNS resolution machinery, |
| 245 | * scans network config and such - tens of syscalls. |
| 246 | */ |
| 247 | /* If we were not asked specifically for IPv6, |
| 248 | * check whether this is a numeric IPv4 */ |
| 249 | IF_FEATURE_IPV6(if(af != AF_INET6)) { |
| 250 | struct in_addr in4; |
| 251 | if (inet_aton(host, &in4) != 0) { |
| 252 | r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in)); |
| 253 | r->len = sizeof(struct sockaddr_in); |
| 254 | r->u.sa.sa_family = AF_INET; |
| 255 | r->u.sin.sin_addr = in4; |
| 256 | goto set_port; |
| 257 | } |
| 258 | } |
| 259 | #if ENABLE_FEATURE_IPV6 |
| 260 | /* If we were not asked specifically for IPv4, |
| 261 | * check whether this is a numeric IPv6 */ |
| 262 | if (af != AF_INET) { |
| 263 | struct in6_addr in6; |
| 264 | if (inet_pton(AF_INET6, host, &in6) > 0) { |
| 265 | r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6)); |
| 266 | r->len = sizeof(struct sockaddr_in6); |
| 267 | r->u.sa.sa_family = AF_INET6; |
| 268 | r->u.sin6.sin6_addr = in6; |
| 269 | goto set_port; |
| 270 | } |
| 271 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 272 | #endif |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 273 | |
| 274 | memset(&hint, 0 , sizeof(hint)); |
| 275 | hint.ai_family = af; |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 276 | /* Need SOCK_STREAM, or else we get each address thrice (or more) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 277 | * for each possible socket type (tcp,udp,raw...): */ |
| 278 | hint.ai_socktype = SOCK_STREAM; |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 279 | hint.ai_flags = ai_flags & ~DIE_ON_ERROR; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 280 | rc = getaddrinfo(host, NULL, &hint, &result); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 281 | if (rc || !result) { |
Denis Vlasenko | 5870ad9 | 2007-02-04 02:39:55 +0000 | [diff] [blame] | 282 | bb_error_msg("bad address '%s'", org_host); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 283 | if (ai_flags & DIE_ON_ERROR) |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 284 | xfunc_die(); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 285 | goto ret; |
| 286 | } |
Denis Vlasenko | 9d6c469 | 2007-11-14 10:18:33 +0000 | [diff] [blame] | 287 | used_res = result; |
| 288 | #if ENABLE_FEATURE_PREFER_IPV4_ADDRESS |
| 289 | while (1) { |
| 290 | if (used_res->ai_family == AF_INET) |
| 291 | break; |
| 292 | used_res = used_res->ai_next; |
| 293 | if (!used_res) { |
| 294 | used_res = result; |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | #endif |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 299 | r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen); |
Denis Vlasenko | 9d6c469 | 2007-11-14 10:18:33 +0000 | [diff] [blame] | 300 | r->len = used_res->ai_addrlen; |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 301 | memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen); |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 302 | |
Denys Vlasenko | 75b7397 | 2009-11-24 14:06:04 +0100 | [diff] [blame] | 303 | set_port: |
Denys Vlasenko | ca18311 | 2011-04-07 17:52:20 +0200 | [diff] [blame] | 304 | set_nport(&r->u.sa, htons(port)); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 305 | ret: |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 306 | if (result) |
| 307 | freeaddrinfo(result); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 308 | return r; |
| 309 | } |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 310 | #if !ENABLE_FEATURE_IPV6 |
| 311 | #define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags) |
| 312 | #endif |
| 313 | |
| 314 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 315 | len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af) |
Denis Vlasenko | e991303 | 2007-02-04 02:41:57 +0000 | [diff] [blame] | 316 | { |
| 317 | return str2sockaddr(host, port, af, 0); |
| 318 | } |
| 319 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 320 | len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af) |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 321 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 322 | return str2sockaddr(host, port, af, DIE_ON_ERROR); |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 323 | } |
| 324 | #endif |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 325 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 326 | len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port) |
Denis Vlasenko | 5870ad9 | 2007-02-04 02:39:55 +0000 | [diff] [blame] | 327 | { |
| 328 | return str2sockaddr(host, port, AF_UNSPEC, 0); |
| 329 | } |
| 330 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 331 | len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 332 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 333 | return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 336 | len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 337 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 338 | return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Jonh Wendell | 9106107 | 2012-02-09 15:14:33 +0100 | [diff] [blame] | 341 | int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, int family, int sock_type) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 342 | { |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 343 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 344 | int fd; |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 345 | int len; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 346 | |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 347 | if (family == AF_UNSPEC) { |
Jonh Wendell | 9106107 | 2012-02-09 15:14:33 +0100 | [diff] [blame] | 348 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 349 | fd = socket(AF_INET6, sock_type, 0); |
| 350 | if (fd >= 0) { |
| 351 | family = AF_INET6; |
| 352 | goto done; |
| 353 | } |
Jonh Wendell | 9106107 | 2012-02-09 15:14:33 +0100 | [diff] [blame] | 354 | #endif |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 355 | family = AF_INET; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 356 | } |
Jonh Wendell | 9106107 | 2012-02-09 15:14:33 +0100 | [diff] [blame] | 357 | |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 358 | fd = xsocket(family, sock_type, 0); |
Jonh Wendell | 9106107 | 2012-02-09 15:14:33 +0100 | [diff] [blame] | 359 | |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 360 | len = sizeof(struct sockaddr_in); |
Jonh Wendell | 9106107 | 2012-02-09 15:14:33 +0100 | [diff] [blame] | 361 | if (family == AF_UNIX) |
| 362 | len = sizeof(struct sockaddr_un); |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 363 | #if ENABLE_FEATURE_IPV6 |
| 364 | if (family == AF_INET6) { |
| 365 | done: |
| 366 | len = sizeof(struct sockaddr_in6); |
| 367 | } |
| 368 | #endif |
Denys Vlasenko | b1278a3 | 2009-11-24 16:03:47 +0100 | [diff] [blame] | 369 | lsa = xzalloc(LSA_LEN_SIZE + len); |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 370 | lsa->len = len; |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 371 | lsa->u.sa.sa_family = family; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 372 | *lsap = lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 373 | return fd; |
| 374 | } |
| 375 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 376 | int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap) |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 377 | { |
Jonh Wendell | 9106107 | 2012-02-09 15:14:33 +0100 | [diff] [blame] | 378 | return xsocket_type(lsap, AF_UNSPEC, SOCK_STREAM); |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 382 | { |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 383 | int fd; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 384 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 385 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 386 | if (bindaddr && bindaddr[0]) { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 387 | lsa = xdotted2sockaddr(bindaddr, port); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 388 | /* user specified bind addr dictates family */ |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 389 | fd = xsocket(lsa->u.sa.sa_family, sock_type, 0); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 390 | } else { |
Jonh Wendell | 9106107 | 2012-02-09 15:14:33 +0100 | [diff] [blame] | 391 | fd = xsocket_type(&lsa, AF_UNSPEC, sock_type); |
Denys Vlasenko | ca18311 | 2011-04-07 17:52:20 +0200 | [diff] [blame] | 392 | set_nport(&lsa->u.sa, htons(port)); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 393 | } |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 394 | setsockopt_reuseaddr(fd); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 395 | xbind(fd, &lsa->u.sa, lsa->len); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 396 | free(lsa); |
| 397 | return fd; |
| 398 | } |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 399 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 400 | int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port) |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 401 | { |
| 402 | return create_and_bind_or_die(bindaddr, port, SOCK_STREAM); |
| 403 | } |
| 404 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 405 | int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port) |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 406 | { |
| 407 | return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM); |
| 408 | } |
| 409 | |
| 410 | |
Denys Vlasenko | 45e3967 | 2019-06-03 14:16:52 +0200 | [diff] [blame] | 411 | #if ENABLE_IFPLUGD || ENABLE_FEATURE_MDEV_DAEMON || ENABLE_UEVENT |
| 412 | int FAST_FUNC create_and_bind_to_netlink(int proto, int grp, unsigned rcvbuf) |
| 413 | { |
| 414 | struct sockaddr_nl sa; |
| 415 | int fd; |
| 416 | |
Denys Vlasenko | 45e3967 | 2019-06-03 14:16:52 +0200 | [diff] [blame] | 417 | fd = xsocket(AF_NETLINK, SOCK_DGRAM, proto); |
Denys Vlasenko | 45e3967 | 2019-06-03 14:16:52 +0200 | [diff] [blame] | 418 | |
Jan Klötzke | 12aa68d | 2019-12-16 22:56:49 +0100 | [diff] [blame] | 419 | /* Set receive buffer size before binding the socket |
| 420 | * We want to have enough space before we start receiving messages. |
| 421 | */ |
Denys Vlasenko | 45e3967 | 2019-06-03 14:16:52 +0200 | [diff] [blame] | 422 | if (rcvbuf != 0) { |
Jan Klötzke | 12aa68d | 2019-12-16 22:56:49 +0100 | [diff] [blame] | 423 | setsockopt_SOL_SOCKET_int(fd, SO_RCVBUF, rcvbuf); |
| 424 | /* SO_RCVBUFFORCE (root only) can go above net.core.rmem_max */ |
Denys Vlasenko | 45e3967 | 2019-06-03 14:16:52 +0200 | [diff] [blame] | 425 | setsockopt_SOL_SOCKET_int(fd, SO_RCVBUFFORCE, rcvbuf); |
| 426 | # if 0 |
| 427 | { |
| 428 | int z; |
| 429 | socklen_t zl = sizeof(z); |
| 430 | getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &z, &zl); |
| 431 | bb_error_msg("SO_RCVBUF:%d", z); |
| 432 | } |
| 433 | # endif |
| 434 | } |
| 435 | |
Jan Klötzke | 12aa68d | 2019-12-16 22:56:49 +0100 | [diff] [blame] | 436 | memset(&sa, 0, sizeof(sa)); |
| 437 | sa.nl_family = AF_NETLINK; |
| 438 | sa.nl_pid = getpid(); |
| 439 | sa.nl_groups = grp; |
| 440 | xbind(fd, (struct sockaddr *) &sa, sizeof(sa)); |
| 441 | close_on_exec_on(fd); |
| 442 | |
Denys Vlasenko | 45e3967 | 2019-06-03 14:16:52 +0200 | [diff] [blame] | 443 | return fd; |
| 444 | } |
| 445 | #endif |
| 446 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 447 | int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 448 | { |
| 449 | int fd; |
| 450 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 451 | |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 452 | lsa = xhost2sockaddr(peer, port); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 453 | fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 454 | setsockopt_reuseaddr(fd); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 455 | xconnect(fd, &lsa->u.sa, lsa->len); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 456 | free(lsa); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 457 | return fd; |
| 458 | } |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 459 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 460 | int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 461 | { |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 462 | int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0); |
| 463 | xconnect(fd, &lsa->u.sa, lsa->len); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 464 | return fd; |
| 465 | } |
| 466 | |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 467 | /* We hijack this constant to mean something else */ |
| 468 | /* It doesn't hurt because we will add this bit anyway */ |
| 469 | #define IGNORE_PORT NI_NUMERICSERV |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 470 | static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 471 | { |
| 472 | char host[128]; |
| 473 | char serv[16]; |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 474 | int rc; |
| 475 | socklen_t salen; |
| 476 | |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 477 | if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) { |
| 478 | struct sockaddr_un *sun = (struct sockaddr_un *)sa; |
| 479 | return xasprintf("local:%.*s", |
| 480 | (int) sizeof(sun->sun_path), |
| 481 | sun->sun_path); |
| 482 | } |
| 483 | |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 484 | salen = LSA_SIZEOF_SA; |
| 485 | #if ENABLE_FEATURE_IPV6 |
| 486 | if (sa->sa_family == AF_INET) |
| 487 | salen = sizeof(struct sockaddr_in); |
| 488 | if (sa->sa_family == AF_INET6) |
| 489 | salen = sizeof(struct sockaddr_in6); |
| 490 | #endif |
| 491 | rc = getnameinfo(sa, salen, |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 492 | host, sizeof(host), |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 493 | /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 494 | serv, sizeof(serv), |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 495 | /* do not resolve port# into service _name_ */ |
| 496 | flags | NI_NUMERICSERV |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 497 | ); |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 498 | if (rc) |
| 499 | return NULL; |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 500 | if (flags & IGNORE_PORT) |
| 501 | return xstrdup(host); |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 502 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 503 | if (sa->sa_family == AF_INET6) { |
| 504 | if (strchr(host, ':')) /* heh, it's not a resolved hostname */ |
| 505 | return xasprintf("[%s]:%s", host, serv); |
| 506 | /*return xasprintf("%s:%s", host, serv);*/ |
| 507 | /* - fall through instead */ |
| 508 | } |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 509 | #endif |
| 510 | /* For now we don't support anything else, so it has to be INET */ |
| 511 | /*if (sa->sa_family == AF_INET)*/ |
| 512 | return xasprintf("%s:%s", host, serv); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 513 | /*return xstrdup(host);*/ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 516 | char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 517 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 518 | return sockaddr2str(sa, 0); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 521 | char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa) |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 522 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 523 | return sockaddr2str(sa, IGNORE_PORT); |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 526 | char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa) |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 527 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 528 | return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 529 | } |
Denys Vlasenko | 3c18e30 | 2016-09-26 19:53:04 +0200 | [diff] [blame] | 530 | #ifndef NI_NUMERICSCOPE |
| 531 | # define NI_NUMERICSCOPE 0 |
| 532 | #endif |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 533 | char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 534 | { |
Denys Vlasenko | 3c18e30 | 2016-09-26 19:53:04 +0200 | [diff] [blame] | 535 | return sockaddr2str(sa, NI_NUMERICHOST | NI_NUMERICSCOPE); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 536 | } |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 537 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 538 | char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa) |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 539 | { |
Denys Vlasenko | 3c18e30 | 2016-09-26 19:53:04 +0200 | [diff] [blame] | 540 | return sockaddr2str(sa, NI_NUMERICHOST | NI_NUMERICSCOPE | IGNORE_PORT); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 541 | } |