-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNILayer.cpp
More file actions
167 lines (146 loc) · 3.71 KB
/
CNILayer.cpp
File metadata and controls
167 lines (146 loc) · 3.71 KB
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
#include "pch.h"
#include "CNILayer.h"
#include <pcap.h>
#include <Packet32.h>
#pragma comment(lib, "packet.lib")
#define BPF_MAJOR_VERSION
CNILayer::CNILayer(char* pName)
: CBaseLayer(pName), devicesList{}
{
LoadNpcapDlls();
PopulateDeviceList();
}
CNILayer::~CNILayer()
{
pcap_freealldevs(pointerToDeviceList);
}
BOOL CNILayer::LoadNpcapDlls()
{
TCHAR npcap_dir[512];
UINT len;
len = GetSystemDirectory(npcap_dir, 480);
if (!len) {
fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
return FALSE;
}
strcat_s(npcap_dir, 512, TEXT("\\Npcap"));
if (SetDllDirectory(npcap_dir) == 0) {
fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
return FALSE;
}
return TRUE;
}
void CNILayer::PopulateDeviceList()
{
char error[PCAP_ERRBUF_SIZE];
pcap_if_t* allDevices;
if (!pcap_findalldevs(&allDevices, error))
{
this->pointerToDeviceList = allDevices;
for (pcap_if_t* currentDevice = allDevices; currentDevice != nullptr; currentDevice = currentDevice->next)
{
NetworkDevice device{};
device.name = currentDevice->name;
device.description = currentDevice->description;
devicesList.push_back(device);
}
}
}
std::vector<CNILayer::NetworkDevice>* CNILayer::GetDevicesList()
{
return &this->devicesList;
}
BOOL CNILayer::GetMacAddress(char* deviceName, CNILayer::PhysicalAddress* outAddress)
{
PPACKET_OID_DATA oidData = (PPACKET_OID_DATA)malloc(sizeof(PACKET_OID_DATA) + 6);
if (oidData == nullptr)
{
return false;
}
oidData->Oid = OID_802_3_PERMANENT_ADDRESS;
oidData->Length = 6;
ZeroMemory(oidData->Data, 6);
if (deviceName == nullptr)
{
return false;
}
LPADAPTER adapter = PacketOpenAdapter(deviceName);
if (adapter == nullptr)
{
return false;
}
if (!PacketRequest(adapter, false, oidData))
{
return false;
}
for (int i = 0; i < 6; i++)
{
((char*)outAddress)[i] = oidData->Data[i];
}
PacketCloseAdapter(adapter);
return true;
}
BOOL CNILayer::Send(unsigned char* payload, int length)
{
if (pcap_sendpacket(_adapter, payload, length))
{
AfxMessageBox(_T("패킷 전송 실패"));
return FALSE;
}
return TRUE;
}
void CNILayer::StartReceive(char* adapterName)
{
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* handle = pcap_create(adapterName, errbuf);
if (!handle)
{
AfxMessageBox("Failed to open device handle");
#ifdef DEBUG
DebugBreak();
#endif
return;
}
pcap_set_timeout(handle, 0);
pcap_set_promisc(handle, TRUE);
//pcap_set_immediate_mode(handle, TRUE); WinPCAP ̺귯 ȵ
pcap_set_buffer_size(handle, 1024 * 1024);
pcap_set_snaplen(handle, 65535);
pcap_activate(handle);
_adapter = handle;
}
BOOL CNILayer::Receive(unsigned char* packet)
{
return this->GetUpperLayer(0)->Receive(packet);
}
UINT CNILayer::ReadingThread(LPVOID pParam)
{
pcap_pkthdr* packetHeader;
u_char* packet;
CNILayer* NIlayer = (CNILayer*)pParam;
int code = 1;
while (NIlayer->m_isWorkingThread)
{
code = pcap_next_ex(NIlayer->_adapter, &packetHeader, (const u_char**)&packet);
switch (code)
{
case 1:
break;
case PCAP_ERROR:
AfxMessageBox(pcap_geterr(NIlayer->_adapter));
#ifdef DEBUG
DebugBreak();
#endif
case 0:
default:
return false;
}
NIlayer->Receive(packet);
Sleep(30);
//Do something...
}
return 0;
}
void CNILayer::SetThreadloop() {
m_isWorkingThread = true;
}