"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/Host.cc" (11 Jul 2020, 4293 Bytes) of package /linux/misc/xosview-1.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Host.cc" see the Fossies "Dox" file reference documentation.

    1 //
    2 //  Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
    3 //
    4 //  This file may be distributed under terms of the GPL
    5 //
    6 
    7 #include <stdlib.h>
    8 #include <string.h>
    9 #include "Host.h"
   10 
   11 #if defined(__hpux__) || defined(__hpux)
   12 extern int h_errno;
   13 #endif
   14 
   15 
   16 Host::Host(const char *hostname){
   17   struct hostent *hent = gethostbyname(hostname);
   18 
   19   check(hent);
   20 
   21   copy(hent);
   22 }
   23 
   24 Host::Host(const struct in_addr *address){
   25   constuct(address);
   26 }
   27 
   28 Host::Host(unsigned int addr){
   29   struct in_addr ia;
   30   ia.s_addr = htonl(addr);
   31   constuct(&ia);
   32 }
   33 
   34 bool Host::constuct(const struct in_addr *address){
   35   struct hostent *hent = gethostbyaddr((char *)address, sizeof(in_addr),
   36                                        AF_INET);
   37   bool tmp = check(hent);
   38 
   39   copy(hent);
   40 
   41   return tmp;
   42 }
   43 
   44 void Host::copy(const struct hostent *hent){
   45   _hent.h_name = NULL;
   46   _hent.h_aliases = NULL;
   47   _numAliases = 0;
   48   _hent.h_addrtype = -1;
   49   _hent.h_length = -1;
   50   _numAddresses = 0;
   51   _hent.h_addr_list = NULL;
   52   _valid = false;
   53 
   54   if (hent != NULL){
   55     // Then it is valid.
   56     _valid = true;
   57 
   58     // find the number of aliases.
   59     char **tmp = hent->h_aliases;
   60     while (*tmp != NULL){
   61       _numAliases++;
   62       tmp++;
   63     }
   64 
   65     // copy the official name.
   66     int hnamelen = strlen(hent->h_name) + 1;
   67     _hent.h_name = new char[hnamelen];
   68     strncpy((char *)_hent.h_name, hent->h_name, hnamelen);
   69 
   70     // copy the aliases.
   71     _hent.h_aliases = new char *[_numAliases + 1];
   72     for (int i = 0 ; i < _numAliases ; i++){
   73       int len = strlen(hent->h_aliases[i]) + 1;
   74       _hent.h_aliases[i] = new char[len + 1];
   75       strncpy(_hent.h_aliases[i], hent->h_aliases[i], len);
   76     }
   77     _hent.h_aliases[_numAliases] = NULL;
   78 
   79     // copy the address type and length.
   80     _hent.h_addrtype = hent->h_addrtype;
   81     _hent.h_length = hent->h_length;
   82 
   83     // find the number of addresses.
   84     char **taddr = hent->h_addr_list;
   85     while (*taddr != NULL){
   86       _numAddresses++;
   87       taddr++;
   88     }
   89 
   90     // copy the addresses.
   91     _hent.h_addr_list = new char *[_numAddresses + 1];
   92     for (int j = 0 ; j < _numAddresses ; j++){
   93       _hent.h_addr_list[j] = (char *)new in_addr;
   94       memcpy(_hent.h_addr_list[j], hent->h_addr_list[j], sizeof(in_addr));
   95     }
   96     _hent.h_addr_list[_numAddresses] = NULL;
   97   }
   98 }
   99 
  100 Host::~Host(void){
  101   clear();
  102 }
  103 
  104 void Host::clear(void){
  105   _valid = false;
  106 
  107   delete[] _hent.h_name;
  108   _hent.h_name = NULL;
  109 
  110   for (int i = 0 ; i < _numAliases ; i++)
  111     delete[] _hent.h_aliases[i];
  112   delete[] _hent.h_aliases;
  113   _hent.h_aliases = NULL;
  114 
  115   for (int j = 0 ; j < _numAddresses ; j++)
  116     delete _hent.h_addr_list[j];
  117   delete[] _hent.h_addr_list;
  118   _hent.h_addr_list = NULL;
  119 
  120   _numAliases = 0;
  121   _hent.h_addrtype = -1;
  122   _hent.h_length = -1;
  123   _numAddresses = 0;
  124 }
  125 
  126 bool Host::check(const struct hostent *hent) {
  127   if (hent != NULL)  // all is well
  128     return true;
  129 
  130   _valid = false;
  131   _failure = h_errno;
  132   return false;
  133 }
  134 
  135 int Host::reasonForFailure(void) const{
  136   if (_valid)
  137     return 0; //NETDB_SUCCESS;
  138 
  139   return _failure;
  140 }
  141 
  142 bool Host::tryAgain(void) const {
  143   if (reasonForFailure() == TRY_AGAIN)
  144     return true;
  145 
  146   return false;
  147 }
  148 
  149 bool Host::operator==(const Host& host) const {
  150   if (valid() != host.valid())
  151     return false;
  152 
  153   if (!valid())  // they are both invalid
  154     return true;
  155 
  156   // now check all of the addresses for each host.
  157   for (int i = 0 ; i < _numAddresses ; i++)
  158     for (int j = 0 ; j < host.numAddresses() ; j++)
  159       if (address(i)->s_addr == host.address(j)->s_addr)
  160         return true;
  161 
  162   return false;
  163 }
  164 
  165 std::ostream &Host::print(std::ostream& os) const {
  166   /*  Cast 'this' to a char*, so we don't need to create a Host::! operator.*/
  167   if (!*((char*)this))
  168     return os <<"Invalid Host.  h_errno was = " <<_failure <<"\n";
  169 
  170   os <<"---- Host ----\n"
  171      <<"Official Name    = " <<officialName() <<"\n"
  172      <<"Aliases          = ";
  173 
  174   for (int i = 0 ; i < numAliases() ; i++){
  175     os <<alias(i);
  176     if (i != numAliases() - 1)
  177       os <<", ";
  178   }
  179   os <<"\n";
  180 
  181   os <<"Address Type     = " <<addrType() <<"\n"
  182      <<"Address Length   = " <<addrLength() <<"\n"
  183      <<"Addresses        = ";
  184 
  185   for (int j = 0 ; j < numAddresses() ; j++){
  186     os <<strAddress(j);
  187     if (j != numAddresses() - 1)
  188       os <<", ";
  189   }
  190 
  191   return os;
  192 }