InetAddrNSPR.cpp

Go to the documentation of this file.
00001 /****************** <VPR heading BEGIN do not edit this line> *****************
00002  *
00003  * VR Juggler Portable Runtime
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Patrick Hartling, Kevin Meinert, Carolina Cruz-Neira
00007  *
00008  * -----------------------------------------------------------------
00009  * File:          $RCSfile$
00010  * Date modified: $Date: 2005-12-16 08:07:50 -0600 (Fri, 16 Dec 2005) $
00011  * Version:       $Revision: 18288 $
00012  * -----------------------------------------------------------------
00013  *
00014  ****************** <VPR heading END do not edit this line> ******************/
00015 
00016 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00017  *
00018  * VR Juggler is (C) Copyright 1998-2005 by Iowa State University
00019  *
00020  * Original Authors:
00021  *   Allen Bierbaum, Christopher Just,
00022  *   Patrick Hartling, Kevin Meinert,
00023  *   Carolina Cruz-Neira, Albert Baker
00024  *
00025  * This library is free software; you can redistribute it and/or
00026  * modify it under the terms of the GNU Library General Public
00027  * License as published by the Free Software Foundation; either
00028  * version 2 of the License, or (at your option) any later version.
00029  *
00030  * This library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00033  * Library General Public License for more details.
00034  *
00035  * You should have received a copy of the GNU Library General Public
00036  * License along with this library; if not, write to the
00037  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00038  * Boston, MA 02111-1307, USA.
00039  *
00040  *************** <auto-copyright.pl END do not edit this line> ***************/
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;      // Default constructor defaults to ANY addr
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 // Set the address for this object using the given address.  It must be of the
00082 // form <address>:<port> where <address> can be a hostname or a dotted-decimal
00083 // IP address.
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    // Extract the address and the port number from the given string.
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 // Get the protocol family of this address structure.
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 // Set the protocol family of this address structure.
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 // Get the IP address associated with this structure as a human-readable
00147 // string.
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 // Look up the address in mName and store the address in mAddr.
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);           // Error on lookup, so zero the address
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 } // End of vpr namespace

Generated on Thu Jan 4 10:52:09 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1