-
Notifications
You must be signed in to change notification settings - Fork 0
/
sniffer.cc
276 lines (249 loc) · 7.33 KB
/
sniffer.cc
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <list>
#include "sniffer.h"
#include "pmanager.h"
CSniffer::CSniffer()
{
m_PcapHandler = NULL;
memset(m_ErrBuf, '\0', PCAP_ERRBUF_SIZE);
memset(m_DevName, '\0', 16);
memset(m_MACAddr, 0, 6);
m_Netmask = 0;
}
CSniffer::CSniffer(CSniffer const &sniffer)
{
m_PcapHandler = sniffer.m_PcapHandler;
memset(m_ErrBuf, '\0', PCAP_ERRBUF_SIZE);
memcpy(m_DevName, sniffer.m_DevName, 16);
memcpy(m_MACAddr, sniffer.m_MACAddr, 6);
m_Netmask = sniffer.m_Netmask;
}
CSniffer::CSniffer(char *devname, uint8_t *macaddr, uint32_t netmask)
{
m_PcapHandler = NULL;
memcpy(m_DevName, devname, sizeof(NICInfo));
memcpy(m_MACAddr, macaddr, 6);
m_Netmask = netmask;
memset(m_ErrBuf, '\0', PCAP_ERRBUF_SIZE);
}
CSniffer::~CSniffer()
{
// if(m_PcapHandler != NULL)
// StopCapture();
}
int CSniffer::StartCapture()
{
// pcap_open -> pcap_compile -> pcap_setfilter
// 위 과정을 진행하면 캡처 시작
// filter는 모든 패킷을 받기 위해 ""으로 셋팅
struct bpf_program fp;
char *filter = (char*)"not (port 11112)";
if(m_PcapHandler == NULL)
{
m_PcapHandler = pcap_open_live(m_DevName, PACKETBUF_SIZE, PROMISCUOUS, -1, m_ErrBuf );
}
else
{
fprintf(stderr, "Sniffer Error: already exist pcap handler\n");
return -1;
}
//m_PcapHandler = pcap_open_live("eth1", PACKETBUF_SIZE, PROMISCUOUS, -1, m_ErrBuf );
if(m_PcapHandler == NULL)
{
fprintf(stderr, "PCAP Error: %s\n", m_ErrBuf);
return -1;
}
if(pcap_compile(m_PcapHandler, &fp, filter, 0, m_Netmask) == -1)
{
fprintf(stderr, "PCAP Error: compile\n");
return -1;
}
if(pcap_setfilter(m_PcapHandler, &fp) == -1)
{
fprintf(stderr, "PCAP_Error: setfileter\n");
return -1;
}
//pcap_freecode(&fp);
}
void CSniffer::StopCapture()
{
// pcap_close를 통해 핸들 삭제로 캡처 종료
if(m_PcapHandler != NULL)
{
pcap_close(m_PcapHandler);
m_PcapHandler = NULL;
}
}
uint32_t CSniffer::GetNextPacket(uint8_t *data, struct pcap_pkthdr *pkt_hdr)
{
uint8_t *temp = (uint8_t*)pcap_next(m_PcapHandler, pkt_hdr);
if(temp == NULL)
{
return 0;
}
else
{
memcpy(data, (char *)m_MACAddr, 6);
memcpy(data + 6, (char *)pkt_hdr, sizeof(struct pcap_pkthdr));
memcpy(data + 6 + sizeof(struct pcap_pkthdr), temp, pkt_hdr->len);
return pkt_hdr->len;
}
}
//-----------------------------------------------
// CCaptureLoop
CCaptureLoop g_SnifferLoop;
CCaptureLoop::CCaptureLoop()
{
// mutex 생성
pthread_mutex_init(&m_CaptureMutex, NULL);
pthread_cond_init(&m_CaptureCond, NULL);
m_CaptureSenderThread = NULL;
m_SnifferNum = 0;
m_IsStop = false;
}
CCaptureLoop::~CCaptureLoop()
{
// mutex 해제
pthread_mutex_destroy(&m_CaptureMutex);
pthread_cond_destroy(&m_CaptureCond);
}
int CCaptureLoop::CreateCaptureThread()
{
int tid;
// 스레드 생성
m_IsStop = false;
tid = pthread_create(&m_CaptureSenderThread, NULL, CaptureLoop, (void *)this);
if(tid < 0)
{
fprintf(stderr, "Error: capture thread create fail,errno: %d\n", errno);
return -1;
}
}
void CCaptureLoop::StopCaptureThread()
{
int status;
// 캡처 스레드 정지
m_IsStop = true;
pthread_cond_signal(&m_CaptureCond);
sleep(0);
// Sniffer thread 종료 기다리기
pthread_join(m_CaptureSenderThread, (void **)&status);
}
// static CaptureLoop()
// 캡처 루프 함수
// @data: 파라미터
void *CCaptureLoop::CaptureLoop(void *data)
{
// 파라미터
CCaptureLoop *caploop = (CCaptureLoop *)data;
pthread_mutex_t *mlock = caploop->GetCaptureMutexPtr();
pthread_cond_t *mcond = caploop->GetCaptureCondPtr();
bool *stopptr = caploop->GetIsStopPtr();
int *sniffernum = caploop->GetSnifferNumPtr();
std::list<CSniffer> *snifferlist = caploop->GetSnifferListPtr();
std::list<CSniffer> *removesnifferlist = caploop->GetRemoveSnifferListPtr();
printf("[Capture Thread] Start Capture Loop\n");
std::list<CSniffer>::iterator sli, eli;
uint8_t packet[3000];
struct pcap_pkthdr h;
uint32_t ret;
// 캡처 루프 시작
while(*stopptr == false)
{
// Remove Sniffer List에 Sniffer 제거
sli = removesnifferlist->begin();
eli = removesnifferlist->end();
for(;sli != eli; sli = removesnifferlist->begin())
{
sli->StopCapture();
removesnifferlist->pop_front();
}
// 캡처 락
printf("sniffer num : %d \n", *sniffernum);
printf("[Capture Loop] Lock\n");
pthread_cond_wait(mcond, mlock);
printf("[Capture Loop] UnLock\n");
// 캡처 뮤텍스로 Capture start 명령을 받으면 시작
if(*stopptr)
break;
// 캡처 시작
int i=0;
while(*sniffernum > 0)
{
if(*stopptr)
{
printf("stopptr\n");
break;
}
sli = snifferlist->begin();
eli = snifferlist->end();
if(sli == eli)
{
printf("empty sniffer\n", h.len + 30);
break;
}
for(int i = 0; i < *sniffernum ; i++)
{
ret = sli->GetNextPacket((uint8_t*)packet, &h);
if(ret == 0)
continue;
//for(uint32_t i=30;i<(h.len + 30);i++)
//{
//if(i%10 == 0)
//printf("\n");
//printf("%02d ", packet[i]);
//}
//printf("\n");
//printf("SendData: len: %d\n", h.len + 30);
CPManager::Instance().SendData((char *)packet, h.len + 30);
}
}
}
printf("[Capture Thread] End Capture Loop\n");
return NULL;
}
int CCaptureLoop::CreateSniffer(char *devname, uint8_t *macaddr, uint32_t netmask)
{
// devname의 스니퍼가 존재하는지 검색
std::list<CSniffer>::iterator sli = m_SnifferList.begin();
std::list<CSniffer>::iterator eli = m_SnifferList.end();
for(; sli != eli ; sli++)
{
if(strncmp(sli->GetDevName(), devname, strlen(devname)) == 0)
return 1;
}
// 스니퍼 생성
CSniffer sniffer(devname, macaddr, netmask);
// 스니퍼 리스트에 등록
m_SnifferList.push_back(sniffer);
m_SnifferNum = m_SnifferList.size();
m_SnifferList.back().StartCapture();
// 락 해제(Mutex signal 신호)
pthread_cond_signal(&m_CaptureCond);
return 0;
}
int CCaptureLoop::RemoveSniffer(char *devname, int namelen)
{
if(namelen > 16)
return -1;
std::list<CSniffer>::iterator sli = m_SnifferList.begin();
std::list<CSniffer>::iterator eli = m_SnifferList.end();
char *snifferdevname;
// 스니퍼 이름 확인
// 동일한 NIC이름 가지고 있으면 정지
for(;sli != eli ; sli++)
{
snifferdevname = sli->GetDevName();
if(strncmp(snifferdevname, devname, namelen) == 0)
{
m_SnifferNum--;
m_RemoveSnifferList.push_back(*sli);
m_SnifferList.erase(sli);
return 0;
}
}
}