ipsocket.h

Go to the documentation of this file.
00001 /* IP sockets base class.
00002  *
00003  * PegSoft sockets library (c) 2007 PegSoft
00004  * Contact us at pegsoft@pegsoft.net
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program (see the file COPYING); if not, write to the
00018  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00024 #ifndef SOCKETS_IPSOCKET_H
00025 #define SOCKETS_IPSOCKET_H
00026 
00027 /*************************************************************************/
00028 
00029 #include <string>
00030 
00031 #include <sockets/socket.h>
00032 
00033 /*************************************************************************/
00034 
00035 #if defined(AF_INET6) && HAVE_GETADDRINFO && HAVE_INET_NTOP
00036 # define HAVE_IPV6 1
00037 #endif
00038 
00039 /*************************************************************************/
00040 
00041 namespace sockets
00042 {
00043 
00044 /*************************************************************************/
00045 
00047 
00057 class IPSocket : public Socket
00058 {
00059     public:
00061 
00070         explicit IPSocket(int type = 0, int protocol = 0) :
00071                 Socket(PF_UNSPEC, type, protocol), m_local(0), m_remote(0) { }
00073         IPSocket(IPSocket const &ips) : Socket(ips), 
00074                 m_local(ips.m_local ? new IPInfo(*ips.m_local) : 0),
00075                 m_remote(ips.m_remote ? new IPInfo(*ips.m_remote) : 0) { }
00077         virtual IPSocket &operator=(IPSocket const &right);
00079         ~IPSocket();
00080         
00082 
00095         virtual bool assign(int fd, uint32_t status);
00096         
00098 
00108         virtual bool bind();
00109         
00111 
00128         virtual bool set_local(char const *address, char const *port,
00129                 char const **error = 0);
00131         virtual bool set_local(char const *address, unsigned short port,
00132                 char const **error = 0);
00133         
00135 
00152         virtual bool set_remote(char const *address, char const *port,
00153                 char const **error = 0);
00155         virtual bool set_remote(char const *address, unsigned short port,
00156                 char const **error = 0);
00157         
00159 
00165         virtual char const *laddress() const;
00166         
00168 
00174         virtual unsigned short lport() const;
00175         
00177 
00181         virtual char const *raddress() const;
00182         
00184 
00188         virtual unsigned short rport() const;
00189     protected:
00191 
00193         class IPInfo
00194         {
00195             public:
00197                 IPInfo(int family, size_t addrlen, struct sockaddr const *addr,
00198                         char const *name, IPInfo *next);
00200                 IPInfo(IPInfo const &ipi);
00202                 ~IPInfo();
00203                 
00205                 int family() const { return m_family; }
00206                 
00208                 size_t addrlen() const { return m_addrlen; }
00210                 struct sockaddr const *addr() const 
00211                         { return (struct sockaddr *)&m_addr; }
00212                 
00214                 char const *cname() const { return m_name.c_str(); }
00216                 std::string const &name() const { return m_name; }
00217                     
00219                 IPInfo const *next() const { return m_next; }
00220             private:
00221                 int m_family;
00222                 
00223                 size_t m_addrlen;
00224 #if HAVE_IPV6
00225                 struct sockaddr_storage m_addr;
00226 #else
00227                 struct sockaddr_in m_addr;
00228 #endif
00229                 
00230                 std::string m_name;
00231                 
00232                 IPInfo *m_next;
00233                 
00234                 IPInfo &operator=(IPInfo const &);
00235         };
00236         
00238         virtual bool create();
00239         
00241 
00254         bool resolve(char const *address, char const *port,
00255                 char const **error, IPInfo **ipi);
00256         
00258         IPInfo *local() { return m_local; }
00260         IPInfo const *local() const { return m_local; }
00262         IPInfo *remote() { return m_remote; }
00264         IPInfo const *remote() const { return m_remote; }
00265         
00267         unsigned short port(IPInfo const *ipi) const;
00268     private:
00269         IPInfo *m_local;
00270         IPInfo *m_remote;
00271 };
00272 
00273 /*************************************************************************/
00274 
00275 } /* namespace sockets */
00276 
00277 /*************************************************************************/
00278 
00279 #endif /* SOCKETS_IPSOCKET_H */

Generated on Sun May 20 21:32:14 2007 for Epona API by  doxygen 1.4.6