Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 1 | /* |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 2 | * (C) 2011-2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 3 | * |
| 4 | * All Rights Reserved |
| 5 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 24 | #include "../config.h" |
| 25 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 26 | /*! \addtogroup socket |
| 27 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 28 | * Osmocom socket convenience functions. |
| 29 | * |
| 30 | * \file socket.c */ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 31 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 32 | #ifdef HAVE_SYS_SOCKET_H |
| 33 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 34 | #include <osmocom/core/logging.h> |
| 35 | #include <osmocom/core/select.h> |
| 36 | #include <osmocom/core/socket.h> |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 37 | #include <osmocom/core/talloc.h> |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 38 | #include <osmocom/core/utils.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 39 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 40 | #include <sys/ioctl.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 41 | #include <sys/socket.h> |
| 42 | #include <sys/types.h> |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 43 | #include <sys/un.h> |
Harald Welte | 44b9926 | 2020-03-07 14:59:05 +0100 | [diff] [blame] | 44 | #include <net/if.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 45 | |
Holger Hans Peter Freyther | 4772348 | 2011-11-09 11:26:15 +0100 | [diff] [blame] | 46 | #include <netinet/in.h> |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 47 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 4772348 | 2011-11-09 11:26:15 +0100 | [diff] [blame] | 48 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 49 | #include <stdio.h> |
| 50 | #include <unistd.h> |
| 51 | #include <stdint.h> |
| 52 | #include <string.h> |
| 53 | #include <errno.h> |
| 54 | #include <netdb.h> |
| 55 | #include <ifaddrs.h> |
| 56 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 57 | #ifdef HAVE_LIBSCTP |
| 58 | #include <netinet/sctp.h> |
| 59 | #endif |
| 60 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 61 | static struct addrinfo *addrinfo_helper(uint16_t family, uint16_t type, uint8_t proto, |
| 62 | const char *host, uint16_t port, bool passive) |
| 63 | { |
Pau Espin Pedrol | ff42852 | 2019-10-10 17:38:16 +0200 | [diff] [blame] | 64 | struct addrinfo hints, *result, *rp; |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 65 | char portbuf[6]; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 66 | int rc; |
| 67 | |
| 68 | snprintf(portbuf, sizeof(portbuf), "%u", port); |
| 69 | memset(&hints, 0, sizeof(struct addrinfo)); |
| 70 | hints.ai_family = family; |
| 71 | if (type == SOCK_RAW) { |
| 72 | /* Workaround for glibc, that returns EAI_SERVICE (-8) if |
| 73 | * SOCK_RAW and IPPROTO_GRE is used. |
Pau Espin Pedrol | ff42852 | 2019-10-10 17:38:16 +0200 | [diff] [blame] | 74 | * http://sourceware.org/bugzilla/show_bug.cgi?id=15015 |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 75 | */ |
| 76 | hints.ai_socktype = SOCK_DGRAM; |
| 77 | hints.ai_protocol = IPPROTO_UDP; |
| 78 | } else { |
| 79 | hints.ai_socktype = type; |
| 80 | hints.ai_protocol = proto; |
| 81 | } |
| 82 | |
| 83 | if (passive) |
| 84 | hints.ai_flags |= AI_PASSIVE; |
| 85 | |
| 86 | rc = getaddrinfo(host, portbuf, &hints, &result); |
| 87 | if (rc != 0) { |
| 88 | LOGP(DLGLOBAL, LOGL_ERROR, "getaddrinfo returned NULL: %s:%u: %s\n", |
| 89 | host, port, strerror(errno)); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Pau Espin Pedrol | ff42852 | 2019-10-10 17:38:16 +0200 | [diff] [blame] | 93 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
| 94 | /* Workaround for glibc again */ |
| 95 | if (type == SOCK_RAW) { |
| 96 | rp->ai_socktype = SOCK_RAW; |
| 97 | rp->ai_protocol = proto; |
| 98 | } |
| 99 | } |
| 100 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 101 | return result; |
| 102 | } |
| 103 | |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 104 | #ifdef HAVE_LIBSCTP |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 105 | /*! Retrieve an array of addrinfo with specified hints, one for each host in the hosts array. |
| 106 | * \param[out] addrinfo array of addrinfo pointers, will be filled by the function on success. |
| 107 | * Its size must be at least the one of hosts. |
| 108 | * \param[in] family Socket family like AF_INET, AF_INET6. |
| 109 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM. |
| 110 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP. |
| 111 | * \param[in] hosts array of char pointers (strings) containing the addresses to query. |
| 112 | * \param[in] host_cnt length of the hosts array (in items). |
| 113 | * \param[in] port port number in host byte order. |
| 114 | * \param[in] passive whether to include the AI_PASSIVE flag in getaddrinfo() hints. |
| 115 | * \returns 0 is returned on success together with a filled addrinfo array; negative on error |
| 116 | */ |
| 117 | static int addrinfo_helper_multi(struct addrinfo **addrinfo, uint16_t family, uint16_t type, uint8_t proto, |
| 118 | const char **hosts, size_t host_cnt, uint16_t port, bool passive) |
| 119 | { |
| 120 | int i, j; |
| 121 | |
| 122 | for (i = 0; i < host_cnt; i++) { |
| 123 | addrinfo[i] = addrinfo_helper(family, type, proto, hosts[i], port, passive); |
| 124 | if (!addrinfo[i]) { |
| 125 | for (j = 0; j < i; j++) |
| 126 | freeaddrinfo(addrinfo[j]); |
| 127 | return -EINVAL; |
| 128 | } |
| 129 | } |
| 130 | return 0; |
| 131 | } |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 132 | #endif /* HAVE_LIBSCTP*/ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 133 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 134 | static int socket_helper(const struct addrinfo *rp, unsigned int flags) |
| 135 | { |
| 136 | int sfd, on = 1; |
| 137 | |
| 138 | sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 139 | if (sfd == -1) { |
| 140 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 141 | "unable to create socket: %s\n", strerror(errno)); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 142 | return sfd; |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 143 | } |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 144 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 145 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
| 146 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 147 | "cannot set this socket unblocking: %s\n", |
| 148 | strerror(errno)); |
| 149 | close(sfd); |
| 150 | sfd = -EINVAL; |
| 151 | } |
| 152 | } |
| 153 | return sfd; |
| 154 | } |
| 155 | |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 156 | #ifdef HAVE_LIBSCTP |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 157 | /* Fill buf with a string representation of the address set, in the form: |
| 158 | * buf_len == 0: "()" |
| 159 | * buf_len == 1: "hostA" |
| 160 | * buf_len >= 2: (hostA|hostB|...|...) |
| 161 | */ |
| 162 | static int multiaddr_snprintf(char* buf, size_t buf_len, const char **hosts, size_t host_cnt) |
| 163 | { |
| 164 | int len = 0, offset = 0, rem = buf_len; |
| 165 | int ret, i; |
| 166 | char *after; |
| 167 | |
| 168 | if (buf_len < 3) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | if (host_cnt != 1) { |
| 172 | ret = snprintf(buf, rem, "("); |
| 173 | if (ret < 0) |
| 174 | return ret; |
| 175 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 176 | } |
| 177 | for (i = 0; i < host_cnt; i++) { |
| 178 | if (host_cnt == 1) |
| 179 | after = ""; |
| 180 | else |
| 181 | after = (i == (host_cnt - 1)) ? ")" : "|"; |
| 182 | ret = snprintf(buf + offset, rem, "%s%s", hosts[i] ? : "0.0.0.0", after); |
| 183 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 184 | } |
| 185 | |
| 186 | return len; |
| 187 | } |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 188 | #endif /* HAVE_LIBSCTP */ |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 189 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 190 | static int osmo_sock_init_tail(int fd, uint16_t type, unsigned int flags) |
| 191 | { |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 192 | int rc; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 193 | |
| 194 | /* Make sure to call 'listen' on a bound, connection-oriented sock */ |
| 195 | if ((flags & (OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT)) == OSMO_SOCK_F_BIND) { |
| 196 | switch (type) { |
| 197 | case SOCK_STREAM: |
| 198 | case SOCK_SEQPACKET: |
| 199 | rc = listen(fd, 10); |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 200 | if (rc < 0) { |
| 201 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to listen on socket: %s\n", |
| 202 | strerror(errno)); |
| 203 | return rc; |
| 204 | } |
| 205 | break; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 209 | if (flags & OSMO_SOCK_F_NO_MCAST_LOOP) { |
| 210 | rc = osmo_sock_mcast_loop_set(fd, false); |
| 211 | if (rc < 0) { |
| 212 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to disable multicast loop: %s\n", |
| 213 | strerror(errno)); |
| 214 | return rc; |
| 215 | } |
| 216 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 217 | |
Harald Welte | 37d204a | 2017-07-13 16:33:16 +0200 | [diff] [blame] | 218 | if (flags & OSMO_SOCK_F_NO_MCAST_ALL) { |
| 219 | rc = osmo_sock_mcast_all_set(fd, false); |
| 220 | if (rc < 0) { |
| 221 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to disable receive of all multicast: %s\n", |
| 222 | strerror(errno)); |
| 223 | /* do not abort here, as this is just an |
| 224 | * optional additional optimization that only |
| 225 | * exists on Linux only */ |
| 226 | } |
| 227 | } |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 228 | return 0; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 231 | /*! Initialize a socket (including bind and/or connect) |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 232 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 233 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 234 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 235 | * \param[in] local_host local host name or IP address in string form |
| 236 | * \param[in] local_port local port number in host byte order |
| 237 | * \param[in] remote_host remote host name or IP address in string form |
| 238 | * \param[in] remote_port remote port number in host byte order |
| 239 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 240 | * \returns socket file descriptor on success; negative on error |
| 241 | * |
| 242 | * This function creates a new socket of the designated \a family, \a |
| 243 | * type and \a proto and optionally binds it to the \a local_host and \a |
| 244 | * local_port as well as optionally connects it to the \a remote_host |
| 245 | * and \q remote_port, depending on the value * of \a flags parameter. |
| 246 | * |
| 247 | * As opposed to \ref osmo_sock_init(), this function allows to combine |
| 248 | * the \ref OSMO_SOCK_F_BIND and \ref OSMO_SOCK_F_CONNECT flags. This |
| 249 | * is useful if you want to connect to a remote host/port, but still |
| 250 | * want to bind that socket to either a specific local alias IP and/or a |
| 251 | * specific local source port. |
| 252 | * |
| 253 | * You must specify either \ref OSMO_SOCK_F_BIND, or \ref |
| 254 | * OSMO_SOCK_F_CONNECT, or both. |
| 255 | * |
| 256 | * If \ref OSMO_SOCK_F_NONBLOCK is specified, the socket will be set to |
| 257 | * non-blocking mode. |
| 258 | */ |
| 259 | int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto, |
| 260 | const char *local_host, uint16_t local_port, |
| 261 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 262 | { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 263 | struct addrinfo *local = NULL, *remote = NULL, *rp; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 264 | int sfd = -1, rc, on = 1; |
| 265 | |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 266 | bool local_ipv4 = false, local_ipv6 = false; |
| 267 | bool remote_ipv4 = false, remote_ipv6 = false; |
| 268 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 269 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 270 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 271 | "BIND or CONNECT flags\n"); |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 275 | /* figure out local address infos */ |
| 276 | if (flags & OSMO_SOCK_F_BIND) { |
| 277 | local = addrinfo_helper(family, type, proto, local_host, local_port, true); |
| 278 | if (!local) |
| 279 | return -EINVAL; |
| 280 | } |
| 281 | |
| 282 | /* figure out remote address infos */ |
| 283 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 284 | remote = addrinfo_helper(family, type, proto, remote_host, remote_port, false); |
| 285 | if (!remote) { |
| 286 | if (local) |
| 287 | freeaddrinfo(local); |
| 288 | |
| 289 | return -EINVAL; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /* It must do a full run to ensure AF_UNSPEC does not fail. |
| 294 | * In case first local valid entry is IPv4 and only remote valid entry |
| 295 | * is IPv6 or vice versa */ |
| 296 | if (family == AF_UNSPEC) { |
| 297 | for (rp = local; rp != NULL; rp = rp->ai_next) { |
| 298 | switch (rp->ai_family) { |
| 299 | case AF_INET: |
| 300 | local_ipv4 = true; |
| 301 | break; |
| 302 | case AF_INET6: |
| 303 | local_ipv6 = true; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | for (rp = remote; rp != NULL; rp = rp->ai_next) { |
| 309 | switch (rp->ai_family) { |
| 310 | case AF_INET: |
| 311 | remote_ipv4 = true; |
| 312 | break; |
| 313 | case AF_INET6: |
| 314 | remote_ipv6 = true; |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* priotize ipv6 as per RFC */ |
| 320 | if (local_ipv6 && remote_ipv6) |
| 321 | family = AF_INET6; |
| 322 | else if (local_ipv4 && remote_ipv4) |
| 323 | family = AF_INET; |
| 324 | else { |
| 325 | if (local) |
| 326 | freeaddrinfo(local); |
| 327 | if (remote) |
| 328 | freeaddrinfo(remote); |
| 329 | LOGP(DLGLOBAL, LOGL_ERROR, "Unable to find a common protocol (IPv4 or IPv6) for local host: %s and remote host: %s.\n", |
| 330 | local_host, remote_host); |
| 331 | return -ENODEV; |
| 332 | } |
| 333 | } |
| 334 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 335 | /* figure out local side of socket */ |
| 336 | if (flags & OSMO_SOCK_F_BIND) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 337 | for (rp = local; rp != NULL; rp = rp->ai_next) { |
| 338 | /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ |
| 339 | if (rp->ai_family != family) |
| 340 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 341 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 342 | sfd = socket_helper(rp, flags); |
| 343 | if (sfd < 0) |
| 344 | continue; |
| 345 | |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 346 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 347 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 348 | &on, sizeof(on)); |
| 349 | if (rc < 0) { |
| 350 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 351 | "cannot setsockopt socket:" |
| 352 | " %s:%u: %s\n", |
| 353 | local_host, local_port, |
| 354 | strerror(errno)); |
| 355 | close(sfd); |
| 356 | continue; |
| 357 | } |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 358 | } |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 359 | |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 360 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 361 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n", |
| 362 | local_host, local_port, strerror(errno)); |
| 363 | close(sfd); |
| 364 | continue; |
| 365 | } |
| 366 | break; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 367 | } |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 368 | |
| 369 | freeaddrinfo(local); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 370 | if (rp == NULL) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 371 | if (remote) |
| 372 | freeaddrinfo(remote); |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 373 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: %s:%u\n", |
| 374 | local_host, local_port); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 375 | return -ENODEV; |
| 376 | } |
| 377 | } |
| 378 | |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 379 | /* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it |
| 380 | was already closed and func returned. If OSMO_SOCK_F_BIND is not |
| 381 | set, then sfd = -1 */ |
| 382 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 383 | /* figure out remote side of socket */ |
| 384 | if (flags & OSMO_SOCK_F_CONNECT) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 385 | for (rp = remote; rp != NULL; rp = rp->ai_next) { |
| 386 | /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ |
| 387 | if (rp->ai_family != family) |
| 388 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 389 | |
Harald Welte | 5cfa6dc | 2017-07-21 16:52:29 +0200 | [diff] [blame] | 390 | if (sfd < 0) { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 391 | sfd = socket_helper(rp, flags); |
| 392 | if (sfd < 0) |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 397 | if (rc != 0 && errno != EINPROGRESS) { |
| 398 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 399 | remote_host, remote_port, strerror(errno)); |
| 400 | /* We want to maintain the bind socket if bind was enabled */ |
| 401 | if (!(flags & OSMO_SOCK_F_BIND)) { |
| 402 | close(sfd); |
| 403 | sfd = -1; |
| 404 | } |
| 405 | continue; |
| 406 | } |
| 407 | break; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 408 | } |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 409 | |
| 410 | freeaddrinfo(remote); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 411 | if (rp == NULL) { |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 412 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable remote addr found for: %s:%u\n", |
| 413 | remote_host, remote_port); |
| 414 | if (sfd >= 0) |
| 415 | close(sfd); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 416 | return -ENODEV; |
| 417 | } |
| 418 | } |
| 419 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 420 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 421 | if (rc < 0) { |
| 422 | close(sfd); |
| 423 | sfd = -1; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 424 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 425 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 426 | return sfd; |
| 427 | } |
| 428 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 429 | #ifdef HAVE_LIBSCTP |
| 430 | |
| 431 | |
| 432 | /* Build array of addresses taking first addrinfo result of the requested family |
| 433 | * for each host in hosts. addrs4 or addrs6 are filled based on family type. */ |
| 434 | static int addrinfo_to_sockaddr(uint16_t family, const struct addrinfo **result, |
| 435 | const char **hosts, int host_cont, |
| 436 | struct sockaddr_in *addrs4, struct sockaddr_in6 *addrs6) { |
| 437 | size_t host_idx; |
| 438 | const struct addrinfo *rp; |
| 439 | OSMO_ASSERT(family == AF_INET || family == AF_INET6); |
| 440 | |
| 441 | for (host_idx = 0; host_idx < host_cont; host_idx++) { |
| 442 | for (rp = result[host_idx]; rp != NULL; rp = rp->ai_next) { |
| 443 | if (rp->ai_family != family) |
| 444 | continue; |
| 445 | if (family == AF_INET) |
| 446 | memcpy(&addrs4[host_idx], rp->ai_addr, sizeof(addrs4[host_idx])); |
| 447 | else |
| 448 | memcpy(&addrs6[host_idx], rp->ai_addr, sizeof(addrs6[host_idx])); |
| 449 | break; |
| 450 | } |
| 451 | if (!rp) { /* No addr could be bound for this host! */ |
| 452 | LOGP(DLGLOBAL, LOGL_ERROR, "No suitable remote address found for host: %s\n", |
| 453 | hosts[host_idx]); |
| 454 | return -ENODEV; |
| 455 | } |
| 456 | } |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | /*! Initialize a socket (including bind and/or connect) with multiple local or remote addresses. |
| 461 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 462 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 463 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 464 | * \param[in] local_hosts array of char pointers (strings), each containing local host name or IP address in string form |
| 465 | * \param[in] local_hosts_cnt length of local_hosts (in items) |
| 466 | * \param[in] local_port local port number in host byte order |
| 467 | * \param[in] remote_host array of char pointers (strings), each containing remote host name or IP address in string form |
| 468 | * \param[in] remote_hosts_cnt length of remote_hosts (in items) |
| 469 | * \param[in] remote_port remote port number in host byte order |
| 470 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 471 | * \returns socket file descriptor on success; negative on error |
| 472 | * |
| 473 | * This function is similar to \ref osmo_sock_init2(), but can be passed an |
| 474 | * array of local or remote addresses for protocols supporting multiple |
| 475 | * addresses per socket, like SCTP (currently only one supported). This function |
| 476 | * should not be used by protocols not supporting this kind of features, but |
| 477 | * rather \ref osmo_sock_init2() should be used instead. |
| 478 | * See \ref osmo_sock_init2() for more information on flags and general behavior. |
| 479 | */ |
| 480 | int osmo_sock_init2_multiaddr(uint16_t family, uint16_t type, uint8_t proto, |
| 481 | const char **local_hosts, size_t local_hosts_cnt, uint16_t local_port, |
| 482 | const char **remote_hosts, size_t remote_hosts_cnt, uint16_t remote_port, |
| 483 | unsigned int flags) |
| 484 | |
| 485 | { |
| 486 | struct addrinfo *result[OSMO_SOCK_MAX_ADDRS]; |
| 487 | int sfd = -1, rc, on = 1; |
| 488 | int i; |
| 489 | struct sockaddr_in addrs4[OSMO_SOCK_MAX_ADDRS]; |
| 490 | struct sockaddr_in6 addrs6[OSMO_SOCK_MAX_ADDRS]; |
| 491 | struct sockaddr *addrs; |
| 492 | char strbuf[512]; |
| 493 | |
| 494 | /* TODO: So far this function is only aimed for SCTP, but could be |
| 495 | reused in the future for other protocols with multi-addr support */ |
| 496 | if (proto != IPPROTO_SCTP) |
| 497 | return -ENOTSUP; |
| 498 | |
| 499 | /* TODO: Let's not support AF_UNSPEC for now. sctp_bindx() actually |
| 500 | supports binding both types of addresses on a AF_INET6 soscket, but |
| 501 | that would mean we could get both AF_INET and AF_INET6 addresses for |
| 502 | each host, and makes complexity of this function increase a lot since |
| 503 | we'd need to find out which subsets to use, use v4v6 mapped socket, |
| 504 | etc. */ |
| 505 | if (family == AF_UNSPEC) |
| 506 | return -ENOTSUP; |
| 507 | |
| 508 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 509 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 510 | "BIND or CONNECT flags\n"); |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
| 514 | if (((flags & OSMO_SOCK_F_BIND) && !local_hosts_cnt) || |
| 515 | ((flags & OSMO_SOCK_F_CONNECT) && !remote_hosts_cnt) || |
| 516 | local_hosts_cnt > OSMO_SOCK_MAX_ADDRS || |
| 517 | remote_hosts_cnt > OSMO_SOCK_MAX_ADDRS) |
| 518 | return -EINVAL; |
| 519 | |
| 520 | /* figure out local side of socket */ |
| 521 | if (flags & OSMO_SOCK_F_BIND) { |
| 522 | rc = addrinfo_helper_multi(result, family, type, proto, local_hosts, |
| 523 | local_hosts_cnt, local_port, true); |
| 524 | if (rc < 0) |
| 525 | return -EINVAL; |
| 526 | |
| 527 | /* Since addrinfo_helper sets ai_family, socktype and |
| 528 | ai_protocol in hints, we know all results will use same |
| 529 | values, so simply pick the first one and pass it to create |
| 530 | the socket: |
| 531 | */ |
| 532 | sfd = socket_helper(result[0], flags); |
| 533 | if (sfd < 0) { |
| 534 | for (i = 0; i < local_hosts_cnt; i++) |
| 535 | freeaddrinfo(result[i]); |
| 536 | return sfd; |
| 537 | } |
| 538 | |
Pau Espin Pedrol | 272dfc1 | 2019-10-21 11:11:27 +0200 | [diff] [blame] | 539 | /* Since so far we only allow IPPROTO_SCTP in this function, |
| 540 | no need to check below for "proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR" */ |
| 541 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 542 | &on, sizeof(on)); |
| 543 | if (rc < 0) { |
| 544 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 545 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 546 | "cannot setsockopt socket:" |
| 547 | " %s:%u: %s\n", |
| 548 | strbuf, local_port, |
| 549 | strerror(errno)); |
| 550 | for (i = 0; i < local_hosts_cnt; i++) |
| 551 | freeaddrinfo(result[i]); |
| 552 | close(sfd); |
| 553 | return rc; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* Build array of addresses taking first of same family for each host. |
| 557 | TODO: Ideally we should use backtracking storing last used |
| 558 | indexes and trying next combination if connect() fails .*/ |
| 559 | rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)result, |
| 560 | local_hosts, local_hosts_cnt, addrs4, addrs6); |
| 561 | if (rc < 0) { |
| 562 | for (i = 0; i < local_hosts_cnt; i++) |
| 563 | freeaddrinfo(result[i]); |
| 564 | close(sfd); |
| 565 | return -ENODEV; |
| 566 | } |
| 567 | |
| 568 | if (family == AF_INET) |
| 569 | addrs = (struct sockaddr *)addrs4; |
| 570 | else |
| 571 | addrs = (struct sockaddr *)addrs6; |
| 572 | if (sctp_bindx(sfd, addrs, local_hosts_cnt, SCTP_BINDX_ADD_ADDR) == -1) { |
| 573 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 574 | LOGP(DLGLOBAL, LOGL_NOTICE, "unable to bind socket: %s:%u: %s\n", |
| 575 | strbuf, local_port, strerror(errno)); |
| 576 | for (i = 0; i < local_hosts_cnt; i++) |
| 577 | freeaddrinfo(result[i]); |
| 578 | close(sfd); |
| 579 | return -ENODEV; |
| 580 | } |
| 581 | for (i = 0; i < local_hosts_cnt; i++) |
| 582 | freeaddrinfo(result[i]); |
| 583 | } |
| 584 | |
| 585 | /* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it |
| 586 | was already closed and func returned. If OSMO_SOCK_F_BIND is not |
| 587 | set, then sfd = -1 */ |
| 588 | |
| 589 | /* figure out remote side of socket */ |
| 590 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 591 | rc = addrinfo_helper_multi(result, family, type, proto, remote_hosts, |
| 592 | remote_hosts_cnt, remote_port, false); |
| 593 | if (rc < 0) { |
| 594 | if (sfd >= 0) |
| 595 | close(sfd); |
| 596 | return -EINVAL; |
| 597 | } |
| 598 | |
| 599 | if (sfd < 0) { |
| 600 | /* Since addrinfo_helper sets ai_family, socktype and |
| 601 | ai_protocol in hints, we know all results will use same |
| 602 | values, so simply pick the first one and pass it to create |
| 603 | the socket: |
| 604 | */ |
| 605 | sfd = socket_helper(result[0], flags); |
| 606 | if (sfd < 0) { |
| 607 | for (i = 0; i < remote_hosts_cnt; i++) |
| 608 | freeaddrinfo(result[i]); |
| 609 | return sfd; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* Build array of addresses taking first of same family for each host. |
| 614 | TODO: Ideally we should use backtracking storing last used |
| 615 | indexes and trying next combination if connect() fails .*/ |
| 616 | rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)result, |
| 617 | remote_hosts, remote_hosts_cnt, addrs4, addrs6); |
| 618 | if (rc < 0) { |
| 619 | for (i = 0; i < remote_hosts_cnt; i++) |
| 620 | freeaddrinfo(result[i]); |
| 621 | close(sfd); |
| 622 | return -ENODEV; |
| 623 | } |
| 624 | |
| 625 | if (family == AF_INET) |
| 626 | addrs = (struct sockaddr *)addrs4; |
| 627 | else |
| 628 | addrs = (struct sockaddr *)addrs6; |
| 629 | rc = sctp_connectx(sfd, addrs, remote_hosts_cnt, NULL); |
| 630 | if (rc != 0 && errno != EINPROGRESS) { |
| 631 | multiaddr_snprintf(strbuf, sizeof(strbuf), remote_hosts, remote_hosts_cnt); |
| 632 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 633 | strbuf, remote_port, strerror(errno)); |
| 634 | for (i = 0; i < remote_hosts_cnt; i++) |
| 635 | freeaddrinfo(result[i]); |
| 636 | close(sfd); |
| 637 | return -ENODEV; |
| 638 | } |
| 639 | for (i = 0; i < remote_hosts_cnt; i++) |
| 640 | freeaddrinfo(result[i]); |
| 641 | } |
| 642 | |
| 643 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 644 | if (rc < 0) { |
| 645 | close(sfd); |
| 646 | sfd = -1; |
| 647 | } |
| 648 | |
| 649 | return sfd; |
| 650 | } |
| 651 | #endif /* HAVE_LIBSCTP */ |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 652 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 653 | /*! Initialize a socket (including bind/connect) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 654 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 655 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 656 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 657 | * \param[in] host remote host name or IP address in string form |
| 658 | * \param[in] port remote port number in host byte order |
| 659 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 660 | * \returns socket file descriptor on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 661 | * |
| 662 | * This function creates a new socket of the designated \a family, \a |
| 663 | * type and \a proto and optionally binds or connects it, depending on |
| 664 | * the value of \a flags parameter. |
| 665 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 666 | int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 667 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 668 | { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 669 | struct addrinfo *result, *rp; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 670 | int sfd, rc, on = 1; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 671 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 672 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 673 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 674 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: both bind and connect flags set:" |
Neels Hofmeyr | b7f191f | 2016-08-29 11:22:03 +0200 | [diff] [blame] | 675 | " %s:%u\n", host, port); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 676 | return -EINVAL; |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 677 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 678 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 679 | result = addrinfo_helper(family, type, proto, host, port, flags & OSMO_SOCK_F_BIND); |
| 680 | if (!result) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 681 | LOGP(DLGLOBAL, LOGL_ERROR, "getaddrinfo returned NULL: %s:%u: %s\n", |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 682 | host, port, strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 683 | return -EINVAL; |
| 684 | } |
| 685 | |
| 686 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 687 | sfd = socket_helper(rp, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 688 | if (sfd == -1) |
| 689 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 690 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 691 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 692 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 693 | if (rc != 0 && errno != EINPROGRESS) { |
| 694 | close(sfd); |
| 695 | continue; |
| 696 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 697 | } else { |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 698 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 699 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 700 | &on, sizeof(on)); |
| 701 | if (rc < 0) { |
| 702 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 703 | "cannot setsockopt socket:" |
| 704 | " %s:%u: %s\n", |
| 705 | host, port, strerror(errno)); |
| 706 | close(sfd); |
| 707 | continue; |
| 708 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 709 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 710 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 711 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket:" |
| 712 | "%s:%u: %s\n", |
| 713 | host, port, strerror(errno)); |
| 714 | close(sfd); |
| 715 | continue; |
| 716 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 717 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 718 | break; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 719 | } |
| 720 | freeaddrinfo(result); |
| 721 | |
| 722 | if (rp == NULL) { |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 723 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable addr found for: %s:%u\n", |
| 724 | host, port); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 725 | return -ENODEV; |
| 726 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 727 | |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 728 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 729 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 730 | if (rc < 0) { |
| 731 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 732 | "cannot setsockopt socket: %s:%u: %s\n", host, |
| 733 | port, strerror(errno)); |
| 734 | close(sfd); |
| 735 | sfd = -1; |
| 736 | } |
Philipp Maier | 0659c5d | 2018-08-01 12:43:08 +0200 | [diff] [blame] | 737 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 738 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 739 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 740 | if (rc < 0) { |
| 741 | close(sfd); |
| 742 | sfd = -1; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 743 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 744 | |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 745 | return sfd; |
| 746 | } |
| 747 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 748 | /*! fill \ref osmo_fd for a give sfd |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 749 | * \param[out] ofd file descriptor (will be filled in) |
| 750 | * \param[in] sfd socket file descriptor |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 751 | * \returns socket fd on success; negative on error |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 752 | * |
| 753 | * This function fills the \a ofd structure. |
| 754 | */ |
| 755 | static inline int osmo_fd_init_ofd(struct osmo_fd *ofd, int sfd) |
| 756 | { |
| 757 | int rc; |
| 758 | |
| 759 | if (sfd < 0) |
| 760 | return sfd; |
| 761 | |
| 762 | ofd->fd = sfd; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 763 | ofd->when = OSMO_FD_READ; |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 764 | |
| 765 | rc = osmo_fd_register(ofd); |
| 766 | if (rc < 0) { |
| 767 | close(sfd); |
| 768 | return rc; |
| 769 | } |
| 770 | |
| 771 | return sfd; |
| 772 | } |
| 773 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 774 | /*! Initialize a socket and fill \ref osmo_fd |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 775 | * \param[out] ofd file descriptor (will be filled in) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 776 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 777 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 778 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 779 | * \param[in] host remote host name or IP address in string form |
| 780 | * \param[in] port remote port number in host byte order |
| 781 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 782 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 783 | * |
| 784 | * This function creates (and optionall binds/connects) a socket using |
| 785 | * \ref osmo_sock_init, but also fills the \a ofd structure. |
| 786 | */ |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 787 | int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 788 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 789 | { |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 790 | return osmo_fd_init_ofd(ofd, osmo_sock_init(family, type, proto, host, port, flags)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 791 | } |
| 792 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 793 | /*! Initialize a socket and fill \ref osmo_fd |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 794 | * \param[out] ofd file descriptor (will be filled in) |
| 795 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 796 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 797 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 798 | * \param[in] local_host local host name or IP address in string form |
| 799 | * \param[in] local_port local port number in host byte order |
| 800 | * \param[in] remote_host remote host name or IP address in string form |
| 801 | * \param[in] remote_port remote port number in host byte order |
| 802 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 803 | * \returns socket fd on success; negative on error |
| 804 | * |
| 805 | * This function creates (and optionall binds/connects) a socket using |
| 806 | * \ref osmo_sock_init2, but also fills the \a ofd structure. |
| 807 | */ |
| 808 | int osmo_sock_init2_ofd(struct osmo_fd *ofd, int family, int type, int proto, |
| 809 | const char *local_host, uint16_t local_port, |
| 810 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 811 | { |
| 812 | return osmo_fd_init_ofd(ofd, osmo_sock_init2(family, type, proto, local_host, |
| 813 | local_port, remote_host, remote_port, flags)); |
| 814 | } |
| 815 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 816 | /*! Initialize a socket and fill \ref sockaddr |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 817 | * \param[out] ss socket address (will be filled in) |
| 818 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 819 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 820 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 821 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 822 | * |
| 823 | * This function creates (and optionall binds/connects) a socket using |
| 824 | * \ref osmo_sock_init, but also fills the \a ss structure. |
| 825 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 826 | int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 827 | uint8_t proto, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 828 | { |
| 829 | char host[NI_MAXHOST]; |
| 830 | uint16_t port; |
| 831 | struct sockaddr_in *sin; |
| 832 | struct sockaddr_in6 *sin6; |
| 833 | int s, sa_len; |
| 834 | |
| 835 | /* determine port and host from ss */ |
| 836 | switch (ss->sa_family) { |
| 837 | case AF_INET: |
| 838 | sin = (struct sockaddr_in *) ss; |
| 839 | sa_len = sizeof(struct sockaddr_in); |
| 840 | port = ntohs(sin->sin_port); |
| 841 | break; |
| 842 | case AF_INET6: |
| 843 | sin6 = (struct sockaddr_in6 *) ss; |
| 844 | sa_len = sizeof(struct sockaddr_in6); |
| 845 | port = ntohs(sin6->sin6_port); |
| 846 | break; |
| 847 | default: |
| 848 | return -EINVAL; |
| 849 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 850 | |
| 851 | s = getnameinfo(ss, sa_len, host, NI_MAXHOST, |
| 852 | NULL, 0, NI_NUMERICHOST); |
| 853 | if (s != 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 854 | LOGP(DLGLOBAL, LOGL_ERROR, "getnameinfo failed:" |
| 855 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 856 | return s; |
| 857 | } |
| 858 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 859 | return osmo_sock_init(ss->sa_family, type, proto, host, port, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | static int sockaddr_equal(const struct sockaddr *a, |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 863 | const struct sockaddr *b, unsigned int len) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 864 | { |
| 865 | struct sockaddr_in *sin_a, *sin_b; |
| 866 | struct sockaddr_in6 *sin6_a, *sin6_b; |
| 867 | |
| 868 | if (a->sa_family != b->sa_family) |
| 869 | return 0; |
| 870 | |
| 871 | switch (a->sa_family) { |
| 872 | case AF_INET: |
| 873 | sin_a = (struct sockaddr_in *)a; |
| 874 | sin_b = (struct sockaddr_in *)b; |
| 875 | if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr, |
| 876 | sizeof(struct in_addr))) |
| 877 | return 1; |
| 878 | break; |
| 879 | case AF_INET6: |
| 880 | sin6_a = (struct sockaddr_in6 *)a; |
| 881 | sin6_b = (struct sockaddr_in6 *)b; |
| 882 | if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr, |
| 883 | sizeof(struct in6_addr))) |
| 884 | return 1; |
| 885 | break; |
| 886 | } |
| 887 | return 0; |
| 888 | } |
| 889 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 890 | /*! Determine if the given address is a local address |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 891 | * \param[in] addr Socket Address |
| 892 | * \param[in] addrlen Length of socket address in bytes |
| 893 | * \returns 1 if address is local, 0 otherwise. |
| 894 | */ |
Harald Welte | bc32d05 | 2012-04-08 11:31:32 +0200 | [diff] [blame] | 895 | int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 896 | { |
| 897 | struct ifaddrs *ifaddr, *ifa; |
| 898 | |
| 899 | if (getifaddrs(&ifaddr) == -1) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 900 | LOGP(DLGLOBAL, LOGL_ERROR, "getifaddrs:" |
| 901 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 902 | return -EIO; |
| 903 | } |
| 904 | |
| 905 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
Harald Welte | 4d3a7b1 | 2011-05-24 21:31:53 +0200 | [diff] [blame] | 906 | if (!ifa->ifa_addr) |
| 907 | continue; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 908 | if (sockaddr_equal(ifa->ifa_addr, addr, addrlen)) { |
| 909 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 910 | return 1; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 911 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 912 | } |
| 913 | |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 914 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 915 | return 0; |
| 916 | } |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 917 | |
Max | 9d7a247 | 2018-11-20 15:18:31 +0100 | [diff] [blame] | 918 | /*! Convert sockaddr_in to IP address as char string and port as uint16_t. |
| 919 | * \param[out] addr String buffer to write IP address to, or NULL. |
| 920 | * \param[out] addr_len Size of \a addr. |
| 921 | * \param[out] port Pointer to uint16_t to write the port number to, or NULL. |
| 922 | * \param[in] sin Sockaddr to convert. |
| 923 | * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL. |
| 924 | */ |
| 925 | size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 926 | const struct sockaddr_in *sin) |
| 927 | { |
| 928 | if (port) |
| 929 | *port = ntohs(sin->sin_port); |
| 930 | |
| 931 | if (addr) |
| 932 | return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len); |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 937 | /*! Convert sockaddr to IP address as char string and port as uint16_t. |
| 938 | * \param[out] addr String buffer to write IP address to, or NULL. |
| 939 | * \param[out] addr_len Size of \a addr. |
| 940 | * \param[out] port Pointer to uint16_t to write the port number to, or NULL. |
| 941 | * \param[in] sa Sockaddr to convert. |
| 942 | * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL. |
| 943 | */ |
| 944 | unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 945 | const struct sockaddr *sa) |
| 946 | { |
| 947 | const struct sockaddr_in *sin = (const struct sockaddr_in *)sa; |
| 948 | |
Max | 9d7a247 | 2018-11-20 15:18:31 +0100 | [diff] [blame] | 949 | return osmo_sockaddr_in_to_str_and_uint(addr, addr_len, port, sin); |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 950 | } |
| 951 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 952 | /*! Initialize a unix domain socket (including bind/connect) |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 953 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 954 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 955 | * \param[in] socket_path path to identify the socket |
| 956 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 957 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 958 | * |
| 959 | * This function creates a new unix domain socket, \a |
| 960 | * type and \a proto and optionally binds or connects it, depending on |
| 961 | * the value of \a flags parameter. |
| 962 | */ |
Eric Wild | eb5769b | 2019-06-27 15:31:17 +0200 | [diff] [blame] | 963 | #if defined(__clang__) && defined(SUN_LEN) |
| 964 | __attribute__((no_sanitize("undefined"))) |
| 965 | #endif |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 966 | int osmo_sock_unix_init(uint16_t type, uint8_t proto, |
| 967 | const char *socket_path, unsigned int flags) |
| 968 | { |
| 969 | struct sockaddr_un local; |
| 970 | int sfd, rc, on = 1; |
| 971 | unsigned int namelen; |
| 972 | |
| 973 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
| 974 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) |
| 975 | return -EINVAL; |
| 976 | |
| 977 | local.sun_family = AF_UNIX; |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 978 | /* When an AF_UNIX socket is bound, sun_path should be NUL-terminated. See unix(7) man page. */ |
| 979 | if (osmo_strlcpy(local.sun_path, socket_path, sizeof(local.sun_path)) >= sizeof(local.sun_path)) { |
Stefan Sperling | 896ff6d | 2018-08-28 14:34:17 +0200 | [diff] [blame] | 980 | LOGP(DLGLOBAL, LOGL_ERROR, "Socket path exceeds maximum length of %zd bytes: %s\n", |
| 981 | sizeof(local.sun_path), socket_path); |
| 982 | return -ENOSPC; |
| 983 | } |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 984 | |
| 985 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 986 | local.sun_len = strlen(local.sun_path); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 987 | #endif |
| 988 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 989 | namelen = SUN_LEN(&local); |
| 990 | #else |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 991 | namelen = strlen(local.sun_path) + |
| 992 | offsetof(struct sockaddr_un, sun_path); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 993 | #endif |
| 994 | |
| 995 | sfd = socket(AF_UNIX, type, proto); |
| 996 | if (sfd < 0) |
| 997 | return -1; |
| 998 | |
| 999 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 1000 | rc = connect(sfd, (struct sockaddr *)&local, namelen); |
| 1001 | if (rc < 0) |
| 1002 | goto err; |
| 1003 | } else { |
| 1004 | unlink(local.sun_path); |
| 1005 | rc = bind(sfd, (struct sockaddr *)&local, namelen); |
| 1006 | if (rc < 0) |
| 1007 | goto err; |
| 1008 | } |
| 1009 | |
| 1010 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 1011 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 1012 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 1013 | "cannot set this socket unblocking: %s\n", |
| 1014 | strerror(errno)); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1015 | close(sfd); |
| 1016 | return -EINVAL; |
| 1017 | } |
| 1018 | } |
| 1019 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 1020 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 1021 | if (rc < 0) { |
| 1022 | close(sfd); |
| 1023 | sfd = -1; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | return sfd; |
| 1027 | err: |
| 1028 | close(sfd); |
| 1029 | return -1; |
| 1030 | } |
| 1031 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1032 | /*! Initialize a unix domain socket and fill \ref osmo_fd |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1033 | * \param[out] ofd file descriptor (will be filled in) |
| 1034 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1035 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1036 | * \param[in] socket_path path to identify the socket |
| 1037 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1038 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1039 | * |
Vadim Yanitskiy | b606d76 | 2019-06-01 19:02:47 +0700 | [diff] [blame] | 1040 | * This function creates (and optionally binds/connects) a socket |
| 1041 | * using osmo_sock_unix_init, but also fills the ofd structure. |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1042 | */ |
| 1043 | int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto, |
| 1044 | const char *socket_path, unsigned int flags) |
| 1045 | { |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 1046 | return osmo_fd_init_ofd(ofd, osmo_sock_unix_init(type, proto, socket_path, flags)); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1047 | } |
| 1048 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1049 | /*! Get the IP and/or port number on socket in separate string buffers. |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1050 | * \param[in] fd file descriptor of socket |
| 1051 | * \param[out] ip IP address (will be filled in when not NULL) |
| 1052 | * \param[in] ip_len length of the ip buffer |
| 1053 | * \param[out] port number (will be filled in when not NULL) |
| 1054 | * \param[in] port_len length of the port buffer |
| 1055 | * \param[in] local (true) or remote (false) name will get looked at |
| 1056 | * \returns 0 on success; negative otherwise |
| 1057 | */ |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1058 | int osmo_sock_get_ip_and_port(int fd, char *ip, size_t ip_len, char *port, size_t port_len, bool local) |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1059 | { |
| 1060 | struct sockaddr sa; |
| 1061 | socklen_t len = sizeof(sa); |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 1062 | char ipbuf[INET6_ADDRSTRLEN], portbuf[6]; |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1063 | int rc; |
| 1064 | |
| 1065 | rc = local ? getsockname(fd, &sa, &len) : getpeername(fd, &sa, &len); |
| 1066 | if (rc < 0) |
| 1067 | return rc; |
| 1068 | |
| 1069 | rc = getnameinfo(&sa, len, ipbuf, sizeof(ipbuf), |
| 1070 | portbuf, sizeof(portbuf), |
| 1071 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 1072 | if (rc < 0) |
| 1073 | return rc; |
| 1074 | |
| 1075 | if (ip) |
| 1076 | strncpy(ip, ipbuf, ip_len); |
| 1077 | if (port) |
| 1078 | strncpy(port, portbuf, port_len); |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | /*! Get local IP address on socket |
| 1083 | * \param[in] fd file descriptor of socket |
| 1084 | * \param[out] ip IP address (will be filled in) |
| 1085 | * \param[in] len length of the output buffer |
| 1086 | * \returns 0 on success; negative otherwise |
| 1087 | */ |
| 1088 | int osmo_sock_get_local_ip(int fd, char *ip, size_t len) |
| 1089 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1090 | return osmo_sock_get_ip_and_port(fd, ip, len, NULL, 0, true); |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /*! Get local port on socket |
| 1094 | * \param[in] fd file descriptor of socket |
| 1095 | * \param[out] port number (will be filled in) |
| 1096 | * \param[in] len length of the output buffer |
| 1097 | * \returns 0 on success; negative otherwise |
| 1098 | */ |
| 1099 | int osmo_sock_get_local_ip_port(int fd, char *port, size_t len) |
| 1100 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1101 | return osmo_sock_get_ip_and_port(fd, NULL, 0, port, len, true); |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /*! Get remote IP address on socket |
| 1105 | * \param[in] fd file descriptor of socket |
| 1106 | * \param[out] ip IP address (will be filled in) |
| 1107 | * \param[in] len length of the output buffer |
| 1108 | * \returns 0 on success; negative otherwise |
| 1109 | */ |
| 1110 | int osmo_sock_get_remote_ip(int fd, char *ip, size_t len) |
| 1111 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1112 | return osmo_sock_get_ip_and_port(fd, ip, len, NULL, 0, false); |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | /*! Get remote port on socket |
| 1116 | * \param[in] fd file descriptor of socket |
| 1117 | * \param[out] port number (will be filled in) |
| 1118 | * \param[in] len length of the output buffer |
| 1119 | * \returns 0 on success; negative otherwise |
| 1120 | */ |
| 1121 | int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len) |
| 1122 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1123 | return osmo_sock_get_ip_and_port(fd, NULL, 0, port, len, false); |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1124 | } |
| 1125 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1126 | /*! Get address/port information on socket in dyn-alloc string like "(r=1.2.3.4:5<->l=6.7.8.9:10)". |
| 1127 | * Usually, it is better to use osmo_sock_get_name2() for a static string buffer or osmo_sock_get_name_buf() for a |
| 1128 | * caller provided string buffer, to avoid the dynamic talloc allocation. |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1129 | * \param[in] ctx talloc context from which to allocate string buffer |
| 1130 | * \param[in] fd file descriptor of socket |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1131 | * \returns string identifying the connection of this socket, talloc'd from ctx. |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1132 | */ |
Harald Welte | c0dfc9d | 2019-03-18 18:29:43 +0100 | [diff] [blame] | 1133 | char *osmo_sock_get_name(const void *ctx, int fd) |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1134 | { |
Philipp Maier | 64b51eb | 2019-01-14 11:59:11 +0100 | [diff] [blame] | 1135 | char str[OSMO_SOCK_NAME_MAXLEN]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1136 | int rc; |
| 1137 | rc = osmo_sock_get_name_buf(str, sizeof(str), fd); |
| 1138 | if (rc <= 0) |
| 1139 | return NULL; |
| 1140 | return talloc_asprintf(ctx, "(%s)", str); |
| 1141 | } |
| 1142 | |
| 1143 | /*! Get address/port information on socket in provided string buffer, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1144 | * This does not include braces like osmo_sock_get_name(). |
| 1145 | * \param[out] str Destination string buffer. |
| 1146 | * \param[in] str_len sizeof(str). |
| 1147 | * \param[in] fd File descriptor of socket. |
| 1148 | * \return String length as returned by snprintf(), or negative on error. |
| 1149 | */ |
| 1150 | int osmo_sock_get_name_buf(char *str, size_t str_len, int fd) |
| 1151 | { |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 1152 | char hostbuf_l[INET6_ADDRSTRLEN], hostbuf_r[INET6_ADDRSTRLEN]; |
| 1153 | char portbuf_l[6], portbuf_r[6]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1154 | int rc; |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1155 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1156 | /* get local */ |
Harald Welte | ea9ea52 | 2019-05-10 09:28:24 +0200 | [diff] [blame] | 1157 | if ((rc = osmo_sock_get_ip_and_port(fd, hostbuf_l, sizeof(hostbuf_l), portbuf_l, sizeof(portbuf_l), true))) { |
| 1158 | osmo_strlcpy(str, "<error-in-getsockname>", str_len); |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1159 | return rc; |
Harald Welte | ea9ea52 | 2019-05-10 09:28:24 +0200 | [diff] [blame] | 1160 | } |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1161 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1162 | /* get remote */ |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1163 | if (osmo_sock_get_ip_and_port(fd, hostbuf_r, sizeof(hostbuf_r), portbuf_r, sizeof(portbuf_r), false) != 0) |
| 1164 | return snprintf(str, str_len, "r=NULL<->l=%s:%s", hostbuf_l, portbuf_l); |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1165 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1166 | return snprintf(str, str_len, "r=%s:%s<->l=%s:%s", hostbuf_r, portbuf_r, hostbuf_l, portbuf_l); |
| 1167 | } |
| 1168 | |
| 1169 | /*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1170 | * This does not include braces like osmo_sock_get_name(). |
| 1171 | * \param[in] fd File descriptor of socket. |
| 1172 | * \return Static string buffer containing the result. |
| 1173 | */ |
| 1174 | const char *osmo_sock_get_name2(int fd) |
| 1175 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 1176 | static __thread char str[OSMO_SOCK_NAME_MAXLEN]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1177 | osmo_sock_get_name_buf(str, sizeof(str), fd); |
| 1178 | return str; |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1179 | } |
| 1180 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 1181 | /*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1182 | * This does not include braces like osmo_sock_get_name(). |
| 1183 | * \param[in] fd File descriptor of socket. |
| 1184 | * \return Static string buffer containing the result. |
| 1185 | */ |
| 1186 | char *osmo_sock_get_name2_c(const void *ctx, int fd) |
| 1187 | { |
| 1188 | char *str = talloc_size(ctx, OSMO_SOCK_NAME_MAXLEN); |
| 1189 | if (!str) |
| 1190 | return NULL; |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 1191 | osmo_sock_get_name_buf(str, OSMO_SOCK_NAME_MAXLEN, fd); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 1192 | return str; |
| 1193 | } |
| 1194 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 1195 | static int sock_get_domain(int fd) |
| 1196 | { |
| 1197 | int domain; |
| 1198 | #ifdef SO_DOMAIN |
| 1199 | socklen_t dom_len = sizeof(domain); |
| 1200 | int rc; |
| 1201 | |
| 1202 | rc = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &dom_len); |
| 1203 | if (rc < 0) |
| 1204 | return rc; |
| 1205 | #else |
| 1206 | /* This of course sucks, but what shall we do on OSs like |
| 1207 | * FreeBSD that don't seem to expose a method by which one can |
| 1208 | * learn the address family of a socket? */ |
| 1209 | domain = AF_INET; |
| 1210 | #endif |
| 1211 | return domain; |
| 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | /*! Activate or de-activate local loop-back of transmitted multicast packets |
| 1216 | * \param[in] fd file descriptor of related socket |
| 1217 | * \param[in] enable Enable (true) or disable (false) loop-back |
| 1218 | * \returns 0 on success; negative otherwise */ |
| 1219 | int osmo_sock_mcast_loop_set(int fd, bool enable) |
| 1220 | { |
| 1221 | int domain, loop = 0; |
| 1222 | |
| 1223 | if (enable) |
| 1224 | loop = 1; |
| 1225 | |
| 1226 | domain = sock_get_domain(fd); |
| 1227 | if (domain < 0) |
| 1228 | return domain; |
| 1229 | |
| 1230 | switch (domain) { |
| 1231 | case AF_INET: |
| 1232 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 1233 | case AF_INET6: |
| 1234 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 1235 | default: |
| 1236 | return -EINVAL; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | /*! Set the TTL of outbound multicast packets |
| 1241 | * \param[in] fd file descriptor of related socket |
| 1242 | * \param[in] ttl TTL of to-be-sent multicast packets |
| 1243 | * \returns 0 on success; negative otherwise */ |
| 1244 | int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl) |
| 1245 | { |
| 1246 | int domain, ttli = ttl; |
| 1247 | |
| 1248 | domain = sock_get_domain(fd); |
| 1249 | if (domain < 0) |
| 1250 | return domain; |
| 1251 | |
| 1252 | switch (domain) { |
| 1253 | case AF_INET: |
| 1254 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttli, sizeof(ttli)); |
| 1255 | case AF_INET6: |
| 1256 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttli, sizeof(ttli)); |
| 1257 | default: |
| 1258 | return -EINVAL; |
| 1259 | } |
| 1260 | } |
| 1261 | |
Harald Welte | 44b9926 | 2020-03-07 14:59:05 +0100 | [diff] [blame] | 1262 | /*! Set the network device to which we should bind the multicast socket |
| 1263 | * \param[in] fd file descriptor of related socket |
| 1264 | * \param[in] ifname name of network interface to user for multicast |
| 1265 | * \returns 0 on success; negative otherwise */ |
| 1266 | int osmo_sock_mcast_iface_set(int fd, const char *ifname) |
| 1267 | { |
| 1268 | unsigned int ifindex; |
| 1269 | struct ip_mreqn mr; |
| 1270 | |
| 1271 | /* first, resolve interface name to ifindex */ |
| 1272 | ifindex = if_nametoindex(ifname); |
| 1273 | if (ifindex == 0) |
| 1274 | return -errno; |
| 1275 | |
| 1276 | /* next, configure kernel to use that ifindex for this sockets multicast traffic */ |
| 1277 | memset(&mr, 0, sizeof(mr)); |
| 1278 | mr.imr_ifindex = ifindex; |
| 1279 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mr, sizeof(mr)); |
| 1280 | } |
| 1281 | |
| 1282 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 1283 | /*! Enable/disable receiving all multicast packets, even for non-subscribed groups |
| 1284 | * \param[in] fd file descriptor of related socket |
| 1285 | * \param[in] enable Enable or Disable receiving of all packets |
| 1286 | * \returns 0 on success; negative otherwise */ |
| 1287 | int osmo_sock_mcast_all_set(int fd, bool enable) |
| 1288 | { |
| 1289 | int domain, all = 0; |
| 1290 | |
| 1291 | if (enable) |
| 1292 | all = 1; |
| 1293 | |
| 1294 | domain = sock_get_domain(fd); |
| 1295 | if (domain < 0) |
| 1296 | return domain; |
| 1297 | |
| 1298 | switch (domain) { |
| 1299 | case AF_INET: |
| 1300 | #ifdef IP_MULTICAST_ALL |
| 1301 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)); |
| 1302 | #endif |
| 1303 | case AF_INET6: |
| 1304 | /* there seems no equivalent ?!? */ |
| 1305 | default: |
| 1306 | return -EINVAL; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | /* FreeBSD calls the socket option differently */ |
| 1311 | #if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP) |
| 1312 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP |
| 1313 | #endif |
| 1314 | |
| 1315 | /*! Subscribe to the given IP multicast group |
| 1316 | * \param[in] fd file descriptor of related scoket |
| 1317 | * \param[in] grp_addr ASCII representation of the multicast group address |
| 1318 | * \returns 0 on success; negative otherwise */ |
| 1319 | int osmo_sock_mcast_subscribe(int fd, const char *grp_addr) |
| 1320 | { |
| 1321 | int rc, domain; |
| 1322 | struct ip_mreq mreq; |
| 1323 | struct ipv6_mreq mreq6; |
| 1324 | struct in6_addr i6a; |
| 1325 | |
| 1326 | domain = sock_get_domain(fd); |
| 1327 | if (domain < 0) |
| 1328 | return domain; |
| 1329 | |
| 1330 | switch (domain) { |
| 1331 | case AF_INET: |
| 1332 | memset(&mreq, 0, sizeof(mreq)); |
| 1333 | mreq.imr_multiaddr.s_addr = inet_addr(grp_addr); |
| 1334 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
| 1335 | return setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 1336 | #ifdef IPV6_ADD_MEMBERSHIP |
| 1337 | case AF_INET6: |
| 1338 | memset(&mreq6, 0, sizeof(mreq6)); |
| 1339 | rc = inet_pton(AF_INET6, grp_addr, (void *)&i6a); |
| 1340 | if (rc < 0) |
| 1341 | return -EINVAL; |
| 1342 | mreq6.ipv6mr_multiaddr = i6a; |
| 1343 | return setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)); |
| 1344 | #endif |
| 1345 | default: |
| 1346 | return -EINVAL; |
| 1347 | } |
| 1348 | } |
| 1349 | |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1350 | /*! Determine the matching local IP-address for a given remote IP-Address. |
| 1351 | * \param[out] local_ip caller provided memory for resulting local IP-address |
| 1352 | * \param[in] remote_ip remote IP-address |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1353 | * \returns 0 on success; negative otherwise |
| 1354 | * |
| 1355 | * The function accepts IPv4 and IPv6 address strings. The caller must provide |
| 1356 | * at least INET6_ADDRSTRLEN bytes for local_ip if an IPv6 is expected as |
| 1357 | * as result. For IPv4 addresses the required amount is INET_ADDRSTRLEN. */ |
| 1358 | int osmo_sock_local_ip(char *local_ip, const char *remote_ip) |
| 1359 | { |
| 1360 | int sfd; |
| 1361 | int rc; |
| 1362 | struct addrinfo addrinfo_hint; |
| 1363 | struct addrinfo *addrinfo = NULL; |
| 1364 | struct sockaddr_in local_addr; |
| 1365 | socklen_t local_addr_len; |
| 1366 | uint16_t family; |
| 1367 | |
| 1368 | /* Find out the address family (AF_INET or AF_INET6?) */ |
| 1369 | memset(&addrinfo_hint, '\0', sizeof(addrinfo_hint)); |
| 1370 | addrinfo_hint.ai_family = PF_UNSPEC; |
| 1371 | addrinfo_hint.ai_flags = AI_NUMERICHOST; |
| 1372 | rc = getaddrinfo(remote_ip, NULL, &addrinfo_hint, &addrinfo); |
| 1373 | if (rc) |
| 1374 | return -EINVAL; |
| 1375 | family = addrinfo->ai_family; |
| 1376 | freeaddrinfo(addrinfo); |
| 1377 | |
| 1378 | /* Connect a dummy socket to trick the kernel into determining the |
| 1379 | * ip-address of the interface that would be used if we would send |
| 1380 | * out an actual packet */ |
| 1381 | sfd = osmo_sock_init2(family, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, remote_ip, 0, OSMO_SOCK_F_CONNECT); |
| 1382 | if (sfd < 0) |
| 1383 | return -EINVAL; |
| 1384 | |
| 1385 | /* Request the IP address of the interface that the kernel has |
| 1386 | * actually choosen. */ |
| 1387 | memset(&local_addr, 0, sizeof(local_addr)); |
| 1388 | local_addr_len = sizeof(local_addr); |
| 1389 | rc = getsockname(sfd, (struct sockaddr *)&local_addr, &local_addr_len); |
Philipp Maier | 8b7975b | 2018-01-22 15:38:07 +0100 | [diff] [blame] | 1390 | close(sfd); |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1391 | if (rc < 0) |
| 1392 | return -EINVAL; |
| 1393 | if (local_addr.sin_family == AF_INET) |
Philipp Maier | 91cfda8 | 2018-01-22 16:56:27 +0100 | [diff] [blame] | 1394 | inet_ntop(AF_INET, &local_addr.sin_addr, local_ip, INET_ADDRSTRLEN); |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1395 | else if (local_addr.sin_family == AF_INET6) |
Philipp Maier | 91cfda8 | 2018-01-22 16:56:27 +0100 | [diff] [blame] | 1396 | inet_ntop(AF_INET6, &local_addr.sin_addr, local_ip, INET6_ADDRSTRLEN); |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1397 | else |
| 1398 | return -EINVAL; |
| 1399 | |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 1403 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1404 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 1405 | /*! @} */ |