xrootd
XrdNet.hh
Go to the documentation of this file.
1 #ifndef __XRDNET_H__
2 #define __XRDNET_H__
3 /******************************************************************************/
4 /* */
5 /* X r d N e t . h h */
6 /* */
7 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <stdlib.h>
33 #include <string.h>
34 #ifndef WIN32
35 #include <strings.h>
36 #include <unistd.h>
37 #include <netinet/in.h>
38 #include <sys/socket.h>
39 #else
40 #include <Winsock2.h>
41 #endif
42 
43 #include "XrdNet/XrdNetOpts.hh"
44 
45 class XrdNetBufferQ;
46 class XrdNetPeer;
47 class XrdNetSecurity;
48 class XrdSysError;
49 
50 class XrdNet
51 {
52 public:
53 
54 // Accept() processes incomming connections. When a succesful connection is
55 // made, it places the connection informatio in myPeer and returns
56 // true (1). If a timeout or permanent error occurs, it returns
57 // false (0). The opts are those defined above and timeout is
58 // specified as seconds. Use this method to associate specialized
59 // versions of XrdNetLink objects with the connection.
60 //
61 int Accept(XrdNetPeer &myPeer,
62  int opts=0,
63  int timeout=-1);
64 
65 // Bind() binds this object to a communications medium. This may be TCP or
66 // UDP network via the given port number or a Unix named socket
67 // specified by path (the second form).
68 // Bind() returns 0 upon success or -errno upon failure.
69 //
70 int Bind( int port, // Port number
71  const char *contype="tcp" // "tcp" or "udp"
72  );
73 int Bind( char *path, // Unix path < |109|
74  const char *contype="stream" // stream | datagram
75  );
76 
77 // Connect() Creates a socket and connects to the given host and port. Upon
78 // success, it fills in the peer object describing the connection.
79 // and returns true (1). Upon failure it returns zero. Opts are as
80 // above. A timeout, in seconds, may be specified. Use this method to
81 // associate specialized versions of XrdNetLink with the connection.
82 //
83 int Connect(XrdNetPeer &myPeer,
84  const char *host, // Destination host or ip address
85  int port, // Port number
86  int opts=0, // Options
87  int timeout=-1 // Second timeout
88  );
89 
90 // Relay() creates a UDP socket and optionally decomposes a destination
91 // of the form host:port. Upon success it fills in the Peer object
92 // and return true (1). Upon failure, it returns false (0).
93 //
94 int Relay(XrdNetPeer &Peer, // Peer object to be initialized
95  const char *dest, // Optional destination
96  int opts=0 // Optional options as above
97  );
98 
99 // Port() returns he port number, if any, bound to this network.
100 //
101 int Port() {return Portnum;}
102 
103 // Secure() adds the given NetSecurity object to the existing security
104 // constraints. The supplied object is ultimately deleted in the
105 // process and cannot be referenced.
106 //
107 void Secure(XrdNetSecurity *secp);
108 
109 // setDefaults() sets the default socket options, and buffer size for UDP
110 // sockets (default is 32k) or window size for TCP sockets
111 // (defaults to OS default).
112 //
113 void setDefaults(int options, int buffsz=0)
114  {netOpts = options; Windowsz = buffsz;}
115 
116 // setDomain() is used to indicate what part of the hostname is so common
117 // that it may be trimmed of for incomming hostnames. This is
118 // usually the domain in which this object resides/
119 //
120 void setDomain(const char *dname)
121  {if (Domain) free(Domain);
122  Domain = strdup(dname);
123  Domlen = strlen(dname);
124  }
125 
126 // Trim() trims off the domain name in hname (it's modified).
127 //
128 void Trim(char *hname);
129 
130 // unbind() Destroys the association between this object and whatever
131 // communications medium it was previously bound to.
132 //
133 void unBind();
134 
135 // WSzize() Returns the actual RCVBUF window size. A value of zero
136 // indicates that an error has occurred.
137 //
138 int WSize();
139 
140 // When creating this object, you must specify the error routing object.
141 // Optionally, specify the security object to screen incomming connections.
142 // (if zero, no screening is done).
143 //
144  XrdNet(XrdSysError *erp, XrdNetSecurity *secp=0);
145  ~XrdNet();
146 
147 protected:
148 
150 XrdNetSecurity *Police;
151 char *Domain;
152 int Domlen;
153 int iofd;
159 XrdNetBufferQ *BuffQ;
160 
161 private:
162 
163 int do_Accept_TCP(XrdNetPeer &myPeer, int opts);
164 int do_Accept_UDP(XrdNetPeer &myPeer, int opts);
165 };
166 #endif
int do_Accept_TCP(XrdNetPeer &myPeer, int opts)
int WSize()
void setDomain(const char *dname)
Definition: XrdNet.hh:120
void unBind()
int Portnum
Definition: XrdNet.hh:154
char * Domain
Definition: XrdNet.hh:151
void Trim(char *hname)
int netOpts
Definition: XrdNet.hh:157
int Relay(XrdNetPeer &Peer, const char *dest, int opts=0)
XrdNet(XrdSysError *erp, XrdNetSecurity *secp=0)
XrdNetBufferQ * BuffQ
Definition: XrdNet.hh:159
int do_Accept_UDP(XrdNetPeer &myPeer, int opts)
int PortType
Definition: XrdNet.hh:155
int BuffSize
Definition: XrdNet.hh:158
Definition: XrdSysError.hh:87
int Accept(XrdNetPeer &myPeer, int opts=0, int timeout=-1)
XrdSysError * eDest
Definition: XrdNet.hh:149
int Bind(int port, const char *contype="tcp")
void Secure(XrdNetSecurity *secp)
XrdNetSecurity * Police
Definition: XrdNet.hh:150
int Connect(XrdNetPeer &myPeer, const char *host, int port, int opts=0, int timeout=-1)
int iofd
Definition: XrdNet.hh:153
int Port()
Definition: XrdNet.hh:101
int Domlen
Definition: XrdNet.hh:152
void setDefaults(int options, int buffsz=0)
Definition: XrdNet.hh:113
Definition: XrdNet.hh:50
int Windowsz
Definition: XrdNet.hh:156