-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNIC.cpp
More file actions
218 lines (177 loc) · 5.75 KB
/
NIC.cpp
File metadata and controls
218 lines (177 loc) · 5.75 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
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
/*!
\file NIC.cpp
\author Tom Mahler, contact at tommahler@gmail.com
\brief Implements the NIC class.
*/
#include "NIC.h"
#include "NIC_Cable.h"
#include "L2.h"
#include "utils.h"
struct netlab::NetworkInterface::Info
{
netlab::IPv4Address ip_addr;
netlab::IPv4Address netmask;
netlab::IPv4Address bcast_addr;
address_type hw_addr;
};
NIC::NIC(inet_os &inet, struct in_addr *my_ip, mac_addr my_mac, struct in_addr *my_gw,
struct in_addr *my_netmask, bool promisc_mode, std::string filter)
: inet(inet), _ifa_flags(IFF_UP), _etherbroadcastaddr(mac_addr::broadcast)
{
inet.cable(new NIC_Cable(inet, promisc_mode, filter));
inet.nic(this);
_mac =
my_mac == "" ?
mac_addr(inet.cable()->iface.addresses().hw_addr.to_string()) :
my_mac;
_ip_addr.s_addr =
my_ip ?
my_ip->s_addr :
inet_addr(inet.cable()->iface.addresses().ip_addr.to_string().c_str());
_netmask_addr.s_addr =
my_netmask ?
my_netmask->s_addr :
inet_addr(inet.cable()->iface.addresses().netmask.to_string().c_str());
if (my_gw)
_dgw_addr.s_addr = my_gw->s_addr;
else {
netlab::IPv4Address gw;
netlab::utils::gateway_from_ip(netlab::IPv4Address(_ip_addr.s_addr), gw);
_dgw_addr.s_addr = inet_addr(gw.to_string().c_str());
}
_bcast_addr.s_addr =
inet_addr(inet.cable()->iface.addresses().bcast_addr.to_string().c_str());
}
NIC::NIC(class inet_os &inet, netlab::IPv4Address my_ip, mac_addr my_mac, netlab::IPv4Address my_gw,
netlab::IPv4Address my_netmask, bool promisc_mode, std::string filter)
: NIC(inet, nullptr, my_mac, nullptr, nullptr, promisc_mode, filter)
{
_dgw_addr.s_addr = inet_addr(my_gw.to_string().c_str());
_ip_addr.s_addr = inet_addr(my_ip.to_string().c_str());
}
bool NIC::in_localaddr(struct in_addr &addr) const { return ((addr.s_addr & _netmask_addr.s_addr) ^ (addr.s_addr & _netmask_addr.s_addr)) == 0; }
void NIC::HexDump(byte *m, const size_t &m_len, std::ostream& str)
{
char szBuf[100];
long lIndent = 1, lOutLen, lIndex, lIndex2, lOutLen2, lRelPos;
struct { char *pData; unsigned long lSize; } buf;
unsigned char *pTmp, ucTmp, *pAddress = (unsigned char *)m;
buf.pData = (char *)pAddress;
buf.lSize = m_len;
while (buf.lSize > 0)
{
pTmp = (unsigned char *)buf.pData;
lOutLen = (int)buf.lSize;
if (lOutLen > 16)
lOutLen = 16;
// create a 64-character formatted output line:
sprintf(szBuf, " "
" "
" %08lX", (long unsigned int) (pTmp - pAddress));
lOutLen2 = lOutLen;
for (lIndex = 1 + lIndent, lIndex2 = 53 - 15 + lIndent, lRelPos = 0; lOutLen2; lOutLen2--, lIndex += 2, lIndex2++)
{
ucTmp = *pTmp++;
sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp);
if (!isprint(ucTmp)) ucTmp = '.'; // nonprintable char
szBuf[lIndex2] = ucTmp;
if (!(++lRelPos & 3)) // extra blank after 4 bytes
lIndex++; szBuf[lIndex + 2] = ' ';
}
if (!(lRelPos & 3)) lIndex--;
szBuf[lIndex] = ' ';
szBuf[lIndex + 1] = ' ';
str << szBuf << std::endl;
buf.pData += lOutLen;
buf.lSize -= lOutLen;
}
}
void NIC::HexDump(std::shared_ptr<std::vector<byte>> &m, const std::vector<byte>::iterator &it, std::ostream& str)
{
char szBuf[100];
long lIndent = 1, lOutLen, lIndex, lIndex2, lOutLen2, lRelPos;
struct { char *pData; unsigned long lSize; } buf;
unsigned char *pTmp, ucTmp, *pAddress = (unsigned char *)m->data();
buf.pData = (char *)pAddress;
buf.lSize = m->end() - it;
while (buf.lSize > 0)
{
pTmp = (unsigned char *)buf.pData;
lOutLen = (int)buf.lSize;
if (lOutLen > 16)
lOutLen = 16;
// create a 64-character formatted output line:
sprintf(szBuf, " "
" "
" %08lX", (long unsigned int) (pTmp - pAddress));
lOutLen2 = lOutLen;
for (lIndex = 1 + lIndent, lIndex2 = 53 - 15 + lIndent, lRelPos = 0; lOutLen2; lOutLen2--, lIndex += 2, lIndex2++)
{
ucTmp = *pTmp++;
sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp);
if (!isprint(ucTmp)) ucTmp = '.'; // nonprintable char
szBuf[lIndex2] = ucTmp;
if (!(++lRelPos & 3)) // extra blank after 4 bytes
lIndex++; szBuf[lIndex + 2] = ' ';
}
if (!(lRelPos & 3)) lIndex--;
szBuf[lIndex] = ' ';
szBuf[lIndex + 1] = ' ';
str << szBuf << std::endl;
buf.pData += lOutLen;
buf.lSize -= lOutLen;
}
}
NIC::~NIC()
{
disconnect();
if (inet.cable()) {
delete inet.cable();
inet.cable(nullptr);
}
inet.nic(nullptr);
}
void NIC::leread(std::shared_ptr<std::vector<byte>> &m, std::vector<byte>::iterator &it)
{
struct L2::ether_header *et(reinterpret_cast<struct L2::ether_header *>(&m->data()[m->begin() - it]));
et->ether_type = ntohs(static_cast<u_short>(et->ether_type));
/* adjust input length to account for header and CRC */
if ((it += sizeof(struct L2::ether_header)) > m->end())
return;
inet.datalink()->ether_input(m, it, et);
}
void NIC::lestart(std::shared_ptr<std::vector<byte>> &m, const std::vector<byte>::iterator &it) {
if (m == nullptr)
{
std::lock_guard<std::mutex> lock(inet.print_mutex);
std::cout << "[!] lestart(nothing to send)" << std::endl;
return;
}
#ifdef NIC_DEBUG_OUT
{
std::lock_guard<std::mutex> lock(inet.print_mutex);
std::cout << "[#] HexDump of sent Packet:" << std::endl;
HexDump(m, it);
}
#endif
try
{
inet.cable()->send_l2(m, it, inet.cable()->iface);
}
catch (const Tins::socket_write_error& socex)
{
std::lock_guard<std::mutex> lock(inet.print_mutex);
std::cout << "[!] Send failed with the error: " << socex.what() << std::endl;
}
return;
}
void NIC::connect(L2 &upperInterface, class L0_buffer *buf, const uint32_t count)
{
ifa_flags(ifa_flags() | IFF_RUNNING);
inet.cable()->connect(upperInterface, buf, count);
}
void NIC::disconnect(bool from_buf)
{
ifa_flags(ifa_flags() & ~IFF_RUNNING);
inet.cable()->disconnect(from_buf);
}