]>
Commit | Line | Data |
---|---|---|
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> |
b5a0699f LP |
30 | #include <sys/types.h> |
31 | #include <sys/stat.h> | |
42f4e3c4 LP |
32 | |
33 | #include "macro.h" | |
34 | #include "util.h" | |
35 | #include "socket-util.h" | |
36 | ||
542563ba | 37 | int socket_address_parse(SocketAddress *a, const char *s) { |
42f4e3c4 LP |
38 | int r; |
39 | char *e, *n; | |
40 | unsigned u; | |
41 | ||
42 | assert(a); | |
43 | assert(s); | |
44 | ||
9152c765 | 45 | zero(*a); |
542563ba | 46 | a->type = SOCK_STREAM; |
42f4e3c4 LP |
47 | |
48 | if (*s == '[') { | |
49 | /* IPv6 in [x:.....:z]:p notation */ | |
50 | ||
51 | if (!(e = strchr(s+1, ']'))) | |
52 | return -EINVAL; | |
53 | ||
54 | if (!(n = strndup(s+1, e-s-1))) | |
55 | return -ENOMEM; | |
56 | ||
57 | errno = 0; | |
58 | if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0) { | |
59 | free(n); | |
60 | return errno != 0 ? -errno : -EINVAL; | |
61 | } | |
62 | ||
63 | free(n); | |
64 | ||
65 | e++; | |
66 | if (*e != ':') | |
67 | return -EINVAL; | |
68 | ||
69 | e++; | |
70 | if ((r = safe_atou(e, &u)) < 0) | |
71 | return r; | |
72 | ||
73 | if (u <= 0 || u > 0xFFFF) | |
74 | return -EINVAL; | |
75 | ||
76 | a->sockaddr.in6.sin6_family = AF_INET6; | |
77 | a->sockaddr.in6.sin6_port = htons((uint16_t) u); | |
78 | a->size = sizeof(struct sockaddr_in6); | |
42f4e3c4 LP |
79 | |
80 | } else if (*s == '/') { | |
81 | /* AF_UNIX socket */ | |
82 | ||
83 | size_t l; | |
84 | ||
85 | l = strlen(s); | |
86 | if (l >= sizeof(a->sockaddr.un.sun_path)) | |
87 | return -EINVAL; | |
88 | ||
89 | a->sockaddr.un.sun_family = AF_UNIX; | |
90 | memcpy(a->sockaddr.un.sun_path, s, l); | |
91 | a->size = sizeof(sa_family_t) + l + 1; | |
92 | ||
1c24e7bd | 93 | } else if (*s == '@') { |
42f4e3c4 LP |
94 | /* Abstract AF_UNIX socket */ |
95 | size_t l; | |
96 | ||
97 | l = strlen(s+1); | |
98 | if (l >= sizeof(a->sockaddr.un.sun_path) - 1) | |
99 | return -EINVAL; | |
100 | ||
101 | a->sockaddr.un.sun_family = AF_UNIX; | |
102 | memcpy(a->sockaddr.un.sun_path+1, s+1, l); | |
103 | a->size = sizeof(struct sockaddr_un); | |
104 | ||
105 | } else { | |
106 | ||
107 | if ((e = strchr(s, ':'))) { | |
542563ba LP |
108 | |
109 | if ((r = safe_atou(e+1, &u)) < 0) | |
110 | return r; | |
111 | ||
112 | if (u <= 0 || u > 0xFFFF) | |
113 | return -EINVAL; | |
42f4e3c4 | 114 | |
42f4e3c4 LP |
115 | if (!(n = strndup(s, e-s))) |
116 | return -ENOMEM; | |
117 | ||
542563ba LP |
118 | /* IPv4 in w.x.y.z:p notation? */ |
119 | if ((r = inet_pton(AF_INET, n, &a->sockaddr.in4.sin_addr)) < 0) { | |
42f4e3c4 | 120 | free(n); |
542563ba | 121 | return -errno; |
42f4e3c4 LP |
122 | } |
123 | ||
542563ba LP |
124 | if (r > 0) { |
125 | /* Gotcha, it's a traditional IPv4 address */ | |
126 | free(n); | |
42f4e3c4 | 127 | |
542563ba LP |
128 | a->sockaddr.in4.sin_family = AF_INET; |
129 | a->sockaddr.in4.sin_port = htons((uint16_t) u); | |
130 | a->size = sizeof(struct sockaddr_in); | |
131 | } else { | |
132 | unsigned idx; | |
42f4e3c4 | 133 | |
acbb0225 LP |
134 | if (strlen(n) > IF_NAMESIZE-1) { |
135 | free(n); | |
136 | return -EINVAL; | |
137 | } | |
138 | ||
542563ba LP |
139 | /* Uh, our last resort, an interface name */ |
140 | idx = if_nametoindex(n); | |
141 | free(n); | |
142 | ||
83c60c9f | 143 | if (idx == 0) |
542563ba | 144 | return -EINVAL; |
42f4e3c4 | 145 | |
542563ba LP |
146 | a->sockaddr.in6.sin6_family = AF_INET6; |
147 | a->sockaddr.in6.sin6_port = htons((uint16_t) u); | |
148 | a->sockaddr.in6.sin6_scope_id = idx; | |
83c60c9f | 149 | a->sockaddr.in6.sin6_addr = in6addr_any; |
542563ba | 150 | a->size = sizeof(struct sockaddr_in6); |
acbb0225 | 151 | |
542563ba | 152 | } |
42f4e3c4 LP |
153 | } else { |
154 | ||
155 | /* Just a port */ | |
156 | if ((r = safe_atou(s, &u)) < 0) | |
157 | return r; | |
158 | ||
159 | if (u <= 0 || u > 0xFFFF) | |
160 | return -EINVAL; | |
161 | ||
162 | a->sockaddr.in6.sin6_family = AF_INET6; | |
42f4e3c4 | 163 | a->sockaddr.in6.sin6_port = htons((uint16_t) u); |
83c60c9f | 164 | a->sockaddr.in6.sin6_addr = in6addr_any; |
42f4e3c4 LP |
165 | a->size = sizeof(struct sockaddr_in6); |
166 | } | |
167 | } | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
542563ba | 172 | int socket_address_verify(const SocketAddress *a) { |
42f4e3c4 LP |
173 | assert(a); |
174 | ||
542563ba | 175 | switch (socket_address_family(a)) { |
42f4e3c4 LP |
176 | case AF_INET: |
177 | if (a->size != sizeof(struct sockaddr_in)) | |
178 | return -EINVAL; | |
179 | ||
180 | if (a->sockaddr.in4.sin_port == 0) | |
181 | return -EINVAL; | |
182 | ||
183 | return 0; | |
184 | ||
185 | case AF_INET6: | |
186 | if (a->size != sizeof(struct sockaddr_in6)) | |
187 | return -EINVAL; | |
188 | ||
189 | if (a->sockaddr.in6.sin6_port == 0) | |
190 | return -EINVAL; | |
191 | ||
192 | return 0; | |
193 | ||
194 | case AF_UNIX: | |
195 | if (a->size < sizeof(sa_family_t)) | |
196 | return -EINVAL; | |
197 | ||
198 | if (a->size > sizeof(sa_family_t)) { | |
199 | ||
200 | if (a->sockaddr.un.sun_path[0] == 0) { | |
201 | /* abstract */ | |
202 | if (a->size != sizeof(struct sockaddr_un)) | |
203 | return -EINVAL; | |
204 | } else { | |
205 | char *e; | |
206 | ||
207 | /* path */ | |
208 | if (!(e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path)))) | |
209 | return -EINVAL; | |
210 | ||
211 | if (a->size != sizeof(sa_family_t) + (e - a->sockaddr.un.sun_path) + 1) | |
212 | return -EINVAL; | |
213 | } | |
214 | } | |
215 | ||
216 | return 0; | |
217 | ||
218 | default: | |
219 | return -EAFNOSUPPORT; | |
220 | } | |
221 | } | |
222 | ||
542563ba | 223 | int socket_address_print(const SocketAddress *a, char **p) { |
42f4e3c4 LP |
224 | int r; |
225 | assert(a); | |
226 | assert(p); | |
227 | ||
542563ba | 228 | if ((r = socket_address_verify(a)) < 0) |
42f4e3c4 LP |
229 | return r; |
230 | ||
542563ba | 231 | switch (socket_address_family(a)) { |
42f4e3c4 LP |
232 | case AF_INET: { |
233 | char *ret; | |
234 | ||
235 | if (!(ret = new(char, INET_ADDRSTRLEN+1+5+1))) | |
236 | return -ENOMEM; | |
237 | ||
238 | if (!inet_ntop(AF_INET, &a->sockaddr.in4.sin_addr, ret, INET_ADDRSTRLEN)) { | |
239 | free(ret); | |
240 | return -errno; | |
241 | } | |
242 | ||
243 | sprintf(strchr(ret, 0), ":%u", ntohs(a->sockaddr.in4.sin_port)); | |
244 | *p = ret; | |
245 | return 0; | |
246 | } | |
247 | ||
248 | case AF_INET6: { | |
249 | char *ret; | |
250 | ||
251 | if (!(ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1))) | |
252 | return -ENOMEM; | |
253 | ||
254 | ret[0] = '['; | |
255 | if (!inet_ntop(AF_INET6, &a->sockaddr.in6.sin6_addr, ret+1, INET6_ADDRSTRLEN)) { | |
256 | free(ret); | |
257 | return -errno; | |
258 | } | |
259 | ||
260 | sprintf(strchr(ret, 0), "]:%u", ntohs(a->sockaddr.in6.sin6_port)); | |
261 | *p = ret; | |
262 | return 0; | |
263 | } | |
264 | ||
265 | case AF_UNIX: { | |
266 | char *ret; | |
267 | ||
268 | if (a->size <= sizeof(sa_family_t)) { | |
269 | ||
270 | if (!(ret = strdup("<unamed>"))) | |
271 | return -ENOMEM; | |
272 | ||
273 | } else if (a->sockaddr.un.sun_path[0] == 0) { | |
274 | /* abstract */ | |
275 | ||
276 | /* FIXME: We assume we can print the | |
277 | * socket path here and that it hasn't | |
278 | * more than one NUL byte. That is | |
279 | * actually an invalid assumption */ | |
280 | ||
281 | if (!(ret = new(char, sizeof(a->sockaddr.un.sun_path)+1))) | |
282 | return -ENOMEM; | |
283 | ||
1c24e7bd | 284 | ret[0] = '@'; |
42f4e3c4 LP |
285 | memcpy(ret+1, a->sockaddr.un.sun_path+1, sizeof(a->sockaddr.un.sun_path)-1); |
286 | ret[sizeof(a->sockaddr.un.sun_path)] = 0; | |
287 | ||
288 | } else { | |
289 | ||
290 | if (!(ret = strdup(a->sockaddr.un.sun_path))) | |
291 | return -ENOMEM; | |
292 | } | |
293 | ||
294 | *p = ret; | |
295 | return 0; | |
296 | } | |
297 | ||
298 | default: | |
299 | return -EINVAL; | |
300 | } | |
301 | } | |
302 | ||
b5a0699f LP |
303 | int socket_address_listen( |
304 | const SocketAddress *a, | |
305 | int backlog, | |
306 | SocketAddressBindIPv6Only only, | |
307 | const char *bind_to_device, | |
308 | mode_t directory_mode, | |
309 | mode_t socket_mode, | |
310 | int *ret) { | |
311 | ||
acbb0225 | 312 | int r, fd, one; |
42f4e3c4 | 313 | assert(a); |
83c60c9f | 314 | assert(ret); |
42f4e3c4 | 315 | |
542563ba | 316 | if ((r = socket_address_verify(a)) < 0) |
42f4e3c4 LP |
317 | return r; |
318 | ||
4f2d528d | 319 | if ((fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK, 0)) < 0) |
42f4e3c4 LP |
320 | return -errno; |
321 | ||
542563ba LP |
322 | if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) { |
323 | int flag = only == SOCKET_ADDRESS_IPV6_ONLY; | |
324 | ||
acbb0225 LP |
325 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0) |
326 | goto fail; | |
542563ba LP |
327 | } |
328 | ||
acbb0225 LP |
329 | if (bind_to_device) |
330 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0) | |
331 | goto fail; | |
332 | ||
333 | one = 1; | |
334 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) | |
335 | goto fail; | |
336 | ||
b5a0699f LP |
337 | if (socket_address_family(a) == AF_UNIX && a->sockaddr.un.sun_path[0] != 0) { |
338 | mode_t old_mask; | |
339 | ||
340 | /* Create parents */ | |
341 | mkdir_parents(a->sockaddr.un.sun_path, directory_mode); | |
342 | ||
343 | /* Enforce the right access mode for the socket*/ | |
344 | old_mask = umask(~ socket_mode); | |
345 | ||
346 | /* Include the original umask in our mask */ | |
347 | umask(~socket_mode | old_mask); | |
348 | ||
349 | r = bind(fd, &a->sockaddr.sa, a->size); | |
350 | ||
351 | if (r < 0 && errno == EADDRINUSE) { | |
352 | /* Unlink and try again */ | |
353 | unlink(a->sockaddr.un.sun_path); | |
354 | r = bind(fd, &a->sockaddr.sa, a->size); | |
355 | } | |
356 | ||
357 | umask(old_mask); | |
358 | } else | |
359 | r = bind(fd, &a->sockaddr.sa, a->size); | |
360 | ||
361 | if (r < 0) | |
acbb0225 | 362 | goto fail; |
42f4e3c4 LP |
363 | |
364 | if (a->type == SOCK_STREAM) | |
acbb0225 LP |
365 | if (listen(fd, backlog) < 0) |
366 | goto fail; | |
42f4e3c4 | 367 | |
83c60c9f | 368 | *ret = fd; |
42f4e3c4 | 369 | return 0; |
acbb0225 LP |
370 | |
371 | fail: | |
372 | r = -errno; | |
373 | close_nointr(fd); | |
374 | return r; | |
42f4e3c4 | 375 | } |
4f2d528d LP |
376 | |
377 | bool socket_address_can_accept(const SocketAddress *a) { | |
378 | assert(a); | |
379 | ||
380 | return | |
381 | a->type == SOCK_STREAM || | |
382 | a->type == SOCK_SEQPACKET; | |
383 | } |