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