/src/logging-log4cxx/src/main/cpp/inetaddress.cpp
| Line | Count | Source | 
| 1 |  | /* | 
| 2 |  |  * Licensed to the Apache Software Foundation (ASF) under one or more | 
| 3 |  |  * contributor license agreements.  See the NOTICE file distributed with | 
| 4 |  |  * this work for additional information regarding copyright ownership. | 
| 5 |  |  * The ASF licenses this file to You under the Apache License, Version 2.0 | 
| 6 |  |  * (the "License"); you may not use this file except in compliance with | 
| 7 |  |  * the License.  You may obtain a copy of the License at | 
| 8 |  |  * | 
| 9 |  |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
| 10 |  |  * | 
| 11 |  |  * Unless required by applicable law or agreed to in writing, software | 
| 12 |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
| 13 |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 14 |  |  * See the License for the specific language governing permissions and | 
| 15 |  |  * limitations under the License. | 
| 16 |  |  */ | 
| 17 |  |  | 
| 18 |  | #include <log4cxx/logstring.h> | 
| 19 |  | #include <log4cxx/helpers/inetaddress.h> | 
| 20 |  | #include <log4cxx/helpers/loglog.h> | 
| 21 |  | #include <log4cxx/helpers/transcoder.h> | 
| 22 |  | #include <log4cxx/helpers/pool.h> | 
| 23 |  |  | 
| 24 |  | #include "apr_network_io.h" | 
| 25 |  |  | 
| 26 |  | using namespace LOG4CXX_NS; | 
| 27 |  | using namespace LOG4CXX_NS::helpers; | 
| 28 |  |  | 
| 29 |  | IMPLEMENT_LOG4CXX_OBJECT(InetAddress) | 
| 30 |  |  | 
| 31 |  | struct InetAddress::InetAddressPrivate{ | 
| 32 |  |  | 
| 33 |  |   LogString ipAddrString; | 
| 34 |  |   LogString hostNameString; | 
| 35 |  | }; | 
| 36 |  |  | 
| 37 |  | UnknownHostException::UnknownHostException(const LogString& msg1) | 
| 38 | 0 |   : Exception(msg1) | 
| 39 | 0 | { | 
| 40 | 0 | } | 
| 41 |  |  | 
| 42 |  | UnknownHostException::UnknownHostException(const UnknownHostException& src) | 
| 43 | 0 |   : Exception(src) | 
| 44 | 0 | { | 
| 45 | 0 | } | 
| 46 |  |  | 
| 47 |  | UnknownHostException& UnknownHostException::operator=(const UnknownHostException& src) | 
| 48 | 0 | { | 
| 49 | 0 |   Exception::operator=(src); | 
| 50 | 0 |   return *this; | 
| 51 | 0 | } | 
| 52 |  |  | 
| 53 |  |  | 
| 54 |  | InetAddress::InetAddress(const LogString& hostName, const LogString& hostAddr) | 
| 55 | 0 |   : m_priv(std::make_unique<InetAddressPrivate>()) | 
| 56 | 0 | { | 
| 57 | 0 |   m_priv->ipAddrString = hostAddr; | 
| 58 | 0 |   m_priv->hostNameString = hostName; | 
| 59 | 0 | } | 
| 60 |  |  | 
| 61 | 0 | InetAddress::~InetAddress(){} | 
| 62 |  |  | 
| 63 |  | /** Determines all the IP addresses of a host, given the host's name. | 
| 64 |  | */ | 
| 65 |  | std::vector<InetAddressPtr> InetAddress::getAllByName(const LogString& host) | 
| 66 | 0 | { | 
| 67 | 0 |   LOG4CXX_ENCODE_CHAR(encodedHost, host); | 
| 68 |  |  | 
| 69 |  |   // retrieve information about the given host | 
| 70 | 0 |   Pool addrPool; | 
| 71 |  | 
 | 
| 72 | 0 |   apr_sockaddr_t* address = 0; | 
| 73 | 0 |   apr_status_t status = | 
| 74 | 0 |     apr_sockaddr_info_get(&address, encodedHost.c_str(), | 
| 75 | 0 |       APR_INET, 0, 0, addrPool.getAPRPool()); | 
| 76 |  | 
 | 
| 77 | 0 |   if (status != APR_SUCCESS) | 
| 78 | 0 |   { | 
| 79 | 0 |     LogString msg(LOG4CXX_STR("Cannot get information about host: ")); | 
| 80 | 0 |     msg.append(host); | 
| 81 | 0 |     LogLog::error(msg); | 
| 82 | 0 |     throw UnknownHostException(msg); | 
| 83 | 0 |   } | 
| 84 |  |  | 
| 85 | 0 |   std::vector<InetAddressPtr> result; | 
| 86 | 0 |   apr_sockaddr_t* currentAddr = address; | 
| 87 |  | 
 | 
| 88 | 0 |   while (currentAddr != NULL) | 
| 89 | 0 |   { | 
| 90 |  |     // retrieve the IP address of this InetAddress. | 
| 91 | 0 |     LogString ipAddrString; | 
| 92 | 0 |     char* ipAddr; | 
| 93 | 0 |     status = apr_sockaddr_ip_get(&ipAddr, currentAddr); | 
| 94 |  | 
 | 
| 95 | 0 |     if (status == APR_SUCCESS) | 
| 96 | 0 |     { | 
| 97 | 0 |       std::string ip(ipAddr); | 
| 98 | 0 |       Transcoder::decode(ip, ipAddrString); | 
| 99 | 0 |     } | 
| 100 |  |  | 
| 101 |  |     // retrieve the host name of this InetAddress. | 
| 102 | 0 |     LogString hostNameString; | 
| 103 | 0 |     char* hostName; | 
| 104 | 0 |     status = apr_getnameinfo(&hostName, currentAddr, 0); | 
| 105 |  | 
 | 
| 106 | 0 |     if (status == APR_SUCCESS) | 
| 107 | 0 |     { | 
| 108 | 0 |       std::string host(hostName); | 
| 109 | 0 |       Transcoder::decode(host, hostNameString); | 
| 110 | 0 |     } | 
| 111 |  | 
 | 
| 112 | 0 |     result.push_back(std::make_shared<InetAddress>(hostNameString, ipAddrString)); | 
| 113 | 0 |     currentAddr = currentAddr->next; | 
| 114 | 0 |   } | 
| 115 |  | 
 | 
| 116 | 0 |   return result; | 
| 117 | 0 | } | 
| 118 |  |  | 
| 119 |  |  | 
| 120 |  | /** Determines the IP address of a host, given the host's name. | 
| 121 |  | */ | 
| 122 |  | InetAddressPtr InetAddress::getByName(const LogString& host) | 
| 123 | 0 | { | 
| 124 | 0 |   InetAddressPtr result; | 
| 125 | 0 |   auto address = getAllByName(host); | 
| 126 | 0 |   if (!address.empty()) | 
| 127 | 0 |     result = address.front(); | 
| 128 | 0 |   return result; | 
| 129 | 0 | } | 
| 130 |  |  | 
| 131 |  | /** Returns the IP address string "%d.%d.%d.%d". | 
| 132 |  | */ | 
| 133 |  | LogString InetAddress::getHostAddress() const | 
| 134 | 0 | { | 
| 135 | 0 |   return m_priv->ipAddrString; | 
| 136 | 0 | } | 
| 137 |  |  | 
| 138 |  | /** Gets the host name for this IP address. | 
| 139 |  | */ | 
| 140 |  | LogString InetAddress::getHostName() const | 
| 141 | 0 | { | 
| 142 | 0 |   return m_priv->hostNameString; | 
| 143 | 0 | } | 
| 144 |  |  | 
| 145 |  | /** Returns the local host. | 
| 146 |  | */ | 
| 147 |  | InetAddressPtr InetAddress::getLocalHost() | 
| 148 | 0 | { | 
| 149 | 0 |   return getByName(LOG4CXX_STR("127.0.0.1")); | 
| 150 | 0 | } | 
| 151 |  |  | 
| 152 |  |  | 
| 153 |  | InetAddressPtr InetAddress::anyAddress() | 
| 154 | 0 | { | 
| 155 |  |   // APR_ANYADDR does not work with the LOG4CXX_STR macro | 
| 156 | 0 |   return getByName(LOG4CXX_STR("0.0.0.0")); | 
| 157 | 0 | } | 
| 158 |  |  | 
| 159 |  |  | 
| 160 |  | /** Converts this IP address to a String. | 
| 161 |  | */ | 
| 162 |  | LogString InetAddress::toString() const | 
| 163 | 0 | { | 
| 164 | 0 |   LogString rv(getHostName()); | 
| 165 | 0 |   if (!rv.empty()) | 
| 166 | 0 |     rv.append(LOG4CXX_STR("/")); | 
| 167 | 0 |   rv.append(getHostAddress()); | 
| 168 | 0 |   return rv; | 
| 169 | 0 | } | 
| 170 |  |  |