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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#include "pdesc.h"
#include "psock.h"
#include <errno.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, size_t packet_buffer_size)
{
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->current.packet.max = packet_buffer_size;
psock->current.packet.used = 0;
psock->current.packet.buffer = (uint8_t *)calloc(packet_buffer_size, sizeof(*psock->current.packet.buffer));
if (psock->current.packet.buffer == NULL) {
goto error;
}
psock->remotes.max = max_descriptors;
psock->remotes.used = 0;
psock->remotes.descriptors = (struct pdesc *)calloc(max_descriptors, sizeof(*psock->remotes.descriptors));
if (psock->remotes.descriptors == NULL) {
goto error;
}
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_process_cmsg(struct msghdr * hdr)
{
for (struct cmsghdr * cmsg = CMSG_FIRSTHDR(hdr); cmsg != NULL; cmsg = CMSG_NXTHDR(hdr, cmsg)) {
printf("CMSG TYPE/LEVEL/LEN: %d / %d / %zu\n", cmsg->cmsg_type, cmsg->cmsg_level, cmsg->cmsg_len);
}
}
static int psock_recvmsg(struct psock * psock)
{
struct iovec iov;
struct msghdr hdr = {};
ssize_t nread;
iov.iov_base = (void *)psock->current.packet.buffer;
iov.iov_len = psock->current.packet.max;
hdr.msg_name = &psock->current.peer;
hdr.msg_namelen = sizeof(psock->current.peer);
hdr.msg_iov = &iov;
hdr.msg_iovlen = 1;
do {
nread = recvmsg(psock->icmp_fd, &hdr, 0);
} while (nread == -1 && errno == EINTR);
if (nread >= 0) {
psock->current.packet.used = nread;
psock_process_cmsg(&hdr);
return 0;
} else {
psock->current.packet.used = 0;
return -1;
}
}
static int psock_sendmsg(struct psock * psock, uint8_t * buf, size_t siz)
{
struct iovec iov;
struct msghdr hdr = {};
ssize_t n = siz, nwritten;
iov.iov_base = buf;
iov.iov_len = siz;
hdr.msg_name = &psock->current.peer;
hdr.msg_namelen = sizeof(psock->current.peer);
hdr.msg_iov = &iov;
hdr.msg_iovlen = 1;
nwritten = sendmsg(psock->icmp_fd, &hdr, 0);
return (nwritten == n ? 0 : nwritten);
}
static void psock_handle_events(struct psock * psock)
{
if (psock_recvmsg(psock) == 0) {
struct pdesc * remote = pdesc_find_remote(psock);
if (remote != NULL) {
printf("Remote descriptor ID: %u\n", remote->identifier);
} else {
fprintf(stderr, "Could not find/alloc a remote descriptor.\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;
}
}
}
|