📄 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.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. * * Revision 1.66 1998/01/26 00:49:28 robertj * Fixed bug in detecting local host on NT, 95 bug kludge was interfering with it. * * Revision 1.65 1998/01/06 12:43:23 craigs * Added definition of REENTRANT_BUFFER_LEN * * Revision 1.64 1998/01/04 07:25:09 robertj * Added pthreads compatible calls for gethostbyx functions. * * Revision 1.63 1997/12/18 05:06:13 robertj * Moved IsLocalHost() to platform dependent code. * * Revision 1.62 1997/12/11 10:30:35 robertj * Added operators for IP address to DWORD conversions. * * Revision 1.61 1997/10/03 13:33:22 robertj * Added workaround for NT winsock bug with RAS and DNS lookups. * * Revision 1.60 1997/09/27 00:58:39 robertj * Fixed race condition on socket close in Select() function. * * Revision 1.59 1997/06/06 10:56:36 craigs * Added new functions for connectionless UDP writes * * Revision 1.58 1997/01/04 07:42:18 robertj * Fixed GCC Warnings. * * Revision 1.57 1997/01/04 06:54:38 robertj * Added missing canonical name to alias list. * * Revision 1.56 1996/12/17 11:07:05 robertj * Added clear of name cache. * * Revision 1.55 1996/12/12 09:23:27 robertj * Fixed name cache to cache missing names as well. * Fixed new connect with specific local port so can be re-used (simultaneous FTP session bug) * * Revision 1.54 1996/12/05 11:46:39 craigs * Fixed problem with Win95 recvfrom not having timeouts * * Revision 1.53 1996/11/30 12:08:17 robertj * Added Connect() variant so can set the local port number on link. * * Revision 1.52 1996/11/16 10:49:03 robertj * Fixed missing const in PIPSocket::Address stream output operator.. * * Revision 1.51 1996/11/16 01:43:49 craigs * Fixed problem with ambiguous DNS cache keys * * Revision 1.50 1996/11/10 21:08:31 robertj * Added host name caching. * * Revision 1.49 1996/11/04 03:40:22 robertj * Moved address printer from inline to source. * * Revision 1.48 1996/10/26 01:41:09 robertj * Compensated for Win'95 gethostbyaddr bug. * * Revision 1.47 1996/09/14 13:09:40 robertj * Major upgrade: * rearranged sockets to help support IPX. * added indirect channel class and moved all protocols to descend from it, * separating the protocol from the low level byte transport. * * Revision 1.46 1996/08/25 09:33:32 robertj * Added function to detect "local" host name. * * Revision 1.45 1996/07/30 12:24:53 robertj * Fixed incorrect conditional stopping Select() from working. * * Revision 1.44 1996/07/27 04:10:35 robertj * Changed Select() calls to return error codes. * * Revision 1.43 1996/06/10 09:58:21 robertj * Fixed win95 compatibility with looking up zero address (got a response and shouldn't). * * Revision 1.42 1996/05/26 03:47:03 robertj * Compatibility to GNU 2.7.x * * Revision 1.39 1996/04/29 12:20:01 robertj * Fixed GetHostAliases() so doesn't overwrite names with IP numbers. * * Revision 1.38 1996/04/15 10:59:41 robertj * Opened socket on UDP sockets so ReadFrom/WriteTo work when no Connect/Listen. * * Revision 1.37 1996/03/31 09:06:41 robertj * Added socket shutdown function. * * Revision 1.35 1996/03/18 13:33:18 robertj * Fixed incompatibilities to GNU compiler where PINDEX != int. * * Revision 1.34 1996/03/17 05:51:18 robertj * Fixed strange bug in accept cant have NULL address. * * Revision 1.33 1996/03/16 04:52:20 robertj * Changed all the get host name and get host address functions to be more consistent. * * Revision 1.32 1996/03/04 12:21:00 robertj * Split file into telnet.cxx * * Revision 1.31 1996/03/03 07:38:45 robertj * Added Reusability clause to the Listen() function on sockets. * * Revision 1.30 1996/03/02 03:25:13 robertj * Added Capability to get and set Berkeley socket options. * * Revision 1.29 1996/02/25 11:30:08 robertj * Changed Listen so can do a listen on a socket that is connected. * * Revision 1.28 1996/02/25 03:10:55 robertj * Moved some socket functions to platform dependent code. * * Revision 1.27 1996/02/19 13:30:15 robertj * Fixed bug in getting port by service name when specifying service by string number. * Added SO_LINGER option to socket to stop data loss on close. * * Revision 1.26 1996/02/15 14:46:44 robertj * Added Select() function to PSocket. * * Revision 1.25 1996/02/13 13:08:09 robertj * Fixed usage of sock_addr structure, not being cleared correctly. * * Revision 1.24 1996/02/08 12:27:22 robertj * Added function to get peer port as well as IP number.. * * Revision 1.23 1996/02/03 11:07:37 robertj * Fixed buf in assuring error when converting string to IP number and string is empty. * * Revision 1.22 1996/01/28 14:08:13 robertj * Changed service parameter to PString for ease of use in GetPortByService function * Fixed up comments. * Added default value in string for service name. * * Revision 1.21 1996/01/23 13:19:13 robertj * Moved Accept() function to platform dependent code. * * Revision 1.20 1995/12/23 03:42:53 robertj * Unix portability issues. * * Revision 1.19 1995/12/10 11:42:23 robertj * Numerous fixes for sockets. * * Revision 1.18 1995/10/14 15:11:31 robertj * Added internet address to string conversion functionality. * * Revision 1.17 1995/07/02 01:21:23 robertj * Added static functions to get the current host name/address. * * Revision 1.16 1995/06/17 00:47:01 robertj * Changed overloaded Open() calls to 3 separate function names. * More logical design of port numbers and service names. * * Revision 1.15 1995/06/04 12:45:33 robertj * Added application layer protocol sockets. * Slight redesign of port numbers on sockets. * * Revision 1.14 1995/04/25 11:12:44 robertj * Fixed functions hiding ancestor virtuals. * * Revision 1.13 1995/04/01 08:31:54 robertj * Finally got a working TELNET. * * Revision 1.12 1995/03/18 06:27:49 robertj * Rewrite of telnet socket protocol according to RFC1143. * * Revision 1.11 1995/03/12 04:46:29 robertj * Added more functionality. * * Revision 1.10 1995/02/21 11:25:29 robertj * Further implementation of telnet socket, feature complete now. * * Revision 1.9 1995/01/27 11:16:16 robertj * Fixed missing cast in function, required by some platforms. * * Revision 1.8 1995/01/15 04:55:47 robertj * Moved all Berkley socket functions inside #ifdef. * * Revision 1.7 1995/01/04 10:57:08 robertj * Changed for HPUX and GNU2.6.x * * Revision 1.6 1995/01/03 09:37:52 robertj * Added constructor to open TCP socket. * * Revision 1.5 1995/01/02 12:28:25 robertj * Documentation. * Added more socket functions. * * Revision 1.4 1995/01/01 01:06:58 robertj * More implementation. * * Revision 1.3 1994/11/28 12:38:49 robertj * Added DONT and WONT states. * * Revision 1.2 1994/08/21 23:43:02 robertj * Some implementation. * * Revision 1.1 1994/08/01 03:39:05 robertj * Initial revision * */#ifdef __NUCLEUS_PLUS__#include <ConfigurationClass.h>#endif#include <ptlib.h>#include <ptlib/sockets.h>#include <ctype.h>#if (defined(_WIN32) || defined(WINDOWS)) && !defined(__NUCLEUS_MNT__)static PWinSock dummyForWinSock; // Assure winsock is initialised#endif#if (defined(P_PTHREADS) && !defined(P_THREAD_SAFE_CLIB)) || defined(__NUCLEUS_PLUS__)#define REENTRANT_BUFFER_LEN 1024#endifclass PIPCacheData : public PObject{ PCLASSINFO(PIPCacheData, PObject) public: PIPCacheData(struct hostent * ent, const char * original); const PString & GetHostName() const { return hostname; } const PIPSocket::Address & GetHostAddress() const { return address; } const PStringList & GetHostAliases() const { return aliases; } BOOL HasAged() const; private: PString hostname; PIPSocket::Address address; PStringList aliases; PTime birthDate;};PDICTIONARY(PHostByName_private, PCaselessString, PIPCacheData);class PHostByName : PHostByName_private{ public: BOOL GetHostName(const PString & name, PString & hostname); BOOL GetHostAddress(const PString & name, PIPSocket::Address & address); BOOL GetHostAliases(const PString & name, PStringArray & aliases); private: PIPCacheData * GetHost(const PString & name); PMutex mutex; friend void PIPSocket::ClearNameCache();};static PHostByName & pHostByName(){ static PHostByName t; return t;}class PIPCacheKey : public PObject{ PCLASSINFO(PIPCacheKey, PObject) public: PIPCacheKey(const PIPSocket::Address & a) { addr = a; } PObject * Clone() const { return new PIPCacheKey(*this); } PINDEX HashFunction() const { return (addr.Byte2() + addr.Byte3() + addr.Byte4())%41; } private: PIPSocket::Address addr;};PDICTIONARY(PHostByAddr_private, PIPCacheKey, PIPCacheData);class PHostByAddr : PHostByAddr_private{ public: BOOL GetHostName(const PIPSocket::Address & addr, PString & hostname); BOOL GetHostAddress(const PIPSocket::Address & addr, PIPSocket::Address & address); BOOL GetHostAliases(const PIPSocket::Address & addr, PStringArray & aliases); private: PIPCacheData * GetHost(const PIPSocket::Address & addr); PMutex mutex; friend void PIPSocket::ClearNameCache();};static PHostByAddr & pHostByAddr(){ static PHostByAddr t; return t;}#define new PNEW//////////////////////////////////////////////////////////////////////////////// IP CachingPIPCacheData::PIPCacheData(struct hostent * host_info, const char * original){ if (host_info == NULL) { address.s_addr = 0; return; } hostname = host_info->h_name; address = *(DWORD *)host_info->h_addr; aliases.AppendString(host_info->h_name); PINDEX i; for (i = 0; host_info->h_aliases[i] != NULL; i++) aliases.AppendString(host_info->h_aliases[i]); for (i = 0; host_info->h_addr_list[i] != NULL; i++) aliases.AppendString(inet_ntoa(*(struct in_addr *)host_info->h_addr_list[i])); for (i = 0; i < aliases.GetSize(); i++) if (aliases[i] *= original) return; aliases.AppendString(original);}static PTimeInterval GetConfigTime(const char * key, DWORD dflt){ PConfig cfg("DNS Cache"); return cfg.GetInteger(key, dflt);}BOOL PIPCacheData::HasAged() const{ static PTimeInterval retirement = GetConfigTime("Age Limit", 300000); // 5 minutes PTime now; PTimeInterval age = now - birthDate; return age > retirement;}BOOL PHostByName::GetHostName(const PString & name, PString & hostname){ PIPCacheData * host = GetHost(name); if (host != NULL) { hostname = host->GetHostName(); hostname.MakeUnique(); } mutex.Signal(); return host != NULL;}BOOL PHostByName::GetHostAddress(const PString & name, PIPSocket::Address & address){ PIPCacheData * host = GetHost(name); if (host != NULL) address = host->GetHostAddress(); mutex.Signal(); return host != NULL;}BOOL PHostByName::GetHostAliases(const PString & name, PStringArray & aliases){ PIPCacheData * host = GetHost(name); if (host != NULL) { const PStringList & a = host->GetHostAliases(); aliases.SetSize(a.GetSize()); for (PINDEX i = 0; i < a.GetSize(); i++) aliases[i] = a[i]; } mutex.Signal(); return host != NULL;}PIPCacheData * PHostByName::GetHost(const PString & name){ mutex.Wait(); PCaselessString key = name; PIPCacheData * host = GetAt(key); if (host != NULL && host->HasAged()) { SetAt(key, NULL); host = NULL; } if (host == NULL) { mutex.Signal();#ifdef P_AIX struct hostent_data ht_data; memset(&ht_data, 0, sizeof(ht_data)); struct hostent host_info;#else struct hostent * host_info;#endif int retry = 3; do {#if ( ( defined(P_PTHREADS) && !defined(P_THREAD_SAFE_CLIB) ) || (defined(__NUCLEUS_PLUS__) ) ) // this function should really be a static on PIPSocket, but this would // require allocating thread-local storage for the data and that's too much // of a pain!#ifndef P_AIX // that I get no warnings int localErrNo; char buffer[REENTRANT_BUFFER_LEN]; struct hostent hostEnt;#endif#ifdef P_LINUX ::gethostbyname_r(name, &hostEnt, buffer, REENTRANT_BUFFER_LEN, &host_info, &localErrNo); #elif defined P_AIX ::gethostbyname_r(name, &host_info, &ht_data); #else host_info = ::gethostbyname_r(name, &hostEnt, buffer, REENTRANT_BUFFER_LEN, &localErrNo);#endif#else host_info = ::gethostbyname(name);#endif } while (h_errno == TRY_AGAIN && --retry > 0); mutex.Wait(); if (retry == 0) return NULL;#ifdef P_AIX host = new PIPCacheData (&host_info, (const char*) name);#else host = new PIPCacheData(host_info, name);#endif SetAt(key, host);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -