-
Notifications
You must be signed in to change notification settings - Fork 2
/
macnockserver.c
123 lines (97 loc) · 3.13 KB
/
macnockserver.c
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
#include "macnockserver.h"
#include "macstorage.h"
#include "nockpackage.h"
#include "log.h"
#include "tc.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
extern const char *g_hood;
extern const char *g_interface;
extern const int PORT;
extern const uint8_t VERSION;
static uint8_t stop;
int fd;
void macNockServer_stop()
{
log_debug("[s] Stopping Server\n");
stop = 1;
shutdown(fd, SHUT_RDWR);
}
void macNockServer_run()
{
stop = 0;
tc_start();
fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (fd < 0)
{
perror("[s] ERROR: Can't create socket");
return;
}
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, g_interface, strlen(g_interface)) < 0)
{
perror("[c] WARNING: Can't bind to device");
}
// allow to reuse the port immediately as soon as the service exits.
int sockoptval = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &sockoptval, sizeof(int)) != 0)
{
perror("[s] WARNING: Can't set socket options");
}
struct sockaddr_in6 my_addr;
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin6_family = AF_INET6;
my_addr.sin6_port = htons(PORT);
my_addr.sin6_addr = in6addr_any;
if (bind(fd, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
{
perror("[s] ERROR: Can't bind to address");
return;
}
while (!stop)
{
log_trace("[s] waiting for connection\n");
struct sockaddr_in6 client_addr;
socklen_t addrlen = sizeof(client_addr);
static const size_t BUFSIZE = 2048;
uint8_t buf[BUFSIZE];
int recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&client_addr, &addrlen);
if (recvlen <= 0)
{
continue;
}
char addrBuf[INET6_ADDRSTRLEN];
const char *ret = inet_ntop(AF_INET6, &client_addr.sin6_addr, addrBuf, sizeof(addrBuf));
log_trace("[s] received %d bytes from %s\n", recvlen, ret);
struct NockPackage *nock = (struct NockPackage *)buf;
nock->hood[nock->hoodLen] = '\0';
if (nock->version != VERSION)
{
fprintf(stderr, "Received package with wrong version from %s\n", ret);
continue;
}
log_trace("[s] The MAC: %02x:%02x:%02x:%02x:%02x:%02x, the Hood: %s\n",
nock->mac[0], nock->mac[1], nock->mac[2], nock->mac[3], nock->mac[4], nock->mac[5], nock->hood);
if (strcmp(nock->hood, g_hood) == 0)
{
log_trace("[s] allowing %02x:%02x:%02x:%02x:%02x:%02x\n",
nock->mac[0], nock->mac[1], nock->mac[2], nock->mac[3], nock->mac[4], nock->mac[5]);
macStorage_add(nock->mac);
}
else
{
log_debug("[s] Not allowing %02x:%02x:%02x:%02x:%02x:%02x. Wrong hood: \"%s\"\n",
nock->mac[0], nock->mac[1], nock->mac[2], nock->mac[3], nock->mac[4], nock->mac[5], nock->hood);
}
}
close(fd);
tc_stop();
log_debug("[s] Server closed\n");
return;
}