PTLib  Version 2.10.10
ipsock.h
Go to the documentation of this file.
1 /*
2  * ipsock.h
3  *
4  * Internet Protocol socket I/O channel class.
5  *
6  * Portable Tools Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  *
29  * $Revision: 28179 $
30  * $Author: rjongbloed $
31  * $Date: 2012-08-10 06:23:27 -0500 (Fri, 10 Aug 2012) $
32  */
33 
34 #ifndef PTLIB_IPSOCKET_H
35 #define PTLIB_IPSOCKET_H
36 
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40 
41 #include <ptlib/socket.h>
42 
43 #if P_QOS
44 #ifdef _WIN32
45 #ifdef P_KNOCKOUT_WINSOCK2
46  #include "IPExport.h"
47 #endif // KNOCKOUT_WINSOCK2
48 #endif // _WIN32
49 #endif // P_QOS
50 
51 
53 
54 
55 
63 class PIPSocket : public PSocket
64 {
65  PCLASSINFO(PIPSocket, PSocket);
66  protected:
70  PIPSocket();
71 
72  public:
75  class Address : public PObject {
76  public:
77 
80  Address();
82 
86  Address(const PString & dotNotation);
87 
89  Address(PINDEX len, const BYTE * bytes);
90 
92  Address(BYTE b1, BYTE b2, BYTE b3, BYTE b4);
93 
95  Address(DWORD dw);
96 
98  Address(const in_addr & addr);
99 
100 #if P_HAS_IPV6
101  Address(const in6_addr & addr);
103 #endif
104 
107  Address(const int ai_family, const int ai_addrlen,struct sockaddr *ai_addr);
108 
109 #ifdef __NUCLEUS_NET__
110  Address(const struct id_struct & addr);
111  Address & operator=(const struct id_struct & addr);
112 #endif
113 
115  Address & operator=(const in_addr & addr);
116 
117 #if P_HAS_IPV6
118  Address & operator=(const in6_addr & addr);
120 #endif
121 
123  Address & operator=(const PString & dotNotation);
124 
126  Address & operator=(DWORD dw);
128 
130  Comparison Compare(const PObject & obj) const;
131  bool operator==(const Address & addr) const { return Compare(addr) == EqualTo; }
132  bool operator!=(const Address & addr) const { return Compare(addr) != EqualTo; }
133 #if P_HAS_IPV6
134  bool operator==(in6_addr & addr) const;
135  bool operator!=(in6_addr & addr) const { return !operator==(addr); }
136 #endif
137  bool operator==(in_addr & addr) const;
138  bool operator!=(in_addr & addr) const { return !operator==(addr); }
139  bool operator==(DWORD dw) const;
140  bool operator!=(DWORD dw) const { return !operator==(dw); }
141 #ifdef P_VXWORKS
142  bool operator==(long unsigned int u) const { return operator==((DWORD)u); }
143  bool operator!=(long unsigned int u) const { return !operator==((DWORD)u); }
144 #endif
145 #ifdef _WIN32
146  bool operator==(unsigned u) const { return operator==((DWORD)u); }
147  bool operator!=(unsigned u) const { return !operator==((DWORD)u); }
148 #endif
149 #ifdef P_RTEMS
150  bool operator==(u_long u) const { return operator==((DWORD)u); }
151  bool operator!=(u_long u) const { return !operator==((DWORD)u); }
152 #endif
153 #ifdef P_BEOS
154  bool operator==(in_addr_t a) const { return operator==((DWORD)a); }
155  bool operator!=(in_addr_t a) const { return !operator==((DWORD)a); }
156 #endif
157  bool operator==(int i) const { return operator==((DWORD)i); }
158  bool operator!=(int i) const { return !operator==((DWORD)i); }
159 
162 #if P_HAS_IPV6
163  bool operator*=(const Address & addr) const;
164 #else
165  bool operator*=(const Address & addr) const { return operator==(addr); }
166 #endif
167 
170  bool bracketIPv6 = false
171  ) const;
172 
175  const PString & str
176  );
177 
179  operator PString() const;
180 
182  operator in_addr() const;
183 
184 #if P_HAS_IPV6
185  operator in6_addr() const;
187 #endif
188 
190  operator DWORD() const;
191 
193  BYTE Byte1() const;
194 
196  BYTE Byte2() const;
197 
199  BYTE Byte3() const;
200 
202  BYTE Byte4() const;
203 
205  BYTE operator[](PINDEX idx) const;
206 
208  PINDEX GetSize() const;
209 
211  const char * GetPointer() const { return (const char *)&v; }
212 
214  unsigned GetVersion() const { return version; }
215 
217  PBoolean IsValid() const;
218  PBoolean IsAny() const;
219 
221  PBoolean IsLoopback() const;
222 
224  PBoolean IsBroadcast() const;
225 
227  PBoolean IsMulticast() const;
228 
237  PBoolean IsRFC1918() const ;
238 
239 #if P_HAS_IPV6
240  PBoolean IsV4Mapped() const;
242 
244  PBoolean IsLinkLocal() const;
245 #endif
246 
247  static const Address & GetLoopback(int version = 4);
248  static const Address & GetAny(int version = 4);
249  static const Address GetBroadcast(int version = 4);
250 
251  protected:
253  union {
254  in_addr four;
255 #if P_HAS_IPV6
256  in6_addr six;
257 #endif
258  } v;
259  unsigned version;
260 
262  friend ostream & operator<<(ostream & s, const Address & a);
263 
265  friend istream & operator>>(istream & s, Address & a);
266  };
267 
268  //**@name Overrides from class PChannel */
270 
277  virtual PString GetName() const;
278 
285  static int GetDefaultIpAddressFamily();
286  static void SetDefaultIpAddressFamily(int ipAdressFamily); // PF_INET, PF_INET6
287  static void SetDefaultIpAddressFamilyV4(); // PF_INET
288 #if P_HAS_IPV6
289  static void SetDefaultIpAddressFamilyV6(); // PF_INET6
290  static PBoolean IsIpAddressFamilyV6Supported();
291 
292  static void SetDefaultV6ScopeId(int scopeId); // local-link adresses require one
293  static int GetDefaultV6ScopeId();
294 #endif
296 
303  static void SetSuppressCanonicalName(bool suppress);
304 
311  static bool GetSuppressCanonicalName();
312 
315  virtual PBoolean OpenSocket(
316  int ipAdressFamily=PF_INET
317  ) = 0;
319 
333  virtual PBoolean Connect(
334  const PString & address
335  );
336  virtual PBoolean Connect(
337  const Address & addr
338  );
339  virtual PBoolean Connect(
340  WORD localPort,
341  const Address & addr
342  );
343  virtual PBoolean Connect(
344  const Address & iface,
345  const Address & addr
346  );
347  virtual PBoolean Connect(
348  const Address & iface,
349  WORD localPort,
350  const Address & addr
351  );
352 
368  virtual PBoolean Listen(
369  unsigned queueSize = 5,
370  WORD port = 0,
372  );
373  virtual PBoolean Listen(
374  const Address & bind,
375  unsigned queueSize = 5,
376  WORD port = 0,
378  );
380 
390  static PString GetHostName();
391  static PString GetHostName(
392  const PString & hostname
393  );
394  static PString GetHostName(
395  const Address & addr
396  );
397 
404  static PBoolean GetHostAddress(
405  Address & addr
406  );
407  static PBoolean GetHostAddress(
408  const PString & hostname,
412  Address & addr
413  );
414 
426  const PString & hostname
427  );
429  const Address & addr
430  /* Name of host to get address for. This may be either a domain name or
431  an IP number in "dot" format.
432  */
433  );
434 
442  static PBoolean IsLocalHost(
446  const PString & hostname
447  );
448 
454  virtual PString GetLocalAddress();
455  virtual PBoolean GetLocalAddress(
456  Address & addr
457  );
458  virtual PBoolean GetLocalAddress(
460  );
461  virtual PBoolean GetLocalAddress(
462  Address & addr,
463  WORD & port
464  );
465 
472  virtual PString GetPeerAddress();
473  virtual PBoolean GetPeerAddress(
474  Address & addr
475  );
476  virtual PBoolean GetPeerAddress(
478  );
479  virtual PBoolean GetPeerAddress(
480  Address & addr,
481  WORD & port
482  );
483 
490 
497 
500  static void ClearNameCache();
501 
514  Address & addr,
515  int version = 4
516  );
517 
529  static PString GetGatewayInterface(int version = 4);
530 
539 
540 #ifdef _WIN32
541 
552  static PIPSocket::Address GetGatewayInterfaceAddress(int version = 4);
553 
557  static PIPSocket::Address GetRouteAddress(PIPSocket::Address RemoteAddress);
558 
561  static unsigned AsNumeric(Address addr);
562 
565  static PBoolean IsAddressReachable(PIPSocket::Address LocalIP,
566  PIPSocket::Address LocalMask,
567  PIPSocket::Address RemoteIP);
568 
571  static PString GetInterface(PIPSocket::Address addr);
573  #endif
574 
576  class RouteEntry : public PObject
577  {
578  PCLASSINFO(RouteEntry, PObject);
579  public:
581  RouteEntry(const Address & addr) : network(addr) { }
582 
584  Address GetNetwork() const { return network; }
585 
587  Address GetNetMask() const { return net_mask; }
588 
590  Address GetDestination() const { return destination; }
591 
593  const PString & GetInterface() const { return interfaceName; }
594 
596  long GetMetric() const { return metric; }
597 
598  protected:
603  long metric;
604 
605  friend class PIPSocket;
606  };
607 
608  PARRAY(RouteTable, RouteEntry);
609 
615  static PBoolean GetRouteTable(
616  RouteTable & table
617  );
618 
621  {
622  public:
623  virtual ~RouteTableDetector() { }
624  virtual bool Wait(
625  const PTimeInterval & timeout
626  ) = 0;
627  virtual void Cancel() = 0;
628  };
629 
638  static RouteTableDetector * CreateRouteTableDetector();
639 
642  class InterfaceEntry : public PObject
643  {
645 
646  public:
648  InterfaceEntry();
650  const PString & name,
651  const Address & addr,
652  const Address & mask,
653  const PString & macAddr
654  );
655 
657  virtual void PrintOn(
658  ostream &strm // Stream to print the object into.
659  ) const;
660 
669  const PString & GetName() const { return m_name; }
670 
672  Address GetAddress() const { return m_ipAddress; }
673 
675  Address GetNetMask() const { return m_netMask; }
676 
678  const PString & GetMACAddress() const { return m_macAddress; }
679 
681  static void SanitiseName(PString & name);
682 
683  protected:
688 
689  friend class PIPSocket;
690  };
691 
692  PARRAY(InterfaceTable, InterfaceEntry);
693 
699  InterfaceTable & table,
700  PBoolean includeDown = false
701  );
702 
708 
709 #if P_HAS_RECVMSG
710 
716  { if (!SetOption(IP_PKTINFO, 1, SOL_IP)) return false; catchReceiveToAddr = true; return true; }
717 
721  { return lastReceiveToAddr; }
722 
723  protected:
724  void SetLastReceiveAddr(void * addr, int addrLen)
725  { if (addrLen == sizeof(in_addr)) lastReceiveToAddr = *(in_addr *)addr; }
726 
727  PIPSocket::Address lastReceiveToAddr;
728 
729 #else
730 
736  { return false; }
737 
741  { return PIPSocket::Address(); }
742 
743 #endif
744 
745 // Include platform dependent part of class
746 #ifdef _WIN32
747 #include "msos/ptlib/ipsock.h"
748 #else
749 #include "unix/ptlib/ipsock.h"
750 #endif
751 };
752 
754 {
755  public:
757  : m_port(0), m_separator(':')
758  { }
759 
760  PIPSocketAddressAndPort(char separator)
761  : m_port(0), m_separator(separator)
762  { }
763 
764  PIPSocketAddressAndPort(const PString & str, WORD defaultPort = 0, char separator = ':')
765  : m_port(defaultPort), m_separator(separator)
766  { Parse(str, defaultPort, m_separator); }
767 
768  PBoolean Parse(const PString & str, WORD defaultPort = 0, char separator = ':');
769 
770  PString AsString(char separator = 0) const
771  { return m_address.AsString() + (separator ? separator : m_separator) + PString(PString::Unsigned, m_port); }
772 
773  void SetAddress(
774  const PIPSocket::Address & addr,
775  WORD port = 0
776  );
777  const PIPSocket::Address & GetAddress() const { return m_address; }
778  WORD GetPort() const { return m_port; }
779  void SetPort(
780  WORD port
781  ) { m_port = port; }
782 
783  bool IsValid() const { return m_address.IsValid() && m_port != 0; }
784 
785  friend ostream & operator<<(ostream & strm, const PIPSocketAddressAndPort & ap)
786  {
787  return strm << ap.m_address << ap.m_separator << ap.m_port;
788  }
789 
790  protected:
792  WORD m_port;
794 };
795 
796 typedef std::vector<PIPSocketAddressAndPort> PIPSocketAddressAndPortVector;
797 
798 
799 #endif // PTLIB_IPSOCKET_H
800 
801 
802 // End Of File ///////////////////////////////////////////////////////////////
Class for detector of Route Table changes.
Definition: ipsock.h:620
static const Address GetBroadcast(int version=4)
PString interfaceName
Definition: ipsock.h:602
Definition: socket.h:86
BYTE Byte3() const
Return third byte of IPv4 address.
BYTE operator[](PINDEX idx) const
Return specified byte of IPv4 or IPv6 address.
static PString GetGatewayInterface(int version=4)
Get the name for the interface that is being used as the gateway, that is, the interface that packets...
virtual PBoolean Listen(unsigned queueSize=5, WORD port=0, Reusability reuse=AddressIsExclusive)
Listen on a socket for a remote host on the specified port number.
Address net_mask
Definition: ipsock.h:600
Address GetNetwork() const
Get the network address associated with the route table entry.
Definition: ipsock.h:584
Definition: object.h:1186
bool operator!=(in_addr &addr) const
Definition: ipsock.h:138
PBoolean IsMulticast() const
Check if address is multicast group.
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
bool operator==(const Address &addr) const
Definition: ipsock.h:131
bool operator!=(DWORD dw) const
Definition: ipsock.h:140
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
static int GetDefaultIpAddressFamily()
Set the default IP address familly.
PBoolean FromString(const PString &str)
Convert string to IP address. Returns true if was a valid address.
static PBoolean IsLocalHost(const PString &hostname)
Determine if the specified host is actually the local machine.
static PBoolean GetGatewayAddress(Address &addr, int version=4)
Get the IP address that is being used as the gateway, that is, the computer that packets on the defau...
friend ostream & operator<<(ostream &s, const Address &a)
Output IPv6 & IPv4 address as a string to the specified string.
bool operator!=(const Address &addr) const
Definition: ipsock.h:132
static RouteTableDetector * CreateRouteTableDetector()
Create an object that can wait for a change in the route table or active network interfaces.
PBoolean IsBroadcast() const
Check for Broadcast address 255.255.255.255.
Describe an interface table entry.
Definition: ipsock.h:642
PBoolean Parse(const PString &str, WORD defaultPort=0, char separator= ':')
static PBoolean GetHostAddress(Address &addr)
Get the Internet Protocol address for the specified host, or if none specified, for the host this pro...
static const Address & GetAny(int version=4)
This class describes a type of socket that will communicate using the Internet Protocol.
Definition: ipsock.h:63
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
Address m_netMask
Definition: ipsock.h:686
virtual PString GetLocalAddress()
Get the Internet Protocol address and port for the local host.
friend istream & operator>>(istream &s, Address &a)
Input IPv4 (not IPv6 yet!) address as a string from the specified string.
Address m_ipAddress
Definition: ipsock.h:685
const PString & GetInterface() const
Get the network address name associated with the route table entry.
Definition: ipsock.h:593
static void SetSuppressCanonicalName(bool suppress)
Set flag for suppress getting canonical name when doing lookup via hostname.
virtual void PrintOn(ostream &strm) const
Print to specified stream.
This is an array collection class of PString objects.
Definition: pstring.h:2024
PINDEX GetSize() const
Get the address length (will be either 4 or 16).
WORD port
Port to be used by the socket when opening the channel.
Definition: socket.h:444
PString GetPeerHostName()
Get the host name for the peer host the socket is connected to.
unsigned GetVersion() const
Get the version of the IP address being used.
Definition: ipsock.h:214
virtual PString GetName() const
Get the platform and I/O channel type name of the channel.
static bool GetSuppressCanonicalName()
Get flag for suppress getting canonical name when doing lookup via hostname.
PIPSocketAddressAndPort(char separator)
Definition: ipsock.h:760
void SetAddress(const PIPSocket::Address &addr, WORD port=0)
in_addr four
Definition: ipsock.h:254
BYTE Byte1() const
Return first byte of IPv4 address.
PString m_macAddress
Definition: ipsock.h:687
BYTE Byte4() const
Return fourth byte of IPv4 address.
BOOL PBoolean
Definition: object.h:102
static PBoolean GetRouteTable(RouteTable &table)
Get the systems route table.
PBoolean IsRFC1918() const
Check if the remote address is a private address.
PIPSocketAddressAndPort(const PString &str, WORD defaultPort=0, char separator= ':')
Definition: ipsock.h:764
static PIPSocket::Address GetRouteInterfaceAddress(PIPSocket::Address remoteAddress)
Get the interface address that will be used to reach the specified remote address.
RouteEntry(const Address &addr)
Create a route table entry from an IP address.
Definition: ipsock.h:581
Address GetAddress() const
Get the address associated with the interface.
Definition: ipsock.h:672
WORD m_port
Definition: ipsock.h:792
PString GetLocalHostName()
Get the host name for the local host.
PIPSocket()
Create a new Internet Protocol socket based on the port number specified.
Describe a route table entry.
Definition: ipsock.h:576
static PBoolean GetNetworkInterface(PIPSocket::Address &addr)
Get the address of an interface that corresponds to a real network.
BYTE Byte2() const
Return second byte of IPv4 address.
PString m_name
Definition: ipsock.h:684
Address GetDestination() const
Get the default gateway address associated with the route table entry.
Definition: ipsock.h:590
union PIPSocket::Address::@5 v
Runtime test of IP addresse type.
static PStringArray GetHostAliases(const PString &hostname)
Get the alias host names for the specified host.
unsigned version
Definition: ipsock.h:259
bool IsValid() const
Definition: ipsock.h:783
virtual PBoolean OpenSocket()=0
virtual PBoolean Connect(const PString &address)
Connect a socket to a remote host on the specified port number.
virtual ~RouteTableDetector()
Definition: ipsock.h:623
const char * GetPointer() const
Get the pointer to IP address data.
Definition: ipsock.h:211
WORD GetPort() const
Definition: ipsock.h:778
PIPSocket::Address m_address
Definition: ipsock.h:791
bool operator*=(const Address &addr) const
Compare two addresses for equivalence.
Definition: ipsock.h:165
static void SetDefaultIpAddressFamilyV4()
Address destination
Definition: ipsock.h:601
PARRAY(RouteTable, RouteEntry)
long GetMetric() const
Get the network metric associated with the route table entry.
Definition: ipsock.h:596
long metric
Definition: ipsock.h:603
static void ClearNameCache()
Clear the name (DNS) cache.
The character string class.
Definition: pstring.h:108
std::vector< PIPSocketAddressAndPort > PIPSocketAddressAndPortVector
Definition: ipsock.h:796
Definition: ipsock.h:753
bool operator!=(int i) const
Definition: ipsock.h:158
InterfaceEntry()
Create an interface entry from a name, IP addr and MAC addr.
void SetPort(WORD port)
Definition: ipsock.h:779
friend ostream & operator<<(ostream &strm, const PIPSocketAddressAndPort &ap)
Definition: ipsock.h:785
const PIPSocket::Address & GetAddress() const
Definition: ipsock.h:777
static const Address & GetLoopback(int version=4)
Comparison Compare(const PObject &obj) const
Compare two adresses for absolute (in)equality.
Definition: pstring.h:286
PIPSocket::Address GetLastReceiveToAddress() const
Return the interface address of the last incoming packet.
Definition: ipsock.h:740
static PIPSocket::Address GetDefaultIpAny()
Address & operator=(const in_addr &addr)
Copy an address from another IP v4 address.
A class describing an IP address.
Definition: ipsock.h:75
PBoolean IsLoopback() const
Check address 127.0.0.1 or ::1.
Address GetNetMask() const
Get the network address mask associated with the route table entry.
Definition: ipsock.h:587
PString AsString(char separator=0) const
Definition: ipsock.h:770
PBoolean IsAny() const
Address GetNetMask() const
Get the net mask associated with the interface.
Definition: ipsock.h:675
virtual PString GetPeerAddress()
Get the Internet Protocol address for the peer host and port the socket is connected to...
bool operator==(int i) const
Definition: ipsock.h:157
virtual bool Wait(const PTimeInterval &timeout)=0
Address network
Definition: ipsock.h:599
PString AsString(bool bracketIPv6=false) const
Format an address as a string.
static PBoolean GetInterfaceTable(InterfaceTable &table, PBoolean includeDown=false)
Get a list of all interfaces.
const PString & GetName() const
Get the name of the interface.
Definition: ipsock.h:669
static PString GetHostName()
Get the "official" host name for the host specified or if none, the host this process is running on...
A network communications channel.
Definition: socket.h:58
const PString & GetMACAddress() const
Get the MAC address associate with the interface.
Definition: ipsock.h:678
PBoolean SetCaptureReceiveToAddress()
Set flag to capture interface address for incoming packets.
Definition: ipsock.h:735
PIPSocketAddressAndPort()
Definition: ipsock.h:756
Reusability
Flags to reuse of port numbers in Listen() function.
Definition: socket.h:84
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
static void SanitiseName(PString &name)
Sanitise a device name for use in PTLib.
PBoolean SetOption(int option, int value, int level=SOL_SOCKET)
Set options on the socket.
static void SetDefaultIpAddressFamily(int ipAdressFamily)
PBoolean IsValid() const
Check address 0.0.0.0 or ::.
Address()
Create an IPv4 address with the default address: 127.0.0.1 (loopback).
char m_separator
Definition: ipsock.h:793