]> git.ipfire.org Git - people/ms/systemd.git/blob - socket-util.c
build: basic autoconfization
[people/ms/systemd.git] / socket-util.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
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
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>
29 #include <net/if.h>
30
31 #include "macro.h"
32 #include "util.h"
33 #include "socket-util.h"
34
35 int socket_address_parse(SocketAddress *a, const char *s) {
36 int r;
37 char *e, *n;
38 unsigned u;
39
40 assert(a);
41 assert(s);
42
43 zero(*a);
44 a->type = SOCK_STREAM;
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);
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
91 } else if (*s == '@') {
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, ':'))) {
106
107 if ((r = safe_atou(e+1, &u)) < 0)
108 return r;
109
110 if (u <= 0 || u > 0xFFFF)
111 return -EINVAL;
112
113 if (!(n = strndup(s, e-s)))
114 return -ENOMEM;
115
116 /* IPv4 in w.x.y.z:p notation? */
117 if ((r = inet_pton(AF_INET, n, &a->sockaddr.in4.sin_addr)) < 0) {
118 free(n);
119 return -errno;
120 }
121
122 if (r > 0) {
123 /* Gotcha, it's a traditional IPv4 address */
124 free(n);
125
126 a->sockaddr.in4.sin_family = AF_INET;
127 a->sockaddr.in4.sin_port = htons((uint16_t) u);
128 a->size = sizeof(struct sockaddr_in);
129 } else {
130 unsigned idx;
131
132 if (strlen(n) > IF_NAMESIZE-1) {
133 free(n);
134 return -EINVAL;
135 }
136
137 /* Uh, our last resort, an interface name */
138 idx = if_nametoindex(n);
139 free(n);
140
141 if (idx == 0)
142 return -EINVAL;
143
144 a->sockaddr.in6.sin6_family = AF_INET6;
145 a->sockaddr.in6.sin6_port = htons((uint16_t) u);
146 a->sockaddr.in6.sin6_scope_id = idx;
147 a->sockaddr.in6.sin6_addr = in6addr_any;
148 a->size = sizeof(struct sockaddr_in6);
149
150 }
151 } else {
152
153 /* Just a port */
154 if ((r = safe_atou(s, &u)) < 0)
155 return r;
156
157 if (u <= 0 || u > 0xFFFF)
158 return -EINVAL;
159
160 a->sockaddr.in6.sin6_family = AF_INET6;
161 a->sockaddr.in6.sin6_port = htons((uint16_t) u);
162 a->sockaddr.in6.sin6_addr = in6addr_any;
163 a->size = sizeof(struct sockaddr_in6);
164 }
165 }
166
167 return 0;
168 }
169
170 int socket_address_verify(const SocketAddress *a) {
171 assert(a);
172
173 switch (socket_address_family(a)) {
174 case AF_INET:
175 if (a->size != sizeof(struct sockaddr_in))
176 return -EINVAL;
177
178 if (a->sockaddr.in4.sin_port == 0)
179 return -EINVAL;
180
181 return 0;
182
183 case AF_INET6:
184 if (a->size != sizeof(struct sockaddr_in6))
185 return -EINVAL;
186
187 if (a->sockaddr.in6.sin6_port == 0)
188 return -EINVAL;
189
190 return 0;
191
192 case AF_UNIX:
193 if (a->size < sizeof(sa_family_t))
194 return -EINVAL;
195
196 if (a->size > sizeof(sa_family_t)) {
197
198 if (a->sockaddr.un.sun_path[0] == 0) {
199 /* abstract */
200 if (a->size != sizeof(struct sockaddr_un))
201 return -EINVAL;
202 } else {
203 char *e;
204
205 /* path */
206 if (!(e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path))))
207 return -EINVAL;
208
209 if (a->size != sizeof(sa_family_t) + (e - a->sockaddr.un.sun_path) + 1)
210 return -EINVAL;
211 }
212 }
213
214 return 0;
215
216 default:
217 return -EAFNOSUPPORT;
218 }
219 }
220
221 int socket_address_print(const SocketAddress *a, char **p) {
222 int r;
223 assert(a);
224 assert(p);
225
226 if ((r = socket_address_verify(a)) < 0)
227 return r;
228
229 switch (socket_address_family(a)) {
230 case AF_INET: {
231 char *ret;
232
233 if (!(ret = new(char, INET_ADDRSTRLEN+1+5+1)))
234 return -ENOMEM;
235
236 if (!inet_ntop(AF_INET, &a->sockaddr.in4.sin_addr, ret, INET_ADDRSTRLEN)) {
237 free(ret);
238 return -errno;
239 }
240
241 sprintf(strchr(ret, 0), ":%u", ntohs(a->sockaddr.in4.sin_port));
242 *p = ret;
243 return 0;
244 }
245
246 case AF_INET6: {
247 char *ret;
248
249 if (!(ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1)))
250 return -ENOMEM;
251
252 ret[0] = '[';
253 if (!inet_ntop(AF_INET6, &a->sockaddr.in6.sin6_addr, ret+1, INET6_ADDRSTRLEN)) {
254 free(ret);
255 return -errno;
256 }
257
258 sprintf(strchr(ret, 0), "]:%u", ntohs(a->sockaddr.in6.sin6_port));
259 *p = ret;
260 return 0;
261 }
262
263 case AF_UNIX: {
264 char *ret;
265
266 if (a->size <= sizeof(sa_family_t)) {
267
268 if (!(ret = strdup("<unamed>")))
269 return -ENOMEM;
270
271 } else if (a->sockaddr.un.sun_path[0] == 0) {
272 /* abstract */
273
274 /* FIXME: We assume we can print the
275 * socket path here and that it hasn't
276 * more than one NUL byte. That is
277 * actually an invalid assumption */
278
279 if (!(ret = new(char, sizeof(a->sockaddr.un.sun_path)+1)))
280 return -ENOMEM;
281
282 ret[0] = '@';
283 memcpy(ret+1, a->sockaddr.un.sun_path+1, sizeof(a->sockaddr.un.sun_path)-1);
284 ret[sizeof(a->sockaddr.un.sun_path)] = 0;
285
286 } else {
287
288 if (!(ret = strdup(a->sockaddr.un.sun_path)))
289 return -ENOMEM;
290 }
291
292 *p = ret;
293 return 0;
294 }
295
296 default:
297 return -EINVAL;
298 }
299 }
300
301 int socket_address_listen(const SocketAddress *a, int backlog, SocketAddressBindIPv6Only only, const char *bind_to_device, int *ret) {
302 int r, fd, one;
303 assert(a);
304 assert(ret);
305
306 if ((r = socket_address_verify(a)) < 0)
307 return r;
308
309 if ((fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)) < 0)
310 return -errno;
311
312 if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
313 int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
314
315 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
316 goto fail;
317 }
318
319 if (bind_to_device)
320 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
321 goto fail;
322
323 one = 1;
324 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
325 goto fail;
326
327 if (bind(fd, &a->sockaddr.sa, a->size) < 0)
328 goto fail;
329
330 if (a->type == SOCK_STREAM)
331 if (listen(fd, backlog) < 0)
332 goto fail;
333
334 *ret = fd;
335 return 0;
336
337 fail:
338 r = -errno;
339 close_nointr(fd);
340 return r;
341 }