Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 1 | /* |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 2 | * (C) 2011-2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 3 | * |
| 4 | * All Rights Reserved |
| 5 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 24 | #include "../config.h" |
| 25 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 26 | /*! \addtogroup socket |
| 27 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 28 | * Osmocom socket convenience functions. |
| 29 | * |
| 30 | * \file socket.c */ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 31 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 32 | #ifdef HAVE_SYS_SOCKET_H |
| 33 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 34 | #include <osmocom/core/logging.h> |
| 35 | #include <osmocom/core/select.h> |
| 36 | #include <osmocom/core/socket.h> |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 37 | #include <osmocom/core/sockaddr_str.h> |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 38 | #include <osmocom/core/talloc.h> |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 39 | #include <osmocom/core/utils.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 40 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 41 | #include <sys/ioctl.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 42 | #include <sys/socket.h> |
| 43 | #include <sys/types.h> |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 44 | #include <sys/un.h> |
Harald Welte | 44b9926 | 2020-03-07 14:59:05 +0100 | [diff] [blame] | 45 | #include <net/if.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 46 | |
Holger Hans Peter Freyther | 4772348 | 2011-11-09 11:26:15 +0100 | [diff] [blame] | 47 | #include <netinet/in.h> |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 48 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 4772348 | 2011-11-09 11:26:15 +0100 | [diff] [blame] | 49 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 50 | #include <stdio.h> |
| 51 | #include <unistd.h> |
| 52 | #include <stdint.h> |
| 53 | #include <string.h> |
| 54 | #include <errno.h> |
| 55 | #include <netdb.h> |
| 56 | #include <ifaddrs.h> |
| 57 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 58 | #ifdef HAVE_LIBSCTP |
| 59 | #include <netinet/sctp.h> |
| 60 | #endif |
| 61 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 62 | static struct addrinfo *addrinfo_helper(uint16_t family, uint16_t type, uint8_t proto, |
| 63 | const char *host, uint16_t port, bool passive) |
| 64 | { |
Pau Espin Pedrol | ff42852 | 2019-10-10 17:38:16 +0200 | [diff] [blame] | 65 | struct addrinfo hints, *result, *rp; |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 66 | char portbuf[6]; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 67 | int rc; |
| 68 | |
| 69 | snprintf(portbuf, sizeof(portbuf), "%u", port); |
| 70 | memset(&hints, 0, sizeof(struct addrinfo)); |
| 71 | hints.ai_family = family; |
| 72 | if (type == SOCK_RAW) { |
| 73 | /* Workaround for glibc, that returns EAI_SERVICE (-8) if |
| 74 | * SOCK_RAW and IPPROTO_GRE is used. |
Pau Espin Pedrol | ff42852 | 2019-10-10 17:38:16 +0200 | [diff] [blame] | 75 | * http://sourceware.org/bugzilla/show_bug.cgi?id=15015 |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 76 | */ |
| 77 | hints.ai_socktype = SOCK_DGRAM; |
| 78 | hints.ai_protocol = IPPROTO_UDP; |
| 79 | } else { |
| 80 | hints.ai_socktype = type; |
| 81 | hints.ai_protocol = proto; |
| 82 | } |
Pau Espin Pedrol | 3119d47 | 2020-08-25 12:30:55 +0200 | [diff] [blame] | 83 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 84 | if (passive) |
| 85 | hints.ai_flags |= AI_PASSIVE; |
| 86 | |
| 87 | rc = getaddrinfo(host, portbuf, &hints, &result); |
| 88 | if (rc != 0) { |
Pau Espin Pedrol | ba828c3 | 2020-08-20 18:28:10 +0200 | [diff] [blame] | 89 | LOGP(DLGLOBAL, LOGL_ERROR, "getaddrinfo(%s, %u) failed: %s\n", |
| 90 | host, port, gai_strerror(rc)); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 91 | return NULL; |
| 92 | } |
| 93 | |
Pau Espin Pedrol | ff42852 | 2019-10-10 17:38:16 +0200 | [diff] [blame] | 94 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
| 95 | /* Workaround for glibc again */ |
| 96 | if (type == SOCK_RAW) { |
| 97 | rp->ai_socktype = SOCK_RAW; |
| 98 | rp->ai_protocol = proto; |
| 99 | } |
| 100 | } |
| 101 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 102 | return result; |
| 103 | } |
| 104 | |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 105 | #ifdef HAVE_LIBSCTP |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 106 | /*! Retrieve an array of addrinfo with specified hints, one for each host in the hosts array. |
| 107 | * \param[out] addrinfo array of addrinfo pointers, will be filled by the function on success. |
| 108 | * Its size must be at least the one of hosts. |
| 109 | * \param[in] family Socket family like AF_INET, AF_INET6. |
| 110 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM. |
| 111 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP. |
| 112 | * \param[in] hosts array of char pointers (strings) containing the addresses to query. |
| 113 | * \param[in] host_cnt length of the hosts array (in items). |
| 114 | * \param[in] port port number in host byte order. |
| 115 | * \param[in] passive whether to include the AI_PASSIVE flag in getaddrinfo() hints. |
| 116 | * \returns 0 is returned on success together with a filled addrinfo array; negative on error |
| 117 | */ |
| 118 | static int addrinfo_helper_multi(struct addrinfo **addrinfo, uint16_t family, uint16_t type, uint8_t proto, |
| 119 | const char **hosts, size_t host_cnt, uint16_t port, bool passive) |
| 120 | { |
| 121 | int i, j; |
| 122 | |
| 123 | for (i = 0; i < host_cnt; i++) { |
| 124 | addrinfo[i] = addrinfo_helper(family, type, proto, hosts[i], port, passive); |
| 125 | if (!addrinfo[i]) { |
| 126 | for (j = 0; j < i; j++) |
| 127 | freeaddrinfo(addrinfo[j]); |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | } |
| 131 | return 0; |
| 132 | } |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 133 | #endif /* HAVE_LIBSCTP*/ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 134 | |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 135 | static int socket_helper_tail(int sfd, unsigned int flags) |
| 136 | { |
Harald Welte | c545ff6 | 2021-04-28 13:09:49 +0200 | [diff] [blame] | 137 | int rc, on = 1; |
| 138 | uint8_t dscp = GET_OSMO_SOCK_F_DSCP(flags); |
| 139 | uint8_t prio = GET_OSMO_SOCK_F_PRIO(flags); |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 140 | |
| 141 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 142 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
| 143 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 144 | "cannot set this socket unblocking: %s\n", |
| 145 | strerror(errno)); |
| 146 | close(sfd); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | } |
| 150 | |
Harald Welte | c545ff6 | 2021-04-28 13:09:49 +0200 | [diff] [blame] | 151 | if (dscp) { |
| 152 | rc = osmo_sock_set_dscp(sfd, dscp); |
| 153 | if (rc) { |
| 154 | LOGP(DLGLOBAL, LOGL_ERROR, "cannot set IP DSCP of socket to %u: %s\n", |
| 155 | dscp, strerror(errno)); |
| 156 | /* we consider this a non-fatal error */ |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (prio) { |
| 161 | rc = osmo_sock_set_priority(sfd, prio); |
| 162 | if (rc) { |
| 163 | LOGP(DLGLOBAL, LOGL_ERROR, "cannot set priority of socket to %u: %s\n", |
| 164 | prio, strerror(errno)); |
| 165 | /* we consider this a non-fatal error */ |
| 166 | } |
| 167 | } |
| 168 | |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 172 | static int socket_helper(const struct addrinfo *rp, unsigned int flags) |
| 173 | { |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 174 | int sfd, rc; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 175 | |
| 176 | sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 177 | if (sfd == -1) { |
| 178 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 179 | "unable to create socket: %s\n", strerror(errno)); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 180 | return sfd; |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 181 | } |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 182 | |
| 183 | rc = socket_helper_tail(sfd, flags); |
| 184 | if (rc < 0) |
| 185 | return rc; |
| 186 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 187 | return sfd; |
| 188 | } |
| 189 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 190 | static int socket_helper_osa(const struct osmo_sockaddr *addr, uint16_t type, uint8_t proto, unsigned int flags) |
| 191 | { |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 192 | int sfd, rc; |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 193 | |
| 194 | sfd = socket(addr->u.sa.sa_family, type, proto); |
| 195 | if (sfd == -1) { |
| 196 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 197 | "unable to create socket: %s\n", strerror(errno)); |
| 198 | return sfd; |
| 199 | } |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 200 | |
| 201 | rc = socket_helper_tail(sfd, flags); |
| 202 | if (rc < 0) |
| 203 | return rc; |
| 204 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 205 | return sfd; |
| 206 | } |
| 207 | |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 208 | #ifdef HAVE_LIBSCTP |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 209 | /* Fill buf with a string representation of the address set, in the form: |
| 210 | * buf_len == 0: "()" |
| 211 | * buf_len == 1: "hostA" |
| 212 | * buf_len >= 2: (hostA|hostB|...|...) |
| 213 | */ |
| 214 | static int multiaddr_snprintf(char* buf, size_t buf_len, const char **hosts, size_t host_cnt) |
| 215 | { |
| 216 | int len = 0, offset = 0, rem = buf_len; |
| 217 | int ret, i; |
| 218 | char *after; |
| 219 | |
| 220 | if (buf_len < 3) |
| 221 | return -EINVAL; |
| 222 | |
| 223 | if (host_cnt != 1) { |
| 224 | ret = snprintf(buf, rem, "("); |
| 225 | if (ret < 0) |
| 226 | return ret; |
| 227 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 228 | } |
| 229 | for (i = 0; i < host_cnt; i++) { |
| 230 | if (host_cnt == 1) |
| 231 | after = ""; |
| 232 | else |
| 233 | after = (i == (host_cnt - 1)) ? ")" : "|"; |
| 234 | ret = snprintf(buf + offset, rem, "%s%s", hosts[i] ? : "0.0.0.0", after); |
| 235 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 236 | } |
| 237 | |
| 238 | return len; |
| 239 | } |
Pau Espin Pedrol | 8fac511 | 2019-10-24 15:39:25 +0200 | [diff] [blame] | 240 | #endif /* HAVE_LIBSCTP */ |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 241 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 242 | static int osmo_sock_init_tail(int fd, uint16_t type, unsigned int flags) |
| 243 | { |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 244 | int rc; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 245 | |
| 246 | /* Make sure to call 'listen' on a bound, connection-oriented sock */ |
| 247 | if ((flags & (OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT)) == OSMO_SOCK_F_BIND) { |
| 248 | switch (type) { |
| 249 | case SOCK_STREAM: |
| 250 | case SOCK_SEQPACKET: |
| 251 | rc = listen(fd, 10); |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 252 | if (rc < 0) { |
| 253 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to listen on socket: %s\n", |
| 254 | strerror(errno)); |
| 255 | return rc; |
| 256 | } |
| 257 | break; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 261 | if (flags & OSMO_SOCK_F_NO_MCAST_LOOP) { |
| 262 | rc = osmo_sock_mcast_loop_set(fd, false); |
| 263 | if (rc < 0) { |
| 264 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to disable multicast loop: %s\n", |
| 265 | strerror(errno)); |
| 266 | return rc; |
| 267 | } |
| 268 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 269 | |
Harald Welte | 37d204a | 2017-07-13 16:33:16 +0200 | [diff] [blame] | 270 | if (flags & OSMO_SOCK_F_NO_MCAST_ALL) { |
| 271 | rc = osmo_sock_mcast_all_set(fd, false); |
| 272 | if (rc < 0) { |
| 273 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to disable receive of all multicast: %s\n", |
| 274 | strerror(errno)); |
| 275 | /* do not abort here, as this is just an |
| 276 | * optional additional optimization that only |
| 277 | * exists on Linux only */ |
| 278 | } |
| 279 | } |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 280 | return 0; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 283 | /*! Initialize a socket (including bind and/or connect) |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 284 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 285 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 286 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 287 | * \param[in] local_host local host name or IP address in string form |
| 288 | * \param[in] local_port local port number in host byte order |
| 289 | * \param[in] remote_host remote host name or IP address in string form |
| 290 | * \param[in] remote_port remote port number in host byte order |
| 291 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 292 | * \returns socket file descriptor on success; negative on error |
| 293 | * |
| 294 | * This function creates a new socket of the designated \a family, \a |
| 295 | * type and \a proto and optionally binds it to the \a local_host and \a |
| 296 | * local_port as well as optionally connects it to the \a remote_host |
| 297 | * and \q remote_port, depending on the value * of \a flags parameter. |
| 298 | * |
| 299 | * As opposed to \ref osmo_sock_init(), this function allows to combine |
| 300 | * the \ref OSMO_SOCK_F_BIND and \ref OSMO_SOCK_F_CONNECT flags. This |
| 301 | * is useful if you want to connect to a remote host/port, but still |
| 302 | * want to bind that socket to either a specific local alias IP and/or a |
| 303 | * specific local source port. |
| 304 | * |
| 305 | * You must specify either \ref OSMO_SOCK_F_BIND, or \ref |
| 306 | * OSMO_SOCK_F_CONNECT, or both. |
| 307 | * |
| 308 | * If \ref OSMO_SOCK_F_NONBLOCK is specified, the socket will be set to |
| 309 | * non-blocking mode. |
| 310 | */ |
| 311 | int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto, |
| 312 | const char *local_host, uint16_t local_port, |
| 313 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 314 | { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 315 | struct addrinfo *local = NULL, *remote = NULL, *rp; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 316 | int sfd = -1, rc, on = 1; |
| 317 | |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 318 | bool local_ipv4 = false, local_ipv6 = false; |
| 319 | bool remote_ipv4 = false, remote_ipv6 = false; |
| 320 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 321 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 322 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 323 | "BIND or CONNECT flags\n"); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 327 | /* figure out local address infos */ |
| 328 | if (flags & OSMO_SOCK_F_BIND) { |
| 329 | local = addrinfo_helper(family, type, proto, local_host, local_port, true); |
| 330 | if (!local) |
| 331 | return -EINVAL; |
| 332 | } |
| 333 | |
| 334 | /* figure out remote address infos */ |
| 335 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 336 | remote = addrinfo_helper(family, type, proto, remote_host, remote_port, false); |
| 337 | if (!remote) { |
| 338 | if (local) |
| 339 | freeaddrinfo(local); |
| 340 | |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* It must do a full run to ensure AF_UNSPEC does not fail. |
| 346 | * In case first local valid entry is IPv4 and only remote valid entry |
| 347 | * is IPv6 or vice versa */ |
| 348 | if (family == AF_UNSPEC) { |
| 349 | for (rp = local; rp != NULL; rp = rp->ai_next) { |
| 350 | switch (rp->ai_family) { |
| 351 | case AF_INET: |
| 352 | local_ipv4 = true; |
| 353 | break; |
| 354 | case AF_INET6: |
| 355 | local_ipv6 = true; |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | for (rp = remote; rp != NULL; rp = rp->ai_next) { |
| 361 | switch (rp->ai_family) { |
| 362 | case AF_INET: |
| 363 | remote_ipv4 = true; |
| 364 | break; |
| 365 | case AF_INET6: |
| 366 | remote_ipv6 = true; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | |
Pau Espin Pedrol | d8cf52b | 2020-08-31 19:00:59 +0200 | [diff] [blame] | 371 | if ((flags & OSMO_SOCK_F_BIND) && (flags & OSMO_SOCK_F_CONNECT)) { |
| 372 | /* prioritize ipv6 as per RFC */ |
| 373 | if (local_ipv6 && remote_ipv6) |
| 374 | family = AF_INET6; |
| 375 | else if (local_ipv4 && remote_ipv4) |
| 376 | family = AF_INET; |
| 377 | else { |
| 378 | if (local) |
| 379 | freeaddrinfo(local); |
| 380 | if (remote) |
| 381 | freeaddrinfo(remote); |
| 382 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 383 | "Unable to find a common protocol (IPv4 or IPv6) " |
| 384 | "for local host: %s and remote host: %s.\n", |
| 385 | local_host, remote_host); |
| 386 | return -ENODEV; |
| 387 | } |
| 388 | } else if ((flags & OSMO_SOCK_F_BIND)) { |
| 389 | family = local_ipv6 ? AF_INET6 : AF_INET; |
| 390 | } else if ((flags & OSMO_SOCK_F_CONNECT)) { |
| 391 | family = remote_ipv6 ? AF_INET6 : AF_INET; |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 395 | /* figure out local side of socket */ |
| 396 | if (flags & OSMO_SOCK_F_BIND) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 397 | for (rp = local; rp != NULL; rp = rp->ai_next) { |
| 398 | /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ |
| 399 | if (rp->ai_family != family) |
| 400 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 401 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 402 | sfd = socket_helper(rp, flags); |
| 403 | if (sfd < 0) |
| 404 | continue; |
| 405 | |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 406 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 407 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 408 | &on, sizeof(on)); |
| 409 | if (rc < 0) { |
| 410 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 411 | "cannot setsockopt socket:" |
| 412 | " %s:%u: %s\n", |
| 413 | local_host, local_port, |
| 414 | strerror(errno)); |
| 415 | close(sfd); |
| 416 | continue; |
| 417 | } |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 418 | } |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 419 | |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 420 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 421 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n", |
| 422 | local_host, local_port, strerror(errno)); |
| 423 | close(sfd); |
| 424 | continue; |
| 425 | } |
| 426 | break; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 427 | } |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 428 | |
| 429 | freeaddrinfo(local); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 430 | if (rp == NULL) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 431 | if (remote) |
| 432 | freeaddrinfo(remote); |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 433 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: %s:%u\n", |
| 434 | local_host, local_port); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 435 | return -ENODEV; |
| 436 | } |
| 437 | } |
| 438 | |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 439 | /* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it |
| 440 | was already closed and func returned. If OSMO_SOCK_F_BIND is not |
| 441 | set, then sfd = -1 */ |
| 442 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 443 | /* figure out remote side of socket */ |
| 444 | if (flags & OSMO_SOCK_F_CONNECT) { |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 445 | for (rp = remote; rp != NULL; rp = rp->ai_next) { |
| 446 | /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ |
| 447 | if (rp->ai_family != family) |
| 448 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 449 | |
Harald Welte | 5cfa6dc | 2017-07-21 16:52:29 +0200 | [diff] [blame] | 450 | if (sfd < 0) { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 451 | sfd = socket_helper(rp, flags); |
| 452 | if (sfd < 0) |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 457 | if (rc != 0 && errno != EINPROGRESS) { |
| 458 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 459 | remote_host, remote_port, strerror(errno)); |
| 460 | /* We want to maintain the bind socket if bind was enabled */ |
| 461 | if (!(flags & OSMO_SOCK_F_BIND)) { |
| 462 | close(sfd); |
| 463 | sfd = -1; |
| 464 | } |
| 465 | continue; |
| 466 | } |
| 467 | break; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 468 | } |
Alexander Couzens | 2c962f5 | 2020-06-03 00:28:02 +0200 | [diff] [blame] | 469 | |
| 470 | freeaddrinfo(remote); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 471 | if (rp == NULL) { |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 472 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable remote addr found for: %s:%u\n", |
| 473 | remote_host, remote_port); |
| 474 | if (sfd >= 0) |
| 475 | close(sfd); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 476 | return -ENODEV; |
| 477 | } |
| 478 | } |
| 479 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 480 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 481 | if (rc < 0) { |
| 482 | close(sfd); |
| 483 | sfd = -1; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 484 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 485 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 486 | return sfd; |
| 487 | } |
| 488 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 489 | #define _SOCKADDR_TO_STR(dest, sockaddr) do { \ |
| 490 | if (osmo_sockaddr_str_from_sockaddr(&dest, &sockaddr->u.sas)) \ |
| 491 | osmo_strlcpy(dest.ip, "Invalid IP", 11); \ |
| 492 | } while (0) |
| 493 | |
| 494 | /*! Initialize a socket (including bind and/or connect) |
| 495 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 496 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 497 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 498 | * \param[in] local local address |
| 499 | * \param[in] remote remote address |
| 500 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 501 | * \returns socket file descriptor on success; negative on error |
| 502 | * |
| 503 | * This function creates a new socket of the |
| 504 | * \a type and \a proto and optionally binds it to the \a local |
| 505 | * as well as optionally connects it to the \a remote |
| 506 | * depending on the value * of \a flags parameter. |
| 507 | * |
| 508 | * As opposed to \ref osmo_sock_init(), this function allows to combine |
| 509 | * the \ref OSMO_SOCK_F_BIND and \ref OSMO_SOCK_F_CONNECT flags. This |
| 510 | * is useful if you want to connect to a remote host/port, but still |
| 511 | * want to bind that socket to either a specific local alias IP and/or a |
| 512 | * specific local source port. |
| 513 | * |
| 514 | * You must specify either \ref OSMO_SOCK_F_BIND, or \ref |
| 515 | * OSMO_SOCK_F_CONNECT, or both. |
| 516 | * |
| 517 | * If \ref OSMO_SOCK_F_NONBLOCK is specified, the socket will be set to |
| 518 | * non-blocking mode. |
| 519 | */ |
| 520 | int osmo_sock_init_osa(uint16_t type, uint8_t proto, |
| 521 | const struct osmo_sockaddr *local, |
| 522 | const struct osmo_sockaddr *remote, |
| 523 | unsigned int flags) |
| 524 | { |
| 525 | int sfd = -1, rc, on = 1; |
| 526 | struct osmo_sockaddr_str sastr = {}; |
| 527 | |
| 528 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 529 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 530 | "BIND or CONNECT flags\n"); |
| 531 | return -EINVAL; |
| 532 | } |
| 533 | |
| 534 | if ((flags & OSMO_SOCK_F_BIND) && !local) { |
| 535 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid argument. Cannot BIND when local is NULL\n"); |
| 536 | return -EINVAL; |
| 537 | } |
| 538 | |
| 539 | if ((flags & OSMO_SOCK_F_CONNECT) && !remote) { |
| 540 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid argument. Cannot CONNECT when remote is NULL\n"); |
| 541 | return -EINVAL; |
| 542 | } |
| 543 | |
| 544 | if ((flags & OSMO_SOCK_F_BIND) && |
| 545 | (flags & OSMO_SOCK_F_CONNECT) && |
| 546 | local->u.sa.sa_family != remote->u.sa.sa_family) { |
| 547 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: the family for " |
| 548 | "local and remote endpoint must be same.\n"); |
| 549 | return -EINVAL; |
| 550 | } |
| 551 | |
| 552 | /* figure out local side of socket */ |
| 553 | if (flags & OSMO_SOCK_F_BIND) { |
| 554 | sfd = socket_helper_osa(local, type, proto, flags); |
| 555 | if (sfd < 0) { |
| 556 | _SOCKADDR_TO_STR(sastr, local); |
| 557 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: %s:%u\n", |
| 558 | sastr.ip, sastr.port); |
| 559 | return -ENODEV; |
| 560 | } |
| 561 | |
| 562 | if (proto != IPPROTO_UDP || (flags & OSMO_SOCK_F_UDP_REUSEADDR)) { |
| 563 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 564 | &on, sizeof(on)); |
| 565 | if (rc < 0) { |
| 566 | _SOCKADDR_TO_STR(sastr, local); |
| 567 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 568 | "cannot setsockopt socket:" |
| 569 | " %s:%u: %s\n", |
| 570 | sastr.ip, sastr.port, |
| 571 | strerror(errno)); |
| 572 | close(sfd); |
| 573 | return rc; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (bind(sfd, &local->u.sa, sizeof(struct osmo_sockaddr)) == -1) { |
| 578 | _SOCKADDR_TO_STR(sastr, local); |
| 579 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n", |
| 580 | sastr.ip, sastr.port, strerror(errno)); |
| 581 | close(sfd); |
| 582 | return -1; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it |
| 587 | was already closed and func returned. If OSMO_SOCK_F_BIND is not |
| 588 | set, then sfd = -1 */ |
| 589 | |
| 590 | /* figure out remote side of socket */ |
| 591 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 592 | if (sfd < 0) { |
| 593 | sfd = socket_helper_osa(remote, type, proto, flags); |
| 594 | if (sfd < 0) { |
| 595 | return sfd; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | rc = connect(sfd, &remote->u.sa, sizeof(struct osmo_sockaddr)); |
| 600 | if (rc != 0 && errno != EINPROGRESS) { |
| 601 | _SOCKADDR_TO_STR(sastr, remote); |
| 602 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 603 | sastr.ip, sastr.port, strerror(errno)); |
| 604 | close(sfd); |
| 605 | return rc; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 610 | if (rc < 0) { |
| 611 | close(sfd); |
| 612 | sfd = -1; |
| 613 | } |
| 614 | |
| 615 | return sfd; |
| 616 | } |
| 617 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 618 | #ifdef HAVE_LIBSCTP |
| 619 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 620 | /* Check whether there's an IPv6 Addr as first option of any addrinfo item in the addrinfo set */ |
| 621 | static void addrinfo_has_v4v6addr(const struct addrinfo **result, size_t result_count, bool *has_v4, bool *has_v6) |
| 622 | { |
| 623 | size_t host_idx; |
| 624 | *has_v4 = false; |
| 625 | *has_v6 = false; |
| 626 | |
| 627 | for (host_idx = 0; host_idx < result_count; host_idx++) { |
| 628 | if (result[host_idx]->ai_family == AF_INET) |
| 629 | *has_v4 = true; |
| 630 | else if (result[host_idx]->ai_family == AF_INET6) |
| 631 | *has_v6 = true; |
| 632 | } |
| 633 | } |
| 634 | |
Pau Espin Pedrol | 4f463c5 | 2020-08-28 14:32:02 +0200 | [diff] [blame] | 635 | /* Check whether there's an IPv6 with IN6ADDR_ANY_INIT ("::") */ |
| 636 | static bool addrinfo_has_in6addr_any(const struct addrinfo **result, size_t result_count) |
| 637 | { |
| 638 | size_t host_idx; |
| 639 | struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; |
| 640 | |
| 641 | for (host_idx = 0; host_idx < result_count; host_idx++) { |
| 642 | if (result[host_idx]->ai_family != AF_INET6) |
| 643 | continue; |
| 644 | if (memcmp(&((struct sockaddr_in6 *)result[host_idx]->ai_addr)->sin6_addr, |
| 645 | &in6addr_any, sizeof(in6addr_any)) == 0) |
| 646 | return true; |
| 647 | } |
| 648 | return false; |
| 649 | } |
| 650 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 651 | static int socket_helper_multiaddr(uint16_t family, uint16_t type, uint8_t proto, unsigned int flags) |
| 652 | { |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 653 | int sfd, rc; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 654 | |
| 655 | sfd = socket(family, type, proto); |
| 656 | if (sfd == -1) { |
| 657 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 658 | "Unable to create socket: %s\n", strerror(errno)); |
| 659 | return sfd; |
| 660 | } |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 661 | |
| 662 | rc = socket_helper_tail(sfd, flags); |
| 663 | if (rc < 0) |
| 664 | return rc; |
| 665 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 666 | return sfd; |
| 667 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 668 | |
| 669 | /* 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] | 670 | * for each host in addrs_buf. */ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 671 | static int addrinfo_to_sockaddr(uint16_t family, const struct addrinfo **result, |
| 672 | const char **hosts, int host_cont, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 673 | uint8_t *addrs_buf, size_t addrs_buf_len) { |
| 674 | size_t host_idx, offset = 0; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 675 | const struct addrinfo *rp; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 676 | |
| 677 | for (host_idx = 0; host_idx < host_cont; host_idx++) { |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 678 | /* Addresses are ordered based on RFC 3484, see man getaddrinfo */ |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 679 | for (rp = result[host_idx]; rp != NULL; rp = rp->ai_next) { |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 680 | if (family != AF_UNSPEC && rp->ai_family != family) |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 681 | continue; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 682 | if (offset + rp->ai_addrlen > addrs_buf_len) { |
| 683 | LOGP(DLGLOBAL, LOGL_ERROR, "Output buffer to small: %zu\n", |
| 684 | addrs_buf_len); |
| 685 | return -ENOSPC; |
| 686 | } |
| 687 | memcpy(addrs_buf + offset, rp->ai_addr, rp->ai_addrlen); |
| 688 | offset += rp->ai_addrlen; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 689 | break; |
| 690 | } |
| 691 | if (!rp) { /* No addr could be bound for this host! */ |
| 692 | LOGP(DLGLOBAL, LOGL_ERROR, "No suitable remote address found for host: %s\n", |
| 693 | hosts[host_idx]); |
| 694 | return -ENODEV; |
| 695 | } |
| 696 | } |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | /*! Initialize a socket (including bind and/or connect) with multiple local or remote addresses. |
| 701 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 702 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 703 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 704 | * \param[in] local_hosts array of char pointers (strings), each containing local host name or IP address in string form |
| 705 | * \param[in] local_hosts_cnt length of local_hosts (in items) |
| 706 | * \param[in] local_port local port number in host byte order |
| 707 | * \param[in] remote_host array of char pointers (strings), each containing remote host name or IP address in string form |
| 708 | * \param[in] remote_hosts_cnt length of remote_hosts (in items) |
| 709 | * \param[in] remote_port remote port number in host byte order |
| 710 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 711 | * \returns socket file descriptor on success; negative on error |
| 712 | * |
| 713 | * This function is similar to \ref osmo_sock_init2(), but can be passed an |
| 714 | * array of local or remote addresses for protocols supporting multiple |
| 715 | * addresses per socket, like SCTP (currently only one supported). This function |
| 716 | * should not be used by protocols not supporting this kind of features, but |
| 717 | * rather \ref osmo_sock_init2() should be used instead. |
| 718 | * See \ref osmo_sock_init2() for more information on flags and general behavior. |
| 719 | */ |
| 720 | int osmo_sock_init2_multiaddr(uint16_t family, uint16_t type, uint8_t proto, |
| 721 | const char **local_hosts, size_t local_hosts_cnt, uint16_t local_port, |
| 722 | const char **remote_hosts, size_t remote_hosts_cnt, uint16_t remote_port, |
| 723 | unsigned int flags) |
| 724 | |
| 725 | { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 726 | 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] | 727 | int sfd = -1, rc, on = 1; |
| 728 | int i; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 729 | bool loc_has_v4addr, rem_has_v4addr; |
| 730 | bool loc_has_v6addr, rem_has_v6addr; |
| 731 | struct sockaddr_in6 addrs_buf[OSMO_SOCK_MAX_ADDRS]; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 732 | char strbuf[512]; |
| 733 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 734 | /* updated later in case of AF_UNSPEC */ |
| 735 | loc_has_v4addr = rem_has_v4addr = (family == AF_INET); |
| 736 | loc_has_v6addr = rem_has_v6addr = (family == AF_INET6); |
| 737 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 738 | /* TODO: So far this function is only aimed for SCTP, but could be |
| 739 | reused in the future for other protocols with multi-addr support */ |
| 740 | if (proto != IPPROTO_SCTP) |
| 741 | return -ENOTSUP; |
| 742 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 743 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 744 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 745 | "BIND or CONNECT flags\n"); |
| 746 | return -EINVAL; |
| 747 | } |
| 748 | |
| 749 | if (((flags & OSMO_SOCK_F_BIND) && !local_hosts_cnt) || |
| 750 | ((flags & OSMO_SOCK_F_CONNECT) && !remote_hosts_cnt) || |
| 751 | local_hosts_cnt > OSMO_SOCK_MAX_ADDRS || |
| 752 | remote_hosts_cnt > OSMO_SOCK_MAX_ADDRS) |
| 753 | return -EINVAL; |
| 754 | |
| 755 | /* figure out local side of socket */ |
| 756 | if (flags & OSMO_SOCK_F_BIND) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 757 | rc = addrinfo_helper_multi(res_loc, family, type, proto, local_hosts, |
| 758 | local_hosts_cnt, local_port, true); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 759 | if (rc < 0) |
| 760 | return -EINVAL; |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 761 | /* Figure out if there's any IPV4 or IPv6 addr in the set */ |
| 762 | if (family == AF_UNSPEC) |
| 763 | addrinfo_has_v4v6addr((const struct addrinfo **)res_loc, local_hosts_cnt, |
| 764 | &loc_has_v4addr, &loc_has_v6addr); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 765 | } |
| 766 | /* figure out remote side of socket */ |
| 767 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 768 | rc = addrinfo_helper_multi(res_rem, family, type, proto, remote_hosts, |
| 769 | remote_hosts_cnt, remote_port, false); |
Pau Espin Pedrol | 4541cf2 | 2020-08-25 11:22:09 +0200 | [diff] [blame] | 770 | if (rc < 0) { |
| 771 | rc = -EINVAL; |
| 772 | goto ret_freeaddrinfo_loc; |
| 773 | } |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 774 | /* Figure out if there's any IPv4 or IPv6 addr in the set */ |
| 775 | if (family == AF_UNSPEC) |
| 776 | addrinfo_has_v4v6addr((const struct addrinfo **)res_rem, remote_hosts_cnt, |
| 777 | &rem_has_v4addr, &rem_has_v6addr); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 778 | } |
| 779 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 780 | if (((flags & OSMO_SOCK_F_BIND) && (flags & OSMO_SOCK_F_CONNECT)) && |
Pau Espin Pedrol | 4f463c5 | 2020-08-28 14:32:02 +0200 | [diff] [blame] | 781 | !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] | 782 | (loc_has_v4addr != rem_has_v4addr || loc_has_v6addr != rem_has_v6addr)) { |
| 783 | LOGP(DLGLOBAL, LOGL_ERROR, "Invalid v4 vs v6 in local vs remote addresses\n"); |
| 784 | rc = -EINVAL; |
| 785 | goto ret_freeaddrinfo; |
| 786 | } |
| 787 | |
| 788 | sfd = socket_helper_multiaddr(loc_has_v6addr ? AF_INET6 : AF_INET, |
| 789 | type, proto, flags); |
| 790 | if (sfd < 0) { |
| 791 | rc = sfd; |
| 792 | goto ret_freeaddrinfo; |
| 793 | } |
| 794 | |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 795 | if (flags & OSMO_SOCK_F_BIND) { |
Pau Espin Pedrol | 272dfc1 | 2019-10-21 11:11:27 +0200 | [diff] [blame] | 796 | /* Since so far we only allow IPPROTO_SCTP in this function, |
| 797 | no need to check below for "proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR" */ |
| 798 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 799 | &on, sizeof(on)); |
| 800 | if (rc < 0) { |
| 801 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 802 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 803 | "cannot setsockopt socket:" |
| 804 | " %s:%u: %s\n", |
| 805 | strbuf, local_port, |
| 806 | strerror(errno)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 807 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 808 | } |
| 809 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 810 | /* Build array of addresses taking first entry for each host. |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 811 | TODO: Ideally we should use backtracking storing last used |
| 812 | indexes and trying next combination if connect() fails .*/ |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 813 | /* 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] | 814 | rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)res_loc, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 815 | local_hosts, local_hosts_cnt, |
| 816 | (uint8_t*)addrs_buf, sizeof(addrs_buf)); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 817 | if (rc < 0) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 818 | rc = -ENODEV; |
| 819 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 822 | rc = sctp_bindx(sfd, (struct sockaddr *)addrs_buf, local_hosts_cnt, SCTP_BINDX_ADD_ADDR); |
| 823 | if (rc == -1) { |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 824 | multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt); |
| 825 | LOGP(DLGLOBAL, LOGL_NOTICE, "unable to bind socket: %s:%u: %s\n", |
| 826 | strbuf, local_port, strerror(errno)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 827 | rc = -ENODEV; |
| 828 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 829 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 832 | if (flags & OSMO_SOCK_F_CONNECT) { |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 833 | /* Build array of addresses taking first of same family for each host. |
| 834 | TODO: Ideally we should use backtracking storing last used |
| 835 | indexes and trying next combination if connect() fails .*/ |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 836 | rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)res_rem, |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 837 | remote_hosts, remote_hosts_cnt, |
| 838 | (uint8_t*)addrs_buf, sizeof(addrs_buf)); |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 839 | if (rc < 0) { |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 840 | rc = -ENODEV; |
| 841 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 842 | } |
| 843 | |
Pau Espin Pedrol | cd13331 | 2020-08-19 17:25:04 +0200 | [diff] [blame] | 844 | 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] | 845 | if (rc != 0 && errno != EINPROGRESS) { |
| 846 | multiaddr_snprintf(strbuf, sizeof(strbuf), remote_hosts, remote_hosts_cnt); |
| 847 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 848 | strbuf, remote_port, strerror(errno)); |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 849 | rc = -ENODEV; |
| 850 | goto ret_close; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 851 | } |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 855 | if (rc < 0) { |
| 856 | close(sfd); |
| 857 | sfd = -1; |
| 858 | } |
| 859 | |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 860 | rc = sfd; |
| 861 | goto ret_freeaddrinfo; |
| 862 | |
| 863 | ret_close: |
| 864 | if (sfd >= 0) |
| 865 | close(sfd); |
| 866 | ret_freeaddrinfo: |
Pau Espin Pedrol | 4541cf2 | 2020-08-25 11:22:09 +0200 | [diff] [blame] | 867 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 868 | for (i = 0; i < remote_hosts_cnt; i++) |
| 869 | freeaddrinfo(res_rem[i]); |
| 870 | } |
| 871 | ret_freeaddrinfo_loc: |
| 872 | if (flags & OSMO_SOCK_F_BIND) { |
| 873 | for (i = 0; i < local_hosts_cnt; i++) |
| 874 | freeaddrinfo(res_loc[i]); |
| 875 | } |
Pau Espin Pedrol | 796c651 | 2020-08-19 12:03:25 +0200 | [diff] [blame] | 876 | return rc; |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 877 | } |
| 878 | #endif /* HAVE_LIBSCTP */ |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 879 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 880 | /*! Initialize a socket (including bind/connect) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 881 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 882 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 883 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 884 | * \param[in] host remote host name or IP address in string form |
| 885 | * \param[in] port remote port number in host byte order |
| 886 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 887 | * \returns socket file descriptor on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 888 | * |
| 889 | * This function creates a new socket of the designated \a family, \a |
| 890 | * type and \a proto and optionally binds or connects it, depending on |
| 891 | * the value of \a flags parameter. |
| 892 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 893 | 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] | 894 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 895 | { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 896 | struct addrinfo *result, *rp; |
Pau Espin Pedrol | 6fe865d | 2021-07-15 13:08:08 +0200 | [diff] [blame] | 897 | int sfd = -1; /* initialize to avoid uninitialized false warnings on some gcc versions (11.1.0) */ |
| 898 | int on = 1; |
| 899 | int rc; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 900 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 901 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 902 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 903 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: both bind and connect flags set:" |
Neels Hofmeyr | b7f191f | 2016-08-29 11:22:03 +0200 | [diff] [blame] | 904 | " %s:%u\n", host, port); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 905 | return -EINVAL; |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 906 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 907 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 908 | 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] | 909 | if (!result) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 910 | return -EINVAL; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 911 | |
| 912 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 913 | sfd = socket_helper(rp, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 914 | if (sfd == -1) |
| 915 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 916 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 917 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 918 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 919 | if (rc != 0 && errno != EINPROGRESS) { |
| 920 | close(sfd); |
| 921 | continue; |
| 922 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 923 | } else { |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 924 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 925 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 926 | &on, sizeof(on)); |
| 927 | if (rc < 0) { |
| 928 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 929 | "cannot setsockopt socket:" |
| 930 | " %s:%u: %s\n", |
| 931 | host, port, strerror(errno)); |
| 932 | close(sfd); |
| 933 | continue; |
| 934 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 935 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 936 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 937 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket:" |
| 938 | "%s:%u: %s\n", |
| 939 | host, port, strerror(errno)); |
| 940 | close(sfd); |
| 941 | continue; |
| 942 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 943 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 944 | break; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 945 | } |
| 946 | freeaddrinfo(result); |
| 947 | |
| 948 | if (rp == NULL) { |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 949 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable addr found for: %s:%u\n", |
| 950 | host, port); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 951 | return -ENODEV; |
| 952 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 953 | |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 954 | if (proto != IPPROTO_UDP || flags & OSMO_SOCK_F_UDP_REUSEADDR) { |
Philipp Maier | 99f706d | 2018-08-01 12:40:36 +0200 | [diff] [blame] | 955 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 956 | if (rc < 0) { |
| 957 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 958 | "cannot setsockopt socket: %s:%u: %s\n", host, |
| 959 | port, strerror(errno)); |
| 960 | close(sfd); |
| 961 | sfd = -1; |
| 962 | } |
Philipp Maier | 0659c5d | 2018-08-01 12:43:08 +0200 | [diff] [blame] | 963 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 964 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 965 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 966 | if (rc < 0) { |
| 967 | close(sfd); |
| 968 | sfd = -1; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 969 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 970 | |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 971 | return sfd; |
| 972 | } |
| 973 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 974 | /*! fill \ref osmo_fd for a give sfd |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 975 | * \param[out] ofd file descriptor (will be filled in) |
| 976 | * \param[in] sfd socket file descriptor |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 977 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 978 | * \returns socket fd on success; negative on error |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 979 | * |
| 980 | * This function fills the \a ofd structure. |
| 981 | */ |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 982 | 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] | 983 | { |
| 984 | int rc; |
| 985 | |
| 986 | if (sfd < 0) |
| 987 | return sfd; |
| 988 | |
| 989 | ofd->fd = sfd; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 990 | ofd->when = OSMO_FD_READ; |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 991 | |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 992 | /* if we're doing a non-blocking connect, the completion will be signaled |
| 993 | * by marking the fd as WRITE-able. So in this exceptional case, we're |
| 994 | * also interested in when the socket becomes write-able */ |
| 995 | if ((flags & (OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK)) == |
| 996 | (OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK)) { |
| 997 | ofd->when |= OSMO_FD_WRITE; |
| 998 | } |
| 999 | |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 1000 | rc = osmo_fd_register(ofd); |
| 1001 | if (rc < 0) { |
| 1002 | close(sfd); |
| 1003 | return rc; |
| 1004 | } |
| 1005 | |
| 1006 | return sfd; |
| 1007 | } |
| 1008 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1009 | /*! Initialize a socket and fill \ref osmo_fd |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 1010 | * \param[out] ofd file descriptor (will be filled in) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1011 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 1012 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1013 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1014 | * \param[in] host remote host name or IP address in string form |
| 1015 | * \param[in] port remote port number in host byte order |
| 1016 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1017 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1018 | * |
| 1019 | * This function creates (and optionall binds/connects) a socket using |
| 1020 | * \ref osmo_sock_init, but also fills the \a ofd structure. |
| 1021 | */ |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 1022 | 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] | 1023 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 1024 | { |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1025 | 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] | 1026 | } |
| 1027 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1028 | /*! Initialize a socket and fill \ref osmo_fd |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 1029 | * \param[out] ofd file descriptor (will be filled in) |
| 1030 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 1031 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1032 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1033 | * \param[in] local_host local host name or IP address in string form |
| 1034 | * \param[in] local_port local port number in host byte order |
| 1035 | * \param[in] remote_host remote host name or IP address in string form |
| 1036 | * \param[in] remote_port remote port number in host byte order |
| 1037 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 1038 | * \returns socket fd on success; negative on error |
| 1039 | * |
| 1040 | * This function creates (and optionall binds/connects) a socket using |
| 1041 | * \ref osmo_sock_init2, but also fills the \a ofd structure. |
| 1042 | */ |
| 1043 | int osmo_sock_init2_ofd(struct osmo_fd *ofd, int family, int type, int proto, |
| 1044 | const char *local_host, uint16_t local_port, |
| 1045 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 1046 | { |
| 1047 | 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] | 1048 | local_port, remote_host, remote_port, flags), flags); |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 1051 | int osmo_sock_init_osa_ofd(struct osmo_fd *ofd, int type, int proto, |
| 1052 | const struct osmo_sockaddr *local, |
| 1053 | const struct osmo_sockaddr *remote, unsigned int flags) |
| 1054 | { |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1055 | 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] | 1056 | } |
| 1057 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1058 | /*! Initialize a socket and fill \ref sockaddr |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1059 | * \param[out] ss socket address (will be filled in) |
| 1060 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1061 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1062 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1063 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1064 | * |
| 1065 | * This function creates (and optionall binds/connects) a socket using |
| 1066 | * \ref osmo_sock_init, but also fills the \a ss structure. |
| 1067 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1068 | int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1069 | uint8_t proto, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1070 | { |
| 1071 | char host[NI_MAXHOST]; |
| 1072 | uint16_t port; |
| 1073 | struct sockaddr_in *sin; |
| 1074 | struct sockaddr_in6 *sin6; |
| 1075 | int s, sa_len; |
| 1076 | |
| 1077 | /* determine port and host from ss */ |
| 1078 | switch (ss->sa_family) { |
| 1079 | case AF_INET: |
| 1080 | sin = (struct sockaddr_in *) ss; |
| 1081 | sa_len = sizeof(struct sockaddr_in); |
| 1082 | port = ntohs(sin->sin_port); |
| 1083 | break; |
| 1084 | case AF_INET6: |
| 1085 | sin6 = (struct sockaddr_in6 *) ss; |
| 1086 | sa_len = sizeof(struct sockaddr_in6); |
| 1087 | port = ntohs(sin6->sin6_port); |
| 1088 | break; |
| 1089 | default: |
| 1090 | return -EINVAL; |
| 1091 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1092 | |
| 1093 | s = getnameinfo(ss, sa_len, host, NI_MAXHOST, |
| 1094 | NULL, 0, NI_NUMERICHOST); |
| 1095 | if (s != 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 1096 | LOGP(DLGLOBAL, LOGL_ERROR, "getnameinfo failed:" |
| 1097 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1098 | return s; |
| 1099 | } |
| 1100 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 1101 | return osmo_sock_init(ss->sa_family, type, proto, host, port, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | static int sockaddr_equal(const struct sockaddr *a, |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 1105 | const struct sockaddr *b, unsigned int len) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1106 | { |
| 1107 | struct sockaddr_in *sin_a, *sin_b; |
| 1108 | struct sockaddr_in6 *sin6_a, *sin6_b; |
| 1109 | |
| 1110 | if (a->sa_family != b->sa_family) |
| 1111 | return 0; |
| 1112 | |
| 1113 | switch (a->sa_family) { |
| 1114 | case AF_INET: |
| 1115 | sin_a = (struct sockaddr_in *)a; |
| 1116 | sin_b = (struct sockaddr_in *)b; |
| 1117 | if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr, |
| 1118 | sizeof(struct in_addr))) |
| 1119 | return 1; |
| 1120 | break; |
| 1121 | case AF_INET6: |
| 1122 | sin6_a = (struct sockaddr_in6 *)a; |
| 1123 | sin6_b = (struct sockaddr_in6 *)b; |
| 1124 | if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr, |
| 1125 | sizeof(struct in6_addr))) |
| 1126 | return 1; |
| 1127 | break; |
| 1128 | } |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
Eric | 8ae40cb | 2021-09-27 22:10:01 +0200 | [diff] [blame] | 1132 | /* linux has a default route: |
| 1133 | local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1 |
| 1134 | */ |
| 1135 | static int sockaddr_is_local_routed(const struct sockaddr *a) |
| 1136 | { |
| 1137 | #if __linux__ |
| 1138 | if (a->sa_family != AF_INET) |
| 1139 | return 0; |
| 1140 | |
| 1141 | uint32_t address = ((struct sockaddr_in *)a)->sin_addr.s_addr; /* already BE */ |
| 1142 | uint32_t eightmask = htonl(0xff000000); /* /8 mask */ |
| 1143 | uint32_t local_prefix_127 = htonl(0x7f000000); /* 127.0.0.0 */ |
| 1144 | |
| 1145 | if ((address & eightmask) == local_prefix_127) |
| 1146 | return 1; |
| 1147 | #endif |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1151 | /*! Determine if the given address is a local address |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1152 | * \param[in] addr Socket Address |
| 1153 | * \param[in] addrlen Length of socket address in bytes |
| 1154 | * \returns 1 if address is local, 0 otherwise. |
| 1155 | */ |
Harald Welte | bc32d05 | 2012-04-08 11:31:32 +0200 | [diff] [blame] | 1156 | int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1157 | { |
| 1158 | struct ifaddrs *ifaddr, *ifa; |
| 1159 | |
Eric | 8ae40cb | 2021-09-27 22:10:01 +0200 | [diff] [blame] | 1160 | if (sockaddr_is_local_routed(addr)) |
| 1161 | return 1; |
| 1162 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1163 | if (getifaddrs(&ifaddr) == -1) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 1164 | LOGP(DLGLOBAL, LOGL_ERROR, "getifaddrs:" |
| 1165 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1166 | return -EIO; |
| 1167 | } |
| 1168 | |
| 1169 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
Harald Welte | 4d3a7b1 | 2011-05-24 21:31:53 +0200 | [diff] [blame] | 1170 | if (!ifa->ifa_addr) |
| 1171 | continue; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 1172 | if (sockaddr_equal(ifa->ifa_addr, addr, addrlen)) { |
| 1173 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1174 | return 1; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 1175 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 1178 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1179 | return 0; |
| 1180 | } |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 1181 | |
Max | 9d7a247 | 2018-11-20 15:18:31 +0100 | [diff] [blame] | 1182 | /*! Convert sockaddr_in to IP address as char string and port as uint16_t. |
| 1183 | * \param[out] addr String buffer to write IP address to, or NULL. |
| 1184 | * \param[out] addr_len Size of \a addr. |
| 1185 | * \param[out] port Pointer to uint16_t to write the port number to, or NULL. |
| 1186 | * \param[in] sin Sockaddr to convert. |
| 1187 | * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL. |
| 1188 | */ |
| 1189 | size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 1190 | const struct sockaddr_in *sin) |
| 1191 | { |
| 1192 | if (port) |
| 1193 | *port = ntohs(sin->sin_port); |
| 1194 | |
| 1195 | if (addr) |
| 1196 | return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len); |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 1201 | /*! Convert sockaddr to IP address as char string and port as uint16_t. |
| 1202 | * \param[out] addr String buffer to write IP address to, or NULL. |
| 1203 | * \param[out] addr_len Size of \a addr. |
| 1204 | * \param[out] port Pointer to uint16_t to write the port number to, or NULL. |
| 1205 | * \param[in] sa Sockaddr to convert. |
| 1206 | * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL. |
| 1207 | */ |
| 1208 | unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 1209 | const struct sockaddr *sa) |
| 1210 | { |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 1211 | |
Pau Espin Pedrol | 1a3d24e | 2020-08-28 18:31:32 +0200 | [diff] [blame] | 1212 | const struct sockaddr_in6 *sin6; |
| 1213 | |
| 1214 | switch (sa->sa_family) { |
| 1215 | case AF_INET: |
| 1216 | return osmo_sockaddr_in_to_str_and_uint(addr, addr_len, port, |
| 1217 | (const struct sockaddr_in *)sa); |
| 1218 | case AF_INET6: |
| 1219 | sin6 = (const struct sockaddr_in6 *)sa; |
| 1220 | if (port) |
| 1221 | *port = ntohs(sin6->sin6_port); |
| 1222 | if (addr && inet_ntop(sa->sa_family, &sin6->sin6_addr, addr, addr_len)) |
| 1223 | return strlen(addr); |
| 1224 | break; |
| 1225 | } |
| 1226 | return 0; |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 1227 | } |
| 1228 | |
Pau Espin Pedrol | 5cc4fe4 | 2020-08-31 12:52:06 +0200 | [diff] [blame] | 1229 | /*! inet_ntop() wrapper for a struct sockaddr. |
| 1230 | * \param[in] sa source sockaddr to get the address from. |
| 1231 | * \param[out] dst string buffer of at least INET6_ADDRSTRLEN size. |
| 1232 | * \returns returns a non-null pointer to dst. NULL is returned if there was an |
| 1233 | * error, with errno set to indicate the error. |
| 1234 | */ |
| 1235 | const char *osmo_sockaddr_ntop(const struct sockaddr *sa, char *dst) |
| 1236 | { |
| 1237 | const struct osmo_sockaddr *osa = (const struct osmo_sockaddr *)sa; |
| 1238 | return inet_ntop(osa->u.sa.sa_family, |
| 1239 | osa->u.sa.sa_family == AF_INET6 ? |
| 1240 | (const void *)&osa->u.sin6.sin6_addr : |
| 1241 | (const void *)&osa->u.sin.sin_addr, |
| 1242 | dst, INET6_ADDRSTRLEN); |
| 1243 | } |
| 1244 | |
| 1245 | /*! Get sockaddr port content (in host byte order) |
| 1246 | * \param[in] sa source sockaddr to get the port from. |
| 1247 | * \returns returns the sockaddr port in host byte order |
| 1248 | */ |
| 1249 | uint16_t osmo_sockaddr_port(const struct sockaddr *sa) |
| 1250 | { |
| 1251 | const struct osmo_sockaddr *osa = (const struct osmo_sockaddr *)sa; |
| 1252 | switch (osa->u.sa.sa_family) { |
| 1253 | case AF_INET6: |
| 1254 | return ntohs(osa->u.sin6.sin6_port); |
| 1255 | case AF_INET: |
| 1256 | return ntohs(osa->u.sin.sin_port); |
| 1257 | } |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1261 | /*! Initialize a unix domain socket (including bind/connect) |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1262 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1263 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1264 | * \param[in] socket_path path to identify the socket |
| 1265 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1266 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1267 | * |
| 1268 | * This function creates a new unix domain socket, \a |
| 1269 | * type and \a proto and optionally binds or connects it, depending on |
| 1270 | * the value of \a flags parameter. |
| 1271 | */ |
Eric Wild | eb5769b | 2019-06-27 15:31:17 +0200 | [diff] [blame] | 1272 | #if defined(__clang__) && defined(SUN_LEN) |
| 1273 | __attribute__((no_sanitize("undefined"))) |
| 1274 | #endif |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1275 | int osmo_sock_unix_init(uint16_t type, uint8_t proto, |
| 1276 | const char *socket_path, unsigned int flags) |
| 1277 | { |
| 1278 | struct sockaddr_un local; |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 1279 | int sfd, rc; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1280 | unsigned int namelen; |
| 1281 | |
| 1282 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
| 1283 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) |
| 1284 | return -EINVAL; |
| 1285 | |
| 1286 | local.sun_family = AF_UNIX; |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1287 | /* When an AF_UNIX socket is bound, sun_path should be NUL-terminated. See unix(7) man page. */ |
| 1288 | 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] | 1289 | LOGP(DLGLOBAL, LOGL_ERROR, "Socket path exceeds maximum length of %zd bytes: %s\n", |
| 1290 | sizeof(local.sun_path), socket_path); |
| 1291 | return -ENOSPC; |
| 1292 | } |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1293 | |
| 1294 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1295 | local.sun_len = strlen(local.sun_path); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1296 | #endif |
| 1297 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 1298 | namelen = SUN_LEN(&local); |
| 1299 | #else |
Stefan Sperling | 6afb3f5 | 2018-09-20 17:21:05 +0200 | [diff] [blame] | 1300 | namelen = strlen(local.sun_path) + |
| 1301 | offsetof(struct sockaddr_un, sun_path); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1302 | #endif |
| 1303 | |
| 1304 | sfd = socket(AF_UNIX, type, proto); |
| 1305 | if (sfd < 0) |
| 1306 | return -1; |
| 1307 | |
| 1308 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 1309 | rc = connect(sfd, (struct sockaddr *)&local, namelen); |
| 1310 | if (rc < 0) |
| 1311 | goto err; |
| 1312 | } else { |
| 1313 | unlink(local.sun_path); |
| 1314 | rc = bind(sfd, (struct sockaddr *)&local, namelen); |
| 1315 | if (rc < 0) |
| 1316 | goto err; |
| 1317 | } |
| 1318 | |
Harald Welte | faf6b70 | 2021-04-28 13:04:59 +0200 | [diff] [blame] | 1319 | rc = socket_helper_tail(sfd, flags); |
| 1320 | if (rc < 0) |
| 1321 | return rc; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1322 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 1323 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 1324 | if (rc < 0) { |
| 1325 | close(sfd); |
| 1326 | sfd = -1; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | return sfd; |
| 1330 | err: |
| 1331 | close(sfd); |
| 1332 | return -1; |
| 1333 | } |
| 1334 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1335 | /*! Initialize a unix domain socket and fill \ref osmo_fd |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1336 | * \param[out] ofd file descriptor (will be filled in) |
| 1337 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 1338 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 1339 | * \param[in] socket_path path to identify the socket |
| 1340 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1341 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1342 | * |
Vadim Yanitskiy | b606d76 | 2019-06-01 19:02:47 +0700 | [diff] [blame] | 1343 | * This function creates (and optionally binds/connects) a socket |
| 1344 | * using osmo_sock_unix_init, but also fills the ofd structure. |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 1345 | */ |
| 1346 | int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto, |
| 1347 | const char *socket_path, unsigned int flags) |
| 1348 | { |
Harald Welte | 24980ba | 2021-04-23 14:07:18 +0200 | [diff] [blame] | 1349 | 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] | 1350 | } |
| 1351 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1352 | /*! 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] | 1353 | * \param[in] fd file descriptor of socket |
| 1354 | * \param[out] ip IP address (will be filled in when not NULL) |
| 1355 | * \param[in] ip_len length of the ip buffer |
| 1356 | * \param[out] port number (will be filled in when not NULL) |
| 1357 | * \param[in] port_len length of the port buffer |
| 1358 | * \param[in] local (true) or remote (false) name will get looked at |
| 1359 | * \returns 0 on success; negative otherwise |
| 1360 | */ |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1361 | 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] | 1362 | { |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1363 | struct sockaddr_storage sa; |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1364 | socklen_t len = sizeof(sa); |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 1365 | char ipbuf[INET6_ADDRSTRLEN], portbuf[6]; |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1366 | int rc; |
| 1367 | |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1368 | 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] | 1369 | if (rc < 0) |
| 1370 | return rc; |
| 1371 | |
Pau Espin Pedrol | ed42a88 | 2020-08-21 15:39:17 +0200 | [diff] [blame] | 1372 | rc = getnameinfo((const struct sockaddr*)&sa, len, ipbuf, sizeof(ipbuf), |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1373 | portbuf, sizeof(portbuf), |
| 1374 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 1375 | if (rc < 0) |
| 1376 | return rc; |
| 1377 | |
| 1378 | if (ip) |
| 1379 | strncpy(ip, ipbuf, ip_len); |
| 1380 | if (port) |
| 1381 | strncpy(port, portbuf, port_len); |
| 1382 | return 0; |
| 1383 | } |
| 1384 | |
| 1385 | /*! Get local IP address on socket |
| 1386 | * \param[in] fd file descriptor of socket |
| 1387 | * \param[out] ip IP address (will be filled in) |
| 1388 | * \param[in] len length of the output buffer |
| 1389 | * \returns 0 on success; negative otherwise |
| 1390 | */ |
| 1391 | int osmo_sock_get_local_ip(int fd, char *ip, size_t len) |
| 1392 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1393 | 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] | 1394 | } |
| 1395 | |
| 1396 | /*! Get local port on socket |
| 1397 | * \param[in] fd file descriptor of socket |
| 1398 | * \param[out] port number (will be filled in) |
| 1399 | * \param[in] len length of the output buffer |
| 1400 | * \returns 0 on success; negative otherwise |
| 1401 | */ |
| 1402 | int osmo_sock_get_local_ip_port(int fd, char *port, size_t len) |
| 1403 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1404 | 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] | 1405 | } |
| 1406 | |
| 1407 | /*! Get remote IP address on socket |
| 1408 | * \param[in] fd file descriptor of socket |
| 1409 | * \param[out] ip IP address (will be filled in) |
| 1410 | * \param[in] len length of the output buffer |
| 1411 | * \returns 0 on success; negative otherwise |
| 1412 | */ |
| 1413 | int osmo_sock_get_remote_ip(int fd, char *ip, size_t len) |
| 1414 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1415 | 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] | 1416 | } |
| 1417 | |
| 1418 | /*! Get remote port on socket |
| 1419 | * \param[in] fd file descriptor of socket |
| 1420 | * \param[out] port number (will be filled in) |
| 1421 | * \param[in] len length of the output buffer |
| 1422 | * \returns 0 on success; negative otherwise |
| 1423 | */ |
| 1424 | int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len) |
| 1425 | { |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1426 | 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] | 1427 | } |
| 1428 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1429 | /*! Get address/port information on socket in dyn-alloc string like "(r=1.2.3.4:5<->l=6.7.8.9:10)". |
| 1430 | * Usually, it is better to use osmo_sock_get_name2() for a static string buffer or osmo_sock_get_name_buf() for a |
| 1431 | * caller provided string buffer, to avoid the dynamic talloc allocation. |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1432 | * \param[in] ctx talloc context from which to allocate string buffer |
| 1433 | * \param[in] fd file descriptor of socket |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1434 | * \returns string identifying the connection of this socket, talloc'd from ctx. |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1435 | */ |
Harald Welte | c0dfc9d | 2019-03-18 18:29:43 +0100 | [diff] [blame] | 1436 | char *osmo_sock_get_name(const void *ctx, int fd) |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1437 | { |
Philipp Maier | 64b51eb | 2019-01-14 11:59:11 +0100 | [diff] [blame] | 1438 | char str[OSMO_SOCK_NAME_MAXLEN]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1439 | int rc; |
| 1440 | rc = osmo_sock_get_name_buf(str, sizeof(str), fd); |
| 1441 | if (rc <= 0) |
| 1442 | return NULL; |
| 1443 | return talloc_asprintf(ctx, "(%s)", str); |
| 1444 | } |
| 1445 | |
| 1446 | /*! Get address/port information on socket in provided string buffer, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1447 | * This does not include braces like osmo_sock_get_name(). |
| 1448 | * \param[out] str Destination string buffer. |
| 1449 | * \param[in] str_len sizeof(str). |
| 1450 | * \param[in] fd File descriptor of socket. |
| 1451 | * \return String length as returned by snprintf(), or negative on error. |
| 1452 | */ |
| 1453 | int osmo_sock_get_name_buf(char *str, size_t str_len, int fd) |
| 1454 | { |
Oliver Smith | 860651e | 2018-10-30 14:31:57 +0100 | [diff] [blame] | 1455 | char hostbuf_l[INET6_ADDRSTRLEN], hostbuf_r[INET6_ADDRSTRLEN]; |
| 1456 | char portbuf_l[6], portbuf_r[6]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1457 | int rc; |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1458 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1459 | /* get local */ |
Harald Welte | ea9ea52 | 2019-05-10 09:28:24 +0200 | [diff] [blame] | 1460 | if ((rc = osmo_sock_get_ip_and_port(fd, hostbuf_l, sizeof(hostbuf_l), portbuf_l, sizeof(portbuf_l), true))) { |
| 1461 | osmo_strlcpy(str, "<error-in-getsockname>", str_len); |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1462 | return rc; |
Harald Welte | ea9ea52 | 2019-05-10 09:28:24 +0200 | [diff] [blame] | 1463 | } |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1464 | |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 1465 | /* get remote */ |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1466 | if (osmo_sock_get_ip_and_port(fd, hostbuf_r, sizeof(hostbuf_r), portbuf_r, sizeof(portbuf_r), false) != 0) |
| 1467 | 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] | 1468 | |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1469 | return snprintf(str, str_len, "r=%s:%s<->l=%s:%s", hostbuf_r, portbuf_r, hostbuf_l, portbuf_l); |
| 1470 | } |
| 1471 | |
| 1472 | /*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1473 | * This does not include braces like osmo_sock_get_name(). |
| 1474 | * \param[in] fd File descriptor of socket. |
| 1475 | * \return Static string buffer containing the result. |
| 1476 | */ |
| 1477 | const char *osmo_sock_get_name2(int fd) |
| 1478 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 1479 | static __thread char str[OSMO_SOCK_NAME_MAXLEN]; |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 1480 | osmo_sock_get_name_buf(str, sizeof(str), fd); |
| 1481 | return str; |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 1482 | } |
| 1483 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 1484 | /*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". |
| 1485 | * This does not include braces like osmo_sock_get_name(). |
| 1486 | * \param[in] fd File descriptor of socket. |
| 1487 | * \return Static string buffer containing the result. |
| 1488 | */ |
| 1489 | char *osmo_sock_get_name2_c(const void *ctx, int fd) |
| 1490 | { |
| 1491 | char *str = talloc_size(ctx, OSMO_SOCK_NAME_MAXLEN); |
| 1492 | if (!str) |
| 1493 | return NULL; |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 1494 | osmo_sock_get_name_buf(str, OSMO_SOCK_NAME_MAXLEN, fd); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 1495 | return str; |
| 1496 | } |
| 1497 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 1498 | static int sock_get_domain(int fd) |
| 1499 | { |
| 1500 | int domain; |
| 1501 | #ifdef SO_DOMAIN |
| 1502 | socklen_t dom_len = sizeof(domain); |
| 1503 | int rc; |
| 1504 | |
| 1505 | rc = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &dom_len); |
| 1506 | if (rc < 0) |
| 1507 | return rc; |
| 1508 | #else |
| 1509 | /* This of course sucks, but what shall we do on OSs like |
| 1510 | * FreeBSD that don't seem to expose a method by which one can |
| 1511 | * learn the address family of a socket? */ |
| 1512 | domain = AF_INET; |
| 1513 | #endif |
| 1514 | return domain; |
| 1515 | } |
| 1516 | |
| 1517 | |
| 1518 | /*! Activate or de-activate local loop-back of transmitted multicast packets |
| 1519 | * \param[in] fd file descriptor of related socket |
| 1520 | * \param[in] enable Enable (true) or disable (false) loop-back |
| 1521 | * \returns 0 on success; negative otherwise */ |
| 1522 | int osmo_sock_mcast_loop_set(int fd, bool enable) |
| 1523 | { |
| 1524 | int domain, loop = 0; |
| 1525 | |
| 1526 | if (enable) |
| 1527 | loop = 1; |
| 1528 | |
| 1529 | domain = sock_get_domain(fd); |
| 1530 | if (domain < 0) |
| 1531 | return domain; |
| 1532 | |
| 1533 | switch (domain) { |
| 1534 | case AF_INET: |
| 1535 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 1536 | case AF_INET6: |
| 1537 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 1538 | default: |
| 1539 | return -EINVAL; |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | /*! Set the TTL of outbound multicast packets |
| 1544 | * \param[in] fd file descriptor of related socket |
| 1545 | * \param[in] ttl TTL of to-be-sent multicast packets |
| 1546 | * \returns 0 on success; negative otherwise */ |
| 1547 | int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl) |
| 1548 | { |
| 1549 | int domain, ttli = ttl; |
| 1550 | |
| 1551 | domain = sock_get_domain(fd); |
| 1552 | if (domain < 0) |
| 1553 | return domain; |
| 1554 | |
| 1555 | switch (domain) { |
| 1556 | case AF_INET: |
| 1557 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttli, sizeof(ttli)); |
| 1558 | case AF_INET6: |
| 1559 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttli, sizeof(ttli)); |
| 1560 | default: |
| 1561 | return -EINVAL; |
| 1562 | } |
| 1563 | } |
| 1564 | |
Harald Welte | 44b9926 | 2020-03-07 14:59:05 +0100 | [diff] [blame] | 1565 | /*! Set the network device to which we should bind the multicast socket |
| 1566 | * \param[in] fd file descriptor of related socket |
| 1567 | * \param[in] ifname name of network interface to user for multicast |
| 1568 | * \returns 0 on success; negative otherwise */ |
| 1569 | int osmo_sock_mcast_iface_set(int fd, const char *ifname) |
| 1570 | { |
| 1571 | unsigned int ifindex; |
| 1572 | struct ip_mreqn mr; |
| 1573 | |
| 1574 | /* first, resolve interface name to ifindex */ |
| 1575 | ifindex = if_nametoindex(ifname); |
| 1576 | if (ifindex == 0) |
| 1577 | return -errno; |
| 1578 | |
| 1579 | /* next, configure kernel to use that ifindex for this sockets multicast traffic */ |
| 1580 | memset(&mr, 0, sizeof(mr)); |
| 1581 | mr.imr_ifindex = ifindex; |
| 1582 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mr, sizeof(mr)); |
| 1583 | } |
| 1584 | |
| 1585 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 1586 | /*! Enable/disable receiving all multicast packets, even for non-subscribed groups |
| 1587 | * \param[in] fd file descriptor of related socket |
| 1588 | * \param[in] enable Enable or Disable receiving of all packets |
| 1589 | * \returns 0 on success; negative otherwise */ |
| 1590 | int osmo_sock_mcast_all_set(int fd, bool enable) |
| 1591 | { |
| 1592 | int domain, all = 0; |
| 1593 | |
| 1594 | if (enable) |
| 1595 | all = 1; |
| 1596 | |
| 1597 | domain = sock_get_domain(fd); |
| 1598 | if (domain < 0) |
| 1599 | return domain; |
| 1600 | |
| 1601 | switch (domain) { |
| 1602 | case AF_INET: |
| 1603 | #ifdef IP_MULTICAST_ALL |
| 1604 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)); |
| 1605 | #endif |
| 1606 | case AF_INET6: |
| 1607 | /* there seems no equivalent ?!? */ |
| 1608 | default: |
| 1609 | return -EINVAL; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | /* FreeBSD calls the socket option differently */ |
| 1614 | #if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP) |
| 1615 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP |
| 1616 | #endif |
| 1617 | |
| 1618 | /*! Subscribe to the given IP multicast group |
| 1619 | * \param[in] fd file descriptor of related scoket |
| 1620 | * \param[in] grp_addr ASCII representation of the multicast group address |
| 1621 | * \returns 0 on success; negative otherwise */ |
| 1622 | int osmo_sock_mcast_subscribe(int fd, const char *grp_addr) |
| 1623 | { |
| 1624 | int rc, domain; |
| 1625 | struct ip_mreq mreq; |
| 1626 | struct ipv6_mreq mreq6; |
| 1627 | struct in6_addr i6a; |
| 1628 | |
| 1629 | domain = sock_get_domain(fd); |
| 1630 | if (domain < 0) |
| 1631 | return domain; |
| 1632 | |
| 1633 | switch (domain) { |
| 1634 | case AF_INET: |
| 1635 | memset(&mreq, 0, sizeof(mreq)); |
| 1636 | mreq.imr_multiaddr.s_addr = inet_addr(grp_addr); |
| 1637 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
| 1638 | return setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 1639 | #ifdef IPV6_ADD_MEMBERSHIP |
| 1640 | case AF_INET6: |
| 1641 | memset(&mreq6, 0, sizeof(mreq6)); |
| 1642 | rc = inet_pton(AF_INET6, grp_addr, (void *)&i6a); |
| 1643 | if (rc < 0) |
| 1644 | return -EINVAL; |
| 1645 | mreq6.ipv6mr_multiaddr = i6a; |
| 1646 | return setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)); |
| 1647 | #endif |
| 1648 | default: |
| 1649 | return -EINVAL; |
| 1650 | } |
| 1651 | } |
| 1652 | |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1653 | /*! Determine the matching local IP-address for a given remote IP-Address. |
| 1654 | * \param[out] local_ip caller provided memory for resulting local IP-address |
| 1655 | * \param[in] remote_ip remote IP-address |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1656 | * \returns 0 on success; negative otherwise |
| 1657 | * |
| 1658 | * The function accepts IPv4 and IPv6 address strings. The caller must provide |
| 1659 | * at least INET6_ADDRSTRLEN bytes for local_ip if an IPv6 is expected as |
| 1660 | * as result. For IPv4 addresses the required amount is INET_ADDRSTRLEN. */ |
| 1661 | int osmo_sock_local_ip(char *local_ip, const char *remote_ip) |
| 1662 | { |
| 1663 | int sfd; |
| 1664 | int rc; |
| 1665 | struct addrinfo addrinfo_hint; |
| 1666 | struct addrinfo *addrinfo = NULL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 1667 | struct sockaddr_storage local_addr; |
| 1668 | struct sockaddr_in *sin; |
| 1669 | struct sockaddr_in6 *sin6; |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1670 | socklen_t local_addr_len; |
| 1671 | uint16_t family; |
| 1672 | |
| 1673 | /* Find out the address family (AF_INET or AF_INET6?) */ |
| 1674 | memset(&addrinfo_hint, '\0', sizeof(addrinfo_hint)); |
Pau Espin Pedrol | 308ab79 | 2020-08-28 19:47:25 +0200 | [diff] [blame] | 1675 | addrinfo_hint.ai_family = AF_UNSPEC; |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1676 | addrinfo_hint.ai_flags = AI_NUMERICHOST; |
| 1677 | rc = getaddrinfo(remote_ip, NULL, &addrinfo_hint, &addrinfo); |
| 1678 | if (rc) |
| 1679 | return -EINVAL; |
| 1680 | family = addrinfo->ai_family; |
| 1681 | freeaddrinfo(addrinfo); |
| 1682 | |
| 1683 | /* Connect a dummy socket to trick the kernel into determining the |
| 1684 | * ip-address of the interface that would be used if we would send |
| 1685 | * out an actual packet */ |
| 1686 | sfd = osmo_sock_init2(family, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, remote_ip, 0, OSMO_SOCK_F_CONNECT); |
| 1687 | if (sfd < 0) |
| 1688 | return -EINVAL; |
| 1689 | |
| 1690 | /* Request the IP address of the interface that the kernel has |
| 1691 | * actually choosen. */ |
| 1692 | memset(&local_addr, 0, sizeof(local_addr)); |
| 1693 | local_addr_len = sizeof(local_addr); |
| 1694 | rc = getsockname(sfd, (struct sockaddr *)&local_addr, &local_addr_len); |
Philipp Maier | 8b7975b | 2018-01-22 15:38:07 +0100 | [diff] [blame] | 1695 | close(sfd); |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1696 | if (rc < 0) |
| 1697 | return -EINVAL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 1698 | |
| 1699 | switch (local_addr.ss_family) { |
| 1700 | case AF_INET: |
| 1701 | sin = (struct sockaddr_in*)&local_addr; |
| 1702 | if (!inet_ntop(AF_INET, &sin->sin_addr, local_ip, INET_ADDRSTRLEN)) |
| 1703 | return -EINVAL; |
| 1704 | break; |
| 1705 | case AF_INET6: |
| 1706 | sin6 = (struct sockaddr_in6*)&local_addr; |
Pau Espin Pedrol | 7bda854 | 2020-08-31 11:21:30 +0200 | [diff] [blame] | 1707 | 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] | 1708 | return -EINVAL; |
| 1709 | break; |
| 1710 | default: |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1711 | return -EINVAL; |
Pau Espin Pedrol | 81e7a6c | 2020-08-28 20:44:26 +0200 | [diff] [blame] | 1712 | } |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 1713 | |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
Alexander Couzens | 0f36421 | 2020-07-27 22:33:01 +0200 | [diff] [blame] | 1717 | /*! Determine the matching local address for a given remote address. |
| 1718 | * \param[out] local_ip caller provided memory for resulting local address |
| 1719 | * \param[in] remote_ip remote address |
| 1720 | * \returns 0 on success; negative otherwise |
| 1721 | */ |
| 1722 | int osmo_sockaddr_local_ip(struct osmo_sockaddr *local_ip, const struct osmo_sockaddr *remote_ip) |
| 1723 | { |
| 1724 | int sfd; |
| 1725 | int rc; |
| 1726 | socklen_t local_ip_len; |
| 1727 | |
| 1728 | sfd = osmo_sock_init_osa(SOCK_DGRAM, IPPROTO_UDP, NULL, remote_ip, OSMO_SOCK_F_CONNECT); |
| 1729 | if (sfd < 0) |
| 1730 | return -EINVAL; |
| 1731 | |
| 1732 | memset(local_ip, 0, sizeof(*local_ip)); |
| 1733 | local_ip_len = sizeof(*local_ip); |
| 1734 | rc = getsockname(sfd, (struct sockaddr *)local_ip, &local_ip_len); |
| 1735 | close(sfd); |
| 1736 | |
| 1737 | return rc; |
| 1738 | } |
| 1739 | |
Alexander Couzens | e4181ea | 2020-07-20 00:03:16 +0200 | [diff] [blame] | 1740 | /*! Compare two osmo_sockaddr. |
| 1741 | * \param[in] a |
| 1742 | * \param[in] b |
| 1743 | * \return 0 if a and b are equal. Otherwise it follows memcmp() |
| 1744 | */ |
Vadim Yanitskiy | def5a40 | 2020-10-09 21:40:47 +0700 | [diff] [blame] | 1745 | int osmo_sockaddr_cmp(const struct osmo_sockaddr *a, |
| 1746 | const struct osmo_sockaddr *b) |
Alexander Couzens | e4181ea | 2020-07-20 00:03:16 +0200 | [diff] [blame] | 1747 | { |
| 1748 | if (a == b) |
| 1749 | return 0; |
| 1750 | if (!a) |
| 1751 | return 1; |
| 1752 | if (!b) |
| 1753 | return -1; |
| 1754 | |
| 1755 | if (a->u.sa.sa_family != b->u.sa.sa_family) { |
| 1756 | return OSMO_CMP(a->u.sa.sa_family, b->u.sa.sa_family); |
| 1757 | } |
| 1758 | |
| 1759 | switch (a->u.sa.sa_family) { |
| 1760 | case AF_INET: |
| 1761 | return memcmp(&a->u.sin, &b->u.sin, sizeof(struct sockaddr_in)); |
| 1762 | case AF_INET6: |
| 1763 | return memcmp(&a->u.sin6, &b->u.sin6, sizeof(struct sockaddr_in6)); |
| 1764 | default: |
| 1765 | /* fallback to memcmp for remaining AF over the full osmo_sockaddr length */ |
| 1766 | return memcmp(a, b, sizeof(struct osmo_sockaddr)); |
| 1767 | } |
| 1768 | } |
| 1769 | |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 1770 | /*! string-format a given osmo_sockaddr address |
| 1771 | * \param[in] sockaddr the osmo_sockaddr to print |
| 1772 | * \return pointer to the string on success; NULL on error |
| 1773 | */ |
| 1774 | const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *sockaddr) |
| 1775 | { |
| 1776 | /* INET6_ADDRSTRLEN contains already a null termination, |
| 1777 | * adding '[' ']' ':' '16 bit port' */ |
| 1778 | static __thread char buf[INET6_ADDRSTRLEN + 8]; |
| 1779 | return osmo_sockaddr_to_str_buf(buf, sizeof(buf), sockaddr); |
| 1780 | } |
| 1781 | |
| 1782 | /*! string-format a given osmo_sockaddr address into a user-supplied buffer |
| 1783 | * \param[in] buf user-supplied output buffer |
| 1784 | * \param[in] buf_len size of the user-supplied output buffer in bytes |
| 1785 | * \param[in] sockaddr the osmo_sockaddr to print |
| 1786 | * \return pointer to the string on success; NULL on error |
| 1787 | */ |
| 1788 | char *osmo_sockaddr_to_str_buf(char *buf, size_t buf_len, |
| 1789 | const struct osmo_sockaddr *sockaddr) |
| 1790 | { |
| 1791 | uint16_t port = 0; |
| 1792 | size_t written; |
| 1793 | if (buf_len < 5) |
| 1794 | return NULL; |
| 1795 | |
| 1796 | if (!sockaddr) |
| 1797 | return NULL; |
| 1798 | |
| 1799 | switch (sockaddr->u.sa.sa_family) { |
| 1800 | case AF_INET: |
| 1801 | written = osmo_sockaddr_to_str_and_uint(buf, buf_len, &port, &sockaddr->u.sa); |
| 1802 | if (written + 1 >= buf_len && port) |
| 1803 | return NULL; |
| 1804 | if (port) |
| 1805 | snprintf(buf + written, buf_len - written, ":%u", port); |
| 1806 | break; |
| 1807 | case AF_INET6: |
| 1808 | buf[0] = '['; |
| 1809 | written = osmo_sockaddr_to_str_and_uint(buf + 1, buf_len - 1, &port, &sockaddr->u.sa); |
| 1810 | if (written + 2 >= buf_len) |
| 1811 | return NULL; |
| 1812 | |
| 1813 | if (written + 3 >= buf_len && port) |
| 1814 | return NULL; |
| 1815 | |
| 1816 | if (port) |
| 1817 | snprintf(buf + 1 + written, buf_len - written - 1, "]:%u", port); |
| 1818 | else { |
| 1819 | buf[written + 1] = ']'; |
| 1820 | buf[written + 2] = 0; |
| 1821 | } |
| 1822 | break; |
| 1823 | default: |
| 1824 | snprintf(buf, buf_len, "unsupported family %d", sockaddr->u.sa.sa_family); |
| 1825 | return buf; |
| 1826 | } |
| 1827 | |
| 1828 | return buf; |
| 1829 | } |
| 1830 | |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1831 | /*! Set the DSCP (differentiated services code point) of a socket. |
| 1832 | * \param[in] dscp DSCP value in range 0..63 |
| 1833 | * \returns 0 on success; negative on error. */ |
| 1834 | int osmo_sock_set_dscp(int fd, uint8_t dscp) |
| 1835 | { |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 1836 | struct sockaddr_storage local_addr; |
| 1837 | socklen_t local_addr_len = sizeof(local_addr); |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1838 | uint8_t tos; |
| 1839 | socklen_t tos_len = sizeof(tos); |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 1840 | int tclass; |
| 1841 | socklen_t tclass_len = sizeof(tclass); |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1842 | int rc; |
| 1843 | |
| 1844 | /* DSCP is a 6-bit value stored in the upper 6 bits of the 8-bit TOS */ |
| 1845 | if (dscp > 63) |
| 1846 | return -EINVAL; |
| 1847 | |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 1848 | rc = getsockname(fd, (struct sockaddr *)&local_addr, &local_addr_len); |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1849 | if (rc < 0) |
| 1850 | return rc; |
| 1851 | |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 1852 | switch (local_addr.ss_family) { |
| 1853 | case AF_INET: |
| 1854 | /* read the original value */ |
| 1855 | rc = getsockopt(fd, IPPROTO_IP, IP_TOS, &tos, &tos_len); |
| 1856 | if (rc < 0) |
| 1857 | return rc; |
| 1858 | /* mask-in the DSCP into the upper 6 bits */ |
| 1859 | tos &= 0x03; |
| 1860 | tos |= dscp << 2; |
| 1861 | /* and write it back to the kernel */ |
| 1862 | rc = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1863 | break; |
| 1864 | case AF_INET6: |
| 1865 | /* read the original value */ |
| 1866 | rc = getsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tclass, &tclass_len); |
| 1867 | if (rc < 0) |
| 1868 | return rc; |
| 1869 | /* mask-in the DSCP into the upper 6 bits */ |
| 1870 | tclass &= 0x03; |
| 1871 | tclass |= dscp << 2; |
| 1872 | /* and write it back to the kernel */ |
| 1873 | rc = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof(tclass)); |
| 1874 | break; |
| 1875 | case AF_UNSPEC: |
| 1876 | default: |
| 1877 | LOGP(DLGLOBAL, LOGL_ERROR, "No DSCP support for socket family %u\n", |
| 1878 | local_addr.ss_family); |
| 1879 | rc = -1; |
| 1880 | break; |
| 1881 | } |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1882 | |
Harald Welte | 903e670 | 2021-04-28 13:27:12 +0200 | [diff] [blame] | 1883 | return rc; |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 1884 | } |
| 1885 | |
Harald Welte | ecc0bd8 | 2021-04-27 22:24:08 +0200 | [diff] [blame] | 1886 | /*! Set the priority value of a socket. |
| 1887 | * \param[in] prio priority value. Values outside 0..6 require CAP_NET_ADMIN. |
| 1888 | * \returns 0 on success; negative on error. */ |
| 1889 | int osmo_sock_set_priority(int fd, int prio) |
| 1890 | { |
| 1891 | /* and write it back to the kernel */ |
| 1892 | return setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)); |
| 1893 | } |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 1894 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 1895 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 1896 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 1897 | /*! @} */ |