ipsock.h

Go to the documentation of this file.
00001 /*
00002  * ipsock.h
00003  *
00004  * Internet Protocol socket I/O channel class.
00005  *
00006  * Portable Tools Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 21963 $
00030  * $Author: rjongbloed $
00031  * $Date: 2009-01-28 19:33:59 -0600 (Wed, 28 Jan 2009) $
00032  */
00033 
00034 #ifndef PTLIB_IPSOCKET_H
00035 #define PTLIB_IPSOCKET_H
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 #include <ptlib/socket.h>
00042 
00043 #if P_QOS
00044 #ifdef _WIN32
00045 #ifdef P_KNOCKOUT_WINSOCK2
00046    #include "IPExport.h"
00047 #endif // KNOCKOUT_WINSOCK2
00048 #endif // _WIN32
00049 #endif // P_QOS
00050 
00051 
00052 class PIPSocketAddressAndPort;
00053 
00054 
00055 
00063 class PIPSocket : public PSocket
00064 {
00065   PCLASSINFO(PIPSocket, PSocket);
00066   protected:
00070     PIPSocket();
00071 
00072   public:
00075     class Address : public PObject {
00076       public:
00077 
00080 
00081         Address();
00082 
00086         Address(const PString & dotNotation);
00087 
00089         Address(PINDEX len, const BYTE * bytes);
00090 
00092         Address(BYTE b1, BYTE b2, BYTE b3, BYTE b4);
00093 
00095         Address(DWORD dw);
00096 
00098         Address(const in_addr & addr);
00099 
00100 #if P_HAS_IPV6
00102         Address(const in6_addr & addr);
00103 #endif
00104 
00107         Address(const int ai_family, const int ai_addrlen,struct sockaddr *ai_addr);
00108 
00109 #ifdef __NUCLEUS_NET__
00110         Address(const struct id_struct & addr);
00111         Address & operator=(const struct id_struct & addr);
00112 #endif
00113 
00115         Address & operator=(const in_addr & addr);
00116 
00117 #if P_HAS_IPV6
00119         Address & operator=(const in6_addr & addr);
00120 #endif
00121 
00123         Address & operator=(const PString & dotNotation);
00124 
00126         Address & operator=(DWORD dw);
00128 
00130         Comparison Compare(const PObject & obj) const;
00131         bool operator==(const Address & addr) const { return Compare(addr) == EqualTo; }
00132         bool operator!=(const Address & addr) const { return Compare(addr) != EqualTo; }
00133 #if P_HAS_IPV6
00134         bool operator==(in6_addr & addr) const;
00135         bool operator!=(in6_addr & addr) const { return !operator==(addr); }
00136 #endif
00137         bool operator==(in_addr & addr) const;
00138         bool operator!=(in_addr & addr) const { return !operator==(addr); }
00139         bool operator==(DWORD dw) const;
00140         bool operator!=(DWORD dw) const   { return !operator==(dw); }
00141 #ifdef P_VXWORKS
00142         bool operator==(long unsigned int u) const { return  operator==((DWORD)u); }
00143         bool operator!=(long unsigned int u) const { return !operator==((DWORD)u); }
00144 #endif
00145 #ifdef _WIN32
00146         bool operator==(unsigned u) const { return  operator==((DWORD)u); }
00147         bool operator!=(unsigned u) const { return !operator==((DWORD)u); }
00148 #endif
00149 #ifdef P_RTEMS
00150         bool operator==(u_long u) const { return  operator==((DWORD)u); }
00151         bool operator!=(u_long u) const { return !operator==((DWORD)u); }
00152 #endif
00153 #ifdef P_BEOS
00154         bool operator==(in_addr_t a) const { return  operator==((DWORD)a); }
00155         bool operator!=(in_addr_t a) const { return !operator==((DWORD)a); }
00156 #endif
00157         bool operator==(int i) const      { return  operator==((DWORD)i); }
00158         bool operator!=(int i) const      { return !operator==((DWORD)i); }
00159 
00162 #if P_HAS_IPV6
00163         bool operator*=(const Address & addr) const;
00164 #else
00165         bool operator*=(const Address & addr) const { return operator==(addr); }
00166 #endif
00167 
00169         PString AsString(
00170           bool bracketIPv6 = false 
00171         ) const;
00172 
00174         PBoolean FromString(
00175           const PString & str
00176         );
00177 
00179         operator PString() const;
00180 
00182         operator in_addr() const;
00183 
00184 #if P_HAS_IPV6
00186         operator in6_addr() const;
00187 #endif
00188 
00190         operator DWORD() const;
00191 
00193         BYTE Byte1() const;
00194 
00196         BYTE Byte2() const;
00197 
00199         BYTE Byte3() const;
00200 
00202         BYTE Byte4() const;
00203 
00205         BYTE operator[](PINDEX idx) const;
00206 
00208         PINDEX GetSize() const;
00209 
00211         const char * GetPointer() const { return (const char *)&v; }
00212 
00214         unsigned GetVersion() const { return version; }
00215 
00217         PBoolean IsValid() const;
00218         PBoolean IsAny() const;
00219 
00221         PBoolean IsLoopback() const;
00222 
00224         PBoolean IsBroadcast() const;
00225 
00227         PBoolean IsMulticast() const;
00228 
00237         PBoolean IsRFC1918() const ;
00238 
00239 #if P_HAS_IPV6
00241         PBoolean IsV4Mapped() const;
00242 #endif
00243 
00244         static const Address & GetLoopback(int version = 4);
00245         static const Address & GetAny(int version = 4);
00246         static const Address GetBroadcast(int version = 4);
00247 
00248       protected:
00250         union {
00251           in_addr four;
00252 #if P_HAS_IPV6
00253           in6_addr six;
00254 #endif
00255         } v;
00256         unsigned version;
00257 
00259       friend ostream & operator<<(ostream & s, const Address & a);
00260 
00262       friend istream & operator>>(istream & s, Address & a);
00263     };
00264 
00265     //**@name Overrides from class PChannel */
00267 
00274     virtual PString GetName() const;
00275 
00282     static int GetDefaultIpAddressFamily();
00283     static void SetDefaultIpAddressFamily(int ipAdressFamily); // PF_INET, PF_INET6
00284     static void SetDefaultIpAddressFamilyV4(); // PF_INET
00285 #if P_HAS_IPV6
00286     static void SetDefaultIpAddressFamilyV6(); // PF_INET6
00287     static PBoolean IsIpAddressFamilyV6Supported();
00288 
00289         static void SetDefaultV6ScopeId(int scopeId); // local-link adresses require one
00290     static int GetDefaultV6ScopeId(); 
00291 #endif
00292     static PIPSocket::Address GetDefaultIpAny();
00293 
00296     virtual PBoolean OpenSocket(
00297       int ipAdressFamily=PF_INET
00298     ) = 0;
00300 
00314     virtual PBoolean Connect(
00315       const PString & address   
00316     );
00317     virtual PBoolean Connect(
00318       const Address & addr      
00319     );
00320     virtual PBoolean Connect(
00321       WORD localPort,           
00322       const Address & addr      
00323     );
00324     virtual PBoolean Connect(
00325       const Address & iface,    
00326       const Address & addr      
00327     );
00328     virtual PBoolean Connect(
00329       const Address & iface,    
00330       WORD localPort,           
00331       const Address & addr      
00332     );
00333 
00349     virtual PBoolean Listen(
00350       unsigned queueSize = 5,  
00351       WORD port = 0,           
00352       Reusability reuse = AddressIsExclusive 
00353     );
00354     virtual PBoolean Listen(
00355       const Address & bind,     
00356       unsigned queueSize = 5,   
00357       WORD port = 0,            
00358       Reusability reuse = AddressIsExclusive 
00359     );
00361 
00371     static PString GetHostName();
00372     static PString GetHostName(
00373       const PString & hostname  
00374     );
00375     static PString GetHostName(
00376       const Address & addr    
00377     );
00378 
00385     static PBoolean GetHostAddress(
00386       Address & addr    
00387     );
00388     static PBoolean GetHostAddress(
00389       const PString & hostname,
00390       /* Name of host to get address for. This may be either a domain name or
00391          an IP number in "dot" format.
00392        */
00393       Address & addr    
00394     );
00395 
00403     static PStringArray GetHostAliases(
00407       const PString & hostname
00408     );
00409     static PStringArray GetHostAliases(
00410       const Address & addr    
00411       /* Name of host to get address for. This may be either a domain name or
00412          an IP number in "dot" format.
00413        */
00414     );
00415 
00423     static PBoolean IsLocalHost(
00427       const PString & hostname
00428     );
00429 
00435     virtual PString GetLocalAddress();
00436     virtual PBoolean GetLocalAddress(
00437       Address & addr    
00438     );
00439     virtual PBoolean GetLocalAddress(
00440       PIPSocketAddressAndPort & addr    
00441     );
00442     virtual PBoolean GetLocalAddress(
00443       Address & addr,    
00444       WORD & port        
00445     );
00446 
00453     virtual PString GetPeerAddress();
00454     virtual PBoolean GetPeerAddress(
00455       Address & addr    
00456     );
00457     virtual PBoolean GetPeerAddress(
00458       PIPSocketAddressAndPort & addr    
00459     );
00460     virtual PBoolean GetPeerAddress(
00461       Address & addr,    
00462       WORD & port        
00463     );
00464 
00470     PString GetLocalHostName();
00471 
00477     PString GetPeerHostName();
00478 
00481     static void ClearNameCache();
00482 
00494     static PBoolean GetGatewayAddress(
00495       Address & addr,     
00496           int version = 4
00497     );
00498 
00510     static PString GetGatewayInterface(int version = 4);
00511 
00519     static PIPSocket::Address GetRouteInterfaceAddress(PIPSocket::Address remoteAddress);
00520 
00521 #ifdef _WIN32
00522 
00533     static PIPSocket::Address GetGatewayInterfaceAddress(int version = 4);
00534 
00538     static PIPSocket::Address GetRouteAddress(PIPSocket::Address RemoteAddress);
00539 
00542     static unsigned AsNumeric(Address addr);
00543 
00546     static PBoolean IsAddressReachable(PIPSocket::Address LocalIP,
00547                                    PIPSocket::Address LocalMask,
00548                                    PIPSocket::Address RemoteIP);
00549 
00552     static PString GetInterface(PIPSocket::Address addr);
00554  #endif
00555 
00557     class RouteEntry : public PObject
00558     {
00559       PCLASSINFO(RouteEntry, PObject);
00560       public:
00562         RouteEntry(const Address & addr) : network(addr) { }
00563 
00565         Address GetNetwork() const { return network; }
00566 
00568         Address GetNetMask() const { return net_mask; }
00569 
00571         Address GetDestination() const { return destination; }
00572 
00574         const PString & GetInterface() const { return interfaceName; }
00575 
00577         long GetMetric() const { return metric; }
00578 
00579       protected:
00580         Address network;
00581         Address net_mask;
00582         Address destination;
00583         PString interfaceName;
00584         long    metric;
00585 
00586       friend class PIPSocket;
00587     };
00588 
00589     PARRAY(RouteTable, RouteEntry);
00590 
00596     static PBoolean GetRouteTable(
00597       RouteTable & table      
00598         );
00599 
00604     static bool WaitForRouteTableChange(
00605       const PTimeInterval & timeout,    
00606       PSyncPoint * cancellation = NULL  
00607     );
00608 
00611     class InterfaceEntry : public PObject
00612     {
00613       PCLASSINFO(InterfaceEntry, PObject)
00614 
00615       public:
00617         InterfaceEntry();
00618         InterfaceEntry(
00619           const PString & name,
00620           const Address & addr,
00621           const Address & mask,
00622           const PString & macAddr
00623 #if P_HAS_IPV6
00624           , const PString & ip6Addr = PString::Empty()
00625 #endif
00626         );
00627 
00629         virtual void PrintOn(
00630           ostream &strm   // Stream to print the object into.
00631         ) const;
00632 
00634         const PString & GetName() const { return name; }
00635 
00637         Address GetAddress() const { return ipAddr; }
00638 
00639         PBoolean HasIP6Address() const
00640 #if ! P_HAS_IPV6
00641         { return PFalse;}
00642 #else
00643         { return !ip6Addr.IsEmpty();}
00644 
00646         Address GetIP6Address() const { return ip6Addr; }
00647 #endif
00648 
00650         Address GetNetMask() const { return netMask; }
00651 
00653         const PString & GetMACAddress() const { return macAddr; }
00654 
00655       protected:
00656         PString name;
00657         Address ipAddr;
00658         Address netMask;
00659         PString macAddr;
00660 #if P_HAS_IPV6
00661         PString ip6Addr;
00662 #endif
00663     };
00664 
00665     PARRAY(InterfaceTable, InterfaceEntry);
00666 
00671     static PBoolean GetInterfaceTable(
00672       InterfaceTable & table,      
00673       PBoolean includeDown = PFalse     
00674     );
00675 
00680     static PBoolean GetNetworkInterface(PIPSocket::Address & addr);
00681 
00682 #if P_HAS_RECVMSG
00683 
00688     PBoolean SetCaptureReceiveToAddress()
00689     { if (!SetOption(IP_PKTINFO, 1, SOL_IP)) return PFalse; catchReceiveToAddr = PTrue; return PTrue; }
00690 
00693     PIPSocket::Address GetLastReceiveToAddress() const
00694     { return lastReceiveToAddr; }
00695 
00696   protected:
00697     void SetLastReceiveAddr(void * addr, int addrLen)
00698     { if (addrLen == sizeof(in_addr)) lastReceiveToAddr = *(in_addr *)addr; }
00699 
00700     PIPSocket::Address lastReceiveToAddr;
00701 
00702 #else
00703 
00708     PBoolean SetCaptureReceiveToAddress()
00709     { return PFalse; }
00710 
00713     PIPSocket::Address GetLastReceiveToAddress() const
00714     { return PIPSocket::Address(); }
00715 
00716 #endif
00717 
00718 // Include platform dependent part of class
00719 #ifdef _WIN32
00720 #include "msos/ptlib/ipsock.h"
00721 #else
00722 #include "unix/ptlib/ipsock.h"
00723 #endif
00724 };
00725 
00726 class PIPSocketAddressAndPort
00727 {
00728   public:
00729     PIPSocketAddressAndPort()
00730       : m_port(0), m_separator(':')
00731       { }
00732 
00733     PIPSocketAddressAndPort(char separator)
00734       : m_port(0), m_separator(separator)
00735       { }
00736 
00737     PIPSocketAddressAndPort(const PString & str, WORD defaultPort = 0, char separator = ':')
00738       : m_port(defaultPort), m_separator(separator)
00739       { Parse(str, defaultPort, m_separator); }
00740 
00741     PBoolean Parse(const PString & str, WORD defaultPort = 0, char separator = ':');
00742 
00743     PString AsString(char separator = 0) const
00744       { return m_address.AsString() + (separator ? separator : m_separator) + PString(PString::Unsigned, m_port); }
00745 
00746     void SetAddress(
00747       const PIPSocket::Address & addr,
00748       WORD port = 0
00749     );
00750     const PIPSocket::Address & GetAddress() const { return m_address; }
00751     WORD GetPort() const { return m_port; }
00752 
00753   protected:
00754     PIPSocket::Address m_address;
00755     WORD               m_port;
00756     char               m_separator;
00757 };
00758 
00759 typedef std::vector<PIPSocketAddressAndPort> PIPSocketAddressAndPortVector;
00760 
00761 
00762 #endif // PTLIB_IPSOCKET_H
00763 
00764 
00765 // End Of File ///////////////////////////////////////////////////////////////

Generated on Thu May 27 01:36:47 2010 for PTLib by  doxygen 1.4.7