Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:40

0001 #ifndef __NETSOCKET__
0002 #define __NETSOCKET__
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                       X r d N e t S o c k e t . h h                        */
0006 /*                                                                            */
0007 /* (C) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
0008 /*                            All Rights Reserved                             */
0009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
0010 /*                DE-AC02-76-SFO0515 with the Deprtment of Energy             */
0011 /*                                                                            */
0012 /* This file is part of the XRootD software suite.                            */
0013 /*                                                                            */
0014 /* XRootD is free software: you can redistribute it and/or modify it under    */
0015 /* the terms of the GNU Lesser General Public License as published by the     */
0016 /* Free Software Foundation, either version 3 of the License, or (at your     */
0017 /* option) any later version.                                                 */
0018 /*                                                                            */
0019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
0020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
0021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
0022 /* License for more details.                                                  */
0023 /*                                                                            */
0024 /* You should have received a copy of the GNU Lesser General Public License   */
0025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
0026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
0027 /*                                                                            */
0028 /* The copyright holder's institutional names and contributor's names may not */
0029 /* be used to endorse or promote products derived from this software without  */
0030 /* specific prior written permission of the institution or contributor.       */
0031 /******************************************************************************/
0032   
0033 #ifndef WIN32
0034 #include <sys/socket.h>
0035 #else
0036 #include <Winsock2.h>
0037 #endif
0038 
0039 #include "XrdNet/XrdNetAddr.hh"
0040 
0041 /******************************************************************************/
0042 /*                      C l a s s   D e f i n i t i o n                       */
0043 /******************************************************************************/
0044   
0045 class XrdSysError;
0046 
0047 class XrdNetSocket
0048 {
0049 public:
0050 
0051 // When creating a socket object, you may pass an optional error routing object.
0052 // If you do so, error messages will be writen via the error object. Otherwise,
0053 // errors will be returned quietly. Addionally, you can attach a file descriptor
0054 // to the socket object. This is useful when creating an object for accepted
0055 // connections, e.g., ClientSock = new XrdNetSocket("", ServSock.Accept()).
0056 //
0057             XrdNetSocket(XrdSysError *erobj=0, int SockFileDesc=-1);
0058 
0059            ~XrdNetSocket() {Close();}
0060 
0061 // Create a named socket. Returns a NetSocket object that can be used for the
0062 // given path. A udp or tcp socket can be created on the path with the given
0063 // file name. The access permission mode must also be supplied. Upon failure,
0064 // a null pointer is returned.
0065 //
0066 static XrdNetSocket *Create(XrdSysError *Say, const char *path,
0067                             const char *fn, mode_t mode, int isudp=0);
0068 
0069 // Open a socket. Returns socket number upon success otherwise a -1. Use
0070 // LastError() to find out the reason for failure. Only one socket at a time
0071 // may be created. Use Close() to close the socket of Detach() to remove
0072 // the socket association before creating a new one.
0073 
0074 //         |<-------- C l i e n t -------->|  |<-------- S e r v e r -------->|
0075 //         Unix Socket       Internet Socket  Unix Socket       Internet Socket
0076 // path  = Filname           hostname.        filename          0 or ""
0077 // port  = -1                port number      -1                port number
0078 // flags = ~XRDNET_SERVER    ~XRDNET_SERVER   XRDNET_SERVER     XRDNET_SERVER
0079 
0080 // If the client path does not start with a slash and the port number is -1
0081 // then hostname must be of the form hostname:port. Open() will always set
0082 // the REUSEADDR option when binding to a port number.
0083 //
0084        int  Open(const char *path, int port=-1, int flags=0, int sockbuffsz=0);
0085 
0086 // Issue accept on the created socket. Upon success return socket FD, upon
0087 // failure return -1. Use LastError() to obtain reason for failure. Note that
0088 // Accept() is valid only for Server Sockets. An optional millisecond
0089 // timeout may be specified. If no new connection is attempted within the
0090 // millisecond time limit, a return is made with -1 and an error code of 0.
0091 // Accept() always sets the "close on exec" flag for the new fd.
0092 //
0093        int  Accept(int ms=-1);
0094 
0095 // Close a socket.
0096 //
0097        void Close();
0098 
0099 // Detach the socket filedescriptor without closing it. Useful when you
0100 // will be attaching the descriptor to a stream. Returns the descriptor so
0101 // you can do something like Stream.Attach(Socket.Detach()).
0102 //
0103        int  Detach();
0104 
0105 // Return last errno.
0106 //
0107 inline int  LastError() {return ErrCode;}
0108 
0109 // Obtain the name of the host on the other side of a socket. Upon success,
0110 // a pointer to the hostname is returned. Otherwise null is returned. An
0111 // optional address for holding the vided to obtain the hostname for it.
0112 // The string is strdup'd and is deleted when the socket object is deleted.
0113 //
0114 const char *Peername(const struct sockaddr **InetAddr=0, int *InetSize=0);
0115 
0116 // Set socket options (see definitions in XrdNetOpts.hh). The defaults
0117 // defaults are such that each option must be set to override the default
0118 // behaviour. The method is static so it can be used in any context. 
0119 // An optional error routing object may be specified if error messages are 
0120 // wanted. Only when all option settings succeed is 0 is returned.
0121 //
0122 static int setOpts(int fd, int options, XrdSysError *eDest=0);
0123 
0124 // Set socket recv/send buffer sizes. The method is static so it can be used in 
0125 // any context. An optional error routing object may be specified if error 
0126 // messages are wanted. Only when all option settings succeed is 0 is returned.
0127 //
0128 static int setWindow(int fd, int  Windowsz, XrdSysError *eDest=0);
0129 
0130 static int getWindow(int fd, int &Windowsz, XrdSysError *eDest=0);
0131 
0132 // Return the name of the socket into the provided buffer. Returns 0
0133 // upon success or errno upon failure.
0134 //
0135 int         SockName(char *buff, int blen);
0136 
0137 // Return socket file descriptor number (useful when attaching to a stream).
0138 //
0139 inline int  SockNum() {return SockFD;}
0140 
0141 // Create a path to a named socket returning the actual name of the socket.
0142 // This method does not actually create the socket, only the path to the
0143 // socket. If the full path exists then it must be a named socket. Upon
0144 // success, it returns a pointer to the buffer holding the name (supplied by
0145 // the caller). Otherwise, it returns a null pointer.
0146 //
0147 static char *socketPath(XrdSysError *Say, char *inbuff,
0148                         const char *path, const char *fn, 
0149                         mode_t mode);
0150 
0151 /******************************************************************************/
0152   
0153 private:
0154 XrdNetAddr      SockInfo;
0155 XrdSysError    *eroute;
0156 int             SockFD;
0157 int             ErrCode;
0158 };
0159 #endif