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 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 22 | #include "../config.h" |
| 23 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 24 | /*! \addtogroup socket |
| 25 | * @{ |
| 26 | */ |
| 27 | |
| 28 | /*! \file socket.c |
| 29 | * \brief Osmocom socket convenience functions |
| 30 | */ |
| 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> |
| 45 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 46 | #include <stdio.h> |
| 47 | #include <unistd.h> |
| 48 | #include <stdint.h> |
| 49 | #include <string.h> |
| 50 | #include <errno.h> |
| 51 | #include <netdb.h> |
| 52 | #include <ifaddrs.h> |
| 53 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 54 | static struct addrinfo *addrinfo_helper(uint16_t family, uint16_t type, uint8_t proto, |
| 55 | const char *host, uint16_t port, bool passive) |
| 56 | { |
| 57 | struct addrinfo hints, *result; |
| 58 | char portbuf[16]; |
| 59 | int rc; |
| 60 | |
| 61 | snprintf(portbuf, sizeof(portbuf), "%u", port); |
| 62 | memset(&hints, 0, sizeof(struct addrinfo)); |
| 63 | hints.ai_family = family; |
| 64 | if (type == SOCK_RAW) { |
| 65 | /* Workaround for glibc, that returns EAI_SERVICE (-8) if |
| 66 | * SOCK_RAW and IPPROTO_GRE is used. |
| 67 | */ |
| 68 | hints.ai_socktype = SOCK_DGRAM; |
| 69 | hints.ai_protocol = IPPROTO_UDP; |
| 70 | } else { |
| 71 | hints.ai_socktype = type; |
| 72 | hints.ai_protocol = proto; |
| 73 | } |
| 74 | |
| 75 | if (passive) |
| 76 | hints.ai_flags |= AI_PASSIVE; |
| 77 | |
| 78 | rc = getaddrinfo(host, portbuf, &hints, &result); |
| 79 | if (rc != 0) { |
| 80 | LOGP(DLGLOBAL, LOGL_ERROR, "getaddrinfo returned NULL: %s:%u: %s\n", |
| 81 | host, port, strerror(errno)); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | static int socket_helper(const struct addrinfo *rp, unsigned int flags) |
| 89 | { |
| 90 | int sfd, on = 1; |
| 91 | |
| 92 | sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
| 93 | if (sfd == -1) |
| 94 | return sfd; |
| 95 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 96 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
| 97 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 98 | "cannot set this socket unblocking: %s\n", |
| 99 | strerror(errno)); |
| 100 | close(sfd); |
| 101 | sfd = -EINVAL; |
| 102 | } |
| 103 | } |
| 104 | return sfd; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /*! \brief Initialize a socket (including bind and/or connect) |
| 109 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 110 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 111 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 112 | * \param[in] local_host local host name or IP address in string form |
| 113 | * \param[in] local_port local port number in host byte order |
| 114 | * \param[in] remote_host remote host name or IP address in string form |
| 115 | * \param[in] remote_port remote port number in host byte order |
| 116 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
| 117 | * \returns socket file descriptor on success; negative on error |
| 118 | * |
| 119 | * This function creates a new socket of the designated \a family, \a |
| 120 | * type and \a proto and optionally binds it to the \a local_host and \a |
| 121 | * local_port as well as optionally connects it to the \a remote_host |
| 122 | * and \q remote_port, depending on the value * of \a flags parameter. |
| 123 | * |
| 124 | * As opposed to \ref osmo_sock_init(), this function allows to combine |
| 125 | * the \ref OSMO_SOCK_F_BIND and \ref OSMO_SOCK_F_CONNECT flags. This |
| 126 | * is useful if you want to connect to a remote host/port, but still |
| 127 | * want to bind that socket to either a specific local alias IP and/or a |
| 128 | * specific local source port. |
| 129 | * |
| 130 | * You must specify either \ref OSMO_SOCK_F_BIND, or \ref |
| 131 | * OSMO_SOCK_F_CONNECT, or both. |
| 132 | * |
| 133 | * If \ref OSMO_SOCK_F_NONBLOCK is specified, the socket will be set to |
| 134 | * non-blocking mode. |
| 135 | */ |
| 136 | int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto, |
| 137 | const char *local_host, uint16_t local_port, |
| 138 | const char *remote_host, uint16_t remote_port, unsigned int flags) |
| 139 | { |
| 140 | struct addrinfo *result, *rp; |
| 141 | int sfd = -1, rc, on = 1; |
| 142 | |
| 143 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { |
| 144 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " |
| 145 | "BIND or CONNECT flags\n"); |
| 146 | return -EINVAL; |
| 147 | } |
| 148 | |
| 149 | /* figure out local side of socket */ |
| 150 | if (flags & OSMO_SOCK_F_BIND) { |
| 151 | result = addrinfo_helper(family, type, proto, local_host, local_port, true); |
| 152 | if (!result) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
| 156 | /* Workaround for glibc again */ |
| 157 | if (type == SOCK_RAW) { |
| 158 | rp->ai_socktype = SOCK_RAW; |
| 159 | rp->ai_protocol = proto; |
| 160 | } |
| 161 | |
| 162 | sfd = socket_helper(rp, flags); |
| 163 | if (sfd < 0) |
| 164 | continue; |
| 165 | |
| 166 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 167 | &on, sizeof(on)); |
| 168 | if (rc < 0) { |
| 169 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 170 | "cannot setsockopt socket:" |
| 171 | " %s:%u: %s\n", |
| 172 | local_host, local_port, strerror(errno)); |
| 173 | break; |
| 174 | } |
| 175 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1) |
| 176 | break; |
| 177 | close(sfd); |
| 178 | } |
| 179 | freeaddrinfo(result); |
| 180 | if (rp == NULL) { |
| 181 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n", |
| 182 | local_host, local_port, strerror(errno)); |
| 183 | return -ENODEV; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /* figure out remote side of socket */ |
| 188 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 189 | result = addrinfo_helper(family, type, proto, remote_host, remote_port, false); |
| 190 | if (!result) { |
| 191 | close(sfd); |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
| 195 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
| 196 | /* Workaround for glibc again */ |
| 197 | if (type == SOCK_RAW) { |
| 198 | rp->ai_socktype = SOCK_RAW; |
| 199 | rp->ai_protocol = proto; |
| 200 | } |
| 201 | |
| 202 | if (!sfd) { |
| 203 | sfd = socket_helper(rp, flags); |
| 204 | if (sfd < 0) |
| 205 | continue; |
| 206 | } |
| 207 | |
| 208 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
| 209 | if (rc != -1 || (rc == -1 && errno == EINPROGRESS)) |
| 210 | break; |
| 211 | |
| 212 | close(sfd); |
| 213 | sfd = -1; |
| 214 | } |
| 215 | freeaddrinfo(result); |
| 216 | if (rp == NULL) { |
| 217 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", |
| 218 | remote_host, remote_port, strerror(errno)); |
| 219 | return -ENODEV; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /* Make sure to call 'listen' on a bound, connection-oriented sock */ |
| 224 | if ((flags & (OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT)) == OSMO_SOCK_F_BIND) { |
| 225 | switch (type) { |
| 226 | case SOCK_STREAM: |
| 227 | case SOCK_SEQPACKET: |
| 228 | listen(sfd, 10); |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | return sfd; |
| 233 | } |
| 234 | |
| 235 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 236 | /*! \brief Initialize a socket (including bind/connect) |
| 237 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 238 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 239 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 240 | * \param[in] host remote host name or IP address in string form |
| 241 | * \param[in] port remote port number in host byte order |
| 242 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 243 | * \returns socket file descriptor on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 244 | * |
| 245 | * This function creates a new socket of the designated \a family, \a |
| 246 | * type and \a proto and optionally binds or connects it, depending on |
| 247 | * the value of \a flags parameter. |
| 248 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 249 | 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] | 250 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 251 | { |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 252 | struct addrinfo *result, *rp; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 253 | int sfd, rc, on = 1; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 254 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 255 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 256 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 257 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: both bind and connect flags set:" |
Neels Hofmeyr | b7f191f | 2016-08-29 11:22:03 +0200 | [diff] [blame] | 258 | " %s:%u\n", host, port); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 259 | return -EINVAL; |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 260 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 261 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 262 | result = addrinfo_helper(family, type, proto, host, port, flags & OSMO_SOCK_F_BIND); |
| 263 | if (!result) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 264 | LOGP(DLGLOBAL, LOGL_ERROR, "getaddrinfo returned NULL: %s:%u: %s\n", |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 265 | host, port, strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 266 | return -EINVAL; |
| 267 | } |
| 268 | |
| 269 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
Pablo Neira Ayuso | e04a14d | 2013-01-14 00:12:28 +0100 | [diff] [blame] | 270 | /* Workaround for glibc again */ |
| 271 | if (type == SOCK_RAW) { |
| 272 | rp->ai_socktype = SOCK_RAW; |
| 273 | rp->ai_protocol = proto; |
| 274 | } |
| 275 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 276 | sfd = socket_helper(rp, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 277 | if (sfd == -1) |
| 278 | continue; |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 279 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 280 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 281 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
| 282 | if (rc != -1 || (rc == -1 && errno == EINPROGRESS)) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 283 | break; |
| 284 | } else { |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 285 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 286 | &on, sizeof(on)); |
| 287 | if (rc < 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 288 | LOGP(DLGLOBAL, LOGL_ERROR, |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 289 | "cannot setsockopt socket:" |
| 290 | " %s:%u: %s\n", |
| 291 | host, port, strerror(errno)); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 292 | break; |
| 293 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 294 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1) |
| 295 | break; |
| 296 | } |
| 297 | close(sfd); |
| 298 | } |
| 299 | freeaddrinfo(result); |
| 300 | |
| 301 | if (rp == NULL) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 302 | LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect/bind socket: %s:%u: %s\n", |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 303 | host, port, strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 304 | return -ENODEV; |
| 305 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 306 | |
| 307 | setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 308 | |
| 309 | /* Make sure to call 'listen' on a bound, connection-oriented sock */ |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 310 | if (flags & OSMO_SOCK_F_BIND) { |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 311 | switch (type) { |
| 312 | case SOCK_STREAM: |
| 313 | case SOCK_SEQPACKET: |
| 314 | listen(sfd, 10); |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | return sfd; |
| 319 | } |
| 320 | |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 321 | /*! \brief fill \ref osmo_fd for a give sfd |
| 322 | * \param[out] ofd file descriptor (will be filled in) |
| 323 | * \param[in] sfd socket file descriptor |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 324 | * \returns socket fd on success; negative on error |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 325 | * |
| 326 | * This function fills the \a ofd structure. |
| 327 | */ |
| 328 | static inline int osmo_fd_init_ofd(struct osmo_fd *ofd, int sfd) |
| 329 | { |
| 330 | int rc; |
| 331 | |
| 332 | if (sfd < 0) |
| 333 | return sfd; |
| 334 | |
| 335 | ofd->fd = sfd; |
| 336 | ofd->when = BSC_FD_READ; |
| 337 | |
| 338 | rc = osmo_fd_register(ofd); |
| 339 | if (rc < 0) { |
| 340 | close(sfd); |
| 341 | return rc; |
| 342 | } |
| 343 | |
| 344 | return sfd; |
| 345 | } |
| 346 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 347 | /*! \brief Initialize a socket and fill \ref osmo_fd |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 348 | * \param[out] ofd file descriptor (will be filled in) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 349 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 350 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 351 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 352 | * \param[in] host remote host name or IP address in string form |
| 353 | * \param[in] port remote port number in host byte order |
| 354 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 355 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 356 | * |
| 357 | * This function creates (and optionall binds/connects) a socket using |
| 358 | * \ref osmo_sock_init, but also fills the \a ofd structure. |
| 359 | */ |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 360 | 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] | 361 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 362 | { |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 363 | 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] | 364 | } |
| 365 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 366 | /*! \brief Initialize a socket and fill \ref sockaddr |
| 367 | * \param[out] ss socket address (will be filled in) |
| 368 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 369 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 370 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 371 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 372 | * |
| 373 | * This function creates (and optionall binds/connects) a socket using |
| 374 | * \ref osmo_sock_init, but also fills the \a ss structure. |
| 375 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 376 | int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 377 | uint8_t proto, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 378 | { |
| 379 | char host[NI_MAXHOST]; |
| 380 | uint16_t port; |
| 381 | struct sockaddr_in *sin; |
| 382 | struct sockaddr_in6 *sin6; |
| 383 | int s, sa_len; |
| 384 | |
| 385 | /* determine port and host from ss */ |
| 386 | switch (ss->sa_family) { |
| 387 | case AF_INET: |
| 388 | sin = (struct sockaddr_in *) ss; |
| 389 | sa_len = sizeof(struct sockaddr_in); |
| 390 | port = ntohs(sin->sin_port); |
| 391 | break; |
| 392 | case AF_INET6: |
| 393 | sin6 = (struct sockaddr_in6 *) ss; |
| 394 | sa_len = sizeof(struct sockaddr_in6); |
| 395 | port = ntohs(sin6->sin6_port); |
| 396 | break; |
| 397 | default: |
| 398 | return -EINVAL; |
| 399 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 400 | |
| 401 | s = getnameinfo(ss, sa_len, host, NI_MAXHOST, |
| 402 | NULL, 0, NI_NUMERICHOST); |
| 403 | if (s != 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 404 | LOGP(DLGLOBAL, LOGL_ERROR, "getnameinfo failed:" |
| 405 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 406 | return s; |
| 407 | } |
| 408 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 409 | return osmo_sock_init(ss->sa_family, type, proto, host, port, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static int sockaddr_equal(const struct sockaddr *a, |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 413 | const struct sockaddr *b, unsigned int len) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 414 | { |
| 415 | struct sockaddr_in *sin_a, *sin_b; |
| 416 | struct sockaddr_in6 *sin6_a, *sin6_b; |
| 417 | |
| 418 | if (a->sa_family != b->sa_family) |
| 419 | return 0; |
| 420 | |
| 421 | switch (a->sa_family) { |
| 422 | case AF_INET: |
| 423 | sin_a = (struct sockaddr_in *)a; |
| 424 | sin_b = (struct sockaddr_in *)b; |
| 425 | if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr, |
| 426 | sizeof(struct in_addr))) |
| 427 | return 1; |
| 428 | break; |
| 429 | case AF_INET6: |
| 430 | sin6_a = (struct sockaddr_in6 *)a; |
| 431 | sin6_b = (struct sockaddr_in6 *)b; |
| 432 | if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr, |
| 433 | sizeof(struct in6_addr))) |
| 434 | return 1; |
| 435 | break; |
| 436 | } |
| 437 | return 0; |
| 438 | } |
| 439 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 440 | /*! \brief Determine if the given address is a local address |
| 441 | * \param[in] addr Socket Address |
| 442 | * \param[in] addrlen Length of socket address in bytes |
| 443 | * \returns 1 if address is local, 0 otherwise. |
| 444 | */ |
Harald Welte | bc32d05 | 2012-04-08 11:31:32 +0200 | [diff] [blame] | 445 | int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 446 | { |
| 447 | struct ifaddrs *ifaddr, *ifa; |
| 448 | |
| 449 | if (getifaddrs(&ifaddr) == -1) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 450 | LOGP(DLGLOBAL, LOGL_ERROR, "getifaddrs:" |
| 451 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 452 | return -EIO; |
| 453 | } |
| 454 | |
| 455 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
Harald Welte | 4d3a7b1 | 2011-05-24 21:31:53 +0200 | [diff] [blame] | 456 | if (!ifa->ifa_addr) |
| 457 | continue; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 458 | if (sockaddr_equal(ifa->ifa_addr, addr, addrlen)) |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | return 0; |
| 463 | } |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 464 | |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 465 | /*! \brief Initialize a unix domain socket (including bind/connect) |
| 466 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 467 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 468 | * \param[in] socket_path path to identify the socket |
| 469 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 470 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 471 | * |
| 472 | * This function creates a new unix domain socket, \a |
| 473 | * type and \a proto and optionally binds or connects it, depending on |
| 474 | * the value of \a flags parameter. |
| 475 | */ |
| 476 | int osmo_sock_unix_init(uint16_t type, uint8_t proto, |
| 477 | const char *socket_path, unsigned int flags) |
| 478 | { |
| 479 | struct sockaddr_un local; |
| 480 | int sfd, rc, on = 1; |
| 481 | unsigned int namelen; |
| 482 | |
| 483 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
| 484 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) |
| 485 | return -EINVAL; |
| 486 | |
| 487 | local.sun_family = AF_UNIX; |
| 488 | strncpy(local.sun_path, socket_path, sizeof(local.sun_path)); |
| 489 | local.sun_path[sizeof(local.sun_path) - 1] = '\0'; |
| 490 | |
| 491 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
| 492 | local.sun_len = strlen(local.sun_path); |
| 493 | #endif |
| 494 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 495 | namelen = SUN_LEN(&local); |
| 496 | #else |
| 497 | namelen = strlen(local.sun_path) + |
| 498 | offsetof(struct sockaddr_un, sun_path); |
| 499 | #endif |
| 500 | |
| 501 | sfd = socket(AF_UNIX, type, proto); |
| 502 | if (sfd < 0) |
| 503 | return -1; |
| 504 | |
| 505 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 506 | rc = connect(sfd, (struct sockaddr *)&local, namelen); |
| 507 | if (rc < 0) |
| 508 | goto err; |
| 509 | } else { |
| 510 | unlink(local.sun_path); |
| 511 | rc = bind(sfd, (struct sockaddr *)&local, namelen); |
| 512 | if (rc < 0) |
| 513 | goto err; |
| 514 | } |
| 515 | |
| 516 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 517 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 518 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 519 | "cannot set this socket unblocking: %s\n", |
| 520 | strerror(errno)); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 521 | close(sfd); |
| 522 | return -EINVAL; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (flags & OSMO_SOCK_F_BIND) { |
| 527 | rc = listen(sfd, 10); |
| 528 | if (rc < 0) |
| 529 | goto err; |
| 530 | } |
| 531 | |
| 532 | return sfd; |
| 533 | err: |
| 534 | close(sfd); |
| 535 | return -1; |
| 536 | } |
| 537 | |
| 538 | /*! \brief Initialize a unix domain socket and fill \ref osmo_fd |
| 539 | * \param[out] ofd file descriptor (will be filled in) |
| 540 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 541 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 542 | * \param[in] socket_path path to identify the socket |
| 543 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 544 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 545 | * |
| 546 | * This function creates (and optionall binds/connects) a socket using |
| 547 | * \ref osmo_sock_unix_init, but also fills the \a ofd structure. |
| 548 | */ |
| 549 | int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto, |
| 550 | const char *socket_path, unsigned int flags) |
| 551 | { |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 552 | 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] | 553 | } |
| 554 | |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 555 | /*! \brief Get address/port information on soocket in dyn-alloc string |
| 556 | * \param[in] ctx talloc context from which to allocate string buffer |
| 557 | * \param[in] fd file descriptor of socket |
| 558 | * \returns string identifying the connection of this socket |
| 559 | */ |
| 560 | char *osmo_sock_get_name(void *ctx, int fd) |
| 561 | { |
| 562 | struct sockaddr sa_l, sa_r; |
| 563 | socklen_t sa_len_l = sizeof(sa_l); |
| 564 | socklen_t sa_len_r = sizeof(sa_r); |
| 565 | char hostbuf_l[64], hostbuf_r[64]; |
| 566 | char portbuf_l[16], portbuf_r[16]; |
| 567 | int rc; |
| 568 | |
| 569 | rc = getsockname(fd, &sa_l, &sa_len_l); |
| 570 | if (rc < 0) |
| 571 | return NULL; |
| 572 | |
| 573 | rc = getnameinfo(&sa_l, sa_len_l, hostbuf_l, sizeof(hostbuf_l), |
| 574 | portbuf_l, sizeof(portbuf_l), |
| 575 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 576 | if (rc < 0) |
| 577 | return NULL; |
| 578 | |
| 579 | rc = getpeername(fd, &sa_r, &sa_len_r); |
| 580 | if (rc < 0) |
| 581 | goto local_only; |
| 582 | |
| 583 | rc = getnameinfo(&sa_r, sa_len_r, hostbuf_r, sizeof(hostbuf_r), |
| 584 | portbuf_r, sizeof(portbuf_r), |
| 585 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 586 | if (rc < 0) |
| 587 | goto local_only; |
| 588 | |
| 589 | return talloc_asprintf(ctx, "(%s:%s<->%s:%s)", hostbuf_r, portbuf_r, |
| 590 | hostbuf_l, portbuf_l); |
| 591 | |
| 592 | local_only: |
| 593 | return talloc_asprintf(ctx, "(NULL<->%s:%s)", hostbuf_l, portbuf_l); |
| 594 | } |
| 595 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 596 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 597 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 598 | /*! @} */ |