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

socket.h

Go to the documentation of this file.
00001 /*
00002  * socket.h
00003  *
00004  * Berkley Socket channel ancestor 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: socket.h,v $
00030  * Revision 1.48  2004/08/24 07:08:13  csoutheren
00031  * Added use of recvmsg to determine which interface UDP packets arrive on
00032  *
00033  * Revision 1.47  2004/05/06 11:28:30  rjongbloed
00034  * Changed P_fd_set to use malloc/free isntead of new/delete due to pedantry about [].
00035  *
00036  * Revision 1.46  2004/04/27 04:37:50  rjongbloed
00037  * Fixed ability to break of a PSocket::Select call under linux when a socket
00038  *   is closed by another thread.
00039  *
00040  * Revision 1.45  2004/01/28 08:53:02  csoutheren
00041  * Fixed missing delete[] operator. Thanks to Borko Jandras
00042  *
00043  * Revision 1.44  2003/09/17 05:41:59  csoutheren
00044  * Removed recursive includes
00045  *
00046  * Revision 1.43  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.42  2002/10/18 08:07:41  robertj
00051  * Fixed use of FD_ZERO as (strangely) crashes on some paltforms and would
00052  *   not have cleared enough of an enlarges fd_set anyway.
00053  *
00054  * Revision 1.41  2002/10/17 07:17:42  robertj
00055  * Added ability to increase maximum file handles on a process.
00056  *
00057  * Revision 1.40  2002/09/16 01:08:59  robertj
00058  * Added #define so can select if #pragma interface/implementation is used on
00059  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00060  *
00061  * Revision 1.39  2002/04/12 01:42:41  robertj
00062  * Changed return value on os_connect() and os_accept() to make sure
00063  *   get the correct error codes propagated up under unix.
00064  *
00065  * Revision 1.38  2002/02/14 03:34:18  craigs
00066  * Added comment on using SetReadTimeout to set maximum wait for Connect
00067  *
00068  * Revision 1.37  2001/09/06 02:30:17  robertj
00069  * Fixed mismatched declarations, thanks Vjacheslav Andrejev
00070  *
00071  * Revision 1.36  2001/05/22 12:49:32  robertj
00072  * Did some seriously wierd rewrite of platform headers to eliminate the
00073  *   stupid GNU compiler warning about braces not matching.
00074  *
00075  * Revision 1.35  2001/03/20 06:44:25  robertj
00076  * Lots of changes to fix the problems with terminating threads that are I/O
00077  *   blocked, especially when doing orderly shutdown of service via SIGTERM.
00078  *
00079  * Revision 1.34  2000/06/26 13:58:42  robertj
00080  * Nucleus port (again)
00081  *
00082  * Revision 1.33  2000/06/26 11:17:19  robertj
00083  * Nucleus++ port (incomplete).
00084  *
00085  * Revision 1.32  2000/02/18 09:55:21  robertj
00086  * Added parameter so get/setsockopt can have other levels to SOL_SOCKET.
00087  *
00088  * Revision 1.31  1999/03/09 02:59:51  robertj
00089  * Changed comments to doc++ compatible documentation.
00090  *
00091  * Revision 1.30  1999/02/16 08:11:10  robertj
00092  * MSVC 6.0 compatibility changes.
00093  *
00094  * Revision 1.29  1998/11/30 02:51:58  robertj
00095  * New directory structure
00096  *
00097  * Revision 1.28  1998/09/23 06:21:25  robertj
00098  * Added open source copyright license.
00099  *
00100  * Revision 1.27  1998/08/27 00:46:58  robertj
00101  * Resolved signedness problems with various GNU libraries.
00102  *
00103  * Revision 1.26  1998/08/25 14:07:42  robertj
00104  * Added getprotobyxxx wrapper functions.
00105  *
00106  * Revision 1.25  1998/01/26 00:35:21  robertj
00107  * Fixed documentation of PSocket::Select().
00108  *
00109  * Revision 1.24  1996/09/14 13:09:24  robertj
00110  * Major upgrade:
00111  *   rearranged sockets to help support IPX.
00112  *   added indirect channel class and moved all protocols to descend from it,
00113  *   separating the protocol from the low level byte transport.
00114  *
00115  * Revision 1.23  1996/07/27 04:14:00  robertj
00116  * Changed Select() calls to return error codes.
00117  *
00118  * Revision 1.22  1996/05/15 10:11:38  robertj
00119  * Added timeout to accept function.
00120  *
00121  * Revision 1.21  1996/03/31 08:52:36  robertj
00122  * Added socket shutdown function.
00123  *
00124  * Revision 1.20  1996/03/18 13:33:12  robertj
00125  * Fixed incompatibilities to GNU compiler where PINDEX != int.
00126  *
00127  * Revision 1.19  1996/03/03 07:37:58  robertj
00128  * Added Reusability clause to the Listen() function on sockets.
00129  *
00130  * Revision 1.18  1996/03/02 03:10:18  robertj
00131  * Added Apability to get and set Berkeley socket options.
00132  *
00133  * Revision 1.17  1996/02/25 03:02:14  robertj
00134  * Moved some socket functions to platform dependent code.
00135  * Added array of fds to os_select for unix threading support.
00136  *
00137  * Revision 1.16  1996/02/15 14:46:43  robertj
00138  * Added Select() function to PSocket.
00139  *
00140  * Revision 1.15  1995/12/23 03:46:54  robertj
00141  * Fixed portability issue with closingh sockets.
00142  *
00143  * Revision 1.14  1995/12/10 11:35:21  robertj
00144  * Numerous fixes for sockets.
00145  *
00146  * Revision 1.13  1995/10/14 15:05:54  robertj
00147  * Added functions for changing integer from host to network byte order.
00148  *
00149  * Revision 1.12  1995/06/17 11:13:25  robertj
00150  * Documentation update.
00151  *
00152  * Revision 1.11  1995/06/17 00:44:35  robertj
00153  * More logical design of port numbers and service names.
00154  * Changed overloaded Open() calls to 3 separate function names.
00155  *
00156  * Revision 1.10  1995/06/04 12:36:37  robertj
00157  * Slight redesign of port numbers on sockets.
00158  *
00159  * Revision 1.9  1995/03/14 12:42:39  robertj
00160  * Updated documentation to use HTML codes.
00161  *
00162  * Revision 1.8  1995/03/12  04:45:40  robertj
00163  * Added more functionality.
00164  *
00165  * Revision 1.7  1995/01/03  09:36:19  robertj
00166  * Documentation.
00167  *
00168  * Revision 1.6  1995/01/02  12:16:17  robertj
00169  * Moved constructor to platform dependent code.
00170  *
00171  * Revision 1.5  1994/08/23  11:32:52  robertj
00172  * Oops
00173  *
00174  * Revision 1.4  1994/08/22  00:46:48  robertj
00175  * Added pragma fro GNU C++ compiler.
00176  *
00177  * Revision 1.3  1994/08/21  23:43:02  robertj
00178  * Changed type of socket port number for better portability.
00179  *
00180  * Revision 1.2  1994/07/25  03:36:03  robertj
00181  * Added sockets to common, normalising to same comment standard.
00182  *
00183  */
00184 
00185 #ifndef _PSOCKETS
00186 #define _PSOCKETS
00187 
00188 #ifdef P_USE_PRAGMA
00189 #pragma interface
00190 #endif
00191 
00192 #include <ptlib/channel.h>
00193 
00194 #ifdef __NUCLEUS_PLUS__
00195 #include <sys/socket.h>
00196 #endif
00197 
00198 class PSocket;
00199 
00200 PLIST(PSocketList, PSocket);
00201 
00202 
00209 class PSocket : public PChannel
00210 {
00211   PCLASSINFO(PSocket, PChannel);
00212 
00213   protected:
00214     PSocket();
00215 
00216   public:
00229     virtual BOOL Connect(
00230       const PString & address   
00231     );
00232 
00233 
00235     enum Reusability {
00236       CanReuseAddress,
00237       AddressIsExclusive
00238     };
00239 
00253     virtual BOOL Listen(
00254       unsigned queueSize = 5,  
00255       WORD port = 0,           
00256       Reusability reuse = AddressIsExclusive 
00257     );
00258 
00259 
00281     virtual BOOL Accept(
00282       PSocket & socket          
00283     );
00284 
00290     virtual BOOL Shutdown(
00291       ShutdownValue option   
00292     );
00294 
00303     BOOL SetOption(
00304       int option,             
00305       int value,              
00306       int level = SOL_SOCKET  
00307     );
00308 
00315     BOOL SetOption(
00316       int option,             
00317       const void * valuePtr,  
00318       PINDEX valueSize,       
00319       int level = SOL_SOCKET  
00320     );
00321 
00328     BOOL GetOption(
00329       int option,             
00330       int & value,            
00331       int level = SOL_SOCKET  
00332     );
00333 
00340     BOOL GetOption(
00341       int option,             
00342       void * valuePtr,        
00343       PINDEX valueSize,       
00344       int level = SOL_SOCKET  
00345     );
00347 
00355     static WORD GetProtocolByName(
00356       const PString & name      
00357     );
00358 
00364     static PString GetNameByProtocol(
00365       WORD proto                
00366     );
00367 
00368 
00370     virtual WORD GetPortByService(
00371       const PString & service   
00372     ) const;
00390     static WORD GetPortByService(
00391       const char * protocol,     
00392       const PString & service    
00393     );
00394 
00396     virtual PString GetServiceByPort(
00397       WORD port   
00398     ) const;
00416     static PString GetServiceByPort(
00417       const char * protocol,  
00418       WORD port   
00419     );
00420 
00421 
00423     void SetPort(
00424       WORD port   
00425     );
00438     void SetPort(
00439       const PString & service   
00440     );
00441 
00447     WORD GetPort() const;
00448 
00456     PString GetService() const;
00458 
00461 
00462     class SelectList : public PSocketList
00463     {
00464       PCLASSINFO(SelectList, PSocketList)
00465       public:
00466         SelectList()
00467           { DisallowDeleteObjects(); }
00469         void operator+=(PSocket & sock )
00470           { Append(&sock); }
00472         void operator-=(PSocket & sock )
00473           { Remove(&sock); }
00474     };
00475 
00477     static int Select(
00478       PSocket & sock1,        
00479       PSocket & sock2         
00480     );
00482     static int Select(
00483       PSocket & sock1,        
00484       PSocket & sock2,        
00485       const PTimeInterval & timeout 
00486     );
00488     static Errors Select(
00489       SelectList & read       
00490     );
00492     static Errors Select(
00493       SelectList & read,      
00494       const PTimeInterval & timeout 
00495     );
00497     static Errors Select(
00498       SelectList & read,      
00499       SelectList & write      
00500     );
00502     static Errors Select(
00503       SelectList & read,      
00504       SelectList & write,     
00505       const PTimeInterval & timeout 
00506     );
00508     static Errors Select(
00509       SelectList & read,      
00510       SelectList & write,     
00511       SelectList & except     
00512     );
00534     static Errors Select(
00535       SelectList & read,      
00536       SelectList & write,     
00537       SelectList & except,    
00538       const PTimeInterval & timeout 
00539     );
00541 
00544 
00545     inline static WORD  Host2Net(WORD  v) { return htons(v); }
00547     inline static DWORD Host2Net(DWORD v) { return htonl(v); }
00548 
00550     inline static WORD  Net2Host(WORD  v) { return ntohs(v); }
00552     inline static DWORD Net2Host(DWORD v) { return ntohl(v); }
00554 
00555   protected:
00556     /*This function calls os_socket() with the correct parameters for the
00557        socket protocol type.
00558      */
00559     virtual BOOL OpenSocket() = 0;
00560 
00563     virtual const char * GetProtocolName() const = 0;
00564 
00565 
00566     int os_close();
00567     int os_socket(int af, int type, int proto);
00568     BOOL os_connect(
00569       struct sockaddr * sin,
00570       PINDEX size
00571     );
00572     BOOL os_recvfrom(
00573       void * buf,
00574       PINDEX len,
00575       int flags,
00576       struct sockaddr * from,
00577       PINDEX * fromlen
00578     );
00579     BOOL os_sendto(
00580       const void * buf,
00581       PINDEX len,
00582       int flags,
00583       struct sockaddr * to,
00584       PINDEX tolen
00585     );
00586     BOOL os_accept(
00587       PSocket & listener,
00588       struct sockaddr * addr,
00589       PINDEX * size
00590     );
00591 
00592 
00593   // Member variables
00595     WORD port;
00596 
00597 #if P_HAS_RECVMSG
00598     BOOL catchReceiveToAddr;
00599     virtual void SetLastReceiveAddr(void * /*addr*/, int /*addrLen*/)
00600     { }
00601 #endif
00602 
00603 // Include platform dependent part of class
00604 #ifdef _WIN32
00605 #include "msos/ptlib/socket.h"
00606 #else
00607 #include "unix/ptlib/socket.h"
00608 #endif
00609 };
00610 
00611 
00612 // Utility classes
00613 
00614 class P_fd_set {
00615   public:
00616     P_fd_set();
00617     P_fd_set(SOCKET fd);
00618     ~P_fd_set()
00619       {
00620         free(set);
00621       }
00622 
00623     P_fd_set & operator=(SOCKET fd);
00624     P_fd_set & operator+=(SOCKET fd);
00625     P_fd_set & operator-=(SOCKET fd);
00626 
00627     void Zero();
00628 
00629     BOOL IsPresent(SOCKET fd) const
00630       {
00631         return FD_ISSET(fd, set);
00632       }
00633 
00634     operator fd_set*() const
00635       {
00636         return set;
00637       }
00638 
00639   private:
00640     void Construct();
00641 
00642     SOCKET max_fd;
00643     fd_set * set;
00644 };
00645 
00646 
00647 class P_timeval {
00648   public:
00649     P_timeval();
00650     P_timeval(const PTimeInterval & time)
00651       {
00652         operator=(time);
00653       }
00654 
00655     P_timeval & operator=(const PTimeInterval & time);
00656 
00657     operator timeval*()
00658       {
00659         return infinite ? NULL : &tval;
00660       }
00661 
00662     timeval * operator->()
00663       {
00664         return &tval;
00665       }
00666 
00667     timeval & operator*()
00668       {
00669         return tval;
00670       }
00671 
00672   private:
00673     struct timeval tval;
00674     BOOL infinite;
00675 };
00676 
00677 #ifdef _WIN32
00678 class PWinSock : public PSocket
00679 {
00680   PCLASSINFO(PWinSock, PSocket)
00681 // Must be one and one only instance of this class, and it must be static!.
00682   public:
00683     PWinSock();
00684     ~PWinSock();
00685   private:
00686     virtual BOOL OpenSocket();
00687     virtual const char * GetProtocolName() const;
00688 };
00689 #endif
00690 
00691 #endif
00692 
00693 // End Of File ///////////////////////////////////////////////////////////////

Generated on Sat Dec 22 16:46:51 2007 for PWLib by  doxygen 1.4.2