forked from mc36/freeRtr
/
pcapInt.c
205 lines (185 loc) · 5.9 KB
/
pcapInt.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pcap.h>
char *ifaceName;
pcap_t *ifacePcap;
struct sockaddr_in addrLoc;
struct sockaddr_in addrRem;
int portLoc;
int portRem;
int commSock;
pthread_t threadUdp;
pthread_t threadRaw;
pthread_t threadPrint;
long int byteRx;
long int packRx;
long int byteTx;
long int packTx;
void err(char*buf) {
printf("%s\n", buf);
exit(1);
}
void gotRawPack(unsigned char*dummyparameter, const struct pcap_pkthdr *hdr, unsigned char *dat) {
int len = hdr->caplen;
packRx++;
byteRx += len;
send(commSock, dat, len, 0);
}
void doRawLoop() {
pcap_loop(ifacePcap, 0, (pcap_handler) gotRawPack, NULL);
err("raw thread exited");
}
void doUdpLoop() {
unsigned char bufD[16384];
int bufS;
for (;;) {
bufS = sizeof (bufD);
bufS = recv(commSock, bufD, bufS, 0);
if (bufS < 0) break;
packTx++;
byteTx += bufS;
pcap_sendpacket(ifacePcap, bufD, bufS);
}
err("udp thread exited");
}
void doMainLoop() {
unsigned char buf[1024];
doer:
printf("> ");
buf[0] = 0;
int i = scanf("%1023s", buf);
if (i < 1) {
sleep(1);
goto doer;
}
switch (buf[0]) {
case 0:
goto doer;
break;
case 'H':
case 'h':
case '?':
printf("commands:\n");
printf("h - this help\n");
printf("q - exit process\n");
printf("d - display counters\n");
printf("c - clear counters\n");
break;
case 'Q':
case 'q':
err("exiting");
break;
case 'D':
case 'd':
printf("iface counters:\n");
printf(" packets bytes\n");
printf("received %20li %20li\n", packRx, byteRx);
printf("sent %20li %20li\n", packTx, byteTx);
break;
case 'C':
case 'c':
printf("counters cleared.\n");
byteRx = 0;
packRx = 0;
byteTx = 0;
packTx = 0;
break;
default:
printf("unknown command '%s', try ?\n", buf);
break;
}
printf("\n");
goto doer;
}
int main(int argc, char **argv) {
char errbuf[PCAP_ERRBUF_SIZE + 1];
if (argc < 5) {
if (argc <= 1) goto help;
char*curr = argv[1];
pcap_if_t *pcifcs;
if ((curr[0] == '-') || (curr[0] == '/')) curr++;
switch (curr[0]) {
case 'V':
case 'v':
err("pcap interface driver v1.0");
break;
case 'L':
case 'l':
if (pcap_findalldevs(&pcifcs, errbuf) == -1) err("unable to find all interfaces");
printf("list of interfaces:\n");
int i = 0;
for (; pcifcs; pcifcs = pcifcs->next) {
i++;
printf("interface #%i:\n", i);
printf(" name: %s\n", pcifcs->name);
if (pcifcs->description != NULL) printf(" desc: %s\n", pcifcs->description);
}
if (i < 1) err("no interfaces found");
break;
case '?':
case 'h':
case 'H':
help :
curr = argv[0];
printf("using: %s <iface> <lport> <raddr> <rport> [laddr]\n", curr);
printf(" or: %s <command>\n", curr);
printf("commands: l=list interfaces\n");
printf(" v=version\n");
printf(" h=this help\n");
exit(1);
break;
default:
err("unknown command, try -h");
break;
}
exit(1);
}
portLoc = atoi(argv[2]);
portRem = atoi(argv[4]);
memset(&addrLoc, 0, sizeof (addrLoc));
memset(&addrRem, 0, sizeof (addrRem));
if (inet_aton(argv[3], &addrRem.sin_addr) == 0) err("bad raddr address");
if (argc > 5) {
if (inet_aton(argv[5], &addrLoc.sin_addr) == 0) err("bad laddr address");
} else {
addrLoc.sin_addr.s_addr = htonl(INADDR_ANY);
}
addrLoc.sin_family = AF_INET;
addrLoc.sin_port = htons(portLoc);
addrRem.sin_family = AF_INET;
addrRem.sin_port = htons(portRem);
if ((commSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) err("unable to open socket");
if (bind(commSock, (struct sockaddr *) &addrLoc, sizeof (addrLoc)) < 0) err("failed to bind socket");
printf("binded to local port %s %i.\n", inet_ntoa(addrLoc.sin_addr), portLoc);
if (connect(commSock, (struct sockaddr *) &addrRem, sizeof (addrRem)) < 0) err("failed to connect socket");
printf("will send to %s %i.\n", inet_ntoa(addrRem.sin_addr), portRem);
int sockOpt = 524288;
if (setsockopt(commSock, SOL_SOCKET, SO_RCVBUF, &sockOpt, sizeof(sockOpt)) < 0) err("failed to set socket rxbuf");
if (setsockopt(commSock, SOL_SOCKET, SO_SNDBUF, &sockOpt, sizeof(sockOpt)) < 0) err("failed to set socket txbuf");
printf("pcap version: %s\n", pcap_lib_version());
ifaceName = malloc(strlen(argv[1]) + 1);
if (ifaceName == NULL) err("error allocating memory");
strcpy(ifaceName, argv[1]);
printf("opening interface %s\n", ifaceName);
ifacePcap = pcap_create(ifaceName, errbuf);
if (ifacePcap == NULL) err("unable to open interface");
if (pcap_set_snaplen(ifacePcap, 65536) < 0) err("unable to set snaplen");
if (pcap_set_promisc(ifacePcap, 1) < 0) err("unable to set promisc");
if (pcap_set_immediate_mode(ifacePcap, 1) < 0) err("unable to set immediate");
if (pcap_activate(ifacePcap) < 0) err("activation failed");
if (pcap_setdirection(ifacePcap, PCAP_D_IN) < 0) err("unable to set direction");
printf("serving others\n");
byteRx = 0;
packRx = 0;
byteTx = 0;
packTx = 0;
if (pthread_create(&threadRaw, NULL, (void*) & doRawLoop, NULL)) err("error creating raw thread");
if (pthread_create(&threadUdp, NULL, (void*) & doUdpLoop, NULL)) err("error creating udp thread");
doMainLoop();
}