Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

ipsock.h

Go to the documentation of this file.
00001 /* 00002 * ipsock.h 00003 * 00004 * Internet Protocol socket I/O channel class. 00005 * 00006 * Portable Windows Library 00007 * 00008 * Copyright (c) 1993-1998 Equivalence Pty. Ltd. 00009 * 00010 * The contents of this file are subject to the Mozilla Public License 00011 * Version 1.0 (the "License"); you may not use this file except in 00012 * compliance with the License. You may obtain a copy of the License at 00013 * http://www.mozilla.org/MPL/ 00014 * 00015 * Software distributed under the License is distributed on an "AS IS" 00016 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00017 * the License for the specific language governing rights and limitations 00018 * under the License. 00019 * 00020 * The Original Code is Portable Windows Library. 00021 * 00022 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 00023 * 00024 * Portions are Copyright (C) 1993 Free Software Foundation, Inc. 00025 * All Rights Reserved. 00026 * 00027 * Contributor(s): ______________________________________. 00028 * 00029 * $Log: ipsock.h,v $ 00030 * Revision 1.66 2004/07/11 07:56:35 csoutheren 00031 * Applied jumbo VxWorks patch, thanks to Eize Slange 00032 * 00033 * Revision 1.65 2004/04/18 04:33:36 rjongbloed 00034 * Changed all operators that return BOOL to return standard type bool. This is primarily 00035 * for improved compatibility with std STL usage removing many warnings. 00036 * 00037 * Revision 1.64 2004/04/07 05:29:50 csoutheren 00038 * Added function to detect RFC 1918 addresses 00039 * 00040 * Revision 1.63 2004/02/23 17:27:19 ykiryanov 00041 * Added == and != operators for in_addr_t on BeOS as suggested by Craig Southeren to please compiler 00042 * 00043 * Revision 1.62 2003/09/17 05:41:58 csoutheren 00044 * Removed recursive includes 00045 * 00046 * Revision 1.61 2003/09/17 01:18:02 csoutheren 00047 * Removed recursive include file system and removed all references 00048 * to deprecated coooperative threading support 00049 * 00050 * Revision 1.60 2003/05/21 09:34:43 rjongbloed 00051 * Name lookup support for IPv6, thanks again Sébastien Josset 00052 * 00053 * Revision 1.59 2003/04/28 02:55:08 robertj 00054 * Added function to see at run time if IPv6 available, thanks Sebastien Josset 00055 * 00056 * Revision 1.58 2003/04/03 08:43:23 robertj 00057 * Added IPv4 mapping into IPv6, thanks Sebastien Josset 00058 * 00059 * Revision 1.57 2003/03/26 05:36:37 robertj 00060 * More IPv6 support (INADDR_ANY handling), thanks Sébastien Josset 00061 * 00062 * Revision 1.56 2003/02/03 11:23:32 robertj 00063 * Added function to get pointer to IP address data. 00064 * 00065 * Revision 1.55 2003/02/03 08:51:44 robertj 00066 * Fixed compatibility with old code so taking address of PIPSocket::Address 00067 * gets address of 4 or 16 byte IP address. 00068 * 00069 * Revision 1.54 2002/12/02 03:57:18 robertj 00070 * More RTEMS support patches, thank you Vladimir Nesic. 00071 * 00072 * Revision 1.53 2002/11/02 00:32:21 robertj 00073 * Further fixes to VxWorks (Tornado) port, thanks Andreas Sikkema. 00074 * 00075 * Revision 1.52 2002/10/29 07:59:45 robertj 00076 * Changed in_addr6 to more universally used in6_addr. 00077 * 00078 * Revision 1.51 2002/10/08 14:31:43 robertj 00079 * Changed for IPv6 support, thanks Sébastien Josset. 00080 * 00081 * Revision 1.50 2002/10/08 12:41:51 robertj 00082 * Changed for IPv6 support, thanks Sébastien Josset. 00083 * 00084 * Revision 1.49 2002/09/16 01:08:59 robertj 00085 * Added #define so can select if #pragma interface/implementation is used on 00086 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00087 * 00088 * Revision 1.48 2001/12/13 09:17:01 robertj 00089 * Added function to convert PString to IP address with error checking that can 00090 * distinguish between 0.0.0.0 or 255.255.255.255 and illegal address. 00091 * 00092 * Revision 1.47 2001/09/14 08:00:38 robertj 00093 * Added new versions of Conenct() to allow binding to a specific local interface. 00094 * 00095 * Revision 1.46 2001/05/22 12:49:32 robertj 00096 * Did some seriously wierd rewrite of platform headers to eliminate the 00097 * stupid GNU compiler warning about braces not matching. 00098 * 00099 * Revision 1.45 2001/03/05 04:18:27 robertj 00100 * Added net mask to interface info returned by GetInterfaceTable() 00101 * 00102 * Revision 1.44 2001/01/29 06:41:18 robertj 00103 * Added printing of entry of interface table. 00104 * 00105 * Revision 1.43 2000/06/26 11:17:19 robertj 00106 * Nucleus++ port (incomplete). 00107 * 00108 * Revision 1.42 1999/09/10 04:35:42 robertj 00109 * Added Windows version of PIPSocket::GetInterfaceTable() function. 00110 * 00111 * Revision 1.41 1999/09/10 02:31:42 craigs 00112 * Added interface table routines 00113 * 00114 * Revision 1.40 1999/08/30 02:21:03 robertj 00115 * Added ability to listen to specific interfaces for IP sockets. 00116 * 00117 * Revision 1.39 1999/08/08 09:04:01 robertj 00118 * Added operator>> for PIPSocket::Address class. 00119 * 00120 * Revision 1.38 1999/03/09 02:59:50 robertj 00121 * Changed comments to doc++ compatible documentation. 00122 * 00123 * Revision 1.37 1999/02/23 07:19:22 robertj 00124 * Added [] operator PIPSocket::Address to get the bytes out of an IP address. 00125 * 00126 * Revision 1.36 1999/02/16 08:12:00 robertj 00127 * MSVC 6.0 compatibility changes. 00128 * 00129 * Revision 1.35 1998/12/21 07:22:50 robertj 00130 * Virtualised functions for SOCKS support. 00131 * 00132 * Revision 1.34 1998/12/18 04:34:14 robertj 00133 * PPC Linux GNU C compatibility. 00134 * 00135 * Revision 1.33 1998/11/30 08:57:32 robertj 00136 * New directory structure 00137 * 00138 * Revision 1.32 1998/11/22 11:30:08 robertj 00139 * Check route table function to get a list 00140 * 00141 * Revision 1.31 1998/11/19 05:18:22 robertj 00142 * Added route table manipulation functions to PIPSocket class. 00143 * 00144 * Revision 1.30 1998/09/23 06:20:45 robertj 00145 * Added open source copyright license. 00146 * 00147 * Revision 1.29 1997/12/11 10:28:57 robertj 00148 * Added operators for IP address to DWORD conversions. 00149 * 00150 * Revision 1.28 1996/12/17 11:08:05 robertj 00151 * Added DNS name cache clear command. 00152 * 00153 * Revision 1.27 1996/11/30 12:10:00 robertj 00154 * Added Connect() variant so can set the local port number on link. 00155 * 00156 * Revision 1.26 1996/11/16 10:48:49 robertj 00157 * Fixed missing const in PIPSocket::Address stream output operator.. 00158 * 00159 * Revision 1.25 1996/11/04 03:40:54 robertj 00160 * Moved address printer from inline to source. 00161 * 00162 * Revision 1.24 1996/09/14 13:09:21 robertj 00163 * Major upgrade: 00164 * rearranged sockets to help support IPX. 00165 * added indirect channel class and moved all protocols to descend from it, 00166 * separating the protocol from the low level byte transport. 00167 * 00168 * Revision 1.23 1996/08/25 09:33:55 robertj 00169 * Added function to detect "local" host name. 00170 * 00171 * Revision 1.22 1996/03/26 00:51:13 robertj 00172 * Added GetLocalAddress() variant that returns port number as well. 00173 * 00174 * Revision 1.21 1996/03/16 04:41:30 robertj 00175 * Changed all the get host name and get host address functions to be more consistent. 00176 * 00177 * Revision 1.20 1996/03/03 07:37:56 robertj 00178 * Added Reusability clause to the Listen() function on sockets. 00179 * 00180 * Revision 1.19 1996/02/25 03:00:31 robertj 00181 * Added operator<< to PIPSocket::Address. 00182 * Moved some socket functions to platform dependent code. 00183 * 00184 * Revision 1.18 1996/02/08 12:11:19 robertj 00185 * Added GetPeerAddress that returns a port. 00186 * 00187 * Revision 1.17 1996/01/28 14:07:31 robertj 00188 * Changed service parameter to PString for ease of use in GetPortByService function 00189 * Fixed up comments. 00190 * 00191 * Revision 1.16 1995/12/23 03:44:59 robertj 00192 * Fixed unix portability issues. 00193 * 00194 * Revision 1.15 1995/12/10 11:32:11 robertj 00195 * Numerous fixes for sockets. 00196 * 00197 * Revision 1.14 1995/10/14 14:57:26 robertj 00198 * Added internet address to string conversion functionality. 00199 * 00200 * Revision 1.13 1995/07/02 01:18:19 robertj 00201 * Added static functions to get the current host name/address. 00202 * 00203 * Revision 1.12 1995/06/17 00:41:40 robertj 00204 * More logical design of port numbers and service names. 00205 * 00206 * Revision 1.11 1995/03/18 06:26:44 robertj 00207 * Changed IP address variable for GNU compatibility. 00208 * 00209 * Revision 1.10 1995/03/14 12:41:38 robertj 00210 * Updated documentation to use HTML codes. 00211 * 00212 * Revision 1.9 1995/03/12 04:38:41 robertj 00213 * Added more functionality. 00214 * 00215 * Revision 1.8 1995/01/02 12:28:24 robertj 00216 * Documentation. 00217 * Added more socket functions. 00218 * 00219 * Revision 1.7 1995/01/01 01:07:33 robertj 00220 * More implementation. 00221 * 00222 * Revision 1.6 1994/12/15 12:47:14 robertj 00223 * Documentation. 00224 * 00225 * Revision 1.5 1994/08/23 11:32:52 robertj 00226 * Oops 00227 * 00228 * Revision 1.4 1994/08/22 00:46:48 robertj 00229 * Added pragma fro GNU C++ compiler. 00230 * 00231 * Revision 1.3 1994/08/21 23:43:02 robertj 00232 * Spelt Berkeley correctly. 00233 * 00234 * Revision 1.2 1994/07/25 03:36:03 robertj 00235 * Added sockets to common, normalising to same comment standard. 00236 * 00237 */ 00238 00239 #ifndef _PIPSOCKET 00240 #define _PIPSOCKET 00241 00242 #ifdef P_USE_PRAGMA 00243 #pragma interface 00244 #endif 00245 00246 #include <ptlib/socket.h> 00247 00255 class PIPSocket : public PSocket 00256 { 00257 PCLASSINFO(PIPSocket, PSocket); 00258 protected: 00259 /* Create a new Internet Protocol socket based on the port number 00260 specified. 00261 */ 00262 PIPSocket(); 00263 00264 public: 00268 class Address : public PObject { 00269 public: 00270 00273 00274 Address(); 00275 00279 Address(const PString & dotNotation); 00280 00282 Address(PINDEX len, const BYTE * bytes); 00283 00285 Address(BYTE b1, BYTE b2, BYTE b3, BYTE b4); 00286 00288 Address(DWORD dw); 00289 00291 Address(const in_addr & addr); 00292 00293 #if P_HAS_IPV6 00294 00295 Address(const in6_addr & addr); 00296 00299 Address(const int ai_family, const int ai_addrlen,struct sockaddr *ai_addr); 00300 #endif 00301 00302 #ifdef __NUCLEUS_NET__ 00303 Address(const struct id_struct & addr); 00304 Address & operator=(const struct id_struct & addr); 00305 #endif 00306 00308 Address & operator=(const in_addr & addr); 00309 00310 #if P_HAS_IPV6 00311 00312 Address & operator=(const in6_addr & addr); 00313 #endif 00314 00316 Address & operator=(const PString & dotNotation); 00317 00319 Address & operator=(DWORD dw); 00321 00323 Comparison Compare(const PObject & obj) const; 00324 bool operator==(const Address & addr) const { return Compare(addr) == EqualTo; } 00325 bool operator!=(const Address & addr) const { return Compare(addr) != EqualTo; } 00326 #if P_HAS_IPV6 00327 bool operator==(in6_addr & addr) const; 00328 bool operator!=(in6_addr & addr) const { return !operator==(addr); } 00329 #endif 00330 bool operator==(in_addr & addr) const; 00331 bool operator!=(in_addr & addr) const { return !operator==(addr); } 00332 bool operator==(DWORD dw) const; 00333 bool operator!=(DWORD dw) const { return !operator==(dw); } 00334 #ifdef P_VXWORKS 00335 bool operator==(long unsigned int u) const { return operator==((DWORD)u); } 00336 bool operator!=(long unsigned int u) const { return !operator==((DWORD)u); } 00337 #endif 00338 #ifdef _WIN32 00339 bool operator==(unsigned u) const { return operator==((DWORD)u); } 00340 bool operator!=(unsigned u) const { return !operator==((DWORD)u); } 00341 #endif 00342 #ifdef P_RTEMS 00343 bool operator==(u_long u) const { return operator==((DWORD)u); } 00344 bool operator!=(u_long u) const { return !operator==((DWORD)u); } 00345 #endif 00346 #ifdef __BEOS__ 00347 bool operator==(in_addr_t a) const { return operator==((DWORD)a); } 00348 bool operator!=(in_addr_t a) const { return !operator==((DWORD)a); } 00349 #endif 00350 bool operator==(int i) const { return operator==((DWORD)i); } 00351 bool operator!=(int i) const { return !operator==((DWORD)i); } 00352 00354 PString AsString() const; 00355 00357 BOOL FromString( 00358 const PString & str 00359 ); 00360 00362 operator PString() const; 00363 00365 operator in_addr() const; 00366 00367 #if P_HAS_IPV6 00369 operator in6_addr() const; 00370 #endif 00371 00373 operator DWORD() const; 00374 00376 BYTE Byte1() const; 00377 00379 BYTE Byte2() const; 00380 00382 BYTE Byte3() const; 00383 00385 BYTE Byte4() const; 00386 00388 BYTE operator[](PINDEX idx) const; 00389 00391 PINDEX GetSize() const; 00392 00394 const char * GetPointer() const { return (const char *)&v; } 00395 00397 unsigned GetVersion() const { return version; } 00398 00400 BOOL IsValid() const; 00401 BOOL IsAny(); 00402 00404 BOOL IsLoopback() const; 00405 00407 BOOL IsBroadcast() const; 00408 00409 // Check if the remote address is private address as specified 00410 // by RFC 1918 address 00411 // 10.0.0.0 - 10.255.255.255.255 00412 // 172.16.0.0 - 172.31.255.255 00413 // 192.168.0.0 - 192.168.255.255 00414 BOOL IsRFC1918() const 00415 { return (Byte1() == 10) 00416 || 00417 ( 00418 (Byte1() == 172) 00419 && 00420 (Byte2() >= 16) && (Byte2() <= 31) 00421 ) 00422 || 00423 ( 00424 (Byte1() == 192) 00425 && 00426 (Byte2() == 168) 00427 ); 00428 } 00429 00430 #if P_HAS_IPV6 00431 00432 BOOL IsV4Mapped() const; 00433 #endif 00434 00435 static const Address & GetLoopback(); 00436 #if P_HAS_IPV6 00437 static const Address & GetLoopback6(); 00438 static const Address & GetAny6(); 00439 #endif 00440 static const Address & GetBroadcast(); 00441 00442 protected: 00444 union { 00445 in_addr four; 00446 #if P_HAS_IPV6 00447 in6_addr six; 00448 #endif 00449 } v; 00450 unsigned version; 00451 00453 friend ostream & operator<<(ostream & s, const Address & a); 00454 00456 friend istream & operator>>(istream & s, Address & a); 00457 }; 00458 00459 // Overrides from class PChannel 00467 virtual PString GetName() const; 00468 00469 // Set the default IP address familly. 00470 // Needed as lot of IPv6 stack are not able to receive IPv4 packets in IPv6 sockets 00471 // They are not RFC 2553, chapter 7.3, compliant. 00472 // As a concequence, when opening a socket to listen to port 1720 (for exemple) from any remot host 00473 // one must decide whether this an IPv4 or an IPv6 socket... 00474 static int GetDefaultIpAddressFamily(); 00475 static void SetDefaultIpAddressFamily(int ipAdressFamily); // PF_INET, PF_INET6 00476 static void SetDefaultIpAddressFamilyV4(); // PF_INET 00477 #if P_HAS_IPV6 00478 static void SetDefaultIpAddressFamilyV6(); // PF_INET6 00479 static BOOL IsIpAddressFamilyV6Supported(); 00480 #endif 00481 static PIPSocket::Address GetDefaultIpAny(); 00482 00483 // Open an IPv4 or IPv6 socket 00484 virtual BOOL OpenSocket( 00485 int ipAdressFamily=PF_INET 00486 ) = 0; 00487 00488 00489 // Overrides from class PSocket. 00501 virtual BOOL Connect( 00502 const PString & address 00503 ); 00504 virtual BOOL Connect( 00505 const Address & addr 00506 ); 00507 virtual BOOL Connect( 00508 WORD localPort, 00509 const Address & addr 00510 ); 00511 virtual BOOL Connect( 00512 const Address & iface, 00513 const Address & addr 00514 ); 00515 virtual BOOL Connect( 00516 const Address & iface, 00517 WORD localPort, 00518 const Address & addr 00519 ); 00520 00536 virtual BOOL Listen( 00537 unsigned queueSize = 5, 00538 WORD port = 0, 00539 Reusability reuse = AddressIsExclusive 00540 ); 00541 virtual BOOL Listen( 00542 const Address & bind, 00543 unsigned queueSize = 5, 00544 WORD port = 0, 00545 Reusability reuse = AddressIsExclusive 00546 ); 00547 00548 00549 // New functions for class 00557 static PString GetHostName(); 00558 static PString GetHostName( 00559 const PString & hostname 00560 ); 00561 static PString GetHostName( 00562 const Address & addr 00563 ); 00564 00571 static BOOL GetHostAddress( 00572 Address & addr 00573 ); 00574 static BOOL GetHostAddress( 00575 const PString & hostname, 00576 /* Name of host to get address for. This may be either a domain name or 00577 an IP number in "dot" format. 00578 */ 00579 Address & addr 00580 ); 00581 00589 static PStringArray GetHostAliases( 00590 const PString & hostname 00591 /* Name of host to get address for. This may be either a domain name or 00592 an IP number in "dot" format. 00593 */ 00594 ); 00595 static PStringArray GetHostAliases( 00596 const Address & addr 00597 /* Name of host to get address for. This may be either a domain name or 00598 an IP number in "dot" format. 00599 */ 00600 ); 00601 00609 static BOOL IsLocalHost( 00610 const PString & hostname 00611 /* Name of host to get address for. This may be either a domain name or 00612 an IP number in "dot" format. 00613 */ 00614 ); 00615 00621 virtual BOOL GetLocalAddress( 00622 Address & addr 00623 ); 00624 virtual BOOL GetLocalAddress( 00625 Address & addr, 00626 WORD & port 00627 ); 00628 00635 virtual BOOL GetPeerAddress( 00636 Address & addr 00637 ); 00638 virtual BOOL GetPeerAddress( 00639 Address & addr, 00640 WORD & port 00641 ); 00642 00648 PString GetLocalHostName(); 00649 00655 PString GetPeerHostName(); 00656 00659 static void ClearNameCache(); 00660 00672 static BOOL GetGatewayAddress( 00673 Address & addr 00674 ); 00675 00688 static PString GetGatewayInterface(); 00689 00693 class RouteEntry : public PObject 00694 { 00695 PCLASSINFO(RouteEntry, PObject); 00696 public: 00698 RouteEntry(const Address & addr) : network(addr) { } 00699 00701 Address GetNetwork() const { return network; } 00702 00704 Address GetNetMask() const { return net_mask; } 00705 00707 Address GetDestination() const { return destination; } 00708 00710 const PString & GetInterface() const { return interfaceName; } 00711 00713 long GetMetric() const { return metric; } 00714 00715 protected: 00716 Address network; 00717 Address net_mask; 00718 Address destination; 00719 PString interfaceName; 00720 long metric; 00721 00722 friend class PIPSocket; 00723 }; 00724 00725 PLIST(RouteTable, RouteEntry); 00726 00732 static BOOL GetRouteTable( 00733 RouteTable & table 00734 ); 00735 00736 00740 class InterfaceEntry : public PObject 00741 { 00742 PCLASSINFO(InterfaceEntry, PObject) 00743 00744 public: 00746 InterfaceEntry( 00747 const PString & _name, 00748 const Address & _addr, 00749 const Address & _mask, 00750 const PString & _macAddr 00751 ); 00752 00754 virtual void PrintOn( 00755 ostream &strm // Stream to print the object into. 00756 ) const; 00757 00759 const PString & GetName() const { return name; } 00760 00762 Address GetAddress() const { return ipAddr; } 00763 00765 Address GetNetMask() const { return netMask; } 00766 00768 const PString & GetMACAddress() const { return macAddr; } 00769 00770 protected: 00771 PString name; 00772 Address ipAddr; 00773 Address netMask; 00774 PString macAddr; 00775 }; 00776 00777 PLIST(InterfaceTable, InterfaceEntry); 00778 00784 static BOOL GetInterfaceTable( 00785 InterfaceTable & table 00786 ); 00787 00788 00789 // Include platform dependent part of class 00790 #ifdef _WIN32 00791 #include "msos/ptlib/ipsock.h" 00792 #else 00793 #include "unix/ptlib/ipsock.h" 00794 #endif 00795 }; 00796 00797 #endif 00798 00799 00800 // End Of File ///////////////////////////////////////////////////////////////

Generated on Sat Jul 24 15:35:56 2004 for PWLib by doxygen 1.3.7