]> git.ipfire.org Git - people/ms/systemd.git/blame - socket-util.c
license: add GPLv2+ license blurbs everwhere
[people/ms/systemd.git] / socket-util.c
CommitLineData
42f4e3c4
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
42f4e3c4
LP
22#include <assert.h>
23#include <string.h>
24#include <unistd.h>
25#include <errno.h>
26#include <stdlib.h>
27#include <arpa/inet.h>
28#include <stdio.h>
542563ba 29#include <net/if.h>
42f4e3c4
LP
30
31#include "macro.h"
32#include "util.h"
33#include "socket-util.h"
34
542563ba 35int socket_address_parse(SocketAddress *a, const char *s) {
42f4e3c4
LP
36 int r;
37 char *e, *n;
38 unsigned u;
39
40 assert(a);
41 assert(s);
42
9152c765 43 zero(*a);
542563ba 44 a->type = SOCK_STREAM;
42f4e3c4
LP
45
46 if (*s == '[') {
47 /* IPv6 in [x:.....:z]:p notation */
48
49 if (!(e = strchr(s+1, ']')))
50 return -EINVAL;
51
52 if (!(n = strndup(s+1, e-s-1)))
53 return -ENOMEM;
54
55 errno = 0;
56 if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0) {
57 free(n);
58 return errno != 0 ? -errno : -EINVAL;
59 }
60
61 free(n);
62
63 e++;
64 if (*e != ':')
65 return -EINVAL;
66
67 e++;
68 if ((r = safe_atou(e, &u)) < 0)
69 return r;
70
71 if (u <= 0 || u > 0xFFFF)
72 return -EINVAL;
73
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);
42f4e3c4
LP
77
78 } else if (*s == '/') {
79 /* AF_UNIX socket */
80
81 size_t l;
82
83 l = strlen(s);
84 if (l >= sizeof(a->sockaddr.un.sun_path))
85 return -EINVAL;
86
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;
90
1c24e7bd 91 } else if (*s == '@') {
42f4e3c4
LP
92 /* Abstract AF_UNIX socket */
93 size_t l;
94
95 l = strlen(s+1);
96 if (l >= sizeof(a->sockaddr.un.sun_path) - 1)
97 return -EINVAL;
98
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);
102
103 } else {
104
105 if ((e = strchr(s, ':'))) {
542563ba
LP
106 int r;
107
108 if ((r = safe_atou(e+1, &u)) < 0)
109 return r;
110
111 if (u <= 0 || u > 0xFFFF)
112 return -EINVAL;
42f4e3c4 113
42f4e3c4
LP
114 if (!(n = strndup(s, e-s)))
115 return -ENOMEM;
116
542563ba
LP
117 /* IPv4 in w.x.y.z:p notation? */
118 if ((r = inet_pton(AF_INET, n, &a->sockaddr.in4.sin_addr)) < 0) {
42f4e3c4 119 free(n);
542563ba 120 return -errno;
42f4e3c4
LP
121 }
122
542563ba
LP
123 if (r > 0) {
124 /* Gotcha, it's a traditional IPv4 address */
125 free(n);
42f4e3c4 126
542563ba
LP
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);
130 } else {
131 unsigned idx;
42f4e3c4 132
acbb0225
LP
133 if (strlen(n) > IF_NAMESIZE-1) {
134 free(n);
135 return -EINVAL;
136 }
137
542563ba
LP
138 /* Uh, our last resort, an interface name */
139 idx = if_nametoindex(n);
140 free(n);
141
83c60c9f 142 if (idx == 0)
542563ba 143 return -EINVAL;
42f4e3c4 144
542563ba
LP
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;
83c60c9f 148 a->sockaddr.in6.sin6_addr = in6addr_any;
542563ba 149 a->size = sizeof(struct sockaddr_in6);
acbb0225 150
542563ba 151 }
42f4e3c4
LP
152 } else {
153
154 /* Just a port */
155 if ((r = safe_atou(s, &u)) < 0)
156 return r;
157
158 if (u <= 0 || u > 0xFFFF)
159 return -EINVAL;
160
161 a->sockaddr.in6.sin6_family = AF_INET6;
42f4e3c4 162 a->sockaddr.in6.sin6_port = htons((uint16_t) u);
83c60c9f 163 a->sockaddr.in6.sin6_addr = in6addr_any;
42f4e3c4
LP
164 a->size = sizeof(struct sockaddr_in6);
165 }
166 }
167
168 return 0;
169}
170
542563ba 171int socket_address_verify(const SocketAddress *a) {
42f4e3c4
LP
172 assert(a);
173
542563ba 174 switch (socket_address_family(a)) {
42f4e3c4
LP
175 case AF_INET:
176 if (a->size != sizeof(struct sockaddr_in))
177 return -EINVAL;
178
179 if (a->sockaddr.in4.sin_port == 0)
180 return -EINVAL;
181
182 return 0;
183
184 case AF_INET6:
185 if (a->size != sizeof(struct sockaddr_in6))
186 return -EINVAL;
187
188 if (a->sockaddr.in6.sin6_port == 0)
189 return -EINVAL;
190
191 return 0;
192
193 case AF_UNIX:
194 if (a->size < sizeof(sa_family_t))
195 return -EINVAL;
196
197 if (a->size > sizeof(sa_family_t)) {
198
199 if (a->sockaddr.un.sun_path[0] == 0) {
200 /* abstract */
201 if (a->size != sizeof(struct sockaddr_un))
202 return -EINVAL;
203 } else {
204 char *e;
205
206 /* path */
207 if (!(e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path))))
208 return -EINVAL;
209
210 if (a->size != sizeof(sa_family_t) + (e - a->sockaddr.un.sun_path) + 1)
211 return -EINVAL;
212 }
213 }
214
215 return 0;
216
217 default:
218 return -EAFNOSUPPORT;
219 }
220}
221
542563ba 222int socket_address_print(const SocketAddress *a, char **p) {
42f4e3c4
LP
223 int r;
224 assert(a);
225 assert(p);
226
542563ba 227 if ((r = socket_address_verify(a)) < 0)
42f4e3c4
LP
228 return r;
229
542563ba 230 switch (socket_address_family(a)) {
42f4e3c4
LP
231 case AF_INET: {
232 char *ret;
233
234 if (!(ret = new(char, INET_ADDRSTRLEN+1+5+1)))
235 return -ENOMEM;
236
237 if (!inet_ntop(AF_INET, &a->sockaddr.in4.sin_addr, ret, INET_ADDRSTRLEN)) {
238 free(ret);
239 return -errno;
240 }
241
242 sprintf(strchr(ret, 0), ":%u", ntohs(a->sockaddr.in4.sin_port));
243 *p = ret;
244 return 0;
245 }
246
247 case AF_INET6: {
248 char *ret;
249
250 if (!(ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1)))
251 return -ENOMEM;
252
253 ret[0] = '[';
254 if (!inet_ntop(AF_INET6, &a->sockaddr.in6.sin6_addr, ret+1, INET6_ADDRSTRLEN)) {
255 free(ret);
256 return -errno;
257 }
258
259 sprintf(strchr(ret, 0), "]:%u", ntohs(a->sockaddr.in6.sin6_port));
260 *p = ret;
261 return 0;
262 }
263
264 case AF_UNIX: {
265 char *ret;
266
267 if (a->size <= sizeof(sa_family_t)) {
268
269 if (!(ret = strdup("<unamed>")))
270 return -ENOMEM;
271
272 } else if (a->sockaddr.un.sun_path[0] == 0) {
273 /* abstract */
274
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 */
279
280 if (!(ret = new(char, sizeof(a->sockaddr.un.sun_path)+1)))
281 return -ENOMEM;
282
1c24e7bd 283 ret[0] = '@';
42f4e3c4
LP
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;
286
287 } else {
288
289 if (!(ret = strdup(a->sockaddr.un.sun_path)))
290 return -ENOMEM;
291 }
292
293 *p = ret;
294 return 0;
295 }
296
297 default:
298 return -EINVAL;
299 }
300}
301
acbb0225
LP
302int socket_address_listen(const SocketAddress *a, int backlog, SocketAddressBindIPv6Only only, const char *bind_to_device, int *ret) {
303 int r, fd, one;
42f4e3c4 304 assert(a);
83c60c9f 305 assert(ret);
42f4e3c4 306
542563ba 307 if ((r = socket_address_verify(a)) < 0)
42f4e3c4
LP
308 return r;
309
542563ba 310 if ((fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)) < 0)
42f4e3c4
LP
311 return -errno;
312
542563ba
LP
313 if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
314 int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
315
acbb0225
LP
316 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
317 goto fail;
542563ba
LP
318 }
319
acbb0225
LP
320 if (bind_to_device)
321 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
322 goto fail;
323
324 one = 1;
325 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
326 goto fail;
327
328 if (bind(fd, &a->sockaddr.sa, a->size) < 0)
329 goto fail;
42f4e3c4
LP
330
331 if (a->type == SOCK_STREAM)
acbb0225
LP
332 if (listen(fd, backlog) < 0)
333 goto fail;
42f4e3c4 334
83c60c9f 335 *ret = fd;
42f4e3c4 336 return 0;
acbb0225
LP
337
338fail:
339 r = -errno;
340 close_nointr(fd);
341 return r;
42f4e3c4 342}