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> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 38 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 39 | #include <sys/ioctl.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 40 | #include <sys/socket.h> |
| 41 | #include <sys/types.h> |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 42 | #include <sys/un.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 43 | |
Holger Hans Peter Freyther | 4772348 | 2011-11-09 11:26:15 +0100 | [diff] [blame] | 44 | #include <netinet/in.h> |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 45 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 4772348 | 2011-11-09 11:26:15 +0100 | [diff] [blame] | 46 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 47 | #include <stdio.h> |
| 48 | #include <unistd.h> |
| 49 | #include <stdint.h> |
| 50 | #include <string.h> |
| 51 | #include <errno.h> |
| 52 | #include <netdb.h> |
| 53 | #include <ifaddrs.h> |
| 54 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 55 | static struct addrinfo *addrinfo_helper(uint16_t family, uint16_t type, uint8_t proto, |
| 56 | const char *host, uint16_t port, bool passive) |
| 57 | { |
| 58 | struct addrinfo hints, *result; |
| 59 | char portbuf[16]; |
| 60 | int rc; |
| 61 | |
| 62 | snprintf(portbuf, sizeof(portbuf), "%u", port); |
| 63 | memset(&hints, 0, sizeof(struct addrinfo)); |
| 64 | hints.ai_family = family; |
| 65 | if (type == SOCK_RAW) { |
| 66 | /* Workaround for glibc, that returns EAI_SERVICE (-8) if |
| 67 | * SOCK_RAW and IPPROTO_GRE is used. |
| 68 | */ |
| 69 | hints.ai_socktype = SOCK_DGRAM; |
| 70 | hints.ai_protocol = IPPROTO_UDP; |
| 71 | } else { |
| 72 | hints.ai_socktype = type; |
| 73 | hints.ai_protocol = proto; |
| 74 | } |
| 75 | |
| 76 | if (passive) |
| 77 | hints.ai_flags |= AI_PASSIVE; |
| 78 | |
| 79 | rc = getaddrinfo(host, portbuf, &hints, &result); |
| 80 | if (rc != 0) { |
| 81 | LOGP(DLGLOBAL, LOGL_ERROR, "getaddrinfo returned NULL: %s:%u: %s\n", |
| 82 | host, port, strerror(errno)); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | return result; |
| 87 | } |
| 88 | |
| 89 | static int socket_helper(const struct addrinfo *rp, unsigned int flags) |
| 90 | { |
| 91 | int sfd, on = 1; |
| 92 | |
| 93 | sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 94 | if (sfd == -1) { |
| 95 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 96 | "unable to create socket: %s\n", strerror(errno)); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 97 | return sfd; |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 98 | } |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 99 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 100 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
| 101 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 102 | "cannot set this socket unblocking: %s\n", |
| 103 | strerror(errno)); |
| 104 | close(sfd); |
| 105 | sfd = -EINVAL; |
| 106 | } |
| 107 | } |
| 108 | return sfd; |
| 109 | } |
| 110 | |
| 111 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 112 | static int osmo_sock_init_tail(int fd, uint16_t type, unsigned int flags) |
| 113 | { |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 114 | int rc; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 115 | |
| 116 | /* Make sure to call 'listen' on a bound, connection-oriented sock */ |
| 117 | if ((flags & (OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT)) == OSMO_SOCK_F_BIND) { |
| 118 | switch (type) { |
| 119 | case SOCK_STREAM: |
| 120 | case SOCK_SEQPACKET: |
| 121 | rc = listen(fd, 10); |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 122 | if (rc < 0) { |
| 123 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to listen on socket: %s\n", |
| 124 | strerror(errno)); |
| 125 | return rc; |
| 126 | } |
| 127 | break; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 131 | if (flags & OSMO_SOCK_F_NO_MCAST_LOOP) { |
| 132 | rc = osmo_sock_mcast_loop_set(fd, false); |
| 133 | if (rc < 0) { |
| 134 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to disable multicast loop: %s\n", |
| 135 | strerror(errno)); |
| 136 | return rc; |
| 137 | } |
| 138 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 139 | |
Harald Welte | 37d204a | 2017-07-13 16:33:16 +0200 | [diff] [blame] | 140 | if (flags & OSMO_SOCK_F_NO_MCAST_ALL) { |
| 141 | rc = osmo_sock_mcast_all_set(fd, false); |
| 142 | if (rc < 0) { |
| 143 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to disable receive of all multicast: %s\n", |
| 144 | strerror(errno)); |
| 145 | /* do not abort here, as this is just an |
| 146 | * optional additional optimization that only |
| 147 | * exists on Linux only */ |
| 148 | } |
| 149 | } |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 150 | return 0; |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 153 | /*! Initialize a socket (including bind and/or connect) |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 154 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 155 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 156 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 157 | * \param[in] local_host local host name or IP address in string form |
| 158 | * \param[in] local_port local port number in host byte order |
| 159 | * \param[in] remote_host remote host name or IP address in string form |
| 160 | * \param[in] remote_port remote port number in host byte order |
| 161 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 162 | * \returns socket file descriptor on success; negative on error |
| 163 | * |
| 164 | * This function creates a new socket of the designated \a family, \a |
| 165 | * type and \a proto and optionally binds it to the \a local_host and \a |
| 166 | * local_port as well as optionally connects it to the \a remote_host |
| 167 | * and \q remote_port, depending on the value * of \a flags parameter. |
| 168 | * |
| 169 | * As opposed to \ref osmo_sock_init(), this function allows to combine |
| 170 | * the \ref OSMO_SOCK_F_BIND and \ref OSMO_SOCK_F_CONNECT flags. This |
| 171 | * is useful if you want to connect to a remote host/port, but still |
| 172 | * want to bind that socket to either a specific local alias IP and/or a |
| 173 | * specific local source port. |
| 174 | * |
| 175 | * You must specify either \ref OSMO_SOCK_F_BIND, or \ref |
| 176 | * OSMO_SOCK_F_CONNECT, or both. |
| 177 | * |
| 178 | * If \ref OSMO_SOCK_F_NONBLOCK is specified, the socket will be set to |
| 179 | * non-blocking mode. |
| 180 | */ |
| 181 | int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto, |
| 182 | const char *local_host, uint16_t local_port, |
| 183 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 184 | { |
| 185 | struct addrinfo *result, *rp; |
| 186 | int sfd = -1, rc, on = 1; |
| 187 | |
| 188 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 189 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 190 | "BIND or CONNECT flags\n"); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | /* figure out local side of socket */ |
| 195 | if (flags & OSMO_SOCK_F_BIND) { |
| 196 | result = addrinfo_helper(family, type, proto, local_host, local_port, true); |
| 197 | if (!result) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
| 201 | /* Workaround for glibc again */ |
| 202 | if (type == SOCK_RAW) { |
| 203 | rp->ai_socktype = SOCK_RAW; |
| 204 | rp->ai_protocol = proto; |
| 205 | } |
| 206 | |
| 207 | sfd = socket_helper(rp, flags); |
| 208 | if (sfd < 0) |
| 209 | continue; |
| 210 | |
| 211 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 212 | &on, sizeof(on)); |
| 213 | if (rc < 0) { |
| 214 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 215 | "cannot setsockopt socket:" |
| 216 | " %s:%u: %s\n", |
| 217 | local_host, local_port, strerror(errno)); |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 218 | close(sfd); |
| 219 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 220 | } |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 221 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 222 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n", |
| 223 | local_host, local_port, strerror(errno)); |
| 224 | close(sfd); |
| 225 | continue; |
| 226 | } |
| 227 | break; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 228 | } |
| 229 | freeaddrinfo(result); |
| 230 | if (rp == NULL) { |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 231 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: %s:%u\n", |
| 232 | local_host, local_port); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 233 | return -ENODEV; |
| 234 | } |
| 235 | } |
| 236 | |
Pau Espin Pedrol | 5d50fa5 | 2018-04-05 17:00:22 +0200 | [diff] [blame] | 237 | /* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it |
| 238 | was already closed and func returned. If OSMO_SOCK_F_BIND is not |
| 239 | set, then sfd = -1 */ |
| 240 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 241 | /* figure out remote side of socket */ |
| 242 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 243 | result = addrinfo_helper(family, type, proto, remote_host, remote_port, false); |
| 244 | if (!result) { |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 245 | if (sfd >= 0) |
| 246 | close(sfd); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 247 | return -EINVAL; |
| 248 | } |
| 249 | |
| 250 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
| 251 | /* Workaround for glibc again */ |
| 252 | if (type == SOCK_RAW) { |
| 253 | rp->ai_socktype = SOCK_RAW; |
| 254 | rp->ai_protocol = proto; |
| 255 | } |
| 256 | |
Harald Welte | 5cfa6dc | 2017-07-21 16:52:29 +0200 | [diff] [blame] | 257 | if (sfd < 0) { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 258 | sfd = socket_helper(rp, flags); |
| 259 | if (sfd < 0) |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 264 | if (rc != 0 && errno != EINPROGRESS) { |
| 265 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 266 | remote_host, remote_port, strerror(errno)); |
| 267 | /* We want to maintain the bind socket if bind was enabled */ |
| 268 | if (!(flags & OSMO_SOCK_F_BIND)) { |
| 269 | close(sfd); |
| 270 | sfd = -1; |
| 271 | } |
| 272 | continue; |
| 273 | } |
| 274 | break; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 275 | } |
| 276 | freeaddrinfo(result); |
| 277 | if (rp == NULL) { |
Pau Espin Pedrol | 27cf8df | 2018-04-05 17:49:08 +0200 | [diff] [blame] | 278 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable remote addr found for: %s:%u\n", |
| 279 | remote_host, remote_port); |
| 280 | if (sfd >= 0) |
| 281 | close(sfd); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 282 | return -ENODEV; |
| 283 | } |
| 284 | } |
| 285 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 286 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 287 | if (rc < 0) { |
| 288 | close(sfd); |
| 289 | sfd = -1; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 290 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 291 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 292 | return sfd; |
| 293 | } |
| 294 | |
| 295 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 296 | /*! Initialize a socket (including bind/connect) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 297 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 298 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 299 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 300 | * \param[in] host remote host name or IP address in string form |
| 301 | * \param[in] port remote port number in host byte order |
| 302 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 303 | * \returns socket file descriptor on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 304 | * |
| 305 | * This function creates a new socket of the designated \a family, \a |
| 306 | * type and \a proto and optionally binds or connects it, depending on |
| 307 | * the value of \a flags parameter. |
| 308 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 309 | 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] | 310 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 311 | { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 312 | struct addrinfo *result, *rp; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 313 | int sfd, rc, on = 1; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 314 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 315 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 316 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 317 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: both bind and connect flags set:" |
Neels Hofmeyr | b7f191f | 2016-08-29 11:22:03 +0200 | [diff] [blame] | 318 | " %s:%u\n", host, port); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 319 | return -EINVAL; |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 320 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 321 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 322 | result = addrinfo_helper(family, type, proto, host, port, flags & OSMO_SOCK_F_BIND); |
| 323 | if (!result) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 324 | LOGP(DLGLOBAL, LOGL_ERROR, "getaddrinfo returned NULL: %s:%u: %s\n", |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 325 | host, port, strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 326 | return -EINVAL; |
| 327 | } |
| 328 | |
| 329 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
Pablo Neira Ayuso | e04a14d | 2013-01-14 00:12:28 +0100 | [diff] [blame] | 330 | /* Workaround for glibc again */ |
| 331 | if (type == SOCK_RAW) { |
| 332 | rp->ai_socktype = SOCK_RAW; |
| 333 | rp->ai_protocol = proto; |
| 334 | } |
| 335 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 336 | sfd = socket_helper(rp, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 337 | if (sfd == -1) |
| 338 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 339 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 340 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 341 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 342 | if (rc != 0 && errno != EINPROGRESS) { |
| 343 | close(sfd); |
| 344 | continue; |
| 345 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 346 | } else { |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 347 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 348 | &on, sizeof(on)); |
| 349 | if (rc < 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 350 | LOGP(DLGLOBAL, LOGL_ERROR, |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 351 | "cannot setsockopt socket:" |
| 352 | " %s:%u: %s\n", |
| 353 | host, port, strerror(errno)); |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 354 | close(sfd); |
| 355 | continue; |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 356 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 357 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == -1) { |
| 358 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket:" |
| 359 | "%s:%u: %s\n", |
| 360 | host, port, strerror(errno)); |
| 361 | close(sfd); |
| 362 | continue; |
| 363 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 364 | } |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 365 | break; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 366 | } |
| 367 | freeaddrinfo(result); |
| 368 | |
| 369 | if (rp == NULL) { |
Pau Espin Pedrol | 3a32147 | 2018-04-05 17:49:40 +0200 | [diff] [blame] | 370 | LOGP(DLGLOBAL, LOGL_ERROR, "no suitable addr found for: %s:%u\n", |
| 371 | host, port); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 372 | return -ENODEV; |
| 373 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 374 | |
| 375 | setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 376 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 377 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 378 | if (rc < 0) { |
| 379 | close(sfd); |
| 380 | sfd = -1; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 381 | } |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 382 | |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 383 | return sfd; |
| 384 | } |
| 385 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 386 | /*! fill \ref osmo_fd for a give sfd |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 387 | * \param[out] ofd file descriptor (will be filled in) |
| 388 | * \param[in] sfd socket file descriptor |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 389 | * \returns socket fd on success; negative on error |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 390 | * |
| 391 | * This function fills the \a ofd structure. |
| 392 | */ |
| 393 | static inline int osmo_fd_init_ofd(struct osmo_fd *ofd, int sfd) |
| 394 | { |
| 395 | int rc; |
| 396 | |
| 397 | if (sfd < 0) |
| 398 | return sfd; |
| 399 | |
| 400 | ofd->fd = sfd; |
| 401 | ofd->when = BSC_FD_READ; |
| 402 | |
| 403 | rc = osmo_fd_register(ofd); |
| 404 | if (rc < 0) { |
| 405 | close(sfd); |
| 406 | return rc; |
| 407 | } |
| 408 | |
| 409 | return sfd; |
| 410 | } |
| 411 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 412 | /*! Initialize a socket and fill \ref osmo_fd |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 413 | * \param[out] ofd file descriptor (will be filled in) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 414 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 415 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 416 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 417 | * \param[in] host remote host name or IP address in string form |
| 418 | * \param[in] port remote port number in host byte order |
| 419 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 420 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 421 | * |
| 422 | * This function creates (and optionall binds/connects) a socket using |
| 423 | * \ref osmo_sock_init, but also fills the \a ofd structure. |
| 424 | */ |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 425 | 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] | 426 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 427 | { |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 428 | 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] | 429 | } |
| 430 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 431 | /*! Initialize a socket and fill \ref osmo_fd |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 432 | * \param[out] ofd file descriptor (will be filled in) |
| 433 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 434 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 435 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 436 | * \param[in] local_host local host name or IP address in string form |
| 437 | * \param[in] local_port local port number in host byte order |
| 438 | * \param[in] remote_host remote host name or IP address in string form |
| 439 | * \param[in] remote_port remote port number in host byte order |
| 440 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 441 | * \returns socket fd on success; negative on error |
| 442 | * |
| 443 | * This function creates (and optionall binds/connects) a socket using |
| 444 | * \ref osmo_sock_init2, but also fills the \a ofd structure. |
| 445 | */ |
| 446 | int osmo_sock_init2_ofd(struct osmo_fd *ofd, int family, int type, int proto, |
| 447 | const char *local_host, uint16_t local_port, |
| 448 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 449 | { |
| 450 | return osmo_fd_init_ofd(ofd, osmo_sock_init2(family, type, proto, local_host, |
| 451 | local_port, remote_host, remote_port, flags)); |
| 452 | } |
| 453 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 454 | /*! Initialize a socket and fill \ref sockaddr |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 455 | * \param[out] ss socket address (will be filled in) |
| 456 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 457 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 458 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 459 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 460 | * |
| 461 | * This function creates (and optionall binds/connects) a socket using |
| 462 | * \ref osmo_sock_init, but also fills the \a ss structure. |
| 463 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 464 | int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 465 | uint8_t proto, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 466 | { |
| 467 | char host[NI_MAXHOST]; |
| 468 | uint16_t port; |
| 469 | struct sockaddr_in *sin; |
| 470 | struct sockaddr_in6 *sin6; |
| 471 | int s, sa_len; |
| 472 | |
| 473 | /* determine port and host from ss */ |
| 474 | switch (ss->sa_family) { |
| 475 | case AF_INET: |
| 476 | sin = (struct sockaddr_in *) ss; |
| 477 | sa_len = sizeof(struct sockaddr_in); |
| 478 | port = ntohs(sin->sin_port); |
| 479 | break; |
| 480 | case AF_INET6: |
| 481 | sin6 = (struct sockaddr_in6 *) ss; |
| 482 | sa_len = sizeof(struct sockaddr_in6); |
| 483 | port = ntohs(sin6->sin6_port); |
| 484 | break; |
| 485 | default: |
| 486 | return -EINVAL; |
| 487 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 488 | |
| 489 | s = getnameinfo(ss, sa_len, host, NI_MAXHOST, |
| 490 | NULL, 0, NI_NUMERICHOST); |
| 491 | if (s != 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 492 | LOGP(DLGLOBAL, LOGL_ERROR, "getnameinfo failed:" |
| 493 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 494 | return s; |
| 495 | } |
| 496 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 497 | return osmo_sock_init(ss->sa_family, type, proto, host, port, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static int sockaddr_equal(const struct sockaddr *a, |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 501 | const struct sockaddr *b, unsigned int len) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 502 | { |
| 503 | struct sockaddr_in *sin_a, *sin_b; |
| 504 | struct sockaddr_in6 *sin6_a, *sin6_b; |
| 505 | |
| 506 | if (a->sa_family != b->sa_family) |
| 507 | return 0; |
| 508 | |
| 509 | switch (a->sa_family) { |
| 510 | case AF_INET: |
| 511 | sin_a = (struct sockaddr_in *)a; |
| 512 | sin_b = (struct sockaddr_in *)b; |
| 513 | if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr, |
| 514 | sizeof(struct in_addr))) |
| 515 | return 1; |
| 516 | break; |
| 517 | case AF_INET6: |
| 518 | sin6_a = (struct sockaddr_in6 *)a; |
| 519 | sin6_b = (struct sockaddr_in6 *)b; |
| 520 | if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr, |
| 521 | sizeof(struct in6_addr))) |
| 522 | return 1; |
| 523 | break; |
| 524 | } |
| 525 | return 0; |
| 526 | } |
| 527 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 528 | /*! Determine if the given address is a local address |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 529 | * \param[in] addr Socket Address |
| 530 | * \param[in] addrlen Length of socket address in bytes |
| 531 | * \returns 1 if address is local, 0 otherwise. |
| 532 | */ |
Harald Welte | bc32d05 | 2012-04-08 11:31:32 +0200 | [diff] [blame] | 533 | int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 534 | { |
| 535 | struct ifaddrs *ifaddr, *ifa; |
| 536 | |
| 537 | if (getifaddrs(&ifaddr) == -1) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 538 | LOGP(DLGLOBAL, LOGL_ERROR, "getifaddrs:" |
| 539 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 540 | return -EIO; |
| 541 | } |
| 542 | |
| 543 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
Harald Welte | 4d3a7b1 | 2011-05-24 21:31:53 +0200 | [diff] [blame] | 544 | if (!ifa->ifa_addr) |
| 545 | continue; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 546 | if (sockaddr_equal(ifa->ifa_addr, addr, addrlen)) { |
| 547 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 548 | return 1; |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 549 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Pau Espin Pedrol | 15753e9 | 2018-04-18 19:57:41 +0200 | [diff] [blame] | 552 | freeifaddrs(ifaddr); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 553 | return 0; |
| 554 | } |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 555 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 556 | /*! Initialize a unix domain socket (including bind/connect) |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 557 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 558 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 559 | * \param[in] socket_path path to identify the socket |
| 560 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 561 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 562 | * |
| 563 | * This function creates a new unix domain socket, \a |
| 564 | * type and \a proto and optionally binds or connects it, depending on |
| 565 | * the value of \a flags parameter. |
| 566 | */ |
| 567 | int osmo_sock_unix_init(uint16_t type, uint8_t proto, |
| 568 | const char *socket_path, unsigned int flags) |
| 569 | { |
| 570 | struct sockaddr_un local; |
| 571 | int sfd, rc, on = 1; |
| 572 | unsigned int namelen; |
| 573 | |
| 574 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
| 575 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | local.sun_family = AF_UNIX; |
| 579 | strncpy(local.sun_path, socket_path, sizeof(local.sun_path)); |
| 580 | local.sun_path[sizeof(local.sun_path) - 1] = '\0'; |
| 581 | |
| 582 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
| 583 | local.sun_len = strlen(local.sun_path); |
| 584 | #endif |
| 585 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 586 | namelen = SUN_LEN(&local); |
| 587 | #else |
| 588 | namelen = strlen(local.sun_path) + |
| 589 | offsetof(struct sockaddr_un, sun_path); |
| 590 | #endif |
| 591 | |
| 592 | sfd = socket(AF_UNIX, type, proto); |
| 593 | if (sfd < 0) |
| 594 | return -1; |
| 595 | |
| 596 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 597 | rc = connect(sfd, (struct sockaddr *)&local, namelen); |
| 598 | if (rc < 0) |
| 599 | goto err; |
| 600 | } else { |
| 601 | unlink(local.sun_path); |
| 602 | rc = bind(sfd, (struct sockaddr *)&local, namelen); |
| 603 | if (rc < 0) |
| 604 | goto err; |
| 605 | } |
| 606 | |
| 607 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 608 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 609 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 610 | "cannot set this socket unblocking: %s\n", |
| 611 | strerror(errno)); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 612 | close(sfd); |
| 613 | return -EINVAL; |
| 614 | } |
| 615 | } |
| 616 | |
Harald Welte | c47bbda | 2017-07-13 16:13:26 +0200 | [diff] [blame] | 617 | rc = osmo_sock_init_tail(sfd, type, flags); |
| 618 | if (rc < 0) { |
| 619 | close(sfd); |
| 620 | sfd = -1; |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | return sfd; |
| 624 | err: |
| 625 | close(sfd); |
| 626 | return -1; |
| 627 | } |
| 628 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 629 | /*! Initialize a unix domain socket and fill \ref osmo_fd |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 630 | * \param[out] ofd file descriptor (will be filled in) |
| 631 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 632 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 633 | * \param[in] socket_path path to identify the socket |
| 634 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 635 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 636 | * |
| 637 | * This function creates (and optionall binds/connects) a socket using |
| 638 | * \ref osmo_sock_unix_init, but also fills the \a ofd structure. |
| 639 | */ |
| 640 | int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto, |
| 641 | const char *socket_path, unsigned int flags) |
| 642 | { |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 643 | 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] | 644 | } |
| 645 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 646 | /*! Get address/port information on soocket in dyn-alloc string |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 647 | * \param[in] ctx talloc context from which to allocate string buffer |
| 648 | * \param[in] fd file descriptor of socket |
| 649 | * \returns string identifying the connection of this socket |
| 650 | */ |
| 651 | char *osmo_sock_get_name(void *ctx, int fd) |
| 652 | { |
| 653 | struct sockaddr sa_l, sa_r; |
| 654 | socklen_t sa_len_l = sizeof(sa_l); |
| 655 | socklen_t sa_len_r = sizeof(sa_r); |
| 656 | char hostbuf_l[64], hostbuf_r[64]; |
| 657 | char portbuf_l[16], portbuf_r[16]; |
| 658 | int rc; |
| 659 | |
| 660 | rc = getsockname(fd, &sa_l, &sa_len_l); |
| 661 | if (rc < 0) |
| 662 | return NULL; |
| 663 | |
| 664 | rc = getnameinfo(&sa_l, sa_len_l, hostbuf_l, sizeof(hostbuf_l), |
| 665 | portbuf_l, sizeof(portbuf_l), |
| 666 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 667 | if (rc < 0) |
| 668 | return NULL; |
| 669 | |
| 670 | rc = getpeername(fd, &sa_r, &sa_len_r); |
| 671 | if (rc < 0) |
| 672 | goto local_only; |
| 673 | |
| 674 | rc = getnameinfo(&sa_r, sa_len_r, hostbuf_r, sizeof(hostbuf_r), |
| 675 | portbuf_r, sizeof(portbuf_r), |
| 676 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 677 | if (rc < 0) |
| 678 | goto local_only; |
| 679 | |
Neels Hofmeyr | 1f82d0a | 2017-06-21 22:54:46 +0200 | [diff] [blame] | 680 | return talloc_asprintf(ctx, "(r=%s:%s<->l=%s:%s)", hostbuf_r, portbuf_r, |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 681 | hostbuf_l, portbuf_l); |
| 682 | |
| 683 | local_only: |
Neels Hofmeyr | 1f82d0a | 2017-06-21 22:54:46 +0200 | [diff] [blame] | 684 | return talloc_asprintf(ctx, "(r=NULL<->l=%s:%s)", hostbuf_l, portbuf_l); |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 687 | static int sock_get_domain(int fd) |
| 688 | { |
| 689 | int domain; |
| 690 | #ifdef SO_DOMAIN |
| 691 | socklen_t dom_len = sizeof(domain); |
| 692 | int rc; |
| 693 | |
| 694 | rc = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &dom_len); |
| 695 | if (rc < 0) |
| 696 | return rc; |
| 697 | #else |
| 698 | /* This of course sucks, but what shall we do on OSs like |
| 699 | * FreeBSD that don't seem to expose a method by which one can |
| 700 | * learn the address family of a socket? */ |
| 701 | domain = AF_INET; |
| 702 | #endif |
| 703 | return domain; |
| 704 | } |
| 705 | |
| 706 | |
| 707 | /*! Activate or de-activate local loop-back of transmitted multicast packets |
| 708 | * \param[in] fd file descriptor of related socket |
| 709 | * \param[in] enable Enable (true) or disable (false) loop-back |
| 710 | * \returns 0 on success; negative otherwise */ |
| 711 | int osmo_sock_mcast_loop_set(int fd, bool enable) |
| 712 | { |
| 713 | int domain, loop = 0; |
| 714 | |
| 715 | if (enable) |
| 716 | loop = 1; |
| 717 | |
| 718 | domain = sock_get_domain(fd); |
| 719 | if (domain < 0) |
| 720 | return domain; |
| 721 | |
| 722 | switch (domain) { |
| 723 | case AF_INET: |
| 724 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 725 | case AF_INET6: |
| 726 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof(loop)); |
| 727 | default: |
| 728 | return -EINVAL; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /*! Set the TTL of outbound multicast packets |
| 733 | * \param[in] fd file descriptor of related socket |
| 734 | * \param[in] ttl TTL of to-be-sent multicast packets |
| 735 | * \returns 0 on success; negative otherwise */ |
| 736 | int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl) |
| 737 | { |
| 738 | int domain, ttli = ttl; |
| 739 | |
| 740 | domain = sock_get_domain(fd); |
| 741 | if (domain < 0) |
| 742 | return domain; |
| 743 | |
| 744 | switch (domain) { |
| 745 | case AF_INET: |
| 746 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttli, sizeof(ttli)); |
| 747 | case AF_INET6: |
| 748 | return setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttli, sizeof(ttli)); |
| 749 | default: |
| 750 | return -EINVAL; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | /*! Enable/disable receiving all multicast packets, even for non-subscribed groups |
| 755 | * \param[in] fd file descriptor of related socket |
| 756 | * \param[in] enable Enable or Disable receiving of all packets |
| 757 | * \returns 0 on success; negative otherwise */ |
| 758 | int osmo_sock_mcast_all_set(int fd, bool enable) |
| 759 | { |
| 760 | int domain, all = 0; |
| 761 | |
| 762 | if (enable) |
| 763 | all = 1; |
| 764 | |
| 765 | domain = sock_get_domain(fd); |
| 766 | if (domain < 0) |
| 767 | return domain; |
| 768 | |
| 769 | switch (domain) { |
| 770 | case AF_INET: |
| 771 | #ifdef IP_MULTICAST_ALL |
| 772 | return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)); |
| 773 | #endif |
| 774 | case AF_INET6: |
| 775 | /* there seems no equivalent ?!? */ |
| 776 | default: |
| 777 | return -EINVAL; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | /* FreeBSD calls the socket option differently */ |
| 782 | #if !defined(IPV6_ADD_MEMBERSHIP) && defined(IPV6_JOIN_GROUP) |
| 783 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP |
| 784 | #endif |
| 785 | |
| 786 | /*! Subscribe to the given IP multicast group |
| 787 | * \param[in] fd file descriptor of related scoket |
| 788 | * \param[in] grp_addr ASCII representation of the multicast group address |
| 789 | * \returns 0 on success; negative otherwise */ |
| 790 | int osmo_sock_mcast_subscribe(int fd, const char *grp_addr) |
| 791 | { |
| 792 | int rc, domain; |
| 793 | struct ip_mreq mreq; |
| 794 | struct ipv6_mreq mreq6; |
| 795 | struct in6_addr i6a; |
| 796 | |
| 797 | domain = sock_get_domain(fd); |
| 798 | if (domain < 0) |
| 799 | return domain; |
| 800 | |
| 801 | switch (domain) { |
| 802 | case AF_INET: |
| 803 | memset(&mreq, 0, sizeof(mreq)); |
| 804 | mreq.imr_multiaddr.s_addr = inet_addr(grp_addr); |
| 805 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
| 806 | return setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 807 | #ifdef IPV6_ADD_MEMBERSHIP |
| 808 | case AF_INET6: |
| 809 | memset(&mreq6, 0, sizeof(mreq6)); |
| 810 | rc = inet_pton(AF_INET6, grp_addr, (void *)&i6a); |
| 811 | if (rc < 0) |
| 812 | return -EINVAL; |
| 813 | mreq6.ipv6mr_multiaddr = i6a; |
| 814 | return setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)); |
| 815 | #endif |
| 816 | default: |
| 817 | return -EINVAL; |
| 818 | } |
| 819 | } |
| 820 | |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 821 | /*! Determine the matching local IP-address for a given remote IP-Address. |
| 822 | * \param[out] local_ip caller provided memory for resulting local IP-address |
| 823 | * \param[in] remote_ip remote IP-address |
| 824 | * \param[in] fd file descriptor of related scoket |
| 825 | * \returns 0 on success; negative otherwise |
| 826 | * |
| 827 | * The function accepts IPv4 and IPv6 address strings. The caller must provide |
| 828 | * at least INET6_ADDRSTRLEN bytes for local_ip if an IPv6 is expected as |
| 829 | * as result. For IPv4 addresses the required amount is INET_ADDRSTRLEN. */ |
| 830 | int osmo_sock_local_ip(char *local_ip, const char *remote_ip) |
| 831 | { |
| 832 | int sfd; |
| 833 | int rc; |
| 834 | struct addrinfo addrinfo_hint; |
| 835 | struct addrinfo *addrinfo = NULL; |
| 836 | struct sockaddr_in local_addr; |
| 837 | socklen_t local_addr_len; |
| 838 | uint16_t family; |
| 839 | |
| 840 | /* Find out the address family (AF_INET or AF_INET6?) */ |
| 841 | memset(&addrinfo_hint, '\0', sizeof(addrinfo_hint)); |
| 842 | addrinfo_hint.ai_family = PF_UNSPEC; |
| 843 | addrinfo_hint.ai_flags = AI_NUMERICHOST; |
| 844 | rc = getaddrinfo(remote_ip, NULL, &addrinfo_hint, &addrinfo); |
| 845 | if (rc) |
| 846 | return -EINVAL; |
| 847 | family = addrinfo->ai_family; |
| 848 | freeaddrinfo(addrinfo); |
| 849 | |
| 850 | /* Connect a dummy socket to trick the kernel into determining the |
| 851 | * ip-address of the interface that would be used if we would send |
| 852 | * out an actual packet */ |
| 853 | sfd = osmo_sock_init2(family, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, remote_ip, 0, OSMO_SOCK_F_CONNECT); |
| 854 | if (sfd < 0) |
| 855 | return -EINVAL; |
| 856 | |
| 857 | /* Request the IP address of the interface that the kernel has |
| 858 | * actually choosen. */ |
| 859 | memset(&local_addr, 0, sizeof(local_addr)); |
| 860 | local_addr_len = sizeof(local_addr); |
| 861 | rc = getsockname(sfd, (struct sockaddr *)&local_addr, &local_addr_len); |
Philipp Maier | 8b7975b | 2018-01-22 15:38:07 +0100 | [diff] [blame] | 862 | close(sfd); |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 863 | if (rc < 0) |
| 864 | return -EINVAL; |
| 865 | if (local_addr.sin_family == AF_INET) |
Philipp Maier | 91cfda8 | 2018-01-22 16:56:27 +0100 | [diff] [blame] | 866 | inet_ntop(AF_INET, &local_addr.sin_addr, local_ip, INET_ADDRSTRLEN); |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 867 | else if (local_addr.sin_family == AF_INET6) |
Philipp Maier | 91cfda8 | 2018-01-22 16:56:27 +0100 | [diff] [blame] | 868 | inet_ntop(AF_INET6, &local_addr.sin_addr, local_ip, INET6_ADDRSTRLEN); |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 869 | else |
| 870 | return -EINVAL; |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 875 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 876 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 877 | /*! @} */ |