aboutsummaryrefslogtreecommitdiff
path: root/src/psock.c
blob: ca6f95d5b5a6fee12f46e15b1f4680624cd3e562 (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
#include "pdesc.h"
#include "psock.h"

#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <unistd.h>


int psock_init(struct psock * psock, size_t max_descriptors)
{
    struct epoll_event ev;

    memset(psock, 0, sizeof(*psock));

    psock->icmp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    if (psock->icmp_fd < 0)
    {
        goto error;
    }

    psock->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
    if (psock->epoll_fd < 0)
    {
        goto error;
    }

    ev.events = EPOLLIN;
    ev.data.fd = psock->icmp_fd;
    if (epoll_ctl(psock->epoll_fd, EPOLL_CTL_ADD, psock->icmp_fd, &ev) != 0)
    {
        goto error;
    }

    psock->remotes.max = max_descriptors;
    psock->remotes.used = 0;
    psock->remotes.descriptors = (struct pdesc **)calloc(max_descriptors, sizeof(**psock->remotes.descriptors));

    return 0;
error:
    if (errno != 0)
    {
        perror("[FATAL] psock_init failed");
    }
    psock_free(psock);
    return -1;
}

void psock_free(struct psock * psock)
{
    free(psock->remotes.descriptors);
    psock->remotes.descriptors = NULL;
    psock->remotes.used = 0;
    psock->remotes.max = 0;

    close(psock->icmp_fd);
    psock->icmp_fd = -1;

    close(psock->epoll_fd);
    psock->epoll_fd = -1;
}

static void psock_handle_events(struct psock * psock)
{
    printf("!!!!!!\n");
}

void psock_loop(struct psock * psock)
{
    const int max_events = 32;
    struct epoll_event events[max_events];

    while (1)
    {
        int nready = epoll_wait(psock->epoll_fd, events, max_events, -1);

        switch (nready)
        {
            case -1:
                break;
            case 0:
                continue;
            default:
                psock_handle_events(psock);
                break;
        }
    }
}