aboutsummaryrefslogtreecommitdiff
path: root/src/socket.c
blob: cf4866173f3e525d235ea6a3844871aeaebf4ae5 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <assert.h>

#include "socket.h"


static int socket_nonblock(const psocket *psocket)
{
    int flags;

    flags = fcntl(psocket->fd, F_GETFL, 0);
    if (flags < 0)
        return 1;
    flags |= O_NONBLOCK;
    if (fcntl(psocket->fd, F_SETFL, flags) == -1)
        return 1;
    return 0;
}

int socket_init_in(psocket *psocket, const char *listen_addr,
                   const char *listen_port, struct addrinfo **results)
{
    struct addrinfo hints;

    assert(psocket);
    assert(listen_addr || listen_port); /* getaddrinfo wants either node or service */

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC; /* IPV4 && IPV6 */
    hints.ai_socktype = SOCK_STREAM; /* TCP */
    hints.ai_flags = AI_PASSIVE; /* all interfaces */

    return getaddrinfo(listen_addr, listen_port, &hints, results);
}

int socket_bind_in(psocket *psocket, struct addrinfo *results)
{
    int fd = -1, rv, reuse_enable = 1;
    struct addrinfo *rp = NULL;

    assert(psocket && results);

    for (rp = results; rp != NULL; rp = rp->ai_next) {
        fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (fd < 0)
            continue;
        rv = bind(fd, rp->ai_addr, rp->ai_addrlen);
        if (!rv)
            break;
        close(fd);
    }

    if (!rp)
        return -1;

    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_enable, sizeof(int)) < 0)
        return -2;
    psocket->fd = fd;
    psocket->addr = *rp;
    freeaddrinfo(results);
    return socket_nonblock(psocket);
}

int socket_listen_in(psocket *psocket)
{
    assert(psocket);

    return listen(psocket->fd, POTD_BACKLOG);
}

int socket_accept_in(const psocket *psocket, struct sockaddr_in *clientaddr)
{
    int fd;
    socklen_t alen = {0};

    assert(psocket && clientaddr);

    fd = accept(psocket->fd, (struct sockaddr*) clientaddr, &alen);
    if (fd < 0)
        return -1;
    if (socket_nonblock(psocket))
        return -2;

    return fd;
}

int socket_addrtostr_in(const struct sockaddr_in *in_addr,
                        char hbuf[NI_MAXHOST], char sbuf[NI_MAXSERV])
{
    int s;

    assert(in_addr);
    s = getnameinfo((const struct sockaddr *) in_addr,
                    sizeof(struct sockaddr_in),
                    &hbuf[0], NI_MAXHOST,
                    &sbuf[0],NI_MAXSERV,
                    NI_NUMERICHOST | NI_NUMERICSERV);
    return s;
}