00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _pnetdb_h_
00021 #define _pnetdb_h_
00022
00023 #include <pclasses/pexport.h>
00024 #include <pclasses/pexception.h>
00025 #include <pclasses/psocket.h>
00026 #include <string>
00027 #include <vector>
00028
00029 struct hostent;
00030 struct servent;
00031
00032 namespace P {
00033
00035
00039 class PNET_EXPORT NetDbError: public RuntimeError {
00040 public:
00041 inline NetDbError(int err, const char* _what, const SourceInfo& _si) throw()
00042 : RuntimeError(_what,_si), m_err(err) {}
00043
00044 inline BaseError* clone() const
00045 { return new NetDbError(*this); }
00046
00047 inline int error() const throw()
00048 { return m_err; }
00049
00050 std::string text() const throw();
00051
00052 private:
00053 int m_err;
00054 };
00055
00057
00061 class PNET_EXPORT NetDb {
00062 public:
00063
00065
00069 class PNET_EXPORT HostEntry {
00070 friend class NetDb;
00071
00072 public:
00073 HostEntry();
00074 HostEntry(const HostEntry& h);
00075 HostEntry(const hostent* h);
00076 ~HostEntry();
00077
00078 inline int domain() const
00079 { return m_domain; }
00080
00081 inline const std::string& name() const
00082 { return m_name; }
00083
00084 inline const std::string& alias(int num) const
00085 { return m_aliases[num]; }
00086
00087 inline const NetworkAddress& addr(int num) const
00088 { return *m_addrs[num]; }
00089
00090 inline int addrCount() const
00091 { return m_addrs.size(); }
00092
00093 HostEntry& operator=(const HostEntry& h);
00094
00095 private:
00096 int m_domain;
00097 std::string m_name;
00098 std::vector<std::string> m_aliases;
00099 std::vector<NetworkAddress*> m_addrs;
00100 };
00101
00103
00107 class PNET_EXPORT ServiceEntry {
00108 friend class NetDb;
00109
00110 public:
00111 ServiceEntry();
00112 ServiceEntry(const ServiceEntry& s);
00113 ServiceEntry(const servent* s);
00114 ~ServiceEntry();
00115
00116 inline const std::string& name() const
00117 { return m_name; }
00118
00119 inline const std::string& alias(int num) const
00120 { return m_aliases[num]; }
00121
00122 inline port_t port() const
00123 { return m_port; }
00124
00125 inline const std::string& protocol() const
00126 { return m_proto; }
00127
00128 ServiceEntry& operator=(const ServiceEntry& s);
00129
00130 private:
00131 std::string m_name;
00132 std::vector<
00133 std::string> m_aliases;
00134 port_t m_port;
00135 std::string m_proto;
00136 };
00137
00138
00140 static HostEntry hostByName(const std::string& name, int domain) throw(NetDbError);
00141
00143 static HostEntry hostByAddress(const NetworkAddress& addr) throw(NetDbError);
00144
00146 static ServiceEntry serviceByName(const std::string& name, const std::string& proto) throw(NetDbError);
00147
00149 static ServiceEntry serviceByPort(port_t port, const std::string& proto) throw(NetDbError);
00150
00151 };
00152
00153 }
00154
00155 #endif