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 | } |
Pau Espin Pedrol | 3119d47 | 2020-08-25 12:30:55 +0200 | [diff] [blame] | 82 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 83 | if (passive) |
| 84 | hints.ai_flags |= AI_PASSIVE; |
| 85 | |
| 86 | rc = getaddrinfo(host, portbuf, &hints, &result); |
| 87 | if (rc != 0) { |
Pau Espin Pedrol | ba828c3 | 2020-08-20 18:28:10 +0200 | [diff] [blame] | 88 | LOGP(DLGLOBAL, LOGL_ERROR, "getaddrinfo(%s, %u) failed: %s\n", |
| 89 | host, port, gai_strerror(rc)); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 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 | |
Pau Espin Pedrol | d8cf52b | 2020-08-31 19:00:59 +0200 | [diff] [blame] | 319 | if ((flags & OSMO_SOCK_F_BIND) && (flags & OSMO_SOCK_F_CONNECT)) { |
| 320 | /* prioritize ipv6 as per RFC */ |
| 321 | if (local_ipv6 && remote_ipv6) |
| 322 | family = AF_INET6; |
| 323 | else if (local_ipv4 && remote_ipv4) |
| 324 | family = AF_INET; |
| 325 | else { |
| 326 | if (local) |
| 327 | freeaddrinfo(local); |
| 328 | if (remote) |
| 329 | freeaddrinfo(remote); |
| 330 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 331 | "Unable to find a common protocol (IPv4 or IPv6) " |
| 332 | "for local host: %s and remote host: %s.\n", |
| 333 | local_host, remote_host); |
| 334 | return -ENODEV; |
| 335 | } |
| 336 | } else if ((flags & OSMO_SOCK_F_BIND)) { |
| 337 | family = local_ipv6 ? AF_INET6 : AF_INET; |
| 338 | } else if ((flags & OSMO_SOCK_F_CONNECT)) { |
| 339 | family = remote_ipv6 ? AF_INET6 : AF_INET; |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 343 | /* figure out local side of socket */ |
| 344 | if (flags & OSMO_SOCK_F_BIND) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 345 | for (rp = local; rp != NULL; rp = rp->ai_next) { |
| 346 | /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ |
| 347 | if (rp->ai_family != family) |
| 348 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 349 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 350 | sfd = socket_helper(rp, flags); |
| 351 | if (sfd < 0) |
| 352 | continue; |
| 353 | |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 354 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 355 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 356 | &on, sizeof(on)); |
| 357 | if (rc < 0) { |
| 358 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 359 | "cannot setsockopt socket:" |
| 360 | " %s:%u: %s\n", |
| 361 | local_host, local_port, |
| 362 | strerror(errno)); |
| 363 | close(sfd); |
| 364 | continue; |
| 365 | } |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 366 | } |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 367 | |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 368 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 369 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n", |
| 370 | local_host, local_port, strerror(errno)); |
| 371 | close(sfd); |
| 372 | continue; |
| 373 | } |
| 374 | break; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 375 | } |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 376 | |
| 377 | freeaddrinfo(local); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 378 | if (rp == NULL) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 379 | if (remote) |
| 380 | freeaddrinfo(remote); |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 381 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: %s:%u\n", |
| 382 | local_host, local_port); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 383 | return -ENODEV; |
| 384 | } |
| 385 | } |
| 386 | |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 387 | /* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it |
| 388 | was already closed and func returned. If OSMO_SOCK_F_BIND is not |
| 389 | set, then sfd = -1 */ |
| 390 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 391 | /* figure out remote side of socket */ |
| 392 | if (flags & OSMO_SOCK_F_CONNECT) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 393 | for (rp = remote; rp != NULL; rp = rp->ai_next) { |
| 394 | /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ |
| 395 | if (rp->ai_family != family) |
| 396 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 397 | |
Harald Welte | 5cfa6dc | 2017-07-21 16:52:29 +0200 | [diff] [blame] | 398 | if (sfd < 0) { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 399 | sfd = socket_helper(rp, flags); |
| 400 | if (sfd < 0) |
| 401 | continue; |
| 402 | } |
| 403 | |
| 404 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 405 | if (rc != 0 && errno != EINPROGRESS) { |
| 406 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 407 | remote_host, remote_port, strerror(errno)); |
| 408 | /* We want to maintain the bind socket if bind was enabled */ |
| 409 | if (!(flags & OSMO_SOCK_F_BIND)) { |
| 410 | close(sfd); |
| 411 | sfd = -1; |
| 412 | } |
| 413 | continue; |
| 414 | } |
| 415 | break; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 416 | } |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 417 | |
| 418 | freeaddrinfo(remote); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 419 | if (rp == NULL) { |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 420 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable remote addr found for: %s:%u\n", |
| 421 | remote_host, remote_port); |
| 422 | if (sfd >= 0) |
| 423 | close(sfd); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 424 | return -ENODEV; |
| 425 | } |
| 426 | } |
| 427 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 428 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 429 | if (rc < 0) { |
| 430 | close(sfd); |
| 431 | sfd = -1; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 432 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 433 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 434 | return sfd; |
| 435 | } |
| 436 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 437 | #ifdef HAVE_LIBSCTP |
| 438 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 439 | /* Check whether there's an IPv6 Addr as first option of any addrinfo item in the addrinfo set */ |
| 440 | static void addrinfo_has_v4v6addr(const struct addrinfo **result, size_t result_count, bool *has_v4, bool *has_v6) |
| 441 | { |
| 442 | size_t host_idx; |
| 443 | *has_v4 = false; |
| 444 | *has_v6 = false; |
| 445 | |
| 446 | for (host_idx = 0; host_idx < result_count; host_idx++) { |
| 447 | if (result[host_idx]->ai_family == AF_INET) |
| 448 | *has_v4 = true; |
| 449 | else if (result[host_idx]->ai_family == AF_INET6) |
| 450 | *has_v6 = true; |
| 451 | } |
| 452 | } |
| 453 | |
Pau Espin Pedrol | 4f463c5 | 2020-08-28 14:32:02 +0200 | [diff] [blame] | 454 | /* Check whether there's an IPv6 with IN6ADDR_ANY_INIT ("::") */ |
| 455 | static bool addrinfo_has_in6addr_any(const struct addrinfo **result, size_t result_count) |
| 456 | { |
| 457 | size_t host_idx; |
| 458 | struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; |
| 459 | |
| 460 | for (host_idx = 0; host_idx < result_count; host_idx++) { |
| 461 | if (result[host_idx]->ai_family != AF_INET6) |
| 462 | continue; |
| 463 | if (memcmp(&((struct sockaddr_in6 *)result[host_idx]->ai_addr)->sin6_addr, |
| 464 | &in6addr_any, sizeof(in6addr_any)) == 0) |
| 465 | return true; |
| 466 | } |
| 467 | return false; |
| 468 | } |
| 469 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 470 | static int socket_helper_multiaddr(uint16_t family, uint16_t type, uint8_t proto, unsigned int flags) |
| 471 | { |
| 472 | int sfd, on = 1; |
| 473 | |
| 474 | sfd = socket(family, type, proto); |
| 475 | if (sfd == -1) { |
| 476 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 477 | "Unable to create socket: %s\n", strerror(errno)); |
| 478 | return sfd; |
| 479 | } |
| 480 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 481 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
| 482 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 483 | "Cannot set this socket unblocking: %s\n", |
| 484 | strerror(errno)); |
| 485 | close(sfd); |
| 486 | sfd = -EINVAL; |
| 487 | } |
| 488 | } |
| 489 | return sfd; |
| 490 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 491 | |
| 492 | /* Build array of addresses taking first addrinfo result of the requested family |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 493 | * for each host in addrs_buf. */ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 494 | static int addrinfo_to_sockaddr(uint16_t family, const struct addrinfo **result, |
| 495 | const char **hosts, int host_cont, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 496 | uint8_t *addrs_buf, size_t addrs_buf_len) { |
| 497 | size_t host_idx, offset = 0; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 498 | const struct addrinfo *rp; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 499 | |
| 500 | for (host_idx = 0; host_idx < host_cont; host_idx++) { |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 501 | /* Addresses are ordered based on RFC 3484, see man getaddrinfo */ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 502 | for (rp = result[host_idx]; rp != NULL; rp = rp->ai_next) { |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 503 | if (family != AF_UNSPEC && rp->ai_family != family) |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 504 | continue; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 505 | if (offset + rp->ai_addrlen > addrs_buf_len) { |
| 506 | LOGP(DLGLOBAL, LOGL_ERROR, "Output buffer to small: %zu\n", |
| 507 | addrs_buf_len); |
| 508 | return -ENOSPC; |
| 509 | } |
| 510 | memcpy(addrs_buf + offset, rp->ai_addr, rp->ai_addrlen); |
| 511 | offset += rp->ai_addrlen; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 512 | break; |
| 513 | } |
| 514 | if (!rp) { /* No addr could be bound for this host! */ |
| 515 | LOGP(DLGLOBAL, LOGL_ERROR, "No suitable remote address found for host: %s\n", |
| 516 | hosts[host_idx]); |
| 517 | return -ENODEV; |
| 518 | } |
| 519 | } |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | /*! Initialize a socket (including bind and/or connect) with multiple local or remote addresses. |
| 524 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 525 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 526 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 527 | * \param[in] local_hosts array of char pointers (strings), each containing local host name or IP address in string form |
| 528 | * \param[in] local_hosts_cnt length of local_hosts (in items) |
| 529 | * \param[in] local_port local port number in host byte order |
| 530 | * \param[in] remote_host array of char pointers (strings), each containing remote host name or IP address in string form |
| 531 | * \param[in] remote_hosts_cnt length of remote_hosts (in items) |
| 532 | * \param[in] remote_port remote port number in host byte order |
| 533 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 534 | * \returns socket file descriptor on success; negative on error |
| 535 | * |
| 536 | * This function is similar to \ref osmo_sock_init2(), but can be passed an |
| 537 | * array of local or remote addresses for protocols supporting multiple |
| 538 | * addresses per socket, like SCTP (currently only one supported). This function |
| 539 | * should not be used by protocols not supporting this kind of features, but |
| 540 | * rather \ref osmo_sock_init2() should be used instead. |
| 541 | * See \ref osmo_sock_init2() for more information on flags and general behavior. |
| 542 | */ |
| 543 | int osmo_sock_init2_multiaddr(uint16_t family, uint16_t type, uint8_t proto, |
| 544 | const char **local_hosts, size_t local_hosts_cnt, uint16_t local_port, |
| 545 | const char **remote_hosts, size_t remote_hosts_cnt, uint16_t remote_port, |
| 546 | unsigned int flags) |
| 547 | |
| 548 | { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 549 | struct addrinfo *res_loc[OSMO_SOCK_MAX_ADDRS], *res_rem[OSMO_SOCK_MAX_ADDRS]; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 550 | int sfd = -1, rc, on = 1; |
| 551 | int i; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 552 | bool loc_has_v4addr, rem_has_v4addr; |
| 553 | bool loc_has_v6addr, rem_has_v6addr; |
| 554 | struct sockaddr_in6 addrs_buf[OSMO_SOCK_MAX_ADDRS]; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 555 | char strbuf[512]; |
| 556 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 557 | /* updated later in case of AF_UNSPEC */ |
| 558 | loc_has_v4addr = rem_has_v4addr = (family == AF_INET); |
| 559 | loc_has_v6addr = rem_has_v6addr = (family == AF_INET6); |
| 560 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 561 | /* TODO: So far this function is only aimed for SCTP, but could be |
| 562 | reused in the future for other protocols with multi-addr support */ |
| 563 | if (proto != IPPROTO_SCTP) |
| 564 | return -ENOTSUP; |
| 565 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 566 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 567 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 568 | "BIND or CONNECT flags\n"); |
| 569 | return -EINVAL; |
| 570 | } |
| 571 | |
| 572 | if (((flags & OSMO_SOCK_F_BIND) && !local_hosts_cnt) || |
| 573 | ((flags & OSMO_SOCK_F_CONNECT) && !remote_hosts_cnt) || |
| 574 | local_hosts_cnt > OSMO_SOCK_MAX_ADDRS || |
| 575 | remote_hosts_cnt > OSMO_SOCK_MAX_ADDRS) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | /* figure out local side of socket */ |
| 579 | if (flags & OSMO_SOCK_F_BIND) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 580 | rc = addrinfo_helper_multi(res_loc, family, type, proto, local_hosts, |
| 581 | local_hosts_cnt, local_port, true); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 582 | if (rc < 0) |
| 583 | return -EINVAL; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 584 | /* Figure out if there's any IPV4 or IPv6 addr in the set */ |
| 585 | if (family == AF_UNSPEC) |
| 586 | addrinfo_has_v4v6addr((const struct addrinfo **)res_loc, local_hosts_cnt, |
| 587 | &loc_has_v4addr, &loc_has_v6addr); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 588 | } |
| 589 | /* figure out remote side of socket */ |
| 590 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 591 | rc = addrinfo_helper_multi(res_rem, family, type, proto, remote_hosts, |
| 592 | remote_hosts_cnt, remote_port, false); |
Pau Espin Pedrol | 4541cf2 | 2020-08-25 11:22:09 +0200 | [diff] [blame] | 593 | if (rc < 0) { |
| 594 | rc = -EINVAL; |
| 595 | goto ret_freeaddrinfo_loc; |
| 596 | } |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 597 | /* Figure out if there's any IPv4 or IPv6 addr in the set */ |
| 598 | if (family == AF_UNSPEC) |
| 599 | addrinfo_has_v4v6addr((const struct addrinfo **)res_rem, remote_hosts_cnt, |
| 600 | &rem_has_v4addr, &rem_has_v6addr); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 601 | } |
| 602 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 603 | if (((flags & OSMO_SOCK_F_BIND) && (flags & OSMO_SOCK_F_CONNECT)) && |
Pau Espin Pedrol | 4f463c5 | 2020-08-28 14:32:02 +0200 | [diff] [blame] | 604 | !addrinfo_has_in6addr_any((const struct addrinfo **)res_loc, local_hosts_cnt) && |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 605 | (loc_has_v4addr != rem_has_v4addr || loc_has_v6addr != rem_has_v6addr)) { |
| 606 | LOGP(DLGLOBAL, LOGL_ERROR, "Invalid v4 vs v6 in local vs remote addresses\n"); |
| 607 | rc = -EINVAL; |
| 608 | goto ret_freeaddrinfo; |
| 609 | } |
| 610 | |
| 611 | sfd = socket_helper_multiaddr(loc_has_v6addr ? AF_INET6 : AF_INET, |
| 612 | type, proto, flags); |
| 613 | if (sfd < 0) { |
| 614 | rc = sfd; |
| 615 | goto ret_freeaddrinfo; |
| 616 | } |
| 617 | |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 618 | if (flags & OSMO_SOCK_F_BIND) { |
Pau Espin Pedrol | 272dfc1 | 2019-10-21 11:11:27 +0200 | [diff] [blame] | 619 | /* Since so far we only allow IPPROTO_SCTP in this function, |
| 620 | no need to check below for "proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR" */ |
| 621 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 622 | &on, sizeof(on)); |
| 623 | if (rc < 0) { |
| 624 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 625 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 626 | "cannot setsockopt socket:" |
| 627 | " %s:%u: %s\n", |
| 628 | strbuf, local_port, |
| 629 | strerror(errno)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 630 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 633 | /* Build array of addresses taking first entry for each host. |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 634 | TODO: Ideally we should use backtracking storing last used |
| 635 | indexes and trying next combination if connect() fails .*/ |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 636 | /* We could alternatively use v4v6 mapped addresses and call sctp_bindx once with an array od sockaddr_in6 */ |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 637 | rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)res_loc, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 638 | local_hosts, local_hosts_cnt, |
| 639 | (uint8_t*)addrs_buf, sizeof(addrs_buf)); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 640 | if (rc < 0) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 641 | rc = -ENODEV; |
| 642 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 643 | } |
| 644 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 645 | rc = sctp_bindx(sfd, (struct sockaddr *)addrs_buf, local_hosts_cnt, SCTP_BINDX_ADD_ADDR); |
| 646 | if (rc == -1) { |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 647 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 648 | LOGP(DLGLOBAL, LOGL_NOTICE, "unable to bind socket: %s:%u: %s\n", |
| 649 | strbuf, local_port, strerror(errno)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 650 | rc = -ENODEV; |
| 651 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 652 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 653 | } |
| 654 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 655 | if (flags & OSMO_SOCK_F_CONNECT) { |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 656 | /* Build array of addresses taking first of same family for each host. |
| 657 | TODO: Ideally we should use backtracking storing last used |
| 658 | indexes and trying next combination if connect() fails .*/ |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 659 | rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)res_rem, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 660 | remote_hosts, remote_hosts_cnt, |
| 661 | (uint8_t*)addrs_buf, sizeof(addrs_buf)); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 662 | if (rc < 0) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 663 | rc = -ENODEV; |
| 664 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 665 | } |
| 666 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 667 | rc = sctp_connectx(sfd, (struct sockaddr *)addrs_buf, remote_hosts_cnt, NULL); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 668 | if (rc != 0 && errno != EINPROGRESS) { |
| 669 | multiaddr_snprintf(strbuf, sizeof(strbuf), remote_hosts, remote_hosts_cnt); |
| 670 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 671 | strbuf, remote_port, strerror(errno)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 672 | rc = -ENODEV; |
| 673 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 674 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 678 | if (rc < 0) { |
| 679 | close(sfd); |
| 680 | sfd = -1; |
| 681 | } |
| 682 | |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 683 | rc = sfd; |
| 684 | goto ret_freeaddrinfo; |
| 685 | |
| 686 | ret_close: |
| 687 | if (sfd >= 0) |
| 688 | close(sfd); |
| 689 | ret_freeaddrinfo: |
Pau Espin Pedrol | 4541cf2 | 2020-08-25 11:22:09 +0200 | [diff] [blame] | 690 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 691 | for (i = 0; i < remote_hosts_cnt; i++) |
| 692 | freeaddrinfo(res_rem[i]); |
| 693 | } |
| 694 | ret_freeaddrinfo_loc: |
| 695 | if (flags & OSMO_SOCK_F_BIND) { |
| 696 | for (i = 0; i < local_hosts_cnt; i++) |
| 697 | freeaddrinfo(res_loc[i]); |
| 698 | } |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 699 | return rc; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 700 | } |
| 701 | #endif /* HAVE_LIBSCTP */ |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 702 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 703 | /*! Initialize a socket (including bind/connect) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 704 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 705 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 706 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 707 | * \param[in] host remote host name or IP address in string form |
| 708 | * \param[in] port remote port number in host byte order |
| 709 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 710 | * \returns socket file descriptor on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 711 | * |
| 712 | * This function creates a new socket of the designated \a family, \a |
| 713 | * type and \a proto and optionally binds or connects it, depending on |
| 714 | * the value of \a flags parameter. |
| 715 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 716 | 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] | 717 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 718 | { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 719 | struct addrinfo *result, *rp; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 720 | int sfd, rc, on = 1; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 721 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 722 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 723 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 724 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: both bind and connect flags set:" |
Neels Hofmeyr | b7f191f | 2016-08-29 11:22:03 +0200 | [diff] [blame] | 725 | " %s:%u\n", host, port); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 726 | return -EINVAL; |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 727 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 728 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 729 | result = addrinfo_helper(family, type, proto, host, port, flags & OSMO_SOCK_F_BIND); |
Pau Espin Pedrol | ba828c3 | 2020-08-20 18:28:10 +0200 | [diff] [blame] | 730 | if (!result) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 731 | return -EINVAL; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 732 | |
| 733 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 734 | sfd = socket_helper(rp, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 735 | if (sfd == -1) |
| 736 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 737 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 738 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 739 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 740 | if (rc != 0 && errno != EINPROGRESS) { |
| 741 | close(sfd); |
| 742 | continue; |
| 743 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 744 | } else { |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 745 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 746 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 747 | &on, sizeof(on)); |
| 748 | if (rc < 0) { |
| 749 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 750 | "cannot setsockopt socket:" |
| 751 | " %s:%u: %s\n", |
| 752 | host, port, strerror(errno)); |
| 753 | close(sfd); |
| 754 | continue; |
| 755 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 756 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 757 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 758 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket:" |
| 759 | "%s:%u: %s\n", |
| 760 | host, port, strerror(errno)); |
| 761 | close(sfd); |
| 762 | continue; |
| 763 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 764 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 765 | break; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 766 | } |
| 767 | freeaddrinfo(result); |
| 768 | |
| 769 | if (rp == NULL) { |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 770 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable addr found for: %s:%u\n", |
| 771 | host, port); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 772 | return -ENODEV; |
| 773 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 774 | |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 775 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 776 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 777 | if (rc < 0) { |
| 778 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 779 | "cannot setsockopt socket: %s:%u: %s\n", host, |
| 780 | port, strerror(errno)); |
| 781 | close(sfd); |
| 782 | sfd = -1; |
| 783 | } |
Philipp Maier | 0659c5d | 2018-08-01 12:43:08 +0200 | [diff] [blame] | 784 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 785 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 786 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 787 | if (rc < 0) { |
| 788 | close(sfd); |
| 789 | sfd = -1; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 790 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 791 | |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 792 | return sfd; |
| 793 | } |
| 794 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 795 | /*! fill \ref osmo_fd for a give sfd |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 796 | * \param[out] ofd file descriptor (will be filled in) |
| 797 | * \param[in] sfd socket file descriptor |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 798 | * \returns socket fd on success; negative on error |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 799 | * |
| 800 | * This function fills the \a ofd structure. |
| 801 | */ |
| 802 | static inline int osmo_fd_init_ofd(struct osmo_fd *ofd, int sfd) |
| 803 | { |
| 804 | int rc; |
| 805 | |
| 806 | if (sfd < 0) |
| 807 | return sfd; |
| 808 | |
| 809 | ofd->fd = sfd; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 810 | ofd->when = OSMO_FD_READ; |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 811 | |
| 812 | rc = osmo_fd_register(ofd); |
| 813 | if (rc < 0) { |
| 814 | close(sfd); |
| 815 | return rc; |
| 816 | } |
| 817 | |
| 818 | return sfd; |
| 819 | } |
| 820 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 821 | /*! Initialize a socket and fill \ref osmo_fd |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 822 | * \param[out] ofd file descriptor (will be filled in) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 823 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 824 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 825 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 826 | * \param[in] host remote host name or IP address in string form |
| 827 | * \param[in] port remote port number in host byte order |
| 828 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 829 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 830 | * |
| 831 | * This function creates (and optionall binds/connects) a socket using |
| 832 | * \ref osmo_sock_init, but also fills the \a ofd structure. |
| 833 | */ |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 834 | 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] | 835 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 836 | { |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 837 | 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] | 838 | } |
| 839 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 840 | /*! Initialize a socket and fill \ref osmo_fd |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 841 | * \param[out] ofd file descriptor (will be filled in) |
| 842 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 843 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 844 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 845 | * \param[in] local_host local host name or IP address in string form |
| 846 | * \param[in] local_port local port number in host byte order |
| 847 | * \param[in] remote_host remote host name or IP address in string form |
| 848 | * \param[in] remote_port remote port number in host byte order |
| 849 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 850 | * \returns socket fd on success; negative on error |
| 851 | * |
| 852 | * This function creates (and optionall binds/connects) a socket using |
| 853 | * \ref osmo_sock_init2, but also fills the \a ofd structure. |
| 854 | */ |
| 855 | int osmo_sock_init2_ofd(struct osmo_fd *ofd, int family, int type, int proto, |
| 856 | const char *local_host, uint16_t local_port, |
| 857 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 858 | { |
| 859 | return osmo_fd_init_ofd(ofd, osmo_sock_init2(family, type, proto, local_host, |
| 860 | local_port, remote_host, remote_port, flags)); |
| 861 | } |
| 862 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 863 | /*! Initialize a socket and fill \ref sockaddr |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 864 | * \param[out] ss socket address (will be filled in) |
| 865 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 866 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 867 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 868 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 869 | * |
| 870 | * This function creates (and optionall binds/connects) a socket using |
| 871 | * \ref osmo_sock_init, but also fills the \a ss structure. |
| 872 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 873 | int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 874 | uint8_t proto, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 875 | { |
| 876 | char host[NI_MAXHOST]; |
| 877 | uint16_t port; |
| 878 | struct sockaddr_in *sin; |
| 879 | struct sockaddr_in6 *sin6; |
| 880 | int s, sa_len; |
| 881 | |
| 882 | /* determine port and host from ss */ |
| 883 | switch (ss->sa_family) { |
| 884 | case AF_INET: |
| 885 | sin = (struct sockaddr_in *) ss; |
| 886 | sa_len = sizeof(struct sockaddr_in); |
| 887 | port = ntohs(sin->sin_port); |
| 888 | break; |
| 889 | case AF_INET6: |
| 890 | sin6 = (struct sockaddr_in6 *) ss; |
| 891 | sa_len = sizeof(struct sockaddr_in6); |
| 892 | port = ntohs(sin6->sin6_port); |
| 893 | break; |
| 894 | default: |
| 895 | return -EINVAL; |
| 896 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 897 | |
| 898 | s = getnameinfo(ss, sa_len, host, NI_MAXHOST, |
| 899 | NULL, 0, NI_NUMERICHOST); |
| 900 | if (s != 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 901 | LOGP(DLGLOBAL, LOGL_ERROR, "getnameinfo failed:" |
| 902 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 903 | return s; |
| 904 | } |
| 905 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 906 | return osmo_sock_init(ss->sa_family, type, proto, host, port, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static int sockaddr_equal(const struct sockaddr *a, |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 910 | const struct sockaddr *b, unsigned int len) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 911 | { |
| 912 | struct sockaddr_in *sin_a, *sin_b; |
| 913 | struct sockaddr_in6 *sin6_a, *sin6_b; |
| 914 | |
| 915 | if (a->sa_family != b->sa_family) |
| 916 | return 0; |
| 917 | |
| 918 | switch (a->sa_family) { |
| 919 | case AF_INET: |
| 920 | sin_a = (struct sockaddr_in *)a; |
| 921 | sin_b = (struct sockaddr_in *)b; |
| 922 | if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr, |
| 923 | sizeof(struct in_addr))) |
| 924 | return 1; |
| 925 | break; |
| 926 | case AF_INET6: |
| 927 | sin6_a = (struct sockaddr_in6 *)a; |
| 928 | sin6_b = (struct sockaddr_in6 *)b; |
| 929 | if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr, |
| 930 | sizeof(struct in6_addr))) |
| 931 | return 1; |
| 932 | break; |
| 933 | } |
| 934 | return 0; |
| 935 | } |
| 936 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 937 | /*! Determine if the given address is a local address |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 938 | * \param[in] addr Socket Address |
| 939 | * \param[in] addrlen Length of socket address in bytes |
| 940 | * \returns 1 if address is local, 0 otherwise. |
| 941 | */ |
Harald Welte | bc32d05 | 2012-04-08 11:31:32 +0200 | [diff] [blame] | 942 | int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 943 | { |
| 944 | struct ifaddrs *ifaddr, *ifa; |
| 945 | |
| 946 | if (getifaddrs(&ifaddr) == -1) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 947 | LOGP(DLGLOBAL, LOGL_ERROR, "getifaddrs:" |
| 948 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 949 | return -EIO; |
| 950 | } |
| 951 | |
| 952 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
Harald Welte | 4d3a7b1 | 2011-05-24 21:31:53 +0200 | [diff] [blame] | 953 | if (!ifa->ifa_addr) |
| 954 | continue; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 955 | if (sockaddr_equal(ifa->ifa_addr, addr, addrlen)) { |
| 956 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 957 | return 1; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 958 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 959 | } |
| 960 | |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 961 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 962 | return 0; |
| 963 | } |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 964 | |
Max | 9d7a247 | 2018-11-20 15:18:31 +0100 | [diff] [blame] | 965 | /*! Convert sockaddr_in to IP address as char string and port as uint16_t. |
| 966 | * \param[out] addr String buffer to write IP address to, or NULL. |
| 967 | * \param[out] addr_len Size of \a addr. |
| 968 | * \param[out] port Pointer to uint16_t to write the port number to, or NULL. |
| 969 | * \param[in] sin Sockaddr to convert. |
| 970 | * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL. |
| 971 | */ |
| 972 | size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 973 | const struct sockaddr_in *sin) |
| 974 | { |
| 975 | if (port) |
| 976 | *port = ntohs(sin->sin_port); |
| 977 | |
| 978 | if (addr) |
| 979 | return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len); |
| 980 | |
| 981 | return 0; |
| 982 | } |
| 983 | |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 984 | /*! Convert sockaddr to IP address as char string and port as uint16_t. |
| 985 | * \param[out] addr String buffer to write IP address to, or NULL. |
| 986 | * \param[out] addr_len Size of \a addr. |
| 987 | * \param[out] port Pointer to uint16_t to write the port number to, or NULL. |
| 988 | * \param[in] sa Sockaddr to convert. |
| 989 | * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL. |
| 990 | */ |
| 991 | unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 992 | const struct sockaddr *sa) |
| 993 | { |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 994 | |
Pau Espin Pedrol | 1a3d24e | 2020-08-28 18:31:32 +0200 | [diff] [blame] | 995 | const struct sockaddr_in6 *sin6; |
| 996 | |
| 997 | switch (sa->sa_family) { |
| 998 | case AF_INET: |
| 999 | return osmo_sockaddr_in_to_str_and_uint(addr, addr_len, port, |
| 1000 | (const struct sockaddr_in *)sa); |
| 1001 | case AF_INET6: |
| 1002 | sin6 = (const struct sockaddr_in6 *)sa; |
| 1003 | if (port) |
| 1004 | *port = ntohs(sin6->sin6_port); |
| 1005 | if (addr && inet_ntop(sa->sa_family, &sin6->sin6_addr, addr, addr_len)) |
| 1006 | return strlen(addr); |
| 1007 | break; |
| 1008 | } |
| 1009 | return 0; |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 1010 | } |
| 1011 | |
Pau Espin Pedrol | 5cc4fe4 | 2020-08-31 12:52:06 +0200 | [diff] [blame] | 1012 | /*! inet_ntop() wrapper for a struct sockaddr. |
| 1013 | * \param[in] sa source sockaddr to get the address from. |
| 1014 | * \param[out] dst string buffer of at least INET6_ADDRSTRLEN size. |
| 1015 | * \returns returns a non-null pointer to dst. NULL is returned if there was an |
| 1016 | * error, with errno set to indicate the error. |
| 1017 | */ |
| 1018 | const char *osmo_sockaddr_ntop(const struct sockaddr *sa, char *dst) |
| 1019 | { |
| 1020 | const struct osmo_sockaddr *osa = (const struct osmo_sockaddr *)sa; |
| 1021 | return inet_ntop(osa->u.sa.sa_family, |
| 1022 | osa->u.sa.sa_family == AF_INET6 ? |
| 1023 | (const void *)&osa->u.sin6.sin6_addr : |
| 1024 | (const void *)&osa->u.sin.sin_addr, |
| 1025 | dst, INET6_ADDRSTRLEN); |
| 1026 | } |
| 1027 | |
| 1028 | /*! Get sockaddr port content (in host byte order) |
| 1029 | * \param[in] sa source sockaddr to get the port from. |
| 1030 | * \returns returns the sockaddr port in host byte order |
| 1031 | */ |
| 1032 | uint16_t osmo_sockaddr_port(const struct sockaddr *sa) |
| 1033 | { |
| 1034 | const struct osmo_sockaddr *osa = (const struct osmo_sockaddr *)sa; |
| 1035 | switch (osa->u.sa.sa_family) { |
| 1036 | case AF_INET6: |
| 1037 | return ntohs(osa->u.sin6.sin6_port); |
| 1038 | case AF_INET: |
| 1039 | return ntohs(osa->u.sin.sin_port); |
| 1040 | } |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1044 | /*! Initialize a unix domain socket (including bind/connect) |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1045 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1046 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1047 | * \param[in] socket_path path to identify the socket |
| 1048 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1049 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1050 | * |
| 1051 | * This function creates a new unix domain socket, \a |
| 1052 | * type and \a proto and optionally binds or connects it, depending on |
| 1053 | * the value of \a flags parameter. |
| 1054 | */ |
Eric Wild | eb5769b | 2019-06-27 15:31:17 +0200 | [diff] [blame] | 1055 | #if defined(__clang__) && defined(SUN_LEN) |
| 1056 | __attribute__((no_sanitize("undefined"))) |
| 1057 | #endif |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1058 | int osmo_sock_unix_init(uint16_t type, uint8_t proto, |
| 1059 | const char *socket_path, unsigned int flags) |
| 1060 | { |
| 1061 | struct sockaddr_un local; |
| 1062 | int sfd, rc, on = 1; |
| 1063 | unsigned int namelen; |
| 1064 | |
| 1065 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
| 1066 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) |
| 1067 | return -EINVAL; |
| 1068 | |
| 1069 | local.sun_family = AF_UNIX; |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1070 | /* When an AF_UNIX socket is bound, sun_path should be NUL-terminated. See unix(7) man page. */ |
| 1071 | 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] | 1072 | LOGP(DLGLOBAL, LOGL_ERROR, "Socket path exceeds maximum length of %zd bytes: %s\n", |
| 1073 | sizeof(local.sun_path), socket_path); |
| 1074 | return -ENOSPC; |
| 1075 | } |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1076 | |
| 1077 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1078 | local.sun_len = strlen(local.sun_path); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1079 | #endif |
| 1080 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 1081 | namelen = SUN_LEN(&local); |
| 1082 | #else |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1083 | namelen = strlen(local.sun_path) + |
| 1084 | offsetof(struct sockaddr_un, sun_path); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1085 | #endif |
| 1086 | |
| 1087 | sfd = socket(AF_UNIX, type, proto); |
| 1088 | if (sfd < 0) |
| 1089 | return -1; |
| 1090 | |
| 1091 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 1092 | rc = connect(sfd, (struct sockaddr *)&local, namelen); |
| 1093 | if (rc < 0) |
| 1094 | goto err; |
| 1095 | } else { |
| 1096 | unlink(local.sun_path); |
| 1097 | rc = bind(sfd, (struct sockaddr *)&local, namelen); |
| 1098 | if (rc < 0) |
| 1099 | goto err; |
| 1100 | } |
| 1101 | |
| 1102 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 1103 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 1104 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 1105 | "cannot set this socket unblocking: %s\n", |
| 1106 | strerror(errno)); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1107 | close(sfd); |
| 1108 | return -EINVAL; |
| 1109 | } |
| 1110 | } |
| 1111 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 1112 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 1113 | if (rc < 0) { |
| 1114 | close(sfd); |
| 1115 | sfd = -1; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | return sfd; |
| 1119 | err: |
| 1120 | close(sfd); |
| 1121 | return -1; |
| 1122 | } |
| 1123 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1124 | /*! Initialize a unix domain socket and fill \ref osmo_fd |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1125 | * \param[out] ofd file descriptor (will be filled in) |
| 1126 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1127 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1128 | * \param[in] socket_path path to identify the socket |
| 1129 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1130 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1131 | * |
Vadim Yanitskiy | b606d76 | 2019-06-01 19:02:47 +0700 | [diff] [blame] | 1132 | * This function creates (and optionally binds/connects) a socket |
| 1133 | * using osmo_sock_unix_init, but also fills the ofd structure. |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1134 | */ |
| 1135 | int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto, |
| 1136 | const char *socket_path, unsigned int flags) |
| 1137 | { |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 1138 | 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] | 1139 | } |
| 1140 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1141 | /*! 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] | 1142 | * \param[in] fd file descriptor of socket |
| 1143 | * \param[out] ip IP address (will be filled in when not NULL) |
| 1144 | * \param[in] ip_len length of the ip buffer |
| 1145 | * \param[out] port number (will be filled in when not NULL) |
| 1146 | * \param[in] port_len length of the port buffer |
| 1147 | * \param[in] local (true) or remote (false) name will get looked at |
| 1148 | * \returns 0 on success; negative otherwise |
| 1149 | */ |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1150 | 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] | 1151 | { |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1152 | struct sockaddr_storage sa; |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1153 | socklen_t len = sizeof(sa); |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 1154 | char ipbuf[INET6_ADDRSTRLEN], portbuf[6]; |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1155 | int rc; |
| 1156 | |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1157 | rc = local ? getsockname(fd, (struct sockaddr*)&sa, &len) : getpeername(fd, (struct sockaddr*)&sa, &len); |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1158 | if (rc < 0) |
| 1159 | return rc; |
| 1160 | |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1161 | rc = getnameinfo((const struct sockaddr*)&sa, len, ipbuf, sizeof(ipbuf), |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1162 | portbuf, sizeof(portbuf), |
| 1163 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 1164 | if (rc < 0) |
| 1165 | return rc; |
| 1166 | |
| 1167 | if (ip) |
| 1168 | strncpy(ip, ipbuf, ip_len); |
| 1169 | if (port) |
| 1170 | strncpy(port, portbuf, port_len); |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
| 1174 | /*! Get local IP address on socket |
| 1175 | * \param[in] fd file descriptor of socket |
| 1176 | * \param[out] ip IP address (will be filled in) |
| 1177 | * \param[in] len length of the output buffer |
| 1178 | * \returns 0 on success; negative otherwise |
| 1179 | */ |
| 1180 | int osmo_sock_get_local_ip(int fd, char *ip, size_t len) |
| 1181 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1182 | 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] | 1183 | } |
| 1184 | |
| 1185 | /*! Get local port on socket |
| 1186 | * \param[in] fd file descriptor of socket |
| 1187 | * \param[out] port number (will be filled in) |
| 1188 | * \param[in] len length of the output buffer |
| 1189 | * \returns 0 on success; negative otherwise |
| 1190 | */ |
| 1191 | int osmo_sock_get_local_ip_port(int fd, char *port, size_t len) |
| 1192 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1193 | 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] | 1194 | } |
| 1195 | |
| 1196 | /*! Get remote IP address on socket |
| 1197 | * \param[in] fd file descriptor of socket |
| 1198 | * \param[out] ip IP address (will be filled in) |
| 1199 | * \param[in] len length of the output buffer |
| 1200 | * \returns 0 on success; negative otherwise |
| 1201 | */ |
| 1202 | int osmo_sock_get_remote_ip(int fd, char *ip, size_t len) |
| 1203 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1204 | 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] | 1205 | } |
| 1206 | |
| 1207 | /*! Get remote port on socket |
| 1208 | * \param[in] fd file descriptor of socket |
| 1209 | * \param[out] port number (will be filled in) |
| 1210 | * \param[in] len length of the output buffer |
| 1211 | * \returns 0 on success; negative otherwise |
| 1212 | */ |
| 1213 | int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len) |
| 1214 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1215 | 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] | 1216 | } |
| 1217 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1218 | /*! Get address/port information on socket in dyn-alloc string like "(r=1.2.3.4:5<->l=6.7.8.9:10)". |
| 1219 | * Usually, it is better to use osmo_sock_get_name2() for a static string buffer or osmo_sock_get_name_buf() for a |
| 1220 | * caller provided string buffer, to avoid the dynamic talloc allocation. |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1221 | * \param[in] ctx talloc context from which to allocate string buffer |
| 1222 | * \param[in] fd file descriptor of socket |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1223 | * \returns string identifying the connection of this socket, talloc'd from ctx. |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1224 | */ |
Harald Welte | c0dfc9d | 2019-03-18 18:29:43 +0100 | [diff] [blame] | 1225 | char *osmo_sock_get_name(const void *ctx, int fd) |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1226 | { |
Philipp Maier | 64b51eb | 2019-01-14 11:59:11 +0100 | [diff] [blame] | 1227 | char str[OSMO_SOCK_NAME_MAXLEN]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1228 | int rc; |
| 1229 | rc = osmo_sock_get_name_buf(str, sizeof(str), fd); |
| 1230 | if (rc <= 0) |
| 1231 | return NULL; |
| 1232 | return talloc_asprintf(ctx, "(%s)", str); |
| 1233 | } |
| 1234 | |
| 1235 | /*! Get address/port information on socket in provided string buffer, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1236 | * This does not include braces like osmo_sock_get_name(). |
| 1237 | * \param[out] str Destination string buffer. |
| 1238 | * \param[in] str_len sizeof(str). |
| 1239 | * \param[in] fd File descriptor of socket. |
| 1240 | * \return String length as returned by snprintf(), or negative on error. |
| 1241 | */ |
| 1242 | int osmo_sock_get_name_buf(char *str, size_t str_len, int fd) |
| 1243 | { |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 1244 | char hostbuf_l[INET6_ADDRSTRLEN], hostbuf_r[INET6_ADDRSTRLEN]; |
| 1245 | char portbuf_l[6], portbuf_r[6]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1246 | int rc; |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1247 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1248 | /* get local */ |
Harald Welte | ea9ea52 | 2019-05-10 09:28:24 +0200 | [diff] [blame] | 1249 | if ((rc = osmo_sock_get_ip_and_port(fd, hostbuf_l, sizeof(hostbuf_l), portbuf_l, sizeof(portbuf_l), true))) { |
| 1250 | osmo_strlcpy(str, "<error-in-getsockname>", str_len); |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1251 | return rc; |
Harald Welte | ea9ea52 | 2019-05-10 09:28:24 +0200 | [diff] [blame] | 1252 | } |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1253 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1254 | /* get remote */ |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1255 | if (osmo_sock_get_ip_and_port(fd, hostbuf_r, sizeof(hostbuf_r), portbuf_r, sizeof(portbuf_r), false) != 0) |
| 1256 | 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] | 1257 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1258 | return snprintf(str, str_len, "r=%s:%s<->l=%s:%s", hostbuf_r, portbuf_r, hostbuf_l, portbuf_l); |
| 1259 | } |
| 1260 | |
| 1261 | /*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1262 | * This does not include braces like osmo_sock_get_name(). |
| 1263 | * \param[in] fd File descriptor of socket. |
| 1264 | * \return Static string buffer containing the result. |
| 1265 | */ |
| 1266 | const char *osmo_sock_get_name2(int fd) |
| 1267 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 1268 | static __thread char str[OSMO_SOCK_NAME_MAXLEN]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1269 | osmo_sock_get_name_buf(str, sizeof(str), fd); |
| 1270 | return str; |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1271 | } |
| 1272 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 1273 | /*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1274 | * This does not include braces like osmo_sock_get_name(). |
| 1275 | * \param[in] fd File descriptor of socket. |
| 1276 | * \return Static string buffer containing the result. |
| 1277 | */ |
| 1278 | char *osmo_sock_get_name2_c(const void *ctx, int fd) |
| 1279 | { |
| 1280 | char *str = talloc_size(ctx, OSMO_SOCK_NAME_MAXLEN); |
| 1281 | if (!str) |
| 1282 | return NULL; |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 1283 | osmo_sock_get_name_buf(str, OSMO_SOCK_NAME_MAXLEN, fd); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 1284 | return str; |
| 1285 | } |
| 1286 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 1287 | static int sock_get_domain(int fd) |
| 1288 | { |
| 1289 | int domain; |
| 1290 | #ifdef SO_DOMAIN |
| 1291 | socklen_t dom_len = sizeof(domain); |
| 1292 | int rc; |
| 1293 | |
| 1294 | rc = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &dom_len); |
| 1295 | if (rc < 0) |
| 1296 | return rc; |
| 1297 | #else |
| 1298 | /* This of course sucks, but what shall we do on OSs like |
| 1299 | * FreeBSD that don't seem to expose a method by which one can |
| 1300 | * learn the address family of a socket? */ |
| 1301 | domain = AF_INET; |
| 1302 | #endif |
| 1303 | return domain; |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | /*! Activate or de-activate local loop-back of transmitted multicast packets |
| 1308 | * \param[in] fd file descriptor of related socket |
| 1309 | * \param[in] enable Enable (true) or disable (false) loop-back |
| 1310 | * \returns 0 on success; negative otherwise */ |
| 1311 | int osmo_sock_mcast_loop_set(int fd, bool enable) |
| 1312 | { |
| 1313 | int domain, loop = 0; |
| 1314 | |
| 1315 | if (enable) |
| 1316 | loop = 1; |
| 1317 | |
| 1318 | domain = sock_get_domain(fd); |
| 1319 | if (domain < 0) |
| 1320 | return domain; |
| 1321 | |
| 1322 | switch (domain) { |
| 1323 | case AF_INET: |
| 1324 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 1325 | case AF_INET6: |
| 1326 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 1327 | default: |
| 1328 | return -EINVAL; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | /*! Set the TTL of outbound multicast packets |
| 1333 | * \param[in] fd file descriptor of related socket |
| 1334 | * \param[in] ttl TTL of to-be-sent multicast packets |
| 1335 | * \returns 0 on success; negative otherwise */ |
| 1336 | int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl) |
| 1337 | { |
| 1338 | int domain, ttli = ttl; |
| 1339 | |
| 1340 | domain = sock_get_domain(fd); |
| 1341 | if (domain < 0) |
| 1342 | return domain; |
| 1343 | |
| 1344 | switch (domain) { |
| 1345 | case AF_INET: |
| 1346 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttli, sizeof(ttli)); |
| 1347 | case AF_INET6: |
| 1348 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttli, sizeof(ttli)); |
| 1349 | default: |
| 1350 | return -EINVAL; |
| 1351 | } |
| 1352 | } |
| 1353 | |
Harald Welte | 44b9926 | 2020-03-07 14:59:05 +0100 | [diff] [blame] | 1354 | /*! Set the network device to which we should bind the multicast socket |
| 1355 | * \param[in] fd file descriptor of related socket |
| 1356 | * \param[in] ifname name of network interface to user for multicast |
| 1357 | * \returns 0 on success; negative otherwise */ |
| 1358 | int osmo_sock_mcast_iface_set(int fd, const char *ifname) |
| 1359 | { |
| 1360 | unsigned int ifindex; |
| 1361 | struct ip_mreqn mr; |
| 1362 | |
| 1363 | /* first, resolve interface name to ifindex */ |
| 1364 | ifindex = if_nametoindex(ifname); |
| 1365 | if (ifindex == 0) |
| 1366 | return -errno; |
| 1367 | |
| 1368 | /* next, configure kernel to use that ifindex for this sockets multicast traffic */ |
| 1369 | memset(&mr, 0, sizeof(mr)); |
| 1370 | mr.imr_ifindex = ifindex; |
| 1371 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mr, sizeof(mr)); |
| 1372 | } |
| 1373 | |
| 1374 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 1375 | /*! Enable/disable receiving all multicast packets, even for non-subscribed groups |
| 1376 | * \param[in] fd file descriptor of related socket |
| 1377 | * \param[in] enable Enable or Disable receiving of all packets |
| 1378 | * \returns 0 on success; negative otherwise */ |
| 1379 | int osmo_sock_mcast_all_set(int fd, bool enable) |
| 1380 | { |
| 1381 | int domain, all = 0; |
| 1382 | |
| 1383 | if (enable) |
| 1384 | all = 1; |
| 1385 | |
| 1386 | domain = sock_get_domain(fd); |
| 1387 | if (domain < 0) |
| 1388 | return domain; |
| 1389 | |
| 1390 | switch (domain) { |
| 1391 | case AF_INET: |
| 1392 | #ifdef IP_MULTICAST_ALL |
| 1393 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)); |
| 1394 | #endif |
| 1395 | case AF_INET6: |
| 1396 | /* there seems no equivalent ?!? */ |
| 1397 | default: |
| 1398 | return -EINVAL; |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | /* FreeBSD calls the socket option differently */ |
| 1403 | #if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP) |
| 1404 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP |
| 1405 | #endif |
| 1406 | |
| 1407 | /*! Subscribe to the given IP multicast group |
| 1408 | * \param[in] fd file descriptor of related scoket |
| 1409 | * \param[in] grp_addr ASCII representation of the multicast group address |
| 1410 | * \returns 0 on success; negative otherwise */ |
| 1411 | int osmo_sock_mcast_subscribe(int fd, const char *grp_addr) |
| 1412 | { |
| 1413 | int rc, domain; |
| 1414 | struct ip_mreq mreq; |
| 1415 | struct ipv6_mreq mreq6; |
| 1416 | struct in6_addr i6a; |
| 1417 | |
| 1418 | domain = sock_get_domain(fd); |
| 1419 | if (domain < 0) |
| 1420 | return domain; |
| 1421 | |
| 1422 | switch (domain) { |
| 1423 | case AF_INET: |
| 1424 | memset(&mreq, 0, sizeof(mreq)); |
| 1425 | mreq.imr_multiaddr.s_addr = inet_addr(grp_addr); |
| 1426 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
| 1427 | return setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 1428 | #ifdef IPV6_ADD_MEMBERSHIP |
| 1429 | case AF_INET6: |
| 1430 | memset(&mreq6, 0, sizeof(mreq6)); |
| 1431 | rc = inet_pton(AF_INET6, grp_addr, (void *)&i6a); |
| 1432 | if (rc < 0) |
| 1433 | return -EINVAL; |
| 1434 | mreq6.ipv6mr_multiaddr = i6a; |
| 1435 | return setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)); |
| 1436 | #endif |
| 1437 | default: |
| 1438 | return -EINVAL; |
| 1439 | } |
| 1440 | } |
| 1441 | |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1442 | /*! Determine the matching local IP-address for a given remote IP-Address. |
| 1443 | * \param[out] local_ip caller provided memory for resulting local IP-address |
| 1444 | * \param[in] remote_ip remote IP-address |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1445 | * \returns 0 on success; negative otherwise |
| 1446 | * |
| 1447 | * The function accepts IPv4 and IPv6 address strings. The caller must provide |
| 1448 | * at least INET6_ADDRSTRLEN bytes for local_ip if an IPv6 is expected as |
| 1449 | * as result. For IPv4 addresses the required amount is INET_ADDRSTRLEN. */ |
| 1450 | int osmo_sock_local_ip(char *local_ip, const char *remote_ip) |
| 1451 | { |
| 1452 | int sfd; |
| 1453 | int rc; |
| 1454 | struct addrinfo addrinfo_hint; |
| 1455 | struct addrinfo *addrinfo = NULL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 1456 | struct sockaddr_storage local_addr; |
| 1457 | struct sockaddr_in *sin; |
| 1458 | struct sockaddr_in6 *sin6; |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1459 | socklen_t local_addr_len; |
| 1460 | uint16_t family; |
| 1461 | |
| 1462 | /* Find out the address family (AF_INET or AF_INET6?) */ |
| 1463 | memset(&addrinfo_hint, '\0', sizeof(addrinfo_hint)); |
Pau Espin Pedrol | 308ab79 | 2020-08-28 19:47:25 +0200 | [diff] [blame] | 1464 | addrinfo_hint.ai_family = AF_UNSPEC; |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1465 | addrinfo_hint.ai_flags = AI_NUMERICHOST; |
| 1466 | rc = getaddrinfo(remote_ip, NULL, &addrinfo_hint, &addrinfo); |
| 1467 | if (rc) |
| 1468 | return -EINVAL; |
| 1469 | family = addrinfo->ai_family; |
| 1470 | freeaddrinfo(addrinfo); |
| 1471 | |
| 1472 | /* Connect a dummy socket to trick the kernel into determining the |
| 1473 | * ip-address of the interface that would be used if we would send |
| 1474 | * out an actual packet */ |
| 1475 | sfd = osmo_sock_init2(family, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, remote_ip, 0, OSMO_SOCK_F_CONNECT); |
| 1476 | if (sfd < 0) |
| 1477 | return -EINVAL; |
| 1478 | |
| 1479 | /* Request the IP address of the interface that the kernel has |
| 1480 | * actually choosen. */ |
| 1481 | memset(&local_addr, 0, sizeof(local_addr)); |
| 1482 | local_addr_len = sizeof(local_addr); |
| 1483 | rc = getsockname(sfd, (struct sockaddr *)&local_addr, &local_addr_len); |
Philipp Maier | 8b7975b | 2018-01-22 15:38:07 +0100 | [diff] [blame] | 1484 | close(sfd); |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1485 | if (rc < 0) |
| 1486 | return -EINVAL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 1487 | |
| 1488 | switch (local_addr.ss_family) { |
| 1489 | case AF_INET: |
| 1490 | sin = (struct sockaddr_in*)&local_addr; |
| 1491 | if (!inet_ntop(AF_INET, &sin->sin_addr, local_ip, INET_ADDRSTRLEN)) |
| 1492 | return -EINVAL; |
| 1493 | break; |
| 1494 | case AF_INET6: |
| 1495 | sin6 = (struct sockaddr_in6*)&local_addr; |
Pau Espin Pedrol | 7bda854 | 2020-08-31 11:21:30 +0200 | [diff] [blame] | 1496 | if (!inet_ntop(AF_INET6, &sin6->sin6_addr, local_ip, INET6_ADDRSTRLEN)) |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 1497 | return -EINVAL; |
| 1498 | break; |
| 1499 | default: |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1500 | return -EINVAL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 1501 | } |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1502 | |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 1506 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1507 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 1508 | /*! @} */ |