-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
194 lines (147 loc) · 4.83 KB
/
main.cpp
File metadata and controls
194 lines (147 loc) · 4.83 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
#include <cstdlib>
#include <iostream>
#include <thread>
#include <map>
#include "exception/dump.h"
#include "src/model/UserMap.h"
#include "src/net/PacketHandler.h"
#include "src/net/PCap.h"
#include "net/util.h"
#include "net/DefaultDevice.h"
struct Cmd{
int flags = 0;
int captureTimeout = 0;
std::string deviceName = "";
std::map<int, std::string> addresses;
};
/**
* Prints the device information.
*/
void printDeviceInfo(pcap_if_t *device);
/**
* Processes the command-line arguments.
*/
Cmd parseCmd(int argc, char **argv);
/**
* Prints the program usage information.
*/
void printUsage(const char *programName, std::ostream &out = std::cerr, bool shouldExit = true);
void cleanUpUserMap(){
model::UserMap um;
while (true){
return;
um.remove();
Sleep(1);
}
}
int main(int argc, char **argv)
{
net::PCap pcap;
net::PacketHandler ph;
std::thread bgCleanUp;
Cmd cmd;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
try{
cmd = parseCmd(argc, argv);
}
catch (const std::exception &e){
std::cerr << e.what() << std::endl;
printUsage(argv[0]);
}
// printDeviceInfo(device);
// bgCleanUp = std::thread(cleanUpUserMap);
// bgCleanUp.detach();
ph.setFlags(cmd.flags);
pcap.setPacketHandler(&ph);
pcap.doCapture(cmd.deviceName);
curl_global_cleanup();
return EXIT_SUCCESS;
}
Cmd parseCmd(int argc, char **argv)
{
Cmd cmd;
net::DefaultDevice dd;
if (argc < 2){
throw std::runtime_error("no arguments specified");
}
for (int i = 1; i < argc; ++i){
if (strcmp(argv[i], "-help") == 0){
throw std::runtime_error("help required");
}
else if (strcmp(argv[i], "-tcp") == 0){
cmd.flags |= net::AbstractPacketHandler::Protocol::TCP;
}
else if (strcmp(argv[i], "-udp") == 0){
cmd.flags |= net::AbstractPacketHandler::Protocol::UDP;
}
else if (strcmp(argv[i], "-timeout") == 0){
if (argv[i + 1] == nullptr)
throw std::runtime_error("no capture timeout specified");
cmd.captureTimeout = atoi(argv[i + 1]);
}
else if (strcmp(argv[i], "-device") == 0){
if (argv[i + 1] == nullptr)
throw std::runtime_error("no device name specified");
cmd.deviceName = argv[i + 1];
}
else if (strcmp(argv[i], "-list") == 0){
int i = 0;
for (auto dn : dd.getDeviceNames()){
std::cout << (++i) << ". " << dn << std::endl;
}
exit(EXIT_SUCCESS);
}
}
if (cmd.flags == 0){
throw std::runtime_error("no or incorrect protocol specified");
}
// if no device was specified
if (cmd.deviceName.empty()){
auto device = dd.getDefaultDevice();
if (device == nullptr){
throw std::runtime_error("There was no device name specified and there was an error finding a default network device. "
+ dd.getError());
}
cmd.deviceName = device->name;
// this can be removed from here.
printDeviceInfo(device);
}
return cmd;
}
void printUsage(const char *programName, std::ostream &out, bool shouldExit){
out << "usage: ";
out << programName << " [options...]\n\n";
out << "available options\n";
out << "\t-help\t\t Show this help menu.\n";
out << "\t-list\t\t Show the available devices of this system.\n";
out << "\t-device <name>\t Name of the device to capture from.\n";
out << "\t-timeout <milliseconds>\t The number of milliseconds.\n";
out << "\t-tcp\t\t Capture TCP frames.\n";
out << "\t-udp\t\t Capture UDP frames.\n";
if (shouldExit){
exit(EXIT_FAILURE);
}
}
void printDeviceInfo(pcap_if_t *device)
{
printf("using %s\n", pcap_lib_version());
printf("-> capturing on device: %s (%s)\n", device->name,
device->description == nullptr ? "no description" : device->description);
printf("-> loopback: %s\n", (device->flags & PCAP_IF_LOOPBACK) ? "yes" : "no");
// AF_INET
printf("\n");
for (auto a = device->addresses; a; a = a->next){
std::map<const char*, uint32_t> data;
data["-> ip address: %s\n"] = ((struct sockaddr_in*)a->addr)->sin_addr.s_addr;
data["-> mask: %s\n"] = ((struct sockaddr_in*)a->netmask)->sin_addr.s_addr;
data["-> broadcast: %s\n"] = ((struct sockaddr_in*)a->broadaddr)->sin_addr.s_addr;
//data["-> gateway: %s\n"] = ((struct sockaddr_in*)a->dstaddr)->sin_addr.s_addr;
for (auto values : data){
if (values.second == 0u) continue;
printf(values.first, net::ipString(values.second).c_str());
}
//printf("\n");
}
printf("\n");
}