Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef __XRD_LINK_H__
0002 #define __XRD_LINK_H__
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                            X r d L i n k . h h                             */
0006 /*                                                                            */
0007 /* (c) 2018 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 <sys/types.h>
0033 
0034 #include "XrdNet/XrdNetAddr.hh"
0035 #include "XrdOuc/XrdOucSFVec.hh"
0036 #include "XrdSys/XrdSysPthread.hh"
0037 
0038 #include "Xrd/XrdJob.hh"
0039   
0040 /******************************************************************************/
0041 /*                      C l a s s   D e f i n i t i o n                       */
0042 /******************************************************************************/
0043   
0044 class XrdLinkMatch;
0045 class XrdLinkXeq;
0046 class XrdPollInfo;
0047 class XrdProtocol;
0048 class XrdTlsPeerCerts;
0049 class XrdTlsContext;
0050 
0051 class XrdLink : public XrdJob
0052 {
0053 public:
0054 
0055 //-----------------------------------------------------------------------------
0056 //! Activate a link by attaching it to a poller object.
0057 //!
0058 //! @return True if activation succeeded and false otherwise.
0059 //-----------------------------------------------------------------------------
0060 
0061 bool            Activate();
0062 
0063 //-----------------------------------------------------------------------------
0064 //! Obtain the address information for this link.
0065 //!
0066 //! @return Pointer to the XrdAddrInfo object. The pointer is valid while the
0067 //!         end-point is connected.
0068 //-----------------------------------------------------------------------------
0069 
0070 XrdNetAddrInfo *AddrInfo();
0071 
0072 //-----------------------------------------------------------------------------
0073 //! Obtain the number of queued async requests.
0074 //!
0075 //! @return         The number of async requests queued.
0076 //-----------------------------------------------------------------------------
0077 
0078 int             Backlog();
0079 
0080 //-----------------------------------------------------------------------------
0081 //! Get a copy of the client's name as known by the link.
0082 //!
0083 //! @param  buff    Pointer to buffer to hold the name.
0084 //! @param  blen    Length of the buffer.
0085 //!
0086 //! @return !0      The length of the name in gthe buffer.
0087 //!         =0      The name could not be returned.
0088 //-----------------------------------------------------------------------------
0089 
0090 int             Client(char *buff, int blen);
0091 
0092 //-----------------------------------------------------------------------------
0093 //! Close the link.
0094 //!
0095 //! @param  defer   If true, the link is made unaccessible but the link
0096 //!                 object not the file descriptor is released.
0097 //!
0098 //! @return !0      An error occurred, the return value is the errno.
0099 //!         =0      Action successfully completed.
0100 //-----------------------------------------------------------------------------
0101 
0102 int             Close(bool defer=false);
0103 
0104 //-----------------------------------------------------------------------------
0105 //! Enable the link to field interrupts.
0106 //-----------------------------------------------------------------------------
0107 
0108 void            Enable();
0109 
0110 //-----------------------------------------------------------------------------
0111 //! Get the associated file descriptor.
0112 //!
0113 //! @return         The file descriptor number.
0114 //-----------------------------------------------------------------------------
0115 
0116 int             FDnum();
0117 
0118 //-----------------------------------------------------------------------------
0119 //! Find the next link matching certain attributes.
0120 //!
0121 //! @param  curr    Is an internal tracking value that allows repeated calls.
0122 //!                 It must be set to a value of 0 or less on the initial call
0123 //!                 and not touched therafter unless a null pointer is returned.
0124 //! @param  who     If the object use to check if the link matches the wanted
0125 //!                 criterea (typically, client name and host name). If the
0126 //!                 pointer is nil, the next link is always returned.
0127 //!
0128 //! @return !0      Pointer to the link object that matches the criterea. The
0129 //!                 link's reference counter is increased to prevent it from
0130 //!                 being reused. A subsequent call will reduce the number.
0131 //!         =0      No more links exist with the specified criterea.
0132 //-----------------------------------------------------------------------------
0133 
0134 static XrdLink *Find(int &curr, XrdLinkMatch *who=0);
0135 
0136 //-----------------------------------------------------------------------------
0137 //! Get I/O statistics.
0138 //!
0139 //! @param  inbytes  The number of bytes read.
0140 //! @param  outbytes The number of bytes written.
0141 //! @param  numstall The number of times the link was rescheduled due to
0142 //!                  unavailability.
0143 //! @param  numtardy The number of times the link was delayed due to
0144 //!                  unavailability.
0145 //!
0146 //! @return          The link's reference count. The parameters will hold the
0147 //!                  indicated statistic.
0148 //-----------------------------------------------------------------------------
0149 
0150        int      getIOStats(long long &inbytes, long long &outbytes,
0151                                 int  &numstall,     int  &numtardy);
0152 
0153 //-----------------------------------------------------------------------------
0154 //! Find the next client name matching certain attributes.
0155 //!
0156 //! @param  curr    Is an internal tracking value that allows repeated calls.
0157 //!                 It must be set to a value of 0 or less on the initial call
0158 //!                 and not touched therafter unless zero is returned.
0159 //! @param  bname   Pointer to a buffer where the name is to be returned.
0160 //! @param  blen    The length of the buffer.
0161 //! @param  who     If the object use to check if the link matches the wanted
0162 //!                 criterea (typically, client name and host name). If the
0163 //!                 pointer is nil, a match always occurs.
0164 //!
0165 //! @return !0      The length of the name placed in the buffer.
0166 //!         =0      No more links exist with the specified criterea.
0167 //-----------------------------------------------------------------------------
0168 
0169 static int      getName(int &curr, char *bname, int blen, XrdLinkMatch *who=0);
0170 
0171 //-----------------------------------------------------------------------------
0172 //! Get the x509 certificate information for this TLS enabled link.
0173 //!
0174 //! @return A pointer to the XrdTlsCerts object holding verified certificates
0175 //!         if such certificates exist. Otherwise a nil pointer is returned.
0176 //!
0177 //! @note Used by various protocols, so XrdTlsPeerCerts is a private header.
0178 //-----------------------------------------------------------------------------
0179 
0180 XrdTlsPeerCerts *getPeerCerts();
0181 
0182 //-----------------------------------------------------------------------------
0183 //! Obtain current protocol object pointer.
0184 //-----------------------------------------------------------------------------
0185 
0186 XrdProtocol    *getProtocol();
0187 
0188 //-----------------------------------------------------------------------------
0189 //! Lock or unlock the mutex used for control operations.
0190 //!
0191 //! @param  lk      When true, a lock is obtained. Otherwise it is released.
0192 //!                 The caller is responsible for consistency.
0193 //-----------------------------------------------------------------------------
0194 
0195 void            Hold(bool lk);
0196 
0197 //-----------------------------------------------------------------------------
0198 //! Get the fully qualified name of the endpoint.
0199 //!
0200 //! @return Pointer to fully qualified host name. The contents are valid
0201 //!         while the endpoint is connected.
0202 //-----------------------------------------------------------------------------
0203 
0204 const char     *Host() const {return (const char *)HostName;}
0205 
0206 //-----------------------------------------------------------------------------
0207 //! Pointer to the client's link identity.
0208 //-----------------------------------------------------------------------------
0209 
0210 char           *ID;      // This is referenced a lot (should have been const).
0211 
0212 //-----------------------------------------------------------------------------
0213 //! Obtain the link's instance number.
0214 //!
0215 //! @return The link's instance number.
0216 //-----------------------------------------------------------------------------
0217 
0218 unsigned int    Inst() const {return Instance;}
0219 
0220 //-----------------------------------------------------------------------------
0221 //! Indicate whether or not the link has an outstanding error.
0222 //!
0223 //! @return True    the link has an outstanding error.
0224 //!                 the link has no outstanding error.
0225 //-----------------------------------------------------------------------------
0226 
0227 bool            isFlawed() const;
0228 
0229 //-----------------------------------------------------------------------------
0230 //! Indicate whether or not this link is of a particular instance.
0231 //! only be used for display and not for security purposes.
0232 //!
0233 //! @param  inst    the expected instance number.
0234 //!
0235 //! @return true    the link matches the instance number.
0236 //!         false   the link differs the instance number.
0237 //-----------------------------------------------------------------------------
0238 
0239 bool            isInstance(unsigned int inst) const;
0240 
0241 //-----------------------------------------------------------------------------
0242 //! Obtain the domain trimmed name of the end-point. The returned value should
0243 //! only be used for display and not for security purposes.
0244 //!
0245 //! @return Pointer to the name that remains valid during the link's lifetime.
0246 //-----------------------------------------------------------------------------
0247 
0248 const char     *Name() const;
0249 
0250 //-----------------------------------------------------------------------------
0251 //! Obtain the network address object for this link. The returned value is
0252 //! valid as long as the end-point is connected. Otherwise, it may change.
0253 //!
0254 //! @return Pointer to the object and remains valid during the link's lifetime.
0255 //-----------------------------------------------------------------------------
0256 const
0257 XrdNetAddr     *NetAddr() const;
0258 
0259 //-----------------------------------------------------------------------------
0260 //! Issue a socket peek() and return result (do not use for TLS connections).
0261 //!
0262 //! @param  buff    pointer to buffer to hold data.
0263 //! @param  blen    length of buffer.
0264 //! @param  timeout milliseconds to wait for data. A negative value waits
0265 //!                 forever.
0266 //!
0267 //! @return >=0     buffer holds data equal to the returned value.
0268 //!         < 0     an error or timeout occurred.
0269 //-----------------------------------------------------------------------------
0270 
0271 int             Peek(char *buff, int blen, int timeout=-1);
0272 
0273 //-----------------------------------------------------------------------------
0274 //! Read data from a link. Note that this call blocks until some data is
0275 //! available. Use Recv() with a timeout to avoid blocking.
0276 //!
0277 //! @param  buff    pointer to buffer to hold data.
0278 //! @param  blen    length of buffer (implies the maximum bytes wanted).
0279 //!
0280 //! @return >=0     buffer holds data equal to the returned value.
0281 //!         < 0     an error occurred.
0282 //-----------------------------------------------------------------------------
0283 
0284 int             Recv(char *buff, int blen);
0285 
0286 //-----------------------------------------------------------------------------
0287 //! Read data from a link. Note that this call either reads all the data wanted
0288 //! or no data if the passed timeout occurs before any data is present.
0289 //!
0290 //! @param  buff    pointer to buffer to hold data.
0291 //! @param  blen    length of buffer (implies the actual bytes wanted).
0292 //! @param  timeout milliseconds to wait for data. A negative value waits
0293 //!                 forever.
0294 //!
0295 //! @return >=0     buffer holds data equal to the returned value.
0296 //!         < 0     an error occurred. Note that a special error -ENOMSG
0297 //!                 is returned if poll() indicated data was present but
0298 //!                 no bytes were actually read.
0299 //-----------------------------------------------------------------------------
0300 
0301 int             Recv(char *buff, int blen, int timeout);
0302 
0303 //-----------------------------------------------------------------------------
0304 //! Read data on a link.  Note that this call either reads all the data wanted
0305 //! or no data if the passed timeout occurs before any data is present.
0306 //!
0307 //! @param  iov     pointer to the message vector.
0308 //! @param  iocnt   number of iov elements in the vector.
0309 //! @param  timeout milliseconds to wait for data. A negative value waits
0310 //!                 forever.
0311 //!
0312 //! @return >=0     number of bytes read.
0313 //!         < 0     an error occurred or when -ETIMEDOUT is returned, no data
0314 //!                 arrived within the timeout period. -ENOMSG is returned
0315 //!                 when poll indicated data was present but 0 bytes were read.
0316 //-----------------------------------------------------------------------------
0317 
0318 int             Recv(const struct iovec *iov, int iocnt, int timeout);
0319 
0320 //-----------------------------------------------------------------------------
0321 //! Read data from a link. Note that this call reads as much data as it can
0322 //! or until the passed timeout has occurred.
0323 //!
0324 //! @param  buff    pointer to buffer to hold data.
0325 //! @param  blen    length of buffer (implies the maximum bytes wanted).
0326 //! @param  timeout milliseconds to wait for data. A negative value waits
0327 //!                 forever.
0328 //!
0329 //! @return >=0     buffer holds data equal to the returned value.
0330 //!         < 0     an error occurred or when -ETIMEDOUT is returned, no data
0331 //!                 arrived within the timeout period. -ENOMSG is returned
0332 //!                 when poll indicated data was present but 0 bytes were read.
0333 //-----------------------------------------------------------------------------
0334 
0335 int             RecvAll(char *buff, int blen, int timeout=-1);
0336 
0337 //------------------------------------------------------------------------------
0338 //! Register a host name with this IP address. This is not MT-safe!
0339 //!
0340 //! @param  hName  pointer to a true host name which should be fully qualified.
0341 //!                One of the IP addresses registered to this name must
0342 //!                match the IP address associated with this object.
0343 //!
0344 //! @return True:    Specified name is now associated with this link.
0345 //!         False:   Nothing changed, registration could not be verified.
0346 //------------------------------------------------------------------------------
0347 
0348 bool        Register(const char *hName);
0349 
0350 //-----------------------------------------------------------------------------
0351 //! Send data on a link. This calls may block unless the socket was marked
0352 //! nonblocking. If a block would occur, the data is copied for later sending.
0353 //!
0354 //! @param  buff    pointer to buffer to send.
0355 //! @param  blen    length of buffer.
0356 //!
0357 //! @return >=0     number of bytes sent.
0358 //!         < 0     an error or occurred.
0359 //-----------------------------------------------------------------------------
0360 
0361 int             Send(const char *buff, int blen);
0362 
0363 //-----------------------------------------------------------------------------
0364 //! Send data on a link. This calls may block unless the socket was marked
0365 //! nonblocking. If a block would occur, the data is copied for later sending.
0366 //!
0367 //! @param  iov     pointer to the message vector.
0368 //! @param  iocnt   number of iov elements in the vector.
0369 //! @param  bytes   the sum of the sizes in the vector.
0370 //!
0371 //! @return >=0     number of bytes sent.
0372 //!         < 0     an error occurred.
0373 //-----------------------------------------------------------------------------
0374 
0375 int             Send(const struct iovec *iov, int iocnt, int bytes=0);
0376 
0377 //-----------------------------------------------------------------------------
0378 //! Send data on a link using sendfile(). This call always blocks until all
0379 //! data is sent. It should only be called if sfOK is true (see below).
0380 //!
0381 //! @param  sdP     pointer to the sendfile vector.
0382 //! @param  sdn     number of elements in the vector.
0383 //!
0384 //! @return >=0     number of bytes sent.
0385 //!         < 0     an error occurred.
0386 //-----------------------------------------------------------------------------
0387 
0388 static bool     sfOK;                   // True if Send(sfVec) enabled
0389 
0390 typedef XrdOucSFVec sfVec;
0391 
0392 int             Send(const sfVec *sdP, int sdn); // Iff sfOK is true
0393 
0394 //-----------------------------------------------------------------------------
0395 //! Wait for all outstanding requests to be completed on the link.
0396 //-----------------------------------------------------------------------------
0397 
0398 void            Serialize();
0399 
0400 //-----------------------------------------------------------------------------
0401 //! Set an error indication on he link.
0402 //!
0403 //! @param  text    a message describing the error.
0404 //!
0405 //! @return  =0     message set, the link is considered in error.
0406 //!          -1     the message pointer was nil.
0407 //-----------------------------------------------------------------------------
0408 
0409 int             setEtext(const char *text);
0410 
0411 //-----------------------------------------------------------------------------
0412 //! Set the client's link identity.
0413 //!
0414 //! @param  userid pointer to the client's username.
0415 //! @param  procid the client's process id (i.e. pid).
0416 //-----------------------------------------------------------------------------
0417 
0418 void            setID(const char *userid, int procid);
0419 
0420 //-----------------------------------------------------------------------------
0421 //! Set the client's location.
0422 //!
0423 //! @param  loc    reference to the location information.
0424 //-----------------------------------------------------------------------------
0425 
0426 void            setLocation(XrdNetAddrInfo::LocInfo &loc);
0427 
0428 //-----------------------------------------------------------------------------
0429 //! Set the link to be non-blocking.
0430 //!
0431 //! @return true   mode has been set.
0432 //! @return false  mode is not supported for this operating system.
0433 //-----------------------------------------------------------------------------
0434 
0435 bool            setNB();
0436 
0437 //-----------------------------------------------------------------------------
0438 //! Set the link's protocol.
0439 //!
0440 //! @param  pp     pointer to the protocol object.
0441 //! @param  runit  if true, starts running the protocol.
0442 //! @param  push   if true, pushes current protocol to be the alternate one.
0443 //!
0444 //! @return        pointer to the previous protocol (may be nil).
0445 //-----------------------------------------------------------------------------
0446 
0447 XrdProtocol    *setProtocol(XrdProtocol *pp, bool runit=false, bool push=false);
0448 
0449 //-----------------------------------------------------------------------------
0450 //! Set the link's protocol name.
0451 //!
0452 //! @param  name   name of he protocol being used. The storage must be
0453 //!                valid for the duration of the program.
0454 //-----------------------------------------------------------------------------
0455 
0456 void            setProtName(const char *name);
0457 
0458 //-----------------------------------------------------------------------------
0459 //! Set the link's parallel usage count.
0460 //!
0461 //! @param  cnt    should be 1 to increased the count and -1 to decrease it.
0462 //-----------------------------------------------------------------------------
0463 
0464 void            setRef(int cnt);
0465 
0466 //-----------------------------------------------------------------------------
0467 //! Enable or disable TLS on the link.
0468 //
0469 //! @param  enable  if true, TLS is enabled if not already enabled. Otherwise,
0470 //!                 TLS is disabled and the TLS logical connection torn down.
0471 //!                 statistics may be contradictory as they are collected async.
0472 //! @param  ctx     The context to use when enabling TLS. When nil, the default
0473 //!                 context is used.
0474 //!
0475 //! @return         True if successful, false otherwise.
0476 //-----------------------------------------------------------------------------
0477 
0478 bool            setTLS(bool enable, XrdTlsContext *ctx=0);
0479 
0480 //-----------------------------------------------------------------------------
0481 //! Shutdown the link but otherwise keep it intact.
0482 //!
0483 //! @param  getLock if true, the operation is performed under a lock.
0484 //-----------------------------------------------------------------------------
0485 
0486 void            Shutdown(bool getLock);
0487 
0488 //-----------------------------------------------------------------------------
0489 //! Obtain link statistics.
0490 //!
0491 //! @param  buff    pointer to the buffer for the xml statistics.
0492 //! @param  blen    length of the buffer.
0493 //! @param  do_sync if true, the statistics self-consistent. Otherwise, the
0494 //!                 statistics may be contradictory as they are collected async.
0495 //!
0496 //! @return         number of bytes placed in the buffer excluding the null byte.
0497 //-----------------------------------------------------------------------------
0498 
0499 static int      Stats(char *buff, int blen, bool do_sync=0);
0500 
0501 //-----------------------------------------------------------------------------
0502 //! Add all local statistics to the global counters.
0503 //!
0504 //! @param  ctime   if not nil, return the total connect time in seconds.
0505 //-----------------------------------------------------------------------------
0506 
0507 void            syncStats(int *ctime=0);
0508 
0509 //-----------------------------------------------------------------------------
0510 //! Terminate a connection.
0511 //!
0512 //! @param  owner   pointer to the link ID representing a client who made
0513 //!                 the connection to be terminated. If nil then this is a
0514 //!                 request for the link to terminate another link, if possible.
0515 //! @param  fdnum   the file descriptor number of the link to be terminated.
0516 //! @param  inst    the link's instance number.
0517 //!
0518 //! @return >0      caller should wait this number of seconds and try again.
0519 //! @return =0      link terminated.
0520 //! @return <0      link could not be terminated:
0521 //!                 -EACCES  the links was not created by the passed owner.
0522 //!                 -EPIPE   link already being terminated.
0523 //!                 -ESRCH   fdnum does not refer to a link.
0524 //!                 -ETIME   unsuccessful, too many tries.
0525 //-----------------------------------------------------------------------------
0526 
0527 int             Terminate(const char *owner, int fdnum, unsigned int inst);
0528 
0529 //-----------------------------------------------------------------------------
0530 //! Return the time the link was made active (i.e. time of connection).
0531 //-----------------------------------------------------------------------------
0532 
0533 time_t          timeCon() const;
0534 
0535 //-----------------------------------------------------------------------------
0536 //! Return link's reference count.
0537 //-----------------------------------------------------------------------------
0538 
0539 int             UseCnt() const;
0540 
0541 //-----------------------------------------------------------------------------
0542 //! Mark this link as an in-memory communications bridge (internal use only).
0543 //-----------------------------------------------------------------------------
0544 
0545 void            armBridge();
0546 
0547 //-----------------------------------------------------------------------------
0548 //! Determine if this link is a bridge.
0549 //!
0550 //! @return true    this link is a bridge.
0551 //! @return false   this link is a plain old link.
0552 //-----------------------------------------------------------------------------
0553 
0554 bool            hasBridge() const {return isBridged;}
0555 
0556 //-----------------------------------------------------------------------------
0557 //! Determine if this link is using TLS.
0558 //!
0559 //! @return true    this link is  using TLS.
0560 //! @return false   this link not using TLS.
0561 //-----------------------------------------------------------------------------
0562 
0563 bool            hasTLS() const {return isTLS;}
0564 
0565 //-----------------------------------------------------------------------------
0566 //! Return TLS protocol version being used.
0567 //!
0568 //! @return The TLS protocol version number. If the link is not using TLS,
0569 //!         a null string is returned;
0570 //-----------------------------------------------------------------------------
0571 
0572 const char     *verTLS();
0573 
0574 //-----------------------------------------------------------------------------
0575 //! Constructor
0576 //!
0577 //! @param  lxq     Reference to the implementation.
0578 //-----------------------------------------------------------------------------
0579 
0580                 XrdLink(XrdLinkXeq &lxq);
0581 
0582 protected:
0583                ~XrdLink() {}  // Is never deleted!
0584 
0585 void            DoIt();       // This is an override of XrdJob::DoIt.
0586 void            ResetLink();
0587 int             Wait4Data(int timeout);
0588 
0589 void           *rsvd1[3];     // Reserved for future use
0590 XrdLinkXeq     &linkXQ;       // The implementation
0591 char           *HostName;     // Pointer to the hostname
0592 unsigned int    Instance;     // Instance number of this object
0593 bool            isBridged;    // If true, this link is an in-memory bridge
0594 bool            isTLS;        // If true, this link uses TLS for all I/O
0595 char            rsvd2[2];
0596 };
0597 #endif