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-AC03-76-SFO0515 with the Deprtment of Energy */
11 /******************************************************************************/
12 
13 // $Id$
14 
15 #ifndef WIN32
16 #include <sys/socket.h>
17 #else
18 #include <Winsock2.h>
19 #endif
20 
21 /******************************************************************************/
22 /* C l a s s D e f i n i t i o n */
23 /******************************************************************************/
24 
25 class XrdSysError;
26 
28 {
29 public:
30 
31 // When creating a socket object, you may pass an optional error routing object.
32 // If you do so, error messages will be writen via the error object. Otherwise,
33 // errors will be returned quietly. Addionally, you can attach a file descriptor
34 // to the socket object. This is useful when creating an object for accepted
35 // connections, e.g., ClientSock = new XrdNetSocket("", ServSock.Accept()).
36 //
37  XrdNetSocket(XrdSysError *erobj=0, int SockFileDesc=-1);
38 
40 
41 // Create a named socket. Returns a NetSocket object that can be used for the
42 // given path. A udp or tcp socket can be created on the path with the given
43 // file name. The access permission mode must also be supplied. Upon failure,
44 // a null pointer is returned.
45 //
46 static XrdNetSocket *Create(XrdSysError *Say, const char *path,
47  const char *fn, mode_t mode, int isudp=0);
48 
49 // Open a socket. Returns socket number upon success otherwise a -1. Use
50 // LastError() to find out the reason for failure. Only one socket at a time
51 // may be created. Use Close() to close the socket of Detach() to remove
52 // the socket association before creating a new one.
53 
54 // |<-------- C l i e n t -------->| |<-------- S e r v e r -------->|
55 // Unix Socket Internet Socket Unix Socket Internet Socket
56 // path = Filname hostname. filename 0 or ""
57 // port = -1 port number -1 port number
58 // flags = ~XRDNET_SERVER ~XRDNET_SERVER XRDNET_SERVER XRDNET_SERVER
59 
60 // If the client path does not start with a slash and the port number is -1
61 // then hostname must be of the form hostname:port. Open() will always set
62 // the REUSEADDR option when binding to a port number.
63 //
64  int Open(const char *path, int port=-1, int flags=0, int sockbuffsz=0);
65 
66 // Issue accept on the created socket. Upon success return socket FD, upon
67 // failure return -1. Use LastError() to obtain reason for failure. Note that
68 // Accept() is valid only for Server Sockets. An optional millisecond
69 // timeout may be specified. If no new connection is attempted within the
70 // millisecond time limit, a return is made with -1 and an error code of 0.
71 // Accept() always sets the "close on exec" flag for the new fd.
72 //
73  int Accept(int ms=-1);
74 
75 // Close a socket.
76 //
77  void Close();
78 
79 // Detach the socket filedescriptor without closing it. Useful when you
80 // will be attaching the descriptor to a stream. Returns the descriptor so
81 // you can do something like Stream.Attach(Socket.Detach()).
82 //
83  int Detach();
84 
85 // Return last errno.
86 //
87 inline int LastError() {return ErrCode;}
88 
89 // Obtain the name of the host on the other side of a socket. Upon success,
90 // a pointer to the hostname is returned. Otherwise null is returned. An
91 // optional address for holding the vided to obtain the hostname for it.
92 // The string is strdup'd and is deleted when the socket object is deleted.
93 //
94 const char *Peername(struct sockaddr **InetAddr=0);
95 
96 // Set socket options (see definitions in XrdNetOpts.hh). The defaults
97 // defaults are such that each option must be set to override the default
98 // behaviour. The method is static so it can be used in any context.
99 // An optional error routing object may be specified if error messages are
100 // wanted. Only when all option settings succeed is 0 is returned.
101 //
102 static int setOpts(int fd, int options, XrdSysError *eDest=0);
103 
104 // Set socket recv/send buffer sizes. The method is static so it can be used in
105 // any context. An optional error routing object may be specified if error
106 // messages are wanted. Only when all option settings succeed is 0 is returned.
107 //
108 static int setWindow(int fd, int Windowsz, XrdSysError *eDest=0);
109 
110 static int getWindow(int fd, int &Windowsz, XrdSysError *eDest=0);
111 
112 // Return socket file descriptor number (useful when attaching to a stream).
113 //
114 inline int SockNum() {return SockFD;}
115 
116 // Create an appropriate sockaddr structure for the supplied path which is
117 // either a hostname:port or a unix path. If successful, 0 is returned
118 // otherwise a const error message is returned. The address of the sockaddr
119 // is returned in sockAP and it's size is returned in sockAL upon success.
120 //
121 static const char *socketAddr(XrdSysError *Say, const char *dest,
122  struct sockaddr **sockAP, int &sockAL);
123 
124 // Create a path to a named socket returning the actual name of the socket.
125 // This method does not actually create the socket, only the path to the
126 // socket. If the full path exists then it must be a named socket. Upon
127 // success, it returns a pointer to the buffer holding the name (supplied by
128 // the caller). Otherwise, it returns a null pointer.
129 //
130 static char *socketPath(XrdSysError *Say, char *inbuff,
131  const char *path, const char *fn,
132  mode_t mode);
133 
134 /******************************************************************************/
135 
136 private:
137 int SockFD;
139 struct sockaddr PeerAddr;
140 char *PeerName;
142 };
143 #endif