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 #ifndef _PIPSOCKET
00280 #define _PIPSOCKET
00281
00282 #ifdef P_USE_PRAGMA
00283 #pragma interface
00284 #endif
00285
00286 #include <ptlib/socket.h>
00287
00295 class PIPSocket : public PSocket
00296 {
00297 PCLASSINFO(PIPSocket, PSocket);
00298 protected:
00299
00300
00301
00302 PIPSocket();
00303
00304 public:
00308 class Address : public PObject {
00309 public:
00310
00313
00314 Address();
00315
00319 Address(const PString & dotNotation);
00320
00322 Address(PINDEX len, const BYTE * bytes);
00323
00325 Address(BYTE b1, BYTE b2, BYTE b3, BYTE b4);
00326
00328 Address(DWORD dw);
00329
00331 Address(const in_addr & addr);
00332
00333 #if P_HAS_IPV6
00334
00335 Address(const in6_addr & addr);
00336
00339 Address(const int ai_family, const int ai_addrlen,struct sockaddr *ai_addr);
00340 #endif
00341
00342 #ifdef __NUCLEUS_NET__
00343 Address(const struct id_struct & addr);
00344 Address & operator=(const struct id_struct & addr);
00345 #endif
00346
00348 Address & operator=(const in_addr & addr);
00349
00350 #if P_HAS_IPV6
00351
00352 Address & operator=(const in6_addr & addr);
00353 #endif
00354
00356 Address & operator=(const PString & dotNotation);
00357
00359 Address & operator=(DWORD dw);
00361
00363 Comparison Compare(const PObject & obj) const;
00364 bool operator==(const Address & addr) const { return Compare(addr) == EqualTo; }
00365 bool operator!=(const Address & addr) const { return Compare(addr) != EqualTo; }
00366 #if P_HAS_IPV6
00367 bool operator==(in6_addr & addr) const;
00368 bool operator!=(in6_addr & addr) const { return !operator==(addr); }
00369 #endif
00370 bool operator==(in_addr & addr) const;
00371 bool operator!=(in_addr & addr) const { return !operator==(addr); }
00372 bool operator==(DWORD dw) const;
00373 bool operator!=(DWORD dw) const { return !operator==(dw); }
00374 #ifdef P_VXWORKS
00375 bool operator==(long unsigned int u) const { return operator==((DWORD)u); }
00376 bool operator!=(long unsigned int u) const { return !operator==((DWORD)u); }
00377 #endif
00378 #ifdef _WIN32
00379 bool operator==(unsigned u) const { return operator==((DWORD)u); }
00380 bool operator!=(unsigned u) const { return !operator==((DWORD)u); }
00381 #endif
00382 #ifdef P_RTEMS
00383 bool operator==(u_long u) const { return operator==((DWORD)u); }
00384 bool operator!=(u_long u) const { return !operator==((DWORD)u); }
00385 #endif
00386 #ifdef __BEOS__
00387 bool operator==(in_addr_t a) const { return operator==((DWORD)a); }
00388 bool operator!=(in_addr_t a) const { return !operator==((DWORD)a); }
00389 #endif
00390 bool operator==(int i) const { return operator==((DWORD)i); }
00391 bool operator!=(int i) const { return !operator==((DWORD)i); }
00392
00395 #if P_HAS_IPV6
00396 bool operator*=(const Address & addr) const;
00397 #else
00398 bool operator*=(const Address & addr) const { return operator==(addr); }
00399 #endif
00400
00402 PString AsString() const;
00403
00405 BOOL FromString(
00406 const PString & str
00407 );
00408
00410 operator PString() const;
00411
00413 operator in_addr() const;
00414
00415 #if P_HAS_IPV6
00416
00417 operator in6_addr() const;
00418 #endif
00419
00421 operator DWORD() const;
00422
00424 BYTE Byte1() const;
00425
00427 BYTE Byte2() const;
00428
00430 BYTE Byte3() const;
00431
00433 BYTE Byte4() const;
00434
00436 BYTE operator[](PINDEX idx) const;
00437
00439 PINDEX GetSize() const;
00440
00442 const char * GetPointer() const { return (const char *)&v; }
00443
00445 unsigned GetVersion() const { return version; }
00446
00448 BOOL IsValid() const;
00449 BOOL IsAny() const;
00450
00452 BOOL IsLoopback() const;
00453
00455 BOOL IsBroadcast() const;
00456
00457
00458
00459
00460
00461
00462
00463 BOOL IsRFC1918() const;
00464
00465 #if P_HAS_IPV6
00466
00467 BOOL IsV4Mapped() const;
00468 #endif
00469
00470 static const Address & GetLoopback();
00471 #if P_HAS_IPV6
00472 static const Address & GetLoopback6();
00473 static const Address & GetAny6();
00474 #endif
00475 static const Address & GetBroadcast();
00476
00477 protected:
00479 union {
00480 in_addr four;
00481 #if P_HAS_IPV6
00482 in6_addr six;
00483 #endif
00484 } v;
00485 unsigned version;
00486
00488 friend ostream & operator<<(ostream & s, const Address & a);
00489
00491 friend istream & operator>>(istream & s, Address & a);
00492 };
00493
00494
00502 virtual PString GetName() const;
00503
00504
00505
00506
00507
00508
00509 static int GetDefaultIpAddressFamily();
00510 static void SetDefaultIpAddressFamily(int ipAdressFamily);
00511 static void SetDefaultIpAddressFamilyV4();
00512 #if P_HAS_IPV6
00513 static void SetDefaultIpAddressFamilyV6();
00514 static BOOL IsIpAddressFamilyV6Supported();
00515 #endif
00516 static PIPSocket::Address GetDefaultIpAny();
00517
00518
00519 virtual BOOL OpenSocket(
00520 int ipAdressFamily=PF_INET
00521 ) = 0;
00522
00523
00524
00536 virtual BOOL Connect(
00537 const PString & address
00538 );
00539 virtual BOOL Connect(
00540 const Address & addr
00541 );
00542 virtual BOOL Connect(
00543 WORD localPort,
00544 const Address & addr
00545 );
00546 virtual BOOL Connect(
00547 const Address & iface,
00548 const Address & addr
00549 );
00550 virtual BOOL Connect(
00551 const Address & iface,
00552 WORD localPort,
00553 const Address & addr
00554 );
00555
00571 virtual BOOL Listen(
00572 unsigned queueSize = 5,
00573 WORD port = 0,
00574 Reusability reuse = AddressIsExclusive
00575 );
00576 virtual BOOL Listen(
00577 const Address & bind,
00578 unsigned queueSize = 5,
00579 WORD port = 0,
00580 Reusability reuse = AddressIsExclusive
00581 );
00582
00583
00584
00592 static PString GetHostName();
00593 static PString GetHostName(
00594 const PString & hostname
00595 );
00596 static PString GetHostName(
00597 const Address & addr
00598 );
00599
00606 static BOOL GetHostAddress(
00607 Address & addr
00608 );
00609 static BOOL GetHostAddress(
00610 const PString & hostname,
00611
00612
00613
00614 Address & addr
00615 );
00616
00624 static PStringArray GetHostAliases(
00625 const PString & hostname
00626
00627
00628
00629 );
00630 static PStringArray GetHostAliases(
00631 const Address & addr
00632
00633
00634
00635 );
00636
00644 static BOOL IsLocalHost(
00645 const PString & hostname
00646
00647
00648
00649 );
00650
00656 virtual BOOL GetLocalAddress(
00657 Address & addr
00658 );
00659 virtual BOOL GetLocalAddress(
00660 Address & addr,
00661 WORD & port
00662 );
00663
00670 virtual BOOL GetPeerAddress(
00671 Address & addr
00672 );
00673 virtual BOOL GetPeerAddress(
00674 Address & addr,
00675 WORD & port
00676 );
00677
00683 PString GetLocalHostName();
00684
00690 PString GetPeerHostName();
00691
00694 static void ClearNameCache();
00695
00707 static BOOL GetGatewayAddress(
00708 Address & addr
00709 );
00710
00723 static PString GetGatewayInterface();
00724
00728 class RouteEntry : public PObject
00729 {
00730 PCLASSINFO(RouteEntry, PObject);
00731 public:
00733 RouteEntry(const Address & addr) : network(addr) { }
00734
00736 Address GetNetwork() const { return network; }
00737
00739 Address GetNetMask() const { return net_mask; }
00740
00742 Address GetDestination() const { return destination; }
00743
00745 const PString & GetInterface() const { return interfaceName; }
00746
00748 long GetMetric() const { return metric; }
00749
00750 protected:
00751 Address network;
00752 Address net_mask;
00753 Address destination;
00754 PString interfaceName;
00755 long metric;
00756
00757 friend class PIPSocket;
00758 };
00759
00760 PLIST(RouteTable, RouteEntry);
00761
00767 static BOOL GetRouteTable(
00768 RouteTable & table
00769 );
00770
00771
00775 class InterfaceEntry : public PObject
00776 {
00777 PCLASSINFO(InterfaceEntry, PObject)
00778
00779 public:
00781 InterfaceEntry(
00782 const PString & _name,
00783 const Address & _addr,
00784 const Address & _mask,
00785 const PString & _macAddr
00786 #if P_HAS_IPV6
00787 , const PString & _ip6Addr = PString::Empty()
00788 #endif
00789 );
00790
00792 virtual void PrintOn(
00793 ostream &strm
00794 ) const;
00795
00797 const PString & GetName() const { return name; }
00798
00800 Address GetAddress() const { return ipAddr; }
00801
00802 BOOL HasIP6Address() const
00803 #if ! P_HAS_IPV6
00804 { return FALSE;}
00805 #else
00806 { return !ip6Addr.IsEmpty();}
00807
00809 Address GetIP6Address() const { return ip6Addr; }
00810 #endif
00811
00813 Address GetNetMask() const { return netMask; }
00814
00816 const PString & GetMACAddress() const { return macAddr; }
00817
00818 protected:
00819 PString name;
00820 Address ipAddr;
00821 Address netMask;
00822 PString macAddr;
00823 #if P_HAS_IPV6
00824 PString ip6Addr;
00825 #endif
00826 };
00827
00828 PLIST(InterfaceTable, InterfaceEntry);
00829
00834 static BOOL GetInterfaceTable(
00835 InterfaceTable & table
00836 );
00837
00842 static BOOL GetNetworkInterface(PIPSocket::Address & addr);
00843
00844 #if P_HAS_RECVMSG
00845
00851 BOOL SetCaptureReceiveToAddress()
00852 { if (!SetOption(IP_PKTINFO, 1, SOL_IP)) return FALSE; catchReceiveToAddr = TRUE; return TRUE; }
00853
00857 PIPSocket::Address GetLastReceiveToAddress() const
00858 { return lastReceiveToAddr; }
00859
00860 protected:
00861 void SetLastReceiveAddr(void * addr, int addrLen)
00862 { if (addrLen == sizeof(in_addr)) lastReceiveToAddr = *(in_addr *)addr; }
00863
00864 PIPSocket::Address lastReceiveToAddr;
00865
00866 #else
00867
00873 BOOL SetCaptureReceiveToAddress()
00874 { return FALSE; }
00875
00879 PIPSocket::Address GetLastReceiveToAddress() const
00880 { return PIPSocket::Address(); }
00881
00882 #endif
00883
00884
00885 #ifdef _WIN32
00886 #include "msos/ptlib/ipsock.h"
00887 #else
00888 #include "unix/ptlib/ipsock.h"
00889 #endif
00890 };
00891
00892 #endif
00893
00894
00895