00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022
00023 #include "Bytes.h"
00024 #include "TSocket.h"
00025 #include "TString.h"
00026 #include "TSystem.h"
00027 #include "TUrl.h"
00028
00029 #include "redirguard.h"
00030 #include "pq2ping.h"
00031
00032
00033
00034 typedef struct {
00035 int first;
00036 int second;
00037 int third;
00038 int fourth;
00039 int fifth;
00040 } clnt_HS_t;
00041
00042 typedef struct {
00043 int msglen;
00044 int protover;
00045 int msgval;
00046 } srv_HS_t;
00047
00048
00049 TUrl gUrl;
00050 Bool_t gIsProof = kFALSE;
00051
00052
00053 extern Int_t gverbose;
00054
00055
00056 Int_t checkUrl(const char *url, const char *flog, bool def_proof)
00057 {
00058
00059
00060
00061
00062
00063
00064 gIsProof = kFALSE;
00065 gUrl.SetUrl(url);
00066 TString protocol(gUrl.GetProtocol());
00067 if (protocol == "root" || protocol == "xroot") {
00068
00069 if (pingXrootdAt() != 0) {
00070 Printf("checkUrl: specified URL does not identifies a running (x)rootd server: %s", url);
00071 return -1;
00072 }
00073 } else if (protocol == "proof") {
00074
00075 if (pingXproofdAt() != 0) {
00076 Printf("checkUrl: specified URL does not identifies a running PROOF master: %s", url);
00077 return -1;
00078 }
00079 gIsProof = kTRUE;
00080
00081 gUrl.SetOptions("N");
00082 } else {
00083 Int_t rc = -1;
00084 if (def_proof) {
00085
00086 { redirguard rog(flog, "a", 0);
00087 if ((rc = pingXproofdAt()) == 0)
00088 gIsProof = kTRUE;
00089 }
00090 if (rc != 0) {
00091
00092 if (pingServerAt() != 0) {
00093 Printf("checkUrl: specified URL does not identifies a valid PROOF or data server: %s", url);
00094 return -1;
00095 }
00096 }
00097 } else {
00098
00099 { redirguard rog(flog, "a", 0);
00100 rc = pingServerAt();
00101 }
00102 if (rc != 0) {
00103
00104 if (pingXproofdAt() != 0) {
00105 Printf("checkUrl: specified URL does not identifies a valid data or PROOF server: %s", url);
00106 return -1;
00107 }
00108 gIsProof = kTRUE;
00109 }
00110 }
00111 }
00112 if (gverbose > 0) Printf("checkUrl: %s service", (gIsProof ? "PROOF" : "Data"));
00113
00114
00115 return ((gIsProof) ? 1 : 0);
00116 }
00117
00118
00119 Int_t pingXrootdAt()
00120 {
00121
00122
00123
00124
00125
00126
00127 Int_t port = gUrl.GetPort();
00128 const char *host = gUrl.GetHost();
00129
00130
00131 TSocket s(host, port);
00132 if (!(s.IsValid())) {
00133 if (gDebug > 0)
00134 Printf("pingXrootdAt: could not open connection to %s:%d", host, port);
00135 return -1;
00136 }
00137
00138 clnt_HS_t initHS;
00139 memset(&initHS, 0, sizeof(initHS));
00140 initHS.fourth = host2net((int)4);
00141 initHS.fifth = host2net((int)2012);
00142 int len = sizeof(initHS);
00143 s.SendRaw(&initHS, len);
00144
00145 int type;
00146 len = sizeof(type);
00147 int readCount = s.RecvRaw(&type, len);
00148 if (readCount != len) {
00149 if (gDebug > 0)
00150 Printf("pingXrootdAt: 1st: wrong number of bytes read: %d (expected: %d)",
00151 readCount, len);
00152 return 1;
00153 }
00154
00155 type = net2host(type);
00156
00157 if (type == 0) {
00158 srv_HS_t xbody;
00159 len = sizeof(xbody);
00160 readCount = s.RecvRaw(&xbody, len);
00161 if (readCount != len) {
00162 if (gDebug > 0)
00163 Printf("pingXrootdAt: 2nd: wrong number of bytes read: %d (expected: %d)",
00164 readCount, len);
00165 return 1;
00166 }
00167
00168 } else if (type == 8) {
00169
00170 if (gDebug > 0)
00171 Printf("pingXrootdAt: server is ROOTD");
00172 return 1;
00173 } else {
00174
00175 if (gDebug > 0)
00176 Printf("pingXrootdAt: unknown server type: %d", type);
00177 return 1;
00178 }
00179
00180 return 0;
00181 }
00182
00183
00184 Int_t pingXproofdAt()
00185 {
00186
00187
00188
00189
00190
00191
00192 Int_t port = gUrl.GetPort();
00193 const char *host = gUrl.GetHost();
00194
00195
00196 TSocket s(host, port);
00197 if (!(s.IsValid())) {
00198 if (gDebug > 0)
00199 Printf("pingXproofdAt: could not open connection to %s:%d", host, port);
00200 return -1;
00201 }
00202
00203 clnt_HS_t initHS;
00204 memset(&initHS, 0, sizeof(initHS));
00205 initHS.third = (int)host2net((int)1);
00206 int len = sizeof(initHS);
00207 s.SendRaw(&initHS, len);
00208
00209 int dum[2];
00210 dum[0] = (int)host2net((int)4);
00211 dum[1] = (int)host2net((int)2012);
00212 s.SendRaw(&dum[0], sizeof(dum));
00213
00214 int type;
00215 len = sizeof(type);
00216 int readCount = s.RecvRaw(&type, len);
00217 if (readCount != len) {
00218 if (gDebug > 0)
00219 Printf("pingXproofdAt: 1st: wrong number of bytes read: %d (expected: %d)",
00220 readCount, len);
00221 return 1;
00222 }
00223
00224 type = net2host(type);
00225
00226 if (type == 0) {
00227 srv_HS_t xbody;
00228 len = sizeof(xbody);
00229 readCount = s.RecvRaw(&xbody, len);
00230 if (readCount != len) {
00231 if (gDebug > 0)
00232 Printf("pingXproofdAt: 2nd: wrong number of bytes read: %d (expected: %d)",
00233 readCount, len);
00234 return 1;
00235 }
00236 xbody.protover = net2host(xbody.protover);
00237 xbody.msgval = net2host(xbody.msglen);
00238 xbody.msglen = net2host(xbody.msgval);
00239
00240 } else if (type == 8) {
00241
00242 if (gDebug > 0)
00243 Printf("pingXproofdAt: server is PROOFD");
00244 return 1;
00245 } else {
00246
00247 if (gDebug > 0)
00248 Printf("pingXproofdAt: unknown server type: %d", type);
00249 return 1;
00250 }
00251
00252 return 0;
00253 }
00254
00255
00256 Int_t pingServerAt()
00257 {
00258
00259
00260
00261
00262
00263
00264 Int_t rc = -1;
00265 FileStat_t st;
00266 if (gSystem->GetPathInfo(gUrl.GetUrl(), st) == 0) {
00267 rc = 1;
00268 if (R_ISDIR(st.fMode)) rc = 0;
00269 }
00270
00271
00272 return rc;
00273 }