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