PTLib  Version 2.10.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
81  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 
102  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 
119  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 
186  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 
241  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 ///////////////////////////////////////////////////////////////