/src/strongswan/src/libstrongswan/networking/streams/stream_service_tcp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2013 Martin Willi |
3 | | * |
4 | | * Copyright (C) secunet Security Networks AG |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU General Public License as published by the |
8 | | * Free Software Foundation; either version 2 of the License, or (at your |
9 | | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, but |
12 | | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 | | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | | * for more details. |
15 | | */ |
16 | | |
17 | | #include <library.h> |
18 | | #include <networking/streams/stream_tcp.h> |
19 | | |
20 | | #include <errno.h> |
21 | | #include <unistd.h> |
22 | | #include <sys/stat.h> |
23 | | |
24 | | /** |
25 | | * See header |
26 | | */ |
27 | | stream_service_t *stream_service_create_tcp(char *uri, int backlog) |
28 | 0 | { |
29 | 0 | union { |
30 | 0 | struct sockaddr_in in; |
31 | 0 | struct sockaddr_in6 in6; |
32 | 0 | struct sockaddr sa; |
33 | 0 | } addr; |
34 | 0 | int fd, len, on = 1; |
35 | |
|
36 | 0 | len = stream_parse_uri_tcp(uri, &addr.sa); |
37 | 0 | if (len == -1) |
38 | 0 | { |
39 | 0 | DBG1(DBG_NET, "invalid stream URI: '%s'", uri); |
40 | 0 | return NULL; |
41 | 0 | } |
42 | 0 | fd = socket(addr.sa.sa_family, SOCK_STREAM, 0); |
43 | 0 | if (fd < 0) |
44 | 0 | { |
45 | 0 | DBG1(DBG_NET, "opening socket '%s' failed: %s", uri, strerror(errno)); |
46 | 0 | return NULL; |
47 | 0 | } |
48 | 0 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) != 0) |
49 | 0 | { |
50 | 0 | DBG1(DBG_NET, "SO_REUSADDR on '%s' failed: %s", uri, strerror(errno)); |
51 | 0 | } |
52 | 0 | if (bind(fd, &addr.sa, len) < 0) |
53 | 0 | { |
54 | 0 | DBG1(DBG_NET, "binding socket '%s' failed: %s", uri, strerror(errno)); |
55 | 0 | close(fd); |
56 | 0 | return NULL; |
57 | 0 | } |
58 | 0 | if (listen(fd, backlog) < 0) |
59 | 0 | { |
60 | 0 | DBG1(DBG_NET, "listen on socket '%s' failed: %s", uri, strerror(errno)); |
61 | 0 | close(fd); |
62 | 0 | return NULL; |
63 | 0 | } |
64 | 0 | return stream_service_create_from_fd(fd); |
65 | 0 | } |