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 #ifndef _Host_h 8 #define _Host_h 9 10 #include <sys/types.h> 11 #include <sys/socket.h> 12 #include <netinet/in.h> 13 #include <netdb.h> 14 #include <arpa/inet.h> 15 #include <iostream> 16 17 class Host { 18 public: 19 Host(const char *hostname); 20 Host(const struct in_addr *address); 21 Host(unsigned int addr); 22 Host(const Host& host) { copy(host); _failure = host._failure; } 23 virtual ~Host(void); 24 25 Host &operator=(const Host& host) { 26 clear(); 27 copy(host); 28 _failure = host._failure; 29 30 return *this; 31 } 32 33 bool operator==(const Host& host) const; 34 bool operator!=(const Host& host) const { return !(*this == host); } 35 36 // indicates a valid Host. 37 bool valid(void) const { return _valid; } 38 operator bool(void) const { return _valid; } 39 int reasonForFailure(void) const; // returns h_errno at time of failure 40 bool tryAgain(void) const; // Ok to try again? 41 42 const char *officialName(void) const { return _hent.h_name; } 43 44 int numAliases(void) const { return _numAliases; } 45 const char *alias(int num) const { return _hent.h_aliases[num]; } 46 47 int addrType(void) const { return _hent.h_addrtype; } 48 int addrLength(void) const { return _hent.h_length; } 49 50 int numAddresses(void) const { return _numAddresses; } 51 struct in_addr *address(int num) const { 52 return (in_addr *)_hent.h_addr_list[num]; 53 } 54 55 // Linux will choke and die inside of inet_ntoa() when 56 // this function is called. The Host class has been run 57 // through purify and the problem is not in it. Must be 58 // another linux library problem :(. 59 const char *strAddress(int num) const { return inet_ntoa(*address(num)); } 60 61 // Should not use this under linux for the same reashon as the above 62 // function. 63 std::ostream &print(std::ostream &os) const; 64 65 protected: 66 private: 67 struct hostent _hent; 68 int _numAliases; 69 int _numAddresses; 70 bool _valid; 71 int _failure; 72 73 void copy(const Host& host) { copy(&host._hent); } 74 void copy(const struct hostent *hent); 75 void clear(void); 76 bool check(const struct hostent *hent); 77 bool constuct(const struct in_addr *address); 78 }; 79 80 // Do not use this under linux until inet_ntoa() is fixed. 81 inline std::ostream &operator<<(std::ostream &os, const Host& host) { 82 return host.print(os); 83 } 84 85 #endif