chiark / gitweb /
license: add GPLv2+ license blurbs everwhere
[elogind.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                         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;
113
114                         if (!(n = strndup(s, e-s)))
115                                 return -ENOMEM;
116
117                         /* IPv4 in w.x.y.z:p notation? */
118                         if ((r = inet_pton(AF_INET, n, &a->sockaddr.in4.sin_addr)) < 0) {
119                                 free(n);
120                                 return -errno;
121                         }
122
123                         if (r > 0) {
124                                 /* Gotcha, it's a traditional IPv4 address */
125                                 free(n);
126
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;
132
133                                 if (strlen(n) > IF_NAMESIZE-1) {
134                                         free(n);
135                                         return -EINVAL;
136                                 }
137
138                                 /* Uh, our last resort, an interface name */
139                                 idx = if_nametoindex(n);
140                                 free(n);
141
142                                 if (idx == 0)
143                                         return -EINVAL;
144
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);
150
151                         }
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;
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);
165                 }
166         }
167
168         return 0;
169 }
170
171 int socket_address_verify(const SocketAddress *a) {
172         assert(a);
173
174         switch (socket_address_family(a)) {
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
222 int socket_address_print(const SocketAddress *a, char **p) {
223         int r;
224         assert(a);
225         assert(p);
226
227         if ((r = socket_address_verify(a)) < 0)
228                 return r;
229
230         switch (socket_address_family(a)) {
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
283                                 ret[0] = '@';
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
302 int socket_address_listen(const SocketAddress *a, int backlog, SocketAddressBindIPv6Only only, const char *bind_to_device, int *ret) {
303         int r, fd, one;
304         assert(a);
305         assert(ret);
306
307         if ((r = socket_address_verify(a)) < 0)
308                 return r;
309
310         if ((fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)) < 0)
311                 return -errno;
312
313         if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
314                 int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
315
316                 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
317                         goto fail;
318         }
319
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;
330
331         if (a->type == SOCK_STREAM)
332                 if (listen(fd, backlog) < 0)
333                         goto fail;
334
335         *ret = fd;
336         return 0;
337
338 fail:
339         r = -errno;
340         close_nointr(fd);
341         return r;
342 }