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
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 #ifndef _PIPSOCKET
00324 #define _PIPSOCKET
00325
00326 #ifdef P_USE_PRAGMA
00327 #pragma interface
00328 #endif
00329
00330 #include <ptlib/socket.h>
00331
00332 #ifdef P_HAS_QOS
00333 #ifdef _WIN32
00334 #ifdef P_KNOCKOUT_WINSOCK2
00335 #include "IPExport.h"
00336 #endif // KNOCKOUT_WINSOCK2
00337 #endif // _WIN32
00338 #endif // P_HAS_QOS
00339
00347 class PIPSocket : public PSocket
00348 {
00349 PCLASSINFO(PIPSocket, PSocket);
00350 protected:
00351
00352
00353
00354 PIPSocket();
00355
00356 public:
00360 class Address : public PObject {
00361 public:
00362
00365
00366 Address();
00367
00371 Address(const PString & dotNotation);
00372
00374 Address(PINDEX len, const BYTE * bytes);
00375
00377 Address(BYTE b1, BYTE b2, BYTE b3, BYTE b4);
00378
00380 Address(DWORD dw);
00381
00383 Address(const in_addr & addr);
00384
00385 #if P_HAS_IPV6
00387 Address(const in6_addr & addr);
00388
00391 Address(const int ai_family, const int ai_addrlen,struct sockaddr *ai_addr);
00392 #endif
00393
00394 #ifdef __NUCLEUS_NET__
00395 Address(const struct id_struct & addr);
00396 Address & operator=(const struct id_struct & addr);
00397 #endif
00398
00400 Address & operator=(const in_addr & addr);
00401
00402 #if P_HAS_IPV6
00404 Address & operator=(const in6_addr & addr);
00405 #endif
00406
00408 Address & operator=(const PString & dotNotation);
00409
00411 Address & operator=(DWORD dw);
00413
00415 Comparison Compare(const PObject & obj) const;
00416 bool operator==(const Address & addr) const { return Compare(addr) == EqualTo; }
00417 bool operator!=(const Address & addr) const { return Compare(addr) != EqualTo; }
00418 #if P_HAS_IPV6
00419 bool operator==(in6_addr & addr) const;
00420 bool operator!=(in6_addr & addr) const { return !operator==(addr); }
00421 #endif
00422 bool operator==(in_addr & addr) const;
00423 bool operator!=(in_addr & addr) const { return !operator==(addr); }
00424 bool operator==(DWORD dw) const;
00425 bool operator!=(DWORD dw) const { return !operator==(dw); }
00426 #ifdef P_VXWORKS
00427 bool operator==(long unsigned int u) const { return operator==((DWORD)u); }
00428 bool operator!=(long unsigned int u) const { return !operator==((DWORD)u); }
00429 #endif
00430 #ifdef _WIN32
00431 bool operator==(unsigned u) const { return operator==((DWORD)u); }
00432 bool operator!=(unsigned u) const { return !operator==((DWORD)u); }
00433 #endif
00434 #ifdef P_RTEMS
00435 bool operator==(u_long u) const { return operator==((DWORD)u); }
00436 bool operator!=(u_long u) const { return !operator==((DWORD)u); }
00437 #endif
00438 #ifdef __BEOS__
00439 bool operator==(in_addr_t a) const { return operator==((DWORD)a); }
00440 bool operator!=(in_addr_t a) const { return !operator==((DWORD)a); }
00441 #endif
00442 bool operator==(int i) const { return operator==((DWORD)i); }
00443 bool operator!=(int i) const { return !operator==((DWORD)i); }
00444
00447 #if P_HAS_IPV6
00448 bool operator*=(const Address & addr) const;
00449 #else
00450 bool operator*=(const Address & addr) const { return operator==(addr); }
00451 #endif
00452
00454 PString AsString() const;
00455
00457 BOOL FromString(
00458 const PString & str
00459 );
00460
00462 operator PString() const;
00463
00465 operator in_addr() const;
00466
00467 #if P_HAS_IPV6
00469 operator in6_addr() const;
00470 #endif
00471
00473 operator DWORD() const;
00474
00476 BYTE Byte1() const;
00477
00479 BYTE Byte2() const;
00480
00482 BYTE Byte3() const;
00483
00485 BYTE Byte4() const;
00486
00488 BYTE operator[](PINDEX idx) const;
00489
00491 PINDEX GetSize() const;
00492
00494 const char * GetPointer() const { return (const char *)&v; }
00495
00497 unsigned GetVersion() const { return version; }
00498
00500 BOOL IsValid() const;
00501 BOOL IsAny() const;
00502
00504 BOOL IsLoopback() const;
00505
00507 BOOL IsBroadcast() const;
00508
00509
00510
00511
00512
00513
00514
00515 BOOL IsRFC1918() const ;
00516
00517 #if P_HAS_IPV6
00519 BOOL IsV4Mapped() const;
00520 #endif
00521
00522 static const Address & GetLoopback();
00523 #if P_HAS_IPV6
00524 static const Address & GetLoopback6();
00525 static const Address & GetAny6();
00526 #endif
00527 static const Address & GetBroadcast();
00528
00529 protected:
00531 union {
00532 in_addr four;
00533 #if P_HAS_IPV6
00534 in6_addr six;
00535 #endif
00536 } v;
00537 unsigned version;
00538
00540 friend ostream & operator<<(ostream & s, const Address & a);
00541
00543 friend istream & operator>>(istream & s, Address & a);
00544 };
00545
00546
00554 virtual PString GetName() const;
00555
00556
00557
00558
00559
00560
00561 static int GetDefaultIpAddressFamily();
00562 static void SetDefaultIpAddressFamily(int ipAdressFamily);
00563 static void SetDefaultIpAddressFamilyV4();
00564 #if P_HAS_IPV6
00565 static void SetDefaultIpAddressFamilyV6();
00566 static BOOL IsIpAddressFamilyV6Supported();
00567 #endif
00568 static PIPSocket::Address GetDefaultIpAny();
00569
00570
00571 virtual BOOL OpenSocket(
00572 int ipAdressFamily=PF_INET
00573 ) = 0;
00574
00575
00576
00588 virtual BOOL Connect(
00589 const PString & address
00590 );
00591 virtual BOOL Connect(
00592 const Address & addr
00593 );
00594 virtual BOOL Connect(
00595 WORD localPort,
00596 const Address & addr
00597 );
00598 virtual BOOL Connect(
00599 const Address & iface,
00600 const Address & addr
00601 );
00602 virtual BOOL Connect(
00603 const Address & iface,
00604 WORD localPort,
00605 const Address & addr
00606 );
00607
00623 virtual BOOL Listen(
00624 unsigned queueSize = 5,
00625 WORD port = 0,
00626 Reusability reuse = AddressIsExclusive
00627 );
00628 virtual BOOL Listen(
00629 const Address & bind,
00630 unsigned queueSize = 5,
00631 WORD port = 0,
00632 Reusability reuse = AddressIsExclusive
00633 );
00634
00635
00636
00644 static PString GetHostName();
00645 static PString GetHostName(
00646 const PString & hostname
00647 );
00648 static PString GetHostName(
00649 const Address & addr
00650 );
00651
00658 static BOOL GetHostAddress(
00659 Address & addr
00660 );
00661 static BOOL GetHostAddress(
00662 const PString & hostname,
00663
00664
00665
00666 Address & addr
00667 );
00668
00676 static PStringArray GetHostAliases(
00677 const PString & hostname
00678
00679
00680
00681 );
00682 static PStringArray GetHostAliases(
00683 const Address & addr
00684
00685
00686
00687 );
00688
00696 static BOOL IsLocalHost(
00697 const PString & hostname
00698
00699
00700
00701 );
00702
00708 virtual PString GetLocalAddress();
00709 virtual BOOL GetLocalAddress(
00710 Address & addr
00711 );
00712 virtual BOOL GetLocalAddress(
00713 Address & addr,
00714 WORD & port
00715 );
00716
00723 virtual PString GetPeerAddress();
00724 virtual BOOL GetPeerAddress(
00725 Address & addr
00726 );
00727 virtual BOOL GetPeerAddress(
00728 Address & addr,
00729 WORD & port
00730 );
00731
00737 PString GetLocalHostName();
00738
00744 PString GetPeerHostName();
00745
00748 static void ClearNameCache();
00749
00761 static BOOL GetGatewayAddress(
00762 Address & addr
00763 );
00764
00777 static PString GetGatewayInterface();
00778
00779 #ifdef _WIN32
00780
00791 static PIPSocket::Address GetGatewayInterfaceAddress();
00792
00796 static PIPSocket::Address GetRouteAddress(PIPSocket::Address RemoteAddress);
00797
00800 static unsigned AsNumeric(Address addr);
00801
00804 static BOOL IsAddressReachable(PIPSocket::Address LocalIP,
00805 PIPSocket::Address LocalMask,
00806 PIPSocket::Address RemoteIP);
00807
00810 static PString GetInterface(PIPSocket::Address addr);
00811 #endif
00812
00815 class RouteEntry : public PObject
00816 {
00817 PCLASSINFO(RouteEntry, PObject);
00818 public:
00820 RouteEntry(const Address & addr) : network(addr) { }
00821
00823 Address GetNetwork() const { return network; }
00824
00826 Address GetNetMask() const { return net_mask; }
00827
00829 Address GetDestination() const { return destination; }
00830
00832 const PString & GetInterface() const { return interfaceName; }
00833
00835 long GetMetric() const { return metric; }
00836
00837 protected:
00838 Address network;
00839 Address net_mask;
00840 Address destination;
00841 PString interfaceName;
00842 long metric;
00843
00844 friend class PIPSocket;
00845 };
00846
00847 PLIST(RouteTable, RouteEntry);
00848
00854 static BOOL GetRouteTable(
00855 RouteTable & table
00856 );
00857
00858
00862 class InterfaceEntry : public PObject
00863 {
00864 PCLASSINFO(InterfaceEntry, PObject)
00865
00866 public:
00868 InterfaceEntry();
00869 InterfaceEntry(
00870 const PString & _name,
00871 const Address & _addr,
00872 const Address & _mask,
00873 const PString & _macAddr
00874 #if P_HAS_IPV6
00875 , const PString & _ip6Addr = PString::Empty()
00876 #endif
00877 );
00878
00880 virtual void PrintOn(
00881 ostream &strm
00882 ) const;
00883
00885 const PString & GetName() const { return name; }
00886
00888 Address GetAddress() const { return ipAddr; }
00889
00890 BOOL HasIP6Address() const
00891 #if ! P_HAS_IPV6
00892 { return FALSE;}
00893 #else
00894 { return !ip6Addr.IsEmpty();}
00895
00897 Address GetIP6Address() const { return ip6Addr; }
00898 #endif
00899
00901 Address GetNetMask() const { return netMask; }
00902
00904 const PString & GetMACAddress() const { return macAddr; }
00905
00906 protected:
00907 PString name;
00908 Address ipAddr;
00909 Address netMask;
00910 PString macAddr;
00911 #if P_HAS_IPV6
00912 PString ip6Addr;
00913 #endif
00914 };
00915
00916 PLIST(InterfaceTable, InterfaceEntry);
00917
00922 static BOOL GetInterfaceTable(
00923 InterfaceTable & table,
00924 BOOL includeDown = FALSE
00925 );
00926
00931 static BOOL GetNetworkInterface(PIPSocket::Address & addr);
00932
00933 #if P_HAS_RECVMSG
00934
00940 BOOL SetCaptureReceiveToAddress()
00941 { if (!SetOption(IP_PKTINFO, 1, SOL_IP)) return FALSE; catchReceiveToAddr = TRUE; return TRUE; }
00942
00946 PIPSocket::Address GetLastReceiveToAddress() const
00947 { return lastReceiveToAddr; }
00948
00949 protected:
00950 void SetLastReceiveAddr(void * addr, int addrLen)
00951 { if (addrLen == sizeof(in_addr)) lastReceiveToAddr = *(in_addr *)addr; }
00952
00953 PIPSocket::Address lastReceiveToAddr;
00954
00955 #else
00956
00962 BOOL SetCaptureReceiveToAddress()
00963 { return FALSE; }
00964
00968 PIPSocket::Address GetLastReceiveToAddress() const
00969 { return PIPSocket::Address(); }
00970
00971 #endif
00972
00973
00974 #ifdef _WIN32
00975 #include "msos/ptlib/ipsock.h"
00976 #else
00977 #include "unix/ptlib/ipsock.h"
00978 #endif
00979 };
00980
00981 class PIPSocketAddressAndPort
00982 {
00983 public:
00984 PIPSocketAddressAndPort()
00985 : port(0), sep(':')
00986 { }
00987
00988 PIPSocketAddressAndPort(char _sep)
00989 : port(0), sep(_sep)
00990 { }
00991
00992 PIPSocketAddressAndPort(const PString & str, WORD defaultPort = 0, char _sep = ':')
00993 : sep(_sep)
00994 { Parse(str, defaultPort, sep); }
00995
00996 BOOL Parse(const PString & str, WORD defaultPort = 0, char sep = ':');
00997
00998 PString AsString(char _sep = 0) const
00999 { return address.AsString() + (_sep ? _sep : sep) + PString(PString::Unsigned, port); }
01000
01001 PIPSocket::Address address;
01002 WORD port;
01003 char sep;
01004 };
01005
01006 typedef std::vector<PIPSocketAddressAndPort> PIPSocketAddressAndPortVector;
01007
01008 #endif
01009
01010
01011