1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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>
33 #include "socket-util.h"
35 int socket_address_parse(SocketAddress *a, const char *s) {
44 a->type = SOCK_STREAM;
47 /* IPv6 in [x:.....:z]:p notation */
49 if (!(e = strchr(s+1, ']')))
52 if (!(n = strndup(s+1, e-s-1)))
56 if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0) {
58 return errno != 0 ? -errno : -EINVAL;
68 if ((r = safe_atou(e, &u)) < 0)
71 if (u <= 0 || u > 0xFFFF)
74 a->sockaddr.in6.sin6_family = AF_INET6;
75 a->sockaddr.in6.sin6_port = htons((uint16_t) u);
76 a->size = sizeof(struct sockaddr_in6);
78 } else if (*s == '/') {
84 if (l >= sizeof(a->sockaddr.un.sun_path))
87 a->sockaddr.un.sun_family = AF_UNIX;
88 memcpy(a->sockaddr.un.sun_path, s, l);
89 a->size = sizeof(sa_family_t) + l + 1;
91 } else if (*s == '@') {
92 /* Abstract AF_UNIX socket */
96 if (l >= sizeof(a->sockaddr.un.sun_path) - 1)
99 a->sockaddr.un.sun_family = AF_UNIX;
100 memcpy(a->sockaddr.un.sun_path+1, s+1, l);
101 a->size = sizeof(struct sockaddr_un);
105 if ((e = strchr(s, ':'))) {
108 if ((r = safe_atou(e+1, &u)) < 0)
111 if (u <= 0 || u > 0xFFFF)
114 if (!(n = strndup(s, e-s)))
117 /* IPv4 in w.x.y.z:p notation? */
118 if ((r = inet_pton(AF_INET, n, &a->sockaddr.in4.sin_addr)) < 0) {
124 /* Gotcha, it's a traditional IPv4 address */
127 a->sockaddr.in4.sin_family = AF_INET;
128 a->sockaddr.in4.sin_port = htons((uint16_t) u);
129 a->size = sizeof(struct sockaddr_in);
133 if (strlen(n) > IF_NAMESIZE-1) {
138 /* Uh, our last resort, an interface name */
139 idx = if_nametoindex(n);
145 a->sockaddr.in6.sin6_family = AF_INET6;
146 a->sockaddr.in6.sin6_port = htons((uint16_t) u);
147 a->sockaddr.in6.sin6_scope_id = idx;
148 a->sockaddr.in6.sin6_addr = in6addr_any;
149 a->size = sizeof(struct sockaddr_in6);
155 if ((r = safe_atou(s, &u)) < 0)
158 if (u <= 0 || u > 0xFFFF)
161 a->sockaddr.in6.sin6_family = AF_INET6;
162 a->sockaddr.in6.sin6_port = htons((uint16_t) u);
163 a->sockaddr.in6.sin6_addr = in6addr_any;
164 a->size = sizeof(struct sockaddr_in6);
171 int socket_address_verify(const SocketAddress *a) {
174 switch (socket_address_family(a)) {
176 if (a->size != sizeof(struct sockaddr_in))
179 if (a->sockaddr.in4.sin_port == 0)
185 if (a->size != sizeof(struct sockaddr_in6))
188 if (a->sockaddr.in6.sin6_port == 0)
194 if (a->size < sizeof(sa_family_t))
197 if (a->size > sizeof(sa_family_t)) {
199 if (a->sockaddr.un.sun_path[0] == 0) {
201 if (a->size != sizeof(struct sockaddr_un))
207 if (!(e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path))))
210 if (a->size != sizeof(sa_family_t) + (e - a->sockaddr.un.sun_path) + 1)
218 return -EAFNOSUPPORT;
222 int socket_address_print(const SocketAddress *a, char **p) {
227 if ((r = socket_address_verify(a)) < 0)
230 switch (socket_address_family(a)) {
234 if (!(ret = new(char, INET_ADDRSTRLEN+1+5+1)))
237 if (!inet_ntop(AF_INET, &a->sockaddr.in4.sin_addr, ret, INET_ADDRSTRLEN)) {
242 sprintf(strchr(ret, 0), ":%u", ntohs(a->sockaddr.in4.sin_port));
250 if (!(ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1)))
254 if (!inet_ntop(AF_INET6, &a->sockaddr.in6.sin6_addr, ret+1, INET6_ADDRSTRLEN)) {
259 sprintf(strchr(ret, 0), "]:%u", ntohs(a->sockaddr.in6.sin6_port));
267 if (a->size <= sizeof(sa_family_t)) {
269 if (!(ret = strdup("<unamed>")))
272 } else if (a->sockaddr.un.sun_path[0] == 0) {
275 /* FIXME: We assume we can print the
276 * socket path here and that it hasn't
277 * more than one NUL byte. That is
278 * actually an invalid assumption */
280 if (!(ret = new(char, sizeof(a->sockaddr.un.sun_path)+1)))
284 memcpy(ret+1, a->sockaddr.un.sun_path+1, sizeof(a->sockaddr.un.sun_path)-1);
285 ret[sizeof(a->sockaddr.un.sun_path)] = 0;
289 if (!(ret = strdup(a->sockaddr.un.sun_path)))
302 int socket_address_listen(const SocketAddress *a, int backlog, SocketAddressBindIPv6Only only, const char *bind_to_device, int *ret) {
307 if ((r = socket_address_verify(a)) < 0)
310 if ((fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)) < 0)
313 if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
314 int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
316 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
321 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
325 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
328 if (bind(fd, &a->sockaddr.sa, a->size) < 0)
331 if (a->type == SOCK_STREAM)
332 if (listen(fd, backlog) < 0)