File: xsocket_tcp.c

package info (click to toggle)
dq 20230101-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,020 kB
  • sloc: ansic: 8,269; makefile: 363; sh: 176; python: 82
file content (48 lines) | stat: -rw-r--r-- 1,002 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
20131117
Jan Mojzis
Public domain.
*/

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "hasipv6.h"
#include "blocking.h"
#include "e.h"
#include "xsocket.h"

int xsocket_tcp(int type) {

    int s;
#ifdef HASIPV6
    int opt = 1;
#endif

    if (type == XSOCKET_V6) {
#ifdef HASIPV6
        s = socket(PF_INET6, SOCK_STREAM, 0);
        if (s == -1) return -1;
        if (fcntl(s, F_SETFD, 1) == -1) { close(s); return -1; }
        if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof opt) == -1) { close(s); return -1; }
        blocking_disable(s);
        return s;
#endif
        errno = EPROTONOSUPPORT;
        return -1;
    }

    if (type == XSOCKET_V4) {
        s = socket(PF_INET, SOCK_STREAM, 0);
        if (s == -1) return -1;
        if (fcntl(s, F_SETFD, 1) == -1) { close(s); return -1; }
        blocking_disable(s);
        return s;
    }

    errno = EPROTO;
    return -1;
}