PTLib  Version 2.10.4
 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: 25231 $
30  * $Author: rjongbloed $
31  * $Date: 2011-03-02 08:41:46 -0600 (Wed, 02 Mar 2011) $
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 
299  virtual PBoolean OpenSocket(
300  int ipAdressFamily=PF_INET
301  ) = 0;
303 
317  virtual PBoolean Connect(
318  const PString & address
319  );
320  virtual PBoolean Connect(
321  const Address & addr
322  );
323  virtual PBoolean Connect(
324  WORD localPort,
325  const Address & addr
326  );
327  virtual PBoolean Connect(
328  const Address & iface,
329  const Address & addr
330  );
331  virtual PBoolean Connect(
332  const Address & iface,
333  WORD localPort,
334  const Address & addr
335  );
336 
352  virtual PBoolean Listen(
353  unsigned queueSize = 5,
354  WORD port = 0,
356  );
357  virtual PBoolean Listen(
358  const Address & bind,
359  unsigned queueSize = 5,
360  WORD port = 0,
362  );
364 
374  static PString GetHostName();
375  static PString GetHostName(
376  const PString & hostname
377  );
378  static PString GetHostName(
379  const Address & addr
380  );
381 
388  static PBoolean GetHostAddress(
389  Address & addr
390  );
391  static PBoolean GetHostAddress(
392  const PString & hostname,
396  Address & addr
397  );
398 
410  const PString & hostname
411  );
413  const Address & addr
414  /* Name of host to get address for. This may be either a domain name or
415  an IP number in "dot" format.
416  */
417  );
418 
426  static PBoolean IsLocalHost(
430  const PString & hostname
431  );
432 
438  virtual PString GetLocalAddress();
439  virtual PBoolean GetLocalAddress(
440  Address & addr
441  );
442  virtual PBoolean GetLocalAddress(
444  );
445  virtual PBoolean GetLocalAddress(
446  Address & addr,
447  WORD & port
448  );
449 
456  virtual PString GetPeerAddress();
457  virtual PBoolean GetPeerAddress(
458  Address & addr
459  );
460  virtual PBoolean GetPeerAddress(
462  );
463  virtual PBoolean GetPeerAddress(
464  Address & addr,
465  WORD & port
466  );
467 
474 
481 
484  static void ClearNameCache();
485 
498  Address & addr,
499  int version = 4
500  );
501 
513  static PString GetGatewayInterface(int version = 4);
514 
523 
524 #ifdef _WIN32
525 
536  static PIPSocket::Address GetGatewayInterfaceAddress(int version = 4);
537 
541  static PIPSocket::Address GetRouteAddress(PIPSocket::Address RemoteAddress);
542 
545  static unsigned AsNumeric(Address addr);
546 
549  static PBoolean IsAddressReachable(PIPSocket::Address LocalIP,
550  PIPSocket::Address LocalMask,
551  PIPSocket::Address RemoteIP);
552 
555  static PString GetInterface(PIPSocket::Address addr);
557  #endif
558 
560  class RouteEntry : public PObject
561  {
562  PCLASSINFO(RouteEntry, PObject);
563  public:
565  RouteEntry(const Address & addr) : network(addr) { }
566 
568  Address GetNetwork() const { return network; }
569 
571  Address GetNetMask() const { return net_mask; }
572 
574  Address GetDestination() const { return destination; }
575 
577  const PString & GetInterface() const { return interfaceName; }
578 
580  long GetMetric() const { return metric; }
581 
582  protected:
587  long metric;
588 
589  friend class PIPSocket;
590  };
591 
592  PARRAY(RouteTable, RouteEntry);
593 
599  static PBoolean GetRouteTable(
600  RouteTable & table
601  );
602 
605  {
606  public:
607  virtual ~RouteTableDetector() { }
608  virtual bool Wait(
609  const PTimeInterval & timeout
610  ) = 0;
611  virtual void Cancel() = 0;
612  };
613 
622  static RouteTableDetector * CreateRouteTableDetector();
623 
626  class InterfaceEntry : public PObject
627  {
629 
630  public:
632  InterfaceEntry();
634  const PString & name,
635  const Address & addr,
636  const Address & mask,
637  const PString & macAddr
638  );
639 
641  virtual void PrintOn(
642  ostream &strm // Stream to print the object into.
643  ) const;
644 
646  const PString & GetName() const { return name; }
647 
649  Address GetAddress() const { return ipAddr; }
650 
652  Address GetNetMask() const { return netMask; }
653 
655  const PString & GetMACAddress() const { return macAddr; }
656 
657  protected:
662 
663  friend class PIPSocket;
664  };
665 
666  PARRAY(InterfaceTable, InterfaceEntry);
667 
673  InterfaceTable & table,
674  PBoolean includeDown = false
675  );
676 
682 
683 #if P_HAS_RECVMSG
684 
690  { if (!SetOption(IP_PKTINFO, 1, SOL_IP)) return false; catchReceiveToAddr = true; return true; }
691 
695  { return lastReceiveToAddr; }
696 
697  protected:
698  void SetLastReceiveAddr(void * addr, int addrLen)
699  { if (addrLen == sizeof(in_addr)) lastReceiveToAddr = *(in_addr *)addr; }
700 
701  PIPSocket::Address lastReceiveToAddr;
702 
703 #else
704 
710  { return false; }
711 
715  { return PIPSocket::Address(); }
716 
717 #endif
718 
719 // Include platform dependent part of class
720 #ifdef _WIN32
721 #include "msos/ptlib/ipsock.h"
722 #else
723 #include "unix/ptlib/ipsock.h"
724 #endif
725 };
726 
728 {
729  public:
731  : m_port(0), m_separator(':')
732  { }
733 
734  PIPSocketAddressAndPort(char separator)
735  : m_port(0), m_separator(separator)
736  { }
737 
738  PIPSocketAddressAndPort(const PString & str, WORD defaultPort = 0, char separator = ':')
739  : m_port(defaultPort), m_separator(separator)
740  { Parse(str, defaultPort, m_separator); }
741 
742  PBoolean Parse(const PString & str, WORD defaultPort = 0, char separator = ':');
743 
744  PString AsString(char separator = 0) const
745  { return m_address.AsString() + (separator ? separator : m_separator) + PString(PString::Unsigned, m_port); }
746 
747  void SetAddress(
748  const PIPSocket::Address & addr,
749  WORD port = 0
750  );
751  const PIPSocket::Address & GetAddress() const { return m_address; }
752  WORD GetPort() const { return m_port; }
753  void SetPort(
754  WORD port
755  ) { m_port = port; }
756 
757  bool IsValid() const { return m_address.IsValid() && m_port != 0; }
758 
759  friend ostream & operator<<(ostream & strm, const PIPSocketAddressAndPort & ap)
760  {
761  return strm << ap.m_address << ap.m_separator << ap.m_port;
762  }
763 
764  protected:
766  WORD m_port;
768 };
769 
770 typedef std::vector<PIPSocketAddressAndPort> PIPSocketAddressAndPortVector;
771 
772 
773 #endif // PTLIB_IPSOCKET_H
774 
775 
776 // End Of File ///////////////////////////////////////////////////////////////