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 #include "XrdNet/XrdNetAddr.hh"
00040 
00041 /******************************************************************************/
00042 /*                      C l a s s   D e f i n i t i o n                       */
00043 /******************************************************************************/
00044   
00045 class XrdSysError;
00046 
00047 class XrdNetSocket
00048 {
00049 public:
00050 
00051 // When creating a socket object, you may pass an optional error routing object.
00052 // If you do so, error messages will be writen via the error object. Otherwise,
00053 // errors will be returned quietly. Addionally, you can attach a file descriptor
00054 // to the socket object. This is useful when creating an object for accepted
00055 // connections, e.g., ClientSock = new XrdNetSocket("", ServSock.Accept()).
00056 //
00057             XrdNetSocket(XrdSysError *erobj=0, int SockFileDesc=-1);
00058 
00059            ~XrdNetSocket() {Close();}
00060 
00061 // Create a named socket. Returns a NetSocket object that can be used for the
00062 // given path. A udp or tcp socket can be created on the path with the given
00063 // file name. The access permission mode must also be supplied. Upon failure,
00064 // a null pointer is returned.
00065 //
00066 static XrdNetSocket *Create(XrdSysError *Say, const char *path,
00067                             const char *fn, mode_t mode, int isudp=0);
00068 
00069 // Open a socket. Returns socket number upon success otherwise a -1. Use
00070 // LastError() to find out the reason for failure. Only one socket at a time
00071 // may be created. Use Close() to close the socket of Detach() to remove
00072 // the socket association before creating a new one.
00073 
00074 //         |<-------- C l i e n t -------->|  |<-------- S e r v e r -------->|
00075 //         Unix Socket       Internet Socket  Unix Socket       Internet Socket
00076 // path  = Filname           hostname.        filename          0 or ""
00077 // port  = -1                port number      -1                port number
00078 // flags = ~XRDNET_SERVER    ~XRDNET_SERVER   XRDNET_SERVER     XRDNET_SERVER
00079 
00080 // If the client path does not start with a slash and the port number is -1
00081 // then hostname must be of the form hostname:port. Open() will always set
00082 // the REUSEADDR option when binding to a port number.
00083 //
00084        int  Open(const char *path, int port=-1, int flags=0, int sockbuffsz=0);
00085 
00086 // Issue accept on the created socket. Upon success return socket FD, upon
00087 // failure return -1. Use LastError() to obtain reason for failure. Note that
00088 // Accept() is valid only for Server Sockets. An optional millisecond
00089 // timeout may be specified. If no new connection is attempted within the
00090 // millisecond time limit, a return is made with -1 and an error code of 0.
00091 // Accept() always sets the "close on exec" flag for the new fd.
00092 //
00093        int  Accept(int ms=-1);
00094 
00095 // Close a socket.
00096 //
00097        void Close();
00098 
00099 // Detach the socket filedescriptor without closing it. Useful when you
00100 // will be attaching the descriptor to a stream. Returns the descriptor so
00101 // you can do something like Stream.Attach(Socket.Detach()).
00102 //
00103        int  Detach();
00104 
00105 // Return last errno.
00106 //
00107 inline int  LastError() {return ErrCode;}
00108 
00109 // Obtain the name of the host on the other side of a socket. Upon success,
00110 // a pointer to the hostname is returned. Otherwise null is returned. An
00111 // optional address for holding the vided to obtain the hostname for it.
00112 // The string is strdup'd and is deleted when the socket object is deleted.
00113 //
00114 const char *Peername(const struct sockaddr **InetAddr=0, int *InetSize=0);
00115 
00116 // Set socket options (see definitions in XrdNetOpts.hh). The defaults
00117 // defaults are such that each option must be set to override the default
00118 // behaviour. The method is static so it can be used in any context. 
00119 // An optional error routing object may be specified if error messages are 
00120 // wanted. Only when all option settings succeed is 0 is returned.
00121 //
00122 static int setOpts(int fd, int options, XrdSysError *eDest=0);
00123 
00124 // Set socket recv/send buffer sizes. The method is static so it can be used in 
00125 // any context. An optional error routing object may be specified if error 
00126 // messages are wanted. Only when all option settings succeed is 0 is returned.
00127 //
00128 static int setWindow(int fd, int  Windowsz, XrdSysError *eDest=0);
00129 
00130 static int getWindow(int fd, int &Windowsz, XrdSysError *eDest=0);
00131 
00132 // Return socket file descriptor number (useful when attaching to a stream).
00133 //
00134 inline int  SockNum() {return SockFD;}
00135 
00136 // Create a path to a named socket returning the actual name of the socket.
00137 // This method does not actually create the socket, only the path to the
00138 // socket. If the full path exists then it must be a named socket. Upon
00139 // success, it returns a pointer to the buffer holding the name (supplied by
00140 // the caller). Otherwise, it returns a null pointer.
00141 //
00142 static char *socketPath(XrdSysError *Say, char *inbuff,
00143                         const char *path, const char *fn, 
00144                         mode_t mode);
00145 
00146 /******************************************************************************/
00147   
00148 private:
00149 XrdNetAddr      SockInfo;
00150 XrdSysError    *eroute;
00151 int             SockFD;
00152 int             ErrCode;
00153 };
00154 #endif

Generated on 5 Oct 2016 for xrootd by  doxygen 1.4.7