📄 sockets.cxx
字号:
/*
* sockets.cxx
*
* Berkley sockets classes.
*
* Portable Windows Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Log: sockets.cxx,v $
* Revision 1.2 2007/07/14 07:50:35 joegenbaclor
* Migrated code to use new STL based string class
*
* Revision 1.1 2006/06/29 04:18:19 joegenbaclor
* *** empty log message ***
*
* Revision 1.203 2006/04/09 11:03:59 csoutheren
* Remove warnings on VS.net 2005
*
* Revision 1.202 2006/04/09 07:05:40 rjongbloed
* Moved output stream operator for PString from sockets code to string code and fixed
* its implemetation to continue to use PrintOn. Why it was added is unknown, probably
* a compiler issue, but it should not be in a random source file!
*
* Revision 1.201 2006/02/13 06:56:26 csoutheren
* Fixed problem on Windows
*
* Revision 1.200 2006/02/10 23:56:58 csoutheren
* Fixed compile problems on Debian and Windows
*
* Revision 1.199 2006/02/10 22:50:11 csoutheren
* Fixed error in last commit
*
* Revision 1.198 2006/02/10 22:47:01 csoutheren
* Ensure IPV6 addresses are not returned by IPSocket::GetHostAddress when
* IPV4 has been forced using PIPSocket::SetDefaultIpAddressFamilyV4
* This is intended to address the following Ekiga bug:
* Bug 330388 . Cannot make calls to host with IPv6 address
*
* Revision 1.197 2005/11/21 11:49:36 shorne
* Changed disableQos to disableGQoS to better reflect what it does
*
* Revision 1.196 2005/10/21 06:01:30 csoutheren
* Fixed warning on VS.NET 2005
*
* Revision 1.195 2005/09/25 10:51:23 dominance
* almost complete the mingw support. We'll be there soon. ;)
*
* Revision 1.194 2005/08/18 06:24:30 shorne
* Reversed Last Patch
*
* Revision 1.193 2005/08/18 03:42:53 shorne
* Chaeck for RSVP QoS Thx Zdenek
*
* Revision 1.192 2005/08/09 12:46:00 rjongbloed
* Fixed some platforms where in6addr_any does not exist.
*
* Revision 1.191 2005/07/17 09:25:40 csoutheren
* Fixed problem in IPV6 variant of PIPSocket::Address::IsLoopback
* Thanks to Roger Hardiman
*
* Revision 1.190 2005/07/13 12:08:09 csoutheren
* Fixed QoS patches to be more consistent with PWLib style and to allow Unix compatibility
*
* Revision 1.189 2005/07/13 11:48:54 csoutheren
* Backported QOS changes from isvo branch
*
* Revision 1.188 2005/06/21 22:28:32 rjongbloed
* Assured IP is set to zero, so if parse of dotted decimal fails is not random IP address.
*
* Revision 1.187.2.1 2005/04/25 13:42:28 shorne
* Extended QoS support for per-call negotiation
*
* Revision 1.187 2005/03/22 07:29:30 csoutheren
* Fixed problem where PStrings sometimes get case into
* PIPSocket::Address when outputting to an ostream
*
* Revision 1.186 2005/02/13 23:01:36 csoutheren
* Fixed problem with not detecting mapped IPV6 addresses within the RFC1918
* address range as RFC1918
*
* Revision 1.185 2005/02/07 12:12:30 csoutheren
* Expanded interface list routines to include IPV6 addresses
* Added IPV6 to GetLocalAddress
*
* Revision 1.184 2005/02/07 00:47:18 csoutheren
* Changed IPV6 code to use standard IPV6 macros
*
* Revision 1.183 2005/01/26 05:38:01 csoutheren
* Added ability to remove config file support
*
* Revision 1.182 2005/01/16 21:27:07 csoutheren
* Changed PIPSocket::IsAny to be const
*
* Revision 1.181 2005/01/16 20:35:41 csoutheren
* Fixed problem with IPv6 INADDR_ANY
*
* Revision 1.180 2005/01/15 19:23:39 csoutheren
* Fixed problem in operator *= for IP V6
*
* Revision 1.179 2004/12/14 14:24:20 csoutheren
* Added PIPSocket::Address::operator*= to compare IPV4 addresses
* to IPV4-compatible IPV6 addresses. More documentation needed
* once this is tested as working
*
* Revision 1.178 2004/12/14 06:20:29 csoutheren
* Added function to get address of network interface
*
* Revision 1.177 2004/11/16 00:31:44 csoutheren
* Added Cygwin support (needs to have gethostbyname_r fixed)
*
* Revision 1.176 2004/10/26 18:27:28 ykiryanov
* Added another for of SetOption to set SO_REUSEADDR for BeOS
*
* Revision 1.175 2004/08/24 07:08:11 csoutheren
* Added use of recvmsg to determine which interface UDP packets arrive on
*
* Revision 1.174 2004/07/11 07:56:36 csoutheren
* Applied jumbo VxWorks patch, thanks to Eize Slange
*
* Revision 1.173 2004/04/27 04:37:51 rjongbloed
* Fixed ability to break of a PSocket::Select call under linux when a socket
* is closed by another thread.
*
* Revision 1.172 2004/04/22 12:27:24 rjongbloed
* Fixed selection of QoS to use more flexible #if rather than #ifdef
*
* Revision 1.171 2004/04/18 04:33:38 rjongbloed
* Changed all operators that return BOOL to return standard type bool. This is primarily
* for improved compatibility with std STL usage removing many warnings.
*
* Revision 1.170 2004/04/18 00:50:14 ykiryanov
* Removed BE_BONELESS ifdefs. Be is boned now. More funcitonality
*
* Revision 1.169 2004/04/03 08:22:22 csoutheren
* Remove pseudo-RTTI and replaced with real RTTI
*
* Revision 1.168 2004/02/04 02:32:31 csoutheren
* Changed #ifdef to #if to ensure flags are tested correctly
*
* Revision 1.167 2004/01/23 07:07:35 csoutheren
* Fixed compile warning under Linux
*
* Revision 1.166 2003/11/12 05:16:48 csoutheren
* Fixed compiling problem on systems without QoS or IPV6
*
* Revision 1.165 2003/10/30 11:32:57 rjongbloed
* Added bullet proofing for converting from inaddr
*
* Revision 1.164 2003/10/27 09:48:47 csoutheren
* Changed use of P_HAS_QOS to ensure that setsockopt is still used
* for diffserv if available. Thanks to Henry Harrison
*
* Revision 1.163 2003/10/27 03:46:15 csoutheren
* Added ifdef to disable QoS code on systems that do not support it
*
* Revision 1.162 2003/10/27 03:22:44 csoutheren
* Added handling for QoS
* Thanks to Henry Harrison of AliceStreet
*
* Revision 1.161 2003/05/27 08:53:11 dsandras
*
* Added test error case when the host lookup fails for IPv6.
*
* Revision 1.160 2003/05/21 09:34:44 rjongbloed
* Name lookup support for IPv6, thanks again S閎astien Josset
*
* Revision 1.159 2003/04/28 02:55:50 robertj
* Added function to see at run time if IPv6 available, thanks Sebastien Josset
*
* Revision 1.158 2003/04/15 07:40:08 robertj
* Removed redundent variable.
* Fixed IPv6 support for multiple IP address DNS lookups.
*
* Revision 1.157 2003/04/08 01:12:35 robertj
* Latest patch for IPv6 operation, thanks Sebastien Josset
*
* Revision 1.156 2003/04/07 23:31:33 robertj
* Fixed incorrect host to network byte order function, should be long!
*
* Revision 1.155 2003/04/07 23:22:08 robertj
* Fixed GNU compatibility issue.
*
* Revision 1.154 2003/04/07 11:57:56 robertj
* Allowed for full integer numeric form of IP address read from a stream.
*
* Revision 1.153 2003/04/03 08:43:23 robertj
* Added IPv4 mapping into IPv6, thanks Sebastien Josset
*
* Revision 1.152 2003/03/26 05:36:38 robertj
* More IPv6 support (INADDR_ANY handling), thanks S閎astien Josset
*
* Revision 1.151 2003/02/11 06:49:12 craigs
* Added missing OpenSocket function
*
* Revision 1.150 2003/02/03 11:23:32 robertj
* Added function to get pointer to IP address data.
*
* Revision 1.149 2003/02/03 10:27:55 robertj
* Cleaned up the gethostbyX functions for various platforms
*
* Revision 1.148 2003/02/03 08:43:01 robertj
* Fixed correct scoping of local variables in gethostbyX functions.
*
* Revision 1.147 2003/01/14 04:36:08 robertj
* Fixed v6 conversion of numeric string to binary so does not internally
* doa DNS lookup, confuses other parts of the system.
*
* Revision 1.146 2003/01/11 05:10:51 robertj
* Fixed Win CE compatibility issues, thanks Joerg Schoemer
*
* Revision 1.145 2002/12/16 08:04:46 robertj
* Fixed correct error check for gethostbyname_r, thanks Vladimir Toncar
*
* Revision 1.144 2002/12/13 04:01:46 robertj
* Initialised error code in gethostbyname usage.
*
* Revision 1.143 2002/12/04 00:52:59 robertj
* Fixed GNU warning
*
* Revision 1.142 2002/12/02 12:25:08 craigs
* Fixed problem with error code from gethostbyname_r not being checked correctly
*
* Revision 1.141 2002/11/24 23:47:01 robertj
* Fixed MSVC v5 compatibility
*
* Revision 1.140 2002/11/13 02:15:25 craigs
* Fixed problem with GetLocalHostName
*
* Revision 1.139 2002/11/12 11:47:53 rogerh
* Add a missing memset in the Psockaddr constructor
*
* Revision 1.138 2002/11/02 00:32:21 robertj
* Further fixes to VxWorks (Tornado) port, thanks Andreas Sikkema.
*
* Revision 1.137 2002/11/01 23:56:11 robertj
* Fixed GNu compatibility isse with IPv6
*
* Revision 1.136 2002/11/01 03:32:08 robertj
* More IPv6 fixes, thanks S閎astien Josset.
*
* Revision 1.135 2002/10/31 07:55:33 robertj
* Put sizeof ipv6 structure back as magic number 28 is explained by
* mismatched header file and running implementation.
*
* Revision 1.134 2002/10/29 08:04:44 robertj
* Changed in_addr6 to more universally used in6_addr.
* Changed size of ipv6 address to be 28 under win32, Why? I don't know!
*
* Revision 1.133 2002/10/19 06:12:20 robertj
* Moved P_fd_set::Zero() from platform independent to platform dependent
* code as Win32 implementation is completely different from Unix.
*
* Revision 1.132 2002/10/18 08:07:41 robertj
* Fixed use of FD_ZERO as (strangely) crashes on some paltforms and would
* not have cleared enough of an enlarges fd_set anyway.
*
* Revision 1.131 2002/10/17 13:44:27 robertj
* Port to RTEMS, thanks Vladimir Nesic.
*
* Revision 1.130 2002/10/17 08:17:28 robertj
* Fixed incomplete changes for expandable fd_set
*
* Revision 1.129 2002/10/17 07:17:43 robertj
* Added ability to increase maximum file handles on a process.
*
* Revision 1.128 2002/10/17 01:24:11 robertj
* Fixed so internal sockaddr classes GetSize() returns correct size for
* particular sockaddr it represents, thanks S閎astien Josset.
*
* Revision 1.127 2002/10/16 06:19:36 robertj
* Rewrite of IPv6 sockaddr code to use intelligent class to automatically
* know if it is sockaddr_in or aockaddr_in6.
* Fixed Connect() function to work correctly on unopened socket.
*
* Revision 1.126 2002/10/10 11:38:56 robertj
* Added close of socket if not open in correct ip version, thanks S閎astien Josset
*
* Revision 1.125 2002/10/10 04:43:44 robertj
* VxWorks port, thanks Martijn Roest
*
* Revision 1.124 2002/10/09 05:37:52 robertj
* Fixed IPv6 version of ReadFrom() and WriteTo().
*
* Revision 1.123 2002/10/08 23:31:44 robertj
* Added missing GetSize() implementation in ip address.
*
* Revision 1.122 2002/10/08 12:41:52 robertj
* Changed for IPv6 support, thanks S閎astien Josset.
*
* Revision 1.121 2002/09/23 07:17:24 robertj
* Changes to allow winsock2 to be included.
*
* Revision 1.120 2002/05/22 07:18:46 robertj
* Fixed bug where SO_RESUSEADDR wsa being turned ON instead of OFF when
* making an outgoing connection, should only be ON for listener sockets.
*
* Revision 1.119 2002/04/12 01:42:41 robertj
* Changed return value on os_connect() and os_accept() to make sure
* get the correct error codes propagated up under unix.
*
* Revision 1.118 2002/01/28 01:27:03 robertj
* Removed previous change that actually has nothing to do with GCC 3 compatibility,
* setting default timeout for all sockets to 10 seconds is NOT a sensible thing to do!
*
* Revision 1.117 2002/01/26 23:57:45 craigs
* Changed for GCC 3.0 compatibility, thanks to manty@manty.net
*
* Revision 1.116 2002/01/07 05:37:32 robertj
* Changed to allow for a service name that starts with a number.
*
* Revision 1.115 2002/01/02 04:55:31 craigs
* Fixed problem when PSocket::GetPortByService called with a number
* that is a substring of a valid service name
*
* Revision 1.114 2001/12/13 09:18:07 robertj
* Added function to convert PString to IP address with error checking that can
* distinguish between 0.0.0.0 or 255.255.255.255 and illegal address.
* Added ability to decode bracketed IP addresss [10.1.2.3] as host name.
*
* Revision 1.113 2001/09/14 08:00:38 robertj
* Added new versions of Conenct() to allow binding to a specific local interface.
*
* Revision 1.112 2001/09/10 02:51:23 robertj
* Major change to fix problem with error codes being corrupted in a
* PChannel when have simultaneous reads and writes in threads.
*
* Revision 1.111 2001/06/30 06:59:07 yurik
* Jac Goudsmit from Be submit these changes 6/28. Implemented by Yuri Kiryanov
*
* Revision 1.110 2001/05/24 02:07:31 yurik
* ::setsockopt on WinCE is now not called if option is not supported
*
* Revision 1.109 2001/05/23 19:48:55 yurik
* Fix submitted by Dave Cassel, dcassel@cyberfone.com,
* allowing a connection between a client and a gatekeeper.
*
* Revision 1.108 2001/03/20 06:44:25 robertj
* Lots of changes to fix the problems with terminating threads that are I/O
* blocked, especially when doing orderly shutdown of service via SIGTERM.
*
* Revision 1.107 2001/03/05 04:18:27 robertj
* Added net mask to interface info returned by GetInterfaceTable()
*
* Revision 1.106 2001/01/29 06:41:32 robertj
* Added printing of entry of interface table.
*
* Revision 1.105 2001/01/28 01:15:01 yurik
* WinCE port-related
*
* Revision 1.104 2001/01/24 06:32:17 yurik
* Windows CE port-related changes
*
* Revision 1.103 2000/06/26 11:17:21 robertj
* Nucleus++ port (incomplete).
*
* Revision 1.102 2000/06/21 01:01:22 robertj
* AIX port, thanks Wolfgang Platzer (wolfgang.platzer@infonova.at).
*
* Revision 1.101 2000/05/02 08:14:40 craigs
* Fixed problem with "memory leak" reporting under Unix
*
* Revision 1.100 2000/04/27 02:43:45 robertj
* Fixed warning about signedness mismatch.
*
* Revision 1.99 2000/04/19 00:13:52 robertj
* BeOS port changes.
*
* Revision 1.98 2000/02/18 09:55:21 robertj
* Added parameter so get/setsockopt can have other levels to SOL_SOCKET.
*
* Revision 1.97 1999/10/27 01:21:44 robertj
* Improved portability of copy from host_info struct to IP address.
*
* Revision 1.96 1999/08/30 02:21:03 robertj
* Added ability to listen to specific interfaces for IP sockets.
*
* Revision 1.95 1999/08/27 08:18:52 robertj
* Added ability to get the host/port of the the last packet read/written to UDP socket.
*
* Revision 1.94 1999/08/08 09:04:01 robertj
* Added operator>> for PIPSocket::Address class.
*
* Revision 1.93 1999/07/11 13:42:13 craigs
* pthreads support for Linux
*
* Revision 1.92 1999/06/01 08:04:35 robertj
* Fixed mistake from previous fix.
*
* Revision 1.91 1999/06/01 07:39:23 robertj
* Added retries to DNS lookup if get temporary error.
*
* Revision 1.90 1999/03/09 08:13:52 robertj
* Fixed race condition in doing Select() on closed sockets. Could go into infinite wait.
*
* Revision 1.89 1999/03/02 05:41:58 robertj
* More BeOS changes
*
* Revision 1.88 1999/02/26 04:10:39 robertj
* More BeOS port changes
*
* Revision 1.87 1999/02/25 03:43:35 robertj
* Fixed warning when PINDEX is unsigned.
*
* Revision 1.86 1999/02/23 07:19:22 robertj
* Added [] operator PIPSocket::Address to get the bytes out of an IP address.
*
* Revision 1.85 1999/02/16 08:08:06 robertj
* MSVC 6.0 compatibility changes.
*
* Revision 1.84 1999/01/08 01:29:47 robertj
* Support for pthreads under FreeBSD
*
* Revision 1.83 1999/01/06 10:58:01 robertj
* Fixed subtle mutex bug in returning string hostname from DNS cache.
*
* Revision 1.82 1998/12/22 10:25:01 robertj
* Added clone() function to support SOCKS in FTP style protocols.
* Fixed internal use of new operator in IP cache.
*
* Revision 1.81 1998/12/18 04:34:37 robertj
* PPC Linux GNU C compatibility.
*
* Revision 1.80 1998/11/30 04:47:52 robertj
* New directory structure
*
* Revision 1.79 1998/11/14 06:28:36 robertj
* Changed senatics of os_sendto to return TRUE if ANY bytes are sent.
*
* Revision 1.78 1998/11/08 12:05:04 robertj
* Fixed multiple thread access problem with DNS aliases array.
*
* Revision 1.77 1998/10/01 09:05:35 robertj
* Added check that port number is between 1 and 65535.
*
* Revision 1.76 1998/09/23 06:22:44 robertj
* Added open source copyright license.
*
* Revision 1.75 1998/08/31 13:00:34 robertj
* Prevented dependency on snmpapi.dll for all ptlib apps.
*
* Revision 1.74 1998/08/27 00:58:42 robertj
* Resolved signedness problems with various GNU libraries.
*
* Revision 1.73 1998/08/25 14:07:43 robertj
* Added getprotobyxxx wrapper functions.
*
* Revision 1.72 1998/08/25 11:09:20 robertj
* Fixed parsing of 802.x header on ethernet frames.
* Changed DNS cache to not cache temporary lookup failures, only an authoratative 'no such host'.
*
* Revision 1.71 1998/08/21 05:26:10 robertj
* Fixed bug where write streams out to non-stream socket.
* Added ethernet socket.
*
* Revision 1.70 1998/05/07 05:20:25 robertj
* Fixed DNS lookup so only works around bug in old Win95 and not OSR2
*
* Revision 1.69 1998/03/20 03:18:21 robertj
* Added special classes for specific sepahores, PMutex and PSyncPoint.
*
* Revision 1.68 1998/03/05 12:45:48 robertj
* DNS cache and NT bug fix attempts.
*
* Revision 1.67 1998/01/26 02:49:22 robertj
* GNU support.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -