1 /*
2 20131117
3 Jan Mojzis
4 Public domain.
5 */
6
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <sys/param.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include "hasipv6.h"
14 #include "blocking.h"
15 #include "e.h"
16 #include "xsocket.h"
17
xsocket_tcp(int type)18 int xsocket_tcp(int type) {
19
20 int s;
21 #ifdef HASIPV6
22 int opt = 1;
23 #endif
24
25 if (type == XSOCKET_V6) {
26 #ifdef HASIPV6
27 s = socket(PF_INET6, SOCK_STREAM, 0);
28 if (s == -1) return -1;
29 if (fcntl(s, F_SETFD, 1) == -1) { close(s); return -1; }
30 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof opt) == -1) { close(s); return -1; }
31 blocking_disable(s);
32 return s;
33 #endif
34 errno = EPROTONOSUPPORT;
35 return -1;
36 }
37
38 if (type == XSOCKET_V4) {
39 s = socket(PF_INET, SOCK_STREAM, 0);
40 if (s == -1) return -1;
41 if (fcntl(s, F_SETFD, 1) == -1) { close(s); return -1; }
42 blocking_disable(s);
43 return s;
44 }
45
46 errno = EPROTO;
47 return -1;
48 }
49