/
server.c
220 lines (173 loc) · 4.38 KB
/
server.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <linux/netfilter.h> /* for NF_ACCEPT */
#include <errno.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
#include "netfilterRedirect.h"
#define SOCK_PATH_S "serverSock"
static int redirect_pkt(struct nfq_data* tb, int sockdes)
{
int id = 0;
int len;
unsigned char* data;
struct nfqnl_msg_packet_hdr *ph;
package_with_header curr_package;
ph = nfq_get_msg_packet_hdr(tb);
if (ph) {
id = ntohl(ph->packet_id);
}
len = nfq_get_payload(tb, &data);
if (len < 0) {
perror("payload");
exit(1);
}
curr_package.packet_id = id;
printf("%i\n", id);
memcpy(curr_package.data, data, len);
if (send(sockdes, &curr_package, sizeof(package_with_header), 0) == -1) {
perror("send");
exit(1);
}
return id;
}
static void set_verdict(struct nfq_q_handle* qh, verdict_msg* msg) {
nfq_set_verdict(qh, msg->packet_id, msg->verdict, 0, NULL);
}
static int cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
struct nfq_data *nfa, void *data)
{
redirect_pkt(nfa, *((int*)data));
return 0;
}
int main(int argc, char **argv)
{
struct nfq_handle *h;
struct nfq_q_handle *qh;
fd_set sockets;
int fd;
int rv;
char buf[MAX_PACKET_SIZE] __attribute__ ((aligned));
int s, sock_out, len, t;
struct sockaddr_un local, remote;
if ((s = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH_S);
unlink(local.sun_path);
len = strlen(local.sun_path) + sizeof(local.sun_family);
if (bind(s, (struct sockaddr *)&local, len) == -1) {
perror("bind");
exit(1);
}
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
// unlink(remote.sun_path);
if (connect(s, (struct sockaddr*)&remote, sizeof(remote))) {
perror("connect");
exit(1);
}
/*
if (listen(s, 5) == -1) {
perror("listen");
exit(1);
}
*/
for(;;) {
printf("opening library handle\n");
h = nfq_open();
if (!h) {
fprintf(stderr, "error during nfq_open()\n");
exit(1);
}
printf("unbinding existing nf_queue handler for AF_INET (if any)\n");
if (nfq_unbind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_unbind_pf()\n");
exit(1);
}
printf("binding nfnetlink_queue as nf_queue handler for AF_INET\n");
if (nfq_bind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_bind_pf()\n");
exit(1);
}
printf("binding this socket to queue '0'\n");
qh = nfq_create_queue(h, 0, &cb, &s);
if (!qh) {
fprintf(stderr, "error during nfq_create_queue()\n");
exit(1);
}
printf("setting copy_packet mode\n");
if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
fprintf(stderr, "can't set packet_copy mode\n");
exit(1);
}
if (nfq_set_queue_maxlen(qh, 1024) < 0) {
perror("length");
}
fd = nfq_fd(h);
t = sizeof(remote);
/* if ((sock_out = accept(s, (struct sockaddr *)&remote, &t)) == -1) {
perror("accept");
exit(1);
}
*/
for (;;) {
int nready;
int i;
FD_ZERO(&sockets);
FD_SET(fd, &sockets);
FD_SET(s, &sockets);
int maxfd = MAX(fd, s);
nready = select(maxfd + 1, &sockets, NULL, NULL, NULL);
if (nready == -1) {
perror("select");
exit(1);
}
for(i=maxfd; i>= 0 && nready>0; i--)
{
if(FD_ISSET(i, &sockets))
{
nready--;
if (i == fd) {
if ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0) {
nfq_handle_packet(h, buf, rv);
continue;
}
/* if your application is too slow to digest the packets that
* are sent from kernel-space, the socket buffer that we use
* to enqueue packets may fill up returning ENOBUFS. Depending
* on your application, this error may be ignored. Please, see
* the doxygen documentation of this library on how to improve
* this situation.
*/
if (rv < 0 && errno == ENOBUFS) {
printf("losing packets!\n");
continue;
}
perror("recv failed");
break;
}
if (i == s) {
if ((rv = recv(s, buf, sizeof(buf), 0)) >= 0) {
set_verdict(qh, (verdict_msg*)buf);
continue;
}
}
}
}
}
}
printf("unbinding from queue 0\n");
nfq_destroy_queue(qh);
printf("closing library handle\n");
nfq_close(h);
close(s);
exit(0);
}