Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:40

0001 #ifndef __XRDNET_H__
0002 #define __XRDNET_H__
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                             X r d N e t . h h                              */
0006 /*                                                                            */
0007 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
0008 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
0009 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
0010 /*                                                                            */
0011 /* This file is part of the XRootD software suite.                            */
0012 /*                                                                            */
0013 /* XRootD is free software: you can redistribute it and/or modify it under    */
0014 /* the terms of the GNU Lesser General Public License as published by the     */
0015 /* Free Software Foundation, either version 3 of the License, or (at your     */
0016 /* option) any later version.                                                 */
0017 /*                                                                            */
0018 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
0019 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
0020 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
0021 /* License for more details.                                                  */
0022 /*                                                                            */
0023 /* You should have received a copy of the GNU Lesser General Public License   */
0024 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
0025 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
0026 /*                                                                            */
0027 /* The copyright holder's institutional names and contributor's names may not */
0028 /* be used to endorse or promote products derived from this software without  */
0029 /* specific prior written permission of the institution or contributor.       */
0030 /******************************************************************************/
0031 
0032 #include <cstdlib>
0033 #include <cstring>
0034 #ifndef WIN32
0035 #include <strings.h>
0036 #include <unistd.h>
0037 #include <netinet/in.h>
0038 #include <sys/socket.h>
0039 #else
0040 #include <Winsock2.h>
0041 #endif
0042 
0043 #include "XrdNet/XrdNetOpts.hh"
0044 
0045 class XrdNetAddr;
0046 class XrdNetBufferQ;
0047 class XrdNetPeer;
0048 class XrdNetSecurity;
0049 class XrdSysError;
0050 
0051 class XrdNet
0052 {
0053 public:
0054 
0055 //------------------------------------------------------------------------------
0056 //! Accept incoming TCP connection. This is the preferred method for TCP.
0057 //!
0058 //! @param  myAddr   the address object to contain connection information.
0059 //! @param  opts     processing options:
0060 //!                  XRDNET_DELAY     - do not set nodelay on socket.
0061 //!                  XRDNET_KEEPALIVE - use TCP keep alive on socket.
0062 //!                  XRDNET_NOCLOSEX  - do not allow socket to close on exec()
0063 //!                  XRDNET_NOEMSG    - supress printing error messages
0064 //!                  XRDNET_NOLINGER  - Do not linger when closing socket.
0065 //! @param  timeout  maximum seconds to wait for a conection. When negative,
0066 //!                  the default, no time limit applies.
0067 //! @return !0       Successful connection occurred, myAddr holds information.
0068 //! @return =0       Failure, a timeout or permanent error occurred.
0069 //------------------------------------------------------------------------------
0070 
0071 int             Accept(XrdNetAddr &myAddr,
0072                        int opts=0,
0073                        int timeout=-1);
0074 
0075 //------------------------------------------------------------------------------
0076 //! Accept incoming TCP or UDP connection. This method should only be used for
0077 //! UDP-style networks. The previous method is preferred for TCP-style networks.
0078 //!
0079 //! @param  myPeer   the peer object to contain connection information.
0080 //! @param  opts     processing options:
0081 //!                  XRDNET_DELAY     - TCP: do not set nodelay on socket.
0082 //!                  XRDNET_KEEPALIVE - TCP: use TCP keep alive on socket.
0083 //!                  XRDNET_NEWFD     - UDP: obtain new file descriptor
0084 //!                  XRDNET_NOCLOSEX  - ALL: keep socket across exec() calls
0085 //!                  XRDNET_NODNTRIM  - ALL: don't trim domain name in myPeer
0086 //!                  XRDNET_NOEMSG    - ALL: supress printing error messages
0087 //!                  XRDNET_NORLKUP   - ALL: avoid doing reverse DNS look-up
0088 //!                  XRDNET_USETLS    - ALL: enable TLS upon connection
0089 //! @param  timeout  maximum seconds to wait for a conection. When negative,
0090 //!                  the default, no time limit applies.
0091 //! @return !0       Successful connection occurred, myPeer holds information.
0092 //! @return =0       Failure, a timeout or permanent error occurred.
0093 //------------------------------------------------------------------------------
0094 
0095 int             Accept(XrdNetPeer &myPeer,
0096                        int opts=0,
0097                        int timeout=-1);
0098 
0099 //------------------------------------------------------------------------------
0100 //! Bind a network object to a TCP or UDP port number.
0101 //!
0102 //! @param  port     the port number to bind to. Use 0 for arbitrary port.
0103 //! @param  contype  Either "tcp" for TCP networks or "udp" for UDP networks.
0104 //!
0105 //! @return  0       Successfully bound the port.
0106 //! @return !0       Failure, return value is -errno describing the error.
0107 //------------------------------------------------------------------------------
0108 
0109 int             Bind(      int   port,             // Port number
0110                      const char *contype="tcp"     // "tcp" or "udp"
0111                     );
0112 
0113 //------------------------------------------------------------------------------
0114 //! Bind a network object to a Unix named socket.
0115 //!
0116 //! @param  path     the file system path to a named socket to bind with.
0117 //! @param  contype  Either "stream" to use TCP-tyle streaming protocol or
0118 //!                  "datagram" to use UDP-style messaging.
0119 //!
0120 //! @return  0       Successfully bound the port.
0121 //! @return !0       Failure, return value is -errno describing the error.
0122 //------------------------------------------------------------------------------
0123 
0124 int             Bind(      char *path,             // Unix path < |109|
0125                      const char *contype="stream"  // stream | datagram
0126                     );
0127 
0128 //------------------------------------------------------------------------------
0129 //! Create a TCP socket and connect it to the given host and port. This is the
0130 //! preferred method for making TCP based connections.
0131 //!
0132 //! @param  myAddr   address object where connection information is returned.
0133 //! @param  dest     destination hostname or IP address.
0134 //! @param  port     the port number to connect to. If < 0 then the dest param
0135 //!                  must contain the port number preceeded by a colon.
0136 //! @param  opts     processing options:
0137 //!                  XRDNET_NOCLOSEX  - do not allow socket to close on exec()
0138 //!                  XRDNET_NOEMSG    - supress printing error messages
0139 //!                  XRDNET_NORLKUP   - avoid doing reverse DNS look-up
0140 //! @param  timeout  the maximum number of seconds to wait for the connection to
0141 //!                  complete. A negative value waits forever. Values greater
0142 //!                  than 255 seconds are set to 255.
0143 //!
0144 //! @return true     Connection completed, myAddr holds connection information.
0145 //! @return false    Connection failed.
0146 //------------------------------------------------------------------------------
0147 
0148 int             Connect(XrdNetAddr &myAddr,
0149                         const char *dest,  // Destination host or ip address
0150                         int   port=-1,     // Port number
0151                         int   opts=0,      // Options
0152                         int   timeout=-1   // Second timeout
0153                        );
0154 
0155 //------------------------------------------------------------------------------
0156 //! Create a TCP or UDP socket and connect it to the given host and port. The
0157 //! previous method is preferred for creating TCP sockets.
0158 //!
0159 //! @param  myPeer   peer object where connection information is returned.
0160 //! @param  dest     destination hostname or IP address.
0161 //! @param  port     the port number to connect to. If < 0 then the dest param
0162 //!                  must contain the port number preceeded by a colon.
0163 //! @param  opts     processing options:
0164 //!                  XRDNET_NOCLOSEX  - do not allow socket to close on exec()
0165 //!                  XRDNET_NODNTRIM  - do not trim domain name in myPeer.
0166 //!                  XRDNET_NOEMSG    - supress printing error messages
0167 //!                  XRDNET_NORLKUP   - avoid doing reverse DNS look-up
0168 //!                  XRDNET_UDPSOCKET - create a UDP socket (o/w use TCP).
0169 //! @param  timeout  the maximum number of seconds to wait for the connection to
0170 //!                  complete. A negative value waits forever. Values greater
0171 //!                  than 255 seconds are set to 255.
0172 //!
0173 //! @return true     Connection completed, myPeer holds connection information.
0174 //! @return false    Connection failed.
0175 //------------------------------------------------------------------------------
0176 
0177 int             Connect(XrdNetPeer &myPeer,
0178                         const char *dest,  // Destination host or ip address
0179                         int   port=-1,     // Port number
0180                         int   opts=0,      // Options
0181                         int   timeout=-1   // Second timeout
0182                        );
0183 
0184 //------------------------------------------------------------------------------
0185 //! Get the port number, if any, bound to this network.
0186 //!
0187 //! @return  >0      The bound port number.
0188 //! @return <=0      The network is not bound to a port.
0189 //------------------------------------------------------------------------------
0190 
0191 int             Port() {return Portnum;}
0192 
0193 // Relay() creates a UDP socket and optionally decomposes a destination
0194 //         of the form host:port. Upon success it fills in the Peer object
0195 //         and return true (1). Upon failure, it returns false (0).
0196 //
0197 int             Relay(XrdNetPeer &Peer,   // Peer object to be initialized
0198                       const char *dest,   // Optional destination
0199                       int         opts=0  // Optional options as above
0200                      );
0201 
0202 int             Relay(const char *dest);  // Optional destination
0203 
0204 //------------------------------------------------------------------------------
0205 //! Add a NetSecurity object to the existing accept() security constraints.
0206 //!
0207 //! @param  secp     Pointer to the network security object. This object must
0208 //!                  not be deleted nor directly used after the call as this
0209 //!                  object assumes its ownership and may delete it at any time.
0210 //------------------------------------------------------------------------------
0211 
0212 virtual void    Secure(XrdNetSecurity *secp);
0213 
0214 //------------------------------------------------------------------------------
0215 //! Set network defaults.
0216 //!
0217 //! @param  options  The options to be added to Accept(), Bind() and Connect()
0218 //!                  calls. These options cannot be turned off, so be careful.
0219 //! @param  buffsz   The UDP buffer size (the initial default is 32K) or the TCP
0220 //!                  window size (initial default is OS dependent).
0221 //------------------------------------------------------------------------------
0222 
0223 void            setDefaults(int options, int buffsz=0)
0224                            {netOpts = options; Windowsz = buffsz;}
0225 
0226 //------------------------------------------------------------------------------
0227 //! Set network domain name.
0228 //!
0229 //! @param  dname    The domain name which indicates to Trim() what part of the
0230 //!                  host name is so common that it can be trimmed.
0231 //------------------------------------------------------------------------------
0232 
0233 void            setDomain(const char *dname)
0234                          {if (Domain) free(Domain);
0235                           Domain = strdup(dname);
0236                           Domlen = strlen(dname);
0237                          }
0238 
0239 //------------------------------------------------------------------------------
0240 //! Trims off the domain name in a host name.
0241 //!
0242 //! @param  hname    The host name to be trimmed (it is modified).
0243 //------------------------------------------------------------------------------
0244 
0245 void            Trim(char *hname);
0246 
0247 //------------------------------------------------------------------------------
0248 //! Unbind the network from any bound resouces.
0249 //------------------------------------------------------------------------------
0250 
0251 void            unBind();
0252 
0253 //------------------------------------------------------------------------------
0254 //! Get the current TCP RCVBUF window size.
0255 //!
0256 //! @return   >0     The current window size.
0257 //! @return  <=0     Either the network is not bound to a port or an error has
0258 //!                  occurred. Window size is unavailable.
0259 //------------------------------------------------------------------------------
0260 
0261 int            WSize();
0262 
0263 //------------------------------------------------------------------------------
0264 //! Constructor
0265 //!
0266 //! @param  erp      The error object for printing error messages. It must be
0267 //!                  supplied.
0268 //! @param  secp     The initial NetSecurity object. This secp object must not
0269 //!                  be deleted nor directly used after the call as this
0270 //!                  object assumes its ownership and may delete it at any time.
0271 //------------------------------------------------------------------------------
0272 
0273                 XrdNet(XrdSysError *erp, XrdNetSecurity *secp=0);
0274 
0275 //------------------------------------------------------------------------------
0276 //! Destructor
0277 //------------------------------------------------------------------------------
0278 
0279 virtual        ~XrdNet();
0280 
0281 protected:
0282 
0283 XrdSysError       *eDest;
0284 XrdNetSecurity    *Police;
0285 char              *Domain;
0286 int                Domlen;
0287 int                iofd;
0288 int                Portnum;
0289 int                PortType;
0290 int                Windowsz;
0291 int                netOpts;
0292 int                BuffSize;
0293 XrdNetBufferQ     *BuffQ;
0294 
0295 private:
0296 
0297 int                do_Accept_TCP(XrdNetAddr &myAddr, int opts);
0298 int                do_Accept_TCP(XrdNetPeer &myPeer, int opts);
0299 int                do_Accept_UDP(XrdNetPeer &myPeer, int opts);
0300 };
0301 #endif