xrootd
XrdNetLink.hh
Go to the documentation of this file.
00001 #ifndef __NET_LINK_H__
00002 #define __NET_LINK_H__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                         X r d N e t L i n k . h h                          */
00006 /*                                                                            */
00007 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
00008 /*                            All Rights Reserved                             */
00009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00010 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00011 /*                                                                            */
00012 /* This file is part of the XRootD software suite.                            */
00013 /*                                                                            */
00014 /* XRootD is free software: you can redistribute it and/or modify it under    */
00015 /* the terms of the GNU Lesser General Public License as published by the     */
00016 /* Free Software Foundation, either version 3 of the License, or (at your     */
00017 /* option) any later version.                                                 */
00018 /*                                                                            */
00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00022 /* License for more details.                                                  */
00023 /*                                                                            */
00024 /* You should have received a copy of the GNU Lesser General Public License   */
00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00027 /*                                                                            */
00028 /* The copyright holder's institutional names and contributor's names may not */
00029 /* be used to endorse or promote products derived from this software without  */
00030 /* specific prior written permission of the institution or contributor.       */
00031 /******************************************************************************/
00032 
00033 #ifndef WIN32
00034 #include <netinet/in.h>
00035 #include <sys/socket.h>
00036 #include <sys/types.h>
00037 #include <sys/uio.h>
00038 #include <fcntl.h>
00039 #else
00040 #include <Winsock2.h>
00041 #endif
00042 
00043 #include "XrdNet/XrdNetBuffer.hh"
00044 #include "XrdSys/XrdSysDNS.hh"
00045 #include "XrdSys/XrdSysPthread.hh"
00046 
00047 // Options for SetOpts and Alloc()
00048 //
00049 #define XRDNETLINK_NOBLOCK  0x0001
00050 #define XRDNETLINK_NOCLOSE  0x0002
00051 #define XRDNETLINK_NOSTREAM 0x0004
00052 
00053 // The XrdNetLink class defines the i/o operations on a network link.
00054 //
00055 class XrdNet;
00056 class XrdNetPeer;
00057 class XrdSysError;
00058 class XrdOucStream;
00059 class XrdOucTokenizer;
00060 
00061 class XrdNetLink
00062 {
00063 public:
00064 
00065 XrdOucQSItem<XrdNetLink> LinkLink;
00066 
00067 static XrdNetLink *Alloc(XrdSysError   *erp, XrdNet *Net, XrdNetPeer &Peer,
00068                          XrdNetBufferQ *bq,  int opts=0);
00069 
00070 // Closes() closes the link. Specify defer=1 to postpone deallocating
00071 //          attached objects until the this object is destroyed. You should
00072 //          use defered close for cross-thread unsynchronized closes.
00073 //
00074 int           Close(int defer=0);
00075 
00076 // FDnum() returns the associated file descriptor
00077 //
00078 int           FDnum() {return FD;}
00079 
00080 // The following implement text-oriented read routines. The correspond to
00081 // those implemented by the XrdOucStream object which is the one used.
00082 //
00083 char         *GetLine();
00084 
00085 char         *GetToken(char **rest);
00086 char         *GetToken(void);
00087 void          RetToken(void);
00088 
00089 // isConnected() returns true if this object is connected to an XrdOucStream
00090 //               object and false otherwise.
00091 //
00092 int           isConnected(void) {return (Stream != 0) && (FD >= 0);}
00093 
00094 // LastError() returns the error number associated with the last error
00095 //
00096 int           LastError();
00097 
00098 // Addr() returns the IPV4 address of the endpoint
00099 //
00100 unsigned int Addr() {return XrdSysDNS::IPAddr(&InetAddr);}
00101 
00102 // Moniker() returns the short form of the host name at the endpoint
00103 //
00104 const char  *Moniker() {return Sname;}
00105 
00106 // Name() returns the full host name of the endpoint
00107 //
00108 const char  *Name() {return Lname;}
00109 
00110 // Moniker() returns the short form of the host name at the endpoint
00111 //
00112 const char  *Nick() {return Sname;}
00113 
00114 // OK2Recv() returns true if data can be received within tmo, false otherwise.
00115 //
00116 int           OK2Recv(int mills);
00117 
00118 // Recycle() makes this object available for reuse.
00119 //
00120 void          Recycle();
00121 
00122 // Send() set of methods that accept a char buffer are text oriented send
00123 //        routines. They all add a new-line (\n) character to end the buffer
00124 //        if it does not exist already.
00125 //
00126 int           Send(const char *buff,     // -> Data to send
00127                          int   blen=0,   // Length. If 0, it's compued via strlen()
00128                          int   tmo=-1);  // Millisecond timeout (default is none)
00129 
00130 int           Send(const char *dest,     // -> Hostname to send UDP datagram
00131                    const char *buff,     // Remaining parms as above
00132                          int   blen=0,
00133                           int   tmo=-1);
00134 
00135 int           Send(const struct iovec iov[], // writev() style plist
00136                    int iovcnt,               // Number of elements om iov[]
00137                    int tmo = -1);            // Optional timeout
00138 
00139 int           Send(const char   *dest,       // Hostname to send UDP datagram
00140                    const struct iovec iov[], // Remaining parms as above
00141                    int   iovcnt,
00142                    int   tmo=-1);
00143 
00144 // Send() set of methods that accept a void buffer are byte oriented send
00145 //        routines. These do not inspect the data at all.
00146 //
00147 int           Send(const void *buff,     // -> Data to send
00148                          int   blen=0,   // Length. If 0, it's compued via strlen()
00149                          int   tmo=-1);  // Millisecond timeout (default is none)
00150 
00151 // Recv() receives up to blen bytes. It may receive less than that if
00152 //        additional bytes are not immediately available to receive.
00153 //        it returns the number of bytes read or -1 if an error occurs.
00154 //
00155 int           Recv(char *buff, int blen);
00156 
00157 // Set() sets the maximum number of XrdNetLink objects that may be kept
00158 //       for future re-use.
00159 //
00160 void          Set(int maxl);
00161 
00162 // SetOpts() is used to set socket options, as defined above.
00163 //
00164 void          SetOpts(int opts);
00165 
00166 // Instantiate this object with the pointer to an error object for message
00167 // routing. Additionally, a pointer to a UDP buffer may be provided. This
00168 // buffer must contain a text datagram suitable for tokenization.
00169 //
00170               XrdNetLink(XrdSysError *erp, XrdNetBufferQ *bq) : LinkLink(this)
00171                           {FD = -1; Lname = Sname = 0; recvbuff = sendbuff = 0;
00172                            BuffQ = bq; Stream = 0; Bucket = 0; eDest = erp;
00173                           }
00174              ~XrdNetLink() {Close();}
00175 
00176 private:
00177 
00178 int OK2Send(int timeout=0, const char *dest=0);
00179 int retErr(int ecode, const char *dest=0);
00180 
00181 XrdSysMutex         rdMutex;
00182 XrdSysMutex         wrMutex;
00183 XrdNetBufferQ      *BuffQ;
00184 int                 FD;
00185 int                 noclose;
00186 int                 isReset;
00187 struct sockaddr     InetAddr;
00188 char               *Lname;     // Long  hostname
00189 char               *Sname;     // Short hostname (may be the same as Lname)
00190 XrdNetBuffer       *recvbuff;  // udp receive buffer
00191 XrdNetBuffer       *sendbuff;  // udp send    buffer
00192 XrdOucStream       *Stream;    // tcp tokenizer
00193 XrdOucTokenizer    *Bucket;    // udp tokenizer
00194 XrdSysError        *eDest;
00195 
00196 static XrdSysMutex             LinkList;
00197 static XrdOucStack<XrdNetLink> LinkStack;
00198 static int                     size;
00199 static int                     maxlink;
00200 static int                     numlink;
00201 static int                     devNull;
00202 };
00203 #endif