forked from hills/xosview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Host.cc
192 lines (151 loc) · 4.18 KB
/
Host.cc
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
//
// Copyright (c) 1994, 1995, 2006 by Mike Romberg ( [email protected] )
//
// This file may be distributed under terms of the GPL
//
#include <stdlib.h>
#include <string.h>
#include "Host.h"
#if defined(__hpux__) || defined(__hpux)
extern int h_errno;
#endif
Host::Host(const char *hostname){
struct hostent *hent = gethostbyname(hostname);
check(hent);
copy(hent);
}
Host::Host(const struct in_addr *address){
constuct(address);
}
Host::Host(unsigned int addr){
struct in_addr ia;
ia.s_addr = htonl(addr);
constuct(&ia);
}
bool Host::constuct(const struct in_addr *address){
struct hostent *hent = gethostbyaddr((char *)address, sizeof(in_addr),
AF_INET);
bool tmp = check(hent);
copy(hent);
return tmp;
}
void Host::copy(const struct hostent *hent){
_hent.h_name = NULL;
_hent.h_aliases = NULL;
_numAliases = 0;
_hent.h_addrtype = -1;
_hent.h_length = -1;
_numAddresses = 0;
_hent.h_addr_list = NULL;
_valid = false;
if (hent != NULL){
// Then it is valid.
_valid = true;
// find the number of aliases.
char **tmp = hent->h_aliases;
while (*tmp != NULL){
_numAliases++;
tmp++;
}
// copy the official name.
int hnamelen = strlen(hent->h_name) + 1;
_hent.h_name = new char[hnamelen];
memcpy(_hent.h_name, hent->h_name, hnamelen);
// copy the aliases.
_hent.h_aliases = new char *[_numAliases + 1];
for (int i = 0 ; i < _numAliases ; i++){
int len = strlen(hent->h_aliases[i]) + 1;
_hent.h_aliases[i] = new char[len];
memcpy(_hent.h_aliases[i], hent->h_aliases[i], len);
}
_hent.h_aliases[_numAliases] = NULL;
// copy the address type and length.
_hent.h_addrtype = hent->h_addrtype;
_hent.h_length = hent->h_length;
// find the number of addresses.
char **taddr = hent->h_addr_list;
while (*taddr != NULL){
_numAddresses++;
taddr++;
}
// copy the addresses.
_hent.h_addr_list = new char *[_numAddresses + 1];
for (int j = 0 ; j < _numAddresses ; j++){
_hent.h_addr_list[j] = (char *)new in_addr;
memcpy(_hent.h_addr_list[j], hent->h_addr_list[j], sizeof(in_addr));
}
_hent.h_addr_list[_numAddresses] = NULL;
}
}
Host::~Host(void){
clear();
}
void Host::clear(void){
_valid = false;
delete[] _hent.h_name;
_hent.h_name = NULL;
for (int i = 0 ; i < _numAliases ; i++)
delete[] _hent.h_aliases[i];
delete[] _hent.h_aliases;
_hent.h_aliases = NULL;
for (int j = 0 ; j < _numAddresses ; j++)
delete _hent.h_addr_list[j];
delete[] _hent.h_addr_list;
_hent.h_addr_list = NULL;
_numAliases = 0;
_hent.h_addrtype = -1;
_hent.h_length = -1;
_numAddresses = 0;
}
bool Host::check(const struct hostent *hent) {
if (hent != NULL) // all is well
return true;
_valid = false;
_failure = h_errno;
return false;
}
int Host::reasonForFailure(void) const{
if (_valid)
return 0; //NETDB_SUCCESS;
return _failure;
}
bool Host::tryAgain(void) const {
if (reasonForFailure() == TRY_AGAIN)
return true;
return false;
}
bool Host::operator==(const Host& host) const {
if (valid() != host.valid())
return false;
if (!valid()) // they are both invalid
return true;
// now check all of the addresses for each host.
for (int i = 0 ; i < _numAddresses ; i++)
for (int j = 0 ; j < host.numAddresses() ; j++)
if (address(i)->s_addr == host.address(j)->s_addr)
return true;
return false;
}
std::ostream &Host::print(std::ostream& os) const {
/* Cast 'this' to a char*, so we don't need to create a Host::! operator.*/
if (!*((char*)this))
return os <<"Invalid Host. h_errno was = " <<_failure <<"\n";
os <<"---- Host ----\n"
<<"Official Name = " <<officialName() <<"\n"
<<"Aliases = ";
for (int i = 0 ; i < numAliases() ; i++){
os <<alias(i);
if (i != numAliases() - 1)
os <<", ";
}
os <<"\n";
os <<"Address Type = " <<addrType() <<"\n"
<<"Address Length = " <<addrLength() <<"\n"
<<"Addresses = ";
for (int j = 0 ; j < numAddresses() ; j++){
os <<strAddress(j);
if (j != numAddresses() - 1)
os <<", ";
}
return os;
}