-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathHost.cpp
130 lines (97 loc) · 2.13 KB
/
Host.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
#include "Host.h"
using namespace std;
Host::Host(const Host& host)
: address(host.address), alive(host.alive), reason(host.reason), cpe(host.cpe),
services(new Services()), opSys(host.opSys), osVer(host.osVer), date(host.date), data(host.data)
{
for (auto service : *host.services)
{
services->push_back(new Service(*service));
}
}
Host::Host(const string& address)
: address(address), cpe(), services(new Services()), opSys(OpSys::Unidentified), osVer(0), date(), data(nullptr)
{
}
Host::Host(const string& address, const set<unsigned short>& tcps, const set<unsigned short>& udps)
: address(address), cpe(), services(new Services()), opSys(OpSys::Unidentified), osVer(0), date(), data(nullptr)
{
if (tcps.size() > 0)
{
for (auto tcp : tcps)
{
auto service = new Service(address, tcp, IPPROTO_TCP);
service->host = this;
services->push_back(service);
}
}
if (udps.size() > 0)
{
for (auto udp : udps)
{
auto service = new Service(address, udp, IPPROTO_UDP);
service->host = this;
services->push_back(service);
}
}
}
Service* Host::AddService(Service* service)
{
if (address == service->address)
{
service->host = this;
services->push_back(service);
return service;
}
return nullptr;
}
Service* Host::AddService(unsigned short port, IPPROTO protocol)
{
auto service = new Service(address, port, protocol);
service->host = this;
services->push_back(service);
return service;
}
int Host::AddServices(const Services& servlist)
{
auto count = 0;
for (auto service : servlist)
{
if (address == service->address)
{
service->host = this;
services->push_back(service);
count++;
}
}
return count;
}
int Host::AddServices(const set<unsigned short>& ports, IPPROTO protocol)
{
auto count = 0;
for (auto port : ports)
{
auto service = new Service(address, port, protocol);
service->host = this;
services->push_back(service);
count++;
}
return count;
}
Host::~Host()
{
for (auto& service : *services)
{
delete service;
}
services->clear();
delete services;
}
void freeHosts(Hosts& hosts)
{
for (auto& host : hosts)
{
delete host;
}
hosts.clear();
}