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 | * |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 18 | */ |
| 19 | |
Pau Espin Pedrol | 88955fb | 2023-01-18 18:54:00 +0100 | [diff] [blame] | 20 | #include "config.h" |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 21 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 22 | /*! \addtogroup socket |
| 23 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 24 | * Osmocom socket convenience functions. |
| 25 | * |
| 26 | * \file socket.c */ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 27 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 28 | #ifdef HAVE_SYS_SOCKET_H |
| 29 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 30 | #include <osmocom/core/logging.h> |
| 31 | #include <osmocom/core/select.h> |
| 32 | #include <osmocom/core/socket.h> |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 33 | #include <osmocom/core/sockaddr_str.h> |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 34 | #include <osmocom/core/talloc.h> |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 35 | #include <osmocom/core/utils.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 36 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 37 | #include <sys/ioctl.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 38 | #include <sys/socket.h> |
| 39 | #include <sys/types.h> |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 40 | #include <sys/un.h> |
Harald Welte | 44b9926 | 2020-03-07 14:59:05 +0100 | [diff] [blame] | 41 | #include <net/if.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 42 | |
Holger Hans Peter Freyther | 4772348 | 2011-11-09 11:26:15 +0100 | [diff] [blame] | 43 | #include <netinet/in.h> |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 44 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 4772348 | 2011-11-09 11:26:15 +0100 | [diff] [blame] | 45 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 46 | #include <stdio.h> |
| 47 | #include <unistd.h> |
| 48 | #include <stdint.h> |
| 49 | #include <string.h> |
| 50 | #include <errno.h> |
| 51 | #include <netdb.h> |
| 52 | #include <ifaddrs.h> |
| 53 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 54 | #ifdef HAVE_LIBSCTP |
| 55 | #include <netinet/sctp.h> |
| 56 | #endif |
| 57 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 58 | static struct addrinfo *addrinfo_helper(uint16_t family, uint16_t type, uint8_t proto, |
| 59 | const char *host, uint16_t port, bool passive) |
| 60 | { |
Pau Espin Pedrol | ff42852 | 2019-10-10 17:38:16 +0200 | [diff] [blame] | 61 | struct addrinfo hints, *result, *rp; |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 62 | char portbuf[6]; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 63 | int rc; |
| 64 | |
| 65 | snprintf(portbuf, sizeof(portbuf), "%u", port); |
| 66 | memset(&hints, 0, sizeof(struct addrinfo)); |
| 67 | hints.ai_family = family; |
| 68 | if (type == SOCK_RAW) { |
| 69 | /* Workaround for glibc, that returns EAI_SERVICE (-8) if |
| 70 | * SOCK_RAW and IPPROTO_GRE is used. |
Pau Espin Pedrol | ff42852 | 2019-10-10 17:38:16 +0200 | [diff] [blame] | 71 | * http://sourceware.org/bugzilla/show_bug.cgi?id=15015 |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 72 | */ |
| 73 | hints.ai_socktype = SOCK_DGRAM; |
| 74 | hints.ai_protocol = IPPROTO_UDP; |
| 75 | } else { |
| 76 | hints.ai_socktype = type; |
| 77 | hints.ai_protocol = proto; |
| 78 | } |
Pau Espin Pedrol | 3119d47 | 2020-08-25 12:30:55 +0200 | [diff] [blame] | 79 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 80 | if (passive) |
| 81 | hints.ai_flags |= AI_PASSIVE; |
| 82 | |
| 83 | rc = getaddrinfo(host, portbuf, &hints, &result); |
| 84 | if (rc != 0) { |
Pau Espin Pedrol | ba828c3 | 2020-08-20 18:28:10 +0200 | [diff] [blame] | 85 | LOGP(DLGLOBAL, LOGL_ERROR, "getaddrinfo(%s, %u) failed: %s\n", |
| 86 | host, port, gai_strerror(rc)); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 87 | return NULL; |
| 88 | } |
| 89 | |
Pau Espin Pedrol | ff42852 | 2019-10-10 17:38:16 +0200 | [diff] [blame] | 90 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
| 91 | /* Workaround for glibc again */ |
| 92 | if (type == SOCK_RAW) { |
| 93 | rp->ai_socktype = SOCK_RAW; |
| 94 | rp->ai_protocol = proto; |
| 95 | } |
| 96 | } |
| 97 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 98 | return result; |
| 99 | } |
| 100 | |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 101 | #ifdef HAVE_LIBSCTP |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 102 | /*! Retrieve an array of addrinfo with specified hints, one for each host in the hosts array. |
| 103 | * \param[out] addrinfo array of addrinfo pointers, will be filled by the function on success. |
| 104 | * Its size must be at least the one of hosts. |
| 105 | * \param[in] family Socket family like AF_INET, AF_INET6. |
| 106 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM. |
| 107 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP. |
| 108 | * \param[in] hosts array of char pointers (strings) containing the addresses to query. |
| 109 | * \param[in] host_cnt length of the hosts array (in items). |
| 110 | * \param[in] port port number in host byte order. |
| 111 | * \param[in] passive whether to include the AI_PASSIVE flag in getaddrinfo() hints. |
| 112 | * \returns 0 is returned on success together with a filled addrinfo array; negative on error |
| 113 | */ |
| 114 | static int addrinfo_helper_multi(struct addrinfo **addrinfo, uint16_t family, uint16_t type, uint8_t proto, |
| 115 | const char **hosts, size_t host_cnt, uint16_t port, bool passive) |
| 116 | { |
Harald Welte | 0b08d51 | 2022-01-09 12:03:12 +0100 | [diff] [blame] | 117 | unsigned int i, j; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 118 | |
| 119 | for (i = 0; i < host_cnt; i++) { |
| 120 | addrinfo[i] = addrinfo_helper(family, type, proto, hosts[i], port, passive); |
| 121 | if (!addrinfo[i]) { |
| 122 | for (j = 0; j < i; j++) |
| 123 | freeaddrinfo(addrinfo[j]); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | } |
| 127 | return 0; |
| 128 | } |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 129 | #endif /* HAVE_LIBSCTP*/ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 130 | |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 131 | static int socket_helper_tail(int sfd, unsigned int flags) |
| 132 | { |
Harald Welte | c545ff6 | 2021-04-28 13:09:49 +0200 | [diff] [blame] | 133 | int rc, on = 1; |
| 134 | uint8_t dscp = GET_OSMO_SOCK_F_DSCP(flags); |
| 135 | uint8_t prio = GET_OSMO_SOCK_F_PRIO(flags); |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 136 | |
| 137 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 138 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
| 139 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 140 | "cannot set this socket unblocking: %s\n", |
| 141 | strerror(errno)); |
| 142 | close(sfd); |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | } |
| 146 | |
Harald Welte | c545ff6 | 2021-04-28 13:09:49 +0200 | [diff] [blame] | 147 | if (dscp) { |
| 148 | rc = osmo_sock_set_dscp(sfd, dscp); |
| 149 | if (rc) { |
| 150 | LOGP(DLGLOBAL, LOGL_ERROR, "cannot set IP DSCP of socket to %u: %s\n", |
| 151 | dscp, strerror(errno)); |
| 152 | /* we consider this a non-fatal error */ |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (prio) { |
| 157 | rc = osmo_sock_set_priority(sfd, prio); |
| 158 | if (rc) { |
| 159 | LOGP(DLGLOBAL, LOGL_ERROR, "cannot set priority of socket to %u: %s\n", |
| 160 | prio, strerror(errno)); |
| 161 | /* we consider this a non-fatal error */ |
| 162 | } |
| 163 | } |
| 164 | |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 168 | static int socket_helper(const struct addrinfo *rp, unsigned int flags) |
| 169 | { |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 170 | int sfd, rc; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 171 | |
| 172 | sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 173 | if (sfd == -1) { |
| 174 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 175 | "unable to create socket: %s\n", strerror(errno)); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 176 | return sfd; |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 177 | } |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 178 | |
| 179 | rc = socket_helper_tail(sfd, flags); |
| 180 | if (rc < 0) |
| 181 | return rc; |
| 182 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 183 | return sfd; |
| 184 | } |
| 185 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 186 | static int socket_helper_osa(const struct osmo_sockaddr *addr, uint16_t type, uint8_t proto, unsigned int flags) |
| 187 | { |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 188 | int sfd, rc; |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 189 | |
| 190 | sfd = socket(addr->u.sa.sa_family, type, proto); |
| 191 | if (sfd == -1) { |
| 192 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 193 | "unable to create socket: %s\n", strerror(errno)); |
| 194 | return sfd; |
| 195 | } |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 196 | |
| 197 | rc = socket_helper_tail(sfd, flags); |
| 198 | if (rc < 0) |
| 199 | return rc; |
| 200 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 201 | return sfd; |
| 202 | } |
| 203 | |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 204 | #ifdef HAVE_LIBSCTP |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 205 | /* Fill buf with a string representation of the address set, in the form: |
| 206 | * buf_len == 0: "()" |
| 207 | * buf_len == 1: "hostA" |
| 208 | * buf_len >= 2: (hostA|hostB|...|...) |
| 209 | */ |
| 210 | static int multiaddr_snprintf(char* buf, size_t buf_len, const char **hosts, size_t host_cnt) |
| 211 | { |
| 212 | int len = 0, offset = 0, rem = buf_len; |
Harald Welte | 0b08d51 | 2022-01-09 12:03:12 +0100 | [diff] [blame] | 213 | size_t i; |
| 214 | int ret; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 215 | char *after; |
| 216 | |
| 217 | if (buf_len < 3) |
| 218 | return -EINVAL; |
| 219 | |
| 220 | if (host_cnt != 1) { |
| 221 | ret = snprintf(buf, rem, "("); |
| 222 | if (ret < 0) |
| 223 | return ret; |
| 224 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 225 | } |
| 226 | for (i = 0; i < host_cnt; i++) { |
| 227 | if (host_cnt == 1) |
| 228 | after = ""; |
| 229 | else |
| 230 | after = (i == (host_cnt - 1)) ? ")" : "|"; |
| 231 | ret = snprintf(buf + offset, rem, "%s%s", hosts[i] ? : "0.0.0.0", after); |
| 232 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 233 | } |
| 234 | |
| 235 | return len; |
| 236 | } |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 237 | #endif /* HAVE_LIBSCTP */ |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 238 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 239 | static int osmo_sock_init_tail(int fd, uint16_t type, unsigned int flags) |
| 240 | { |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 241 | int rc; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 242 | |
| 243 | /* Make sure to call 'listen' on a bound, connection-oriented sock */ |
| 244 | if ((flags & (OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT)) == OSMO_SOCK_F_BIND) { |
| 245 | switch (type) { |
| 246 | case SOCK_STREAM: |
| 247 | case SOCK_SEQPACKET: |
| 248 | rc = listen(fd, 10); |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 249 | if (rc < 0) { |
| 250 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to listen on socket: %s\n", |
| 251 | strerror(errno)); |
Max | db7cb69 | 2023-02-11 21:35:21 +0300 | [diff] [blame] | 252 | return -errno; |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 253 | } |
| 254 | break; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 258 | if (flags & OSMO_SOCK_F_NO_MCAST_LOOP) { |
| 259 | rc = osmo_sock_mcast_loop_set(fd, false); |
| 260 | if (rc < 0) { |
| 261 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to disable multicast loop: %s\n", |
| 262 | strerror(errno)); |
| 263 | return rc; |
| 264 | } |
| 265 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 266 | |
Harald Welte | 37d204a | 2017-07-13 16:33:16 +0200 | [diff] [blame] | 267 | if (flags & OSMO_SOCK_F_NO_MCAST_ALL) { |
| 268 | rc = osmo_sock_mcast_all_set(fd, false); |
| 269 | if (rc < 0) { |
| 270 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to disable receive of all multicast: %s\n", |
| 271 | strerror(errno)); |
| 272 | /* do not abort here, as this is just an |
| 273 | * optional additional optimization that only |
| 274 | * exists on Linux only */ |
| 275 | } |
| 276 | } |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 277 | return 0; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 280 | /*! Initialize a socket (including bind and/or connect) |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 281 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 282 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 283 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 284 | * \param[in] local_host local host name or IP address in string form |
| 285 | * \param[in] local_port local port number in host byte order |
| 286 | * \param[in] remote_host remote host name or IP address in string form |
| 287 | * \param[in] remote_port remote port number in host byte order |
| 288 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 289 | * \returns socket file descriptor on success; negative on error |
| 290 | * |
| 291 | * This function creates a new socket of the designated \a family, \a |
| 292 | * type and \a proto and optionally binds it to the \a local_host and \a |
| 293 | * local_port as well as optionally connects it to the \a remote_host |
| 294 | * and \q remote_port, depending on the value * of \a flags parameter. |
| 295 | * |
| 296 | * As opposed to \ref osmo_sock_init(), this function allows to combine |
| 297 | * the \ref OSMO_SOCK_F_BIND and \ref OSMO_SOCK_F_CONNECT flags. This |
| 298 | * is useful if you want to connect to a remote host/port, but still |
| 299 | * want to bind that socket to either a specific local alias IP and/or a |
| 300 | * specific local source port. |
| 301 | * |
| 302 | * You must specify either \ref OSMO_SOCK_F_BIND, or \ref |
| 303 | * OSMO_SOCK_F_CONNECT, or both. |
| 304 | * |
| 305 | * If \ref OSMO_SOCK_F_NONBLOCK is specified, the socket will be set to |
| 306 | * non-blocking mode. |
| 307 | */ |
| 308 | int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto, |
| 309 | const char *local_host, uint16_t local_port, |
| 310 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 311 | { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 312 | struct addrinfo *local = NULL, *remote = NULL, *rp; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 313 | int sfd = -1, rc, on = 1; |
| 314 | |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 315 | bool local_ipv4 = false, local_ipv6 = false; |
| 316 | bool remote_ipv4 = false, remote_ipv6 = false; |
| 317 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 318 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 319 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 320 | "BIND or CONNECT flags\n"); |
| 321 | return -EINVAL; |
| 322 | } |
| 323 | |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 324 | /* figure out local address infos */ |
| 325 | if (flags & OSMO_SOCK_F_BIND) { |
| 326 | local = addrinfo_helper(family, type, proto, local_host, local_port, true); |
| 327 | if (!local) |
| 328 | return -EINVAL; |
| 329 | } |
| 330 | |
| 331 | /* figure out remote address infos */ |
| 332 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 333 | remote = addrinfo_helper(family, type, proto, remote_host, remote_port, false); |
| 334 | if (!remote) { |
| 335 | if (local) |
| 336 | freeaddrinfo(local); |
| 337 | |
| 338 | return -EINVAL; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* It must do a full run to ensure AF_UNSPEC does not fail. |
| 343 | * In case first local valid entry is IPv4 and only remote valid entry |
| 344 | * is IPv6 or vice versa */ |
| 345 | if (family == AF_UNSPEC) { |
| 346 | for (rp = local; rp != NULL; rp = rp->ai_next) { |
| 347 | switch (rp->ai_family) { |
| 348 | case AF_INET: |
| 349 | local_ipv4 = true; |
| 350 | break; |
| 351 | case AF_INET6: |
| 352 | local_ipv6 = true; |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | for (rp = remote; rp != NULL; rp = rp->ai_next) { |
| 358 | switch (rp->ai_family) { |
| 359 | case AF_INET: |
| 360 | remote_ipv4 = true; |
| 361 | break; |
| 362 | case AF_INET6: |
| 363 | remote_ipv6 = true; |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
Pau Espin Pedrol | d8cf52b | 2020-08-31 19:00:59 +0200 | [diff] [blame] | 368 | if ((flags & OSMO_SOCK_F_BIND) && (flags & OSMO_SOCK_F_CONNECT)) { |
| 369 | /* prioritize ipv6 as per RFC */ |
| 370 | if (local_ipv6 && remote_ipv6) |
| 371 | family = AF_INET6; |
| 372 | else if (local_ipv4 && remote_ipv4) |
| 373 | family = AF_INET; |
| 374 | else { |
| 375 | if (local) |
| 376 | freeaddrinfo(local); |
| 377 | if (remote) |
| 378 | freeaddrinfo(remote); |
| 379 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 380 | "Unable to find a common protocol (IPv4 or IPv6) " |
| 381 | "for local host: %s and remote host: %s.\n", |
| 382 | local_host, remote_host); |
| 383 | return -ENODEV; |
| 384 | } |
| 385 | } else if ((flags & OSMO_SOCK_F_BIND)) { |
| 386 | family = local_ipv6 ? AF_INET6 : AF_INET; |
| 387 | } else if ((flags & OSMO_SOCK_F_CONNECT)) { |
| 388 | family = remote_ipv6 ? AF_INET6 : AF_INET; |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 392 | /* figure out local side of socket */ |
| 393 | if (flags & OSMO_SOCK_F_BIND) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 394 | for (rp = local; rp != NULL; rp = rp->ai_next) { |
| 395 | /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ |
| 396 | if (rp->ai_family != family) |
| 397 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 398 | |
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 | |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 403 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 404 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 405 | &on, sizeof(on)); |
| 406 | if (rc < 0) { |
| 407 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 408 | "cannot setsockopt socket:" |
| 409 | " %s:%u: %s\n", |
| 410 | local_host, local_port, |
| 411 | strerror(errno)); |
| 412 | close(sfd); |
| 413 | continue; |
| 414 | } |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 415 | } |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 416 | |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 417 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 418 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n", |
| 419 | local_host, local_port, strerror(errno)); |
| 420 | close(sfd); |
| 421 | continue; |
| 422 | } |
| 423 | break; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 424 | } |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 425 | |
| 426 | freeaddrinfo(local); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 427 | if (rp == NULL) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 428 | if (remote) |
| 429 | freeaddrinfo(remote); |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 430 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: %s:%u\n", |
| 431 | local_host, local_port); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 432 | return -ENODEV; |
| 433 | } |
| 434 | } |
| 435 | |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 436 | /* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it |
| 437 | was already closed and func returned. If OSMO_SOCK_F_BIND is not |
| 438 | set, then sfd = -1 */ |
| 439 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 440 | /* figure out remote side of socket */ |
| 441 | if (flags & OSMO_SOCK_F_CONNECT) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 442 | for (rp = remote; rp != NULL; rp = rp->ai_next) { |
| 443 | /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ |
| 444 | if (rp->ai_family != family) |
| 445 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 446 | |
Harald Welte | 5cfa6dc | 2017-07-21 16:52:29 +0200 | [diff] [blame] | 447 | if (sfd < 0) { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 448 | sfd = socket_helper(rp, flags); |
| 449 | if (sfd < 0) |
| 450 | continue; |
| 451 | } |
| 452 | |
| 453 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 454 | if (rc != 0 && errno != EINPROGRESS) { |
| 455 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 456 | remote_host, remote_port, strerror(errno)); |
| 457 | /* We want to maintain the bind socket if bind was enabled */ |
| 458 | if (!(flags & OSMO_SOCK_F_BIND)) { |
| 459 | close(sfd); |
| 460 | sfd = -1; |
| 461 | } |
| 462 | continue; |
| 463 | } |
| 464 | break; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 465 | } |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 466 | |
| 467 | freeaddrinfo(remote); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 468 | if (rp == NULL) { |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 469 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable remote addr found for: %s:%u\n", |
| 470 | remote_host, remote_port); |
| 471 | if (sfd >= 0) |
| 472 | close(sfd); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 473 | return -ENODEV; |
| 474 | } |
| 475 | } |
| 476 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 477 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 478 | if (rc < 0) { |
| 479 | close(sfd); |
| 480 | sfd = -1; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 481 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 482 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 483 | return sfd; |
| 484 | } |
| 485 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 486 | #define _SOCKADDR_TO_STR(dest, sockaddr) do { \ |
Neels Hofmeyr | f3270f2 | 2021-12-17 14:21:29 +0100 | [diff] [blame] | 487 | if (osmo_sockaddr_str_from_sockaddr(dest, &sockaddr->u.sas)) \ |
| 488 | osmo_strlcpy((dest)->ip, "Invalid IP", 11); \ |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 489 | } while (0) |
| 490 | |
| 491 | /*! Initialize a socket (including bind and/or connect) |
| 492 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 493 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 494 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 495 | * \param[in] local local address |
| 496 | * \param[in] remote remote address |
| 497 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 498 | * \returns socket file descriptor on success; negative on error |
| 499 | * |
| 500 | * This function creates a new socket of the |
| 501 | * \a type and \a proto and optionally binds it to the \a local |
| 502 | * as well as optionally connects it to the \a remote |
| 503 | * depending on the value * of \a flags parameter. |
| 504 | * |
| 505 | * As opposed to \ref osmo_sock_init(), this function allows to combine |
| 506 | * the \ref OSMO_SOCK_F_BIND and \ref OSMO_SOCK_F_CONNECT flags. This |
| 507 | * is useful if you want to connect to a remote host/port, but still |
| 508 | * want to bind that socket to either a specific local alias IP and/or a |
| 509 | * specific local source port. |
| 510 | * |
| 511 | * You must specify either \ref OSMO_SOCK_F_BIND, or \ref |
| 512 | * OSMO_SOCK_F_CONNECT, or both. |
| 513 | * |
| 514 | * If \ref OSMO_SOCK_F_NONBLOCK is specified, the socket will be set to |
| 515 | * non-blocking mode. |
| 516 | */ |
| 517 | int osmo_sock_init_osa(uint16_t type, uint8_t proto, |
| 518 | const struct osmo_sockaddr *local, |
| 519 | const struct osmo_sockaddr *remote, |
| 520 | unsigned int flags) |
| 521 | { |
| 522 | int sfd = -1, rc, on = 1; |
Neels Hofmeyr | f3270f2 | 2021-12-17 14:21:29 +0100 | [diff] [blame] | 523 | struct osmo_sockaddr_str _sastr = {}; |
| 524 | struct osmo_sockaddr_str *sastr = &_sastr; |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 525 | |
| 526 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 527 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 528 | "BIND or CONNECT flags\n"); |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | |
| 532 | if ((flags & OSMO_SOCK_F_BIND) && !local) { |
| 533 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid argument. Cannot BIND when local is NULL\n"); |
| 534 | return -EINVAL; |
| 535 | } |
| 536 | |
| 537 | if ((flags & OSMO_SOCK_F_CONNECT) && !remote) { |
| 538 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid argument. Cannot CONNECT when remote is NULL\n"); |
| 539 | return -EINVAL; |
| 540 | } |
| 541 | |
| 542 | if ((flags & OSMO_SOCK_F_BIND) && |
| 543 | (flags & OSMO_SOCK_F_CONNECT) && |
| 544 | local->u.sa.sa_family != remote->u.sa.sa_family) { |
| 545 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: the family for " |
| 546 | "local and remote endpoint must be same.\n"); |
| 547 | return -EINVAL; |
| 548 | } |
| 549 | |
| 550 | /* figure out local side of socket */ |
| 551 | if (flags & OSMO_SOCK_F_BIND) { |
| 552 | sfd = socket_helper_osa(local, type, proto, flags); |
| 553 | if (sfd < 0) { |
| 554 | _SOCKADDR_TO_STR(sastr, local); |
Neels Hofmeyr | f3270f2 | 2021-12-17 14:21:29 +0100 | [diff] [blame] | 555 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: " OSMO_SOCKADDR_STR_FMT "\n", |
| 556 | OSMO_SOCKADDR_STR_FMT_ARGS(sastr)); |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 557 | return -ENODEV; |
| 558 | } |
| 559 | |
| 560 | if (proto != IPPROTO_UDP || (flags & OSMO_SOCK_F_UDP_REUSEADDR)) { |
| 561 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 562 | &on, sizeof(on)); |
| 563 | if (rc < 0) { |
Pau Espin Pedrol | c3b772b | 2023-07-06 12:25:26 +0200 | [diff] [blame] | 564 | int err = errno; |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 565 | _SOCKADDR_TO_STR(sastr, local); |
| 566 | LOGP(DLGLOBAL, LOGL_ERROR, |
Neels Hofmeyr | f3270f2 | 2021-12-17 14:21:29 +0100 | [diff] [blame] | 567 | "cannot setsockopt socket: " OSMO_SOCKADDR_STR_FMT ": %s\n", |
Pau Espin Pedrol | c3b772b | 2023-07-06 12:25:26 +0200 | [diff] [blame] | 568 | OSMO_SOCKADDR_STR_FMT_ARGS(sastr), strerror(err)); |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 569 | close(sfd); |
| 570 | return rc; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | if (bind(sfd, &local->u.sa, sizeof(struct osmo_sockaddr)) == -1) { |
Pau Espin Pedrol | c3b772b | 2023-07-06 12:25:26 +0200 | [diff] [blame] | 575 | int err = errno; |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 576 | _SOCKADDR_TO_STR(sastr, local); |
Neels Hofmeyr | f3270f2 | 2021-12-17 14:21:29 +0100 | [diff] [blame] | 577 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: " OSMO_SOCKADDR_STR_FMT ": %s\n", |
Pau Espin Pedrol | c3b772b | 2023-07-06 12:25:26 +0200 | [diff] [blame] | 578 | OSMO_SOCKADDR_STR_FMT_ARGS(sastr), strerror(err)); |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 579 | close(sfd); |
| 580 | return -1; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it |
| 585 | was already closed and func returned. If OSMO_SOCK_F_BIND is not |
| 586 | set, then sfd = -1 */ |
| 587 | |
| 588 | /* figure out remote side of socket */ |
| 589 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 590 | if (sfd < 0) { |
| 591 | sfd = socket_helper_osa(remote, type, proto, flags); |
| 592 | if (sfd < 0) { |
| 593 | return sfd; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | rc = connect(sfd, &remote->u.sa, sizeof(struct osmo_sockaddr)); |
| 598 | if (rc != 0 && errno != EINPROGRESS) { |
Pau Espin Pedrol | c3b772b | 2023-07-06 12:25:26 +0200 | [diff] [blame] | 599 | int err = errno; |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 600 | _SOCKADDR_TO_STR(sastr, remote); |
Neels Hofmeyr | f3270f2 | 2021-12-17 14:21:29 +0100 | [diff] [blame] | 601 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: " OSMO_SOCKADDR_STR_FMT ": %s\n", |
Pau Espin Pedrol | c3b772b | 2023-07-06 12:25:26 +0200 | [diff] [blame] | 602 | OSMO_SOCKADDR_STR_FMT_ARGS(sastr), strerror(err)); |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 603 | close(sfd); |
| 604 | return rc; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 609 | if (rc < 0) { |
| 610 | close(sfd); |
| 611 | sfd = -1; |
| 612 | } |
| 613 | |
| 614 | return sfd; |
| 615 | } |
| 616 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 617 | #ifdef HAVE_LIBSCTP |
| 618 | |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 619 | /* Check whether there's an addrinfo item in the addrinfo set with an IPv4 or IPv6 option */ |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 620 | static void addrinfo_has_v4v6addr(const struct addrinfo **result, size_t result_count, bool *has_v4, bool *has_v6) |
| 621 | { |
| 622 | size_t host_idx; |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 623 | const struct addrinfo *rp; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 624 | *has_v4 = false; |
| 625 | *has_v6 = false; |
| 626 | |
| 627 | for (host_idx = 0; host_idx < result_count; host_idx++) { |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 628 | for (rp = result[host_idx]; rp != NULL; rp = rp->ai_next) { |
| 629 | if (result[host_idx]->ai_family == AF_INET) |
| 630 | *has_v4 = true; |
| 631 | else if (result[host_idx]->ai_family == AF_INET6) |
| 632 | *has_v6 = true; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | /* Check whether there's an addrinfo item in the addrinfo set with only an IPv4 or IPv6 option */ |
| 638 | static void addrinfo_has_v4v6only_addr(const struct addrinfo **result, size_t result_count, bool *has_v4only, bool *has_v6only) |
| 639 | { |
| 640 | size_t host_idx; |
| 641 | const struct addrinfo *rp; |
| 642 | *has_v4only = false; |
| 643 | *has_v6only = false; |
| 644 | |
| 645 | for (host_idx = 0; host_idx < result_count; host_idx++) { |
| 646 | bool has_v4 = false; |
| 647 | bool has_v6 = false; |
| 648 | for (rp = result[host_idx]; rp != NULL; rp = rp->ai_next) { |
| 649 | if (rp->ai_family == AF_INET6) |
| 650 | has_v6 = true; |
| 651 | else |
| 652 | has_v4 = true; |
| 653 | } |
| 654 | if (has_v4 && !has_v6) |
| 655 | *has_v4only = true; |
| 656 | else if (has_v6 && !has_v4) |
| 657 | *has_v6only = true; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
Pau Espin Pedrol | 4f463c5 | 2020-08-28 14:32:02 +0200 | [diff] [blame] | 661 | /* Check whether there's an IPv6 with IN6ADDR_ANY_INIT ("::") */ |
| 662 | static bool addrinfo_has_in6addr_any(const struct addrinfo **result, size_t result_count) |
| 663 | { |
| 664 | size_t host_idx; |
| 665 | struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 666 | const struct addrinfo *rp; |
Pau Espin Pedrol | 4f463c5 | 2020-08-28 14:32:02 +0200 | [diff] [blame] | 667 | |
| 668 | for (host_idx = 0; host_idx < result_count; host_idx++) { |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 669 | for (rp = result[host_idx]; rp != NULL; rp = rp->ai_next) { |
| 670 | if (rp->ai_family != AF_INET6) |
| 671 | continue; |
| 672 | if (memcmp(&((struct sockaddr_in6 *)rp->ai_addr)->sin6_addr, |
| 673 | &in6addr_any, sizeof(in6addr_any)) == 0) |
| 674 | return true; |
| 675 | } |
Pau Espin Pedrol | 4f463c5 | 2020-08-28 14:32:02 +0200 | [diff] [blame] | 676 | } |
| 677 | return false; |
| 678 | } |
| 679 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 680 | static int socket_helper_multiaddr(uint16_t family, uint16_t type, uint8_t proto, unsigned int flags) |
| 681 | { |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 682 | int sfd, rc; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 683 | |
| 684 | sfd = socket(family, type, proto); |
| 685 | if (sfd == -1) { |
| 686 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 687 | "Unable to create socket: %s\n", strerror(errno)); |
| 688 | return sfd; |
| 689 | } |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 690 | |
| 691 | rc = socket_helper_tail(sfd, flags); |
| 692 | if (rc < 0) |
| 693 | return rc; |
| 694 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 695 | return sfd; |
| 696 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 697 | |
| 698 | /* 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] | 699 | * for each host in addrs_buf. */ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 700 | static int addrinfo_to_sockaddr(uint16_t family, const struct addrinfo **result, |
Harald Welte | 0b08d51 | 2022-01-09 12:03:12 +0100 | [diff] [blame] | 701 | const char **hosts, unsigned int host_cont, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 702 | uint8_t *addrs_buf, size_t addrs_buf_len) { |
| 703 | size_t host_idx, offset = 0; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 704 | const struct addrinfo *rp; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 705 | |
| 706 | for (host_idx = 0; host_idx < host_cont; host_idx++) { |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 707 | /* Addresses are ordered based on RFC 3484, see man getaddrinfo */ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 708 | for (rp = result[host_idx]; rp != NULL; rp = rp->ai_next) { |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 709 | if (family == AF_UNSPEC || rp->ai_family == family) |
| 710 | break; |
| 711 | } |
| 712 | if (!rp && family == AF_INET6) { |
| 713 | /* See if we can find an AF_INET addr for the AF_INET6 socket instead: */ |
| 714 | for (rp = result[host_idx]; rp != NULL; rp = rp->ai_next) { |
| 715 | if (rp->ai_family == AF_INET) |
| 716 | break; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 717 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 718 | } |
| 719 | if (!rp) { /* No addr could be bound for this host! */ |
| 720 | LOGP(DLGLOBAL, LOGL_ERROR, "No suitable remote address found for host: %s\n", |
| 721 | hosts[host_idx]); |
| 722 | return -ENODEV; |
| 723 | } |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 724 | if (offset + rp->ai_addrlen > addrs_buf_len) { |
| 725 | LOGP(DLGLOBAL, LOGL_ERROR, "Output buffer to small: %zu\n", |
| 726 | addrs_buf_len); |
| 727 | return -ENOSPC; |
| 728 | } |
| 729 | memcpy(addrs_buf + offset, rp->ai_addr, rp->ai_addrlen); |
| 730 | offset += rp->ai_addrlen; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 731 | } |
| 732 | return 0; |
| 733 | } |
| 734 | |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 735 | static int setsockopt_sctp_auth_supported(int fd, uint32_t val) |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 736 | { |
| 737 | #ifdef SCTP_AUTH_SUPPORTED |
| 738 | struct sctp_assoc_value assoc_val = { |
| 739 | .assoc_id = SCTP_FUTURE_ASSOC, |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 740 | .assoc_value = val, |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 741 | }; |
| 742 | return setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_SUPPORTED, &assoc_val, sizeof(assoc_val)); |
| 743 | #else |
| 744 | #pragma message "setsockopt(SCTP_AUTH_SUPPORTED) not supported! some SCTP features may not be available!" |
| 745 | LOGP(DLGLOBAL, LOGL_NOTICE, "Built without support for setsockopt(SCTP_AUTH_SUPPORTED), skipping\n"); |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 746 | return -ENOTSUP; |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 747 | #endif |
| 748 | } |
| 749 | |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 750 | static int setsockopt_sctp_asconf_supported(int fd, uint32_t val) |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 751 | { |
| 752 | #ifdef SCTP_ASCONF_SUPPORTED |
| 753 | struct sctp_assoc_value assoc_val = { |
| 754 | .assoc_id = SCTP_FUTURE_ASSOC, |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 755 | .assoc_value = val, |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 756 | }; |
| 757 | return setsockopt(fd, IPPROTO_SCTP, SCTP_ASCONF_SUPPORTED, &assoc_val, sizeof(assoc_val)); |
| 758 | #else |
| 759 | #pragma message "setsockopt(SCTP_ASCONF_SUPPORTED) not supported! some SCTP features may not be available!" |
| 760 | LOGP(DLGLOBAL, LOGL_NOTICE, "Built without support for setsockopt(SCTP_ASCONF_SUPPORTED), skipping\n"); |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 761 | return -ENOTSUP; |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 762 | #endif |
| 763 | } |
| 764 | |
Pau Espin Pedrol | a45b0be | 2023-09-08 13:42:50 +0200 | [diff] [blame] | 765 | static int setsockopt_sctp_initmsg(int fd, const struct osmo_sock_init2_multiaddr_pars *pars) |
| 766 | { |
| 767 | if (!pars->sctp.sockopt_initmsg.num_ostreams_present && |
| 768 | !pars->sctp.sockopt_initmsg.max_instreams_present && |
| 769 | !pars->sctp.sockopt_initmsg.max_attempts_present && |
| 770 | !pars->sctp.sockopt_initmsg.max_init_timeo_present) |
| 771 | return 0; /* nothing to set/do */ |
| 772 | |
| 773 | #ifdef SCTP_INITMSG |
| 774 | struct sctp_initmsg si = {0}; |
| 775 | socklen_t si_len = sizeof(si); |
| 776 | int rc; |
| 777 | |
| 778 | /* If at least one field not present, obtain current value from kernel: */ |
| 779 | if (!pars->sctp.sockopt_initmsg.num_ostreams_present || |
| 780 | !pars->sctp.sockopt_initmsg.max_instreams_present || |
| 781 | !pars->sctp.sockopt_initmsg.max_attempts_present || |
| 782 | !pars->sctp.sockopt_initmsg.max_init_timeo_present) { |
| 783 | rc = getsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &si, &si_len); |
| 784 | if (rc < 0) |
| 785 | return rc; |
| 786 | } |
| 787 | |
| 788 | if (pars->sctp.sockopt_initmsg.num_ostreams_present) |
| 789 | si.sinit_num_ostreams = pars->sctp.sockopt_initmsg.num_ostreams_value; |
| 790 | if (pars->sctp.sockopt_initmsg.max_instreams_present) |
| 791 | si.sinit_max_instreams = pars->sctp.sockopt_initmsg.max_instreams_value; |
| 792 | if (pars->sctp.sockopt_initmsg.max_attempts_present) |
| 793 | si.sinit_max_attempts = pars->sctp.sockopt_initmsg.max_attempts_value; |
| 794 | if (pars->sctp.sockopt_initmsg.max_init_timeo_present) |
| 795 | si.sinit_max_init_timeo = pars->sctp.sockopt_initmsg.max_init_timeo_value; |
| 796 | |
| 797 | return setsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &si, sizeof(si)); |
| 798 | #else |
| 799 | #pragma message "setsockopt(SCTP_INITMSG) not supported! some SCTP features may not be available!" |
| 800 | LOGP(DLGLOBAL, LOGL_NOTICE, "Built without support for setsockopt(SCTP_INITMSG), skipping\n"); |
| 801 | return -ENOTSUP |
| 802 | #endif |
| 803 | } |
| 804 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 805 | /*! Initialize a socket (including bind and/or connect) with multiple local or remote addresses. |
| 806 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 807 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 808 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 809 | * \param[in] local_hosts array of char pointers (strings), each containing local host name or IP address in string form |
| 810 | * \param[in] local_hosts_cnt length of local_hosts (in items) |
| 811 | * \param[in] local_port local port number in host byte order |
| 812 | * \param[in] remote_host array of char pointers (strings), each containing remote host name or IP address in string form |
| 813 | * \param[in] remote_hosts_cnt length of remote_hosts (in items) |
| 814 | * \param[in] remote_port remote port number in host byte order |
| 815 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 816 | * \returns socket file descriptor on success; negative on error |
| 817 | * |
| 818 | * This function is similar to \ref osmo_sock_init2(), but can be passed an |
| 819 | * array of local or remote addresses for protocols supporting multiple |
| 820 | * addresses per socket, like SCTP (currently only one supported). This function |
| 821 | * should not be used by protocols not supporting this kind of features, but |
| 822 | * rather \ref osmo_sock_init2() should be used instead. |
| 823 | * See \ref osmo_sock_init2() for more information on flags and general behavior. |
| 824 | */ |
| 825 | int osmo_sock_init2_multiaddr(uint16_t family, uint16_t type, uint8_t proto, |
| 826 | const char **local_hosts, size_t local_hosts_cnt, uint16_t local_port, |
| 827 | const char **remote_hosts, size_t remote_hosts_cnt, uint16_t remote_port, |
| 828 | unsigned int flags) |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 829 | { |
| 830 | return osmo_sock_init2_multiaddr2(family, type, proto, local_hosts, local_hosts_cnt, local_port, |
| 831 | remote_hosts, remote_hosts_cnt, remote_port, flags, NULL); |
| 832 | } |
| 833 | |
| 834 | /*! Initialize a socket (including bind and/or connect) with multiple local or remote addresses. |
| 835 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 836 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 837 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 838 | * \param[in] local_hosts array of char pointers (strings), each containing local host name or IP address in string form |
| 839 | * \param[in] local_hosts_cnt length of local_hosts (in items) |
| 840 | * \param[in] local_port local port number in host byte order |
| 841 | * \param[in] remote_host array of char pointers (strings), each containing remote host name or IP address in string form |
| 842 | * \param[in] remote_hosts_cnt length of remote_hosts (in items) |
| 843 | * \param[in] remote_port remote port number in host byte order |
| 844 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 845 | * \param[in] pars Extra parameters for multi-address specific protocols, such as SCTP. Can be NULL. |
| 846 | * \returns socket file descriptor on success; negative on error |
| 847 | * |
| 848 | * This function is similar to \ref osmo_sock_init2(), but can be passed an |
| 849 | * array of local or remote addresses for protocols supporting multiple |
| 850 | * addresses per socket, like SCTP (currently only one supported). This function |
| 851 | * should not be used by protocols not supporting this kind of features, but |
| 852 | * rather \ref osmo_sock_init2() should be used instead. |
| 853 | * See \ref osmo_sock_init2() for more information on flags and general behavior. |
| 854 | * |
| 855 | * pars: If "pars" parameter is passed to the function, sctp.version shall be set to 0. |
| 856 | */ |
| 857 | int osmo_sock_init2_multiaddr2(uint16_t family, uint16_t type, uint8_t proto, |
| 858 | const char **local_hosts, size_t local_hosts_cnt, uint16_t local_port, |
| 859 | const char **remote_hosts, size_t remote_hosts_cnt, uint16_t remote_port, |
| 860 | unsigned int flags, struct osmo_sock_init2_multiaddr_pars *pars) |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 861 | |
| 862 | { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 863 | 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] | 864 | int sfd = -1, rc, on = 1; |
Harald Welte | 0b08d51 | 2022-01-09 12:03:12 +0100 | [diff] [blame] | 865 | unsigned int i; |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 866 | bool loc_has_v4addr = false, loc_has_v6addr = false; |
| 867 | bool rem_has_v4addr = false, rem_has_v6addr = false; |
| 868 | bool loc_has_v4only_addr, rem_has_v4only_addr; |
| 869 | bool loc_has_v6only_addr, rem_has_v6only_addr; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 870 | struct sockaddr_in6 addrs_buf[OSMO_SOCK_MAX_ADDRS]; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 871 | char strbuf[512]; |
| 872 | |
| 873 | /* TODO: So far this function is only aimed for SCTP, but could be |
| 874 | reused in the future for other protocols with multi-addr support */ |
| 875 | if (proto != IPPROTO_SCTP) |
| 876 | return -ENOTSUP; |
| 877 | |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 878 | if (pars && pars->sctp.version != 0) |
| 879 | return -EINVAL; |
| 880 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 881 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 882 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 883 | "BIND or CONNECT flags\n"); |
| 884 | return -EINVAL; |
| 885 | } |
| 886 | |
| 887 | if (((flags & OSMO_SOCK_F_BIND) && !local_hosts_cnt) || |
| 888 | ((flags & OSMO_SOCK_F_CONNECT) && !remote_hosts_cnt) || |
| 889 | local_hosts_cnt > OSMO_SOCK_MAX_ADDRS || |
| 890 | remote_hosts_cnt > OSMO_SOCK_MAX_ADDRS) |
| 891 | return -EINVAL; |
| 892 | |
| 893 | /* figure out local side of socket */ |
| 894 | if (flags & OSMO_SOCK_F_BIND) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 895 | rc = addrinfo_helper_multi(res_loc, family, type, proto, local_hosts, |
| 896 | local_hosts_cnt, local_port, true); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 897 | if (rc < 0) |
| 898 | return -EINVAL; |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 899 | /* Figure out if there's any IPv4 or IPv6 entry in the result set */ |
| 900 | addrinfo_has_v4v6addr((const struct addrinfo **)res_loc, local_hosts_cnt, |
| 901 | &loc_has_v4addr, &loc_has_v6addr); |
| 902 | /* Figure out if there's any IPv4-only or IPv6-only addr in the result set */ |
| 903 | addrinfo_has_v4v6only_addr((const struct addrinfo **)res_loc, local_hosts_cnt, |
| 904 | &loc_has_v4only_addr, &loc_has_v6only_addr); |
| 905 | if (family == AF_INET && loc_has_v6only_addr) { |
| 906 | LOGP(DLGLOBAL, LOGL_ERROR, "Cannot bind an IPv6 address to an AF_INET socket\n"); |
| 907 | rc = -EINVAL; |
Pau Espin Pedrol | d2e8f67 | 2023-12-08 20:07:38 +0100 | [diff] [blame] | 908 | goto ret_freeaddrinfo_loc; |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 909 | } |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 910 | } |
| 911 | /* figure out remote side of socket */ |
| 912 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 913 | rc = addrinfo_helper_multi(res_rem, family, type, proto, remote_hosts, |
| 914 | remote_hosts_cnt, remote_port, false); |
Pau Espin Pedrol | 4541cf2 | 2020-08-25 11:22:09 +0200 | [diff] [blame] | 915 | if (rc < 0) { |
| 916 | rc = -EINVAL; |
| 917 | goto ret_freeaddrinfo_loc; |
| 918 | } |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 919 | /* Figure out if there's any IPv4 or IPv6 entry in the result set */ |
| 920 | addrinfo_has_v4v6addr((const struct addrinfo **)res_rem, remote_hosts_cnt, |
| 921 | &rem_has_v4addr, &rem_has_v6addr); |
| 922 | /* Figure out if there's any IPv4-only or IPv6-only addr in the result set */ |
| 923 | addrinfo_has_v4v6only_addr((const struct addrinfo **)res_rem, remote_hosts_cnt, |
| 924 | &rem_has_v4only_addr, &rem_has_v6only_addr); |
| 925 | if (family == AF_INET && rem_has_v6only_addr) { |
| 926 | LOGP(DLGLOBAL, LOGL_ERROR, "Cannot connect to an IPv6 address in an AF_INET socket\n"); |
| 927 | rc = -EINVAL; |
| 928 | goto ret_freeaddrinfo; |
| 929 | } |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 932 | /* Find out the socket family now if not established by caller: |
| 933 | * Both are checked here through "or" here to account for "bind flag set, |
| 934 | * connect flag not set" and viceversa. */ |
| 935 | if (family == AF_UNSPEC) { |
| 936 | if (!loc_has_v6addr && !rem_has_v6addr) |
| 937 | family = AF_INET; |
| 938 | else |
| 939 | family = AF_INET6; |
| 940 | } |
| 941 | |
| 942 | /* if both sets are used, make sure there's at least 1 address of the |
| 943 | * same type on each set so that SCTP INIT/INIT-ACK can work. */ |
| 944 | if (family == AF_INET6 && ((flags & OSMO_SOCK_F_BIND) && (flags & OSMO_SOCK_F_CONNECT)) && |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 945 | (loc_has_v4addr != rem_has_v4addr || loc_has_v6addr != rem_has_v6addr)) { |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 946 | if (!addrinfo_has_in6addr_any((const struct addrinfo **)res_loc, local_hosts_cnt)) { |
| 947 | LOGP(DLGLOBAL, LOGL_ERROR, "Invalid v4 vs v6 in local vs remote addresses: " |
| 948 | "local:%s%s remote:%s%s\n", |
| 949 | loc_has_v4addr ? " v4" : "", loc_has_v6addr ? " v6" : "", |
| 950 | rem_has_v4addr ? " v4" : "", rem_has_v6addr ? " v6" : ""); |
| 951 | rc = -EINVAL; |
| 952 | goto ret_freeaddrinfo; |
| 953 | } |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 954 | } |
| 955 | |
Pau Espin Pedrol | 1c797fc | 2023-12-05 18:28:28 +0100 | [diff] [blame] | 956 | sfd = socket_helper_multiaddr(family, type, proto, flags); |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 957 | if (sfd < 0) { |
| 958 | rc = sfd; |
| 959 | goto ret_freeaddrinfo; |
| 960 | } |
| 961 | |
Pau Espin Pedrol | cb5cec2 | 2023-12-05 19:57:03 +0100 | [diff] [blame] | 962 | if (pars) { |
| 963 | if (pars->sctp.sockopt_auth_supported.set) { |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 964 | /* RFC 5061 4.2.7: ASCONF also requires AUTH feature. */ |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 965 | rc = setsockopt_sctp_auth_supported(sfd, pars->sctp.sockopt_auth_supported.value); |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 966 | if (rc < 0) { |
| 967 | int err = errno; |
| 968 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 969 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 970 | "cannot setsockopt(SCTP_AUTH_SUPPORTED) socket: %s:%u: %s\n", |
| 971 | strbuf, local_port, strerror(err)); |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 972 | if (pars->sctp.sockopt_auth_supported.abort_on_failure) |
| 973 | goto ret_close; |
Pau Espin Pedrol | b437f80 | 2023-08-28 13:56:25 +0200 | [diff] [blame] | 974 | /* do not fail, some features such as Peer Primary Address won't be available |
| 975 | * unless configured system-wide through sysctl */ |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 976 | } |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 977 | } |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 978 | |
Pau Espin Pedrol | cb5cec2 | 2023-12-05 19:57:03 +0100 | [diff] [blame] | 979 | if (pars->sctp.sockopt_asconf_supported.set) { |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 980 | rc = setsockopt_sctp_asconf_supported(sfd, pars->sctp.sockopt_asconf_supported.value); |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 981 | if (rc < 0) { |
| 982 | int err = errno; |
| 983 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 984 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 985 | "cannot setsockopt(SCTP_ASCONF_SUPPORTED) socket: %s:%u: %s\n", |
| 986 | strbuf, local_port, strerror(err)); |
Pau Espin Pedrol | 658c509 | 2023-09-08 13:02:14 +0200 | [diff] [blame] | 987 | if (pars->sctp.sockopt_asconf_supported.abort_on_failure) |
| 988 | goto ret_close; |
Pau Espin Pedrol | b437f80 | 2023-08-28 13:56:25 +0200 | [diff] [blame] | 989 | /* do not fail, some features such as Peer Primary Address won't be available |
| 990 | * unless configured system-wide through sysctl */ |
Pau Espin Pedrol | e83227f | 2023-08-08 19:32:56 +0200 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
Pau Espin Pedrol | cb5cec2 | 2023-12-05 19:57:03 +0100 | [diff] [blame] | 994 | if (pars->sctp.sockopt_initmsg.set) { |
Pau Espin Pedrol | a45b0be | 2023-09-08 13:42:50 +0200 | [diff] [blame] | 995 | rc = setsockopt_sctp_initmsg(sfd, pars); |
| 996 | if (rc < 0) { |
| 997 | int err = errno; |
| 998 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 999 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 1000 | "cannot setsockopt(SCTP_INITMSG) socket: %s:%u: %s\n", |
| 1001 | strbuf, local_port, strerror(err)); |
| 1002 | if (pars->sctp.sockopt_initmsg.abort_on_failure) |
| 1003 | goto ret_close; |
| 1004 | /* do not fail, some parameters will be left as the global default */ |
| 1005 | } |
| 1006 | } |
Pau Espin Pedrol | cb5cec2 | 2023-12-05 19:57:03 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | if (flags & OSMO_SOCK_F_BIND) { |
| 1010 | /* Since so far we only allow IPPROTO_SCTP in this function, |
| 1011 | no need to check below for "proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR" */ |
| 1012 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 1013 | &on, sizeof(on)); |
| 1014 | if (rc < 0) { |
| 1015 | int err = errno; |
| 1016 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 1017 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 1018 | "cannot setsockopt socket:" |
| 1019 | " %s:%u: %s\n", |
| 1020 | strbuf, local_port, |
| 1021 | strerror(err)); |
| 1022 | goto ret_close; |
| 1023 | } |
Pau Espin Pedrol | a45b0be | 2023-09-08 13:42:50 +0200 | [diff] [blame] | 1024 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 1025 | /* Build array of addresses taking first entry for each host. |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1026 | TODO: Ideally we should use backtracking storing last used |
| 1027 | indexes and trying next combination if connect() fails .*/ |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 1028 | /* 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] | 1029 | rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)res_loc, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 1030 | local_hosts, local_hosts_cnt, |
| 1031 | (uint8_t*)addrs_buf, sizeof(addrs_buf)); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1032 | if (rc < 0) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 1033 | rc = -ENODEV; |
| 1034 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1035 | } |
| 1036 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 1037 | rc = sctp_bindx(sfd, (struct sockaddr *)addrs_buf, local_hosts_cnt, SCTP_BINDX_ADD_ADDR); |
| 1038 | if (rc == -1) { |
Pau Espin Pedrol | c3b772b | 2023-07-06 12:25:26 +0200 | [diff] [blame] | 1039 | int err = errno; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1040 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 1041 | LOGP(DLGLOBAL, LOGL_NOTICE, "unable to bind socket: %s:%u: %s\n", |
Pau Espin Pedrol | c3b772b | 2023-07-06 12:25:26 +0200 | [diff] [blame] | 1042 | strbuf, local_port, strerror(err)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 1043 | rc = -ENODEV; |
| 1044 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1045 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1048 | if (flags & OSMO_SOCK_F_CONNECT) { |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1049 | /* Build array of addresses taking first of same family for each host. |
| 1050 | TODO: Ideally we should use backtracking storing last used |
| 1051 | indexes and trying next combination if connect() fails .*/ |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 1052 | rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)res_rem, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 1053 | remote_hosts, remote_hosts_cnt, |
| 1054 | (uint8_t*)addrs_buf, sizeof(addrs_buf)); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1055 | if (rc < 0) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 1056 | rc = -ENODEV; |
| 1057 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 1060 | 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] | 1061 | if (rc != 0 && errno != EINPROGRESS) { |
Pau Espin Pedrol | c3b772b | 2023-07-06 12:25:26 +0200 | [diff] [blame] | 1062 | int err = errno; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1063 | multiaddr_snprintf(strbuf, sizeof(strbuf), remote_hosts, remote_hosts_cnt); |
| 1064 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
Pau Espin Pedrol | c3b772b | 2023-07-06 12:25:26 +0200 | [diff] [blame] | 1065 | strbuf, remote_port, strerror(err)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 1066 | rc = -ENODEV; |
| 1067 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1068 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 1072 | if (rc < 0) { |
| 1073 | close(sfd); |
| 1074 | sfd = -1; |
| 1075 | } |
| 1076 | |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 1077 | rc = sfd; |
| 1078 | goto ret_freeaddrinfo; |
| 1079 | |
| 1080 | ret_close: |
| 1081 | if (sfd >= 0) |
| 1082 | close(sfd); |
| 1083 | ret_freeaddrinfo: |
Pau Espin Pedrol | 4541cf2 | 2020-08-25 11:22:09 +0200 | [diff] [blame] | 1084 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 1085 | for (i = 0; i < remote_hosts_cnt; i++) |
| 1086 | freeaddrinfo(res_rem[i]); |
| 1087 | } |
| 1088 | ret_freeaddrinfo_loc: |
| 1089 | if (flags & OSMO_SOCK_F_BIND) { |
| 1090 | for (i = 0; i < local_hosts_cnt; i++) |
| 1091 | freeaddrinfo(res_loc[i]); |
| 1092 | } |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 1093 | return rc; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 1094 | } |
| 1095 | #endif /* HAVE_LIBSCTP */ |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 1096 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1097 | /*! Initialize a socket (including bind/connect) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1098 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 1099 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1100 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1101 | * \param[in] host remote host name or IP address in string form |
| 1102 | * \param[in] port remote port number in host byte order |
| 1103 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1104 | * \returns socket file descriptor on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1105 | * |
| 1106 | * This function creates a new socket of the designated \a family, \a |
| 1107 | * type and \a proto and optionally binds or connects it, depending on |
| 1108 | * the value of \a flags parameter. |
| 1109 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1110 | 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] | 1111 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1112 | { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 1113 | struct addrinfo *result, *rp; |
Pau Espin Pedrol | 6fe865d | 2021-07-15 13:08:08 +0200 | [diff] [blame] | 1114 | int sfd = -1; /* initialize to avoid uninitialized false warnings on some gcc versions (11.1.0) */ |
| 1115 | int on = 1; |
| 1116 | int rc; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1117 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1118 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 1119 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 1120 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: both bind and connect flags set:" |
Neels Hofmeyr | b7f191f | 2016-08-29 11:22:03 +0200 | [diff] [blame] | 1121 | " %s:%u\n", host, port); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1122 | return -EINVAL; |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 1123 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1124 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 1125 | 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] | 1126 | if (!result) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1127 | return -EINVAL; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1128 | |
| 1129 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 1130 | sfd = socket_helper(rp, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1131 | if (sfd == -1) |
| 1132 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 1133 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1134 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 1135 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 1136 | if (rc != 0 && errno != EINPROGRESS) { |
| 1137 | close(sfd); |
| 1138 | continue; |
| 1139 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1140 | } else { |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 1141 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 1142 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 1143 | &on, sizeof(on)); |
| 1144 | if (rc < 0) { |
| 1145 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 1146 | "cannot setsockopt socket:" |
| 1147 | " %s:%u: %s\n", |
| 1148 | host, port, strerror(errno)); |
| 1149 | close(sfd); |
| 1150 | continue; |
| 1151 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1152 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 1153 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 1154 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket:" |
| 1155 | "%s:%u: %s\n", |
| 1156 | host, port, strerror(errno)); |
| 1157 | close(sfd); |
| 1158 | continue; |
| 1159 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1160 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 1161 | break; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1162 | } |
| 1163 | freeaddrinfo(result); |
| 1164 | |
| 1165 | if (rp == NULL) { |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 1166 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable addr found for: %s:%u\n", |
| 1167 | host, port); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1168 | return -ENODEV; |
| 1169 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 1170 | |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 1171 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 1172 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 1173 | if (rc < 0) { |
| 1174 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 1175 | "cannot setsockopt socket: %s:%u: %s\n", host, |
| 1176 | port, strerror(errno)); |
| 1177 | close(sfd); |
| 1178 | sfd = -1; |
| 1179 | } |
Philipp Maier | 0659c5d | 2018-08-01 12:43:08 +0200 | [diff] [blame] | 1180 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 1181 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 1182 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 1183 | if (rc < 0) { |
| 1184 | close(sfd); |
| 1185 | sfd = -1; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 1186 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 1187 | |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 1188 | return sfd; |
| 1189 | } |
| 1190 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1191 | /*! fill \ref osmo_fd for a give sfd |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 1192 | * \param[out] ofd file descriptor (will be filled in) |
| 1193 | * \param[in] sfd socket file descriptor |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1194 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1195 | * \returns socket fd on success; negative on error |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 1196 | * |
| 1197 | * This function fills the \a ofd structure. |
| 1198 | */ |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1199 | static inline int osmo_fd_init_ofd(struct osmo_fd *ofd, int sfd, unsigned int flags) |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 1200 | { |
| 1201 | int rc; |
| 1202 | |
| 1203 | if (sfd < 0) |
| 1204 | return sfd; |
| 1205 | |
| 1206 | ofd->fd = sfd; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 1207 | ofd->when = OSMO_FD_READ; |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 1208 | |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1209 | /* if we're doing a non-blocking connect, the completion will be signaled |
| 1210 | * by marking the fd as WRITE-able. So in this exceptional case, we're |
| 1211 | * also interested in when the socket becomes write-able */ |
| 1212 | if ((flags & (OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK)) == |
| 1213 | (OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK)) { |
| 1214 | ofd->when |= OSMO_FD_WRITE; |
| 1215 | } |
| 1216 | |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 1217 | rc = osmo_fd_register(ofd); |
| 1218 | if (rc < 0) { |
| 1219 | close(sfd); |
| 1220 | return rc; |
| 1221 | } |
| 1222 | |
| 1223 | return sfd; |
| 1224 | } |
| 1225 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1226 | /*! Initialize a socket and fill \ref osmo_fd |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 1227 | * \param[out] ofd file descriptor (will be filled in) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1228 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 1229 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1230 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1231 | * \param[in] host remote host name or IP address in string form |
| 1232 | * \param[in] port remote port number in host byte order |
| 1233 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1234 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1235 | * |
| 1236 | * This function creates (and optionall binds/connects) a socket using |
| 1237 | * \ref osmo_sock_init, but also fills the \a ofd structure. |
| 1238 | */ |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 1239 | 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] | 1240 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 1241 | { |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1242 | return osmo_fd_init_ofd(ofd, osmo_sock_init(family, type, proto, host, port, flags), flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1245 | /*! Initialize a socket and fill \ref osmo_fd |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 1246 | * \param[out] ofd file descriptor (will be filled in) |
| 1247 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 1248 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1249 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1250 | * \param[in] local_host local host name or IP address in string form |
| 1251 | * \param[in] local_port local port number in host byte order |
| 1252 | * \param[in] remote_host remote host name or IP address in string form |
| 1253 | * \param[in] remote_port remote port number in host byte order |
| 1254 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 1255 | * \returns socket fd on success; negative on error |
| 1256 | * |
| 1257 | * This function creates (and optionall binds/connects) a socket using |
| 1258 | * \ref osmo_sock_init2, but also fills the \a ofd structure. |
| 1259 | */ |
| 1260 | int osmo_sock_init2_ofd(struct osmo_fd *ofd, int family, int type, int proto, |
| 1261 | const char *local_host, uint16_t local_port, |
| 1262 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 1263 | { |
| 1264 | return osmo_fd_init_ofd(ofd, osmo_sock_init2(family, type, proto, local_host, |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1265 | local_port, remote_host, remote_port, flags), flags); |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 1268 | int osmo_sock_init_osa_ofd(struct osmo_fd *ofd, int type, int proto, |
| 1269 | const struct osmo_sockaddr *local, |
| 1270 | const struct osmo_sockaddr *remote, unsigned int flags) |
| 1271 | { |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1272 | return osmo_fd_init_ofd(ofd, osmo_sock_init_osa(type, proto, local, remote, flags), flags); |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 1273 | } |
| 1274 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1275 | /*! Initialize a socket and fill \ref sockaddr |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1276 | * \param[out] ss socket address (will be filled in) |
| 1277 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1278 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1279 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1280 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1281 | * |
| 1282 | * This function creates (and optionall binds/connects) a socket using |
| 1283 | * \ref osmo_sock_init, but also fills the \a ss structure. |
| 1284 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1285 | int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1286 | uint8_t proto, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1287 | { |
| 1288 | char host[NI_MAXHOST]; |
| 1289 | uint16_t port; |
| 1290 | struct sockaddr_in *sin; |
| 1291 | struct sockaddr_in6 *sin6; |
| 1292 | int s, sa_len; |
| 1293 | |
| 1294 | /* determine port and host from ss */ |
| 1295 | switch (ss->sa_family) { |
| 1296 | case AF_INET: |
| 1297 | sin = (struct sockaddr_in *) ss; |
| 1298 | sa_len = sizeof(struct sockaddr_in); |
| 1299 | port = ntohs(sin->sin_port); |
| 1300 | break; |
| 1301 | case AF_INET6: |
| 1302 | sin6 = (struct sockaddr_in6 *) ss; |
| 1303 | sa_len = sizeof(struct sockaddr_in6); |
| 1304 | port = ntohs(sin6->sin6_port); |
| 1305 | break; |
| 1306 | default: |
| 1307 | return -EINVAL; |
| 1308 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1309 | |
| 1310 | s = getnameinfo(ss, sa_len, host, NI_MAXHOST, |
| 1311 | NULL, 0, NI_NUMERICHOST); |
| 1312 | if (s != 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 1313 | LOGP(DLGLOBAL, LOGL_ERROR, "getnameinfo failed:" |
| 1314 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1315 | return s; |
| 1316 | } |
| 1317 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1318 | return osmo_sock_init(ss->sa_family, type, proto, host, port, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1319 | } |
| 1320 | |
Pau Espin Pedrol | e4f34d8 | 2023-10-23 11:23:45 +0200 | [diff] [blame] | 1321 | #ifdef HAVE_LIBSCTP |
Pau Espin Pedrol | 64ba9ed | 2023-09-29 19:21:10 +0200 | [diff] [blame] | 1322 | /*! Add addresses to the multi-address (SCTP) socket active binding set |
| 1323 | * \param[in] sfd The multi-address (SCTP) socket |
| 1324 | * \param[in] addrs array of char pointers (strings), each containing local host name or IP address in string form |
| 1325 | * \param[in] addrs_cnt length of addrs_hosts (in items) |
| 1326 | * \returns 0 on success; negative on error |
| 1327 | * |
| 1328 | * This function only supports SCTP sockets so far, and hence it should be |
| 1329 | * called only on socket file descriptions referencing that kind of sockets. |
| 1330 | */ |
| 1331 | int osmo_sock_multiaddr_add_local_addr(int sfd, const char **addrs, size_t addrs_cnt) |
| 1332 | { |
| 1333 | struct osmo_sockaddr osa; |
| 1334 | socklen_t slen = sizeof(osa); |
| 1335 | uint16_t sfd_family; |
| 1336 | uint16_t type = SOCK_STREAM ;/* Fixme: we assume fd is SOCK_STREAM */ |
| 1337 | uint8_t proto = IPPROTO_SCTP; /* Fixme: we assume fd is IPPROTO_SCTP */ |
| 1338 | struct addrinfo *res[OSMO_SOCK_MAX_ADDRS]; |
| 1339 | uint16_t port; |
| 1340 | struct sockaddr_in6 addrs_buf[OSMO_SOCK_MAX_ADDRS]; |
| 1341 | unsigned int i; |
| 1342 | int rc; |
| 1343 | bool res_has_v4addr = false, res_has_v6addr = false; |
| 1344 | |
| 1345 | rc = getsockname(sfd, &osa.u.sa, &slen); |
| 1346 | if (rc < 0) |
| 1347 | return rc; /* TODO: log error? */ |
| 1348 | sfd_family = osa.u.sa.sa_family; |
| 1349 | port = osmo_sockaddr_port(&osa.u.sa); |
| 1350 | |
| 1351 | if (sfd_family != AF_INET && sfd_family != AF_INET6) |
| 1352 | return -EINVAL; |
| 1353 | |
| 1354 | rc = addrinfo_helper_multi(res, AF_UNSPEC, type, proto, addrs, |
| 1355 | addrs_cnt, port, true); |
| 1356 | if (rc < 0) |
| 1357 | return -EINVAL; |
| 1358 | |
| 1359 | addrinfo_has_v4v6addr((const struct addrinfo **)res, addrs_cnt, |
| 1360 | &res_has_v4addr, &res_has_v6addr); |
| 1361 | if (sfd_family == AF_INET && !res_has_v4addr) { |
| 1362 | rc = -EINVAL; |
| 1363 | goto ret_free; |
| 1364 | } |
| 1365 | |
| 1366 | uint16_t new_addr_family; |
| 1367 | if (sfd_family == AF_INET) |
| 1368 | new_addr_family = AF_INET; |
| 1369 | else if (sfd_family == AF_INET6 && !res_has_v4addr) |
| 1370 | new_addr_family = AF_INET6; |
| 1371 | else |
| 1372 | new_addr_family = AF_UNSPEC; |
| 1373 | rc = addrinfo_to_sockaddr(new_addr_family, (const struct addrinfo **)res, |
| 1374 | addrs, addrs_cnt, |
| 1375 | (uint8_t *)addrs_buf, sizeof(addrs_buf)); |
| 1376 | if (rc < 0) { |
| 1377 | rc = -ENODEV; |
| 1378 | goto ret_free; |
| 1379 | } |
| 1380 | |
| 1381 | rc = sctp_bindx(sfd, (struct sockaddr *)addrs_buf, addrs_cnt, SCTP_BINDX_ADD_ADDR); |
| 1382 | if (rc == -1) { |
| 1383 | int err = errno; |
| 1384 | char strbuf[512]; |
| 1385 | multiaddr_snprintf(strbuf, sizeof(strbuf), addrs, addrs_cnt); |
| 1386 | LOGP(DLGLOBAL, LOGL_NOTICE, "Unable to bind socket to new addresses: %s:%u: %s\n", |
| 1387 | strbuf, port, strerror(err)); |
| 1388 | rc = -ENODEV; |
| 1389 | goto ret_free; |
| 1390 | } |
| 1391 | |
| 1392 | ret_free: |
| 1393 | for (i = 0; i < addrs_cnt; i++) |
| 1394 | freeaddrinfo(res[i]); |
| 1395 | return rc; |
| 1396 | } |
| 1397 | |
| 1398 | /*! Remove addresses from the multi-address (SCTP) socket active binding set |
| 1399 | * \param[in] sfd The multi-address (SCTP) socket |
| 1400 | * \param[in] addrs array of char pointers (strings), each containing local host name or IP address in string form |
| 1401 | * \param[in] addrs_cnt length of addrs_hosts (in items) |
| 1402 | * \returns 0 on success; negative on error |
| 1403 | * |
| 1404 | * This function only supports SCTP sockets so far, and hence it should be |
| 1405 | * called only on socket file descriptions referencing that kind of sockets. |
| 1406 | */ |
| 1407 | int osmo_sock_multiaddr_del_local_addr(int sfd, const char **addrs, size_t addrs_cnt) |
| 1408 | { |
| 1409 | struct osmo_sockaddr osa; |
| 1410 | socklen_t slen = sizeof(osa); |
| 1411 | uint16_t sfd_family; |
| 1412 | uint16_t type = SOCK_STREAM ;/* Fixme: we assume fd is SOCK_STREAM */ |
| 1413 | uint8_t proto = IPPROTO_SCTP; /* Fixme: we assume fd is IPPROTO_SCTP */ |
| 1414 | struct addrinfo *res[OSMO_SOCK_MAX_ADDRS]; |
| 1415 | uint16_t port; |
| 1416 | struct sockaddr_in6 addrs_buf[OSMO_SOCK_MAX_ADDRS]; |
| 1417 | unsigned int i; |
| 1418 | int rc; |
| 1419 | bool res_has_v4addr = false, res_has_v6addr = false; |
| 1420 | |
| 1421 | rc = getsockname(sfd, &osa.u.sa, &slen); |
| 1422 | if (rc < 0) |
| 1423 | return rc; /* TODO: log error? */ |
| 1424 | sfd_family = osa.u.sa.sa_family; |
| 1425 | port = osmo_sockaddr_port(&osa.u.sa); |
| 1426 | |
| 1427 | if (sfd_family != AF_INET && sfd_family != AF_INET6) |
| 1428 | return -EINVAL; |
| 1429 | |
| 1430 | rc = addrinfo_helper_multi(res, AF_UNSPEC, type, proto, addrs, |
| 1431 | addrs_cnt, port, true); |
| 1432 | if (rc < 0) |
| 1433 | return -EINVAL; |
| 1434 | |
| 1435 | addrinfo_has_v4v6addr((const struct addrinfo **)res, addrs_cnt, |
| 1436 | &res_has_v4addr, &res_has_v6addr); |
| 1437 | if (sfd_family == AF_INET && !res_has_v4addr) { |
| 1438 | rc = -EINVAL; |
| 1439 | goto ret_free; |
| 1440 | } |
| 1441 | |
| 1442 | uint16_t del_addr_family; |
| 1443 | if (sfd_family == AF_INET) |
| 1444 | del_addr_family = AF_INET; |
| 1445 | else if (sfd_family == AF_INET6 && !res_has_v4addr) |
| 1446 | del_addr_family = AF_INET6; |
| 1447 | else |
| 1448 | del_addr_family = AF_UNSPEC; |
| 1449 | rc = addrinfo_to_sockaddr(del_addr_family, (const struct addrinfo **)res, |
| 1450 | addrs, addrs_cnt, |
| 1451 | (uint8_t *)addrs_buf, sizeof(addrs_buf)); |
| 1452 | if (rc < 0) { |
| 1453 | rc = -ENODEV; |
| 1454 | goto ret_free; |
| 1455 | } |
| 1456 | |
| 1457 | rc = sctp_bindx(sfd, (struct sockaddr *)addrs_buf, addrs_cnt, SCTP_BINDX_REM_ADDR); |
| 1458 | if (rc == -1) { |
| 1459 | int err = errno; |
| 1460 | char strbuf[512]; |
| 1461 | multiaddr_snprintf(strbuf, sizeof(strbuf), addrs, addrs_cnt); |
| 1462 | LOGP(DLGLOBAL, LOGL_NOTICE, "Unable to unbind socket from addresses: %s:%u: %s\n", |
| 1463 | strbuf, port, strerror(err)); |
| 1464 | rc = -ENODEV; |
| 1465 | goto ret_free; |
| 1466 | } |
| 1467 | |
| 1468 | ret_free: |
| 1469 | for (i = 0; i < addrs_cnt; i++) |
| 1470 | freeaddrinfo(res[i]); |
| 1471 | return rc; |
| 1472 | } |
Pau Espin Pedrol | e4f34d8 | 2023-10-23 11:23:45 +0200 | [diff] [blame] | 1473 | #endif /* HAVE_LIBSCTP */ |
Pau Espin Pedrol | 64ba9ed | 2023-09-29 19:21:10 +0200 | [diff] [blame] | 1474 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1475 | static int sockaddr_equal(const struct sockaddr *a, |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 1476 | const struct sockaddr *b, unsigned int len) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1477 | { |
| 1478 | struct sockaddr_in *sin_a, *sin_b; |
| 1479 | struct sockaddr_in6 *sin6_a, *sin6_b; |
| 1480 | |
| 1481 | if (a->sa_family != b->sa_family) |
| 1482 | return 0; |
| 1483 | |
| 1484 | switch (a->sa_family) { |
| 1485 | case AF_INET: |
| 1486 | sin_a = (struct sockaddr_in *)a; |
| 1487 | sin_b = (struct sockaddr_in *)b; |
| 1488 | if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr, |
| 1489 | sizeof(struct in_addr))) |
| 1490 | return 1; |
| 1491 | break; |
| 1492 | case AF_INET6: |
| 1493 | sin6_a = (struct sockaddr_in6 *)a; |
| 1494 | sin6_b = (struct sockaddr_in6 *)b; |
| 1495 | if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr, |
| 1496 | sizeof(struct in6_addr))) |
| 1497 | return 1; |
| 1498 | break; |
| 1499 | } |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
Eric | 8ae40cb | 2021-09-27 22:10:01 +0200 | [diff] [blame] | 1503 | /* linux has a default route: |
| 1504 | local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1 |
| 1505 | */ |
| 1506 | static int sockaddr_is_local_routed(const struct sockaddr *a) |
| 1507 | { |
| 1508 | #if __linux__ |
| 1509 | if (a->sa_family != AF_INET) |
| 1510 | return 0; |
| 1511 | |
| 1512 | uint32_t address = ((struct sockaddr_in *)a)->sin_addr.s_addr; /* already BE */ |
| 1513 | uint32_t eightmask = htonl(0xff000000); /* /8 mask */ |
| 1514 | uint32_t local_prefix_127 = htonl(0x7f000000); /* 127.0.0.0 */ |
| 1515 | |
| 1516 | if ((address & eightmask) == local_prefix_127) |
| 1517 | return 1; |
| 1518 | #endif |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1522 | /*! Determine if the given address is a local address |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1523 | * \param[in] addr Socket Address |
| 1524 | * \param[in] addrlen Length of socket address in bytes |
| 1525 | * \returns 1 if address is local, 0 otherwise. |
| 1526 | */ |
Harald Welte | bc32d05 | 2012-04-08 11:31:32 +0200 | [diff] [blame] | 1527 | int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1528 | { |
| 1529 | struct ifaddrs *ifaddr, *ifa; |
| 1530 | |
Eric | 8ae40cb | 2021-09-27 22:10:01 +0200 | [diff] [blame] | 1531 | if (sockaddr_is_local_routed(addr)) |
| 1532 | return 1; |
| 1533 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1534 | if (getifaddrs(&ifaddr) == -1) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 1535 | LOGP(DLGLOBAL, LOGL_ERROR, "getifaddrs:" |
| 1536 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1537 | return -EIO; |
| 1538 | } |
| 1539 | |
| 1540 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
Harald Welte | 4d3a7b1 | 2011-05-24 21:31:53 +0200 | [diff] [blame] | 1541 | if (!ifa->ifa_addr) |
| 1542 | continue; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 1543 | if (sockaddr_equal(ifa->ifa_addr, addr, addrlen)) { |
| 1544 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1545 | return 1; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 1546 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1547 | } |
| 1548 | |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 1549 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1550 | return 0; |
| 1551 | } |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 1552 | |
Pau Espin Pedrol | 3b3955b | 2022-10-04 12:38:49 +0200 | [diff] [blame] | 1553 | /*! Determine if the given address is an ANY address ("0.0.0.0", "::"). Port is not checked. |
| 1554 | * \param[in] addr Socket Address |
| 1555 | * \param[in] addrlen Length of socket address in bytes |
| 1556 | * \returns 1 if address is ANY, 0 otherwise. -1 is address family not supported/detected. |
| 1557 | */ |
| 1558 | int osmo_sockaddr_is_any(const struct osmo_sockaddr *addr) |
| 1559 | { |
| 1560 | switch (addr->u.sa.sa_family) { |
| 1561 | case AF_INET6: { |
| 1562 | struct in6_addr ip6_any = IN6ADDR_ANY_INIT; |
| 1563 | return memcmp(&addr->u.sin6.sin6_addr, |
| 1564 | &ip6_any, sizeof(ip6_any)) == 0; |
| 1565 | } |
| 1566 | case AF_INET: |
| 1567 | return addr->u.sin.sin_addr.s_addr == INADDR_ANY; |
| 1568 | default: |
| 1569 | return -1; |
| 1570 | } |
| 1571 | } |
| 1572 | |
Max | 9d7a247 | 2018-11-20 15:18:31 +0100 | [diff] [blame] | 1573 | /*! Convert sockaddr_in to IP address as char string and port as uint16_t. |
| 1574 | * \param[out] addr String buffer to write IP address to, or NULL. |
| 1575 | * \param[out] addr_len Size of \a addr. |
| 1576 | * \param[out] port Pointer to uint16_t to write the port number to, or NULL. |
| 1577 | * \param[in] sin Sockaddr to convert. |
| 1578 | * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL. |
| 1579 | */ |
| 1580 | size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 1581 | const struct sockaddr_in *sin) |
| 1582 | { |
| 1583 | if (port) |
| 1584 | *port = ntohs(sin->sin_port); |
| 1585 | |
| 1586 | if (addr) |
| 1587 | return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len); |
| 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 1592 | /*! Convert sockaddr to IP address as char string and port as uint16_t. |
| 1593 | * \param[out] addr String buffer to write IP address to, or NULL. |
| 1594 | * \param[out] addr_len Size of \a addr. |
| 1595 | * \param[out] port Pointer to uint16_t to write the port number to, or NULL. |
| 1596 | * \param[in] sa Sockaddr to convert. |
| 1597 | * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL. |
| 1598 | */ |
| 1599 | unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 1600 | const struct sockaddr *sa) |
| 1601 | { |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 1602 | |
Pau Espin Pedrol | 1a3d24e | 2020-08-28 18:31:32 +0200 | [diff] [blame] | 1603 | const struct sockaddr_in6 *sin6; |
| 1604 | |
| 1605 | switch (sa->sa_family) { |
| 1606 | case AF_INET: |
| 1607 | return osmo_sockaddr_in_to_str_and_uint(addr, addr_len, port, |
| 1608 | (const struct sockaddr_in *)sa); |
| 1609 | case AF_INET6: |
| 1610 | sin6 = (const struct sockaddr_in6 *)sa; |
| 1611 | if (port) |
| 1612 | *port = ntohs(sin6->sin6_port); |
| 1613 | if (addr && inet_ntop(sa->sa_family, &sin6->sin6_addr, addr, addr_len)) |
| 1614 | return strlen(addr); |
| 1615 | break; |
| 1616 | } |
| 1617 | return 0; |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 1618 | } |
| 1619 | |
Pau Espin Pedrol | 5cc4fe4 | 2020-08-31 12:52:06 +0200 | [diff] [blame] | 1620 | /*! inet_ntop() wrapper for a struct sockaddr. |
| 1621 | * \param[in] sa source sockaddr to get the address from. |
| 1622 | * \param[out] dst string buffer of at least INET6_ADDRSTRLEN size. |
| 1623 | * \returns returns a non-null pointer to dst. NULL is returned if there was an |
| 1624 | * error, with errno set to indicate the error. |
| 1625 | */ |
| 1626 | const char *osmo_sockaddr_ntop(const struct sockaddr *sa, char *dst) |
| 1627 | { |
| 1628 | const struct osmo_sockaddr *osa = (const struct osmo_sockaddr *)sa; |
| 1629 | return inet_ntop(osa->u.sa.sa_family, |
| 1630 | osa->u.sa.sa_family == AF_INET6 ? |
| 1631 | (const void *)&osa->u.sin6.sin6_addr : |
| 1632 | (const void *)&osa->u.sin.sin_addr, |
| 1633 | dst, INET6_ADDRSTRLEN); |
| 1634 | } |
| 1635 | |
| 1636 | /*! Get sockaddr port content (in host byte order) |
| 1637 | * \param[in] sa source sockaddr to get the port from. |
| 1638 | * \returns returns the sockaddr port in host byte order |
| 1639 | */ |
| 1640 | uint16_t osmo_sockaddr_port(const struct sockaddr *sa) |
| 1641 | { |
| 1642 | const struct osmo_sockaddr *osa = (const struct osmo_sockaddr *)sa; |
| 1643 | switch (osa->u.sa.sa_family) { |
| 1644 | case AF_INET6: |
| 1645 | return ntohs(osa->u.sin6.sin6_port); |
| 1646 | case AF_INET: |
| 1647 | return ntohs(osa->u.sin.sin_port); |
| 1648 | } |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
Neels Hofmeyr | 9c7f7f8 | 2022-01-11 19:25:40 +0100 | [diff] [blame] | 1652 | /*! Set sockaddr port content (to network byte order). |
| 1653 | * \param[out] sa sockaddr to set the port of. |
| 1654 | * \param[in] port port nr to set. |
| 1655 | */ |
| 1656 | void osmo_sockaddr_set_port(struct sockaddr *sa, uint16_t port) |
| 1657 | { |
| 1658 | struct osmo_sockaddr *osa = (struct osmo_sockaddr *)sa; |
| 1659 | switch (osa->u.sa.sa_family) { |
| 1660 | case AF_INET6: |
| 1661 | osa->u.sin6.sin6_port = htons(port); |
| 1662 | return; |
| 1663 | case AF_INET: |
| 1664 | osa->u.sin.sin_port = htons(port); |
| 1665 | return; |
| 1666 | } |
| 1667 | } |
| 1668 | |
Pau Espin Pedrol | cc296c9 | 2023-01-16 14:54:55 +0100 | [diff] [blame] | 1669 | static unsigned int in6_addr_netmask_to_prefixlen(const struct in6_addr *netmask) |
| 1670 | { |
| 1671 | #if defined(__linux__) |
| 1672 | #define ADDRFIELD(i) s6_addr32[i] |
| 1673 | #else |
| 1674 | #define ADDRFIELD(i) __u6_addr.__u6_addr32[i] |
| 1675 | #endif |
| 1676 | |
| 1677 | unsigned int i, j, prefix = 0; |
| 1678 | |
| 1679 | for (j = 0; j < 4; j++) { |
| 1680 | uint32_t bits = netmask->ADDRFIELD(j); |
| 1681 | uint8_t *b = (uint8_t *)&bits; |
| 1682 | for (i = 0; i < 4; i++) { |
| 1683 | while (b[i] & 0x80) { |
| 1684 | prefix++; |
| 1685 | b[i] = b[i] << 1; |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | #undef ADDRFIELD |
| 1691 | |
| 1692 | return prefix; |
| 1693 | } |
| 1694 | |
| 1695 | static unsigned int in_addr_netmask_to_prefixlen(const struct in_addr *netmask) |
| 1696 | { |
| 1697 | uint32_t bits = netmask->s_addr; |
| 1698 | uint8_t *b = (uint8_t *)&bits; |
| 1699 | unsigned int i, prefix = 0; |
| 1700 | |
| 1701 | for (i = 0; i < 4; i++) { |
| 1702 | while (b[i] & 0x80) { |
| 1703 | prefix++; |
| 1704 | b[i] = b[i] << 1; |
| 1705 | } |
| 1706 | } |
| 1707 | return prefix; |
| 1708 | } |
| 1709 | |
| 1710 | /*! Convert netmask to prefix length representation |
| 1711 | * \param[in] netmask sockaddr containing a netmask (consecutive list of 1-bit followed by consecutive list of 0-bit) |
| 1712 | * \returns prefix length representation of the netmask (count of 1-bit from the start of the netmask), negative on error. |
| 1713 | */ |
| 1714 | int osmo_sockaddr_netmask_to_prefixlen(const struct osmo_sockaddr *netmask) |
| 1715 | { |
| 1716 | switch (netmask->u.sa.sa_family) { |
| 1717 | case AF_INET6: |
| 1718 | return in6_addr_netmask_to_prefixlen(&netmask->u.sin6.sin6_addr); |
| 1719 | case AF_INET: |
| 1720 | return in_addr_netmask_to_prefixlen(&netmask->u.sin.sin_addr); |
| 1721 | default: |
| 1722 | return -ENOTSUP; |
| 1723 | } |
| 1724 | } |
| 1725 | |
Harald Welte | 641cc3c | 2023-11-21 19:45:57 +0100 | [diff] [blame] | 1726 | /*! Convert an IP address string (and port number) into a 'struct osmo_sockaddr'. |
| 1727 | * \param[out] osa_out caller-allocated osmo_sockaddr storage |
| 1728 | * \param[in] ipstr IP[v4,v6] address in string format |
| 1729 | * \param[in] port port number (host byte order) |
| 1730 | * \returns 0 on success; negative on error. */ |
| 1731 | int osmo_sockaddr_from_str_and_uint(struct osmo_sockaddr *osa_out, const char *ipstr, uint16_t port) |
| 1732 | { |
| 1733 | struct addrinfo *ai = addrinfo_helper(AF_UNSPEC, 0, 0, ipstr, port, true); |
| 1734 | |
| 1735 | if (!ai) |
| 1736 | return -EIO; |
| 1737 | |
| 1738 | if (ai->ai_addrlen > sizeof(*osa_out)) |
| 1739 | return -ENOSPC; |
| 1740 | |
| 1741 | memcpy(&osa_out->u.sa, ai->ai_addr, ai->ai_addrlen); |
| 1742 | freeaddrinfo(ai); |
| 1743 | |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1747 | /*! Initialize a unix domain socket (including bind/connect) |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1748 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1749 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1750 | * \param[in] socket_path path to identify the socket |
| 1751 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1752 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1753 | * |
| 1754 | * This function creates a new unix domain socket, \a |
| 1755 | * type and \a proto and optionally binds or connects it, depending on |
| 1756 | * the value of \a flags parameter. |
| 1757 | */ |
Eric Wild | eb5769b | 2019-06-27 15:31:17 +0200 | [diff] [blame] | 1758 | #if defined(__clang__) && defined(SUN_LEN) |
| 1759 | __attribute__((no_sanitize("undefined"))) |
| 1760 | #endif |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1761 | int osmo_sock_unix_init(uint16_t type, uint8_t proto, |
| 1762 | const char *socket_path, unsigned int flags) |
| 1763 | { |
| 1764 | struct sockaddr_un local; |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 1765 | int sfd, rc; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1766 | unsigned int namelen; |
| 1767 | |
| 1768 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
| 1769 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) |
| 1770 | return -EINVAL; |
| 1771 | |
| 1772 | local.sun_family = AF_UNIX; |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1773 | /* When an AF_UNIX socket is bound, sun_path should be NUL-terminated. See unix(7) man page. */ |
| 1774 | 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] | 1775 | LOGP(DLGLOBAL, LOGL_ERROR, "Socket path exceeds maximum length of %zd bytes: %s\n", |
| 1776 | sizeof(local.sun_path), socket_path); |
| 1777 | return -ENOSPC; |
| 1778 | } |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1779 | |
| 1780 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1781 | local.sun_len = strlen(local.sun_path); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1782 | #endif |
| 1783 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 1784 | namelen = SUN_LEN(&local); |
| 1785 | #else |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1786 | namelen = strlen(local.sun_path) + |
| 1787 | offsetof(struct sockaddr_un, sun_path); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1788 | #endif |
| 1789 | |
| 1790 | sfd = socket(AF_UNIX, type, proto); |
| 1791 | if (sfd < 0) |
Max | db7cb69 | 2023-02-11 21:35:21 +0300 | [diff] [blame] | 1792 | return -errno; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1793 | |
| 1794 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 1795 | rc = connect(sfd, (struct sockaddr *)&local, namelen); |
| 1796 | if (rc < 0) |
| 1797 | goto err; |
| 1798 | } else { |
| 1799 | unlink(local.sun_path); |
| 1800 | rc = bind(sfd, (struct sockaddr *)&local, namelen); |
| 1801 | if (rc < 0) |
| 1802 | goto err; |
| 1803 | } |
| 1804 | |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 1805 | rc = socket_helper_tail(sfd, flags); |
| 1806 | if (rc < 0) |
| 1807 | return rc; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1808 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 1809 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 1810 | if (rc < 0) { |
| 1811 | close(sfd); |
Max | db7cb69 | 2023-02-11 21:35:21 +0300 | [diff] [blame] | 1812 | sfd = rc; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | return sfd; |
| 1816 | err: |
| 1817 | close(sfd); |
Max | db7cb69 | 2023-02-11 21:35:21 +0300 | [diff] [blame] | 1818 | return -errno; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1819 | } |
| 1820 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1821 | /*! Initialize a unix domain socket and fill \ref osmo_fd |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1822 | * \param[out] ofd file descriptor (will be filled in) |
| 1823 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1824 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1825 | * \param[in] socket_path path to identify the socket |
| 1826 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1827 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1828 | * |
Vadim Yanitskiy | b606d76 | 2019-06-01 19:02:47 +0700 | [diff] [blame] | 1829 | * This function creates (and optionally binds/connects) a socket |
| 1830 | * using osmo_sock_unix_init, but also fills the ofd structure. |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1831 | */ |
| 1832 | int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto, |
| 1833 | const char *socket_path, unsigned int flags) |
| 1834 | { |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1835 | return osmo_fd_init_ofd(ofd, osmo_sock_unix_init(type, proto, socket_path, flags), flags); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1838 | /*! 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] | 1839 | * \param[in] fd file descriptor of socket |
| 1840 | * \param[out] ip IP address (will be filled in when not NULL) |
| 1841 | * \param[in] ip_len length of the ip buffer |
| 1842 | * \param[out] port number (will be filled in when not NULL) |
| 1843 | * \param[in] port_len length of the port buffer |
| 1844 | * \param[in] local (true) or remote (false) name will get looked at |
| 1845 | * \returns 0 on success; negative otherwise |
| 1846 | */ |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1847 | 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] | 1848 | { |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1849 | struct sockaddr_storage sa; |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1850 | socklen_t len = sizeof(sa); |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 1851 | char ipbuf[INET6_ADDRSTRLEN], portbuf[6]; |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1852 | int rc; |
| 1853 | |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1854 | 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] | 1855 | if (rc < 0) |
| 1856 | return rc; |
| 1857 | |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1858 | rc = getnameinfo((const struct sockaddr*)&sa, len, ipbuf, sizeof(ipbuf), |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1859 | portbuf, sizeof(portbuf), |
| 1860 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 1861 | if (rc < 0) |
| 1862 | return rc; |
| 1863 | |
| 1864 | if (ip) |
| 1865 | strncpy(ip, ipbuf, ip_len); |
| 1866 | if (port) |
| 1867 | strncpy(port, portbuf, port_len); |
| 1868 | return 0; |
| 1869 | } |
| 1870 | |
Andreas Eversberg | e8ab1b7 | 2024-02-22 15:38:47 +0100 | [diff] [blame] | 1871 | #ifdef HAVE_LIBSCTP |
Pau Espin Pedrol | 6a2975c | 2023-11-30 18:15:30 +0100 | [diff] [blame] | 1872 | /*! Get multiple IP addresses and/or port number on socket in separate string buffers |
| 1873 | * \param[in] fd file descriptor of socket. |
| 1874 | * \param[out] ip_proto IPPROTO of the socket, eg: IPPROTO_SCTP. |
| 1875 | * \param[out] ip Pointer to memory holding consecutive buffers of size ip_len. |
| 1876 | * \param[out] ip_cnt length ip array pointer. on return it contains the number of addresses found. |
| 1877 | * \param[in] ip_len length of each of the string buffer in the the ip array. |
| 1878 | * \param[out] port number (will be filled in when not NULL). |
| 1879 | * \param[in] port_len length of the port buffer. |
| 1880 | * \param[in] local (true) or remote (false) name will get looked at. |
| 1881 | * \returns 0 on success; negative otherwise. |
| 1882 | * |
| 1883 | * Upon return, ip_cnt can be set to a higher value than the one set by the |
| 1884 | * caller. This can be used by the caller to find out the required array length |
| 1885 | * and then obtaining by calling the function twice. Only up to ip_cnt addresses |
| 1886 | * are filed in, as per the value provided by the caller. |
| 1887 | * |
| 1888 | * Usage example retrieving all (up to OSMO_SOCK_MAX_ADDRS, 32) bound IP addresses and bound port: |
| 1889 | * char hostbuf[OSMO_SOCK_MAX_ADDRS][INET6_ADDRSTRLEN]; |
| 1890 | * size_t num_hostbuf = ARRAY_SIZE(hostbuf); |
| 1891 | * char portbuf[6]; |
| 1892 | * rc = osmo_sock_multiaddr_get_ip_and_port(fd, IPPROTO_SCTP, &hostbuf[0][0], &num_hostbuf, |
| 1893 | * sizeof(hostbuf[0]), portbuf, sizeof(portbuf), true); |
| 1894 | * if (rc < 0) |
| 1895 | * goto error; |
| 1896 | * if (num_hostbuf > ARRAY_SIZE(hostbuf)) |
| 1897 | * goto not_enough_buffers; |
| 1898 | */ |
| 1899 | int osmo_sock_multiaddr_get_ip_and_port(int fd, int ip_proto, char *ip, size_t *ip_cnt, size_t ip_len, |
| 1900 | char *port, size_t port_len, bool local) |
| 1901 | { |
| 1902 | struct sockaddr *addrs = NULL; |
| 1903 | unsigned int n_addrs, i; |
| 1904 | void *addr_buf; |
| 1905 | int rc; |
| 1906 | |
| 1907 | switch (ip_proto) { |
| 1908 | case IPPROTO_SCTP: |
| 1909 | break; /* continue below */ |
| 1910 | default: |
| 1911 | if (*ip_cnt == 0) { |
| 1912 | *ip_cnt = 1; |
| 1913 | return 0; |
| 1914 | } |
| 1915 | *ip_cnt = 1; |
| 1916 | return osmo_sock_get_ip_and_port(fd, ip, ip_len, port, port_len, local); |
| 1917 | } |
| 1918 | |
| 1919 | rc = local ? sctp_getladdrs(fd, 0, &addrs) : sctp_getpaddrs(fd, 0, &addrs); |
| 1920 | if (rc < 0) |
| 1921 | return rc; |
| 1922 | if (rc == 0) |
| 1923 | return -ENOTCONN; |
| 1924 | |
| 1925 | n_addrs = rc; |
| 1926 | addr_buf = (void *)addrs; |
| 1927 | for (i = 0; i < n_addrs; i++) { |
| 1928 | struct sockaddr *sa_addr = (struct sockaddr *)addr_buf; |
| 1929 | size_t addrlen; |
| 1930 | |
| 1931 | if (i >= *ip_cnt) |
| 1932 | break; |
| 1933 | |
| 1934 | switch (sa_addr->sa_family) { |
| 1935 | case AF_INET: |
| 1936 | addrlen = sizeof(struct sockaddr_in); |
| 1937 | break; |
| 1938 | case AF_INET6: |
| 1939 | addrlen = sizeof(struct sockaddr_in6); |
| 1940 | break; |
| 1941 | default: |
| 1942 | rc = -EINVAL; |
| 1943 | goto free_addrs_ret; |
| 1944 | } |
| 1945 | |
| 1946 | rc = getnameinfo(sa_addr, addrlen, &ip[i * ip_len], ip_len, |
| 1947 | port, port_len, |
| 1948 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 1949 | if (rc < 0) |
| 1950 | goto free_addrs_ret; |
| 1951 | addr_buf += addrlen; |
| 1952 | } |
| 1953 | |
| 1954 | *ip_cnt = n_addrs; |
| 1955 | rc = 0; |
| 1956 | free_addrs_ret: |
| 1957 | local ? sctp_freeladdrs(addrs) : sctp_freepaddrs(addrs); |
| 1958 | return rc; |
| 1959 | } |
Andreas Eversberg | e8ab1b7 | 2024-02-22 15:38:47 +0100 | [diff] [blame] | 1960 | #endif |
Pau Espin Pedrol | 6a2975c | 2023-11-30 18:15:30 +0100 | [diff] [blame] | 1961 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1962 | /*! Get local IP address on socket |
| 1963 | * \param[in] fd file descriptor of socket |
| 1964 | * \param[out] ip IP address (will be filled in) |
| 1965 | * \param[in] len length of the output buffer |
| 1966 | * \returns 0 on success; negative otherwise |
| 1967 | */ |
| 1968 | int osmo_sock_get_local_ip(int fd, char *ip, size_t len) |
| 1969 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1970 | 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] | 1971 | } |
| 1972 | |
| 1973 | /*! Get local port on socket |
| 1974 | * \param[in] fd file descriptor of socket |
| 1975 | * \param[out] port number (will be filled in) |
| 1976 | * \param[in] len length of the output buffer |
| 1977 | * \returns 0 on success; negative otherwise |
| 1978 | */ |
| 1979 | int osmo_sock_get_local_ip_port(int fd, char *port, size_t len) |
| 1980 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1981 | 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] | 1982 | } |
| 1983 | |
| 1984 | /*! Get remote IP address on socket |
| 1985 | * \param[in] fd file descriptor of socket |
| 1986 | * \param[out] ip IP address (will be filled in) |
| 1987 | * \param[in] len length of the output buffer |
| 1988 | * \returns 0 on success; negative otherwise |
| 1989 | */ |
| 1990 | int osmo_sock_get_remote_ip(int fd, char *ip, size_t len) |
| 1991 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1992 | 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] | 1993 | } |
| 1994 | |
| 1995 | /*! Get remote port on socket |
| 1996 | * \param[in] fd file descriptor of socket |
| 1997 | * \param[out] port number (will be filled in) |
| 1998 | * \param[in] len length of the output buffer |
| 1999 | * \returns 0 on success; negative otherwise |
| 2000 | */ |
| 2001 | int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len) |
| 2002 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 2003 | 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] | 2004 | } |
| 2005 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 2006 | /*! Get address/port information on socket in dyn-alloc string like "(r=1.2.3.4:5<->l=6.7.8.9:10)". |
| 2007 | * Usually, it is better to use osmo_sock_get_name2() for a static string buffer or osmo_sock_get_name_buf() for a |
| 2008 | * caller provided string buffer, to avoid the dynamic talloc allocation. |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 2009 | * \param[in] ctx talloc context from which to allocate string buffer |
| 2010 | * \param[in] fd file descriptor of socket |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 2011 | * \returns string identifying the connection of this socket, talloc'd from ctx. |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 2012 | */ |
Harald Welte | c0dfc9d | 2019-03-18 18:29:43 +0100 | [diff] [blame] | 2013 | char *osmo_sock_get_name(const void *ctx, int fd) |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 2014 | { |
Philipp Maier | 64b51eb | 2019-01-14 11:59:11 +0100 | [diff] [blame] | 2015 | char str[OSMO_SOCK_NAME_MAXLEN]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 2016 | int rc; |
| 2017 | rc = osmo_sock_get_name_buf(str, sizeof(str), fd); |
| 2018 | if (rc <= 0) |
| 2019 | return NULL; |
| 2020 | return talloc_asprintf(ctx, "(%s)", str); |
| 2021 | } |
| 2022 | |
Andreas Eversberg | e8ab1b7 | 2024-02-22 15:38:47 +0100 | [diff] [blame] | 2023 | #ifdef HAVE_LIBSCTP |
Pau Espin Pedrol | 5ac8aa5 | 2023-11-30 19:12:16 +0100 | [diff] [blame] | 2024 | /*! Format multiple IP addresses and/or port number into a combined string buffer |
| 2025 | * \param[out] str Destination string buffer. |
Pau Espin Pedrol | a37921a | 2023-12-12 14:00:10 +0100 | [diff] [blame] | 2026 | * \param[in] str_len sizeof(str), usually OSMO_SOCK_MULTIADDR_PEER_STR_MAXLEN. |
Pau Espin Pedrol | 5ac8aa5 | 2023-11-30 19:12:16 +0100 | [diff] [blame] | 2027 | * \param[out] ip Pointer to memory holding ip_cnt consecutive buffers of size ip_len. |
| 2028 | * \param[out] ip_cnt length ip array pointer. on return it contains the number of addresses found. |
| 2029 | * \param[in] ip_len length of each of the string buffer in the the ip array. |
| 2030 | * \param[out] port number (will be printed in when not NULL). |
| 2031 | * \return String length as returned by snprintf(), or negative on error. |
| 2032 | * |
Pau Espin Pedrol | a37921a | 2023-12-12 14:00:10 +0100 | [diff] [blame] | 2033 | * This API expects an ip array as the one filled in by |
Pau Espin Pedrol | 5ac8aa5 | 2023-11-30 19:12:16 +0100 | [diff] [blame] | 2034 | * osmo_sock_multiaddr_get_ip_and_port(), and hence it's a good companion for |
| 2035 | * that API. |
| 2036 | */ |
| 2037 | int osmo_multiaddr_ip_and_port_snprintf(char *str, size_t str_len, |
| 2038 | const char *ip, size_t ip_cnt, size_t ip_len, |
| 2039 | const char *portbuf) |
| 2040 | { |
| 2041 | struct osmo_strbuf sb = { .buf = str, .len = str_len }; |
| 2042 | bool is_v6 = false; |
| 2043 | unsigned int i; |
| 2044 | |
| 2045 | if (ip_cnt == 0) { |
| 2046 | OSMO_STRBUF_PRINTF(sb, "NULL:%s", portbuf); |
| 2047 | return sb.chars_needed; |
| 2048 | } |
| 2049 | if (ip_cnt > 1) |
| 2050 | OSMO_STRBUF_PRINTF(sb, "("); |
| 2051 | else if ((is_v6 = !!strchr(&ip[0], ':'))) /* IPv6, add [] to separate from port. */ |
| 2052 | OSMO_STRBUF_PRINTF(sb, "["); |
| 2053 | |
| 2054 | for (i = 0; i < ip_cnt - 1; i++) |
| 2055 | OSMO_STRBUF_PRINTF(sb, "%s|", &ip[i * ip_len]); |
| 2056 | OSMO_STRBUF_PRINTF(sb, "%s", &ip[i * ip_len]); |
| 2057 | |
| 2058 | if (ip_cnt > 1) |
| 2059 | OSMO_STRBUF_PRINTF(sb, ")"); |
| 2060 | else if (is_v6) |
| 2061 | OSMO_STRBUF_PRINTF(sb, "]"); |
| 2062 | if (portbuf) |
| 2063 | OSMO_STRBUF_PRINTF(sb, ":%s", portbuf); |
| 2064 | |
| 2065 | return sb.chars_needed; |
| 2066 | } |
| 2067 | |
| 2068 | /*! Get address/port information on socket in provided string buffer, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 2069 | * This does not include braces like osmo_sock_get_name(). |
| 2070 | * \param[out] str Destination string buffer. |
Pau Espin Pedrol | a37921a | 2023-12-12 14:00:10 +0100 | [diff] [blame] | 2071 | * \param[in] str_len sizeof(str), usually OSMO_SOCK_MULTIADDR_NAME_MAXLEN. |
Pau Espin Pedrol | 5ac8aa5 | 2023-11-30 19:12:16 +0100 | [diff] [blame] | 2072 | * \param[in] fd File descriptor of socket. |
| 2073 | * \param[in] fd IPPROTO of the socket, eg: IPPROTO_SCTP. |
| 2074 | * \return String length as returned by snprintf(), or negative on error. |
| 2075 | */ |
| 2076 | int osmo_sock_multiaddr_get_name_buf(char *str, size_t str_len, int fd, int sk_proto) |
| 2077 | { |
| 2078 | char hostbuf[OSMO_SOCK_MAX_ADDRS][INET6_ADDRSTRLEN]; |
| 2079 | size_t num_hostbuf = ARRAY_SIZE(hostbuf); |
| 2080 | char portbuf[6]; |
| 2081 | struct osmo_strbuf sb = { .buf = str, .len = str_len }; |
| 2082 | |
| 2083 | if (fd < 0) { |
| 2084 | osmo_strlcpy(str, "<error-bad-fd>", str_len); |
| 2085 | return sb.chars_needed; |
| 2086 | } |
| 2087 | |
| 2088 | switch (sk_proto) { |
| 2089 | case IPPROTO_SCTP: |
| 2090 | break; /* continue below */ |
| 2091 | default: |
| 2092 | return osmo_sock_get_name_buf(str, str_len, fd); |
| 2093 | } |
| 2094 | |
| 2095 | /* get remote */ |
| 2096 | OSMO_STRBUF_PRINTF(sb, "r="); |
| 2097 | if (osmo_sock_multiaddr_get_ip_and_port(fd, sk_proto, &hostbuf[0][0], &num_hostbuf, |
| 2098 | sizeof(hostbuf[0]), portbuf, sizeof(portbuf), false) != 0) { |
| 2099 | OSMO_STRBUF_PRINTF(sb, "NULL"); |
| 2100 | } else { |
| 2101 | const bool need_more_bufs = num_hostbuf > ARRAY_SIZE(hostbuf); |
| 2102 | if (need_more_bufs) |
| 2103 | num_hostbuf = ARRAY_SIZE(hostbuf); |
| 2104 | OSMO_STRBUF_APPEND(sb, osmo_multiaddr_ip_and_port_snprintf, |
| 2105 | &hostbuf[0][0], num_hostbuf, sizeof(hostbuf[0]), portbuf); |
| 2106 | if (need_more_bufs) |
| 2107 | OSMO_STRBUF_PRINTF(sb, "<need-more-bufs!>"); |
| 2108 | } |
| 2109 | |
| 2110 | OSMO_STRBUF_PRINTF(sb, "<->l="); |
| 2111 | |
| 2112 | /* get local */ |
| 2113 | num_hostbuf = ARRAY_SIZE(hostbuf); |
| 2114 | if (osmo_sock_multiaddr_get_ip_and_port(fd, sk_proto, &hostbuf[0][0], &num_hostbuf, |
| 2115 | sizeof(hostbuf[0]), portbuf, sizeof(portbuf), true) != 0) { |
| 2116 | OSMO_STRBUF_PRINTF(sb, "NULL"); |
| 2117 | } else { |
| 2118 | const bool need_more_bufs = num_hostbuf > ARRAY_SIZE(hostbuf); |
| 2119 | if (need_more_bufs) |
| 2120 | num_hostbuf = ARRAY_SIZE(hostbuf); |
| 2121 | OSMO_STRBUF_APPEND(sb, osmo_multiaddr_ip_and_port_snprintf, |
| 2122 | &hostbuf[0][0], num_hostbuf, sizeof(hostbuf[0]), portbuf); |
| 2123 | if (need_more_bufs) |
| 2124 | OSMO_STRBUF_PRINTF(sb, "<need-more-bufs!>"); |
| 2125 | } |
| 2126 | |
| 2127 | return sb.chars_needed; |
| 2128 | } |
Andreas Eversberg | e8ab1b7 | 2024-02-22 15:38:47 +0100 | [diff] [blame] | 2129 | #endif |
Pau Espin Pedrol | 5ac8aa5 | 2023-11-30 19:12:16 +0100 | [diff] [blame] | 2130 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 2131 | /*! Get address/port information on socket in provided string buffer, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 2132 | * This does not include braces like osmo_sock_get_name(). |
| 2133 | * \param[out] str Destination string buffer. |
| 2134 | * \param[in] str_len sizeof(str). |
| 2135 | * \param[in] fd File descriptor of socket. |
| 2136 | * \return String length as returned by snprintf(), or negative on error. |
| 2137 | */ |
| 2138 | int osmo_sock_get_name_buf(char *str, size_t str_len, int fd) |
| 2139 | { |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 2140 | char hostbuf_l[INET6_ADDRSTRLEN], hostbuf_r[INET6_ADDRSTRLEN]; |
| 2141 | char portbuf_l[6], portbuf_r[6]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 2142 | int rc; |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 2143 | |
Daniel Willmann | 7052cc6 | 2023-06-16 09:53:15 +0200 | [diff] [blame] | 2144 | if (fd < 0) { |
| 2145 | osmo_strlcpy(str, "<error-bad-fd>", str_len); |
| 2146 | return -EBADF; |
| 2147 | } |
| 2148 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 2149 | /* get local */ |
Harald Welte | ea9ea52 | 2019-05-10 09:28:24 +0200 | [diff] [blame] | 2150 | if ((rc = osmo_sock_get_ip_and_port(fd, hostbuf_l, sizeof(hostbuf_l), portbuf_l, sizeof(portbuf_l), true))) { |
| 2151 | osmo_strlcpy(str, "<error-in-getsockname>", str_len); |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 2152 | return rc; |
Harald Welte | ea9ea52 | 2019-05-10 09:28:24 +0200 | [diff] [blame] | 2153 | } |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 2154 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 2155 | /* get remote */ |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 2156 | if (osmo_sock_get_ip_and_port(fd, hostbuf_r, sizeof(hostbuf_r), portbuf_r, sizeof(portbuf_r), false) != 0) |
| 2157 | 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] | 2158 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 2159 | return snprintf(str, str_len, "r=%s:%s<->l=%s:%s", hostbuf_r, portbuf_r, hostbuf_l, portbuf_l); |
| 2160 | } |
| 2161 | |
| 2162 | /*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 2163 | * This does not include braces like osmo_sock_get_name(). |
| 2164 | * \param[in] fd File descriptor of socket. |
| 2165 | * \return Static string buffer containing the result. |
| 2166 | */ |
| 2167 | const char *osmo_sock_get_name2(int fd) |
| 2168 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 2169 | static __thread char str[OSMO_SOCK_NAME_MAXLEN]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 2170 | osmo_sock_get_name_buf(str, sizeof(str), fd); |
| 2171 | return str; |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 2172 | } |
| 2173 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 2174 | /*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 2175 | * This does not include braces like osmo_sock_get_name(). |
| 2176 | * \param[in] fd File descriptor of socket. |
| 2177 | * \return Static string buffer containing the result. |
| 2178 | */ |
| 2179 | char *osmo_sock_get_name2_c(const void *ctx, int fd) |
| 2180 | { |
| 2181 | char *str = talloc_size(ctx, OSMO_SOCK_NAME_MAXLEN); |
| 2182 | if (!str) |
| 2183 | return NULL; |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 2184 | osmo_sock_get_name_buf(str, OSMO_SOCK_NAME_MAXLEN, fd); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 2185 | return str; |
| 2186 | } |
| 2187 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 2188 | static int sock_get_domain(int fd) |
| 2189 | { |
| 2190 | int domain; |
| 2191 | #ifdef SO_DOMAIN |
| 2192 | socklen_t dom_len = sizeof(domain); |
| 2193 | int rc; |
| 2194 | |
| 2195 | rc = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &dom_len); |
| 2196 | if (rc < 0) |
| 2197 | return rc; |
| 2198 | #else |
| 2199 | /* This of course sucks, but what shall we do on OSs like |
| 2200 | * FreeBSD that don't seem to expose a method by which one can |
| 2201 | * learn the address family of a socket? */ |
| 2202 | domain = AF_INET; |
| 2203 | #endif |
| 2204 | return domain; |
| 2205 | } |
| 2206 | |
| 2207 | |
| 2208 | /*! Activate or de-activate local loop-back of transmitted multicast packets |
| 2209 | * \param[in] fd file descriptor of related socket |
| 2210 | * \param[in] enable Enable (true) or disable (false) loop-back |
| 2211 | * \returns 0 on success; negative otherwise */ |
| 2212 | int osmo_sock_mcast_loop_set(int fd, bool enable) |
| 2213 | { |
| 2214 | int domain, loop = 0; |
| 2215 | |
| 2216 | if (enable) |
| 2217 | loop = 1; |
| 2218 | |
| 2219 | domain = sock_get_domain(fd); |
| 2220 | if (domain < 0) |
| 2221 | return domain; |
| 2222 | |
| 2223 | switch (domain) { |
| 2224 | case AF_INET: |
| 2225 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 2226 | case AF_INET6: |
| 2227 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 2228 | default: |
| 2229 | return -EINVAL; |
| 2230 | } |
| 2231 | } |
| 2232 | |
| 2233 | /*! Set the TTL of outbound multicast packets |
| 2234 | * \param[in] fd file descriptor of related socket |
| 2235 | * \param[in] ttl TTL of to-be-sent multicast packets |
| 2236 | * \returns 0 on success; negative otherwise */ |
| 2237 | int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl) |
| 2238 | { |
| 2239 | int domain, ttli = ttl; |
| 2240 | |
| 2241 | domain = sock_get_domain(fd); |
| 2242 | if (domain < 0) |
| 2243 | return domain; |
| 2244 | |
| 2245 | switch (domain) { |
| 2246 | case AF_INET: |
| 2247 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttli, sizeof(ttli)); |
| 2248 | case AF_INET6: |
| 2249 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttli, sizeof(ttli)); |
| 2250 | default: |
| 2251 | return -EINVAL; |
| 2252 | } |
| 2253 | } |
| 2254 | |
Harald Welte | 44b9926 | 2020-03-07 14:59:05 +0100 | [diff] [blame] | 2255 | /*! Set the network device to which we should bind the multicast socket |
| 2256 | * \param[in] fd file descriptor of related socket |
| 2257 | * \param[in] ifname name of network interface to user for multicast |
| 2258 | * \returns 0 on success; negative otherwise */ |
| 2259 | int osmo_sock_mcast_iface_set(int fd, const char *ifname) |
| 2260 | { |
| 2261 | unsigned int ifindex; |
| 2262 | struct ip_mreqn mr; |
| 2263 | |
| 2264 | /* first, resolve interface name to ifindex */ |
| 2265 | ifindex = if_nametoindex(ifname); |
| 2266 | if (ifindex == 0) |
| 2267 | return -errno; |
| 2268 | |
| 2269 | /* next, configure kernel to use that ifindex for this sockets multicast traffic */ |
| 2270 | memset(&mr, 0, sizeof(mr)); |
| 2271 | mr.imr_ifindex = ifindex; |
| 2272 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mr, sizeof(mr)); |
| 2273 | } |
| 2274 | |
| 2275 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 2276 | /*! Enable/disable receiving all multicast packets, even for non-subscribed groups |
| 2277 | * \param[in] fd file descriptor of related socket |
| 2278 | * \param[in] enable Enable or Disable receiving of all packets |
| 2279 | * \returns 0 on success; negative otherwise */ |
| 2280 | int osmo_sock_mcast_all_set(int fd, bool enable) |
| 2281 | { |
| 2282 | int domain, all = 0; |
| 2283 | |
| 2284 | if (enable) |
| 2285 | all = 1; |
| 2286 | |
| 2287 | domain = sock_get_domain(fd); |
| 2288 | if (domain < 0) |
| 2289 | return domain; |
| 2290 | |
| 2291 | switch (domain) { |
| 2292 | case AF_INET: |
| 2293 | #ifdef IP_MULTICAST_ALL |
| 2294 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)); |
| 2295 | #endif |
| 2296 | case AF_INET6: |
| 2297 | /* there seems no equivalent ?!? */ |
| 2298 | default: |
| 2299 | return -EINVAL; |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | /* FreeBSD calls the socket option differently */ |
| 2304 | #if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP) |
| 2305 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP |
| 2306 | #endif |
| 2307 | |
| 2308 | /*! Subscribe to the given IP multicast group |
| 2309 | * \param[in] fd file descriptor of related scoket |
| 2310 | * \param[in] grp_addr ASCII representation of the multicast group address |
| 2311 | * \returns 0 on success; negative otherwise */ |
| 2312 | int osmo_sock_mcast_subscribe(int fd, const char *grp_addr) |
| 2313 | { |
| 2314 | int rc, domain; |
| 2315 | struct ip_mreq mreq; |
| 2316 | struct ipv6_mreq mreq6; |
| 2317 | struct in6_addr i6a; |
| 2318 | |
| 2319 | domain = sock_get_domain(fd); |
| 2320 | if (domain < 0) |
| 2321 | return domain; |
| 2322 | |
| 2323 | switch (domain) { |
| 2324 | case AF_INET: |
| 2325 | memset(&mreq, 0, sizeof(mreq)); |
| 2326 | mreq.imr_multiaddr.s_addr = inet_addr(grp_addr); |
| 2327 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
| 2328 | return setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 2329 | #ifdef IPV6_ADD_MEMBERSHIP |
| 2330 | case AF_INET6: |
| 2331 | memset(&mreq6, 0, sizeof(mreq6)); |
| 2332 | rc = inet_pton(AF_INET6, grp_addr, (void *)&i6a); |
| 2333 | if (rc < 0) |
| 2334 | return -EINVAL; |
| 2335 | mreq6.ipv6mr_multiaddr = i6a; |
| 2336 | return setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)); |
| 2337 | #endif |
| 2338 | default: |
| 2339 | return -EINVAL; |
| 2340 | } |
| 2341 | } |
| 2342 | |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 2343 | /*! Determine the matching local IP-address for a given remote IP-Address. |
| 2344 | * \param[out] local_ip caller provided memory for resulting local IP-address |
| 2345 | * \param[in] remote_ip remote IP-address |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 2346 | * \returns 0 on success; negative otherwise |
| 2347 | * |
| 2348 | * The function accepts IPv4 and IPv6 address strings. The caller must provide |
| 2349 | * at least INET6_ADDRSTRLEN bytes for local_ip if an IPv6 is expected as |
| 2350 | * as result. For IPv4 addresses the required amount is INET_ADDRSTRLEN. */ |
| 2351 | int osmo_sock_local_ip(char *local_ip, const char *remote_ip) |
| 2352 | { |
| 2353 | int sfd; |
| 2354 | int rc; |
| 2355 | struct addrinfo addrinfo_hint; |
| 2356 | struct addrinfo *addrinfo = NULL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 2357 | struct sockaddr_storage local_addr; |
| 2358 | struct sockaddr_in *sin; |
| 2359 | struct sockaddr_in6 *sin6; |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 2360 | socklen_t local_addr_len; |
| 2361 | uint16_t family; |
| 2362 | |
| 2363 | /* Find out the address family (AF_INET or AF_INET6?) */ |
| 2364 | memset(&addrinfo_hint, '\0', sizeof(addrinfo_hint)); |
Pau Espin Pedrol | 308ab79 | 2020-08-28 19:47:25 +0200 | [diff] [blame] | 2365 | addrinfo_hint.ai_family = AF_UNSPEC; |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 2366 | addrinfo_hint.ai_flags = AI_NUMERICHOST; |
| 2367 | rc = getaddrinfo(remote_ip, NULL, &addrinfo_hint, &addrinfo); |
| 2368 | if (rc) |
| 2369 | return -EINVAL; |
| 2370 | family = addrinfo->ai_family; |
| 2371 | freeaddrinfo(addrinfo); |
| 2372 | |
| 2373 | /* Connect a dummy socket to trick the kernel into determining the |
| 2374 | * ip-address of the interface that would be used if we would send |
| 2375 | * out an actual packet */ |
| 2376 | sfd = osmo_sock_init2(family, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, remote_ip, 0, OSMO_SOCK_F_CONNECT); |
| 2377 | if (sfd < 0) |
| 2378 | return -EINVAL; |
| 2379 | |
| 2380 | /* Request the IP address of the interface that the kernel has |
| 2381 | * actually choosen. */ |
| 2382 | memset(&local_addr, 0, sizeof(local_addr)); |
| 2383 | local_addr_len = sizeof(local_addr); |
| 2384 | rc = getsockname(sfd, (struct sockaddr *)&local_addr, &local_addr_len); |
Philipp Maier | 8b7975b | 2018-01-22 15:38:07 +0100 | [diff] [blame] | 2385 | close(sfd); |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 2386 | if (rc < 0) |
| 2387 | return -EINVAL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 2388 | |
| 2389 | switch (local_addr.ss_family) { |
| 2390 | case AF_INET: |
| 2391 | sin = (struct sockaddr_in*)&local_addr; |
| 2392 | if (!inet_ntop(AF_INET, &sin->sin_addr, local_ip, INET_ADDRSTRLEN)) |
| 2393 | return -EINVAL; |
| 2394 | break; |
| 2395 | case AF_INET6: |
| 2396 | sin6 = (struct sockaddr_in6*)&local_addr; |
Pau Espin Pedrol | 7bda854 | 2020-08-31 11:21:30 +0200 | [diff] [blame] | 2397 | 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] | 2398 | return -EINVAL; |
| 2399 | break; |
| 2400 | default: |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 2401 | return -EINVAL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 2402 | } |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 2403 | |
| 2404 | return 0; |
| 2405 | } |
| 2406 | |
Alexander Couzens | 0f36421 | 2020-07-27 22:33:01 +0200 | [diff] [blame] | 2407 | /*! Determine the matching local address for a given remote address. |
| 2408 | * \param[out] local_ip caller provided memory for resulting local address |
| 2409 | * \param[in] remote_ip remote address |
| 2410 | * \returns 0 on success; negative otherwise |
| 2411 | */ |
| 2412 | int osmo_sockaddr_local_ip(struct osmo_sockaddr *local_ip, const struct osmo_sockaddr *remote_ip) |
| 2413 | { |
| 2414 | int sfd; |
| 2415 | int rc; |
| 2416 | socklen_t local_ip_len; |
| 2417 | |
| 2418 | sfd = osmo_sock_init_osa(SOCK_DGRAM, IPPROTO_UDP, NULL, remote_ip, OSMO_SOCK_F_CONNECT); |
| 2419 | if (sfd < 0) |
| 2420 | return -EINVAL; |
| 2421 | |
| 2422 | memset(local_ip, 0, sizeof(*local_ip)); |
| 2423 | local_ip_len = sizeof(*local_ip); |
| 2424 | rc = getsockname(sfd, (struct sockaddr *)local_ip, &local_ip_len); |
| 2425 | close(sfd); |
| 2426 | |
| 2427 | return rc; |
| 2428 | } |
| 2429 | |
Neels Hofmeyr | cf7f792 | 2022-02-02 03:08:40 +0100 | [diff] [blame] | 2430 | /*! Copy the addr part, the IP address octets in network byte order, to a buffer. |
| 2431 | * Useful for encoding network protocols. |
| 2432 | * \param[out] dst Write octets to this buffer. |
| 2433 | * \param[in] dst_maxlen Space available in buffer. |
| 2434 | * \param[in] os Sockaddr to copy IP of. |
| 2435 | * \return nr of octets written on success, negative on error. |
| 2436 | */ |
| 2437 | int osmo_sockaddr_to_octets(uint8_t *dst, size_t dst_maxlen, const struct osmo_sockaddr *os) |
| 2438 | { |
| 2439 | const void *addr; |
| 2440 | size_t len; |
Neels Hofmeyr | 1790f17 | 2022-02-02 03:08:40 +0100 | [diff] [blame] | 2441 | switch (os->u.sa.sa_family) { |
Neels Hofmeyr | cf7f792 | 2022-02-02 03:08:40 +0100 | [diff] [blame] | 2442 | case AF_INET: |
| 2443 | addr = &os->u.sin.sin_addr; |
| 2444 | len = sizeof(os->u.sin.sin_addr); |
| 2445 | break; |
| 2446 | case AF_INET6: |
| 2447 | addr = &os->u.sin6.sin6_addr; |
| 2448 | len = sizeof(os->u.sin6.sin6_addr); |
| 2449 | break; |
| 2450 | default: |
| 2451 | return -ENOTSUP; |
| 2452 | } |
| 2453 | if (dst_maxlen < len) |
| 2454 | return -ENOSPC; |
| 2455 | memcpy(dst, addr, len); |
| 2456 | return len; |
| 2457 | } |
| 2458 | |
| 2459 | /*! Copy the addr part, the IP address octets in network byte order, from a buffer. |
| 2460 | * Useful for decoding network protocols. |
| 2461 | * \param[out] os Write IP address to this sockaddr. |
| 2462 | * \param[in] src Source buffer to read IP address octets from. |
| 2463 | * \param[in] src_len Number of octets to copy. |
| 2464 | * \return number of octets read on success, negative on error. |
| 2465 | */ |
| 2466 | int osmo_sockaddr_from_octets(struct osmo_sockaddr *os, const void *src, size_t src_len) |
| 2467 | { |
| 2468 | void *addr; |
| 2469 | size_t len; |
| 2470 | *os = (struct osmo_sockaddr){0}; |
| 2471 | switch (src_len) { |
| 2472 | case sizeof(os->u.sin.sin_addr): |
Neels Hofmeyr | 1790f17 | 2022-02-02 03:08:40 +0100 | [diff] [blame] | 2473 | os->u.sa.sa_family = AF_INET; |
Neels Hofmeyr | cf7f792 | 2022-02-02 03:08:40 +0100 | [diff] [blame] | 2474 | addr = &os->u.sin.sin_addr; |
| 2475 | len = sizeof(os->u.sin.sin_addr); |
| 2476 | break; |
| 2477 | case sizeof(os->u.sin6.sin6_addr): |
| 2478 | os->u.sin6.sin6_family = AF_INET6; |
| 2479 | addr = &os->u.sin6.sin6_addr; |
| 2480 | len = sizeof(os->u.sin6.sin6_addr); |
| 2481 | break; |
| 2482 | default: |
| 2483 | return -ENOTSUP; |
| 2484 | } |
| 2485 | memcpy(addr, src, len); |
| 2486 | return len; |
| 2487 | } |
| 2488 | |
Alexander Couzens | e4181ea | 2020-07-20 00:03:16 +0200 | [diff] [blame] | 2489 | /*! Compare two osmo_sockaddr. |
| 2490 | * \param[in] a |
| 2491 | * \param[in] b |
| 2492 | * \return 0 if a and b are equal. Otherwise it follows memcmp() |
| 2493 | */ |
Vadim Yanitskiy | def5a40 | 2020-10-09 21:40:47 +0700 | [diff] [blame] | 2494 | int osmo_sockaddr_cmp(const struct osmo_sockaddr *a, |
| 2495 | const struct osmo_sockaddr *b) |
Alexander Couzens | e4181ea | 2020-07-20 00:03:16 +0200 | [diff] [blame] | 2496 | { |
| 2497 | if (a == b) |
| 2498 | return 0; |
| 2499 | if (!a) |
| 2500 | return 1; |
| 2501 | if (!b) |
| 2502 | return -1; |
| 2503 | |
| 2504 | if (a->u.sa.sa_family != b->u.sa.sa_family) { |
| 2505 | return OSMO_CMP(a->u.sa.sa_family, b->u.sa.sa_family); |
| 2506 | } |
| 2507 | |
| 2508 | switch (a->u.sa.sa_family) { |
| 2509 | case AF_INET: |
| 2510 | return memcmp(&a->u.sin, &b->u.sin, sizeof(struct sockaddr_in)); |
| 2511 | case AF_INET6: |
| 2512 | return memcmp(&a->u.sin6, &b->u.sin6, sizeof(struct sockaddr_in6)); |
| 2513 | default: |
| 2514 | /* fallback to memcmp for remaining AF over the full osmo_sockaddr length */ |
| 2515 | return memcmp(a, b, sizeof(struct osmo_sockaddr)); |
| 2516 | } |
| 2517 | } |
| 2518 | |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 2519 | /*! string-format a given osmo_sockaddr address |
| 2520 | * \param[in] sockaddr the osmo_sockaddr to print |
| 2521 | * \return pointer to the string on success; NULL on error |
| 2522 | */ |
| 2523 | const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *sockaddr) |
| 2524 | { |
| 2525 | /* INET6_ADDRSTRLEN contains already a null termination, |
| 2526 | * adding '[' ']' ':' '16 bit port' */ |
| 2527 | static __thread char buf[INET6_ADDRSTRLEN + 8]; |
| 2528 | return osmo_sockaddr_to_str_buf(buf, sizeof(buf), sockaddr); |
| 2529 | } |
| 2530 | |
Neels Hofmeyr | 09d6574 | 2021-12-01 10:25:02 +0100 | [diff] [blame] | 2531 | /*! string-format a given osmo_sockaddr address into a user-supplied buffer. |
| 2532 | * Same as osmo_sockaddr_to_str_buf() but returns a would-be length in snprintf() style. |
| 2533 | * \param[in] buf user-supplied output buffer |
| 2534 | * \param[in] buf_len size of the user-supplied output buffer in bytes |
| 2535 | * \param[in] sockaddr the osmo_sockaddr to print |
| 2536 | * \return number of characters that would be written if the buffer is large enough, like snprintf(). |
| 2537 | */ |
| 2538 | int osmo_sockaddr_to_str_buf2(char *buf, size_t buf_len, const struct osmo_sockaddr *sockaddr) |
| 2539 | { |
| 2540 | struct osmo_strbuf sb = { .buf = buf, .len = buf_len }; |
| 2541 | uint16_t port = 0; |
| 2542 | |
| 2543 | if (!sockaddr) { |
| 2544 | OSMO_STRBUF_PRINTF(sb, "NULL"); |
| 2545 | return sb.chars_needed; |
| 2546 | } |
| 2547 | |
| 2548 | switch (sockaddr->u.sa.sa_family) { |
| 2549 | case AF_INET: |
| 2550 | OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_and_uint, &port, &sockaddr->u.sa); |
| 2551 | if (port) |
| 2552 | OSMO_STRBUF_PRINTF(sb, ":%u", port); |
| 2553 | break; |
| 2554 | case AF_INET6: |
| 2555 | OSMO_STRBUF_PRINTF(sb, "["); |
| 2556 | OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_and_uint, &port, &sockaddr->u.sa); |
| 2557 | OSMO_STRBUF_PRINTF(sb, "]"); |
| 2558 | if (port) |
| 2559 | OSMO_STRBUF_PRINTF(sb, ":%u", port); |
| 2560 | break; |
| 2561 | default: |
| 2562 | OSMO_STRBUF_PRINTF(sb, "unsupported family %d", sockaddr->u.sa.sa_family); |
| 2563 | break; |
| 2564 | } |
| 2565 | |
| 2566 | return sb.chars_needed; |
| 2567 | } |
| 2568 | |
| 2569 | /*! string-format a given osmo_sockaddr address into a talloc allocated buffer. |
| 2570 | * Like osmo_sockaddr_to_str_buf2() but returns a talloc allocated string. |
| 2571 | * \param[in] ctx talloc context to allocate from, e.g. OTC_SELECT. |
| 2572 | * \param[in] sockaddr the osmo_sockaddr to print. |
| 2573 | * \return human readable string. |
| 2574 | */ |
| 2575 | char *osmo_sockaddr_to_str_c(void *ctx, const struct osmo_sockaddr *sockaddr) |
| 2576 | { |
| 2577 | OSMO_NAME_C_IMPL(ctx, 64, "ERROR", osmo_sockaddr_to_str_buf2, sockaddr) |
| 2578 | } |
| 2579 | |
| 2580 | /*! string-format a given osmo_sockaddr address into a user-supplied buffer. |
| 2581 | * Like osmo_sockaddr_to_str_buf2() but returns buf, or NULL if too short. |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 2582 | * \param[in] buf user-supplied output buffer |
| 2583 | * \param[in] buf_len size of the user-supplied output buffer in bytes |
| 2584 | * \param[in] sockaddr the osmo_sockaddr to print |
| 2585 | * \return pointer to the string on success; NULL on error |
| 2586 | */ |
| 2587 | char *osmo_sockaddr_to_str_buf(char *buf, size_t buf_len, |
| 2588 | const struct osmo_sockaddr *sockaddr) |
| 2589 | { |
Neels Hofmeyr | 09d6574 | 2021-12-01 10:25:02 +0100 | [diff] [blame] | 2590 | int chars_needed = osmo_sockaddr_to_str_buf2(buf, buf_len, sockaddr); |
| 2591 | if (chars_needed >= buf_len) |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 2592 | return NULL; |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 2593 | return buf; |
| 2594 | } |
| 2595 | |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 2596 | /*! Set the DSCP (differentiated services code point) of a socket. |
| 2597 | * \param[in] dscp DSCP value in range 0..63 |
| 2598 | * \returns 0 on success; negative on error. */ |
| 2599 | int osmo_sock_set_dscp(int fd, uint8_t dscp) |
| 2600 | { |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 2601 | struct sockaddr_storage local_addr; |
| 2602 | socklen_t local_addr_len = sizeof(local_addr); |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 2603 | uint8_t tos; |
| 2604 | socklen_t tos_len = sizeof(tos); |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 2605 | int tclass; |
| 2606 | socklen_t tclass_len = sizeof(tclass); |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 2607 | int rc; |
| 2608 | |
| 2609 | /* DSCP is a 6-bit value stored in the upper 6 bits of the 8-bit TOS */ |
| 2610 | if (dscp > 63) |
| 2611 | return -EINVAL; |
| 2612 | |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 2613 | rc = getsockname(fd, (struct sockaddr *)&local_addr, &local_addr_len); |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 2614 | if (rc < 0) |
| 2615 | return rc; |
| 2616 | |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 2617 | switch (local_addr.ss_family) { |
| 2618 | case AF_INET: |
| 2619 | /* read the original value */ |
| 2620 | rc = getsockopt(fd, IPPROTO_IP, IP_TOS, &tos, &tos_len); |
| 2621 | if (rc < 0) |
| 2622 | return rc; |
| 2623 | /* mask-in the DSCP into the upper 6 bits */ |
| 2624 | tos &= 0x03; |
| 2625 | tos |= dscp << 2; |
| 2626 | /* and write it back to the kernel */ |
| 2627 | rc = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2628 | break; |
| 2629 | case AF_INET6: |
| 2630 | /* read the original value */ |
| 2631 | rc = getsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tclass, &tclass_len); |
| 2632 | if (rc < 0) |
| 2633 | return rc; |
| 2634 | /* mask-in the DSCP into the upper 6 bits */ |
| 2635 | tclass &= 0x03; |
| 2636 | tclass |= dscp << 2; |
| 2637 | /* and write it back to the kernel */ |
| 2638 | rc = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof(tclass)); |
| 2639 | break; |
| 2640 | case AF_UNSPEC: |
| 2641 | default: |
| 2642 | LOGP(DLGLOBAL, LOGL_ERROR, "No DSCP support for socket family %u\n", |
| 2643 | local_addr.ss_family); |
| 2644 | rc = -1; |
| 2645 | break; |
| 2646 | } |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 2647 | |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 2648 | return rc; |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 2649 | } |
| 2650 | |
Harald Welte | ecc0bd8 | 2021-04-27 22:24:08 +0200 | [diff] [blame] | 2651 | /*! Set the priority value of a socket. |
| 2652 | * \param[in] prio priority value. Values outside 0..6 require CAP_NET_ADMIN. |
| 2653 | * \returns 0 on success; negative on error. */ |
| 2654 | int osmo_sock_set_priority(int fd, int prio) |
| 2655 | { |
| 2656 | /* and write it back to the kernel */ |
| 2657 | return setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)); |
| 2658 | } |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 2659 | |
Pau Espin Pedrol | 19f27bb | 2023-12-08 14:58:51 +0100 | [diff] [blame] | 2660 | #ifdef HAVE_LIBSCTP |
| 2661 | /*! Fill in array of struct sctp_paddrinfo with each of the remote addresses of an SCTP socket |
| 2662 | * \param[in] fd file descriptor of SCTP socket |
| 2663 | * \param[out] pinfo Pointer to memory holding an array of struct sctp_paddrinfo (pinfo_cnt length). |
| 2664 | * \param[in,out] pinfo_cnt length of pinfo array (in elements). On return it contains the number of addresses found. |
| 2665 | * \returns 0 on success; negative otherwise |
| 2666 | * |
| 2667 | * Upon return, pinfo_cnt can be set to a higher value than the one set by the |
| 2668 | * caller. This can be used by the caller to find out the required array length |
| 2669 | * and then obtaining by calling the function twice. Only up to pinfo_cnt addresses |
| 2670 | * are filled in, as per the value provided by the caller. |
| 2671 | * |
| 2672 | * Usage example retrieving struct sctp_paddrinfo for all (up to OSMO_SOCK_MAX_ADDRS, 32) remote IP addresses: |
| 2673 | * struct sctp_paddrinfo pinfo[OSMO_SOCK_MAX_ADDRS]; |
| 2674 | * size_t pinfo_cnt = ARRAY_SIZE(pinfo); |
| 2675 | * rc = osmo_sock_sctp_get_peer_addr_info(fd, &pinfo[0], &num_hostbuf, pinfo_cnt); |
| 2676 | * if (rc < 0) |
| 2677 | * goto error; |
| 2678 | * if (pinfo_cnt > ARRAY_SIZE(hostbuf)) |
| 2679 | * goto not_enough_buffers; |
| 2680 | */ |
| 2681 | int osmo_sock_sctp_get_peer_addr_info(int fd, struct sctp_paddrinfo *pinfo, size_t *pinfo_cnt) |
| 2682 | { |
| 2683 | struct sockaddr *addrs = NULL; |
| 2684 | unsigned int n_addrs, i; |
| 2685 | void *addr_buf; |
| 2686 | int rc; |
| 2687 | socklen_t optlen; |
| 2688 | |
| 2689 | rc = sctp_getpaddrs(fd, 0, &addrs); |
| 2690 | |
| 2691 | if (rc < 0) |
| 2692 | return rc; |
| 2693 | if (rc == 0) |
| 2694 | return -ENOTCONN; |
| 2695 | |
| 2696 | n_addrs = rc; |
| 2697 | addr_buf = (void *)addrs; |
| 2698 | for (i = 0; i < n_addrs; i++) { |
| 2699 | struct sockaddr *sa_addr = (struct sockaddr *)addr_buf; |
| 2700 | size_t addrlen; |
| 2701 | |
| 2702 | switch (sa_addr->sa_family) { |
| 2703 | case AF_INET: |
| 2704 | addrlen = sizeof(struct sockaddr_in); |
| 2705 | break; |
| 2706 | case AF_INET6: |
| 2707 | addrlen = sizeof(struct sockaddr_in6); |
| 2708 | break; |
| 2709 | default: |
| 2710 | rc = -EINVAL; |
| 2711 | goto free_addrs_ret; |
| 2712 | } |
| 2713 | |
| 2714 | if (i >= *pinfo_cnt) { |
| 2715 | addr_buf += addrlen; |
| 2716 | continue; |
| 2717 | } |
| 2718 | |
| 2719 | memset(&pinfo[i], 0, sizeof(pinfo[0])); |
| 2720 | memcpy(&pinfo[i].spinfo_address, sa_addr, addrlen); |
| 2721 | optlen = sizeof(pinfo[0]); |
| 2722 | rc = getsockopt(fd, SOL_SCTP, SCTP_GET_PEER_ADDR_INFO, &pinfo[i], &optlen); |
| 2723 | if (rc < 0) |
| 2724 | goto free_addrs_ret; |
| 2725 | |
| 2726 | addr_buf += addrlen; |
| 2727 | } |
| 2728 | |
| 2729 | *pinfo_cnt = n_addrs; |
| 2730 | rc = 0; |
| 2731 | free_addrs_ret: |
| 2732 | sctp_freepaddrs(addrs); |
| 2733 | return rc; |
| 2734 | } |
| 2735 | #endif |
| 2736 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 2737 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 2738 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 2739 | /*! @} */ |