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 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 20 | #include "../config.h" |
| 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)); |
| 252 | return rc; |
| 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 { \ |
| 487 | if (osmo_sockaddr_str_from_sockaddr(&dest, &sockaddr->u.sas)) \ |
| 488 | osmo_strlcpy(dest.ip, "Invalid IP", 11); \ |
| 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; |
| 523 | struct osmo_sockaddr_str sastr = {}; |
| 524 | |
| 525 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 526 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 527 | "BIND or CONNECT flags\n"); |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | |
| 531 | if ((flags & OSMO_SOCK_F_BIND) && !local) { |
| 532 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid argument. Cannot BIND when local is NULL\n"); |
| 533 | return -EINVAL; |
| 534 | } |
| 535 | |
| 536 | if ((flags & OSMO_SOCK_F_CONNECT) && !remote) { |
| 537 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid argument. Cannot CONNECT when remote is NULL\n"); |
| 538 | return -EINVAL; |
| 539 | } |
| 540 | |
| 541 | if ((flags & OSMO_SOCK_F_BIND) && |
| 542 | (flags & OSMO_SOCK_F_CONNECT) && |
| 543 | local->u.sa.sa_family != remote->u.sa.sa_family) { |
| 544 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: the family for " |
| 545 | "local and remote endpoint must be same.\n"); |
| 546 | return -EINVAL; |
| 547 | } |
| 548 | |
| 549 | /* figure out local side of socket */ |
| 550 | if (flags & OSMO_SOCK_F_BIND) { |
| 551 | sfd = socket_helper_osa(local, type, proto, flags); |
| 552 | if (sfd < 0) { |
| 553 | _SOCKADDR_TO_STR(sastr, local); |
| 554 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: %s:%u\n", |
| 555 | sastr.ip, sastr.port); |
| 556 | return -ENODEV; |
| 557 | } |
| 558 | |
| 559 | if (proto != IPPROTO_UDP || (flags & OSMO_SOCK_F_UDP_REUSEADDR)) { |
| 560 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 561 | &on, sizeof(on)); |
| 562 | if (rc < 0) { |
| 563 | _SOCKADDR_TO_STR(sastr, local); |
| 564 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 565 | "cannot setsockopt socket:" |
| 566 | " %s:%u: %s\n", |
| 567 | sastr.ip, sastr.port, |
| 568 | strerror(errno)); |
| 569 | close(sfd); |
| 570 | return rc; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | if (bind(sfd, &local->u.sa, sizeof(struct osmo_sockaddr)) == -1) { |
| 575 | _SOCKADDR_TO_STR(sastr, local); |
| 576 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n", |
| 577 | sastr.ip, sastr.port, strerror(errno)); |
| 578 | close(sfd); |
| 579 | return -1; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it |
| 584 | was already closed and func returned. If OSMO_SOCK_F_BIND is not |
| 585 | set, then sfd = -1 */ |
| 586 | |
| 587 | /* figure out remote side of socket */ |
| 588 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 589 | if (sfd < 0) { |
| 590 | sfd = socket_helper_osa(remote, type, proto, flags); |
| 591 | if (sfd < 0) { |
| 592 | return sfd; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | rc = connect(sfd, &remote->u.sa, sizeof(struct osmo_sockaddr)); |
| 597 | if (rc != 0 && errno != EINPROGRESS) { |
| 598 | _SOCKADDR_TO_STR(sastr, remote); |
| 599 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 600 | sastr.ip, sastr.port, strerror(errno)); |
| 601 | close(sfd); |
| 602 | return rc; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 607 | if (rc < 0) { |
| 608 | close(sfd); |
| 609 | sfd = -1; |
| 610 | } |
| 611 | |
| 612 | return sfd; |
| 613 | } |
| 614 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 615 | #ifdef HAVE_LIBSCTP |
| 616 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 617 | /* Check whether there's an IPv6 Addr as first option of any addrinfo item in the addrinfo set */ |
| 618 | static void addrinfo_has_v4v6addr(const struct addrinfo **result, size_t result_count, bool *has_v4, bool *has_v6) |
| 619 | { |
| 620 | size_t host_idx; |
| 621 | *has_v4 = false; |
| 622 | *has_v6 = false; |
| 623 | |
| 624 | for (host_idx = 0; host_idx < result_count; host_idx++) { |
| 625 | if (result[host_idx]->ai_family == AF_INET) |
| 626 | *has_v4 = true; |
| 627 | else if (result[host_idx]->ai_family == AF_INET6) |
| 628 | *has_v6 = true; |
| 629 | } |
| 630 | } |
| 631 | |
Pau Espin Pedrol | 4f463c5 | 2020-08-28 14:32:02 +0200 | [diff] [blame] | 632 | /* Check whether there's an IPv6 with IN6ADDR_ANY_INIT ("::") */ |
| 633 | static bool addrinfo_has_in6addr_any(const struct addrinfo **result, size_t result_count) |
| 634 | { |
| 635 | size_t host_idx; |
| 636 | struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; |
| 637 | |
| 638 | for (host_idx = 0; host_idx < result_count; host_idx++) { |
| 639 | if (result[host_idx]->ai_family != AF_INET6) |
| 640 | continue; |
| 641 | if (memcmp(&((struct sockaddr_in6 *)result[host_idx]->ai_addr)->sin6_addr, |
| 642 | &in6addr_any, sizeof(in6addr_any)) == 0) |
| 643 | return true; |
| 644 | } |
| 645 | return false; |
| 646 | } |
| 647 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 648 | static int socket_helper_multiaddr(uint16_t family, uint16_t type, uint8_t proto, unsigned int flags) |
| 649 | { |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 650 | int sfd, rc; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 651 | |
| 652 | sfd = socket(family, type, proto); |
| 653 | if (sfd == -1) { |
| 654 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 655 | "Unable to create socket: %s\n", strerror(errno)); |
| 656 | return sfd; |
| 657 | } |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 658 | |
| 659 | rc = socket_helper_tail(sfd, flags); |
| 660 | if (rc < 0) |
| 661 | return rc; |
| 662 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 663 | return sfd; |
| 664 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 665 | |
| 666 | /* 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] | 667 | * for each host in addrs_buf. */ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 668 | static int addrinfo_to_sockaddr(uint16_t family, const struct addrinfo **result, |
Harald Welte | 0b08d51 | 2022-01-09 12:03:12 +0100 | [diff] [blame] | 669 | const char **hosts, unsigned int host_cont, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 670 | uint8_t *addrs_buf, size_t addrs_buf_len) { |
| 671 | size_t host_idx, offset = 0; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 672 | const struct addrinfo *rp; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 673 | |
| 674 | for (host_idx = 0; host_idx < host_cont; host_idx++) { |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 675 | /* Addresses are ordered based on RFC 3484, see man getaddrinfo */ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 676 | for (rp = result[host_idx]; rp != NULL; rp = rp->ai_next) { |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 677 | if (family != AF_UNSPEC && rp->ai_family != family) |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 678 | continue; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 679 | if (offset + rp->ai_addrlen > addrs_buf_len) { |
| 680 | LOGP(DLGLOBAL, LOGL_ERROR, "Output buffer to small: %zu\n", |
| 681 | addrs_buf_len); |
| 682 | return -ENOSPC; |
| 683 | } |
| 684 | memcpy(addrs_buf + offset, rp->ai_addr, rp->ai_addrlen); |
| 685 | offset += rp->ai_addrlen; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 686 | break; |
| 687 | } |
| 688 | if (!rp) { /* No addr could be bound for this host! */ |
| 689 | LOGP(DLGLOBAL, LOGL_ERROR, "No suitable remote address found for host: %s\n", |
| 690 | hosts[host_idx]); |
| 691 | return -ENODEV; |
| 692 | } |
| 693 | } |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | /*! Initialize a socket (including bind and/or connect) with multiple local or remote addresses. |
| 698 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 699 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 700 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 701 | * \param[in] local_hosts array of char pointers (strings), each containing local host name or IP address in string form |
| 702 | * \param[in] local_hosts_cnt length of local_hosts (in items) |
| 703 | * \param[in] local_port local port number in host byte order |
| 704 | * \param[in] remote_host array of char pointers (strings), each containing remote host name or IP address in string form |
| 705 | * \param[in] remote_hosts_cnt length of remote_hosts (in items) |
| 706 | * \param[in] remote_port remote port number in host byte order |
| 707 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 708 | * \returns socket file descriptor on success; negative on error |
| 709 | * |
| 710 | * This function is similar to \ref osmo_sock_init2(), but can be passed an |
| 711 | * array of local or remote addresses for protocols supporting multiple |
| 712 | * addresses per socket, like SCTP (currently only one supported). This function |
| 713 | * should not be used by protocols not supporting this kind of features, but |
| 714 | * rather \ref osmo_sock_init2() should be used instead. |
| 715 | * See \ref osmo_sock_init2() for more information on flags and general behavior. |
| 716 | */ |
| 717 | int osmo_sock_init2_multiaddr(uint16_t family, uint16_t type, uint8_t proto, |
| 718 | const char **local_hosts, size_t local_hosts_cnt, uint16_t local_port, |
| 719 | const char **remote_hosts, size_t remote_hosts_cnt, uint16_t remote_port, |
| 720 | unsigned int flags) |
| 721 | |
| 722 | { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 723 | 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] | 724 | int sfd = -1, rc, on = 1; |
Harald Welte | 0b08d51 | 2022-01-09 12:03:12 +0100 | [diff] [blame] | 725 | unsigned int i; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 726 | bool loc_has_v4addr, rem_has_v4addr; |
| 727 | bool loc_has_v6addr, rem_has_v6addr; |
| 728 | struct sockaddr_in6 addrs_buf[OSMO_SOCK_MAX_ADDRS]; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 729 | char strbuf[512]; |
| 730 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 731 | /* updated later in case of AF_UNSPEC */ |
| 732 | loc_has_v4addr = rem_has_v4addr = (family == AF_INET); |
| 733 | loc_has_v6addr = rem_has_v6addr = (family == AF_INET6); |
| 734 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 735 | /* TODO: So far this function is only aimed for SCTP, but could be |
| 736 | reused in the future for other protocols with multi-addr support */ |
| 737 | if (proto != IPPROTO_SCTP) |
| 738 | return -ENOTSUP; |
| 739 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 740 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 741 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 742 | "BIND or CONNECT flags\n"); |
| 743 | return -EINVAL; |
| 744 | } |
| 745 | |
| 746 | if (((flags & OSMO_SOCK_F_BIND) && !local_hosts_cnt) || |
| 747 | ((flags & OSMO_SOCK_F_CONNECT) && !remote_hosts_cnt) || |
| 748 | local_hosts_cnt > OSMO_SOCK_MAX_ADDRS || |
| 749 | remote_hosts_cnt > OSMO_SOCK_MAX_ADDRS) |
| 750 | return -EINVAL; |
| 751 | |
| 752 | /* figure out local side of socket */ |
| 753 | if (flags & OSMO_SOCK_F_BIND) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 754 | rc = addrinfo_helper_multi(res_loc, family, type, proto, local_hosts, |
| 755 | local_hosts_cnt, local_port, true); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 756 | if (rc < 0) |
| 757 | return -EINVAL; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 758 | /* Figure out if there's any IPV4 or IPv6 addr in the set */ |
| 759 | if (family == AF_UNSPEC) |
| 760 | addrinfo_has_v4v6addr((const struct addrinfo **)res_loc, local_hosts_cnt, |
| 761 | &loc_has_v4addr, &loc_has_v6addr); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 762 | } |
| 763 | /* figure out remote side of socket */ |
| 764 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 765 | rc = addrinfo_helper_multi(res_rem, family, type, proto, remote_hosts, |
| 766 | remote_hosts_cnt, remote_port, false); |
Pau Espin Pedrol | 4541cf2 | 2020-08-25 11:22:09 +0200 | [diff] [blame] | 767 | if (rc < 0) { |
| 768 | rc = -EINVAL; |
| 769 | goto ret_freeaddrinfo_loc; |
| 770 | } |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 771 | /* Figure out if there's any IPv4 or IPv6 addr in the set */ |
| 772 | if (family == AF_UNSPEC) |
| 773 | addrinfo_has_v4v6addr((const struct addrinfo **)res_rem, remote_hosts_cnt, |
| 774 | &rem_has_v4addr, &rem_has_v6addr); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 775 | } |
| 776 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 777 | if (((flags & OSMO_SOCK_F_BIND) && (flags & OSMO_SOCK_F_CONNECT)) && |
Pau Espin Pedrol | 4f463c5 | 2020-08-28 14:32:02 +0200 | [diff] [blame] | 778 | !addrinfo_has_in6addr_any((const struct addrinfo **)res_loc, local_hosts_cnt) && |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 779 | (loc_has_v4addr != rem_has_v4addr || loc_has_v6addr != rem_has_v6addr)) { |
| 780 | LOGP(DLGLOBAL, LOGL_ERROR, "Invalid v4 vs v6 in local vs remote addresses\n"); |
| 781 | rc = -EINVAL; |
| 782 | goto ret_freeaddrinfo; |
| 783 | } |
| 784 | |
| 785 | sfd = socket_helper_multiaddr(loc_has_v6addr ? AF_INET6 : AF_INET, |
| 786 | type, proto, flags); |
| 787 | if (sfd < 0) { |
| 788 | rc = sfd; |
| 789 | goto ret_freeaddrinfo; |
| 790 | } |
| 791 | |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 792 | if (flags & OSMO_SOCK_F_BIND) { |
Pau Espin Pedrol | 272dfc1 | 2019-10-21 11:11:27 +0200 | [diff] [blame] | 793 | /* Since so far we only allow IPPROTO_SCTP in this function, |
| 794 | no need to check below for "proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR" */ |
| 795 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 796 | &on, sizeof(on)); |
| 797 | if (rc < 0) { |
| 798 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 799 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 800 | "cannot setsockopt socket:" |
| 801 | " %s:%u: %s\n", |
| 802 | strbuf, local_port, |
| 803 | strerror(errno)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 804 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 805 | } |
| 806 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 807 | /* Build array of addresses taking first entry for each host. |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 808 | TODO: Ideally we should use backtracking storing last used |
| 809 | indexes and trying next combination if connect() fails .*/ |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 810 | /* 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] | 811 | rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)res_loc, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 812 | local_hosts, local_hosts_cnt, |
| 813 | (uint8_t*)addrs_buf, sizeof(addrs_buf)); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 814 | if (rc < 0) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 815 | rc = -ENODEV; |
| 816 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 817 | } |
| 818 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 819 | rc = sctp_bindx(sfd, (struct sockaddr *)addrs_buf, local_hosts_cnt, SCTP_BINDX_ADD_ADDR); |
| 820 | if (rc == -1) { |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 821 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 822 | LOGP(DLGLOBAL, LOGL_NOTICE, "unable to bind socket: %s:%u: %s\n", |
| 823 | strbuf, local_port, strerror(errno)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 824 | rc = -ENODEV; |
| 825 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 826 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 827 | } |
| 828 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 829 | if (flags & OSMO_SOCK_F_CONNECT) { |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 830 | /* Build array of addresses taking first of same family for each host. |
| 831 | TODO: Ideally we should use backtracking storing last used |
| 832 | indexes and trying next combination if connect() fails .*/ |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 833 | rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)res_rem, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 834 | remote_hosts, remote_hosts_cnt, |
| 835 | (uint8_t*)addrs_buf, sizeof(addrs_buf)); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 836 | if (rc < 0) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 837 | rc = -ENODEV; |
| 838 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 841 | 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] | 842 | if (rc != 0 && errno != EINPROGRESS) { |
| 843 | multiaddr_snprintf(strbuf, sizeof(strbuf), remote_hosts, remote_hosts_cnt); |
| 844 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 845 | strbuf, remote_port, strerror(errno)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 846 | rc = -ENODEV; |
| 847 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 848 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 852 | if (rc < 0) { |
| 853 | close(sfd); |
| 854 | sfd = -1; |
| 855 | } |
| 856 | |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 857 | rc = sfd; |
| 858 | goto ret_freeaddrinfo; |
| 859 | |
| 860 | ret_close: |
| 861 | if (sfd >= 0) |
| 862 | close(sfd); |
| 863 | ret_freeaddrinfo: |
Pau Espin Pedrol | 4541cf2 | 2020-08-25 11:22:09 +0200 | [diff] [blame] | 864 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 865 | for (i = 0; i < remote_hosts_cnt; i++) |
| 866 | freeaddrinfo(res_rem[i]); |
| 867 | } |
| 868 | ret_freeaddrinfo_loc: |
| 869 | if (flags & OSMO_SOCK_F_BIND) { |
| 870 | for (i = 0; i < local_hosts_cnt; i++) |
| 871 | freeaddrinfo(res_loc[i]); |
| 872 | } |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 873 | return rc; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 874 | } |
| 875 | #endif /* HAVE_LIBSCTP */ |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 876 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 877 | /*! Initialize a socket (including bind/connect) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 878 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 879 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 880 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 881 | * \param[in] host remote host name or IP address in string form |
| 882 | * \param[in] port remote port number in host byte order |
| 883 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 884 | * \returns socket file descriptor on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 885 | * |
| 886 | * This function creates a new socket of the designated \a family, \a |
| 887 | * type and \a proto and optionally binds or connects it, depending on |
| 888 | * the value of \a flags parameter. |
| 889 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 890 | 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] | 891 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 892 | { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 893 | struct addrinfo *result, *rp; |
Pau Espin Pedrol | 6fe865d | 2021-07-15 13:08:08 +0200 | [diff] [blame] | 894 | int sfd = -1; /* initialize to avoid uninitialized false warnings on some gcc versions (11.1.0) */ |
| 895 | int on = 1; |
| 896 | int rc; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 897 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 898 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 899 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 900 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: both bind and connect flags set:" |
Neels Hofmeyr | b7f191f | 2016-08-29 11:22:03 +0200 | [diff] [blame] | 901 | " %s:%u\n", host, port); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 902 | return -EINVAL; |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 903 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 904 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 905 | 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] | 906 | if (!result) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 907 | return -EINVAL; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 908 | |
| 909 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 910 | sfd = socket_helper(rp, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 911 | if (sfd == -1) |
| 912 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 913 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 914 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 915 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 916 | if (rc != 0 && errno != EINPROGRESS) { |
| 917 | close(sfd); |
| 918 | continue; |
| 919 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 920 | } else { |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 921 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 922 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 923 | &on, sizeof(on)); |
| 924 | if (rc < 0) { |
| 925 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 926 | "cannot setsockopt socket:" |
| 927 | " %s:%u: %s\n", |
| 928 | host, port, strerror(errno)); |
| 929 | close(sfd); |
| 930 | continue; |
| 931 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 932 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 933 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 934 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket:" |
| 935 | "%s:%u: %s\n", |
| 936 | host, port, strerror(errno)); |
| 937 | close(sfd); |
| 938 | continue; |
| 939 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 940 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 941 | break; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 942 | } |
| 943 | freeaddrinfo(result); |
| 944 | |
| 945 | if (rp == NULL) { |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 946 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable addr found for: %s:%u\n", |
| 947 | host, port); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 948 | return -ENODEV; |
| 949 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 950 | |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 951 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 952 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 953 | if (rc < 0) { |
| 954 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 955 | "cannot setsockopt socket: %s:%u: %s\n", host, |
| 956 | port, strerror(errno)); |
| 957 | close(sfd); |
| 958 | sfd = -1; |
| 959 | } |
Philipp Maier | 0659c5d | 2018-08-01 12:43:08 +0200 | [diff] [blame] | 960 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 961 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 962 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 963 | if (rc < 0) { |
| 964 | close(sfd); |
| 965 | sfd = -1; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 966 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 967 | |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 968 | return sfd; |
| 969 | } |
| 970 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 971 | /*! fill \ref osmo_fd for a give sfd |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 972 | * \param[out] ofd file descriptor (will be filled in) |
| 973 | * \param[in] sfd socket file descriptor |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 974 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 975 | * \returns socket fd on success; negative on error |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 976 | * |
| 977 | * This function fills the \a ofd structure. |
| 978 | */ |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 979 | 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] | 980 | { |
| 981 | int rc; |
| 982 | |
| 983 | if (sfd < 0) |
| 984 | return sfd; |
| 985 | |
| 986 | ofd->fd = sfd; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 987 | ofd->when = OSMO_FD_READ; |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 988 | |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 989 | /* if we're doing a non-blocking connect, the completion will be signaled |
| 990 | * by marking the fd as WRITE-able. So in this exceptional case, we're |
| 991 | * also interested in when the socket becomes write-able */ |
| 992 | if ((flags & (OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK)) == |
| 993 | (OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK)) { |
| 994 | ofd->when |= OSMO_FD_WRITE; |
| 995 | } |
| 996 | |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 997 | rc = osmo_fd_register(ofd); |
| 998 | if (rc < 0) { |
| 999 | close(sfd); |
| 1000 | return rc; |
| 1001 | } |
| 1002 | |
| 1003 | return sfd; |
| 1004 | } |
| 1005 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1006 | /*! Initialize a socket and fill \ref osmo_fd |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 1007 | * \param[out] ofd file descriptor (will be filled in) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1008 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 1009 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1010 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1011 | * \param[in] host remote host name or IP address in string form |
| 1012 | * \param[in] port remote port number in host byte order |
| 1013 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1014 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1015 | * |
| 1016 | * This function creates (and optionall binds/connects) a socket using |
| 1017 | * \ref osmo_sock_init, but also fills the \a ofd structure. |
| 1018 | */ |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 1019 | 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] | 1020 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 1021 | { |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1022 | 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] | 1023 | } |
| 1024 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1025 | /*! Initialize a socket and fill \ref osmo_fd |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 1026 | * \param[out] ofd file descriptor (will be filled in) |
| 1027 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 1028 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1029 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1030 | * \param[in] local_host local host name or IP address in string form |
| 1031 | * \param[in] local_port local port number in host byte order |
| 1032 | * \param[in] remote_host remote host name or IP address in string form |
| 1033 | * \param[in] remote_port remote port number in host byte order |
| 1034 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 1035 | * \returns socket fd on success; negative on error |
| 1036 | * |
| 1037 | * This function creates (and optionall binds/connects) a socket using |
| 1038 | * \ref osmo_sock_init2, but also fills the \a ofd structure. |
| 1039 | */ |
| 1040 | int osmo_sock_init2_ofd(struct osmo_fd *ofd, int family, int type, int proto, |
| 1041 | const char *local_host, uint16_t local_port, |
| 1042 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 1043 | { |
| 1044 | 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] | 1045 | local_port, remote_host, remote_port, flags), flags); |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 1048 | int osmo_sock_init_osa_ofd(struct osmo_fd *ofd, int type, int proto, |
| 1049 | const struct osmo_sockaddr *local, |
| 1050 | const struct osmo_sockaddr *remote, unsigned int flags) |
| 1051 | { |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1052 | 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] | 1053 | } |
| 1054 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1055 | /*! Initialize a socket and fill \ref sockaddr |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1056 | * \param[out] ss socket address (will be filled in) |
| 1057 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1058 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1059 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1060 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1061 | * |
| 1062 | * This function creates (and optionall binds/connects) a socket using |
| 1063 | * \ref osmo_sock_init, but also fills the \a ss structure. |
| 1064 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1065 | int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1066 | uint8_t proto, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1067 | { |
| 1068 | char host[NI_MAXHOST]; |
| 1069 | uint16_t port; |
| 1070 | struct sockaddr_in *sin; |
| 1071 | struct sockaddr_in6 *sin6; |
| 1072 | int s, sa_len; |
| 1073 | |
| 1074 | /* determine port and host from ss */ |
| 1075 | switch (ss->sa_family) { |
| 1076 | case AF_INET: |
| 1077 | sin = (struct sockaddr_in *) ss; |
| 1078 | sa_len = sizeof(struct sockaddr_in); |
| 1079 | port = ntohs(sin->sin_port); |
| 1080 | break; |
| 1081 | case AF_INET6: |
| 1082 | sin6 = (struct sockaddr_in6 *) ss; |
| 1083 | sa_len = sizeof(struct sockaddr_in6); |
| 1084 | port = ntohs(sin6->sin6_port); |
| 1085 | break; |
| 1086 | default: |
| 1087 | return -EINVAL; |
| 1088 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1089 | |
| 1090 | s = getnameinfo(ss, sa_len, host, NI_MAXHOST, |
| 1091 | NULL, 0, NI_NUMERICHOST); |
| 1092 | if (s != 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 1093 | LOGP(DLGLOBAL, LOGL_ERROR, "getnameinfo failed:" |
| 1094 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1095 | return s; |
| 1096 | } |
| 1097 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1098 | return osmo_sock_init(ss->sa_family, type, proto, host, port, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | static int sockaddr_equal(const struct sockaddr *a, |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 1102 | const struct sockaddr *b, unsigned int len) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1103 | { |
| 1104 | struct sockaddr_in *sin_a, *sin_b; |
| 1105 | struct sockaddr_in6 *sin6_a, *sin6_b; |
| 1106 | |
| 1107 | if (a->sa_family != b->sa_family) |
| 1108 | return 0; |
| 1109 | |
| 1110 | switch (a->sa_family) { |
| 1111 | case AF_INET: |
| 1112 | sin_a = (struct sockaddr_in *)a; |
| 1113 | sin_b = (struct sockaddr_in *)b; |
| 1114 | if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr, |
| 1115 | sizeof(struct in_addr))) |
| 1116 | return 1; |
| 1117 | break; |
| 1118 | case AF_INET6: |
| 1119 | sin6_a = (struct sockaddr_in6 *)a; |
| 1120 | sin6_b = (struct sockaddr_in6 *)b; |
| 1121 | if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr, |
| 1122 | sizeof(struct in6_addr))) |
| 1123 | return 1; |
| 1124 | break; |
| 1125 | } |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Eric | 8ae40cb | 2021-09-27 22:10:01 +0200 | [diff] [blame] | 1129 | /* linux has a default route: |
| 1130 | local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1 |
| 1131 | */ |
| 1132 | static int sockaddr_is_local_routed(const struct sockaddr *a) |
| 1133 | { |
| 1134 | #if __linux__ |
| 1135 | if (a->sa_family != AF_INET) |
| 1136 | return 0; |
| 1137 | |
| 1138 | uint32_t address = ((struct sockaddr_in *)a)->sin_addr.s_addr; /* already BE */ |
| 1139 | uint32_t eightmask = htonl(0xff000000); /* /8 mask */ |
| 1140 | uint32_t local_prefix_127 = htonl(0x7f000000); /* 127.0.0.0 */ |
| 1141 | |
| 1142 | if ((address & eightmask) == local_prefix_127) |
| 1143 | return 1; |
| 1144 | #endif |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1148 | /*! Determine if the given address is a local address |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1149 | * \param[in] addr Socket Address |
| 1150 | * \param[in] addrlen Length of socket address in bytes |
| 1151 | * \returns 1 if address is local, 0 otherwise. |
| 1152 | */ |
Harald Welte | bc32d05 | 2012-04-08 11:31:32 +0200 | [diff] [blame] | 1153 | int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1154 | { |
| 1155 | struct ifaddrs *ifaddr, *ifa; |
| 1156 | |
Eric | 8ae40cb | 2021-09-27 22:10:01 +0200 | [diff] [blame] | 1157 | if (sockaddr_is_local_routed(addr)) |
| 1158 | return 1; |
| 1159 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1160 | if (getifaddrs(&ifaddr) == -1) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 1161 | LOGP(DLGLOBAL, LOGL_ERROR, "getifaddrs:" |
| 1162 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1163 | return -EIO; |
| 1164 | } |
| 1165 | |
| 1166 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
Harald Welte | 4d3a7b1 | 2011-05-24 21:31:53 +0200 | [diff] [blame] | 1167 | if (!ifa->ifa_addr) |
| 1168 | continue; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 1169 | if (sockaddr_equal(ifa->ifa_addr, addr, addrlen)) { |
| 1170 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1171 | return 1; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 1172 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 1175 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 1178 | |
Max | 9d7a247 | 2018-11-20 15:18:31 +0100 | [diff] [blame] | 1179 | /*! Convert sockaddr_in to IP address as char string and port as uint16_t. |
| 1180 | * \param[out] addr String buffer to write IP address to, or NULL. |
| 1181 | * \param[out] addr_len Size of \a addr. |
| 1182 | * \param[out] port Pointer to uint16_t to write the port number to, or NULL. |
| 1183 | * \param[in] sin Sockaddr to convert. |
| 1184 | * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL. |
| 1185 | */ |
| 1186 | size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 1187 | const struct sockaddr_in *sin) |
| 1188 | { |
| 1189 | if (port) |
| 1190 | *port = ntohs(sin->sin_port); |
| 1191 | |
| 1192 | if (addr) |
| 1193 | return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len); |
| 1194 | |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 1198 | /*! Convert sockaddr to IP address as char string and port as uint16_t. |
| 1199 | * \param[out] addr String buffer to write IP address to, or NULL. |
| 1200 | * \param[out] addr_len Size of \a addr. |
| 1201 | * \param[out] port Pointer to uint16_t to write the port number to, or NULL. |
| 1202 | * \param[in] sa Sockaddr to convert. |
| 1203 | * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL. |
| 1204 | */ |
| 1205 | unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 1206 | const struct sockaddr *sa) |
| 1207 | { |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 1208 | |
Pau Espin Pedrol | 1a3d24e | 2020-08-28 18:31:32 +0200 | [diff] [blame] | 1209 | const struct sockaddr_in6 *sin6; |
| 1210 | |
| 1211 | switch (sa->sa_family) { |
| 1212 | case AF_INET: |
| 1213 | return osmo_sockaddr_in_to_str_and_uint(addr, addr_len, port, |
| 1214 | (const struct sockaddr_in *)sa); |
| 1215 | case AF_INET6: |
| 1216 | sin6 = (const struct sockaddr_in6 *)sa; |
| 1217 | if (port) |
| 1218 | *port = ntohs(sin6->sin6_port); |
| 1219 | if (addr && inet_ntop(sa->sa_family, &sin6->sin6_addr, addr, addr_len)) |
| 1220 | return strlen(addr); |
| 1221 | break; |
| 1222 | } |
| 1223 | return 0; |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 1224 | } |
| 1225 | |
Pau Espin Pedrol | 5cc4fe4 | 2020-08-31 12:52:06 +0200 | [diff] [blame] | 1226 | /*! inet_ntop() wrapper for a struct sockaddr. |
| 1227 | * \param[in] sa source sockaddr to get the address from. |
| 1228 | * \param[out] dst string buffer of at least INET6_ADDRSTRLEN size. |
| 1229 | * \returns returns a non-null pointer to dst. NULL is returned if there was an |
| 1230 | * error, with errno set to indicate the error. |
| 1231 | */ |
| 1232 | const char *osmo_sockaddr_ntop(const struct sockaddr *sa, char *dst) |
| 1233 | { |
| 1234 | const struct osmo_sockaddr *osa = (const struct osmo_sockaddr *)sa; |
| 1235 | return inet_ntop(osa->u.sa.sa_family, |
| 1236 | osa->u.sa.sa_family == AF_INET6 ? |
| 1237 | (const void *)&osa->u.sin6.sin6_addr : |
| 1238 | (const void *)&osa->u.sin.sin_addr, |
| 1239 | dst, INET6_ADDRSTRLEN); |
| 1240 | } |
| 1241 | |
| 1242 | /*! Get sockaddr port content (in host byte order) |
| 1243 | * \param[in] sa source sockaddr to get the port from. |
| 1244 | * \returns returns the sockaddr port in host byte order |
| 1245 | */ |
| 1246 | uint16_t osmo_sockaddr_port(const struct sockaddr *sa) |
| 1247 | { |
| 1248 | const struct osmo_sockaddr *osa = (const struct osmo_sockaddr *)sa; |
| 1249 | switch (osa->u.sa.sa_family) { |
| 1250 | case AF_INET6: |
| 1251 | return ntohs(osa->u.sin6.sin6_port); |
| 1252 | case AF_INET: |
| 1253 | return ntohs(osa->u.sin.sin_port); |
| 1254 | } |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1258 | /*! Initialize a unix domain socket (including bind/connect) |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1259 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1260 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1261 | * \param[in] socket_path path to identify the socket |
| 1262 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1263 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1264 | * |
| 1265 | * This function creates a new unix domain socket, \a |
| 1266 | * type and \a proto and optionally binds or connects it, depending on |
| 1267 | * the value of \a flags parameter. |
| 1268 | */ |
Eric Wild | eb5769b | 2019-06-27 15:31:17 +0200 | [diff] [blame] | 1269 | #if defined(__clang__) && defined(SUN_LEN) |
| 1270 | __attribute__((no_sanitize("undefined"))) |
| 1271 | #endif |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1272 | int osmo_sock_unix_init(uint16_t type, uint8_t proto, |
| 1273 | const char *socket_path, unsigned int flags) |
| 1274 | { |
| 1275 | struct sockaddr_un local; |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 1276 | int sfd, rc; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1277 | unsigned int namelen; |
| 1278 | |
| 1279 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
| 1280 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) |
| 1281 | return -EINVAL; |
| 1282 | |
| 1283 | local.sun_family = AF_UNIX; |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1284 | /* When an AF_UNIX socket is bound, sun_path should be NUL-terminated. See unix(7) man page. */ |
| 1285 | 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] | 1286 | LOGP(DLGLOBAL, LOGL_ERROR, "Socket path exceeds maximum length of %zd bytes: %s\n", |
| 1287 | sizeof(local.sun_path), socket_path); |
| 1288 | return -ENOSPC; |
| 1289 | } |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1290 | |
| 1291 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1292 | local.sun_len = strlen(local.sun_path); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1293 | #endif |
| 1294 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 1295 | namelen = SUN_LEN(&local); |
| 1296 | #else |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1297 | namelen = strlen(local.sun_path) + |
| 1298 | offsetof(struct sockaddr_un, sun_path); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1299 | #endif |
| 1300 | |
| 1301 | sfd = socket(AF_UNIX, type, proto); |
| 1302 | if (sfd < 0) |
| 1303 | return -1; |
| 1304 | |
| 1305 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 1306 | rc = connect(sfd, (struct sockaddr *)&local, namelen); |
| 1307 | if (rc < 0) |
| 1308 | goto err; |
| 1309 | } else { |
| 1310 | unlink(local.sun_path); |
| 1311 | rc = bind(sfd, (struct sockaddr *)&local, namelen); |
| 1312 | if (rc < 0) |
| 1313 | goto err; |
| 1314 | } |
| 1315 | |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 1316 | rc = socket_helper_tail(sfd, flags); |
| 1317 | if (rc < 0) |
| 1318 | return rc; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1319 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 1320 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 1321 | if (rc < 0) { |
| 1322 | close(sfd); |
| 1323 | sfd = -1; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | return sfd; |
| 1327 | err: |
| 1328 | close(sfd); |
| 1329 | return -1; |
| 1330 | } |
| 1331 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1332 | /*! Initialize a unix domain socket and fill \ref osmo_fd |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1333 | * \param[out] ofd file descriptor (will be filled in) |
| 1334 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1335 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1336 | * \param[in] socket_path path to identify the socket |
| 1337 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1338 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1339 | * |
Vadim Yanitskiy | b606d76 | 2019-06-01 19:02:47 +0700 | [diff] [blame] | 1340 | * This function creates (and optionally binds/connects) a socket |
| 1341 | * using osmo_sock_unix_init, but also fills the ofd structure. |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1342 | */ |
| 1343 | int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto, |
| 1344 | const char *socket_path, unsigned int flags) |
| 1345 | { |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1346 | 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] | 1347 | } |
| 1348 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1349 | /*! 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] | 1350 | * \param[in] fd file descriptor of socket |
| 1351 | * \param[out] ip IP address (will be filled in when not NULL) |
| 1352 | * \param[in] ip_len length of the ip buffer |
| 1353 | * \param[out] port number (will be filled in when not NULL) |
| 1354 | * \param[in] port_len length of the port buffer |
| 1355 | * \param[in] local (true) or remote (false) name will get looked at |
| 1356 | * \returns 0 on success; negative otherwise |
| 1357 | */ |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1358 | 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] | 1359 | { |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1360 | struct sockaddr_storage sa; |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1361 | socklen_t len = sizeof(sa); |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 1362 | char ipbuf[INET6_ADDRSTRLEN], portbuf[6]; |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1363 | int rc; |
| 1364 | |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1365 | 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] | 1366 | if (rc < 0) |
| 1367 | return rc; |
| 1368 | |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1369 | rc = getnameinfo((const struct sockaddr*)&sa, len, ipbuf, sizeof(ipbuf), |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1370 | portbuf, sizeof(portbuf), |
| 1371 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 1372 | if (rc < 0) |
| 1373 | return rc; |
| 1374 | |
| 1375 | if (ip) |
| 1376 | strncpy(ip, ipbuf, ip_len); |
| 1377 | if (port) |
| 1378 | strncpy(port, portbuf, port_len); |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | /*! Get local IP address on socket |
| 1383 | * \param[in] fd file descriptor of socket |
| 1384 | * \param[out] ip IP address (will be filled in) |
| 1385 | * \param[in] len length of the output buffer |
| 1386 | * \returns 0 on success; negative otherwise |
| 1387 | */ |
| 1388 | int osmo_sock_get_local_ip(int fd, char *ip, size_t len) |
| 1389 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1390 | 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] | 1391 | } |
| 1392 | |
| 1393 | /*! Get local port on socket |
| 1394 | * \param[in] fd file descriptor of socket |
| 1395 | * \param[out] port number (will be filled in) |
| 1396 | * \param[in] len length of the output buffer |
| 1397 | * \returns 0 on success; negative otherwise |
| 1398 | */ |
| 1399 | int osmo_sock_get_local_ip_port(int fd, char *port, size_t len) |
| 1400 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1401 | 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] | 1402 | } |
| 1403 | |
| 1404 | /*! Get remote IP address on socket |
| 1405 | * \param[in] fd file descriptor of socket |
| 1406 | * \param[out] ip IP address (will be filled in) |
| 1407 | * \param[in] len length of the output buffer |
| 1408 | * \returns 0 on success; negative otherwise |
| 1409 | */ |
| 1410 | int osmo_sock_get_remote_ip(int fd, char *ip, size_t len) |
| 1411 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1412 | 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] | 1413 | } |
| 1414 | |
| 1415 | /*! Get remote port on socket |
| 1416 | * \param[in] fd file descriptor of socket |
| 1417 | * \param[out] port number (will be filled in) |
| 1418 | * \param[in] len length of the output buffer |
| 1419 | * \returns 0 on success; negative otherwise |
| 1420 | */ |
| 1421 | int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len) |
| 1422 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1423 | 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] | 1424 | } |
| 1425 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1426 | /*! Get address/port information on socket in dyn-alloc string like "(r=1.2.3.4:5<->l=6.7.8.9:10)". |
| 1427 | * Usually, it is better to use osmo_sock_get_name2() for a static string buffer or osmo_sock_get_name_buf() for a |
| 1428 | * caller provided string buffer, to avoid the dynamic talloc allocation. |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1429 | * \param[in] ctx talloc context from which to allocate string buffer |
| 1430 | * \param[in] fd file descriptor of socket |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1431 | * \returns string identifying the connection of this socket, talloc'd from ctx. |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1432 | */ |
Harald Welte | c0dfc9d | 2019-03-18 18:29:43 +0100 | [diff] [blame] | 1433 | char *osmo_sock_get_name(const void *ctx, int fd) |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1434 | { |
Philipp Maier | 64b51eb | 2019-01-14 11:59:11 +0100 | [diff] [blame] | 1435 | char str[OSMO_SOCK_NAME_MAXLEN]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1436 | int rc; |
| 1437 | rc = osmo_sock_get_name_buf(str, sizeof(str), fd); |
| 1438 | if (rc <= 0) |
| 1439 | return NULL; |
| 1440 | return talloc_asprintf(ctx, "(%s)", str); |
| 1441 | } |
| 1442 | |
| 1443 | /*! Get address/port information on socket in provided string buffer, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1444 | * This does not include braces like osmo_sock_get_name(). |
| 1445 | * \param[out] str Destination string buffer. |
| 1446 | * \param[in] str_len sizeof(str). |
| 1447 | * \param[in] fd File descriptor of socket. |
| 1448 | * \return String length as returned by snprintf(), or negative on error. |
| 1449 | */ |
| 1450 | int osmo_sock_get_name_buf(char *str, size_t str_len, int fd) |
| 1451 | { |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 1452 | char hostbuf_l[INET6_ADDRSTRLEN], hostbuf_r[INET6_ADDRSTRLEN]; |
| 1453 | char portbuf_l[6], portbuf_r[6]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1454 | int rc; |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1455 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1456 | /* get local */ |
Harald Welte | ea9ea52 | 2019-05-10 09:28:24 +0200 | [diff] [blame] | 1457 | if ((rc = osmo_sock_get_ip_and_port(fd, hostbuf_l, sizeof(hostbuf_l), portbuf_l, sizeof(portbuf_l), true))) { |
| 1458 | osmo_strlcpy(str, "<error-in-getsockname>", str_len); |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1459 | return rc; |
Harald Welte | ea9ea52 | 2019-05-10 09:28:24 +0200 | [diff] [blame] | 1460 | } |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1461 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1462 | /* get remote */ |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1463 | if (osmo_sock_get_ip_and_port(fd, hostbuf_r, sizeof(hostbuf_r), portbuf_r, sizeof(portbuf_r), false) != 0) |
| 1464 | 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] | 1465 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1466 | return snprintf(str, str_len, "r=%s:%s<->l=%s:%s", hostbuf_r, portbuf_r, hostbuf_l, portbuf_l); |
| 1467 | } |
| 1468 | |
| 1469 | /*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1470 | * This does not include braces like osmo_sock_get_name(). |
| 1471 | * \param[in] fd File descriptor of socket. |
| 1472 | * \return Static string buffer containing the result. |
| 1473 | */ |
| 1474 | const char *osmo_sock_get_name2(int fd) |
| 1475 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 1476 | static __thread char str[OSMO_SOCK_NAME_MAXLEN]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1477 | osmo_sock_get_name_buf(str, sizeof(str), fd); |
| 1478 | return str; |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1479 | } |
| 1480 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 1481 | /*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1482 | * This does not include braces like osmo_sock_get_name(). |
| 1483 | * \param[in] fd File descriptor of socket. |
| 1484 | * \return Static string buffer containing the result. |
| 1485 | */ |
| 1486 | char *osmo_sock_get_name2_c(const void *ctx, int fd) |
| 1487 | { |
| 1488 | char *str = talloc_size(ctx, OSMO_SOCK_NAME_MAXLEN); |
| 1489 | if (!str) |
| 1490 | return NULL; |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 1491 | osmo_sock_get_name_buf(str, OSMO_SOCK_NAME_MAXLEN, fd); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 1492 | return str; |
| 1493 | } |
| 1494 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 1495 | static int sock_get_domain(int fd) |
| 1496 | { |
| 1497 | int domain; |
| 1498 | #ifdef SO_DOMAIN |
| 1499 | socklen_t dom_len = sizeof(domain); |
| 1500 | int rc; |
| 1501 | |
| 1502 | rc = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &dom_len); |
| 1503 | if (rc < 0) |
| 1504 | return rc; |
| 1505 | #else |
| 1506 | /* This of course sucks, but what shall we do on OSs like |
| 1507 | * FreeBSD that don't seem to expose a method by which one can |
| 1508 | * learn the address family of a socket? */ |
| 1509 | domain = AF_INET; |
| 1510 | #endif |
| 1511 | return domain; |
| 1512 | } |
| 1513 | |
| 1514 | |
| 1515 | /*! Activate or de-activate local loop-back of transmitted multicast packets |
| 1516 | * \param[in] fd file descriptor of related socket |
| 1517 | * \param[in] enable Enable (true) or disable (false) loop-back |
| 1518 | * \returns 0 on success; negative otherwise */ |
| 1519 | int osmo_sock_mcast_loop_set(int fd, bool enable) |
| 1520 | { |
| 1521 | int domain, loop = 0; |
| 1522 | |
| 1523 | if (enable) |
| 1524 | loop = 1; |
| 1525 | |
| 1526 | domain = sock_get_domain(fd); |
| 1527 | if (domain < 0) |
| 1528 | return domain; |
| 1529 | |
| 1530 | switch (domain) { |
| 1531 | case AF_INET: |
| 1532 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 1533 | case AF_INET6: |
| 1534 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 1535 | default: |
| 1536 | return -EINVAL; |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | /*! Set the TTL of outbound multicast packets |
| 1541 | * \param[in] fd file descriptor of related socket |
| 1542 | * \param[in] ttl TTL of to-be-sent multicast packets |
| 1543 | * \returns 0 on success; negative otherwise */ |
| 1544 | int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl) |
| 1545 | { |
| 1546 | int domain, ttli = ttl; |
| 1547 | |
| 1548 | domain = sock_get_domain(fd); |
| 1549 | if (domain < 0) |
| 1550 | return domain; |
| 1551 | |
| 1552 | switch (domain) { |
| 1553 | case AF_INET: |
| 1554 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttli, sizeof(ttli)); |
| 1555 | case AF_INET6: |
| 1556 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttli, sizeof(ttli)); |
| 1557 | default: |
| 1558 | return -EINVAL; |
| 1559 | } |
| 1560 | } |
| 1561 | |
Harald Welte | 44b9926 | 2020-03-07 14:59:05 +0100 | [diff] [blame] | 1562 | /*! Set the network device to which we should bind the multicast socket |
| 1563 | * \param[in] fd file descriptor of related socket |
| 1564 | * \param[in] ifname name of network interface to user for multicast |
| 1565 | * \returns 0 on success; negative otherwise */ |
| 1566 | int osmo_sock_mcast_iface_set(int fd, const char *ifname) |
| 1567 | { |
| 1568 | unsigned int ifindex; |
| 1569 | struct ip_mreqn mr; |
| 1570 | |
| 1571 | /* first, resolve interface name to ifindex */ |
| 1572 | ifindex = if_nametoindex(ifname); |
| 1573 | if (ifindex == 0) |
| 1574 | return -errno; |
| 1575 | |
| 1576 | /* next, configure kernel to use that ifindex for this sockets multicast traffic */ |
| 1577 | memset(&mr, 0, sizeof(mr)); |
| 1578 | mr.imr_ifindex = ifindex; |
| 1579 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mr, sizeof(mr)); |
| 1580 | } |
| 1581 | |
| 1582 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 1583 | /*! Enable/disable receiving all multicast packets, even for non-subscribed groups |
| 1584 | * \param[in] fd file descriptor of related socket |
| 1585 | * \param[in] enable Enable or Disable receiving of all packets |
| 1586 | * \returns 0 on success; negative otherwise */ |
| 1587 | int osmo_sock_mcast_all_set(int fd, bool enable) |
| 1588 | { |
| 1589 | int domain, all = 0; |
| 1590 | |
| 1591 | if (enable) |
| 1592 | all = 1; |
| 1593 | |
| 1594 | domain = sock_get_domain(fd); |
| 1595 | if (domain < 0) |
| 1596 | return domain; |
| 1597 | |
| 1598 | switch (domain) { |
| 1599 | case AF_INET: |
| 1600 | #ifdef IP_MULTICAST_ALL |
| 1601 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)); |
| 1602 | #endif |
| 1603 | case AF_INET6: |
| 1604 | /* there seems no equivalent ?!? */ |
| 1605 | default: |
| 1606 | return -EINVAL; |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | /* FreeBSD calls the socket option differently */ |
| 1611 | #if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP) |
| 1612 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP |
| 1613 | #endif |
| 1614 | |
| 1615 | /*! Subscribe to the given IP multicast group |
| 1616 | * \param[in] fd file descriptor of related scoket |
| 1617 | * \param[in] grp_addr ASCII representation of the multicast group address |
| 1618 | * \returns 0 on success; negative otherwise */ |
| 1619 | int osmo_sock_mcast_subscribe(int fd, const char *grp_addr) |
| 1620 | { |
| 1621 | int rc, domain; |
| 1622 | struct ip_mreq mreq; |
| 1623 | struct ipv6_mreq mreq6; |
| 1624 | struct in6_addr i6a; |
| 1625 | |
| 1626 | domain = sock_get_domain(fd); |
| 1627 | if (domain < 0) |
| 1628 | return domain; |
| 1629 | |
| 1630 | switch (domain) { |
| 1631 | case AF_INET: |
| 1632 | memset(&mreq, 0, sizeof(mreq)); |
| 1633 | mreq.imr_multiaddr.s_addr = inet_addr(grp_addr); |
| 1634 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
| 1635 | return setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 1636 | #ifdef IPV6_ADD_MEMBERSHIP |
| 1637 | case AF_INET6: |
| 1638 | memset(&mreq6, 0, sizeof(mreq6)); |
| 1639 | rc = inet_pton(AF_INET6, grp_addr, (void *)&i6a); |
| 1640 | if (rc < 0) |
| 1641 | return -EINVAL; |
| 1642 | mreq6.ipv6mr_multiaddr = i6a; |
| 1643 | return setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)); |
| 1644 | #endif |
| 1645 | default: |
| 1646 | return -EINVAL; |
| 1647 | } |
| 1648 | } |
| 1649 | |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1650 | /*! Determine the matching local IP-address for a given remote IP-Address. |
| 1651 | * \param[out] local_ip caller provided memory for resulting local IP-address |
| 1652 | * \param[in] remote_ip remote IP-address |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1653 | * \returns 0 on success; negative otherwise |
| 1654 | * |
| 1655 | * The function accepts IPv4 and IPv6 address strings. The caller must provide |
| 1656 | * at least INET6_ADDRSTRLEN bytes for local_ip if an IPv6 is expected as |
| 1657 | * as result. For IPv4 addresses the required amount is INET_ADDRSTRLEN. */ |
| 1658 | int osmo_sock_local_ip(char *local_ip, const char *remote_ip) |
| 1659 | { |
| 1660 | int sfd; |
| 1661 | int rc; |
| 1662 | struct addrinfo addrinfo_hint; |
| 1663 | struct addrinfo *addrinfo = NULL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 1664 | struct sockaddr_storage local_addr; |
| 1665 | struct sockaddr_in *sin; |
| 1666 | struct sockaddr_in6 *sin6; |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1667 | socklen_t local_addr_len; |
| 1668 | uint16_t family; |
| 1669 | |
| 1670 | /* Find out the address family (AF_INET or AF_INET6?) */ |
| 1671 | memset(&addrinfo_hint, '\0', sizeof(addrinfo_hint)); |
Pau Espin Pedrol | 308ab79 | 2020-08-28 19:47:25 +0200 | [diff] [blame] | 1672 | addrinfo_hint.ai_family = AF_UNSPEC; |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1673 | addrinfo_hint.ai_flags = AI_NUMERICHOST; |
| 1674 | rc = getaddrinfo(remote_ip, NULL, &addrinfo_hint, &addrinfo); |
| 1675 | if (rc) |
| 1676 | return -EINVAL; |
| 1677 | family = addrinfo->ai_family; |
| 1678 | freeaddrinfo(addrinfo); |
| 1679 | |
| 1680 | /* Connect a dummy socket to trick the kernel into determining the |
| 1681 | * ip-address of the interface that would be used if we would send |
| 1682 | * out an actual packet */ |
| 1683 | sfd = osmo_sock_init2(family, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, remote_ip, 0, OSMO_SOCK_F_CONNECT); |
| 1684 | if (sfd < 0) |
| 1685 | return -EINVAL; |
| 1686 | |
| 1687 | /* Request the IP address of the interface that the kernel has |
| 1688 | * actually choosen. */ |
| 1689 | memset(&local_addr, 0, sizeof(local_addr)); |
| 1690 | local_addr_len = sizeof(local_addr); |
| 1691 | rc = getsockname(sfd, (struct sockaddr *)&local_addr, &local_addr_len); |
Philipp Maier | 8b7975b | 2018-01-22 15:38:07 +0100 | [diff] [blame] | 1692 | close(sfd); |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1693 | if (rc < 0) |
| 1694 | return -EINVAL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 1695 | |
| 1696 | switch (local_addr.ss_family) { |
| 1697 | case AF_INET: |
| 1698 | sin = (struct sockaddr_in*)&local_addr; |
| 1699 | if (!inet_ntop(AF_INET, &sin->sin_addr, local_ip, INET_ADDRSTRLEN)) |
| 1700 | return -EINVAL; |
| 1701 | break; |
| 1702 | case AF_INET6: |
| 1703 | sin6 = (struct sockaddr_in6*)&local_addr; |
Pau Espin Pedrol | 7bda854 | 2020-08-31 11:21:30 +0200 | [diff] [blame] | 1704 | 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] | 1705 | return -EINVAL; |
| 1706 | break; |
| 1707 | default: |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1708 | return -EINVAL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 1709 | } |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1710 | |
| 1711 | return 0; |
| 1712 | } |
| 1713 | |
Alexander Couzens | 0f36421 | 2020-07-27 22:33:01 +0200 | [diff] [blame] | 1714 | /*! Determine the matching local address for a given remote address. |
| 1715 | * \param[out] local_ip caller provided memory for resulting local address |
| 1716 | * \param[in] remote_ip remote address |
| 1717 | * \returns 0 on success; negative otherwise |
| 1718 | */ |
| 1719 | int osmo_sockaddr_local_ip(struct osmo_sockaddr *local_ip, const struct osmo_sockaddr *remote_ip) |
| 1720 | { |
| 1721 | int sfd; |
| 1722 | int rc; |
| 1723 | socklen_t local_ip_len; |
| 1724 | |
| 1725 | sfd = osmo_sock_init_osa(SOCK_DGRAM, IPPROTO_UDP, NULL, remote_ip, OSMO_SOCK_F_CONNECT); |
| 1726 | if (sfd < 0) |
| 1727 | return -EINVAL; |
| 1728 | |
| 1729 | memset(local_ip, 0, sizeof(*local_ip)); |
| 1730 | local_ip_len = sizeof(*local_ip); |
| 1731 | rc = getsockname(sfd, (struct sockaddr *)local_ip, &local_ip_len); |
| 1732 | close(sfd); |
| 1733 | |
| 1734 | return rc; |
| 1735 | } |
| 1736 | |
Alexander Couzens | e4181ea | 2020-07-20 00:03:16 +0200 | [diff] [blame] | 1737 | /*! Compare two osmo_sockaddr. |
| 1738 | * \param[in] a |
| 1739 | * \param[in] b |
| 1740 | * \return 0 if a and b are equal. Otherwise it follows memcmp() |
| 1741 | */ |
Vadim Yanitskiy | def5a40 | 2020-10-09 21:40:47 +0700 | [diff] [blame] | 1742 | int osmo_sockaddr_cmp(const struct osmo_sockaddr *a, |
| 1743 | const struct osmo_sockaddr *b) |
Alexander Couzens | e4181ea | 2020-07-20 00:03:16 +0200 | [diff] [blame] | 1744 | { |
| 1745 | if (a == b) |
| 1746 | return 0; |
| 1747 | if (!a) |
| 1748 | return 1; |
| 1749 | if (!b) |
| 1750 | return -1; |
| 1751 | |
| 1752 | if (a->u.sa.sa_family != b->u.sa.sa_family) { |
| 1753 | return OSMO_CMP(a->u.sa.sa_family, b->u.sa.sa_family); |
| 1754 | } |
| 1755 | |
| 1756 | switch (a->u.sa.sa_family) { |
| 1757 | case AF_INET: |
| 1758 | return memcmp(&a->u.sin, &b->u.sin, sizeof(struct sockaddr_in)); |
| 1759 | case AF_INET6: |
| 1760 | return memcmp(&a->u.sin6, &b->u.sin6, sizeof(struct sockaddr_in6)); |
| 1761 | default: |
| 1762 | /* fallback to memcmp for remaining AF over the full osmo_sockaddr length */ |
| 1763 | return memcmp(a, b, sizeof(struct osmo_sockaddr)); |
| 1764 | } |
| 1765 | } |
| 1766 | |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 1767 | /*! string-format a given osmo_sockaddr address |
| 1768 | * \param[in] sockaddr the osmo_sockaddr to print |
| 1769 | * \return pointer to the string on success; NULL on error |
| 1770 | */ |
| 1771 | const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *sockaddr) |
| 1772 | { |
| 1773 | /* INET6_ADDRSTRLEN contains already a null termination, |
| 1774 | * adding '[' ']' ':' '16 bit port' */ |
| 1775 | static __thread char buf[INET6_ADDRSTRLEN + 8]; |
| 1776 | return osmo_sockaddr_to_str_buf(buf, sizeof(buf), sockaddr); |
| 1777 | } |
| 1778 | |
| 1779 | /*! string-format a given osmo_sockaddr address into a user-supplied buffer |
| 1780 | * \param[in] buf user-supplied output buffer |
| 1781 | * \param[in] buf_len size of the user-supplied output buffer in bytes |
| 1782 | * \param[in] sockaddr the osmo_sockaddr to print |
| 1783 | * \return pointer to the string on success; NULL on error |
| 1784 | */ |
| 1785 | char *osmo_sockaddr_to_str_buf(char *buf, size_t buf_len, |
| 1786 | const struct osmo_sockaddr *sockaddr) |
| 1787 | { |
| 1788 | uint16_t port = 0; |
| 1789 | size_t written; |
| 1790 | if (buf_len < 5) |
| 1791 | return NULL; |
| 1792 | |
| 1793 | if (!sockaddr) |
| 1794 | return NULL; |
| 1795 | |
| 1796 | switch (sockaddr->u.sa.sa_family) { |
| 1797 | case AF_INET: |
| 1798 | written = osmo_sockaddr_to_str_and_uint(buf, buf_len, &port, &sockaddr->u.sa); |
| 1799 | if (written + 1 >= buf_len && port) |
| 1800 | return NULL; |
| 1801 | if (port) |
| 1802 | snprintf(buf + written, buf_len - written, ":%u", port); |
| 1803 | break; |
| 1804 | case AF_INET6: |
| 1805 | buf[0] = '['; |
| 1806 | written = osmo_sockaddr_to_str_and_uint(buf + 1, buf_len - 1, &port, &sockaddr->u.sa); |
| 1807 | if (written + 2 >= buf_len) |
| 1808 | return NULL; |
| 1809 | |
| 1810 | if (written + 3 >= buf_len && port) |
| 1811 | return NULL; |
| 1812 | |
| 1813 | if (port) |
| 1814 | snprintf(buf + 1 + written, buf_len - written - 1, "]:%u", port); |
| 1815 | else { |
| 1816 | buf[written + 1] = ']'; |
| 1817 | buf[written + 2] = 0; |
| 1818 | } |
| 1819 | break; |
| 1820 | default: |
| 1821 | snprintf(buf, buf_len, "unsupported family %d", sockaddr->u.sa.sa_family); |
| 1822 | return buf; |
| 1823 | } |
| 1824 | |
| 1825 | return buf; |
| 1826 | } |
| 1827 | |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1828 | /*! Set the DSCP (differentiated services code point) of a socket. |
| 1829 | * \param[in] dscp DSCP value in range 0..63 |
| 1830 | * \returns 0 on success; negative on error. */ |
| 1831 | int osmo_sock_set_dscp(int fd, uint8_t dscp) |
| 1832 | { |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 1833 | struct sockaddr_storage local_addr; |
| 1834 | socklen_t local_addr_len = sizeof(local_addr); |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1835 | uint8_t tos; |
| 1836 | socklen_t tos_len = sizeof(tos); |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 1837 | int tclass; |
| 1838 | socklen_t tclass_len = sizeof(tclass); |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1839 | int rc; |
| 1840 | |
| 1841 | /* DSCP is a 6-bit value stored in the upper 6 bits of the 8-bit TOS */ |
| 1842 | if (dscp > 63) |
| 1843 | return -EINVAL; |
| 1844 | |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 1845 | rc = getsockname(fd, (struct sockaddr *)&local_addr, &local_addr_len); |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1846 | if (rc < 0) |
| 1847 | return rc; |
| 1848 | |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 1849 | switch (local_addr.ss_family) { |
| 1850 | case AF_INET: |
| 1851 | /* read the original value */ |
| 1852 | rc = getsockopt(fd, IPPROTO_IP, IP_TOS, &tos, &tos_len); |
| 1853 | if (rc < 0) |
| 1854 | return rc; |
| 1855 | /* mask-in the DSCP into the upper 6 bits */ |
| 1856 | tos &= 0x03; |
| 1857 | tos |= dscp << 2; |
| 1858 | /* and write it back to the kernel */ |
| 1859 | rc = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1860 | break; |
| 1861 | case AF_INET6: |
| 1862 | /* read the original value */ |
| 1863 | rc = getsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tclass, &tclass_len); |
| 1864 | if (rc < 0) |
| 1865 | return rc; |
| 1866 | /* mask-in the DSCP into the upper 6 bits */ |
| 1867 | tclass &= 0x03; |
| 1868 | tclass |= dscp << 2; |
| 1869 | /* and write it back to the kernel */ |
| 1870 | rc = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof(tclass)); |
| 1871 | break; |
| 1872 | case AF_UNSPEC: |
| 1873 | default: |
| 1874 | LOGP(DLGLOBAL, LOGL_ERROR, "No DSCP support for socket family %u\n", |
| 1875 | local_addr.ss_family); |
| 1876 | rc = -1; |
| 1877 | break; |
| 1878 | } |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1879 | |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 1880 | return rc; |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1881 | } |
| 1882 | |
Harald Welte | ecc0bd8 | 2021-04-27 22:24:08 +0200 | [diff] [blame] | 1883 | /*! Set the priority value of a socket. |
| 1884 | * \param[in] prio priority value. Values outside 0..6 require CAP_NET_ADMIN. |
| 1885 | * \returns 0 on success; negative on error. */ |
| 1886 | int osmo_sock_set_priority(int fd, int prio) |
| 1887 | { |
| 1888 | /* and write it back to the kernel */ |
| 1889 | return setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)); |
| 1890 | } |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 1891 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 1892 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1893 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 1894 | /*! @} */ |