00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef _PIPSOCKET
00035 #define _PIPSOCKET
00036
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040
00041 #include <ptlib/socket.h>
00042
00043 #ifdef P_HAS_QOS
00044 #ifdef _WIN32
00045 #ifdef P_KNOCKOUT_WINSOCK2
00046 #include "IPExport.h"
00047 #endif // KNOCKOUT_WINSOCK2
00048 #endif // _WIN32
00049 #endif // P_HAS_QOS
00050
00058 class PIPSocket : public PSocket
00059 {
00060 PCLASSINFO(PIPSocket, PSocket);
00061 protected:
00062
00063
00064
00065 PIPSocket();
00066
00067 public:
00071 class Address : public PObject {
00072 public:
00073
00076
00077 Address();
00078
00082 Address(const PString & dotNotation);
00083
00085 Address(PINDEX len, const BYTE * bytes);
00086
00088 Address(BYTE b1, BYTE b2, BYTE b3, BYTE b4);
00089
00091 Address(DWORD dw);
00092
00094 Address(const in_addr & addr);
00095
00096 #if P_HAS_IPV6
00098 Address(const in6_addr & addr);
00099 #endif
00100
00103 Address(const int ai_family, const int ai_addrlen,struct sockaddr *ai_addr);
00104
00105 #ifdef __NUCLEUS_NET__
00106 Address(const struct id_struct & addr);
00107 Address & operator=(const struct id_struct & addr);
00108 #endif
00109
00111 Address & operator=(const in_addr & addr);
00112
00113 #if P_HAS_IPV6
00115 Address & operator=(const in6_addr & addr);
00116 #endif
00117
00119 Address & operator=(const PString & dotNotation);
00120
00122 Address & operator=(DWORD dw);
00124
00126 Comparison Compare(const PObject & obj) const;
00127 bool operator==(const Address & addr) const { return Compare(addr) == EqualTo; }
00128 bool operator!=(const Address & addr) const { return Compare(addr) != EqualTo; }
00129 #if P_HAS_IPV6
00130 bool operator==(in6_addr & addr) const;
00131 bool operator!=(in6_addr & addr) const { return !operator==(addr); }
00132 #endif
00133 bool operator==(in_addr & addr) const;
00134 bool operator!=(in_addr & addr) const { return !operator==(addr); }
00135 bool operator==(DWORD dw) const;
00136 bool operator!=(DWORD dw) const { return !operator==(dw); }
00137 #ifdef P_VXWORKS
00138 bool operator==(long unsigned int u) const { return operator==((DWORD)u); }
00139 bool operator!=(long unsigned int u) const { return !operator==((DWORD)u); }
00140 #endif
00141 #ifdef _WIN32
00142 bool operator==(unsigned u) const { return operator==((DWORD)u); }
00143 bool operator!=(unsigned u) const { return !operator==((DWORD)u); }
00144 #endif
00145 #ifdef P_RTEMS
00146 bool operator==(u_long u) const { return operator==((DWORD)u); }
00147 bool operator!=(u_long u) const { return !operator==((DWORD)u); }
00148 #endif
00149 #ifdef __BEOS__
00150 bool operator==(in_addr_t a) const { return operator==((DWORD)a); }
00151 bool operator!=(in_addr_t a) const { return !operator==((DWORD)a); }
00152 #endif
00153 bool operator==(int i) const { return operator==((DWORD)i); }
00154 bool operator!=(int i) const { return !operator==((DWORD)i); }
00155
00158 #if P_HAS_IPV6
00159 bool operator*=(const Address & addr) const;
00160 #else
00161 bool operator*=(const Address & addr) const { return operator==(addr); }
00162 #endif
00163
00165 PString AsString() const;
00166
00168 PBoolean FromString(
00169 const PString & str
00170 );
00171
00173 operator PString() const;
00174
00176 operator in_addr() const;
00177
00178 #if P_HAS_IPV6
00180 operator in6_addr() const;
00181 #endif
00182
00184 operator DWORD() const;
00185
00187 BYTE Byte1() const;
00188
00190 BYTE Byte2() const;
00191
00193 BYTE Byte3() const;
00194
00196 BYTE Byte4() const;
00197
00199 BYTE operator[](PINDEX idx) const;
00200
00202 PINDEX GetSize() const;
00203
00205 const char * GetPointer() const { return (const char *)&v; }
00206
00208 unsigned GetVersion() const { return version; }
00209
00211 PBoolean IsValid() const;
00212 PBoolean IsAny() const;
00213
00215 PBoolean IsLoopback() const;
00216
00218 PBoolean IsBroadcast() const;
00219
00220
00221
00222
00223
00224
00225
00226 PBoolean IsRFC1918() const ;
00227
00228 #if P_HAS_IPV6
00230 PBoolean IsV4Mapped() const;
00231 #endif
00232
00233 static const Address & GetLoopback();
00234 #if P_HAS_IPV6
00235 static const Address & GetLoopback6();
00236 static const Address & GetAny6();
00237 #endif
00238 static const Address & GetBroadcast();
00239
00240 protected:
00242 union {
00243 in_addr four;
00244 #if P_HAS_IPV6
00245 in6_addr six;
00246 #endif
00247 } v;
00248 unsigned version;
00249
00251 friend ostream & operator<<(ostream & s, const Address & a);
00252
00254 friend istream & operator>>(istream & s, Address & a);
00255 };
00256
00257
00265 virtual PString GetName() const;
00266
00267
00268
00269
00270
00271
00272 static int GetDefaultIpAddressFamily();
00273 static void SetDefaultIpAddressFamily(int ipAdressFamily);
00274 static void SetDefaultIpAddressFamilyV4();
00275 #if P_HAS_IPV6
00276 static void SetDefaultIpAddressFamilyV6();
00277 static PBoolean IsIpAddressFamilyV6Supported();
00278 #endif
00279 static PIPSocket::Address GetDefaultIpAny();
00280
00281
00282 virtual PBoolean OpenSocket(
00283 int ipAdressFamily=PF_INET
00284 ) = 0;
00285
00286
00287
00299 virtual PBoolean Connect(
00300 const PString & address
00301 );
00302 virtual PBoolean Connect(
00303 const Address & addr
00304 );
00305 virtual PBoolean Connect(
00306 WORD localPort,
00307 const Address & addr
00308 );
00309 virtual PBoolean Connect(
00310 const Address & iface,
00311 const Address & addr
00312 );
00313 virtual PBoolean Connect(
00314 const Address & iface,
00315 WORD localPort,
00316 const Address & addr
00317 );
00318
00334 virtual PBoolean Listen(
00335 unsigned queueSize = 5,
00336 WORD port = 0,
00337 Reusability reuse = AddressIsExclusive
00338 );
00339 virtual PBoolean Listen(
00340 const Address & bind,
00341 unsigned queueSize = 5,
00342 WORD port = 0,
00343 Reusability reuse = AddressIsExclusive
00344 );
00345
00346
00347
00355 static PString GetHostName();
00356 static PString GetHostName(
00357 const PString & hostname
00358 );
00359 static PString GetHostName(
00360 const Address & addr
00361 );
00362
00369 static PBoolean GetHostAddress(
00370 Address & addr
00371 );
00372 static PBoolean GetHostAddress(
00373 const PString & hostname,
00374
00375
00376
00377 Address & addr
00378 );
00379
00387 static PStringArray GetHostAliases(
00388 const PString & hostname
00389
00390
00391
00392 );
00393 static PStringArray GetHostAliases(
00394 const Address & addr
00395
00396
00397
00398 );
00399
00407 static PBoolean IsLocalHost(
00408 const PString & hostname
00409
00410
00411
00412 );
00413
00419 virtual PString GetLocalAddress();
00420 virtual PBoolean GetLocalAddress(
00421 Address & addr
00422 );
00423 virtual PBoolean GetLocalAddress(
00424 Address & addr,
00425 WORD & port
00426 );
00427
00434 virtual PString GetPeerAddress();
00435 virtual PBoolean GetPeerAddress(
00436 Address & addr
00437 );
00438 virtual PBoolean GetPeerAddress(
00439 Address & addr,
00440 WORD & port
00441 );
00442
00448 PString GetLocalHostName();
00449
00455 PString GetPeerHostName();
00456
00459 static void ClearNameCache();
00460
00472 static PBoolean GetGatewayAddress(
00473 Address & addr
00474 );
00475
00488 static PString GetGatewayInterface();
00489
00497 static PIPSocket::Address GetRouteInterfaceAddress(PIPSocket::Address remoteAddress);
00498
00499 #ifdef _WIN32
00500
00511 static PIPSocket::Address GetGatewayInterfaceAddress();
00512
00516 static PIPSocket::Address GetRouteAddress(PIPSocket::Address RemoteAddress);
00517
00520 static unsigned AsNumeric(Address addr);
00521
00524 static PBoolean IsAddressReachable(PIPSocket::Address LocalIP,
00525 PIPSocket::Address LocalMask,
00526 PIPSocket::Address RemoteIP);
00527
00530 static PString GetInterface(PIPSocket::Address addr);
00531 #endif
00532
00535 class RouteEntry : public PObject
00536 {
00537 PCLASSINFO(RouteEntry, PObject);
00538 public:
00540 RouteEntry(const Address & addr) : network(addr) { }
00541
00543 Address GetNetwork() const { return network; }
00544
00546 Address GetNetMask() const { return net_mask; }
00547
00549 Address GetDestination() const { return destination; }
00550
00552 const PString & GetInterface() const { return interfaceName; }
00553
00555 long GetMetric() const { return metric; }
00556
00557 protected:
00558 Address network;
00559 Address net_mask;
00560 Address destination;
00561 PString interfaceName;
00562 long metric;
00563
00564 friend class PIPSocket;
00565 };
00566
00567 PARRAY(RouteTable, RouteEntry);
00568
00574 static PBoolean GetRouteTable(
00575 RouteTable & table
00576 );
00577
00578
00582 class InterfaceEntry : public PObject
00583 {
00584 PCLASSINFO(InterfaceEntry, PObject)
00585
00586 public:
00588 InterfaceEntry();
00589 InterfaceEntry(
00590 const PString & _name,
00591 const Address & _addr,
00592 const Address & _mask,
00593 const PString & _macAddr
00594 #if P_HAS_IPV6
00595 , const PString & _ip6Addr = PString::Empty()
00596 #endif
00597 );
00598
00600 virtual void PrintOn(
00601 ostream &strm
00602 ) const;
00603
00605 const PString & GetName() const { return name; }
00606
00608 Address GetAddress() const { return ipAddr; }
00609
00610 PBoolean HasIP6Address() const
00611 #if ! P_HAS_IPV6
00612 { return PFalse;}
00613 #else
00614 { return !ip6Addr.IsEmpty();}
00615
00617 Address GetIP6Address() const { return ip6Addr; }
00618 #endif
00619
00621 Address GetNetMask() const { return netMask; }
00622
00624 const PString & GetMACAddress() const { return macAddr; }
00625
00626 protected:
00627 PString name;
00628 Address ipAddr;
00629 Address netMask;
00630 PString macAddr;
00631 #if P_HAS_IPV6
00632 PString ip6Addr;
00633 #endif
00634 };
00635
00636 PARRAY(InterfaceTable, InterfaceEntry);
00637
00642 static PBoolean GetInterfaceTable(
00643 InterfaceTable & table,
00644 PBoolean includeDown = PFalse
00645 );
00646
00651 static PBoolean GetNetworkInterface(PIPSocket::Address & addr);
00652
00653 #if P_HAS_RECVMSG
00654
00660 PBoolean SetCaptureReceiveToAddress()
00661 { if (!SetOption(IP_PKTINFO, 1, SOL_IP)) return PFalse; catchReceiveToAddr = PTrue; return PTrue; }
00662
00666 PIPSocket::Address GetLastReceiveToAddress() const
00667 { return lastReceiveToAddr; }
00668
00669 protected:
00670 void SetLastReceiveAddr(void * addr, int addrLen)
00671 { if (addrLen == sizeof(in_addr)) lastReceiveToAddr = *(in_addr *)addr; }
00672
00673 PIPSocket::Address lastReceiveToAddr;
00674
00675 #else
00676
00682 PBoolean SetCaptureReceiveToAddress()
00683 { return PFalse; }
00684
00688 PIPSocket::Address GetLastReceiveToAddress() const
00689 { return PIPSocket::Address(); }
00690
00691 #endif
00692
00693
00694 #ifdef _WIN32
00695 #include "msos/ptlib/ipsock.h"
00696 #else
00697 #include "unix/ptlib/ipsock.h"
00698 #endif
00699 };
00700
00701 class PIPSocketAddressAndPort
00702 {
00703 public:
00704 PIPSocketAddressAndPort()
00705 : port(0), sep(':')
00706 { }
00707
00708 PIPSocketAddressAndPort(char _sep)
00709 : port(0), sep(_sep)
00710 { }
00711
00712 PIPSocketAddressAndPort(const PString & str, WORD defaultPort = 0, char _sep = ':')
00713 : port(defaultPort), sep(_sep)
00714 { Parse(str, defaultPort, sep); }
00715
00716 PBoolean Parse(const PString & str, WORD defaultPort = 0, char sep = ':');
00717
00718 PString AsString(char _sep = 0) const
00719 { return address.AsString() + (_sep ? _sep : sep) + PString(PString::Unsigned, port); }
00720
00721 PIPSocket::Address address;
00722 WORD port;
00723 char sep;
00724 };
00725
00726 typedef std::vector<PIPSocketAddressAndPort> PIPSocketAddressAndPortVector;
00727
00728 #endif
00729
00730
00731