ipsock.h
Go to the documentation of this file.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 #ifndef _PIPSOCKET
00303 #define _PIPSOCKET
00304
00305 #ifdef P_USE_PRAGMA
00306 #pragma interface
00307 #endif
00308
00309 #include <ptlib/socket.h>
00310
00311 #ifdef P_HAS_QOS
00312 #ifdef _WIN32
00313 #ifdef P_KNOCKOUT_WINSOCK2
00314 #include "IPExport.h"
00315 #endif // KNOCKOUT_WINSOCK2
00316 #endif // _WIN32
00317 #endif // P_HAS_QOS
00318
00326 class PIPSocket : public PSocket
00327 {
00328 PCLASSINFO(PIPSocket, PSocket);
00329 protected:
00330
00331
00332
00333 PIPSocket();
00334
00335 public:
00339 class Address : public PObject {
00340 public:
00341
00344
00345 Address();
00346
00350 Address(const PString & dotNotation);
00351
00353 Address(PINDEX len, const BYTE * bytes);
00354
00356 Address(BYTE b1, BYTE b2, BYTE b3, BYTE b4);
00357
00359 Address(DWORD dw);
00360
00362 Address(const in_addr & addr);
00363
00364 #if P_HAS_IPV6
00366 Address(const in6_addr & addr);
00367
00370 Address(const int ai_family, const int ai_addrlen,struct sockaddr *ai_addr);
00371 #endif
00372
00373 #ifdef __NUCLEUS_NET__
00374 Address(const struct id_struct & addr);
00375 Address & operator=(const struct id_struct & addr);
00376 #endif
00377
00379 Address & operator=(const in_addr & addr);
00380
00381 #if P_HAS_IPV6
00383 Address & operator=(const in6_addr & addr);
00384 #endif
00385
00387 Address & operator=(const PString & dotNotation);
00388
00390 Address & operator=(DWORD dw);
00392
00394 Comparison Compare(const PObject & obj) const;
00395 bool operator==(const Address & addr) const { return Compare(addr) == EqualTo; }
00396 bool operator!=(const Address & addr) const { return Compare(addr) != EqualTo; }
00397 #if P_HAS_IPV6
00398 bool operator==(in6_addr & addr) const;
00399 bool operator!=(in6_addr & addr) const { return !operator==(addr); }
00400 #endif
00401 bool operator==(in_addr & addr) const;
00402 bool operator!=(in_addr & addr) const { return !operator==(addr); }
00403 bool operator==(DWORD dw) const;
00404 bool operator!=(DWORD dw) const { return !operator==(dw); }
00405 #ifdef P_VXWORKS
00406 bool operator==(long unsigned int u) const { return operator==((DWORD)u); }
00407 bool operator!=(long unsigned int u) const { return !operator==((DWORD)u); }
00408 #endif
00409 #ifdef _WIN32
00410 bool operator==(unsigned u) const { return operator==((DWORD)u); }
00411 bool operator!=(unsigned u) const { return !operator==((DWORD)u); }
00412 #endif
00413 #ifdef P_RTEMS
00414 bool operator==(u_long u) const { return operator==((DWORD)u); }
00415 bool operator!=(u_long u) const { return !operator==((DWORD)u); }
00416 #endif
00417 #ifdef __BEOS__
00418 bool operator==(in_addr_t a) const { return operator==((DWORD)a); }
00419 bool operator!=(in_addr_t a) const { return !operator==((DWORD)a); }
00420 #endif
00421 bool operator==(int i) const { return operator==((DWORD)i); }
00422 bool operator!=(int i) const { return !operator==((DWORD)i); }
00423
00426 #if P_HAS_IPV6
00427 bool operator*=(const Address & addr) const;
00428 #else
00429 bool operator*=(const Address & addr) const { return operator==(addr); }
00430 #endif
00431
00433 PString AsString() const;
00434
00436 BOOL FromString(
00437 const PString & str
00438 );
00439
00441 operator PString() const;
00442
00444 operator in_addr() const;
00445
00446 #if P_HAS_IPV6
00448 operator in6_addr() const;
00449 #endif
00450
00452 operator DWORD() const;
00453
00455 BYTE Byte1() const;
00456
00458 BYTE Byte2() const;
00459
00461 BYTE Byte3() const;
00462
00464 BYTE Byte4() const;
00465
00467 BYTE operator[](PINDEX idx) const;
00468
00470 PINDEX GetSize() const;
00471
00473 const char * GetPointer() const { return (const char *)&v; }
00474
00476 unsigned GetVersion() const { return version; }
00477
00479 BOOL IsValid() const;
00480 BOOL IsAny() const;
00481
00483 BOOL IsLoopback() const;
00484
00486 BOOL IsBroadcast() const;
00487
00488
00489
00490
00491
00492
00493
00494 BOOL IsRFC1918() const ;
00495
00496 #if P_HAS_IPV6
00498 BOOL IsV4Mapped() const;
00499 #endif
00500
00501 static const Address & GetLoopback();
00502 #if P_HAS_IPV6
00503 static const Address & GetLoopback6();
00504 static const Address & GetAny6();
00505 #endif
00506 static const Address & GetBroadcast();
00507
00508 protected:
00510 union {
00511 in_addr four;
00512 #if P_HAS_IPV6
00513 in6_addr six;
00514 #endif
00515 } v;
00516 unsigned version;
00517
00519 friend ostream & operator<<(ostream & s, const Address & a);
00520
00522 friend istream & operator>>(istream & s, Address & a);
00523 };
00524
00525
00533 virtual PString GetName() const;
00534
00535
00536
00537
00538
00539
00540 static int GetDefaultIpAddressFamily();
00541 static void SetDefaultIpAddressFamily(int ipAdressFamily);
00542 static void SetDefaultIpAddressFamilyV4();
00543 #if P_HAS_IPV6
00544 static void SetDefaultIpAddressFamilyV6();
00545 static BOOL IsIpAddressFamilyV6Supported();
00546 #endif
00547 static PIPSocket::Address GetDefaultIpAny();
00548
00549
00550 virtual BOOL OpenSocket(
00551 int ipAdressFamily=PF_INET
00552 ) = 0;
00553
00554
00555
00567 virtual BOOL Connect(
00568 const PString & address
00569 );
00570 virtual BOOL Connect(
00571 const Address & addr
00572 );
00573 virtual BOOL Connect(
00574 WORD localPort,
00575 const Address & addr
00576 );
00577 virtual BOOL Connect(
00578 const Address & iface,
00579 const Address & addr
00580 );
00581 virtual BOOL Connect(
00582 const Address & iface,
00583 WORD localPort,
00584 const Address & addr
00585 );
00586
00602 virtual BOOL Listen(
00603 unsigned queueSize = 5,
00604 WORD port = 0,
00605 Reusability reuse = AddressIsExclusive
00606 );
00607 virtual BOOL Listen(
00608 const Address & bind,
00609 unsigned queueSize = 5,
00610 WORD port = 0,
00611 Reusability reuse = AddressIsExclusive
00612 );
00613
00614
00615
00623 static PString GetHostName();
00624 static PString GetHostName(
00625 const PString & hostname
00626 );
00627 static PString GetHostName(
00628 const Address & addr
00629 );
00630
00637 static BOOL GetHostAddress(
00638 Address & addr
00639 );
00640 static BOOL GetHostAddress(
00641 const PString & hostname,
00642
00643
00644
00645 Address & addr
00646 );
00647
00655 static PStringArray GetHostAliases(
00656 const PString & hostname
00657
00658
00659
00660 );
00661 static PStringArray GetHostAliases(
00662 const Address & addr
00663
00664
00665
00666 );
00667
00675 static BOOL IsLocalHost(
00676 const PString & hostname
00677
00678
00679
00680 );
00681
00687 virtual BOOL GetLocalAddress(
00688 Address & addr
00689 );
00690 virtual BOOL GetLocalAddress(
00691 Address & addr,
00692 WORD & port
00693 );
00694
00701 virtual BOOL GetPeerAddress(
00702 Address & addr
00703 );
00704 virtual BOOL GetPeerAddress(
00705 Address & addr,
00706 WORD & port
00707 );
00708
00714 PString GetLocalHostName();
00715
00721 PString GetPeerHostName();
00722
00725 static void ClearNameCache();
00726
00738 static BOOL GetGatewayAddress(
00739 Address & addr
00740 );
00741
00754 static PString GetGatewayInterface();
00755
00756 #ifdef _WIN32
00757
00768 static PIPSocket::Address GetGatewayInterfaceAddress();
00769
00773 static PIPSocket::Address GetRouteAddress(PIPSocket::Address RemoteAddress);
00774
00777 static unsigned AsNumeric(Address addr);
00778
00781 static BOOL IsAddressReachable(PIPSocket::Address LocalIP,
00782 PIPSocket::Address LocalMask,
00783 PIPSocket::Address RemoteIP);
00784
00787 static PString GetInterface(PIPSocket::Address addr);
00788 #endif
00789
00792 class RouteEntry : public PObject
00793 {
00794 PCLASSINFO(RouteEntry, PObject);
00795 public:
00797 RouteEntry(const Address & addr) : network(addr) { }
00798
00800 Address GetNetwork() const { return network; }
00801
00803 Address GetNetMask() const { return net_mask; }
00804
00806 Address GetDestination() const { return destination; }
00807
00809 const PString & GetInterface() const { return interfaceName; }
00810
00812 long GetMetric() const { return metric; }
00813
00814 protected:
00815 Address network;
00816 Address net_mask;
00817 Address destination;
00818 PString interfaceName;
00819 long metric;
00820
00821 friend class PIPSocket;
00822 };
00823
00824 PLIST(RouteTable, RouteEntry);
00825
00831 static BOOL GetRouteTable(
00832 RouteTable & table
00833 );
00834
00835
00839 class InterfaceEntry : public PObject
00840 {
00841 PCLASSINFO(InterfaceEntry, PObject)
00842
00843 public:
00845 InterfaceEntry(
00846 const PString & _name,
00847 const Address & _addr,
00848 const Address & _mask,
00849 const PString & _macAddr
00850 #if P_HAS_IPV6
00851 , const PString & _ip6Addr = PString::Empty()
00852 #endif
00853 );
00854
00856 virtual void PrintOn(
00857 ostream &strm
00858 ) const;
00859
00861 const PString & GetName() const { return name; }
00862
00864 Address GetAddress() const { return ipAddr; }
00865
00866 BOOL HasIP6Address() const
00867 #if ! P_HAS_IPV6
00868 { return FALSE;}
00869 #else
00870 { return !ip6Addr.IsEmpty();}
00871
00873 Address GetIP6Address() const { return ip6Addr; }
00874 #endif
00875
00877 Address GetNetMask() const { return netMask; }
00878
00880 const PString & GetMACAddress() const { return macAddr; }
00881
00882 protected:
00883 PString name;
00884 Address ipAddr;
00885 Address netMask;
00886 PString macAddr;
00887 #if P_HAS_IPV6
00888 PString ip6Addr;
00889 #endif
00890 };
00891
00892 PLIST(InterfaceTable, InterfaceEntry);
00893
00898 static BOOL GetInterfaceTable(
00899 InterfaceTable & table
00900 );
00901
00906 static BOOL GetNetworkInterface(PIPSocket::Address & addr);
00907
00908 #if P_HAS_RECVMSG
00909
00915 BOOL SetCaptureReceiveToAddress()
00916 { if (!SetOption(IP_PKTINFO, 1, SOL_IP)) return FALSE; catchReceiveToAddr = TRUE; return TRUE; }
00917
00921 PIPSocket::Address GetLastReceiveToAddress() const
00922 { return lastReceiveToAddr; }
00923
00924 protected:
00925 void SetLastReceiveAddr(void * addr, int addrLen)
00926 { if (addrLen == sizeof(in_addr)) lastReceiveToAddr = *(in_addr *)addr; }
00927
00928 PIPSocket::Address lastReceiveToAddr;
00929
00930 #else
00931
00937 BOOL SetCaptureReceiveToAddress()
00938 { return FALSE; }
00939
00943 PIPSocket::Address GetLastReceiveToAddress() const
00944 { return PIPSocket::Address(); }
00945
00946 #endif
00947
00948
00949 #ifdef _WIN32
00950 #include "msos/ptlib/ipsock.h"
00951 #else
00952 #include "unix/ptlib/ipsock.h"
00953 #endif
00954 };
00955
00956 class PIPSocketAddressAndPort
00957 {
00958 public:
00959 PIPSocketAddressAndPort()
00960 : port(0)
00961 { }
00962
00963 PIPSocketAddressAndPort(const PString & str, WORD defaultPort = 0)
00964 { Parse(':', str, defaultPort); }
00965
00966 PIPSocketAddressAndPort(char sep, const PString & str, WORD defaultPort = 0)
00967 { Parse(sep, str, defaultPort); }
00968
00969 void Parse(char sep, const PString & str, WORD defaultPort = 0);
00970
00971 PString AsString() const
00972 { return address.AsString() + ':' + PString(PString::Unsigned, port); }
00973
00974 PIPSocket::Address address;
00975 WORD port;
00976 };
00977
00978 typedef std::vector<PIPSocketAddressAndPort> PIPSocketAddressAndPortVector;
00979
00980 #endif
00981
00982
00983