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 | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 54 | /*! \brief Initialize a socket (including bind/connect) |
| 55 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 56 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 57 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 58 | * \param[in] host remote host name or IP address in string form |
| 59 | * \param[in] port remote port number in host byte order |
| 60 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 61 | * \returns socket file descriptor on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 62 | * |
| 63 | * This function creates a new socket of the designated \a family, \a |
| 64 | * type and \a proto and optionally binds or connects it, depending on |
| 65 | * the value of \a flags parameter. |
| 66 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 67 | 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] | 68 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 69 | { |
| 70 | struct addrinfo hints, *result, *rp; |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 71 | int sfd, rc, on = 1; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 72 | char portbuf[16]; |
| 73 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 74 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 75 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 76 | LOGP(DLGLOBAL, LOGL_ERROR, "invalid: both bind and connect flags set:" |
Neels Hofmeyr | b7f191f | 2016-08-29 11:22:03 +0200 | [diff] [blame] | 77 | " %s:%u\n", host, port); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 78 | return -EINVAL; |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 79 | } |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 80 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 81 | sprintf(portbuf, "%u", port); |
| 82 | memset(&hints, 0, sizeof(struct addrinfo)); |
| 83 | hints.ai_family = family; |
Pablo Neira Ayuso | e04a14d | 2013-01-14 00:12:28 +0100 | [diff] [blame] | 84 | if (type == SOCK_RAW) { |
| 85 | /* Workaround for glibc, that returns EAI_SERVICE (-8) if |
| 86 | * SOCK_RAW and IPPROTO_GRE is used. |
| 87 | */ |
| 88 | hints.ai_socktype = SOCK_DGRAM; |
| 89 | hints.ai_protocol = IPPROTO_UDP; |
| 90 | } else { |
| 91 | hints.ai_socktype = type; |
| 92 | hints.ai_protocol = proto; |
| 93 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 94 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 95 | if (flags & OSMO_SOCK_F_BIND) |
Harald Welte | f9e0746 | 2011-05-31 17:47:54 +0200 | [diff] [blame] | 96 | hints.ai_flags |= AI_PASSIVE; |
| 97 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 98 | rc = getaddrinfo(host, portbuf, &hints, &result); |
| 99 | if (rc != 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 100 | LOGP(DLGLOBAL, LOGL_ERROR, "getaddrinfo returned NULL: %s:%u: %s\n", |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 101 | host, port, strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 102 | return -EINVAL; |
| 103 | } |
| 104 | |
| 105 | for (rp = result; rp != NULL; rp = rp->ai_next) { |
Pablo Neira Ayuso | e04a14d | 2013-01-14 00:12:28 +0100 | [diff] [blame] | 106 | /* Workaround for glibc again */ |
| 107 | if (type == SOCK_RAW) { |
| 108 | rp->ai_socktype = SOCK_RAW; |
| 109 | rp->ai_protocol = proto; |
| 110 | } |
| 111 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 112 | sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
| 113 | if (sfd == -1) |
| 114 | continue; |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 115 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 116 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 117 | LOGP(DLGLOBAL, LOGL_ERROR, |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 118 | "cannot set this socket unblocking:" |
| 119 | " %s:%u: %s\n", |
| 120 | host, port, strerror(errno)); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 121 | close(sfd); |
Harald Welte | 0fc315d | 2016-11-26 10:18:24 +0100 | [diff] [blame] | 122 | freeaddrinfo(result); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 123 | return -EINVAL; |
| 124 | } |
| 125 | } |
| 126 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 127 | rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); |
| 128 | if (rc != -1 || (rc == -1 && errno == EINPROGRESS)) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 129 | break; |
| 130 | } else { |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 131 | rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, |
| 132 | &on, sizeof(on)); |
| 133 | if (rc < 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 134 | LOGP(DLGLOBAL, LOGL_ERROR, |
Neels Hofmeyr | f0f07d9 | 2016-08-22 13:34:23 +0200 | [diff] [blame] | 135 | "cannot setsockopt socket:" |
| 136 | " %s:%u: %s\n", |
| 137 | host, port, strerror(errno)); |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 138 | break; |
| 139 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 140 | if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1) |
| 141 | break; |
| 142 | } |
| 143 | close(sfd); |
| 144 | } |
| 145 | freeaddrinfo(result); |
| 146 | |
| 147 | if (rp == NULL) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 148 | 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] | 149 | host, port, strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 150 | return -ENODEV; |
| 151 | } |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 152 | |
| 153 | setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 154 | |
| 155 | /* Make sure to call 'listen' on a bound, connection-oriented sock */ |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 156 | if (flags & OSMO_SOCK_F_BIND) { |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 157 | switch (type) { |
| 158 | case SOCK_STREAM: |
| 159 | case SOCK_SEQPACKET: |
| 160 | listen(sfd, 10); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | return sfd; |
| 165 | } |
| 166 | |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 167 | /*! \brief fill \ref osmo_fd for a give sfd |
| 168 | * \param[out] ofd file descriptor (will be filled in) |
| 169 | * \param[in] sfd socket file descriptor |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 170 | * \returns socket fd on success; negative on error |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 171 | * |
| 172 | * This function fills the \a ofd structure. |
| 173 | */ |
| 174 | static inline int osmo_fd_init_ofd(struct osmo_fd *ofd, int sfd) |
| 175 | { |
| 176 | int rc; |
| 177 | |
| 178 | if (sfd < 0) |
| 179 | return sfd; |
| 180 | |
| 181 | ofd->fd = sfd; |
| 182 | ofd->when = BSC_FD_READ; |
| 183 | |
| 184 | rc = osmo_fd_register(ofd); |
| 185 | if (rc < 0) { |
| 186 | close(sfd); |
| 187 | return rc; |
| 188 | } |
| 189 | |
| 190 | return sfd; |
| 191 | } |
| 192 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 193 | /*! \brief Initialize a socket and fill \ref osmo_fd |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 194 | * \param[out] ofd file descriptor (will be filled in) |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 195 | * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC |
| 196 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 197 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 198 | * \param[in] host remote host name or IP address in string form |
| 199 | * \param[in] port remote port number in host byte order |
| 200 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 201 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 202 | * |
| 203 | * This function creates (and optionall binds/connects) a socket using |
| 204 | * \ref osmo_sock_init, but also fills the \a ofd structure. |
| 205 | */ |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 206 | 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] | 207 | const char *host, uint16_t port, unsigned int flags) |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 208 | { |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 209 | 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] | 210 | } |
| 211 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 212 | /*! \brief Initialize a socket and fill \ref sockaddr |
| 213 | * \param[out] ss socket address (will be filled in) |
| 214 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 215 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 216 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 217 | * \returns socket fd on success; negative on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 218 | * |
| 219 | * This function creates (and optionall binds/connects) a socket using |
| 220 | * \ref osmo_sock_init, but also fills the \a ss structure. |
| 221 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 222 | int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 223 | uint8_t proto, unsigned int flags) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 224 | { |
| 225 | char host[NI_MAXHOST]; |
| 226 | uint16_t port; |
| 227 | struct sockaddr_in *sin; |
| 228 | struct sockaddr_in6 *sin6; |
| 229 | int s, sa_len; |
| 230 | |
| 231 | /* determine port and host from ss */ |
| 232 | switch (ss->sa_family) { |
| 233 | case AF_INET: |
| 234 | sin = (struct sockaddr_in *) ss; |
| 235 | sa_len = sizeof(struct sockaddr_in); |
| 236 | port = ntohs(sin->sin_port); |
| 237 | break; |
| 238 | case AF_INET6: |
| 239 | sin6 = (struct sockaddr_in6 *) ss; |
| 240 | sa_len = sizeof(struct sockaddr_in6); |
| 241 | port = ntohs(sin6->sin6_port); |
| 242 | break; |
| 243 | default: |
| 244 | return -EINVAL; |
| 245 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 246 | |
| 247 | s = getnameinfo(ss, sa_len, host, NI_MAXHOST, |
| 248 | NULL, 0, NI_NUMERICHOST); |
| 249 | if (s != 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 250 | LOGP(DLGLOBAL, LOGL_ERROR, "getnameinfo failed:" |
| 251 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 252 | return s; |
| 253 | } |
| 254 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 255 | return osmo_sock_init(ss->sa_family, type, proto, host, port, flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int sockaddr_equal(const struct sockaddr *a, |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 259 | const struct sockaddr *b, unsigned int len) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 260 | { |
| 261 | struct sockaddr_in *sin_a, *sin_b; |
| 262 | struct sockaddr_in6 *sin6_a, *sin6_b; |
| 263 | |
| 264 | if (a->sa_family != b->sa_family) |
| 265 | return 0; |
| 266 | |
| 267 | switch (a->sa_family) { |
| 268 | case AF_INET: |
| 269 | sin_a = (struct sockaddr_in *)a; |
| 270 | sin_b = (struct sockaddr_in *)b; |
| 271 | if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr, |
| 272 | sizeof(struct in_addr))) |
| 273 | return 1; |
| 274 | break; |
| 275 | case AF_INET6: |
| 276 | sin6_a = (struct sockaddr_in6 *)a; |
| 277 | sin6_b = (struct sockaddr_in6 *)b; |
| 278 | if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr, |
| 279 | sizeof(struct in6_addr))) |
| 280 | return 1; |
| 281 | break; |
| 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 286 | /*! \brief Determine if the given address is a local address |
| 287 | * \param[in] addr Socket Address |
| 288 | * \param[in] addrlen Length of socket address in bytes |
| 289 | * \returns 1 if address is local, 0 otherwise. |
| 290 | */ |
Harald Welte | bc32d05 | 2012-04-08 11:31:32 +0200 | [diff] [blame] | 291 | int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 292 | { |
| 293 | struct ifaddrs *ifaddr, *ifa; |
| 294 | |
| 295 | if (getifaddrs(&ifaddr) == -1) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 296 | LOGP(DLGLOBAL, LOGL_ERROR, "getifaddrs:" |
| 297 | " %s\n", strerror(errno)); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 298 | return -EIO; |
| 299 | } |
| 300 | |
| 301 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
Harald Welte | 4d3a7b1 | 2011-05-24 21:31:53 +0200 | [diff] [blame] | 302 | if (!ifa->ifa_addr) |
| 303 | continue; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 304 | if (sockaddr_equal(ifa->ifa_addr, addr, addrlen)) |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 310 | |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 311 | /*! \brief Initialize a unix domain socket (including bind/connect) |
| 312 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 313 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 314 | * \param[in] socket_path path to identify the socket |
| 315 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 316 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 317 | * |
| 318 | * This function creates a new unix domain socket, \a |
| 319 | * type and \a proto and optionally binds or connects it, depending on |
| 320 | * the value of \a flags parameter. |
| 321 | */ |
| 322 | int osmo_sock_unix_init(uint16_t type, uint8_t proto, |
| 323 | const char *socket_path, unsigned int flags) |
| 324 | { |
| 325 | struct sockaddr_un local; |
| 326 | int sfd, rc, on = 1; |
| 327 | unsigned int namelen; |
| 328 | |
| 329 | if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == |
| 330 | (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | local.sun_family = AF_UNIX; |
| 334 | strncpy(local.sun_path, socket_path, sizeof(local.sun_path)); |
| 335 | local.sun_path[sizeof(local.sun_path) - 1] = '\0'; |
| 336 | |
| 337 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
| 338 | local.sun_len = strlen(local.sun_path); |
| 339 | #endif |
| 340 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 341 | namelen = SUN_LEN(&local); |
| 342 | #else |
| 343 | namelen = strlen(local.sun_path) + |
| 344 | offsetof(struct sockaddr_un, sun_path); |
| 345 | #endif |
| 346 | |
| 347 | sfd = socket(AF_UNIX, type, proto); |
| 348 | if (sfd < 0) |
| 349 | return -1; |
| 350 | |
| 351 | if (flags & OSMO_SOCK_F_CONNECT) { |
| 352 | rc = connect(sfd, (struct sockaddr *)&local, namelen); |
| 353 | if (rc < 0) |
| 354 | goto err; |
| 355 | } else { |
| 356 | unlink(local.sun_path); |
| 357 | rc = bind(sfd, (struct sockaddr *)&local, namelen); |
| 358 | if (rc < 0) |
| 359 | goto err; |
| 360 | } |
| 361 | |
| 362 | if (flags & OSMO_SOCK_F_NONBLOCK) { |
| 363 | if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 364 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 365 | "cannot set this socket unblocking: %s\n", |
| 366 | strerror(errno)); |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 367 | close(sfd); |
| 368 | return -EINVAL; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (flags & OSMO_SOCK_F_BIND) { |
| 373 | rc = listen(sfd, 10); |
| 374 | if (rc < 0) |
| 375 | goto err; |
| 376 | } |
| 377 | |
| 378 | return sfd; |
| 379 | err: |
| 380 | close(sfd); |
| 381 | return -1; |
| 382 | } |
| 383 | |
| 384 | /*! \brief Initialize a unix domain socket and fill \ref osmo_fd |
| 385 | * \param[out] ofd file descriptor (will be filled in) |
| 386 | * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM |
| 387 | * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP |
| 388 | * \param[in] socket_path path to identify the socket |
| 389 | * \param[in] flags flags like \ref OSMO_SOCK_F_CONNECT |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 390 | * \returns socket fd on success; negative on error |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 391 | * |
| 392 | * This function creates (and optionall binds/connects) a socket using |
| 393 | * \ref osmo_sock_unix_init, but also fills the \a ofd structure. |
| 394 | */ |
| 395 | int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto, |
| 396 | const char *socket_path, unsigned int flags) |
| 397 | { |
Max | 862ba65 | 2014-10-13 14:54:25 +0200 | [diff] [blame] | 398 | 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] | 399 | } |
| 400 | |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 401 | /*! \brief Get address/port information on soocket in dyn-alloc string |
| 402 | * \param[in] ctx talloc context from which to allocate string buffer |
| 403 | * \param[in] fd file descriptor of socket |
| 404 | * \returns string identifying the connection of this socket |
| 405 | */ |
| 406 | char *osmo_sock_get_name(void *ctx, int fd) |
| 407 | { |
| 408 | struct sockaddr sa_l, sa_r; |
| 409 | socklen_t sa_len_l = sizeof(sa_l); |
| 410 | socklen_t sa_len_r = sizeof(sa_r); |
| 411 | char hostbuf_l[64], hostbuf_r[64]; |
| 412 | char portbuf_l[16], portbuf_r[16]; |
| 413 | int rc; |
| 414 | |
| 415 | rc = getsockname(fd, &sa_l, &sa_len_l); |
| 416 | if (rc < 0) |
| 417 | return NULL; |
| 418 | |
| 419 | rc = getnameinfo(&sa_l, sa_len_l, hostbuf_l, sizeof(hostbuf_l), |
| 420 | portbuf_l, sizeof(portbuf_l), |
| 421 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 422 | if (rc < 0) |
| 423 | return NULL; |
| 424 | |
| 425 | rc = getpeername(fd, &sa_r, &sa_len_r); |
| 426 | if (rc < 0) |
| 427 | goto local_only; |
| 428 | |
| 429 | rc = getnameinfo(&sa_r, sa_len_r, hostbuf_r, sizeof(hostbuf_r), |
| 430 | portbuf_r, sizeof(portbuf_r), |
| 431 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 432 | if (rc < 0) |
| 433 | goto local_only; |
| 434 | |
| 435 | return talloc_asprintf(ctx, "(%s:%s<->%s:%s)", hostbuf_r, portbuf_r, |
| 436 | hostbuf_l, portbuf_l); |
| 437 | |
| 438 | local_only: |
| 439 | return talloc_asprintf(ctx, "(NULL<->%s:%s)", hostbuf_l, portbuf_l); |
| 440 | } |
| 441 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 442 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 443 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 444 | /*! @} */ |