-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargument_parser.cpp
More file actions
172 lines (149 loc) · 3.89 KB
/
argument_parser.cpp
File metadata and controls
172 lines (149 loc) · 3.89 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
/**
* @file argument_parser.h
* @brief Parsing of command line arguments.
* @author Juraj Holub <xholub40>
* @project IPK - project 2
* @date April 2019
*/
#define MIN_PORT_RANGE 0
#define MAX_PORT_RANGE 65535
#include "argument_parser.h"
#include <sstream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pcap.h>
bool ArgumentParser::parse_args()
{
list<string> args_left;
for (int i = 1; i < argc; i++)
args_left.push_front(string(argv[i]));
int opt;
while ((opt = getopt_long_only(argc, argv, "", long_options, nullptr)) != -1)
{
if (opt == TCP_SCAN)
{
args_left.remove("-pt");
args_left.remove(string(optarg));
if (not parse_ports(this->tcp_ports, optarg, "TCP"))
return false;
}
else if (opt == UDP_SCAN)
{
args_left.remove("-pu");
args_left.remove(string(optarg));
if (not parse_ports(this->udp_ports, optarg, "UDP"))
return false;
}
else if (opt == IFACE)
{
args_left.remove("-i");
args_left.remove(string(optarg));
this->iface = string(optarg);
}
}
if (args_left.size() == 1)
this->ip_address = parse_ipaddr(args_left.front());
if (this->iface.empty()) // user not set iface so I found one by myself
this->iface = get_iface();
return true;
}
bool ArgumentParser::try_set_port_range(vector<int>& ports, string raw_str)
{
if (raw_str[0] == '-')
return false;
try
{
split_ports(ports, raw_str, '-');
if (ports.size() == 2) // it is range interval
{
int bot = ports.at(0);
int top = ports.at(1);
ports.clear();
for (int i = bot; i <= top; i++)
ports.push_back(i);
}
}
catch (InvalidPort& e)
{
return false;
}
return true;
}
bool ArgumentParser::parse_ports(vector<int> &ports, string raw_str, string protocol)
{
bool success = try_set_port_range(ports, raw_str);
if (success)
return true;
try
{
split_ports(ports, raw_str, ',');
return true;
}
catch (InvalidPort& e)
{
cerr << "Invalid " << protocol << " port value: \"" << e.port << "\"\n";
}
return false;
}
void ArgumentParser::split_ports(vector<int>& items, const string &str, char delim)
{
int port;
string item;
istringstream items_stream(str);
while (getline(items_stream, item, delim))
{
if (is_digits(item))
{
istringstream str_to_int(item);
str_to_int >> port;
if (MIN_PORT_RANGE <= port and port <= MAX_PORT_RANGE)
items.push_back(port);
else throw(InvalidPort(item));
}
else throw(InvalidPort(item));
}
}
bool ArgumentParser::is_digits(string str)
{
return str.find_first_not_of("0123456789") == string::npos;
}
string ArgumentParser::parse_ipaddr(string domain)
{
struct hostent *hostent = gethostbyname(domain.c_str());
if (hostent == NULL)
{
herror("gethostbyname");
exit(1);
}
char *ip_addr = inet_ntoa(*((struct in_addr*)hostent->h_addr_list[0]));
return string(ip_addr);
}
string ArgumentParser::get_iface()
{
pcap_if_t *devices;
char errbuff[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&devices, errbuff))
{
perror("pcap_findalldevs");
exit(1);
}
for (pcap_if_t *i = devices; i != NULL; i=i->next)
{
if (i->flags != PCAP_IF_LOOPBACK)
{
string name = i->name;
pcap_freealldevs(devices);
return name;
}
}
pcap_freealldevs(devices);
return "any";
}