xrootd
XrdNetLink.hh
Go to the documentation of this file.
1 #ifndef __NET_LINK_H__
2 #define __NET_LINK_H__
3 /******************************************************************************/
4 /* */
5 /* X r d N e t L i n k . 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 Department 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 <netinet/in.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
37 #include <sys/uio.h>
38 #include <fcntl.h>
39 #else
40 #include <Winsock2.h>
41 #endif
42 
43 #include "XrdNet/XrdNetBuffer.hh"
44 #include "XrdSys/XrdSysDNS.hh"
45 #include "XrdSys/XrdSysPthread.hh"
46 
47 // Options for SetOpts and Alloc()
48 //
49 #define XRDNETLINK_NOBLOCK 0x0001
50 #define XRDNETLINK_NOCLOSE 0x0002
51 #define XRDNETLINK_NOSTREAM 0x0004
52 
53 // The XrdNetLink class defines the i/o operations on a network link.
54 //
55 class XrdNet;
56 class XrdNetPeer;
57 class XrdSysError;
58 class XrdOucStream;
59 class XrdOucTokenizer;
60 
62 {
63 public:
64 
66 
67 static XrdNetLink *Alloc(XrdSysError *erp, XrdNet *Net, XrdNetPeer &Peer,
68  XrdNetBufferQ *bq, int opts=0);
69 
70 // Closes() closes the link. Specify defer=1 to postpone deallocating
71 // attached objects until the this object is destroyed. You should
72 // use defered close for cross-thread unsynchronized closes.
73 //
74 int Close(int defer=0);
75 
76 // FDnum() returns the associated file descriptor
77 //
78 int FDnum() {return FD;}
79 
80 // The following implement text-oriented read routines. The correspond to
81 // those implemented by the XrdOucStream object which is the one used.
82 //
83 char *GetLine();
84 
85 char *GetToken(char **rest);
86 char *GetToken(void);
87 void RetToken(void);
88 
89 // isConnected() returns true if this object is connected to an XrdOucStream
90 // object and false otherwise.
91 //
92 int isConnected(void) {return (Stream != 0) && (FD >= 0);}
93 
94 // LastError() returns the error number associated with the last error
95 //
96 int LastError();
97 
98 // Addr() returns the IPV4 address of the endpoint
99 //
100 unsigned int Addr() {return XrdSysDNS::IPAddr(&InetAddr);}
101 
102 // Moniker() returns the short form of the host name at the endpoint
103 //
104 const char *Moniker() {return Sname;}
105 
106 // Name() returns the full host name of the endpoint
107 //
108 const char *Name() {return Lname;}
109 
110 // Moniker() returns the short form of the host name at the endpoint
111 //
112 const char *Nick() {return Sname;}
113 
114 // OK2Recv() returns true if data can be received within tmo, false otherwise.
115 //
116 int OK2Recv(int mills);
117 
118 // Recycle() makes this object available for reuse.
119 //
120 void Recycle();
121 
122 // Send() set of methods that accept a char buffer are text oriented send
123 // routines. They all add a new-line (\n) character to end the buffer
124 // if it does not exist already.
125 //
126 int Send(const char *buff, // -> Data to send
127  int blen=0, // Length. If 0, it's compued via strlen()
128  int tmo=-1); // Millisecond timeout (default is none)
129 
130 int Send(const char *dest, // -> Hostname to send UDP datagram
131  const char *buff, // Remaining parms as above
132  int blen=0,
133  int tmo=-1);
134 
135 int Send(const struct iovec iov[], // writev() style plist
136  int iovcnt, // Number of elements om iov[]
137  int tmo = -1); // Optional timeout
138 
139 int Send(const char *dest, // Hostname to send UDP datagram
140  const struct iovec iov[], // Remaining parms as above
141  int iovcnt,
142  int tmo=-1);
143 
144 // Send() set of methods that accept a void buffer are byte oriented send
145 // routines. These do not inspect the data at all.
146 //
147 int Send(const void *buff, // -> Data to send
148  int blen=0, // Length. If 0, it's compued via strlen()
149  int tmo=-1); // Millisecond timeout (default is none)
150 
151 // Recv() receives up to blen bytes. It may receive less than that if
152 // additional bytes are not immediately available to receive.
153 // it returns the number of bytes read or -1 if an error occurs.
154 //
155 int Recv(char *buff, int blen);
156 
157 // Set() sets the maximum number of XrdNetLink objects that may be kept
158 // for future re-use.
159 //
160 void Set(int maxl);
161 
162 // SetOpts() is used to set socket options, as defined above.
163 //
164 void SetOpts(int opts);
165 
166 // Instantiate this object with the pointer to an error object for message
167 // routing. Additionally, a pointer to a UDP buffer may be provided. This
168 // buffer must contain a text datagram suitable for tokenization.
169 //
171  {FD = -1; Lname = Sname = 0; recvbuff = sendbuff = 0;
172  BuffQ = bq; Stream = 0; Bucket = 0; eDest = erp;
173  }
175 
176 private:
177 
178 int OK2Send(int timeout=0, const char *dest=0);
179 int retErr(int ecode, const char *dest=0);
180 
184 int FD;
187 struct sockaddr InetAddr;
188 char *Lname; // Long hostname
189 char *Sname; // Short hostname (may be the same as Lname)
190 XrdNetBuffer *recvbuff; // udp receive buffer
191 XrdNetBuffer *sendbuff; // udp send buffer
192 XrdOucStream *Stream; // tcp tokenizer
193 XrdOucTokenizer *Bucket; // udp tokenizer
195 
198 static int size;
199 static int maxlink;
200 static int numlink;
201 static int devNull;
202 };
203 #endif