-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_Net.cpp
135 lines (91 loc) · 3.8 KB
/
C_Net.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
//////////////////////////////////////////////////////////////////////////////////
// [ Net_Class_Source ]
//////////////////////////////////////////////////////////////////////////////////
#include "C_Net.hpp"
//////////////////////////////////////////////////////////////////////////////////
// [ init ]
//////////////////////////////////////////////////////////////////////////////////
int C_Net::init(){
if(CNInterface.create() != C_NET_INTERFACE_READY){
cout << "ERROR: CNInterface.create" << endl;
return(C_NET_ERROR);
}
CNRaw.signal_data().connect(sigc::mem_fun(*this, &C_Net::on_raw_data));
return(C_NET_READY);
}
//////////////////////////////////////////////////////////////////////////////////
// [ start ]
//////////////////////////////////////////////////////////////////////////////////
int C_Net::start(const S_Net_Interface* pSInterface, int cPackets, int cSleep){
if(!pSInterface || bscann) return(C_NET_ERROR);
//////////////////////////////////
memcpy(IP_SOURCE, &pSInterface->dw_IP, SIZE_IP);
memcpy(IP_REQUEST, &pSInterface->dw_Network, SIZE_IP);
memcpy(MAC_SOURCE, pSInterface->uc_MAC, SIZE_MAC);
memcpy(MAC_ETH_S, pSInterface->uc_MAC, SIZE_MAC);
memcpy(MAC_REQUEST, MAC_BROADCAST, SIZE_MAC);
memcpy(MAC_ETH_D, MAC_BROADCAST, SIZE_MAC);
//////////////////////////////////
sArp.Operation = ARP_REQUEST;
sArp.pETH_MAC_D = MAC_ETH_D;
sArp.pETH_MAC_S = MAC_ETH_S;
sArp.pARP_MAC_D = MAC_REQUEST;
sArp.pARP_MAC_S = MAC_SOURCE;
sArp.pARP_IP_D = IP_REQUEST;
sArp.pARP_IP_S = IP_SOURCE;
CNArp.getPacket(pData, &cData, &sArp);
ARP_HEADER* pRCV_arp = (ARP_HEADER*)(pData + cETHERNET_HEADER);
//////////////////////////////////
CDA_Result.clear();
//////////////////////////////////
if(CNRaw.open(pSInterface) != C_NET_RAW_READY) return(C_NET_ERROR);
if(CNRaw.start(C_NET_ID, pBuffer, C_NET_BUFFER) != C_NET_RAW_READY) return(C_NET_ERROR);
bscann = true;
for(int n = 1; n < 255; n++){
pRCV_arp->ARP_IP_D[3] = n;
for(int n = 0; n < cPackets; n++){
CNRaw.send(pData, cData);
usleep(cSleep * 1000);
}
if(!bscann){
CNRaw.stop();
CNRaw.close();
sig_finish();
return(C_NET_READY);
}
}
CNRaw.stop();
CNRaw.close();
sig_finish();
return(C_NET_READY);
}
//////////////////////////////////////////////////////////////////////////////////
// [ stop ]
//////////////////////////////////////////////////////////////////////////////////
int C_Net::stop(){
if(!bscann) return(C_NET_ERROR);
bscann = false;
return(C_NET_READY);
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_raw_data ]
//////////////////////////////////////////////////////////////////////////////////
void C_Net::on_raw_data(int id, int cData){
if(cData < (int)(cETHERNET_HEADER)) return;
ETHERNET_HEADER* pRCV_ethhdr = (ETHERNET_HEADER*)pBuffer;
switch(pRCV_ethhdr->Type){
case ETH_TYP_ARP:{
if(cData < (int)(cETHERNET_HEADER + cARP_HEADER)) return;
ARP_HEADER* pRCV_arp = (ARP_HEADER*)(pBuffer + cETHERNET_HEADER);
if(pRCV_arp->ARP_OpCode != ARP_RESPONSE ||
*((DWORD*)pRCV_arp->ARP_MAC_D) != *((DWORD*)MAC_SOURCE) ||
*((WORD*)pRCV_arp->ARP_MAC_D + 2) != *((WORD*)MAC_SOURCE + 2)) return;
*((DWORD*)SResult.MAC) = *((DWORD*)pRCV_arp->ARP_MAC_S);
*((WORD*)SResult.MAC + 2) = *((WORD*)pRCV_arp->ARP_MAC_S + 2);
*((DWORD*)SResult.IP) = *((DWORD*)pRCV_arp->ARP_IP_S);
CDA_Result.push_back(SResult);
sig_packet();
break;
}
}
}