XRootD
XrdLinkXeq.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d L i n k X e q . c c */
4 /* */
5 /* (c) 2018 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* Produced by Andrew Hanushevsky for Stanford University under contract */
7 /* DE-AC02-76-SFO0515 with the Department of Energy */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19 /* License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24 /* */
25 /* The copyright holder's institutional names and contributor's names may not */
26 /* be used to endorse or promote products derived from this software without */
27 /* specific prior written permission of the institution or contributor. */
28 /******************************************************************************/
29 
30 #include <limits.h>
31 #include <poll.h>
32 #include <signal.h>
33 #include <cstdio>
34 #include <cstring>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/uio.h>
38 
39 #if defined(__linux__) || defined(__GNU__)
40 #include <netinet/tcp.h>
41 #if !defined(TCP_CORK)
42 #undef HAVE_SENDFILE
43 #endif
44 #endif
45 
46 #ifdef HAVE_SENDFILE
47 
48 #if defined(__solaris__) || defined(__linux__) || defined(__GNU__)
49 #include <sys/sendfile.h>
50 #endif
51 
52 #endif
53 
54 #include "XrdSys/XrdSysAtomics.hh"
55 #include "XrdSys/XrdSysError.hh"
56 #include "XrdSys/XrdSysFD.hh"
57 #include "XrdSys/XrdSysPlatform.hh"
58 
59 #include "Xrd/XrdBuffer.hh"
60 #include "Xrd/XrdLink.hh"
61 #include "Xrd/XrdLinkCtl.hh"
62 #include "Xrd/XrdLinkXeq.hh"
63 #include "Xrd/XrdPoll.hh"
64 #include "Xrd/XrdScheduler.hh"
65 #include "Xrd/XrdSendQ.hh"
66 #include "Xrd/XrdTcpMonPin.hh"
67 
68 #define TRACE_IDENT ID
69 #include "Xrd/XrdTrace.hh"
70 
71 /******************************************************************************/
72 /* G l o b a l s */
73 /******************************************************************************/
74 
75 namespace
76 {
77 int getIovMax()
78 {
79 int maxiov;
80 #ifdef _SC_IOV_MAX
81  if ((maxiov = sysconf(_SC_IOV_MAX)) > 0) return maxiov;
82 #endif
83 #ifdef IOV_MAX
84  return IOV_MAX;
85 #else
86  return 1024;
87 #endif
88 }
89 };
90 
91 namespace XrdGlobal
92 {
93 extern XrdSysError Log;
94 extern XrdScheduler Sched;
95 extern XrdTlsContext *tlsCtx;
97 extern int devNull;
98  int maxIOV = getIovMax();
99 };
100 
101 using namespace XrdGlobal;
102 
103 /******************************************************************************/
104 /* S t a t i c s */
105 /******************************************************************************/
106 
107  const char *XrdLinkXeq::TraceID = "LinkXeq";
108 
109  long long XrdLinkXeq::LinkBytesIn = 0;
110  long long XrdLinkXeq::LinkBytesOut = 0;
111  long long XrdLinkXeq::LinkConTime = 0;
112  long long XrdLinkXeq::LinkCountTot = 0;
113  int XrdLinkXeq::LinkCount = 0;
114  int XrdLinkXeq::LinkCountMax = 0;
115  int XrdLinkXeq::LinkTimeOuts = 0;
116  int XrdLinkXeq::LinkStalls = 0;
117  int XrdLinkXeq::LinkSfIntr = 0;
119 
120 /******************************************************************************/
121 /* C o n s t r u c t o r */
122 /******************************************************************************/
123 
124 XrdLinkXeq::XrdLinkXeq() : XrdLink(*this), PollInfo((XrdLink &)*this)
125 {
127 }
128 
130 {
131  memcpy(Uname+sizeof(Uname)-7, "anon.0@", 7);
132  strcpy(Lname, "somewhere");
133  ID = &Uname[sizeof(Uname)-5];
134  Comment = ID;
135  sendQ = 0;
136  stallCnt = stallCntTot = 0;
137  tardyCnt = tardyCntTot = 0;
138  SfIntr = 0;
139  isIdle = 0;
141  LockReads= false;
142  KeepFD = false;
143  Protocol = 0;
144  ProtoAlt = 0;
145 
146  LinkInfo.Reset();
147  PollInfo.Zorch();
148  ResetLink();
149 }
150 
151 /******************************************************************************/
152 /* B a c k l o g */
153 /******************************************************************************/
154 
156 {
158 
159 // Return backlog information
160 //
161  return (sendQ ? sendQ->Backlog() : 0);
162 }
163 
164 /******************************************************************************/
165 /* C l i e n t */
166 /******************************************************************************/
167 
168 int XrdLinkXeq::Client(char *nbuf, int nbsz)
169 {
170  int ulen;
171 
172 // Generate full client name
173 //
174  if (nbsz <= 0) return 0;
175  ulen = (Lname - ID);
176  if ((ulen + HNlen) >= nbsz) ulen = 0;
177  else {strncpy(nbuf, ID, ulen);
178  strcpy(nbuf+ulen, HostName);
179  ulen += HNlen;
180  }
181  return ulen;
182 }
183 
184 /******************************************************************************/
185 /* C l o s e */
186 /******************************************************************************/
187 
188 int XrdLinkXeq::Close(bool defer)
190  int csec, fd, rc = 0;
191 
192 // If a defer close is requested, we can close the descriptor but we must
193 // keep the slot number to prevent a new client getting the same fd number.
194 // Linux is peculiar in that any in-progress operations will remain in that
195 // state even after the FD is closed unless there is some activity either on
196 // the connection or an event occurs that causes an operation restart. We
197 // portably solve this problem by issuing a shutdown() on the socket prior
198 // closing it. On most platforms, this informs readers that the connection is
199 // gone (though not on old (i.e. <= 2.3) versions of Linux, sigh). Also, if
200 // nonblocking mode is enabled, we need to do this in a separate thread as
201 // a shutdown may block for a pretty long time if lots\ of messages are queued.
202 // We will ask the SendQ object to schedule the shutdown for us before it
203 // commits suicide.
204 // Note that we can hold the opMutex while we also get the wrMutex.
205 //
206  if (defer)
207  {if (!sendQ) Shutdown(false);
208  else {TRACEI(DEBUG, "Shutdown FD " <<LinkInfo.FD<<" only via SendQ");
209  LinkInfo.InUse++;
210  LinkInfo.FD = -LinkInfo.FD; // Leave poll version untouched!
211  wrMutex.Lock();
212  sendQ->Terminate(this);
213  sendQ = 0;
214  wrMutex.UnLock();
215  }
216  return 0;
217  }
218 
219 // If we got here then this is not a deferred close so we just need to check
220 // if there is a sendq appendage we need to get rid of.
221 //
222  if (sendQ)
223  {wrMutex.Lock();
224  sendQ->Terminate();
225  sendQ = 0;
226  wrMutex.UnLock();
227  }
228 
229 // Multiple protocols may be bound to this link. If it is in use, defer the
230 // actual close until the use count drops to one.
231 //
232  while(LinkInfo.InUse > 1)
233  {opHelper.UnLock();
234  TRACEI(DEBUG, "Close FD "<<LinkInfo.FD <<" deferred, use count="
235  <<LinkInfo.InUse);
236  Serialize();
237  opHelper.Lock(&LinkInfo.opMutex);
238  }
239  LinkInfo.InUse--;
240  Instance = 0;
241 
242 // Add up the statistic for this link
243 //
244  syncStats(&csec);
245 
246 // Cleanup TLS if it is active
247 //
248  if (isTLS) tlsIO.Shutdown();
249 
250 // Clean this link up
251 //
252  if (Protocol) {Protocol->Recycle(this, csec, LinkInfo.Etext); Protocol = 0;}
253  if (ProtoAlt) {ProtoAlt->Recycle(this, csec, LinkInfo.Etext); ProtoAlt = 0;}
254  if (LinkInfo.Etext) {free(LinkInfo.Etext); LinkInfo.Etext = 0;}
255  LinkInfo.InUse = 0;
256 
257 // At this point we can have no lock conflicts, so if someone is waiting for
258 // us to terminate let them know about it. Note that we will get the condvar
259 // mutex while we hold the opMutex. This is the required order! We will also
260 // zero out the pointer to the condvar while holding the opmutex.
261 //
262  if (LinkInfo.KillcvP)
263  {LinkInfo.KillcvP->Lock();
266  LinkInfo.KillcvP = 0;
267  }
268 
269 // Remove ourselves from the poll table and then from the Link table. We may
270 // not hold on to the opMutex when we acquire the LTMutex. However, the link
271 // table needs to be cleaned up prior to actually closing the socket. So, we
272 // do some fancy footwork to prevent multiple closes of this link.
273 //
274  fd = abs(LinkInfo.FD);
275  if (PollInfo.FD > 0)
277  PollInfo.FD = -1;
278  opHelper.UnLock();
279  XrdLinkCtl::Unhook(fd);
280  } else opHelper.UnLock();
281 
282 // Invoke the TCP monitor if it was loaded.
283 //
284  if (TcpMonPin && fd > 2)
285  {XrdTcpMonPin::LinkInfo lnkInfo;
286  lnkInfo.tident = ID;
287  lnkInfo.fd = fd;
288  lnkInfo.consec = csec;
289  lnkInfo.bytesIn = BytesInTot;
290  lnkInfo.bytesOut = BytesOutTot;
291  TcpMonPin->Monitor(Addr, lnkInfo, sizeof(lnkInfo));
292  }
293 
294 // Close the file descriptor if it isn't being shared. Do it as the last
295 // thing because closes and accepts and not interlocked.
296 //
297  if (fd >= 2) {if (KeepFD) rc = 0;
298  else rc = (close(fd) < 0 ? errno : 0);
299  }
300  if (rc) Log.Emsg("Link", rc, "close", ID);
301  return rc;
302 }
303 
304 /******************************************************************************/
305 /* D o I t */
306 /******************************************************************************/
307 
309 {
310  int rc;
311 
312 // The Process() return code tells us what to do:
313 // < 0 -> Stop getting requests,
314 // -EINPROGRESS leave link disabled but otherwise all is well
315 // -n Error, disable and close the link
316 // = 0 -> OK, get next request, if allowed, o/w enable the link
317 // > 0 -> Slow link, stop getting requests and enable the link
318 //
319  if (Protocol)
320  do {rc = Protocol->Process(this);} while (!rc && Sched.canStick());
321  else {Log.Emsg("Link", "Dispatch on closed link", ID);
322  return;
323  }
324 
325 // Either re-enable the link and cycle back waiting for a new request, leave
326 // disabled, or terminate the connection.
327 //
328  if (rc >= 0)
330  else if (rc != -EINPROGRESS) Close();
331 }
332 
333 /******************************************************************************/
334 /* g e t P e e r C e r t s */
335 /******************************************************************************/
336 
338 {
339  return (isTLS ? tlsIO.getCerts(true) : 0);
340 }
341 
342 /******************************************************************************/
343 /* P e e k */
344 /******************************************************************************/
345 
346 int XrdLinkXeq::Peek(char *Buff, int Blen, int timeout)
347 {
348  XrdSysMutexHelper theMutex;
349  struct pollfd polltab = {PollInfo.FD, POLLIN|POLLRDNORM, 0};
350  ssize_t mlen;
351  int retc;
352 
353 // Lock the read mutex if we need to, the helper will unlock it upon exit
354 //
355  if (LockReads) theMutex.Lock(&rdMutex);
356 
357 // Wait until we can actually read something
358 //
359  isIdle = 0;
360  do {retc = poll(&polltab, 1, timeout);} while(retc < 0 && errno == EINTR);
361  if (retc != 1)
362  {if (retc == 0) return 0;
363  return Log.Emsg("Link", -errno, "poll", ID);
364  }
365 
366 // Verify it is safe to read now
367 //
368  if (!(polltab.revents & (POLLIN|POLLRDNORM)))
369  {Log.Emsg("Link", XrdPoll::Poll2Text(polltab.revents), "polling", ID);
370  return -1;
371  }
372 
373 // Do the peek.
374 //
375  do {mlen = recv(LinkInfo.FD, Buff, Blen, MSG_PEEK);}
376  while(mlen < 0 && errno == EINTR);
377 
378 // Return the result
379 //
380  if (mlen >= 0) return int(mlen);
381  Log.Emsg("Link", errno, "peek on", ID);
382  return -1;
383 }
384 
385 /******************************************************************************/
386 /* R e c v */
387 /******************************************************************************/
388 
389 int XrdLinkXeq::Recv(char *Buff, int Blen)
390 {
391  ssize_t rlen;
392 
393 // Note that we will read only as much as is queued. Use Recv() with a
394 // timeout to receive as much data as possible.
395 //
396  if (LockReads) rdMutex.Lock();
397  isIdle = 0;
398  do {rlen = read(LinkInfo.FD, Buff, Blen);} while(rlen < 0 && errno == EINTR);
399  if (rlen > 0) AtomicAdd(BytesIn, rlen);
400  if (LockReads) rdMutex.UnLock();
401 
402  if (rlen >= 0) return int(rlen);
403  if (LinkInfo.FD >= 0) Log.Emsg("Link", errno, "receive from", ID);
404  return -1;
405 }
406 
407 /******************************************************************************/
408 
409 int XrdLinkXeq::Recv(char *Buff, int Blen, int timeout)
410 {
411  XrdSysMutexHelper theMutex;
412  struct pollfd polltab = {PollInfo.FD, POLLIN|POLLRDNORM, 0};
413  ssize_t rlen, totlen = 0;
414  int retc;
415 
416 // Lock the read mutex if we need to, the helper will unlock it upon exit
417 //
418  if (LockReads) theMutex.Lock(&rdMutex);
419 
420 // Wait up to timeout milliseconds for data to arrive
421 //
422  isIdle = 0;
423  while(Blen > 0)
424  {do {retc = poll(&polltab,1,timeout);} while(retc < 0 && errno == EINTR);
425  if (retc != 1)
426  {if (retc == 0)
427  {tardyCnt++;
428  if (totlen)
429  {if ((++stallCnt & 0xff) == 1) TRACEI(DEBUG,"read timed out");
430  AtomicAdd(BytesIn, totlen);
431  }
432  return int(totlen);
433  }
434  return (LinkInfo.FD >= 0 ? Log.Emsg("Link",-errno,"poll",ID) : -1);
435  }
436 
437  // Verify it is safe to read now
438  //
439  if (!(polltab.revents & (POLLIN|POLLRDNORM)))
440  {Log.Emsg("Link", XrdPoll::Poll2Text(polltab.revents),
441  "polling", ID);
442  return -1;
443  }
444 
445  // Read as much data as you can. Note that we will force an error
446  // if we get a zero-length read after poll said it was OK.
447  //
448  do {rlen = recv(LinkInfo.FD, Buff, Blen, 0);}
449  while(rlen < 0 && errno == EINTR);
450  if (rlen <= 0)
451  {if (!rlen) return -ENOMSG;
452  if (LinkInfo.FD > 0) Log.Emsg("Link", -errno, "receive from", ID);
453  return -1;
454  }
455  totlen += rlen; Blen -= rlen; Buff += rlen;
456  }
457 
458  AtomicAdd(BytesIn, totlen);
459  return int(totlen);
460 }
461 
462 /******************************************************************************/
463 
464 int XrdLinkXeq::Recv(const struct iovec *iov, int iocnt, int timeout)
465 {
466  XrdSysMutexHelper theMutex;
467  struct pollfd polltab = {PollInfo.FD, POLLIN|POLLRDNORM, 0};
468  int retc, rlen;
469 
470 // Lock the read mutex if we need to, the helper will unlock it upon exit
471 //
472  if (LockReads) theMutex.Lock(&rdMutex);
473 
474 // Wait up to timeout milliseconds for data to arrive
475 //
476  isIdle = 0;
477  do {retc = poll(&polltab,1,timeout);} while(retc < 0 && errno == EINTR);
478  if (retc != 1)
479  {if (retc == 0)
480  {tardyCnt++;
481  return 0;
482  }
483  return (LinkInfo.FD >= 0 ? Log.Emsg("Link",-errno,"poll",ID) : -1);
484  }
485 
486 // Verify it is safe to read now
487 //
488  if (!(polltab.revents & (POLLIN|POLLRDNORM)))
489  {Log.Emsg("Link", XrdPoll::Poll2Text(polltab.revents), "polling", ID);
490  return -1;
491  }
492 
493 // If the iocnt is within limits then just go ahead and read once.
494 //
495  if (iocnt <= maxIOV)
496  {rlen = RecvIOV(iov, iocnt);
497  if (rlen > 0) {AtomicAdd(BytesIn, rlen);}
498  return rlen;
499  }
500 
501 // We will have to break this up into allowable segments and we need to add up
502 // the bytes in each segment so that we know when to stop reading.
503 //
504  int seglen, segcnt = maxIOV, totlen = 0;
505  do {seglen = 0;
506  for (int i = 0; i < segcnt; i++) seglen += iov[i].iov_len;
507  if ((rlen = RecvIOV(iov, segcnt)) < 0) return rlen;
508  totlen += rlen;
509  if (rlen < seglen) break;
510  iov += segcnt;
511  iocnt -= segcnt;
512  if (iocnt <= maxIOV) segcnt = iocnt;
513  } while(iocnt > 0);
514 
515 // All done
516 //
517  AtomicAdd(BytesIn, totlen);
518  return totlen;
519 }
520 
521 /******************************************************************************/
522 /* R e c v A l l */
523 /******************************************************************************/
524 
525 int XrdLinkXeq::RecvAll(char *Buff, int Blen, int timeout)
526 {
527  struct pollfd polltab = {PollInfo.FD, POLLIN|POLLRDNORM, 0};
528  ssize_t rlen;
529  int retc;
530 
531 // Check if timeout specified. Notice that the timeout is the max we will
532 // for some data. We will wait forever for all the data. Yeah, it's weird.
533 //
534  if (timeout >= 0)
535  {do {retc = poll(&polltab,1,timeout);} while(retc < 0 && errno == EINTR);
536  if (retc != 1)
537  {if (!retc) return -ETIMEDOUT;
538  Log.Emsg("Link",errno,"poll",ID);
539  return -1;
540  }
541  if (!(polltab.revents & (POLLIN|POLLRDNORM)))
542  {Log.Emsg("Link",XrdPoll::Poll2Text(polltab.revents),"polling",ID);
543  return -1;
544  }
545  }
546 
547 // Note that we will block until we receive all he bytes.
548 //
549  if (LockReads) rdMutex.Lock();
550  isIdle = 0;
551  do {rlen = recv(LinkInfo.FD, Buff, Blen, MSG_WAITALL);}
552  while(rlen < 0 && errno == EINTR);
553  if (rlen > 0) AtomicAdd(BytesIn, rlen);
554  if (LockReads) rdMutex.UnLock();
555 
556  if (int(rlen) == Blen) return Blen;
557  if (!rlen) {TRACEI(DEBUG, "No RecvAll() data; errno=" <<errno);}
558  else if (rlen > 0) Log.Emsg("RecvAll", "Premature end from", ID);
559  else if (LinkInfo.FD >= 0) Log.Emsg("Link", errno, "receive from", ID);
560  return -1;
561 }
562 
563 /******************************************************************************/
564 /* Protected: R e c v I O V */
565 /******************************************************************************/
566 
567 int XrdLinkXeq::RecvIOV(const struct iovec *iov, int iocnt)
568 {
569  ssize_t retc = 0;
570 
571 // Read the data in. On some version of Unix (e.g., Linux) a readv() may
572 // end at any time without reading all the bytes when directed to a socket.
573 // We always return the number bytes read (or an error). The caller needs to
574 // restart the read at the appropriate place in the iovec when more data arrives.
575 //
576  do {retc = readv(LinkInfo.FD, iov, iocnt);}
577  while(retc < 0 && errno == EINTR);
578 
579 // Check how we completed
580 //
581  if (retc < 0) Log.Emsg("Link", errno, "receive from", ID);
582  return retc;
583 }
584 
585 /******************************************************************************/
586 /* R e g i s t e r */
587 /******************************************************************************/
588 
589 bool XrdLinkXeq::Register(const char *hName)
590 {
591 
592 // Make appropriate changes here
593 //
594  if (HostName) free(HostName);
595  HostName = strdup(hName);
596  strlcpy(Lname, hName, sizeof(Lname));
597  return true;
598 }
599 
600 /******************************************************************************/
601 /* S e n d */
602 /******************************************************************************/
603 
604 int XrdLinkXeq::Send(const char *Buff, int Blen)
605 {
606  ssize_t retc = 0, bytesleft = Blen;
607 
608 // Get a lock
609 //
610  wrMutex.Lock();
611  isIdle = 0;
612  AtomicAdd(BytesOut, Blen);
613 
614 // Do non-blocking writes if we are setup to do so.
615 //
616  if (sendQ)
617  {retc = sendQ->Send(Buff, Blen);
618  wrMutex.UnLock();
619  return retc;
620  }
621 
622 // Write the data out
623 //
624  while(bytesleft)
625  {if ((retc = write(LinkInfo.FD, Buff, bytesleft)) < 0)
626  {if (errno == EINTR) continue;
627  else break;
628  }
629  bytesleft -= retc; Buff += retc;
630  }
631 
632 // All done
633 //
634  wrMutex.UnLock();
635  if (retc >= 0) return Blen;
636  Log.Emsg("Link", errno, "send to", ID);
637  return -1;
638 }
639 
640 /******************************************************************************/
641 
642 int XrdLinkXeq::Send(const struct iovec *iov, int iocnt, int bytes)
643 {
644  int retc;
645  static int maxIOV = -1;
646  if (maxIOV == -1) {
647 #ifdef _SC_IOV_MAX
648  maxIOV = sysconf(_SC_IOV_MAX);
649  if (maxIOV == -1)
650 #endif
651 #ifdef IOV_MAX
652  maxIOV = IOV_MAX;
653 #else
654  maxIOV = 1024;
655 #endif
656  }
657 
658 // Get a lock and assume we will be successful (statistically we are)
659 //
660  wrMutex.Lock();
661  isIdle = 0;
662  AtomicAdd(BytesOut, bytes);
663 
664 // Do non-blocking writes if we are setup to do so.
665 //
666  if (sendQ)
667  {retc = sendQ->Send(iov, iocnt, bytes);
668  wrMutex.UnLock();
669  return retc;
670  }
671 
672 // If the iocnt is within limits then just go ahead and write this out
673 //
674  if (iocnt <= maxIOV)
675  {retc = SendIOV(iov, iocnt, bytes);
676  wrMutex.UnLock();
677  return retc;
678  }
679 
680 // We will have to break this up into allowable segments
681 //
682  int seglen, segcnt = maxIOV, iolen = 0;
683  do {seglen = 0;
684  for (int i = 0; i < segcnt; i++) seglen += iov[i].iov_len;
685  if ((retc = SendIOV(iov, segcnt, seglen)) < 0)
686  {wrMutex.UnLock();
687  return retc;
688  }
689  iolen += retc;
690  iov += segcnt;
691  iocnt -= segcnt;
692  if (iocnt <= maxIOV) segcnt = iocnt;
693  } while(iocnt > 0);
694 
695 // All done
696 //
697  wrMutex.UnLock();
698  return iolen;
699 }
700 
701 /******************************************************************************/
702 
703 int XrdLinkXeq::Send(const sfVec *sfP, int sfN)
704 {
705 #if !defined(HAVE_SENDFILE)
706 
707  return -1;
708 
709 #elif defined(__solaris__)
710 
711  sendfilevec_t vecSF[XrdOucSFVec::sfMax], *vecSFP = vecSF;
712  size_t xframt, totamt, bytes = 0;
713  ssize_t retc;
714  int i = 0;
715 
716 // Construct the sendfilev() vector
717 //
718  for (i = 0; i < sfN; sfP++, i++)
719  {if (sfP->fdnum < 0)
720  {vecSF[i].sfv_fd = SFV_FD_SELF;
721  vecSF[i].sfv_off = (off_t)sfP->buffer;
722  } else {
723  vecSF[i].sfv_fd = sfP->fdnum;
724  vecSF[i].sfv_off = sfP->offset;
725  }
726  vecSF[i].sfv_flag = 0;
727  vecSF[i].sfv_len = sfP->sendsz;
728  bytes += sfP->sendsz;
729  }
730  totamt = bytes;
731 
732 // Lock the link, issue sendfilev(), and unlock the link. The documentation
733 // is very spotty and inconsistent. We can only retry this operation under
734 // very limited conditions.
735 //
736  wrMutex.Lock();
737  isIdle = 0;
738 do{retc = sendfilev(LinkInfo.FD, vecSFP, sfN, &xframt);
739 
740 // Check if all went well and return if so (usual case)
741 //
742  if (xframt == bytes)
743  {AtomicAdd(BytesOut, bytes);
744  wrMutex.UnLock();
745  return totamt;
746  }
747 
748 // The only one we will recover from is EINTR. We cannot legally get EAGAIN.
749 //
750  if (retc < 0 && errno != EINTR) break;
751 
752 // Try to resume the transfer
753 //
754  if (xframt > 0)
755  {AtomicAdd(BytesOut, xframt); bytes -= xframt; SfIntr++;
756  while(xframt > 0 && sfN)
757  {if ((ssize_t)xframt < (ssize_t)vecSFP->sfv_len)
758  {vecSFP->sfv_off += xframt; vecSFP->sfv_len -= xframt; break;}
759  xframt -= vecSFP->sfv_len; vecSFP++; sfN--;
760  }
761  }
762  } while(sfN > 0);
763 
764 // See if we can recover without destroying the connection
765 //
766  retc = (retc < 0 ? errno : ECANCELED);
767  wrMutex.UnLock();
768  Log.Emsg("Link", retc, "send file to", ID);
769  return -1;
770 
771 #elif defined(__linux__) || defined(__GNU__)
772 
773  static const int setON = 1, setOFF = 0;
774  ssize_t retc = 0, bytesleft;
775  off_t myOffset;
776  int i, xfrbytes = 0, uncork = 1, xIntr = 0;
777 
778 // lock the link
779 //
780  wrMutex.Lock();
781  isIdle = 0;
782 
783 // In linux we need to cork the socket. On permanent errors we do not uncork
784 // the socket because it will be closed in short order.
785 //
786  if (setsockopt(PollInfo.FD, SOL_TCP, TCP_CORK, &setON, sizeof(setON)) < 0)
787  {Log.Emsg("Link", errno, "cork socket for", ID);
788  uncork = 0; sfOK = 0;
789  }
790 
791 // Send the header first
792 //
793  for (i = 0; i < sfN; sfP++, i++)
794  {if (sfP->fdnum < 0) retc = sendData(sfP->buffer, sfP->sendsz);
795  else {myOffset = sfP->offset; bytesleft = sfP->sendsz;
796  while(bytesleft
797  && (retc=sendfile(LinkInfo.FD,sfP->fdnum,&myOffset,bytesleft)) > 0)
798  {bytesleft -= retc; xIntr++;}
799  }
800  if (retc < 0 && errno == EINTR) continue;
801  if (retc <= 0) break;
802  xfrbytes += sfP->sendsz;
803  }
804 
805 // Diagnose any sendfile errors
806 //
807  if (retc <= 0)
808  {if (retc == 0) errno = ECANCELED;
809  wrMutex.UnLock();
810  Log.Emsg("Link", errno, "send file to", ID);
811  return -1;
812  }
813 
814 // Now uncork the socket
815 //
816  if (uncork
817  && setsockopt(PollInfo.FD, SOL_TCP, TCP_CORK, &setOFF, sizeof(setOFF)) < 0)
818  Log.Emsg("Link", errno, "uncork socket for", ID);
819 
820 // All done
821 //
822  if (xIntr > sfN) SfIntr += (xIntr - sfN);
823  AtomicAdd(BytesOut, xfrbytes);
824  wrMutex.UnLock();
825  return xfrbytes;
826 
827 #else
828 
829  return -1;
830 
831 #endif
832 }
833 
834 /******************************************************************************/
835 /* Protected: s e n d D a t a */
836 /******************************************************************************/
837 
838 int XrdLinkXeq::sendData(const char *Buff, int Blen)
839 {
840  ssize_t retc = 0, bytesleft = Blen;
841 
842 // Write the data out
843 //
844  while(bytesleft)
845  {if ((retc = write(LinkInfo.FD, Buff, bytesleft)) < 0)
846  {if (errno == EINTR) continue;
847  else break;
848  }
849  bytesleft -= retc; Buff += retc;
850  }
851 
852 // All done
853 //
854  return retc;
855 }
856 
857 /******************************************************************************/
858 /* Protected: S e n d I O V */
859 /******************************************************************************/
860 
861 int XrdLinkXeq::SendIOV(const struct iovec *iov, int iocnt, int bytes)
862 {
863  ssize_t bytesleft, n, retc = 0;
864  const char *Buff;
865 
866 // Write the data out. On some version of Unix (e.g., Linux) a writev() may
867 // end at any time without writing all the bytes when directed to a socket.
868 // So, we attempt to resume the writev() using a combination of write() and
869 // a writev() continuation. This approach slowly converts a writev() to a
870 // series of writes if need be. We must do this inline because we must hold
871 // the lock until all the bytes are written or an error occurs.
872 //
873  bytesleft = static_cast<ssize_t>(bytes);
874  while(bytesleft)
875  {do {retc = writev(LinkInfo.FD, iov, iocnt);}
876  while(retc < 0 && errno == EINTR);
877  if (retc >= bytesleft || retc < 0) break;
878  bytesleft -= retc;
879  while(retc >= (n = static_cast<ssize_t>(iov->iov_len)))
880  {retc -= n; iov++; iocnt--;}
881  Buff = (const char *)iov->iov_base + retc; n -= retc; iov++; iocnt--;
882  while(n) {if ((retc = write(LinkInfo.FD, Buff, n)) < 0)
883  {if (errno == EINTR) continue;
884  else break;
885  }
886  n -= retc; Buff += retc; bytesleft -= retc;
887  }
888  if (retc < 0 || iocnt < 1) break;
889  }
890 
891 // All done
892 //
893  if (retc >= 0) return bytes;
894  Log.Emsg("Link", errno, "send to", ID);
895  return -1;
896 }
897 
898 /******************************************************************************/
899 /* s e t I D */
900 /******************************************************************************/
901 
902 void XrdLinkXeq::setID(const char *userid, int procid)
903 {
904  char buff[sizeof(Uname)], *bp, *sp;
905  int ulen;
906 
907  snprintf(buff, sizeof(buff), "%s.%d:%d", userid, procid, PollInfo.FD);
908  ulen = strlen(buff);
909  sp = buff + ulen - 1;
910  bp = &Uname[sizeof(Uname)-1];
911  if (ulen > (int)sizeof(Uname)) ulen = sizeof(Uname);
912  *bp = '@'; bp--;
913  while(ulen--) {*bp = *sp; bp--; sp--;}
914  ID = bp+1;
915  Comment = (const char *)ID;
916 
917 // Update the ID in the TLS socket if enabled
918 //
919  if (isTLS) tlsIO.SetTraceID(ID);
920 }
921 
922 /******************************************************************************/
923 /* s e t N B */
924 /******************************************************************************/
925 
927 {
928 // We don't support non-blocking output except for Linux at the moment
929 //
930 #if !defined(__linux__)
931  return false;
932 #else
933 // Trace this request
934 //
935  TRACEI(DEBUG,"enabling non-blocking output");
936 
937 // If we don't already have a sendQ object get one. This is a one-time call
938 // so to optimize checking if this object exists we also get the opMutex.'
939 //
941  if (!sendQ)
942  {wrMutex.Lock();
943  sendQ = new XrdSendQ(*this, wrMutex);
944  wrMutex.UnLock();
945  }
947  return true;
948 #endif
949 }
950 
951 /******************************************************************************/
952 /* s e t P r o t o c o l */
953 /******************************************************************************/
954 
956 {
957 
958 // Set new protocol.
959 //
961  XrdProtocol *op = Protocol;
962  if (push) ProtoAlt = Protocol;
963  Protocol = pp;
965  return op;
966 }
967 
968 /******************************************************************************/
969 /* s e t P r o t N a m e */
970 /******************************************************************************/
971 
972 void XrdLinkXeq::setProtName(const char *name)
973 {
974 
975 // Set the protocol name.
976 //
978  Addr.SetDialect(name);
980 }
981 
982 /******************************************************************************/
983 /* s e t T L S */
984 /******************************************************************************/
985 
986 bool XrdLinkXeq::setTLS(bool enable, XrdTlsContext *ctx)
987 { //???
988 // static const XrdTlsConnection::RW_Mode rwMode=XrdTlsConnection::TLS_RNB_WBL;
991  const char *eNote;
992  XrdTls::RC rc;
993 
994 // If we are already in a compatible mode, we are done
995 //
996 
997  if (isTLS == enable) return true;
998 
999 // If this is a shutdown, then do it now.
1000 //
1001  if (!enable)
1002  {tlsIO.Shutdown();
1003  isTLS = enable;
1004  Addr.SetTLS(enable);
1005  return true;
1006  }
1007 // We want to initialize TLS, do so now.
1008 //
1009  if (!ctx) ctx = tlsCtx;
1010  eNote = tlsIO.Init(*ctx, PollInfo.FD, rwMode, hsMode, false, false, ID);
1011 
1012 // Check for errors
1013 //
1014  if (eNote)
1015  {char buff[1024];
1016  snprintf(buff, sizeof(buff), "Unable to enable tls for %s;", ID);
1017  Log.Emsg("LinkXeq", buff, eNote);
1018  return false;
1019  }
1020 
1021 // Now we need to accept this TLS connection
1022 //
1023  std::string eMsg;
1024  rc = tlsIO.Accept(&eMsg);
1025 
1026 // Diagnose return state
1027 //
1028  if (rc != XrdTls::TLS_AOK) Log.Emsg("LinkXeq", eMsg.c_str());
1029  else {isTLS = enable;
1030  Addr.SetTLS(enable);
1031  Log.Emsg("LinkXeq", ID, "connection upgraded to", verTLS());
1032  }
1033  return rc == XrdTls::TLS_AOK;
1034 }
1035 
1036 /******************************************************************************/
1037 /* S F E r r o r */
1038 /******************************************************************************/
1039 
1041 {
1042  Log.Emsg("TLS", rc, "send file to", ID);
1043  return -1;
1044 }
1045 
1046 /******************************************************************************/
1047 /* S h u t d o w n */
1048 /******************************************************************************/
1049 
1050 void XrdLinkXeq::Shutdown(bool getLock)
1051 {
1052  int temp;
1053 
1054 // Trace the entry
1055 //
1056  TRACEI(DEBUG, (getLock ? "Async" : "Sync") <<" link shutdown in progress");
1057 
1058 // Get the lock if we need too (external entry via another thread)
1059 //
1060  if (getLock) LinkInfo.opMutex.Lock();
1061 
1062 // If there is something to do, do it now
1063 //
1064  temp = Instance; Instance = 0;
1065  if (!KeepFD)
1066  {shutdown(PollInfo.FD, SHUT_RDWR);
1067  if (dup2(devNull, PollInfo.FD) < 0)
1068  {Instance = temp;
1069  Log.Emsg("Link", errno, "shutdown FD for", ID);
1070  }
1071  }
1072 
1073 // All done
1074 //
1075  if (getLock) LinkInfo.opMutex.UnLock();
1076 }
1077 
1078 /******************************************************************************/
1079 /* S t a t s */
1080 /******************************************************************************/
1081 
1082 int XrdLinkXeq::Stats(char *buff, int blen, bool do_sync)
1083 {
1084  static const char statfmt[] = "<stats id=\"link\"><num>%d</num>"
1085  "<maxn>%d</maxn><tot>%lld</tot><in>%lld</in><out>%lld</out>"
1086  "<ctime>%lld</ctime><tmo>%d</tmo><stall>%d</stall>"
1087  "<sfps>%d</sfps></stats>";
1088  int i;
1089 
1090 // Check if actual length wanted
1091 //
1092  if (!buff) return sizeof(statfmt)+17*6;
1093 
1094 // We must synchronize the statistical counters
1095 //
1096  if (do_sync) XrdLinkCtl::SyncAll();
1097 
1098 // Obtain lock on the stats area and format it
1099 //
1101  i = snprintf(buff, blen, statfmt, AtomicGet(LinkCount),
1111  return i;
1112 }
1113 
1114 /******************************************************************************/
1115 /* s y n c S t a t s */
1116 /******************************************************************************/
1117 
1118 void XrdLinkXeq::syncStats(int *ctime)
1119 {
1120  long long tmpLL;
1121  int tmpI4;
1122 
1123 // If this is dynamic, get the opMutex lock
1124 //
1125  if (!ctime) LinkInfo.opMutex.Lock();
1126 
1127 // Either the caller has the opMutex or this is called out of close. In either
1128 // case, we need to get the read and write mutexes; each followed by the stats
1129 // mutex. This order is important because we should not hold the stats mutex
1130 // for very long and the r/w mutexes may take a long time to acquire. If we
1131 // must maintain the link count we need to actually acquire the stats mutex as
1132 // we will be doing compound operations. Atomics are still used to keep other
1133 // threads from seeing partial results.
1134 //
1135  AtomicBeg(rdMutex);
1136 
1137  if (ctime)
1138  {*ctime = time(0) - LinkInfo.conTime;
1139  AtomicAdd(LinkConTime, *ctime);
1140  statsMutex.Lock();
1141  if (LinkCount > 0) AtomicDec(LinkCount);
1142  statsMutex.UnLock();
1143  }
1144 
1146 
1147  tmpLL = AtomicFAZ(BytesIn);
1148  AtomicAdd(LinkBytesIn, tmpLL); AtomicAdd(BytesInTot, tmpLL);
1149  tmpI4 = AtomicFAZ(tardyCnt);
1150  AtomicAdd(LinkTimeOuts, tmpI4); AtomicAdd(tardyCntTot, tmpI4);
1151  tmpI4 = AtomicFAZ(stallCnt);
1152  AtomicAdd(LinkStalls, tmpI4); AtomicAdd(stallCntTot, tmpI4);
1154 
1156  tmpLL = AtomicFAZ(BytesOut);
1157  AtomicAdd(LinkBytesOut, tmpLL); AtomicAdd(BytesOutTot, tmpLL);
1158  tmpI4 = AtomicFAZ(SfIntr);
1159  AtomicAdd(LinkSfIntr, tmpI4);
1161 
1162 // Make sure the protocol updates it's statistics as well
1163 //
1164  if (Protocol) Protocol->Stats(0, 0, 1);
1165 
1166 // All done
1167 //
1168  if (!ctime) LinkInfo.opMutex.UnLock();
1169 }
1170 
1171 /******************************************************************************/
1172 /* Protected: T L S _ E r r o r */
1173 /******************************************************************************/
1174 
1175 int XrdLinkXeq::TLS_Error(const char *act, XrdTls::RC rc)
1176 {
1177  std::string reason = XrdTls::RC2Text(rc);
1178  char msg[512];
1179 
1180  snprintf(msg, sizeof(msg), "Unable to %s %s;", act, ID);
1181  Log.Emsg("TLS", msg, reason.c_str());
1182  return -1;
1183 }
1184 
1185 /******************************************************************************/
1186 /* T L S _ P e e k */
1187 /******************************************************************************/
1188 
1189 int XrdLinkXeq::TLS_Peek(char *Buff, int Blen, int timeout)
1190 {
1191  XrdSysMutexHelper theMutex;
1192  XrdTls::RC retc;
1193  int rc, rlen;
1194 
1195 // Lock the read mutex if we need to, the helper will unlock it upon exit
1196 //
1197  if (LockReads) theMutex.Lock(&rdMutex);
1198 
1199 // Wait until we can actually read something
1200 //
1201  isIdle = 0;
1202  if (timeout)
1203  {rc = Wait4Data(timeout);
1204  if (rc < 1) return rc;
1205  }
1206 
1207 // Do the peek and if sucessful, the number of bytes available.
1208 //
1209  retc = tlsIO.Peek(Buff, Blen, rlen);
1210  if (retc == XrdTls::TLS_AOK) return rlen;
1211 
1212 // Dianose the TLS error and return failure
1213 //
1214  return TLS_Error("peek on", retc);
1215 }
1216 
1217 /******************************************************************************/
1218 /* T L S _ R e c v */
1219 /******************************************************************************/
1220 
1221 int XrdLinkXeq::TLS_Recv(char *Buff, int Blen)
1222 {
1223  XrdSysMutexHelper theMutex;
1224  XrdTls::RC retc;
1225  int rlen;
1226 
1227 // Lock the read mutex if we need to, the helper will unlock it upon exit
1228 //
1229  if (LockReads) theMutex.Lock(&rdMutex);
1230 
1231 // Note that we will read only as much as is queued. Use Recv() with a
1232 // timeout to receive as much data as possible.
1233 //
1234  isIdle = 0;
1235  retc = tlsIO.Read(Buff, Blen, rlen);
1236  if (retc != XrdTls::TLS_AOK) return TLS_Error("receive from", retc);
1237  if (rlen > 0) AtomicAdd(BytesIn, rlen);
1238  return rlen;
1239 }
1240 
1241 /******************************************************************************/
1242 
1243 int XrdLinkXeq::TLS_Recv(char *Buff, int Blen, int timeout, bool havelock)
1244 {
1245  XrdSysMutexHelper theMutex;
1246  XrdTls::RC retc;
1247  int pend, rlen, totlen = 0;
1248 
1249 // Lock the read mutex if we need to, the helper will unlock it upon exit
1250 //
1251  if (LockReads && !havelock) theMutex.Lock(&rdMutex);
1252 
1253 // Wait up to timeout milliseconds for data to arrive
1254 //
1255  isIdle = 0;
1256  while(Blen > 0)
1257  {pend = tlsIO.Pending(true);
1258  if (!pend) pend = Wait4Data(timeout);
1259  if (pend < 1)
1260  {if (pend < 0) return -1;
1261  tardyCnt++;
1262  if (totlen)
1263  {if ((++stallCnt & 0xff) == 1) TRACEI(DEBUG,"read timed out");
1264  AtomicAdd(BytesIn, totlen);
1265  }
1266  return totlen;
1267  }
1268 
1269  // Read as much data as you can. Note that we will force an error
1270  // if we get a zero-length read after poll said it was OK. However,
1271  // if we never read anything, then we simply return -ENOMSG to avoid
1272  // generating a "read link error" as clearly there was a hangup.
1273  //
1274  retc = tlsIO.Read(Buff, Blen, rlen);
1275  if (retc != XrdTls::TLS_AOK)
1276  {if (!totlen) return -ENOMSG;
1277  AtomicAdd(BytesIn, totlen);
1278  return TLS_Error("receive from", retc);
1279  }
1280  if (rlen <= 0) break;
1281  totlen += rlen; Blen -= rlen; Buff += rlen;
1282  }
1283 
1284  AtomicAdd(BytesIn, totlen);
1285  return totlen;
1286 }
1287 
1288 /******************************************************************************/
1289 
1290 int XrdLinkXeq::TLS_Recv(const struct iovec *iov, int iocnt, int timeout)
1291 {
1292  XrdSysMutexHelper theMutex;
1293  char *Buff;
1294  int Blen, rlen, totlen = 0;
1295 
1296 // Lock the read mutex if we need to, the helper will unlock it upon exit
1297 //
1298  if (LockReads) theMutex.Lock(&rdMutex);
1299 
1300 // Individually process each element until we can't read any more
1301 //
1302  isIdle = 0;
1303  for (int i = 0; i < iocnt; i++)
1304  {Buff = (char *)iov[i].iov_base;
1305  Blen = iov[i].iov_len;
1306  rlen = TLS_Recv(Buff, Blen, timeout, true);
1307  if (rlen <= 0) break;
1308  totlen += rlen;
1309  if (rlen < Blen) break;
1310  }
1311 
1312  if (totlen) {AtomicAdd(BytesIn, totlen);}
1313  return totlen;
1314 }
1315 
1316 /******************************************************************************/
1317 /* T L S _ R e c v A l l */
1318 /******************************************************************************/
1319 
1320 int XrdLinkXeq::TLS_RecvAll(char *Buff, int Blen, int timeout)
1321 {
1322  int retc;
1323 
1324 // Check if timeout specified. Notice that the timeout is the max we will
1325 // wait for some data. We will wait forever for all the data. Yeah, it's weird.
1326 //
1327  if (timeout >= 0)
1328  {retc = tlsIO.Pending(true);
1329  if (!retc) retc = Wait4Data(timeout);
1330  if (retc < 1) return (retc ? -1 : -ETIMEDOUT);
1331  }
1332 
1333 // Note that we will block until we receive all the bytes.
1334 //
1335  return TLS_Recv(Buff, Blen, -1);
1336 }
1337 
1338 /******************************************************************************/
1339 /* T L S _ S e n d */
1340 /******************************************************************************/
1341 
1342 int XrdLinkXeq::TLS_Send(const char *Buff, int Blen)
1343 {
1345  ssize_t bytesleft = Blen;
1346  XrdTls::RC retc;
1347  int byteswritten;
1348 
1349 // Prepare to send
1350 //
1351  isIdle = 0;
1352  AtomicAdd(BytesOut, Blen);
1353 
1354 // Do non-blocking writes if we are setup to do so.
1355 //
1356  if (sendQ) return sendQ->Send(Buff, Blen);
1357 
1358 // Write the data out
1359 //
1360  while(bytesleft)
1361  {retc = tlsIO.Write(Buff, bytesleft, byteswritten);
1362  if (retc != XrdTls::TLS_AOK) return TLS_Error("send to", retc);
1363  bytesleft -= byteswritten; Buff += byteswritten;
1364  }
1365 
1366 // All done
1367 //
1368  return Blen;
1369 }
1370 
1371 /******************************************************************************/
1372 
1373 int XrdLinkXeq::TLS_Send(const struct iovec *iov, int iocnt, int bytes)
1374 {
1376  XrdTls::RC retc;
1377  int byteswritten;
1378 
1379 // Get a lock and assume we will be successful (statistically we are). Note
1380 // that the calling interface gauranteed bytes are not zero.
1381 //
1382  isIdle = 0;
1383  AtomicAdd(BytesOut, bytes);
1384 
1385 // Do non-blocking writes if we are setup to do so.
1386 //
1387  if (sendQ) return sendQ->Send(iov, iocnt, bytes);
1388 
1389 // Write the data out.
1390 //
1391  for (int i = 0; i < iocnt; i++)
1392  {ssize_t bytesleft = iov[i].iov_len;
1393  char *Buff = (char *)iov[i].iov_base;
1394  while(bytesleft)
1395  {retc = tlsIO.Write(Buff, bytesleft, byteswritten);
1396  if (retc != XrdTls::TLS_AOK) return TLS_Error("send to", retc);
1397  bytesleft -= byteswritten; Buff += byteswritten;
1398  }
1399  }
1400 
1401 // All done
1402 //
1403  return bytes;
1404 }
1405 
1406 /******************************************************************************/
1407 
1408 int XrdLinkXeq::TLS_Send(const sfVec *sfP, int sfN)
1409 {
1411  int bytes, buffsz, fileFD, retc;
1412  off_t offset;
1413  ssize_t totamt = 0;
1414  char myBuff[65536];
1415 
1416 // Convert the sendfile to a regular send. The conversion is not particularly
1417 // fast and caller are advised to avoid using sendfile on TLS connections.
1418 //
1419  isIdle = 0;
1420  for (int i = 0; i < sfN; sfP++, i++)
1421  {if (!(bytes = sfP->sendsz)) continue;
1422  totamt += bytes;
1423  if (sfP->fdnum < 0)
1424  {if (!TLS_Write(sfP->buffer, bytes)) return -1;
1425  continue;
1426  }
1427  offset = sfP->offset;
1428  fileFD = sfP->fdnum;
1429  buffsz = (bytes < (int)sizeof(myBuff) ? bytes : sizeof(myBuff));
1430  do {do {retc = pread(fileFD, myBuff, buffsz, offset);}
1431  while(retc < 0 && errno == EINTR);
1432  if (retc < 0) return SFError(errno);
1433  if (!retc) break;
1434  if (!TLS_Write(myBuff, buffsz)) return -1;
1435  offset += buffsz; bytes -= buffsz; totamt += retc;
1436  } while(bytes > 0);
1437  }
1438 
1439 // We are done
1440 //
1441  AtomicAdd(BytesOut, totamt);
1442  return totamt;
1443 }
1444 
1445 /******************************************************************************/
1446 /* Protected: T L S _ W r i t e */
1447 /******************************************************************************/
1448 
1449 bool XrdLinkXeq::TLS_Write(const char *Buff, int Blen)
1450 {
1451  XrdTls::RC retc;
1452  int byteswritten;
1453 
1454 // Write the data out
1455 //
1456  while(Blen)
1457  {retc = tlsIO.Write(Buff, Blen, byteswritten);
1458  if (retc != XrdTls::TLS_AOK)
1459  {TLS_Error("write to", retc);
1460  return false;
1461  }
1462  Blen -= byteswritten; Buff += byteswritten;
1463  }
1464 
1465 // All done
1466 //
1467  return true;
1468 }
1469 
1470 /******************************************************************************/
1471 /* v e r T L S */
1472 /******************************************************************************/
1473 
1474 const char *XrdLinkXeq::verTLS()
1475 {
1476  return tlsIO.Version();
1477 }
#define DEBUG(x)
Definition: XrdBwmTrace.hh:54
ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
ssize_t write(int fildes, const void *buf, size_t nbyte)
ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
ssize_t read(int fildes, void *buf, size_t nbyte)
#define close(a)
Definition: XrdPosix.hh:43
#define eMsg(x)
#define AtomicFAZ(x)
#define AtomicBeg(Mtx)
#define AtomicDec(x)
#define AtomicGet(x)
#define AtomicEnd(Mtx)
#define AtomicAdd(x, y)
size_t strlcpy(char *dst, const char *src, size_t sz)
#define TRACEI(act, x)
Definition: XrdTrace.hh:66
const char * Comment
Definition: XrdJob.hh:47
static void SyncAll()
Synchronize statustics for ll links.
Definition: XrdLinkCtl.cc:374
static void Unhook(int fd)
Unhook a link from the active table of links.
Definition: XrdLinkCtl.cc:392
time_t conTime
Definition: XrdLinkInfo.hh:44
void Reset()
Definition: XrdLinkInfo.hh:52
char * Etext
Definition: XrdLinkInfo.hh:45
XrdSysRecMutex opMutex
Definition: XrdLinkInfo.hh:46
XrdSysCondVar * KillcvP
Definition: XrdLinkInfo.hh:42
static const char * TraceID
Definition: XrdLinkXeq.hh:157
int TLS_Send(const char *Buff, int Blen)
Definition: XrdLinkXeq.cc:1342
long long BytesOut
Definition: XrdLinkXeq.hh:172
int TLS_Error(const char *act, XrdTls::RC rc)
Definition: XrdLinkXeq.cc:1175
int TLS_Peek(char *Buff, int Blen, int timeout)
Definition: XrdLinkXeq.cc:1189
int stallCntTot
Definition: XrdLinkXeq.hh:175
int Client(char *buff, int blen)
Definition: XrdLinkXeq.cc:168
char Uname[24]
Definition: XrdLinkXeq.hh:200
XrdTlsPeerCerts * getPeerCerts()
Definition: XrdLinkXeq.cc:337
static int LinkCountMax
Definition: XrdLinkXeq.hh:166
XrdLinkInfo LinkInfo
Definition: XrdLinkXeq.hh:144
XrdProtocol * ProtoAlt
Definition: XrdLinkXeq.hh:184
int Close(bool defer=false)
Definition: XrdLinkXeq.cc:188
XrdNetAddr Addr
Definition: XrdLinkXeq.hh:192
int TLS_Recv(char *Buff, int Blen)
Definition: XrdLinkXeq.cc:1221
int sendData(const char *Buff, int Blen)
Definition: XrdLinkXeq.cc:838
long long BytesInTot
Definition: XrdLinkXeq.hh:171
bool TLS_Write(const char *Buff, int Blen)
Definition: XrdLinkXeq.cc:1449
int SendIOV(const struct iovec *iov, int iocnt, int bytes)
Definition: XrdLinkXeq.cc:861
XrdProtocol * setProtocol(XrdProtocol *pp, bool push)
Definition: XrdLinkXeq.cc:955
static long long LinkCountTot
Definition: XrdLinkXeq.hh:164
long long BytesOutTot
Definition: XrdLinkXeq.hh:173
void Shutdown(bool getLock)
Definition: XrdLinkXeq.cc:1050
int Peek(char *buff, int blen, int timeout=-1)
Definition: XrdLinkXeq.cc:346
static int LinkCount
Definition: XrdLinkXeq.hh:165
void Reset()
Definition: XrdLinkXeq.cc:129
int Backlog()
Definition: XrdLinkXeq.cc:155
XrdSysMutex wrMutex
Definition: XrdLinkXeq.hh:194
static int Stats(char *buff, int blen, bool do_sync=false)
Definition: XrdLinkXeq.cc:1082
XrdSendQ * sendQ
Definition: XrdLinkXeq.hh:195
XrdPollInfo PollInfo
Definition: XrdLinkXeq.hh:145
void setID(const char *userid, int procid)
Definition: XrdLinkXeq.cc:902
bool LockReads
Definition: XrdLinkXeq.hh:197
int Recv(char *buff, int blen)
Definition: XrdLinkXeq.cc:389
static long long LinkBytesIn
Definition: XrdLinkXeq.hh:161
int TLS_RecvAll(char *Buff, int Blen, int timeout)
Definition: XrdLinkXeq.cc:1320
int SFError(int rc)
Definition: XrdLinkXeq.cc:1040
long long BytesIn
Definition: XrdLinkXeq.hh:170
int tardyCntTot
Definition: XrdLinkXeq.hh:177
int Send(const char *buff, int blen)
Definition: XrdLinkXeq.cc:604
XrdSysMutex rdMutex
Definition: XrdLinkXeq.hh:193
const char * verTLS()
Definition: XrdLinkXeq.cc:1474
bool setNB()
Definition: XrdLinkXeq.cc:926
int RecvIOV(const struct iovec *iov, int iocnt)
Definition: XrdLinkXeq.cc:567
char Lname[256]
Definition: XrdLinkXeq.hh:201
static long long LinkConTime
Definition: XrdLinkXeq.hh:163
static int LinkSfIntr
Definition: XrdLinkXeq.hh:169
XrdTlsSocket tlsIO
Definition: XrdLinkXeq.hh:188
void DoIt()
Definition: XrdLinkXeq.cc:308
int RecvAll(char *buff, int blen, int timeout=-1)
Definition: XrdLinkXeq.cc:525
XrdProtocol * Protocol
Definition: XrdLinkXeq.hh:183
bool Register(const char *hName)
Definition: XrdLinkXeq.cc:589
static XrdSysMutex statsMutex
Definition: XrdLinkXeq.hh:179
void setProtName(const char *name)
Definition: XrdLinkXeq.cc:972
static int LinkStalls
Definition: XrdLinkXeq.hh:168
static long long LinkBytesOut
Definition: XrdLinkXeq.hh:162
void syncStats(int *ctime=0)
Definition: XrdLinkXeq.cc:1118
bool setTLS(bool enable, XrdTlsContext *ctx=0)
Definition: XrdLinkXeq.cc:986
static int LinkTimeOuts
Definition: XrdLinkXeq.hh:167
void SetDialect(const char *dP)
Definition: XrdNetAddr.hh:205
void SetTLS(bool val)
Definition: XrdNetAddr.cc:582
void Zorch()
Definition: XrdPollInfo.hh:49
XrdPoll * Poller
Definition: XrdPollInfo.hh:43
virtual int Enable(XrdPollInfo &pInfo)=0
static char * Poll2Text(short events)
Definition: XrdPoll.cc:272
static void Detach(XrdPollInfo &pInfo)
Definition: XrdPoll.cc:177
virtual void Recycle(XrdLink *lp=0, int consec=0, const char *reason=0)=0
virtual int Stats(char *buff, int blen, int do_sync=0)=0
virtual int Process(XrdLink *lp)=0
void Terminate(XrdLink *lP=0)
Definition: XrdSendQ.cc:396
int Send(const char *buff, int blen)
Definition: XrdSendQ.cc:230
unsigned int Backlog()
Definition: XrdSendQ.hh:46
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
void Lock(XrdSysMutex *Mutex)
int fd
Socket file descriptor.
Definition: XrdTcpMonPin.hh:61
long long bytesOut
Bytes written to the socket.
Definition: XrdTcpMonPin.hh:64
int consec
Seconds connected.
Definition: XrdTcpMonPin.hh:62
virtual void Monitor(XrdNetAddrInfo &netInfo, LinkInfo &lnkInfo, int liLen)=0
long long bytesIn
Bytes read from the socket.
Definition: XrdTcpMonPin.hh:63
const char * tident
Pointer to the client's trace identifier.
Definition: XrdTcpMonPin.hh:60
@ TLS_HS_BLOCK
Always block during handshake.
Definition: XrdTlsSocket.hh:53
XrdTls::RC Accept(std::string *eMsg=0)
void Shutdown(SDType=sdImmed)
@ TLS_RBL_WBL
blocking read blocking write
Definition: XrdTlsSocket.hh:48
XrdTls::RC Write(const char *buffer, size_t size, int &bytesOut)
const char * Version()
XrdTls::RC Read(char *buffer, size_t size, int &bytesRead)
Read from the TLS connection. If necessary, a handshake will be done.
const char * Init(XrdTlsContext &ctx, int sfd, RW_Mode rwm, HS_Mode hsm, bool isClient, bool serial=true, const char *tid="")
void SetTraceID(const char *tid)
int Pending(bool any=true)
XrdTls::RC Peek(char *buffer, size_t size, int &bytesPeek)
XrdTlsPeerCerts * getCerts(bool ver=true)
static std::string RC2Text(XrdTls::RC rc, bool dbg=false)
Definition: XrdTls.cc:127
@ TLS_AOK
All went well, will always be zero.
Definition: XrdTls.hh:40
XrdTlsContext * tlsCtx
Definition: XrdGlobals.cc:52
XrdTcpMonPin * TcpMonPin
Definition: XrdLinkXeq.cc:96
XrdSysError Log
Definition: XrdConfig.cc:111
XrdScheduler Sched
Definition: XrdLinkCtl.cc:54
int maxIOV
Definition: XrdLinkXeq.cc:98
int devNull
Definition: XrdGlobals.cc:55
int fdnum
File descriptor for data.
Definition: XrdOucSFVec.hh:47
int sendsz
Length of data at offset.
Definition: XrdOucSFVec.hh:46