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 #if defined(AF_UNIX)
00040 # define HAVE_UNIX 1
00041 #endif
00042 
00043 /*************************************************************************/
00044 
00045 namespace sockets
00046 {
00047 
00048 /*************************************************************************/
00049 
00051 
00065 class IPSocket : public Socket
00066 {
00067     public:
00069         friend class TCPSocket;
00070 
00072 
00081         explicit IPSocket(int type = 0, int protocol = 0) :
00082                 Socket(PF_UNSPEC, type, protocol), m_local(0), m_remote(0) { }
00084         IPSocket(IPSocket const &ips) : Socket(ips), 
00085                 m_local(ips.m_local ? new IPInfo(*ips.m_local) : 0),
00086                 m_remote(ips.m_remote ? new IPInfo(*ips.m_remote) : 0) { }
00088         virtual IPSocket &operator=(IPSocket const &right);
00090         ~IPSocket();
00091         
00093 
00106         virtual bool assign(int fd, uint32_t status);
00107         
00109 
00119         virtual bool bind();
00120         
00122 
00139         virtual bool set_local(char const *address, char const *port,
00140                 char const **error = 0);
00142         virtual bool set_local(char const *address, unsigned short port,
00143                 char const **error = 0);
00144         
00146 
00155         virtual bool set_local(char const *file);
00156         
00158 
00175         virtual bool set_remote(char const *address, char const *port,
00176                 char const **error = 0);
00178         virtual bool set_remote(char const *address, unsigned short port,
00179                 char const **error = 0);
00180         
00182 
00191         virtual bool set_remote(char const *file);
00192         
00194 
00203         virtual char const *laddress() const;
00204         
00206 
00215         virtual unsigned short lport() const;
00216         
00218 
00227         virtual char const *lfile() const;
00228         
00230 
00237         virtual char const *raddress() const;
00238         
00240 
00247         virtual unsigned short rport() const;
00248         
00250 
00257         virtual char const *rfile() const;
00258     protected:
00260 
00262         class IPInfo
00263         {
00264             public:
00266                 IPInfo(int family, size_t addrlen, struct sockaddr const *addr,
00267                         char const *name, IPInfo *next);
00269                 IPInfo(IPInfo const &ipi);
00271                 ~IPInfo();
00272                 
00274                 int family() const { return m_family; }
00275                 
00277                 size_t addrlen() const { return m_addrlen; }
00279                 struct sockaddr const *addr() const;
00280                 
00282                 char const *cname() const { return m_name.c_str(); }
00284                 std::string const &name() const { return m_name; }
00285                     
00287                 IPInfo const *next() const { return m_next; }
00288             private:
00289                 int m_family;
00290                 
00291                 size_t m_addrlen;
00292 #if HAVE_IPV6
00293                 struct sockaddr_storage m_addr;
00294 #else
00295                 struct sockaddr_in m_addr;
00296 #if HAVE_UNIX
00297                 struct sockaddr_un m_addr_unix;
00298 #endif
00299 #endif
00300                 
00301                 std::string m_name;
00302                 
00303                 IPInfo *m_next;
00304                 
00305                 IPInfo &operator=(IPInfo const &);
00306         };
00307         
00309         virtual bool create();
00310         
00312 
00325         bool resolve(char const *address, char const *port,
00326                 char const **error, IPInfo **ipi);
00327         
00329         IPInfo *local() { return m_local; }
00331         IPInfo const *local() const { return m_local; }
00333         IPInfo *remote() { return m_remote; }
00335         IPInfo const *remote() const { return m_remote; }
00336         
00338         unsigned short port(IPInfo const *ipi) const;
00339     private:
00340         IPInfo *m_local;
00341         IPInfo *m_remote;
00342 };
00343 
00344 /*************************************************************************/
00345 
00346 } /* namespace sockets */
00347 
00348 /*************************************************************************/
00349 
00350 #endif /* SOCKETS_IPSOCKET_H */

Generated on Fri Apr 18 22:03:27 2008 for Epona API by  doxygen 1.5.3