1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 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.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include <arpa/inet.h>
30 #include <sys/types.h>
33 #include <sys/ioctl.h>
37 #include "socket-util.h"
41 int socket_address_parse(SocketAddress *a, const char *s) {
50 a->type = SOCK_STREAM;
53 /* IPv6 in [x:.....:z]:p notation */
55 if (!socket_ipv6_is_supported()) {
56 log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
60 if (!(e = strchr(s+1, ']')))
63 if (!(n = strndup(s+1, e-s-1)))
67 if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0) {
69 return errno != 0 ? -errno : -EINVAL;
79 if ((r = safe_atou(e, &u)) < 0)
82 if (u <= 0 || u > 0xFFFF)
85 a->sockaddr.in6.sin6_family = AF_INET6;
86 a->sockaddr.in6.sin6_port = htons((uint16_t) u);
87 a->size = sizeof(struct sockaddr_in6);
89 } else if (*s == '/') {
95 if (l >= sizeof(a->sockaddr.un.sun_path))
98 a->sockaddr.un.sun_family = AF_UNIX;
99 memcpy(a->sockaddr.un.sun_path, s, l);
100 a->size = offsetof(struct sockaddr_un, sun_path) + l + 1;
102 } else if (*s == '@') {
103 /* Abstract AF_UNIX socket */
107 if (l >= sizeof(a->sockaddr.un.sun_path) - 1)
110 a->sockaddr.un.sun_family = AF_UNIX;
111 memcpy(a->sockaddr.un.sun_path+1, s+1, l);
112 a->size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
116 if ((e = strchr(s, ':'))) {
118 if ((r = safe_atou(e+1, &u)) < 0)
121 if (u <= 0 || u > 0xFFFF)
124 if (!(n = strndup(s, e-s)))
127 /* IPv4 in w.x.y.z:p notation? */
128 if ((r = inet_pton(AF_INET, n, &a->sockaddr.in4.sin_addr)) < 0) {
134 /* Gotcha, it's a traditional IPv4 address */
137 a->sockaddr.in4.sin_family = AF_INET;
138 a->sockaddr.in4.sin_port = htons((uint16_t) u);
139 a->size = sizeof(struct sockaddr_in);
143 if (strlen(n) > IF_NAMESIZE-1) {
148 /* Uh, our last resort, an interface name */
149 idx = if_nametoindex(n);
155 if (!socket_ipv6_is_supported()) {
156 log_warning("Binding to interface is not available since kernel does not support IPv6.");
157 return -EAFNOSUPPORT;
160 a->sockaddr.in6.sin6_family = AF_INET6;
161 a->sockaddr.in6.sin6_port = htons((uint16_t) u);
162 a->sockaddr.in6.sin6_scope_id = idx;
163 a->sockaddr.in6.sin6_addr = in6addr_any;
164 a->size = sizeof(struct sockaddr_in6);
169 if ((r = safe_atou(s, &u)) < 0)
172 if (u <= 0 || u > 0xFFFF)
175 if (socket_ipv6_is_supported()) {
176 a->sockaddr.in6.sin6_family = AF_INET6;
177 a->sockaddr.in6.sin6_port = htons((uint16_t) u);
178 a->sockaddr.in6.sin6_addr = in6addr_any;
179 a->size = sizeof(struct sockaddr_in6);
181 a->sockaddr.in4.sin_family = AF_INET;
182 a->sockaddr.in4.sin_port = htons((uint16_t) u);
183 a->sockaddr.in4.sin_addr.s_addr = INADDR_ANY;
184 a->size = sizeof(struct sockaddr_in);
192 int socket_address_verify(const SocketAddress *a) {
195 switch (socket_address_family(a)) {
197 if (a->size != sizeof(struct sockaddr_in))
200 if (a->sockaddr.in4.sin_port == 0)
206 if (a->size != sizeof(struct sockaddr_in6))
209 if (a->sockaddr.in6.sin6_port == 0)
215 if (a->size < offsetof(struct sockaddr_un, sun_path))
218 if (a->size > offsetof(struct sockaddr_un, sun_path)) {
220 if (a->sockaddr.un.sun_path[0] != 0) {
224 if (!(e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path))))
227 if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1)
235 return -EAFNOSUPPORT;
239 int socket_address_print(const SocketAddress *a, char **p) {
244 if ((r = socket_address_verify(a)) < 0)
247 switch (socket_address_family(a)) {
251 if (!(ret = new(char, INET_ADDRSTRLEN+1+5+1)))
254 if (!inet_ntop(AF_INET, &a->sockaddr.in4.sin_addr, ret, INET_ADDRSTRLEN)) {
259 sprintf(strchr(ret, 0), ":%u", ntohs(a->sockaddr.in4.sin_port));
267 if (!(ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1)))
271 if (!inet_ntop(AF_INET6, &a->sockaddr.in6.sin6_addr, ret+1, INET6_ADDRSTRLEN)) {
276 sprintf(strchr(ret, 0), "]:%u", ntohs(a->sockaddr.in6.sin6_port));
284 if (a->size <= offsetof(struct sockaddr_un, sun_path)) {
286 if (!(ret = strdup("<unamed>")))
289 } else if (a->sockaddr.un.sun_path[0] == 0) {
292 /* FIXME: We assume we can print the
293 * socket path here and that it hasn't
294 * more than one NUL byte. That is
295 * actually an invalid assumption */
297 if (!(ret = new(char, sizeof(a->sockaddr.un.sun_path)+1)))
301 memcpy(ret+1, a->sockaddr.un.sun_path+1, sizeof(a->sockaddr.un.sun_path)-1);
302 ret[sizeof(a->sockaddr.un.sun_path)] = 0;
306 if (!(ret = strdup(a->sockaddr.un.sun_path)))
319 int socket_address_listen(
320 const SocketAddress *a,
322 SocketAddressBindIPv6Only only,
323 const char *bind_to_device,
325 mode_t directory_mode,
334 if ((r = socket_address_verify(a)) < 0)
337 if (socket_address_family(a) == AF_INET6 && !socket_ipv6_is_supported())
338 return -EAFNOSUPPORT;
340 r = label_socket_set(label);
344 fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
345 r = fd < 0 ? -errno : 0;
347 label_socket_clear();
352 if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
353 int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
355 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
360 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
365 if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) < 0)
366 log_warning("IP_FREEBIND failed: %m");
370 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
373 if (socket_address_family(a) == AF_UNIX && a->sockaddr.un.sun_path[0] != 0) {
377 mkdir_parents(a->sockaddr.un.sun_path, directory_mode);
379 /* Enforce the right access mode for the socket*/
380 old_mask = umask(~ socket_mode);
382 /* Include the original umask in our mask */
383 umask(~socket_mode | old_mask);
385 r = bind(fd, &a->sockaddr.sa, a->size);
387 if (r < 0 && errno == EADDRINUSE) {
388 /* Unlink and try again */
389 unlink(a->sockaddr.un.sun_path);
390 r = bind(fd, &a->sockaddr.sa, a->size);
395 r = bind(fd, &a->sockaddr.sa, a->size);
400 if (a->type == SOCK_STREAM)
401 if (listen(fd, backlog) < 0)
409 close_nointr_nofail(fd);
413 bool socket_address_can_accept(const SocketAddress *a) {
417 a->type == SOCK_STREAM ||
418 a->type == SOCK_SEQPACKET;
421 bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
425 /* Invalid addresses are unequal to all */
426 if (socket_address_verify(a) < 0 ||
427 socket_address_verify(b) < 0)
430 if (a->type != b->type)
433 if (a->size != b->size)
436 if (socket_address_family(a) != socket_address_family(b))
439 switch (socket_address_family(a)) {
442 if (a->sockaddr.in4.sin_addr.s_addr != b->sockaddr.in4.sin_addr.s_addr)
445 if (a->sockaddr.in4.sin_port != b->sockaddr.in4.sin_port)
451 if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0)
454 if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port)
461 if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0))
464 if (a->sockaddr.un.sun_path[0]) {
465 if (strncmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, sizeof(a->sockaddr.un.sun_path)) != 0)
468 if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
475 /* Cannot compare, so we assume the addresses are different */
482 bool socket_address_is(const SocketAddress *a, const char *s, int type) {
483 struct SocketAddress b;
488 if (socket_address_parse(&b, s) < 0)
493 return socket_address_equal(a, &b);
496 bool socket_address_needs_mount(const SocketAddress *a, const char *prefix) {
499 if (socket_address_family(a) != AF_UNIX)
502 if (a->sockaddr.un.sun_path[0] == 0)
505 return path_startswith(a->sockaddr.un.sun_path, prefix);
508 bool socket_ipv6_is_supported(void) {
512 if (access("/sys/module/ipv6", F_OK) != 0)
515 /* If we can't check "disable" parameter, assume enabled */
516 if (read_one_line_file("/sys/module/ipv6/parameters/disable", &l) < 0)
519 /* If module was loaded with disable=1 no IPv6 available */
520 enabled = l[0] == '0';
526 static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = {
527 [SOCKET_ADDRESS_DEFAULT] = "default",
528 [SOCKET_ADDRESS_BOTH] = "both",
529 [SOCKET_ADDRESS_IPV6_ONLY] = "ipv6-only"
532 DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only, SocketAddressBindIPv6Only);