| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | #ifndef _Host_h |
|---|
| 11 | #define _Host_h |
|---|
| 12 | |
|---|
| 13 | #define HOST_H_CVSID "$Id: Host.h,v 1.1.1.1 2008/05/04 15:53:48 emasson Exp $" |
|---|
| 14 | |
|---|
| 15 | #include <sys/types.h> |
|---|
| 16 | #include <sys/socket.h> |
|---|
| 17 | #include <netinet/in.h> |
|---|
| 18 | #include <netdb.h> |
|---|
| 19 | #include <arpa/inet.h> |
|---|
| 20 | #ifdef HAVE_IOSTREAM |
|---|
| 21 | #include <iostream> |
|---|
| 22 | #else |
|---|
| 23 | #include <iostream.h> |
|---|
| 24 | #endif |
|---|
| 25 | #include "bool.h" |
|---|
| 26 | |
|---|
| 27 | class Host { |
|---|
| 28 | public: |
|---|
| 29 | Host(const char *hostname); |
|---|
| 30 | Host(const struct in_addr *address); |
|---|
| 31 | Host(unsigned int addr); |
|---|
| 32 | Host(const Host& host) { copy(host); _failure = host._failure; } |
|---|
| 33 | virtual ~Host(void); |
|---|
| 34 | |
|---|
| 35 | Host &operator=(const Host& host) { |
|---|
| 36 | clear(); |
|---|
| 37 | copy(host); |
|---|
| 38 | _failure = host._failure; |
|---|
| 39 | |
|---|
| 40 | return *this; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | bool operator==(const Host& host) const; |
|---|
| 44 | bool operator!=(const Host& host) const { return !(*this == host); } |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | bool valid(void) const { return _valid; } |
|---|
| 48 | operator bool(void) const { return _valid; } |
|---|
| 49 | int reasonForFailure(void) const; |
|---|
| 50 | bool tryAgain(void) const; |
|---|
| 51 | |
|---|
| 52 | const char *officialName(void) const { return _hent.h_name; } |
|---|
| 53 | |
|---|
| 54 | int numAliases(void) const { return _numAliases; } |
|---|
| 55 | const char *alias(int num) const { return _hent.h_aliases[num]; } |
|---|
| 56 | |
|---|
| 57 | int addrType(void) const { return _hent.h_addrtype; } |
|---|
| 58 | int addrLength(void) const { return _hent.h_length; } |
|---|
| 59 | |
|---|
| 60 | int numAddresses(void) const { return _numAddresses; } |
|---|
| 61 | struct in_addr *address(int num) const { |
|---|
| 62 | return (in_addr *)_hent.h_addr_list[num]; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | const char *strAddress(int num) const { return inet_ntoa(*address(num)); } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | std::ostream &print(std::ostream &os) const; |
|---|
| 74 | |
|---|
| 75 | protected: |
|---|
| 76 | private: |
|---|
| 77 | struct hostent _hent; |
|---|
| 78 | int _numAliases; |
|---|
| 79 | int _numAddresses; |
|---|
| 80 | bool _valid; |
|---|
| 81 | int _failure; |
|---|
| 82 | |
|---|
| 83 | void copy(const Host& host) { copy(&host._hent); } |
|---|
| 84 | void copy(const struct hostent *hent); |
|---|
| 85 | void clear(void); |
|---|
| 86 | bool check(const struct hostent *hent); |
|---|
| 87 | bool constuct(const struct in_addr *address); |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | inline std::ostream &operator<<(std::ostream &os, const Host& host) { |
|---|
| 92 | return host.print(os); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | #endif |
|---|