00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <vpr/vprConfig.h>
00043
00044 #include <stdio.h>
00045 #include <prsystem.h>
00046
00047 #include <vpr/md/NSPR/IO/Socket/InetAddrNSPR.h>
00048 #include <vpr/Util/Error.h>
00049
00050
00051 namespace vpr
00052 {
00053
00054 vpr::ReturnStatus getIfAddrs(std::vector<vpr::InetAddrNSPR>& hostAddrs,
00055 const bool withLoopback);
00056
00057 const InetAddrNSPR InetAddrNSPR::AnyAddr;
00058
00059 vpr::ReturnStatus InetAddrNSPR::getLocalHost(vpr::InetAddrNSPR& hostAddr)
00060 {
00061 vpr::ReturnStatus status(vpr::ReturnStatus::Fail);
00062 char local_host_name[257];
00063 memset(local_host_name, 0, 257);
00064
00065 if ( PR_GetSystemInfo(PR_SI_HOSTNAME, local_host_name, 256) == PR_SUCCESS )
00066 {
00067 hostAddr.setAddress(std::string(local_host_name), 0);
00068 status.setCode(vpr::ReturnStatus::Succeed);
00069 }
00070
00071 return status;
00072 }
00073
00074 vpr::ReturnStatus
00075 InetAddrNSPR::getAllLocalAddrs(std::vector<vpr::InetAddrNSPR>& hostAddrs,
00076 const bool withLoopback)
00077 {
00078 return vpr::getIfAddrs(hostAddrs, withLoopback);
00079 }
00080
00081
00082
00083
00084 vpr::ReturnStatus InetAddrNSPR::setAddress(const std::string& address)
00085 {
00086 std::string::size_type pos;
00087 std::string host_addr, host_port;
00088 vpr::Uint16 port;
00089 vpr::ReturnStatus retval;
00090
00091
00092 pos = address.find(":");
00093 host_addr = address.substr(0, pos);
00094 host_port = address.substr(pos + 1);
00095 port = (vpr::Uint16) atoi(host_port.c_str());
00096
00097 retval = lookupAddress(host_addr);
00098 setPort(port);
00099 setFamily(vpr::SocketTypes::INET);
00100 return retval;
00101 }
00102
00103
00104 vpr::SocketTypes::Domain InetAddrNSPR::getFamily() const
00105 {
00106 vpr::SocketTypes::Domain family;
00107
00108 switch ( PR_NetAddrFamily(&mAddr) )
00109 {
00110 case PR_AF_INET:
00111 family = vpr::SocketTypes::INET;
00112 break;
00113 case PR_AF_LOCAL:
00114 family = vpr::SocketTypes::LOCAL;
00115 break;
00116 case PR_AF_INET6:
00117 family = vpr::SocketTypes::INET6;
00118 break;
00119 }
00120
00121 return family;
00122 }
00123
00124
00125 void InetAddrNSPR::setFamily(const vpr::SocketTypes::Domain family)
00126 {
00127 switch ( family )
00128 {
00129 case vpr::SocketTypes::LOCAL:
00130 PR_NetAddrFamily(&mAddr) = PR_AF_LOCAL;
00131 break;
00132 case vpr::SocketTypes::INET:
00133 PR_NetAddrFamily(&mAddr) = PR_AF_INET;
00134 break;
00135 case vpr::SocketTypes::INET6:
00136 PR_NetAddrFamily(&mAddr) = PR_AF_INET6;
00137 break;
00138 default:
00139 fprintf(stderr,
00140 "[vpr::InetAddrNSPR] ERROR: Unknown socket family value %d\n",
00141 family);
00142 break;
00143 }
00144 }
00145
00146
00147
00148 std::string InetAddrNSPR::getAddressString() const
00149 {
00150 char ip_str[256];
00151 memset(ip_str, 0, 256);
00152 PR_NetAddrToString(&mAddr, ip_str, sizeof(PRNetAddr));
00153 std::string temp(ip_str);
00154 return temp;
00155 }
00156
00157 vpr::ReturnStatus InetAddrNSPR::getHostname(std::string& hostname) const
00158 {
00159 vpr::ReturnStatus status;
00160 char buffer[PR_NETDB_BUF_SIZE];
00161 memset(buffer, 0, PR_NETDB_BUF_SIZE);
00162 PRStatus ret_status;
00163 PRHostEnt hostent;
00164
00165 ret_status = PR_GetHostByAddr(&mAddr, buffer, sizeof(buffer), &hostent);
00166
00167 if ( ret_status == PR_FAILURE )
00168 {
00169 vpr::Error::outputCurrentError(std::cerr, "[InetAddrNSPR::getHostname] Failed to get host by address");
00170 status.setCode(vpr::ReturnStatus::Fail);
00171 }
00172 else
00173 {
00174 hostname = hostent.h_name;
00175 }
00176
00177 return status;
00178 }
00179
00180 std::vector<std::string> InetAddrNSPR::getHostnames() const
00181 {
00182 std::vector<std::string> names;
00183 char buffer[PR_NETDB_BUF_SIZE];
00184 PRStatus ret_status;
00185 PRHostEnt hostent;
00186
00187 ret_status = PR_GetHostByAddr(&mAddr, buffer, sizeof(buffer), &hostent);
00188
00189 if ( ret_status != PR_FAILURE )
00190 {
00191 names.push_back(std::string(hostent.h_name));
00192
00193 for ( char** ptr = hostent.h_aliases; *ptr != NULL; ptr++ )
00194 {
00195 names.push_back(std::string(*ptr));
00196 }
00197 }
00198
00199 return names;
00200 }
00201
00202
00203 vpr::ReturnStatus InetAddrNSPR::lookupAddress(const std::string& address)
00204 {
00205 vpr::ReturnStatus retval;
00206 PRStatus ret_status;
00207 PRHostEnt host_entry;
00208 char buffer[PR_NETDB_BUF_SIZE];
00209
00210 ret_status = PR_GetHostByName(address.c_str(), buffer, sizeof(buffer),
00211 &host_entry);
00212
00213 if ( ret_status == PR_FAILURE )
00214 {
00215 setAddressValue(0);
00216 std::string error_msg("[InetAddrNSPR::lookupAddress] Fail to look up host: ");
00217 error_msg += address;
00218
00219 vpr::Error::outputCurrentError(std::cerr, error_msg);
00220 retval.setCode(ReturnStatus::Fail);
00221 }
00222 else
00223 {
00224 if ( PR_EnumerateHostEnt(0, &host_entry, 0, &mAddr) == -1 )
00225 {
00226 retval.setCode(ReturnStatus::Fail);
00227 }
00228
00229 if ( retval.failure() )
00230 {
00231 vpr::Error::outputCurrentError(std::cerr, "[InetAddrNSPR::lookupAddress] Could not enumerate host entry");
00232 }
00233 }
00234
00235 return retval;
00236 }
00237
00238 }