00001 #ifndef __XRDNET_H__ 00002 #define __XRDNET_H__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d N e t . h h */ 00006 /* */ 00007 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved. See XrdInfo.cc for complete License Terms */ 00009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC03-76-SFO0515 with the Department of Energy */ 00011 /******************************************************************************/ 00012 00013 // $Id: XrdNet.hh 22437 2008-03-04 14:35:16Z rdm $ 00014 00015 #include <stdlib.h> 00016 #include <string.h> 00017 #ifndef WIN32 00018 #include <strings.h> 00019 #include <unistd.h> 00020 #include <netinet/in.h> 00021 #include <sys/socket.h> 00022 #else 00023 #include <Winsock2.h> 00024 #endif 00025 00026 #include "XrdNet/XrdNetBuffer.hh" 00027 #include "XrdNet/XrdNetOpts.hh" 00028 00029 class XrdNetPeer; 00030 class XrdNetSecurity; 00031 class XrdSysError; 00032 00033 class XrdNet 00034 { 00035 public: 00036 00037 // Accept() processes incomming connections. When a succesful connection is 00038 // made, it places the connection informatio in myPeer and returns 00039 // true (1). If a timeout or permanent error occurs, it returns 00040 // false (0). The opts are those defined above and timeout is 00041 // specified as seconds. Use this method to associate specialized 00042 // versions of XrdNetLink objects with the connection. 00043 // 00044 int Accept(XrdNetPeer &myPeer, 00045 int opts=0, 00046 int timeout=-1); 00047 00048 // Bind() binds this object to a communications medium. This may be TCP or 00049 // UDP network via the given port number or a Unix named socket 00050 // specified by path (the second form). 00051 // Bind() returns 0 upon success or -errno upon failure. 00052 // 00053 int Bind( int port, // Port number 00054 const char *contype="tcp" // "tcp" or "udp" 00055 ); 00056 int Bind( char *path, // Unix path < |109| 00057 const char *contype="stream" // stream | datagram 00058 ); 00059 00060 // Connect() Creates a socket and connects to the given host and port. Upon 00061 // success, it fills in the peer object describing the connection. 00062 // and returns true (1). Upon failure it returns zero. Opts are as 00063 // above. A timeout, in seconds, may be specified. Use this method to 00064 // associate specialized versions of XrdNetLink with the connection. 00065 // 00066 int Connect(XrdNetPeer &myPeer, 00067 const char *host, // Destination host or ip address 00068 int port, // Port number 00069 int opts=0, // Options 00070 int timeout=-1 // Second timeout 00071 ); 00072 00073 // Relay() creates a UDP socket and optionally decomposes a destination 00074 // of the form host:port. Upon success it fills in the Peer object 00075 // and return true (1). Upon failure, it returns false (0). 00076 // 00077 int Relay(XrdNetPeer &Peer, // Peer object to be initialized 00078 const char *dest, // Optional destination 00079 int opts=0 // Optional options as above 00080 ); 00081 00082 // Port() returns he port number, if any, bound to this network. 00083 // 00084 int Port() {return Portnum;} 00085 00086 // Secure() adds the given NetSecurity object to the existing security 00087 // constraints. The supplied object is ultimately deleted in the 00088 // process and cannot be referenced. 00089 // 00090 void Secure(XrdNetSecurity *secp); 00091 00092 // setDefaults() sets the default socket options, and buffer size for UDP 00093 // sockets (default is 32k) or window size for TCP sockets 00094 // (defaults to OS default). 00095 // 00096 void setDefaults(int options, int buffsz=0) 00097 {netOpts = options; Windowsz = buffsz;} 00098 00099 // setDomain() is used to indicate what part of the hostname is so common 00100 // that it may be trimmed of for incomming hostnames. This is 00101 // usually the domain in which this object resides/ 00102 // 00103 void setDomain(const char *dname) 00104 {if (Domain) free(Domain); 00105 Domain = strdup(dname); 00106 Domlen = strlen(dname); 00107 } 00108 00109 // Trim() trims off the domain name in hname (it's modified). 00110 // 00111 void Trim(char *hname); 00112 00113 // unbind() Destroys the association between this object and whatever 00114 // communications medium it was previously bound to. 00115 // 00116 void unBind(); 00117 00118 // WSzize() Returns the actual RCVBUF window size. A value of zero 00119 // indicates that an error has occurred. 00120 // 00121 int WSize(); 00122 00123 // When creating this object, you must specify the error routing object. 00124 // Optionally, specify the security object to screen incomming connections. 00125 // (if zero, no screening is done). 00126 // 00127 XrdNet(XrdSysError *erp, XrdNetSecurity *secp=0); 00128 ~XrdNet(); 00129 00130 protected: 00131 00132 XrdSysError *eDest; 00133 XrdNetSecurity *Police; 00134 char *Domain; 00135 int Domlen; 00136 int iofd; 00137 int Portnum; 00138 int PortType; 00139 int Windowsz; 00140 int netOpts; 00141 int BuffSize; 00142 XrdNetBufferQ *BuffQ; 00143 00144 private: 00145 00146 int do_Accept_TCP(XrdNetPeer &myPeer, int opts); 00147 int do_Accept_UDP(XrdNetPeer &myPeer, int opts); 00148 }; 00149 #endif