| 1 | // |
| 2 | // HostEntry.cpp |
| 3 | // |
| 4 | // Library: Net |
| 5 | // Package: NetCore |
| 6 | // Module: HostEntry |
| 7 | // |
| 8 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Net/HostEntry.h" |
| 16 | #include "Poco/Exception.h" |
| 17 | #include <algorithm> |
| 18 | |
| 19 | |
| 20 | namespace Poco { |
| 21 | namespace Net { |
| 22 | |
| 23 | |
| 24 | HostEntry::HostEntry() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | |
| 29 | HostEntry::HostEntry(struct hostent* entry) |
| 30 | { |
| 31 | poco_check_ptr (entry); |
| 32 | |
| 33 | _name = entry->h_name; |
| 34 | char** alias = entry->h_aliases; |
| 35 | if (alias) |
| 36 | { |
| 37 | while (*alias) |
| 38 | { |
| 39 | _aliases.push_back(std::string(*alias)); |
| 40 | ++alias; |
| 41 | } |
| 42 | } |
| 43 | char** address = entry->h_addr_list; |
| 44 | if (address) |
| 45 | { |
| 46 | while (*address) |
| 47 | { |
| 48 | _addresses.push_back(IPAddress(*address, entry->h_length)); |
| 49 | ++address; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | #if defined(POCO_HAVE_IPv6) || defined(POCO_HAVE_ADDRINFO) |
| 56 | |
| 57 | |
| 58 | HostEntry::HostEntry(struct addrinfo* ainfo) |
| 59 | { |
| 60 | poco_check_ptr (ainfo); |
| 61 | |
| 62 | for (struct addrinfo* ai = ainfo; ai; ai = ai->ai_next) |
| 63 | { |
| 64 | if (ai->ai_canonname) |
| 65 | { |
| 66 | _name.assign(ai->ai_canonname); |
| 67 | } |
| 68 | if (ai->ai_addrlen && ai->ai_addr) |
| 69 | { |
| 70 | switch (ai->ai_addr->sa_family) |
| 71 | { |
| 72 | case AF_INET: |
| 73 | _addresses.push_back(IPAddress(&reinterpret_cast<struct sockaddr_in*>(ai->ai_addr)->sin_addr, sizeof(in_addr))); |
| 74 | break; |
| 75 | #if defined(POCO_HAVE_IPv6) |
| 76 | case AF_INET6: |
| 77 | _addresses.push_back(IPAddress(&reinterpret_cast<struct sockaddr_in6*>(ai->ai_addr)->sin6_addr, sizeof(in6_addr), reinterpret_cast<struct sockaddr_in6*>(ai->ai_addr)->sin6_scope_id)); |
| 78 | break; |
| 79 | #endif |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | |
| 86 | #endif // POCO_HAVE_IPv6 |
| 87 | |
| 88 | |
| 89 | #if defined(POCO_VXWORKS) |
| 90 | |
| 91 | |
| 92 | HostEntry::HostEntry(const std::string& name, const IPAddress& addr): |
| 93 | _name(name) |
| 94 | { |
| 95 | _addresses.push_back(addr); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | #endif // POCO_VXWORKS |
| 100 | |
| 101 | |
| 102 | HostEntry::HostEntry(const HostEntry& entry): |
| 103 | _name(entry._name), |
| 104 | _aliases(entry._aliases), |
| 105 | _addresses(entry._addresses) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | |
| 110 | HostEntry& HostEntry::operator = (const HostEntry& entry) |
| 111 | { |
| 112 | if (&entry != this) |
| 113 | { |
| 114 | _name = entry._name; |
| 115 | _aliases = entry._aliases; |
| 116 | _addresses = entry._addresses; |
| 117 | } |
| 118 | return *this; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | void HostEntry::swap(HostEntry& hostEntry) |
| 123 | { |
| 124 | std::swap(_name, hostEntry._name); |
| 125 | std::swap(_aliases, hostEntry._aliases); |
| 126 | std::swap(_addresses, hostEntry._addresses); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | HostEntry::~HostEntry() |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | |
| 135 | } } // namespace Poco::Net |
| 136 | |