XRootD
Loading...
Searching...
No Matches
XrdNet Class Reference

#include <XrdNet.hh>

+ Inheritance diagram for XrdNet:
+ Collaboration diagram for XrdNet:

Public Member Functions

 XrdNet (XrdSysError *erp, XrdNetSecurity *secp=0)
 
virtual ~XrdNet ()
 Destructor.
 
int Accept (XrdNetAddr &myAddr, int opts=0, int timeout=-1)
 
int Accept (XrdNetPeer &myPeer, int opts=0, int timeout=-1)
 
int Bind (char *path, const char *contype="stream")
 
int Bind (int port, const char *contype="tcp")
 
int Connect (XrdNetAddr &myAddr, const char *dest, int port=-1, int opts=0, int timeout=-1)
 
int Connect (XrdNetPeer &myPeer, const char *dest, int port=-1, int opts=0, int timeout=-1)
 
int Port ()
 
int Relay (const char *dest)
 
int Relay (XrdNetPeer &Peer, const char *dest, int opts=0)
 
virtual void Secure (XrdNetSecurity *secp)
 
void setDefaults (int options, int buffsz=0)
 
void setDomain (const char *dname)
 
void Trim (char *hname)
 
void unBind ()
 Unbind the network from any bound resouces.
 
int WSize ()
 

Protected Attributes

XrdNetBufferQBuffQ
 
int BuffSize
 
char * Domain
 
int Domlen
 
XrdSysErroreDest
 
int iofd
 
int netOpts
 
XrdNetSecurityPolice
 
int Portnum
 
int PortType
 
int Windowsz
 

Detailed Description

Definition at line 51 of file XrdNet.hh.

Constructor & Destructor Documentation

◆ XrdNet()

XrdNet::XrdNet ( XrdSysError * erp,
XrdNetSecurity * secp = 0 )

Constructor

Parameters
erpThe error object for printing error messages. It must be supplied.
secpThe initial NetSecurity object. This secp object must not be deleted nor directly used after the call as this object assumes its ownership and may delete it at any time.

Definition at line 60 of file XrdNet.cc.

61{
62 iofd = PortType = -1;
63 eDest = erp;
64 Police = secp;
66 Domain = 0;
67 BuffQ = 0;
68}
char * Domain
Definition XrdNet.hh:285
int Windowsz
Definition XrdNet.hh:290
int iofd
Definition XrdNet.hh:287
XrdSysError * eDest
Definition XrdNet.hh:283
XrdNetBufferQ * BuffQ
Definition XrdNet.hh:293
XrdNetSecurity * Police
Definition XrdNet.hh:284
int netOpts
Definition XrdNet.hh:291
int Domlen
Definition XrdNet.hh:286
int Portnum
Definition XrdNet.hh:288
int PortType
Definition XrdNet.hh:289

References BuffQ, Domain, Domlen, eDest, iofd, netOpts, Police, Portnum, PortType, and Windowsz.

◆ ~XrdNet()

XrdNet::~XrdNet ( )
virtual

Destructor.

Definition at line 74 of file XrdNet.cc.

75{
76 unBind();
77 if (Domain) free(Domain);
78}
void unBind()
Unbind the network from any bound resouces.
Definition XrdNet.cc:358

References Domain, and unBind().

+ Here is the call graph for this function:

Member Function Documentation

◆ Accept() [1/2]

int XrdNet::Accept ( XrdNetAddr & myAddr,
int opts = 0,
int timeout = -1 )

Accept incoming TCP connection. This is the preferred method for TCP.

Parameters
myAddrthe address object to contain connection information.
optsprocessing options: XRDNET_DELAY - do not set nodelay on socket. XRDNET_KEEPALIVE - use TCP keep alive on socket. XRDNET_NOCLOSEX - do not allow socket to close on exec() XRDNET_NOEMSG - supress printing error messages XRDNET_NOLINGER - Do not linger when closing socket.
timeoutmaximum seconds to wait for a conection. When negative, the default, no time limit applies.
Returns
!0 Successful connection occurred, myAddr holds information.
=0 Failure, a timeout or permanent error occurred.

Definition at line 84 of file XrdNet.cc.

85{
86 int retc;
87
88// Make sure we are bound to a port
89//
90 opts |= netOpts;
91 if (iofd < 0)
92 {if (!(opts & XRDNET_NOEMSG))
93 eDest->Emsg("Accept", "Network not bound to a port.");
94 return 0;
95 }
96
97// This interface only accepts TCP connections
98//
99 if (PortType != SOCK_STREAM)
100 {if (!(opts & XRDNET_NOEMSG))
101 eDest->Emsg("Accept", "UDP network not supported for NetAddr call.");
102 return 0;
103 }
104
105// Setup up the poll structure to wait for new connections
106//
107 do {if (timeout >= 0)
108 {struct pollfd sfd = {iofd,
109 POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI|POLLHUP,0};
110 do {retc = poll(&sfd, 1, timeout*1000);}
111 while(retc < 0 && (errno == EAGAIN || errno == EINTR));
112 if (!retc)
113 {if (!(opts & XRDNET_NOEMSG))
114 eDest->Emsg("Accept", "Accept timed out.");
115 return 0;
116 }
117 }
118 } while(!do_Accept_TCP(myAddr, opts));
119
120 return 1;
121}
#define XRDNET_NOEMSG
Definition XrdNetOpts.hh:71
struct myOpts opts
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)

References eDest, XrdSysError::Emsg(), iofd, netOpts, opts, PortType, and XRDNET_NOEMSG.

Referenced by XrdInet::Accept().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Accept() [2/2]

int XrdNet::Accept ( XrdNetPeer & myPeer,
int opts = 0,
int timeout = -1 )

Accept incoming TCP or UDP connection. This method should only be used for UDP-style networks. The previous method is preferred for TCP-style networks.

Parameters
myPeerthe peer object to contain connection information.
optsprocessing options: XRDNET_DELAY - TCP: do not set nodelay on socket. XRDNET_KEEPALIVE - TCP: use TCP keep alive on socket. XRDNET_NEWFD - UDP: obtain new file descriptor XRDNET_NOCLOSEX - ALL: keep socket across exec() calls XRDNET_NODNTRIM - ALL: don't trim domain name in myPeer XRDNET_NOEMSG - ALL: supress printing error messages XRDNET_NORLKUP - ALL: avoid doing reverse DNS look-up XRDNET_USETLS - ALL: enable TLS upon connection
timeoutmaximum seconds to wait for a conection. When negative, the default, no time limit applies.
Returns
!0 Successful connection occurred, myPeer holds information.
=0 Failure, a timeout or permanent error occurred.

Definition at line 125 of file XrdNet.cc.

126{
127 int retc;
128
129// Make sure we are bound to a port
130//
131 opts |= netOpts;
132 if (iofd < 0)
133 {if (!(opts & XRDNET_NOEMSG))
134 eDest->Emsg("Accept", "Network not bound to a port.");
135 return 0;
136 }
137
138// Setup up the poll structure to wait for new connections
139//
140 do {if (timeout >= 0)
141 {struct pollfd sfd = {iofd,
142 POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI|POLLHUP,0};
143 do {retc = poll(&sfd, 1, timeout*1000);}
144 while(retc < 0 && (errno == EAGAIN || errno == EINTR));
145 if (!retc)
146 {if (!(opts & XRDNET_NOEMSG))
147 eDest->Emsg("Accept", "Accept timed out.");
148 return 0;
149 }
150 }
151 } while(!(PortType == SOCK_STREAM ? do_Accept_TCP(myPeer, opts)
152 : do_Accept_UDP(myPeer, opts)));
153
154// Accept completed, trim the host name if a domain has been specified,
155//
156 if (Domain && !(opts & XRDNET_NODNTRIM)) Trim(myPeer.InetName);
157 return 1;
158}
#define XRDNET_NODNTRIM
Definition XrdNetOpts.hh:51
char * InetName
Definition XrdNetPeer.hh:44
void Trim(char *hname)
Definition XrdNet.cc:343

References Domain, eDest, XrdSysError::Emsg(), XrdNetPeer::InetName, iofd, netOpts, opts, PortType, Trim(), XRDNET_NODNTRIM, and XRDNET_NOEMSG.

+ Here is the call graph for this function:

◆ Bind() [1/2]

int XrdNet::Bind ( char * path,
const char * contype = "stream" )

Bind a network object to a Unix named socket.

Parameters
paththe file system path to a named socket to bind with.
contypeEither "stream" to use TCP-tyle streaming protocol or "datagram" to use UDP-style messaging.
Returns
0 Successfully bound the port.
!0 Failure, return value is -errno describing the error.

Definition at line 206 of file XrdNet.cc.

207{
208 XrdNetSocket mySocket(eDest);
209 int opts = XRDNET_SERVER | netOpts;
210 int buffsz = Windowsz;
211
212// Make sure this is a path and not a host name
213//
214 if (*path != '/')
215 {eDest->Emsg("Bind", "Invalid bind path -", path);
216 return -EINVAL;
217 }
218
219// Close any open socket here
220//
221 unBind();
222
223// Get correct option settings
224//
225 if (*contype != 'd') PortType = SOCK_STREAM;
226 else {PortType = SOCK_DGRAM;
228 if (!buffsz) buffsz = XRDNET_UDPBUFFSZ;
229 }
230
231// Try to open and bind to this path
232//
233 if (mySocket.Open(path, -1, opts, buffsz) < 0) return -mySocket.LastError();
234
235// Success, get the socket number and return
236//
237 iofd = mySocket.Detach();
238
239// For udp sockets, we must allocate a buffer queue object
240//
241 if (PortType == SOCK_DGRAM)
242 {BuffSize = buffsz;
243 BuffQ = new XrdNetBufferQ(buffsz);
244 }
245 return 0;
246}
#define XRDNET_SERVER
Definition XrdNetOpts.hh:99
#define XRDNET_UDPSOCKET
Definition XrdNetOpts.hh:79
#define XRDNET_UDPBUFFSZ
int BuffSize
Definition XrdNet.hh:292

References BuffQ, BuffSize, XrdNetSocket::Detach(), eDest, XrdSysError::Emsg(), iofd, XrdNetSocket::LastError(), netOpts, XrdNetSocket::Open(), opts, PortType, unBind(), Windowsz, XRDNET_SERVER, XRDNET_UDPBUFFSZ, and XRDNET_UDPSOCKET.

+ Here is the call graph for this function:

◆ Bind() [2/2]

int XrdNet::Bind ( int port,
const char * contype = "tcp" )

Bind a network object to a TCP or UDP port number.

Parameters
portthe port number to bind to. Use 0 for arbitrary port.
contypeEither "tcp" for TCP networks or "udp" for UDP networks.
Returns
0 Successfully bound the port.
!0 Failure, return value is -errno describing the error.

Definition at line 164 of file XrdNet.cc.

165{
166 XrdNetSocket mySocket(eDest);
167 int opts = XRDNET_SERVER | netOpts;
168 int buffsz = Windowsz;
169
170// Close any open socket here
171//
172 unBind();
173
174// Get correct option settings
175//
176 if (*contype != 'u') PortType = SOCK_STREAM;
177 else {PortType = SOCK_DGRAM;
179 if (!buffsz) buffsz = XRDNET_UDPBUFFSZ;
180 }
181
182// Try to open and bind to this port
183//
184 if (mySocket.Open(0, bindport, opts, buffsz) < 0)
185 return -mySocket.LastError();
186
187// Success, get the socket number and return
188//
189 iofd = mySocket.Detach();
190
191// Obtain port number of generic port being used
192//
193 Portnum = (bindport ? bindport : XrdNetUtils::Port(iofd));
194
195// For udp sockets, we must allocate a buffer queue object
196//
197 if (PortType == SOCK_DGRAM)
198 {BuffSize = buffsz;
199 BuffQ = new XrdNetBufferQ(buffsz);
200 }
201 return 0;
202}
static int Port(int fd, const char **eText=0)

References BuffQ, BuffSize, XrdNetSocket::Detach(), eDest, iofd, XrdNetSocket::LastError(), netOpts, XrdNetSocket::Open(), opts, XrdNetUtils::Port(), Portnum, PortType, unBind(), Windowsz, XRDNET_SERVER, XRDNET_UDPBUFFSZ, and XRDNET_UDPSOCKET.

Referenced by XrdInet::BindSD(), and XrdCmsSupervisor::Init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Connect() [1/2]

int XrdNet::Connect ( XrdNetAddr & myAddr,
const char * dest,
int port = -1,
int opts = 0,
int timeout = -1 )

Create a TCP socket and connect it to the given host and port. This is the preferred method for making TCP based connections.

Parameters
myAddraddress object where connection information is returned.
destdestination hostname or IP address.
portthe port number to connect to. If < 0 then the dest param must contain the port number preceeded by a colon.
optsprocessing options: XRDNET_NOCLOSEX - do not allow socket to close on exec() XRDNET_NOEMSG - supress printing error messages XRDNET_NORLKUP - avoid doing reverse DNS look-up
timeoutthe maximum number of seconds to wait for the connection to complete. A negative value waits forever. Values greater than 255 seconds are set to 255.
Returns
true Connection completed, myAddr holds connection information.
false Connection failed.

Definition at line 252 of file XrdNet.cc.

254{
255 XrdNetSocket mySocket(opts & XRDNET_NOEMSG ? 0 : eDest);
256
257// Determine appropriate options but turn off UDP sockets
258//
260 if (tmo > 0) opts = (opts & ~XRDNET_TOUT) | (tmo > 255 ? 255 : tmo);
261
262// Now perform the connect and return the results if successful
263//
264 if (mySocket.Open(host, port, opts, Windowsz) < 0) return 0;
265 myAddr.Set(mySocket.Detach());
266 if (!(opts & XRDNET_NORLKUP)) myAddr.Name();
267 return 1;
268}
#define XRDNET_NORLKUP
Definition XrdNetOpts.hh:87
const char * Name(const char *eName=0, const char **eText=0)
const char * Set(const char *hSpec, int pNum=PortInSpec)
int Open(const char *path, int port=-1, int flags=0, int sockbuffsz=0)

References XrdNetSocket::Detach(), eDest, XrdNetAddrInfo::Name(), netOpts, XrdNetSocket::Open(), opts, XrdNetAddr::Set(), Windowsz, XRDNET_NOEMSG, XRDNET_NORLKUP, and XRDNET_UDPSOCKET.

Referenced by XrdInet::Connect(), Relay(), and Relay().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Connect() [2/2]

int XrdNet::Connect ( XrdNetPeer & myPeer,
const char * dest,
int port = -1,
int opts = 0,
int timeout = -1 )

Create a TCP or UDP socket and connect it to the given host and port. The previous method is preferred for creating TCP sockets.

Parameters
myPeerpeer object where connection information is returned.
destdestination hostname or IP address.
portthe port number to connect to. If < 0 then the dest param must contain the port number preceeded by a colon.
optsprocessing options: XRDNET_NOCLOSEX - do not allow socket to close on exec() XRDNET_NODNTRIM - do not trim domain name in myPeer. XRDNET_NOEMSG - supress printing error messages XRDNET_NORLKUP - avoid doing reverse DNS look-up XRDNET_UDPSOCKET - create a UDP socket (o/w use TCP).
timeoutthe maximum number of seconds to wait for the connection to complete. A negative value waits forever. Values greater than 255 seconds are set to 255.
Returns
true Connection completed, myPeer holds connection information.
false Connection failed.

Definition at line 272 of file XrdNet.cc.

274{
275 XrdNetSocket mySocket(opts & XRDNET_NOEMSG ? 0 : eDest);
276 const struct sockaddr *sap;
277 int buffsz = Windowsz;
278
279// Determine appropriate options
280//
281 opts |= netOpts;
282 if ((opts & XRDNET_UDPSOCKET) && !buffsz) buffsz = XRDNET_UDPBUFFSZ;
283 if (tmo > 0) opts = (opts & ~XRDNET_TOUT) | (tmo > 255 ? 255 : tmo);
284
285// Now perform the connect and return the peer structure if successful
286//
287 if (mySocket.Open(host, port, opts, buffsz) < 0) return 0;
288 if (myPeer.InetName) free(myPeer.InetName);
289 if ((opts & XRDNET_UDPSOCKET) || !host)
290 {myPeer.InetName = strdup("n/a");
291 memset((void *)&myPeer.Inet, 0, sizeof(myPeer.Inet));
292 } else {
293 const char *pn = mySocket.Peername(&sap);
294 if (pn) {memcpy((void *)&myPeer.Inet, sap, sizeof(myPeer.Inet));
295 myPeer.InetName = strdup(pn);
296 if (Domain && !(opts & XRDNET_NODNTRIM)) Trim(myPeer.InetName);
297 } else {
298 memset((void *)&myPeer.Inet, 0, sizeof(myPeer.Inet));
299 myPeer.InetName = strdup("unknown");
300 }
301 }
302 myPeer.fd = mySocket.Detach();
303 return 1;
304}
XrdNetSockAddr Inet
Definition XrdNetPeer.hh:43
const char * Peername(const struct sockaddr **InetAddr=0, int *InetSize=0)

References XrdNetSocket::Detach(), Domain, eDest, XrdNetPeer::fd, XrdNetPeer::Inet, XrdNetPeer::InetName, netOpts, XrdNetSocket::Open(), opts, XrdNetSocket::Peername(), Trim(), Windowsz, XRDNET_NODNTRIM, XRDNET_NOEMSG, XRDNET_UDPBUFFSZ, and XRDNET_UDPSOCKET.

+ Here is the call graph for this function:

◆ Port()

int XrdNet::Port ( )
inline

Get the port number, if any, bound to this network.

Returns
>0 The bound port number.
<=0 The network is not bound to a port.

Definition at line 191 of file XrdNet.hh.

191{return Portnum;}

References Portnum.

Referenced by XrdgetProtocol().

+ Here is the caller graph for this function:

◆ Relay() [1/2]

int XrdNet::Relay ( const char * dest)

Definition at line 317 of file XrdNet.cc.

318{
319 XrdNetPeer myPeer;
320
321 return (Connect(myPeer, dest, -1, XRDNET_UDPSOCKET | XRDNET_SENDONLY)
322 ? myPeer.fd : -1);
323}
#define XRDNET_SENDONLY
Definition XrdNetOpts.hh:43
int Connect(XrdNetAddr &myAddr, const char *dest, int port=-1, int opts=0, int timeout=-1)
Definition XrdNet.cc:252

References Connect(), XrdNetPeer::fd, XRDNET_SENDONLY, and XRDNET_UDPSOCKET.

+ Here is the call graph for this function:

◆ Relay() [2/2]

int XrdNet::Relay ( XrdNetPeer & Peer,
const char * dest,
int opts = 0 )

Definition at line 310 of file XrdNet.cc.

311{
312 return Connect(Peer, dest, -1, opts | XRDNET_UDPSOCKET);
313}

References Connect(), opts, and XRDNET_UDPSOCKET.

Referenced by XrdNetMsg::XrdNetMsg().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Secure()

void XrdNet::Secure ( XrdNetSecurity * secp)
virtual

Add a NetSecurity object to the existing accept() security constraints.

Parameters
secpPointer to the network security object. This object must not be deleted nor directly used after the call as this object assumes its ownership and may delete it at any time.

Reimplemented in XrdInet.

Definition at line 329 of file XrdNet.cc.

330{
331
332// If we don't have a Police object then use the one supplied. Otherwise
333// merge the supplied object into the existing object.
334//
335 if (Police) Police->Merge(secp);
336 else Police = secp;
337}
void Merge(XrdNetSecurity *srcp)

References XrdNetSecurity::Merge(), and Police.

+ Here is the call graph for this function:

◆ setDefaults()

void XrdNet::setDefaults ( int options,
int buffsz = 0 )
inline

Set network defaults.

Parameters
optionsThe options to be added to Accept(), Bind() and Connect() calls. These options cannot be turned off, so be careful.
buffszThe UDP buffer size (the initial default is 32K) or the TCP window size (initial default is OS dependent).

Definition at line 223 of file XrdNet.hh.

224 {netOpts = options; Windowsz = buffsz;}

References netOpts, and Windowsz.

◆ setDomain()

void XrdNet::setDomain ( const char * dname)
inline

Set network domain name.

Parameters
dnameThe domain name which indicates to Trim() what part of the host name is so common that it can be trimmed.

Definition at line 233 of file XrdNet.hh.

234 {if (Domain) free(Domain);
235 Domain = strdup(dname);
236 Domlen = strlen(dname);
237 }

References Domain, and Domlen.

Referenced by XrdCmsSupervisor::Init().

+ Here is the caller graph for this function:

◆ Trim()

void XrdNet::Trim ( char * hname)

Trims off the domain name in a host name.

Parameters
hnameThe host name to be trimmed (it is modified).

Definition at line 343 of file XrdNet.cc.

344{
345 int k = strlen(hname);
346 char *hnp;
347
348 if (Domlen && k > Domlen)
349 {hnp = hname + (k - Domlen);
350 if (!strcmp(Domain, hnp)) *hnp = '\0';
351 }
352}

References Domain, and Domlen.

Referenced by Accept(), XrdLinkCtl::Alloc(), and Connect().

+ Here is the caller graph for this function:

◆ unBind()

void XrdNet::unBind ( )

Unbind the network from any bound resouces.

Definition at line 358 of file XrdNet.cc.

359{
360 if (iofd >= 0) {close(iofd); iofd=-1; Portnum=0;}
361 if (BuffQ) {delete BuffQ; BuffQ = 0;}
362}
#define close(a)
Definition XrdPosix.hh:48

References BuffQ, close, iofd, and Portnum.

Referenced by ~XrdNet(), Bind(), and Bind().

+ Here is the caller graph for this function:

◆ WSize()

int XrdNet::WSize ( )

Get the current TCP RCVBUF window size.

Returns
>0 The current window size.
<=0 Either the network is not bound to a port or an error has occurred. Window size is unavailable.

Definition at line 368 of file XrdNet.cc.

369{
370 int wsz;
371
372 if (iofd >= 0 && !XrdNetSocket::getWindow(iofd, wsz, eDest)) return wsz;
373 return 0;
374}
static int getWindow(int fd, int &Windowsz, XrdSysError *eDest=0)

References eDest, XrdNetSocket::getWindow(), and iofd.

+ Here is the call graph for this function:

Member Data Documentation

◆ BuffQ

XrdNetBufferQ* XrdNet::BuffQ
protected

Definition at line 293 of file XrdNet.hh.

Referenced by XrdNet(), Bind(), Bind(), XrdInet::BindSD(), and unBind().

◆ BuffSize

int XrdNet::BuffSize
protected

Definition at line 292 of file XrdNet.hh.

Referenced by Bind(), Bind(), and XrdInet::BindSD().

◆ Domain

char* XrdNet::Domain
protected

Definition at line 285 of file XrdNet.hh.

Referenced by XrdNet(), ~XrdNet(), Accept(), Connect(), setDomain(), and Trim().

◆ Domlen

int XrdNet::Domlen
protected

Definition at line 286 of file XrdNet.hh.

Referenced by XrdNet(), setDomain(), and Trim().

◆ eDest

XrdSysError* XrdNet::eDest
protected

◆ iofd

int XrdNet::iofd
protected

Definition at line 287 of file XrdNet.hh.

Referenced by XrdNet(), Accept(), Accept(), Bind(), Bind(), XrdInet::BindSD(), unBind(), and WSize().

◆ netOpts

int XrdNet::netOpts
protected

◆ Police

XrdNetSecurity* XrdNet::Police
protected

Definition at line 284 of file XrdNet.hh.

Referenced by XrdNet(), and Secure().

◆ Portnum

int XrdNet::Portnum
protected

Definition at line 288 of file XrdNet.hh.

Referenced by XrdNet(), XrdInet::Accept(), Bind(), XrdInet::BindSD(), Port(), and unBind().

◆ PortType

int XrdNet::PortType
protected

Definition at line 289 of file XrdNet.hh.

Referenced by XrdNet(), Accept(), Accept(), Bind(), Bind(), and XrdInet::BindSD().

◆ Windowsz

int XrdNet::Windowsz
protected

Definition at line 290 of file XrdNet.hh.

Referenced by XrdNet(), Bind(), Bind(), XrdInet::BindSD(), Connect(), Connect(), and setDefaults().


The documentation for this class was generated from the following files: