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