XrdNetSocket.hh

Go to the documentation of this file.
00001 #ifndef __NETSOCKET__
00002 #define __NETSOCKET__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                       X r d N e t S o c k e t . h h                        */
00006 /*                                                                            */
00007 /* (C) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
00008 /*                            All Rights Reserved                             */
00009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00010 /*                DE-AC02-76-SFO0515 with the Deprtment of Energy             */
00011 /*                                                                            */
00012 /* This file is part of the XRootD software suite.                            */
00013 /*                                                                            */
00014 /* XRootD is free software: you can redistribute it and/or modify it under    */
00015 /* the terms of the GNU Lesser General Public License as published by the     */
00016 /* Free Software Foundation, either version 3 of the License, or (at your     */
00017 /* option) any later version.                                                 */
00018 /*                                                                            */
00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00022 /* License for more details.                                                  */
00023 /*                                                                            */
00024 /* You should have received a copy of the GNU Lesser General Public License   */
00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00027 /*                                                                            */
00028 /* The copyright holder's institutional names and contributor's names may not */
00029 /* be used to endorse or promote products derived from this software without  */
00030 /* specific prior written permission of the institution or contributor.       */
00031 /******************************************************************************/
00032   
00033 #ifndef WIN32
00034 #include <sys/socket.h>
00035 #else
00036 #include <Winsock2.h>
00037 #endif
00038 
00039 /******************************************************************************/
00040 /*                      C l a s s   D e f i n i t i o n                       */
00041 /******************************************************************************/
00042   
00043 class XrdSysError;
00044 
00045 class XrdNetSocket
00046 {
00047 public:
00048 
00049 // When creating a socket object, you may pass an optional error routing object.
00050 // If you do so, error messages will be writen via the error object. Otherwise,
00051 // errors will be returned quietly. Addionally, you can attach a file descriptor
00052 // to the socket object. This is useful when creating an object for accepted
00053 // connections, e.g., ClientSock = new XrdNetSocket("", ServSock.Accept()).
00054 //
00055             XrdNetSocket(XrdSysError *erobj=0, int SockFileDesc=-1);
00056 
00057            ~XrdNetSocket() {Close();}
00058 
00059 // Create a named socket. Returns a NetSocket object that can be used for the
00060 // given path. A udp or tcp socket can be created on the path with the given
00061 // file name. The access permission mode must also be supplied. Upon failure,
00062 // a null pointer is returned.
00063 //
00064 static XrdNetSocket *Create(XrdSysError *Say, const char *path,
00065                             const char *fn, mode_t mode, int isudp=0);
00066 
00067 // Open a socket. Returns socket number upon success otherwise a -1. Use
00068 // LastError() to find out the reason for failure. Only one socket at a time
00069 // may be created. Use Close() to close the socket of Detach() to remove
00070 // the socket association before creating a new one.
00071 
00072 //         |<-------- C l i e n t -------->|  |<-------- S e r v e r -------->|
00073 //         Unix Socket       Internet Socket  Unix Socket       Internet Socket
00074 // path  = Filname           hostname.        filename          0 or ""
00075 // port  = -1                port number      -1                port number
00076 // flags = ~XRDNET_SERVER    ~XRDNET_SERVER   XRDNET_SERVER     XRDNET_SERVER
00077 
00078 // If the client path does not start with a slash and the port number is -1
00079 // then hostname must be of the form hostname:port. Open() will always set
00080 // the REUSEADDR option when binding to a port number.
00081 //
00082        int  Open(const char *path, int port=-1, int flags=0, int sockbuffsz=0);
00083 
00084 // Issue accept on the created socket. Upon success return socket FD, upon
00085 // failure return -1. Use LastError() to obtain reason for failure. Note that
00086 // Accept() is valid only for Server Sockets. An optional millisecond
00087 // timeout may be specified. If no new connection is attempted within the
00088 // millisecond time limit, a return is made with -1 and an error code of 0.
00089 // Accept() always sets the "close on exec" flag for the new fd.
00090 //
00091        int  Accept(int ms=-1);
00092 
00093 // Close a socket.
00094 //
00095        void Close();
00096 
00097 // Detach the socket filedescriptor without closing it. Useful when you
00098 // will be attaching the descriptor to a stream. Returns the descriptor so
00099 // you can do something like Stream.Attach(Socket.Detach()).
00100 //
00101        int  Detach();
00102 
00103 // Return last errno.
00104 //
00105 inline int  LastError() {return ErrCode;}
00106 
00107 // Obtain the name of the host on the other side of a socket. Upon success,
00108 // a pointer to the hostname is returned. Otherwise null is returned. An
00109 // optional address for holding the vided to obtain the hostname for it.
00110 // The string is strdup'd and is deleted when the socket object is deleted.
00111 //
00112 const char *Peername(struct sockaddr **InetAddr=0);
00113 
00114 // Set socket options (see definitions in XrdNetOpts.hh). The defaults
00115 // defaults are such that each option must be set to override the default
00116 // behaviour. The method is static so it can be used in any context. 
00117 // An optional error routing object may be specified if error messages are 
00118 // wanted. Only when all option settings succeed is 0 is returned.
00119 //
00120 static int setOpts(int fd, int options, XrdSysError *eDest=0);
00121 
00122 // Set socket recv/send buffer sizes. The method is static so it can be used in 
00123 // any context. An optional error routing object may be specified if error 
00124 // messages are wanted. Only when all option settings succeed is 0 is returned.
00125 //
00126 static int setWindow(int fd, int  Windowsz, XrdSysError *eDest=0);
00127 
00128 static int getWindow(int fd, int &Windowsz, XrdSysError *eDest=0);
00129 
00130 // Return socket file descriptor number (useful when attaching to a stream).
00131 //
00132 inline int  SockNum() {return SockFD;}
00133 
00134 // Create an appropriate sockaddr structure for the supplied path which is
00135 // either a hostname:port or a unix path. If successful, 0 is returned
00136 // otherwise a const error message is returned. The address of the sockaddr
00137 // is returned in sockAP and it's size is returned in sockAL upon success.
00138 //
00139 static const char *socketAddr(XrdSysError *Say, const char *dest,
00140                               struct sockaddr **sockAP, int &sockAL);
00141 
00142 // Create a path to a named socket returning the actual name of the socket.
00143 // This method does not actually create the socket, only the path to the
00144 // socket. If the full path exists then it must be a named socket. Upon
00145 // success, it returns a pointer to the buffer holding the name (supplied by
00146 // the caller). Otherwise, it returns a null pointer.
00147 //
00148 static char *socketPath(XrdSysError *Say, char *inbuff,
00149                         const char *path, const char *fn, 
00150                         mode_t mode);
00151 
00152 /******************************************************************************/
00153   
00154 private:
00155 int             SockFD;
00156 int             ErrCode;
00157 struct sockaddr PeerAddr;
00158 char           *PeerName;
00159 XrdSysError    *eroute;
00160 };
00161 #endif

Generated on 27 Jul 2013 for xrootd by  doxygen 1.4.7