| 121 | } |
| 122 | |
| 123 | Address Address::getHostAddress(const std::string hname) { |
| 124 | Address a; |
| 125 | InAddr tempAddr; |
| 126 | int j; |
| 127 | |
| 128 | struct hostent* hent; |
| 129 | if (hname == "") { // local address |
| 130 | char hostname[MAXHOSTNAMELEN + 1]; |
| 131 | if (gethostname(hostname, sizeof(hostname)) >= 0) { |
| 132 | hent = gethostbyname(hostname); |
| 133 | } |
| 134 | else { |
| 135 | return a; |
| 136 | } |
| 137 | } |
| 138 | else if (inet_aton(hname.c_str(), &tempAddr) != 0) { |
| 139 | a.addr.clear(); |
| 140 | a.addr.push_back(tempAddr); |
| 141 | return a; |
| 142 | } |
| 143 | else { // non-local address |
| 144 | hent = gethostbyname(hname.c_str()); |
| 145 | } |
| 146 | |
| 147 | if (!hent) { |
| 148 | herror("Looking up host name"); |
| 149 | return a; |
| 150 | } |
| 151 | |
| 152 | a.addr.clear(); |
| 153 | for (j = 0; hent->h_addr_list[j] != NULL; j++) { |
| 154 | ::memcpy(&tempAddr, hent->h_addr_list[j], sizeof(tempAddr)); |
| 155 | a.addr.push_back(tempAddr); |
| 156 | } |
| 157 | return a; |
| 158 | } |
| 159 | |
| 160 | std::string Address::getHostByAddress(InAddr addr) { |
| 161 | int addrLen = sizeof(addr); |