-
Notifications
You must be signed in to change notification settings - Fork 18
/
oipd.cpp
221 lines (197 loc) · 5.35 KB
/
oipd.cpp
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
/*
Copyright 2008 Utah State University
This file is part of OIP.
OIP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OIP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OIP. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <set>
using namespace std;
//#include <sys/time.h>
#include <SDL/SDL.h>
#include <stdlib.h>
#include <pcap.h>
#include "structs.h"
#include <arpa/inet.h>
#include "clientmanager.h"
#include <signal.h>
#define SNAPLEN 80
bool globalrun=true;
const char* sniff_dev = 0;
//const char* sniff_dev = "wlan0";
//can use insert(), erase()
set<SDL_Thread*> clients;
struct sniffargs
{
map<string, string> opts;
networkpm & pm;
sniffargs(networkpm&p, map<string, string> o):pm(p),opts(o) {}
};
void handle_packet(u_char * a, const struct pcap_pkthdr* header, const u_char* packet)
{
sniffargs* self = (sniffargs*)a;
const struct sniff_ethernet *ethernet;
const struct sniff_ip *ip = 0;
ethernet = (struct sniff_ethernet*)packet;
if (ntohs(ethernet->ether_type) == T_IP)
ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
if (ntohs(ethernet->ether_type) == T_VLAN)
{
struct sniff_ethernet_8021q* vethernet = (struct sniff_ethernet_8021q*)packet;
//cout << "read a packet from vlan " << VLANID(ntohs(vethernet->vlan_id)) << "\n";
if (ntohs(vethernet->ether_type) == T_IP)
{
// cout << "\tand it was an ip packet\n";
ip = (struct sniff_ip*)(packet + SIZE_8021Q);
}
}
bool clientrun = 1;
if (ip)
{
if (ip->ip_p == T_TCP) //could use header.len/1400.0 or something
clientrun = self->pm.addpacket(ip->ip_src, ip->ip_dst, 0xff00ff00, header->len);
else if (ip->ip_p == T_UDP)
clientrun = self->pm.addpacket(ip->ip_src, ip->ip_dst, 0xffff0000, header->len);
else
clientrun = self->pm.addpacket(ip->ip_src, ip->ip_dst, 0xff0000ff, header->len);
ip = 0;
}
}
int sniff(void* a)
{
sniffargs* self = (sniffargs*)a;
struct pcap_pkthdr* header;
const u_char* packet;
pcap_t * handle = NULL;
const struct sniff_ethernet *ethernet;
const struct sniff_ip *ip;
int result;
bool clientrun = true;
const char* dev = sniff_dev;
char errbuf[PCAP_ERRBUF_SIZE];
if (!dev)
dev = pcap_lookupdev(errbuf);
cout << "Looked up device: " << dev << "\n";
if (clientrun)
{
//open up the device
handle = pcap_open_live(dev, SNAPLEN, 1, 100, errbuf);
cout << "Starting sniffing on dev " << dev << "\n";
if (!handle)
{
cerr << "Unable to open " << dev << ": " << errbuf << endl;
clientrun = false;
}
if (pcap_setnonblock(handle, 1, errbuf))
{
cerr << "Unable to set non-blocking mode: " << errbuf << endl;
clientrun = false;
}
}
if (!dev)
{
cerr << "Unable to open default device: " << errbuf << endl;
cerr << "Attempting to use eth0\n";
dev = "eth0";
//dev = "wlan0";
}
if (self->opts["filter"] != "")
{
struct bpf_program fp;
if (pcap_compile(handle, &fp, self->opts["filter"].c_str(), 1, 0))
{
clientrun = false;
cerr << "Couldn't parse filter: '" << self->opts["filter"] << "': " << pcap_geterr(handle) << "\n";
}
else if (pcap_setfilter(handle, &fp))
{
clientrun = false;
cerr << "Couldn't set filter: '" << self->opts["filter"] << "': " << pcap_geterr(handle) << "\n";
}
}
int retry = 5;
while(globalrun && clientrun)
{
if (!self->pm.consumer)
clientrun = false; //lets not keep going if the consumer is gone
//handle up to 10 packets at a shot
result = pcap_dispatch(handle, 10, handle_packet, (u_char*)self);
if (result > 0) //it succeeded
retry = 5;
else if (result == 0)
{
SDL_Delay(10); //attempt to yield processor control, as we don't have data to process
}
else if (result == -1)
{
cerr << "Unable to read packet: " << pcap_geterr(handle) << "\n";
retry--;
if (retry <= 0)
clientrun = false;
}
else //other negative numbers are unknown, but indicate a reason to quit
{
retry--;
if (retry <= 0)
clientrun = false;
}
}
self->pm.producerdead(); //notify the world that we are quitting
//can't touch the packetmanager now, because the other thread could kill it at any time
if (handle)
pcap_close(handle);
delete self;
return 0;
}
void newclient(networkpm& pm, const map<string, string>& opts)
{
SDL_Thread* tid = SDL_CreateThread(sniff, new sniffargs(pm, opts));
if (tid)
clients.insert(tid);
}
void cleanup()
{
globalrun = false;
set<SDL_Thread*>::iterator i;
for (i = clients.begin(); i != clients.end(); ++i)
SDL_WaitThread(*i, NULL);
SDL_Quit();
}
bool setup()
{
if (SDL_Init(SDL_INIT_TIMER))
return false;
atexit(cleanup);
return true;
}
void sigcatch(int s)
{
cout << "Quitting...\n";
exit(0);
}
int main(int argc, char** argv)
{
cout << "Welcome\n";
if (argc > 1)
sniff_dev = argv[1];
cout << "Device: " << sniff_dev << "\n";
signal(SIGINT, sigcatch);
//get a device to read from
if (setup())
{
clientmanager server(newclient);
while(globalrun)
{
SDL_Delay(1000);
}
}
return 0;
}