25 #include "XrdVersion.hh"
31 #include "XrdSec/XrdSecInterface.hh"
35 # include <sys/socket.h>
37 #include <sys/types.h>
47 #define URLTAG "["<<fUrl.Host<<":"<<fUrl.Port<<"]"
53 XrdProofPhyConn::XrdProofPhyConn(
const char *url,
int psid,
char capver,
54 XrdClientAbsUnsolMsgHandler *uh,
bool tcp,
int fd)
55 : XrdProofConn(0,
'i', psid, capver, uh)
57 XPDLOC(ALL,
"PhyConn")
62 fMutex = new XrdSysRecMutex();
65 if (url && !Init(url, fd)) {
66 TRACE(XERR,
"severe error occurred while"
67 " opening a connection" <<
" to server "<<URLTAG);
75 bool XrdProofPhyConn::Init(
const char *url,
int fd)
77 XPDLOC(ALL,
"PhyConn::Init")
80 fUrl.TakeUrl(XrdOucString(url));
83 fUser = fUrl.User.c_str();
84 if (fUser.length() <= 0) {
87 struct passwd *pw = getpwuid(getuid());
88 fUser = pw ? pw->pw_name :
"";
91 DWORD length =
sizeof (lname);
92 ::GetUserName(lname, &length);
99 char *hn = XrdSysDNS::getHostName(((fUrl.Host.length() > 0) ?
100 fUrl.Host.c_str() :
"localhost"));
108 fHost = fUrl.Host.c_str();
112 struct servent *sent = getservbyname(
"proofd",
"tcp");
114 TRACE(XERR,
"service 'proofd' not found by getservbyname" <<
115 ": using default IANA assigned tcp port 1093");
118 fPort = (int)ntohs(sent->s_port);
121 TRACE(XERR,
"getservbyname found tcp port " << fPort <<
122 " for service 'proofd'");
137 void XrdProofPhyConn::Connect(
int fd)
139 XPDLOC(ALL,
"PhyConn::Connect")
141 int maxTry = -1, timeWait = -1;
143 XrdProofConn::GetRetryParam(maxTry, timeWait);
144 maxTry = (maxTry > -1) ? maxTry : EnvGetLong(NAME_FIRSTCONNECTMAXCNT);
145 timeWait = (timeWait > -1) ? timeWait : EnvGetLong(NAME_CONNECTTIMEOUT);
149 for (; (i < maxTry) && (!fConnected); i++) {
152 logid = TryConnect(fd);
159 TRACE(DBG,
"new logical connection ID: "<<logid);
162 if (!GetAccessToSrv()) {
163 if (fLastErr == kXR_NotAuthorized) {
166 XrdOucString msg = fLastErrMsg;
167 msg.erase(msg.rfind(
":"));
168 TRACE(XERR,
"authentication failure: " << msg);
171 TRACE(XERR,
"access to server failed (" << fLastErrMsg <<
")");
177 TRACE(DBG,
"access to server granted.");
183 TRACE(DBG,
"disconnecting");
187 TRACE(DBG,
"connection attempt failed: sleep " << timeWait <<
" secs");
191 Sleep(timeWait * 1000);
200 int XrdProofPhyConn::TryConnect(
int fd)
202 XPDLOC(ALL,
"PhyConn::TryConnect")
204 const
char *ctype[2] = {
"UNIX",
"TCP"};
207 fPhyConn =
new XrdClientPhyConnection(
this, 0);
210 bool isUnix = (fTcp) ? 0 : 1;
211 #if ROOTXRDVERS <= ROOT_PhyConnNoReuse
213 TRACE(XERR,
"Reusing an existing connection (descriptor "<<fd<<
214 ") not supported by the xroot client version (requires xrootd >= 3.0.3)");
219 if (!(fPhyConn->Connect(fUrl, isUnix))) {
221 if (!(fPhyConn->Connect(fUrl, isUnix, fd))) {
223 TRACE(XERR,
"creating "<<ctype[fTcp]<<
" connection to "<<URLTAG);
228 TRACE(DBG, ctype[fTcp]<<
"-connected to "<<URLTAG);
236 SetAsync(fUnsolMsgHandler);
245 void XrdProofPhyConn::Close(
const char *)
253 fPhyConn->Disconnect();
265 void XrdProofPhyConn::SetAsync(XrdClientAbsUnsolMsgHandler *uh,
266 XrdProofConnSender_t,
void *)
269 fPhyConn->UnsolicitedMsgHandler = uh;
275 XrdClientMessage *XrdProofPhyConn::ReadMsg()
277 return (fPhyConn ? fPhyConn->ReadMessage(fStreamid) : (XrdClientMessage *)0);
284 bool XrdProofPhyConn::GetAccessToSrv(XrdClientPhyConnection *)
286 XPDLOC(ALL,
"PhyConn::GetAccessToSrv")
289 { XrdClientPhyConnLocker pcl(fPhyConn);
290 fServerType = DoHandShake();
293 switch (fServerType) {
296 TRACE(DBG,
"found server at "<<URLTAG);
299 fPhyConn->StartReader();
300 fPhyConn->fServerType = kSTBaseXrootd;
304 TRACE(XERR,
"handshake failed with server "<<URLTAG);
311 TRACE(XERR,
"server at "<<URLTAG<<
" is unknown : protocol error");
317 if (fPhyConn->IsLogged() != kNo) {
318 TRACE(XERR,
"client already logged-in at "<<URLTAG<<
" (!): protocol error!");
330 int XrdProofPhyConn::WriteRaw(
const void *buf,
int len, XrdClientPhyConnection *)
333 return fPhyConn->WriteRaw(buf, len);
342 int XrdProofPhyConn::ReadRaw(
void *buf,
int len, XrdClientPhyConnection *)
345 return fPhyConn->ReadRaw(buf, len);