xrootd
XrdNetSocket.hh
Go to the documentation of this file.
1 #ifndef __NETSOCKET__
2 #define __NETSOCKET__
3 /******************************************************************************/
4 /* */
5 /* X r d N e t S o c k e t . h h */
6 /* */
7 /* (C) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */
11 /* */
12 /* This file is part of the XRootD software suite. */
13 /* */
14 /* XRootD is free software: you can redistribute it and/or modify it under */
15 /* the terms of the GNU Lesser General Public License as published by the */
16 /* Free Software Foundation, either version 3 of the License, or (at your */
17 /* option) any later version. */
18 /* */
19 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22 /* License for more details. */
23 /* */
24 /* You should have received a copy of the GNU Lesser General Public License */
25 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27 /* */
28 /* The copyright holder's institutional names and contributor's names may not */
29 /* be used to endorse or promote products derived from this software without */
30 /* specific prior written permission of the institution or contributor. */
31 /******************************************************************************/
32 
33 #ifndef WIN32
34 #include <sys/socket.h>
35 #else
36 #include <Winsock2.h>
37 #endif
38 
39 /******************************************************************************/
40 /* C l a s s D e f i n i t i o n */
41 /******************************************************************************/
42 
43 class XrdSysError;
44 
46 {
47 public:
48 
49 // When creating a socket object, you may pass an optional error routing object.
50 // If you do so, error messages will be writen via the error object. Otherwise,
51 // errors will be returned quietly. Addionally, you can attach a file descriptor
52 // to the socket object. This is useful when creating an object for accepted
53 // connections, e.g., ClientSock = new XrdNetSocket("", ServSock.Accept()).
54 //
55  XrdNetSocket(XrdSysError *erobj=0, int SockFileDesc=-1);
56 
58 
59 // Create a named socket. Returns a NetSocket object that can be used for the
60 // given path. A udp or tcp socket can be created on the path with the given
61 // file name. The access permission mode must also be supplied. Upon failure,
62 // a null pointer is returned.
63 //
64 static XrdNetSocket *Create(XrdSysError *Say, const char *path,
65  const char *fn, mode_t mode, int isudp=0);
66 
67 // Open a socket. Returns socket number upon success otherwise a -1. Use
68 // LastError() to find out the reason for failure. Only one socket at a time
69 // may be created. Use Close() to close the socket of Detach() to remove
70 // the socket association before creating a new one.
71 
72 // |<-------- C l i e n t -------->| |<-------- S e r v e r -------->|
73 // Unix Socket Internet Socket Unix Socket Internet Socket
74 // path = Filname hostname. filename 0 or ""
75 // port = -1 port number -1 port number
76 // flags = ~XRDNET_SERVER ~XRDNET_SERVER XRDNET_SERVER XRDNET_SERVER
77 
78 // If the client path does not start with a slash and the port number is -1
79 // then hostname must be of the form hostname:port. Open() will always set
80 // the REUSEADDR option when binding to a port number.
81 //
82  int Open(const char *path, int port=-1, int flags=0, int sockbuffsz=0);
83 
84 // Issue accept on the created socket. Upon success return socket FD, upon
85 // failure return -1. Use LastError() to obtain reason for failure. Note that
86 // Accept() is valid only for Server Sockets. An optional millisecond
87 // timeout may be specified. If no new connection is attempted within the
88 // millisecond time limit, a return is made with -1 and an error code of 0.
89 // Accept() always sets the "close on exec" flag for the new fd.
90 //
91  int Accept(int ms=-1);
92 
93 // Close a socket.
94 //
95  void Close();
96 
97 // Detach the socket filedescriptor without closing it. Useful when you
98 // will be attaching the descriptor to a stream. Returns the descriptor so
99 // you can do something like Stream.Attach(Socket.Detach()).
100 //
101  int Detach();
102 
103 // Return last errno.
104 //
105 inline int LastError() {return ErrCode;}
106 
107 // Obtain the name of the host on the other side of a socket. Upon success,
108 // a pointer to the hostname is returned. Otherwise null is returned. An
109 // optional address for holding the vided to obtain the hostname for it.
110 // The string is strdup'd and is deleted when the socket object is deleted.
111 //
112 const char *Peername(struct sockaddr **InetAddr=0);
113 
114 // Set socket options (see definitions in XrdNetOpts.hh). The defaults
115 // defaults are such that each option must be set to override the default
116 // behaviour. The method is static so it can be used in any context.
117 // An optional error routing object may be specified if error messages are
118 // wanted. Only when all option settings succeed is 0 is returned.
119 //
120 static int setOpts(int fd, int options, XrdSysError *eDest=0);
121 
122 // Set socket recv/send buffer sizes. The method is static so it can be used in
123 // any context. An optional error routing object may be specified if error
124 // messages are wanted. Only when all option settings succeed is 0 is returned.
125 //
126 static int setWindow(int fd, int Windowsz, XrdSysError *eDest=0);
127 
128 static int getWindow(int fd, int &Windowsz, XrdSysError *eDest=0);
129 
130 // Return socket file descriptor number (useful when attaching to a stream).
131 //
132 inline int SockNum() {return SockFD;}
133 
134 // Create an appropriate sockaddr structure for the supplied path which is
135 // either a hostname:port or a unix path. If successful, 0 is returned
136 // otherwise a const error message is returned. The address of the sockaddr
137 // is returned in sockAP and it's size is returned in sockAL upon success.
138 //
139 static const char *socketAddr(XrdSysError *Say, const char *dest,
140  struct sockaddr **sockAP, int &sockAL);
141 
142 // Create a path to a named socket returning the actual name of the socket.
143 // This method does not actually create the socket, only the path to the
144 // socket. If the full path exists then it must be a named socket. Upon
145 // success, it returns a pointer to the buffer holding the name (supplied by
146 // the caller). Otherwise, it returns a null pointer.
147 //
148 static char *socketPath(XrdSysError *Say, char *inbuff,
149  const char *path, const char *fn,
150  mode_t mode);
151 
152 /******************************************************************************/
153 
154 private:
155 int SockFD;
157 struct sockaddr PeerAddr;
158 char *PeerName;
160 };
161 #endif