39#if defined(__linux__) || defined(__GNU__)
40#include <netinet/tcp.h>
48#if defined(__solaris__) || defined(__linux__) || defined(__GNU__)
49#include <sys/sendfile.h>
81 if ((maxiov = sysconf(_SC_IOV_MAX)) > 0)
return maxiov;
132 strcpy(
Lname,
"somewhere");
174 if (nbsz <= 0)
return 0;
176 if ((ulen +
HNlen) >= nbsz) ulen = 0;
177 else {strncpy(nbuf,
ID, ulen);
190 int csec, fd, rc = 0;
297 if (fd >= 2) {
if (
KeepFD) rc = 0;
298 else rc = (
close(fd) < 0 ? errno : 0);
300 if (rc)
Log.
Emsg(
"Link", rc,
"close",
ID);
321 else {
Log.
Emsg(
"Link",
"Dispatch on closed link",
ID);
330 else if (rc != -EINPROGRESS)
Close();
349 struct pollfd polltab = {
PollInfo.
FD, POLLIN|POLLRDNORM, 0};
360 do {retc = poll(&polltab, 1, timeout);}
while(retc < 0 && errno == EINTR);
362 {
if (retc == 0)
return 0;
363 return Log.
Emsg(
"Link", -errno,
"poll",
ID);
368 if (!(polltab.revents & (POLLIN|POLLRDNORM)))
375 do {mlen = recv(
LinkInfo.
FD, Buff, Blen, MSG_PEEK);}
376 while(mlen < 0 && errno == EINTR);
380 if (mlen >= 0)
return int(mlen);
398 do {rlen =
read(
LinkInfo.
FD, Buff, Blen);}
while(rlen < 0 && errno == EINTR);
402 if (rlen >= 0)
return int(rlen);
412 struct pollfd polltab = {
PollInfo.
FD, POLLIN|POLLRDNORM, 0};
413 ssize_t rlen, totlen = 0;
424 {
do {retc = poll(&polltab,1,timeout);}
while(retc < 0 && errno == EINTR);
439 if (!(polltab.revents & (POLLIN|POLLRDNORM)))
449 while(rlen < 0 && errno == EINTR);
451 {
if (!rlen)
return -ENOMSG;
455 totlen += rlen; Blen -= rlen; Buff += rlen;
467 struct pollfd polltab = {
PollInfo.
FD, POLLIN|POLLRDNORM, 0};
477 do {retc = poll(&polltab,1,timeout);}
while(retc < 0 && errno == EINTR);
488 if (!(polltab.revents & (POLLIN|POLLRDNORM)))
504 int seglen, segcnt =
maxIOV, totlen = 0;
506 for (
int i = 0; i < segcnt; i++) seglen +=
iov[i].iov_len;
507 if ((rlen =
RecvIOV(
iov, segcnt)) < 0)
return rlen;
509 if (rlen < seglen)
break;
512 if (iocnt <=
maxIOV) segcnt = iocnt;
527 struct pollfd polltab = {
PollInfo.
FD, POLLIN|POLLRDNORM, 0};
535 {
do {retc = poll(&polltab,1,timeout);}
while(retc < 0 && errno == EINTR);
537 {
if (!retc)
return -ETIMEDOUT;
541 if (!(polltab.revents & (POLLIN|POLLRDNORM)))
551 do {rlen = recv(
LinkInfo.
FD, Buff, Blen, MSG_WAITALL);}
552 while(rlen < 0 && errno == EINTR);
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);
577 while(retc < 0 && errno == EINTR);
581 if (retc < 0)
Log.
Emsg(
"Link", errno,
"receive from",
ID);
610 ssize_t retc = 0, bytesleft = Blen;
630 {
if (errno == EINTR)
continue;
633 bytesleft -= retc; Buff += retc;
639 if (retc >= 0)
return Blen;
652 maxIOV = sysconf(_SC_IOV_MAX);
686 int seglen, segcnt =
maxIOV, iolen = 0;
688 for (
int i = 0; i < segcnt; i++) seglen +=
iov[i].iov_len;
689 if ((retc =
SendIOV(
iov, segcnt, seglen)) < 0)
696 if (iocnt <=
maxIOV) segcnt = iocnt;
709#if !defined(HAVE_SENDFILE)
713#elif defined(__solaris__)
716 size_t xframt, totamt, bytes = 0;
722 for (i = 0; i < sfN; sfP++, i++)
724 {vecSF[i].sfv_fd = SFV_FD_SELF;
725 vecSF[i].sfv_off = (off_t)sfP->buffer;
727 vecSF[i].sfv_fd = sfP->
fdnum;
728 vecSF[i].sfv_off = sfP->offset;
730 vecSF[i].sfv_flag = 0;
731 vecSF[i].sfv_len = sfP->
sendsz;
742do{retc = sendfilev(
LinkInfo.
FD, vecSFP, sfN, &xframt);
754 if (retc < 0 && errno != EINTR)
break;
760 while(xframt > 0 && sfN)
761 {
if ((ssize_t)xframt < (ssize_t)vecSFP->sfv_len)
762 {vecSFP->sfv_off += xframt; vecSFP->sfv_len -= xframt;
break;}
763 xframt -= vecSFP->sfv_len; vecSFP++; sfN--;
770 retc = (retc < 0 ? errno : ECANCELED);
772 Log.
Emsg(
"Link", retc,
"send file to",
ID);
775#elif defined(__linux__) || defined(__GNU__)
777 static const int setON = 1, setOFF = 0;
778 ssize_t retc = 0, bytesleft;
780 int i, xfrbytes = 0, uncork = 1, xIntr = 0;
790 if (setsockopt(
PollInfo.
FD, SOL_TCP, TCP_CORK, &setON,
sizeof(setON)) < 0)
791 {
Log.
Emsg(
"Link", errno,
"cork socket for",
ID);
792 uncork = 0;
sfOK = 0;
797 for (i = 0; i < sfN; sfP++, i++)
799 else {myOffset = sfP->offset; bytesleft = sfP->
sendsz;
802 {bytesleft -= retc; xIntr++;}
804 if (retc < 0 && errno == EINTR)
continue;
805 if (retc <= 0)
break;
812 {
if (retc == 0) errno = ECANCELED;
814 Log.
Emsg(
"Link", errno,
"send file to",
ID);
821 && setsockopt(
PollInfo.
FD, SOL_TCP, TCP_CORK, &setOFF,
sizeof(setOFF)) < 0)
822 Log.
Emsg(
"Link", errno,
"uncork socket for",
ID);
826 if (xIntr > sfN)
SfIntr += (xIntr - sfN);
844 ssize_t retc = 0, bytesleft = Blen;
850 {
if (errno == EINTR)
continue;
853 bytesleft -= retc; Buff += retc;
867 ssize_t bytesleft, n, retc = 0;
877 bytesleft =
static_cast<ssize_t
>(bytes);
880 while(retc < 0 && errno == EINTR);
881 if (retc >= bytesleft || retc < 0)
break;
883 while(retc >= (n =
static_cast<ssize_t
>(
iov->iov_len)))
884 {retc -= n;
iov++; iocnt--;}
885 Buff = (
const char *)
iov->iov_base + retc; n -= retc;
iov++; iocnt--;
887 {
if (errno == EINTR)
continue;
890 n -= retc; Buff += retc; bytesleft -= retc;
892 if (retc < 0 || iocnt < 1)
break;
897 if (retc >= 0)
return bytes;
908 char buff[
sizeof(
Uname)], *bp, *sp;
911 snprintf(buff,
sizeof(buff),
"%s.%d:%d", userid, procid,
PollInfo.
FD);
913 sp = buff + ulen - 1;
915 if (ulen > (
int)
sizeof(
Uname)) ulen =
sizeof(
Uname);
917 while(ulen--) {*bp = *sp; bp--; sp--;}
934#if !defined(__linux__)
1001 if (
isTLS == enable)
return true;
1020 snprintf(buff,
sizeof(buff),
"Unable to enable tls for %s;",
ID);
1021 Log.
Emsg(
"LinkXeq", buff, eNote);
1033 else {
isTLS = enable;
1046 Log.
Emsg(
"TLS", rc,
"send file to",
ID);
1060 TRACEI(
DEBUG, (getLock ?
"Async" :
"Sync") <<
" link shutdown in progress");
1073 Log.
Emsg(
"Link", errno,
"shutdown FD for",
ID);
1088 static const char statfmt[] =
"<stats id=\"link\"><num>%d</num>"
1089 "<maxn>%d</maxn><tot>%lld</tot><in>%lld</in><out>%lld</out>"
1090 "<ctime>%lld</ctime><tmo>%d</tmo><stall>%d</stall>"
1091 "<sfps>%d</sfps></stats>";
1096 if (!buff)
return sizeof(statfmt)+17*6;
1184 snprintf(msg,
sizeof(msg),
"Unable to %s %s;", act,
ID);
1185 Log.
Emsg(
"TLS", msg, reason.c_str());
1208 if (rc < 1)
return rc;
1251 int pend, rlen, totlen = 0;
1264 {
if (pend < 0)
return -1;
1280 {
if (!totlen)
return -ENOMSG;
1284 if (rlen <= 0)
break;
1285 totlen += rlen; Blen -= rlen; Buff += rlen;
1298 int Blen, rlen, totlen = 0;
1307 for (
int i = 0; i < iocnt; i++)
1308 {Buff = (
char *)
iov[i].iov_base;
1309 Blen =
iov[i].iov_len;
1310 rlen =
TLS_Recv(Buff, Blen, timeout,
true);
1311 if (rlen <= 0)
break;
1313 if (rlen < Blen)
break;
1334 if (retc < 1)
return (retc ? -1 : -ETIMEDOUT);
1349 ssize_t bytesleft = Blen;
1365 {retc =
tlsIO.
Write(Buff, bytesleft, byteswritten);
1367 bytesleft -= byteswritten; Buff += byteswritten;
1395 for (
int i = 0; i < iocnt; i++)
1396 {ssize_t bytesleft =
iov[i].iov_len;
1397 char *Buff = (
char *)
iov[i].iov_base;
1399 {retc =
tlsIO.
Write(Buff, bytesleft, byteswritten);
1401 bytesleft -= byteswritten; Buff += byteswritten;
1415 int bytes, buffsz, fileFD, retc;
1424 for (
int i = 0; i < sfN; sfP++, i++)
1425 {
if (!(bytes = sfP->
sendsz))
continue;
1428 {
if (!
TLS_Write(sfP->buffer, bytes))
return -1;
1431 offset = sfP->offset;
1432 fileFD = sfP->
fdnum;
1433 buffsz = (bytes < (int)
sizeof(myBuff) ? bytes :
sizeof(myBuff));
1434 do {
do {retc =
pread(fileFD, myBuff, buffsz, offset);}
1435 while(retc < 0 && errno == EINTR);
1436 if (retc < 0)
return SFError(errno);
1438 if (!
TLS_Write(myBuff, buffsz))
return -1;
1439 offset += buffsz; bytes -= buffsz; totamt += retc;
1461 {retc =
tlsIO.
Write(Buff, Blen, byteswritten);
1466 Blen -= byteswritten; Buff += byteswritten;
#define pread(a, b, c, d)
static void SyncAll()
Synchronize statustics for ll links.
static void Unhook(int fd)
Unhook a link from the active table of links.
static const char * TraceID
int TLS_Send(const char *Buff, int Blen)
int TLS_Error(const char *act, XrdTls::RC rc)
int TLS_Peek(char *Buff, int Blen, int timeout)
int Client(char *buff, int blen)
XrdTlsPeerCerts * getPeerCerts()
int Close(bool defer=false)
int TLS_Recv(char *Buff, int Blen)
int sendData(const char *Buff, int Blen)
bool TLS_Write(const char *Buff, int Blen)
int SendIOV(const struct iovec *iov, int iocnt, int bytes)
XrdProtocol * setProtocol(XrdProtocol *pp, bool push)
static long long LinkCountTot
void Shutdown(bool getLock)
int Peek(char *buff, int blen, int timeout=-1)
static int Stats(char *buff, int blen, bool do_sync=false)
void setID(const char *userid, int procid)
int Recv(char *buff, int blen)
static long long LinkBytesIn
int TLS_RecvAll(char *Buff, int Blen, int timeout)
int Send(const char *buff, int blen)
int RecvIOV(const struct iovec *iov, int iocnt)
static long long LinkConTime
int RecvAll(char *buff, int blen, int timeout=-1)
bool Register(const char *hName)
static XrdSysMutex statsMutex
void setProtName(const char *name)
static long long LinkBytesOut
void syncStats(int *ctime=0)
bool setTLS(bool enable, XrdTlsContext *ctx=0)
void Serialize()
Wait for all outstanding requests to be completed on the link.
int Wait4Data(int timeout)
char * ID
Pointer to the client's link identity.
void SetDialect(const char *dP)
bool Register(const char *hName)
virtual int Enable(XrdPollInfo &pInfo)=0
static char * Poll2Text(short events)
static void Detach(XrdPollInfo &pInfo)
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)
int Send(const char *buff, int blen)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
void Lock(XrdSysMutex *Mutex)
int fd
Socket file descriptor.
long long bytesOut
Bytes written to the socket.
int consec
Seconds connected.
virtual void Monitor(XrdNetAddrInfo &netInfo, LinkInfo &lnkInfo, int liLen)=0
long long bytesIn
Bytes read from the socket.
const char * tident
Pointer to the client's trace identifier.
@ TLS_HS_BLOCK
Always block during handshake.
XrdTls::RC Accept(std::string *eMsg=0)
void Shutdown(SDType=sdImmed)
@ TLS_RBL_WBL
blocking read blocking write
XrdTls::RC Write(const char *buffer, size_t size, int &bytesOut)
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)
@ TLS_AOK
All went well, will always be zero.
int fdnum
File descriptor for data.
int sendsz
Length of data at offset.