XRootD
XrdOucTPC.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O u c T P C . c c */
4 /* */
5 /* (c) 2012 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 //#include <iostream>
32 #include <cstdio>
33 #include <cstring>
34 #include <strings.h>
35 
36 #include "XrdNet/XrdNetAddr.hh"
37 #include "XrdOuc/XrdOucTPC.hh"
38 
39 /******************************************************************************/
40 /* S t a t i c V a r i a b l e s */
41 /******************************************************************************/
42 
43 const char *XrdOucTPC::tpcCks = "tpc.cks";
44 const char *XrdOucTPC::tpcDlg = "tpc.dlg";
45 const char *XrdOucTPC::tpcDst = "tpc.dst";
46 const char *XrdOucTPC::tpcKey = "tpc.key";
47 const char *XrdOucTPC::tpcLfn = "tpc.lfn";
48 const char *XrdOucTPC::tpcOrg = "tpc.org";
49 const char *XrdOucTPC::tpcPsh = "tpc.psh";
50 const char *XrdOucTPC::tpcSgi = "tpc.scgi";
51 const char *XrdOucTPC::tpcSrc = "tpc.src";
52 const char *XrdOucTPC::tpcSpr = "tpc.spr";
53 const char *XrdOucTPC::tpcStr = "tpc.str";
54 const char *XrdOucTPC::tpcTpr = "tpc.tpr";
55 const char *XrdOucTPC::tpcTtl = "tpc.ttl";
56 const char *XrdOucTPC::tpcDlgOn = "tpc.dlgon";
57 
58 /******************************************************************************/
59 /* c g i C 2 D s t */
60 /******************************************************************************/
61 
62 const char *XrdOucTPC::cgiC2Dst(const char *cKey, const char *xSrc,
63  const char *xLfn, const char *xCks,
64  char *Buff, int Blen, int strms,
65  const char *iHst,
66  const char *sprt, const char *tprt,
67  bool dlgon,
68  bool push)
69 {
70  tpcInfo Info;
71  char *bP = Buff;
72  int n;
73 
74 // Make sure we have the minimum amount of information here
75 //
76  if (!cKey || !xSrc || Blen <= 0) return "!Invalid cgi parameters.";
77 
78 // Generate the full name of the source
79 //
80  if (!cgiHost(Info, xSrc)) return "!Invalid source specification.";
81 
82 // Construct the cgi string. For the destination we need the full source spec
83 //
84  n = snprintf(bP, Blen, "%s=%s&%s=%s%s%s", tpcKey, cKey, tpcSrc,
85  Info.uName, Info.hName, Info.pName);
86  if (xLfn)
87  {bP += n; Blen -= n;
88  if (Blen > 1) n = snprintf(bP, Blen, "&%s=%s", tpcLfn, xLfn);
89  }
90  if (xCks)
91  {bP += n; Blen -= n;
92  if (Blen > 1) n = snprintf(bP, Blen, "&%s=%s", tpcCks, xCks);
93  }
94 
95  if (strms > 0)
96  {bP += n; Blen -= n;
97  if (Blen > 1) n = snprintf(bP, Blen, "&%s=%d", tpcStr, strms);
98  }
99 
100  if (iHst)
101  {bP += n; Blen -= n;
102  if (Blen > 1) n = snprintf(bP, Blen, "&%s=%s", tpcDlg, iHst);
103  }
104 
105  if (sprt)
106  {bP += n; Blen -= n;
107  if (Blen > 1) n = snprintf(bP, Blen, "&%s=%s", tpcSpr, sprt);
108  }
109 
110  if (tprt)
111  {bP += n; Blen -= n;
112  if (Blen > 1) n = snprintf(bP, Blen, "&%s=%s", tpcTpr, tprt);
113  }
114 
115  if (push)
116  {bP += n; Blen -= n;
117  if (Blen > 1) n = snprintf(bP, Blen, "&%s=1", tpcPsh);
118  }
119 
120  // tpc.dlgon
121  {bP += n; Blen -= n;
122  const char* cgistr = dlgon ? "&%s=1" : "&%s=0";
123  if (Blen > 1) n = snprintf(bP, Blen, cgistr, tpcDlgOn);
124  }
125 
126 
127 // All done
128 //
129  return (n > Blen ? "!Unable to generate full cgi." : Buff);
130 }
131 
132 /******************************************************************************/
133 /* c g i C 2 S r c */
134 /******************************************************************************/
135 
136 const char *XrdOucTPC::cgiC2Src(const char *cKey, const char *xDst, int xTTL,
137  char *Buff, int Blen)
138 {
139  tpcInfo Info;
140  char *bP = Buff;
141  int n;
142 
143 // Make sure we have the minimum amount of information here
144 //
145  if (!cKey || !xDst || Blen <= 0) return "!Invalid cgi parameters.";
146 
147 // Generate the full name of the source
148 //
149  if (!cgiHost(Info, xDst)) return "!Invalid destination specification.";
150 
151 // Construct the cgi string. The source needs only the dest hostname.
152 //
153  n = snprintf(Buff, Blen, "%s=%s&%s=%s", tpcKey, cKey, tpcDst, Info.hName);
154  if (xTTL >= 0)
155  {bP += n; Blen -= n;
156  if (Blen > 1) n = snprintf(bP, Blen, "&%s=%d", tpcTtl, xTTL);
157  }
158 
159 // All done
160 //
161  return (n > Blen ? "!Unable to generate full cgi." : Buff);
162 }
163 
164 /******************************************************************************/
165 /* c g i D 2 S r c */
166 /******************************************************************************/
167 
168 const char *XrdOucTPC::cgiD2Src(const char *cKey, const char *cOrg,
169  char *Buff, int Blen)
170 {
171  int n;
172 
173 // Make sure we have the minimum amount of information here
174 //
175  if (!cKey || !cOrg || Blen <= 0) return "!Invalid cgi parameters.";
176 
177 // Construct the cgi string
178 //
179  n = snprintf(Buff, Blen, "%s=%s&%s=%s", tpcKey, cKey, tpcOrg, cOrg);
180 
181 // All done
182 //
183  return (n > Blen ? "!Unable to generate full cgi." : Buff);
184 }
185 
186 /******************************************************************************/
187 /* c g i H o s t */
188 /******************************************************************************/
189 
190 bool XrdOucTPC::cgiHost(tpcInfo &Info, const char *hSpec)
191 {
192  const char *Colon, *hName;
193  XrdNetAddr hAddr;
194  char hBuff[256];
195  int n;
196 
197 // Extract out the username, if any
198 //
199  if (!(hName = index(hSpec, '@'))) hName = hSpec;
200  else {hName ++;
201  n = hName - hSpec;
202  if (n >= int(sizeof(Info.User))) return false;
203  Info.uName = Info.User;
204  strncpy(Info.User, hSpec, n); Info.User[n] = 0;
205  }
206 
207 // Preaccomodate ipv6 addresses
208 //
209  if (*hName != '[') Colon = hName;
210  else if (!(Colon = index(hName, ']'))) return 0;
211 
212 
213 // Extract out the port specification, if any.
214 //
215  if ((Colon = index(Colon, ':')))
216  {n = Colon - hName;
217  if (n >= int(sizeof(hBuff))) return false;
218  Info.pName = Colon;
219  strncpy(hBuff, hName, n); hBuff[n] = 0; hName = hBuff;
220  }
221 
222 // Resolve the host name
223 //
224  hAddr.Set(hName,0);
225  if ((hName = hAddr.Name())) Info.hName = strdup(hName);
226  return hName != 0;
227 }
228 
229 /******************************************************************************/
230 /* c o p y C G I */
231 /******************************************************************************/
232 
233 int XrdOucTPC::copyCGI(const char *cgi, char *Buff, int Blen)
234 {
235  const char *bgi;
236  char *bP = Buff;
237  int xlen;
238  bool eqs;
239 
240  if (!cgi) {*Buff = 0; return 0;}
241 
242 // Skip over initial ampersands
243 //
244  while(*cgi == '&' && *cgi) cgi++;
245 
246 // Check if there is anything here
247 //
248  if (!*cgi) {*Buff = 0; return 0;}
249  Blen--;
250 
251 // Copy all keys except system oriented ones.
252 //
253 //std::cerr <<"TPC cgi IN: " <<cgi <<'\n' <<std::flush;
254 do{bgi = cgi; eqs = false;
255  while(*cgi != '\t' && *cgi)
256  {if (*cgi == '=') eqs = true;
257  cgi++;
258  }
259  if (*bgi && eqs && strncmp(bgi, "tpc.", 4) && strncmp(bgi, "xrd.", 4)
260  && strncmp(bgi, "xrdcl.", 4))
261  {xlen = cgi - bgi;
262  if (bP != Buff && Blen > 0) {*bP++ = '&'; Blen--;}
263  if (xlen > Blen) xlen = Blen;
264  strncpy(bP, bgi, xlen);
265  bP += xlen;
266  Blen -= xlen;
267  }
268  while(*cgi && *cgi == '\t') cgi++;
269  } while(*cgi && Blen > 2);
270 
271 // Compute length and return
272 //
273  *bP = 0;
274 // std::cerr <<"TPC cgi OT: " <<Buff <<" len=" <<(bP-Buff) <<'\n' <<std::flush;
275  return bP - Buff;
276 }
@ Info
const char * Name(const char *eName=0, const char **eText=0)
const char * Set(const char *hSpec, int pNum=PortInSpec)
Definition: XrdNetAddr.cc:216
static const char * tpcSpr
Definition: XrdOucTPC.hh:63
static const char * tpcDst
Definition: XrdOucTPC.hh:57
static const char * tpcDlgOn
Definition: XrdOucTPC.hh:68
static const char * cgiC2Dst(const char *cKey, const char *xSrc, const char *xLfn, const char *xCks, char *Buff, int Blen, int strms=0, const char *iHst=0, const char *sprt=0, const char *tprt=0, bool dlgon=false, bool push=false)
Definition: XrdOucTPC.cc:62
static const char * cgiC2Src(const char *cKey, const char *xDst, int xTTL, char *Buff, int Blen)
Definition: XrdOucTPC.cc:136
static const char * tpcTpr
Definition: XrdOucTPC.hh:66
static const char * tpcLfn
Definition: XrdOucTPC.hh:59
static int copyCGI(const char *cgi, char *Buff, int Blen)
Definition: XrdOucTPC.cc:233
static const char * tpcKey
Definition: XrdOucTPC.hh:58
static const char * tpcDlg
Definition: XrdOucTPC.hh:56
static const char * tpcSrc
Definition: XrdOucTPC.hh:64
static const char * tpcOrg
Definition: XrdOucTPC.hh:60
static const char * cgiD2Src(const char *cKey, const char *cOrg, char *Buff, int Blen)
Definition: XrdOucTPC.cc:168
static const char * tpcSgi
Definition: XrdOucTPC.hh:62
static const char * tpcTtl
Definition: XrdOucTPC.hh:67
static const char * tpcCks
Definition: XrdOucTPC.hh:55
static const char * tpcPsh
Definition: XrdOucTPC.hh:61
static const char * tpcStr
Definition: XrdOucTPC.hh:65