| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | #include <stdlib.h> |
|---|
| 10 | #include <string.h> |
|---|
| 11 | #include "general.h" |
|---|
| 12 | #include "Host.h" |
|---|
| 13 | |
|---|
| 14 | CVSID("$Id: Host.cc,v 1.1.1.1 2008/05/04 15:53:48 emasson Exp $"); |
|---|
| 15 | CVSID_DOT_H(HOST_H_CVSID); |
|---|
| 16 | |
|---|
| 17 | #if defined(__hpux__) || defined(__hpux) |
|---|
| 18 | extern int h_errno; |
|---|
| 19 | #endif |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | Host::Host(const char *hostname){ |
|---|
| 23 | struct hostent *hent = gethostbyname(hostname); |
|---|
| 24 | |
|---|
| 25 | check(hent); |
|---|
| 26 | |
|---|
| 27 | copy(hent); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | Host::Host(const struct in_addr *address){ |
|---|
| 31 | constuct(address); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | Host::Host(unsigned int addr){ |
|---|
| 35 | struct in_addr ia; |
|---|
| 36 | ia.s_addr = htonl(addr); |
|---|
| 37 | constuct(&ia); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | bool Host::constuct(const struct in_addr *address){ |
|---|
| 41 | struct hostent *hent = gethostbyaddr((char *)address, sizeof(in_addr), |
|---|
| 42 | AF_INET); |
|---|
| 43 | bool tmp = check(hent); |
|---|
| 44 | |
|---|
| 45 | copy(hent); |
|---|
| 46 | |
|---|
| 47 | return tmp; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | void Host::copy(const struct hostent *hent){ |
|---|
| 51 | _hent.h_name = NULL; |
|---|
| 52 | _hent.h_aliases = NULL; |
|---|
| 53 | _numAliases = 0; |
|---|
| 54 | _hent.h_addrtype = -1; |
|---|
| 55 | _hent.h_length = -1; |
|---|
| 56 | _numAddresses = 0; |
|---|
| 57 | _hent.h_addr_list = NULL; |
|---|
| 58 | _valid = false; |
|---|
| 59 | |
|---|
| 60 | if (hent != NULL){ |
|---|
| 61 | |
|---|
| 62 | _valid = true; |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | char **tmp = hent->h_aliases; |
|---|
| 66 | while (*tmp != NULL){ |
|---|
| 67 | _numAliases++; |
|---|
| 68 | tmp++; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | int hnamelen = strlen(hent->h_name) + 1; |
|---|
| 73 | _hent.h_name = new char[hnamelen]; |
|---|
| 74 | strncpy((char *)_hent.h_name, hent->h_name, hnamelen); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | _hent.h_aliases = new char *[_numAliases + 1]; |
|---|
| 78 | for (int i = 0 ; i < _numAliases ; i++){ |
|---|
| 79 | int len = strlen(hent->h_aliases[i]) + 1; |
|---|
| 80 | _hent.h_aliases[i] = new char[len + 1]; |
|---|
| 81 | strncpy(_hent.h_aliases[i], hent->h_aliases[i], len); |
|---|
| 82 | } |
|---|
| 83 | _hent.h_aliases[_numAliases] = NULL; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | _hent.h_addrtype = hent->h_addrtype; |
|---|
| 87 | _hent.h_length = hent->h_length; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | char **taddr = hent->h_addr_list; |
|---|
| 91 | while (*taddr != NULL){ |
|---|
| 92 | _numAddresses++; |
|---|
| 93 | taddr++; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | _hent.h_addr_list = new char *[_numAddresses + 1]; |
|---|
| 98 | for (int j = 0 ; j < _numAddresses ; j++){ |
|---|
| 99 | _hent.h_addr_list[j] = (char *)new in_addr; |
|---|
| 100 | memcpy(_hent.h_addr_list[j], hent->h_addr_list[j], sizeof(in_addr)); |
|---|
| 101 | } |
|---|
| 102 | _hent.h_addr_list[_numAddresses] = NULL; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | Host::~Host(void){ |
|---|
| 107 | clear(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void Host::clear(void){ |
|---|
| 111 | _valid = false; |
|---|
| 112 | |
|---|
| 113 | delete[] _hent.h_name; |
|---|
| 114 | _hent.h_name = NULL; |
|---|
| 115 | |
|---|
| 116 | for (int i = 0 ; i < _numAliases ; i++) |
|---|
| 117 | delete[] _hent.h_aliases[i]; |
|---|
| 118 | delete[] _hent.h_aliases; |
|---|
| 119 | _hent.h_aliases = NULL; |
|---|
| 120 | |
|---|
| 121 | for (int j = 0 ; j < _numAddresses ; j++) |
|---|
| 122 | delete _hent.h_addr_list[j]; |
|---|
| 123 | delete[] _hent.h_addr_list; |
|---|
| 124 | _hent.h_addr_list = NULL; |
|---|
| 125 | |
|---|
| 126 | _numAliases = 0; |
|---|
| 127 | _hent.h_addrtype = -1; |
|---|
| 128 | _hent.h_length = -1; |
|---|
| 129 | _numAddresses = 0; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | bool Host::check(const struct hostent *hent) { |
|---|
| 133 | if (hent != NULL) |
|---|
| 134 | return true; |
|---|
| 135 | |
|---|
| 136 | _valid = false; |
|---|
| 137 | _failure = h_errno; |
|---|
| 138 | return false; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | int Host::reasonForFailure(void) const{ |
|---|
| 142 | if (_valid) |
|---|
| 143 | return 0; |
|---|
| 144 | |
|---|
| 145 | return _failure; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | bool Host::tryAgain(void) const { |
|---|
| 149 | if (reasonForFailure() == TRY_AGAIN) |
|---|
| 150 | return true; |
|---|
| 151 | |
|---|
| 152 | return false; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | bool Host::operator==(const Host& host) const { |
|---|
| 156 | if (valid() != host.valid()) |
|---|
| 157 | return false; |
|---|
| 158 | |
|---|
| 159 | if (!valid()) |
|---|
| 160 | return true; |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | for (int i = 0 ; i < _numAddresses ; i++) |
|---|
| 164 | for (int j = 0 ; j < host.numAddresses() ; j++) |
|---|
| 165 | if (address(i)->s_addr == host.address(j)->s_addr) |
|---|
| 166 | return true; |
|---|
| 167 | |
|---|
| 168 | return false; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | std::ostream &Host::print(std::ostream& os) const { |
|---|
| 172 | |
|---|
| 173 | if (!*((char*)this)) |
|---|
| 174 | return os <<"Invalid Host. h_errno was = " <<_failure <<"\n"; |
|---|
| 175 | |
|---|
| 176 | os <<"---- Host ----\n" |
|---|
| 177 | <<"Official Name = " <<officialName() <<"\n" |
|---|
| 178 | <<"Aliases = "; |
|---|
| 179 | |
|---|
| 180 | for (int i = 0 ; i < numAliases() ; i++){ |
|---|
| 181 | os <<alias(i); |
|---|
| 182 | if (i != numAliases() - 1) |
|---|
| 183 | os <<", "; |
|---|
| 184 | } |
|---|
| 185 | os <<"\n"; |
|---|
| 186 | |
|---|
| 187 | os <<"Address Type = " <<addrType() <<"\n" |
|---|
| 188 | <<"Address Length = " <<addrLength() <<"\n" |
|---|
| 189 | <<"Addresses = "; |
|---|
| 190 | |
|---|
| 191 | for (int j = 0 ; j < numAddresses() ; j++){ |
|---|
| 192 | os <<strAddress(j); |
|---|
| 193 | if (j != numAddresses() - 1) |
|---|
| 194 | os <<", "; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | return os; |
|---|
| 198 | } |
|---|