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 #ifndef _PIPSOCKET
00295 #define _PIPSOCKET
00296
00297 #ifdef P_USE_PRAGMA
00298 #pragma interface
00299 #endif
00300
00301 #include <ptlib/socket.h>
00302
00303 #ifdef P_HAS_QOS
00304 #ifdef _WIN32
00305 #ifdef P_KNOCKOUT_WINSOCK2
00306 #include "IPExport.h"
00307 #endif // KNOCKOUT_WINSOCK2
00308 #endif // _WIN32
00309 #endif // P_HAS_QOS
00310
00318 class PIPSocket : public PSocket
00319 {
00320 PCLASSINFO(PIPSocket, PSocket);
00321 protected:
00322
00323
00324
00325 PIPSocket();
00326
00327 public:
00331 class Address : public PObject {
00332 public:
00333
00336
00337 Address();
00338
00342 Address(const PString & dotNotation);
00343
00345 Address(PINDEX len, const BYTE * bytes);
00346
00348 Address(BYTE b1, BYTE b2, BYTE b3, BYTE b4);
00349
00351 Address(DWORD dw);
00352
00354 Address(const in_addr & addr);
00355
00356 #if P_HAS_IPV6
00358 Address(const in6_addr & addr);
00359
00362 Address(const int ai_family, const int ai_addrlen,struct sockaddr *ai_addr);
00363 #endif
00364
00365 #ifdef __NUCLEUS_NET__
00366 Address(const struct id_struct & addr);
00367 Address & operator=(const struct id_struct & addr);
00368 #endif
00369
00371 Address & operator=(const in_addr & addr);
00372
00373 #if P_HAS_IPV6
00375 Address & operator=(const in6_addr & addr);
00376 #endif
00377
00379 Address & operator=(const PString & dotNotation);
00380
00382 Address & operator=(DWORD dw);
00384
00386 Comparison Compare(const PObject & obj) const;
00387 bool operator==(const Address & addr) const { return Compare(addr) == EqualTo; }
00388 bool operator!=(const Address & addr) const { return Compare(addr) != EqualTo; }
00389 #if P_HAS_IPV6
00390 bool operator==(in6_addr & addr) const;
00391 bool operator!=(in6_addr & addr) const { return !operator==(addr); }
00392 #endif
00393 bool operator==(in_addr & addr) const;
00394 bool operator!=(in_addr & addr) const { return !operator==(addr); }
00395 bool operator==(DWORD dw) const;
00396 bool operator!=(DWORD dw) const { return !operator==(dw); }
00397 #ifdef P_VXWORKS
00398 bool operator==(long unsigned int u) const { return operator==((DWORD)u); }
00399 bool operator!=(long unsigned int u) const { return !operator==((DWORD)u); }
00400 #endif
00401 #ifdef _WIN32
00402 bool operator==(unsigned u) const { return operator==((DWORD)u); }
00403 bool operator!=(unsigned u) const { return !operator==((DWORD)u); }
00404 #endif
00405 #ifdef P_RTEMS
00406 bool operator==(u_long u) const { return operator==((DWORD)u); }
00407 bool operator!=(u_long u) const { return !operator==((DWORD)u); }
00408 #endif
00409 #ifdef __BEOS__
00410 bool operator==(in_addr_t a) const { return operator==((DWORD)a); }
00411 bool operator!=(in_addr_t a) const { return !operator==((DWORD)a); }
00412 #endif
00413 bool operator==(int i) const { return operator==((DWORD)i); }
00414 bool operator!=(int i) const { return !operator==((DWORD)i); }
00415
00418 #if P_HAS_IPV6
00419 bool operator*=(const Address & addr) const;
00420 #else
00421 bool operator*=(const Address & addr) const { return operator==(addr); }
00422 #endif
00423
00425 PString AsString() const;
00426
00428 BOOL FromString(
00429 const PString & str
00430 );
00431
00433 operator PString() const;
00434
00436 operator in_addr() const;
00437
00438 #if P_HAS_IPV6
00440 operator in6_addr() const;
00441 #endif
00442
00444 operator DWORD() const;
00445
00447 BYTE Byte1() const;
00448
00450 BYTE Byte2() const;
00451
00453 BYTE Byte3() const;
00454
00456 BYTE Byte4() const;
00457
00459 BYTE operator[](PINDEX idx) const;
00460
00462 PINDEX GetSize() const;
00463
00465 const char * GetPointer() const { return (const char *)&v; }
00466
00468 unsigned GetVersion() const { return version; }
00469
00471 BOOL IsValid() const;
00472 BOOL IsAny() const;
00473
00475 BOOL IsLoopback() const;
00476
00478 BOOL IsBroadcast() const;
00479
00480
00481
00482
00483
00484
00485
00486 BOOL IsRFC1918() const ;
00487
00488 #if P_HAS_IPV6
00490 BOOL IsV4Mapped() const;
00491 #endif
00492
00493 static const Address & GetLoopback();
00494 #if P_HAS_IPV6
00495 static const Address & GetLoopback6();
00496 static const Address & GetAny6();
00497 #endif
00498 static const Address & GetBroadcast();
00499
00500 protected:
00502 union {
00503 in_addr four;
00504 #if P_HAS_IPV6
00505 in6_addr six;
00506 #endif
00507 } v;
00508 unsigned version;
00509
00511 friend ostream & operator<<(ostream & s, const PString & str);
00512
00514 friend ostream & operator<<(ostream & s, const Address & a);
00515
00517 friend istream & operator>>(istream & s, Address & a);
00518 };
00519
00520
00528 virtual PString GetName() const;
00529
00530
00531
00532
00533
00534
00535 static int GetDefaultIpAddressFamily();
00536 static void SetDefaultIpAddressFamily(int ipAdressFamily);
00537 static void SetDefaultIpAddressFamilyV4();
00538 #if P_HAS_IPV6
00539 static void SetDefaultIpAddressFamilyV6();
00540 static BOOL IsIpAddressFamilyV6Supported();
00541 #endif
00542 static PIPSocket::Address GetDefaultIpAny();
00543
00544
00545 virtual BOOL OpenSocket(
00546 int ipAdressFamily=PF_INET
00547 ) = 0;
00548
00549
00550
00562 virtual BOOL Connect(
00563 const PString & address
00564 );
00565 virtual BOOL Connect(
00566 const Address & addr
00567 );
00568 virtual BOOL Connect(
00569 WORD localPort,
00570 const Address & addr
00571 );
00572 virtual BOOL Connect(
00573 const Address & iface,
00574 const Address & addr
00575 );
00576 virtual BOOL Connect(
00577 const Address & iface,
00578 WORD localPort,
00579 const Address & addr
00580 );
00581
00597 virtual BOOL Listen(
00598 unsigned queueSize = 5,
00599 WORD port = 0,
00600 Reusability reuse = AddressIsExclusive
00601 );
00602 virtual BOOL Listen(
00603 const Address & bind,
00604 unsigned queueSize = 5,
00605 WORD port = 0,
00606 Reusability reuse = AddressIsExclusive
00607 );
00608
00609
00610
00618 static PString GetHostName();
00619 static PString GetHostName(
00620 const PString & hostname
00621 );
00622 static PString GetHostName(
00623 const Address & addr
00624 );
00625
00632 static BOOL GetHostAddress(
00633 Address & addr
00634 );
00635 static BOOL GetHostAddress(
00636 const PString & hostname,
00637
00638
00639
00640 Address & addr
00641 );
00642
00650 static PStringArray GetHostAliases(
00651 const PString & hostname
00652
00653
00654
00655 );
00656 static PStringArray GetHostAliases(
00657 const Address & addr
00658
00659
00660
00661 );
00662
00670 static BOOL IsLocalHost(
00671 const PString & hostname
00672
00673
00674
00675 );
00676
00682 virtual BOOL GetLocalAddress(
00683 Address & addr
00684 );
00685 virtual BOOL GetLocalAddress(
00686 Address & addr,
00687 WORD & port
00688 );
00689
00696 virtual BOOL GetPeerAddress(
00697 Address & addr
00698 );
00699 virtual BOOL GetPeerAddress(
00700 Address & addr,
00701 WORD & port
00702 );
00703
00709 PString GetLocalHostName();
00710
00716 PString GetPeerHostName();
00717
00720 static void ClearNameCache();
00721
00733 static BOOL GetGatewayAddress(
00734 Address & addr
00735 );
00736
00749 static PString GetGatewayInterface();
00750
00751 #ifdef _WIN32
00752
00763 static PIPSocket::Address GetGatewayInterfaceAddress();
00764
00768 static PIPSocket::Address GetRouteAddress(PIPSocket::Address RemoteAddress);
00769
00772 static unsigned AsNumeric(Address addr);
00773
00776 static BOOL IsAddressReachable(PIPSocket::Address LocalIP,
00777 PIPSocket::Address LocalMask,
00778 PIPSocket::Address RemoteIP);
00779
00782 static PString GetInterface(PIPSocket::Address addr);
00783 #endif
00784
00787 class RouteEntry : public PObject
00788 {
00789 PCLASSINFO(RouteEntry, PObject);
00790 public:
00792 RouteEntry(const Address & addr) : network(addr) { }
00793
00795 Address GetNetwork() const { return network; }
00796
00798 Address GetNetMask() const { return net_mask; }
00799
00801 Address GetDestination() const { return destination; }
00802
00804 const PString & GetInterface() const { return interfaceName; }
00805
00807 long GetMetric() const { return metric; }
00808
00809 protected:
00810 Address network;
00811 Address net_mask;
00812 Address destination;
00813 PString interfaceName;
00814 long metric;
00815
00816 friend class PIPSocket;
00817 };
00818
00819 PLIST(RouteTable, RouteEntry);
00820
00826 static BOOL GetRouteTable(
00827 RouteTable & table
00828 );
00829
00830
00834 class InterfaceEntry : public PObject
00835 {
00836 PCLASSINFO(InterfaceEntry, PObject)
00837
00838 public:
00840 InterfaceEntry(
00841 const PString & _name,
00842 const Address & _addr,
00843 const Address & _mask,
00844 const PString & _macAddr
00845 #if P_HAS_IPV6
00846 , const PString & _ip6Addr = PString::Empty()
00847 #endif
00848 );
00849
00851 virtual void PrintOn(
00852 ostream &strm
00853 ) const;
00854
00856 const PString & GetName() const { return name; }
00857
00859 Address GetAddress() const { return ipAddr; }
00860
00861 BOOL HasIP6Address() const
00862 #if ! P_HAS_IPV6
00863 { return FALSE;}
00864 #else
00865 { return !ip6Addr.IsEmpty();}
00866
00868 Address GetIP6Address() const { return ip6Addr; }
00869 #endif
00870
00872 Address GetNetMask() const { return netMask; }
00873
00875 const PString & GetMACAddress() const { return macAddr; }
00876
00877 protected:
00878 PString name;
00879 Address ipAddr;
00880 Address netMask;
00881 PString macAddr;
00882 #if P_HAS_IPV6
00883 PString ip6Addr;
00884 #endif
00885 };
00886
00887 PLIST(InterfaceTable, InterfaceEntry);
00888
00893 static BOOL GetInterfaceTable(
00894 InterfaceTable & table
00895 );
00896
00901 static BOOL GetNetworkInterface(PIPSocket::Address & addr);
00902
00903 #if P_HAS_RECVMSG
00904
00910 BOOL SetCaptureReceiveToAddress()
00911 { if (!SetOption(IP_PKTINFO, 1, SOL_IP)) return FALSE; catchReceiveToAddr = TRUE; return TRUE; }
00912
00916 PIPSocket::Address GetLastReceiveToAddress() const
00917 { return lastReceiveToAddr; }
00918
00919 protected:
00920 void SetLastReceiveAddr(void * addr, int addrLen)
00921 { if (addrLen == sizeof(in_addr)) lastReceiveToAddr = *(in_addr *)addr; }
00922
00923 PIPSocket::Address lastReceiveToAddr;
00924
00925 #else
00926
00932 BOOL SetCaptureReceiveToAddress()
00933 { return FALSE; }
00934
00938 PIPSocket::Address GetLastReceiveToAddress() const
00939 { return PIPSocket::Address(); }
00940
00941 #endif
00942
00943
00944 #ifdef _WIN32
00945 #include "msos/ptlib/ipsock.h"
00946 #else
00947 #include "unix/ptlib/ipsock.h"
00948 #endif
00949 };
00950
00951 class PIPSocketAddressAndPort
00952 {
00953 public:
00954 PIPSocketAddressAndPort()
00955 : port(0)
00956 { }
00957
00958 PIPSocket::Address address;
00959 WORD port;
00960 };
00961
00962 typedef std::vector<PIPSocketAddressAndPort> PIPSocketAddressAndPortVector;
00963
00964 #endif
00965
00966
00967