XRootD
Loading...
Searching...
No Matches
XrdOuca2x.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d O u c a 2 x . c c */
4/* */
5/* (c) 2004 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 <cstdlib>
31#include <cstdio>
32#include <sys/stat.h>
33#include <cerrno>
34
35#ifdef WIN32
36#include "XrdSys/XrdWin32.hh"
37#endif
38#include "XrdNet/XrdNetUtils.hh"
39#include "XrdOuc/XrdOuca2x.hh"
40
41/******************************************************************************/
42/* a 2 i */
43/******************************************************************************/
44
45int XrdOuca2x::a2i(XrdSysError &Eroute, const char *emsg, const char *item,
46 int *val, int minv, int maxv)
47{
48 char *eP;
49
50 if (!item || !*item)
51 {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
52
53 errno = 0;
54 *val = strtol(item, &eP, 10);
55 if (errno || *eP)
56 {Eroute.Emsg("a2x", emsg, item, "is not a number");
57 return -1;
58 }
59 if (*val < minv)
60 return Emsg(Eroute, emsg, item, "may not be less than %d", minv);
61 if (maxv >= 0 && *val > maxv)
62 return Emsg(Eroute, emsg, item, "may not be greater than %d", maxv);
63 return 0;
64}
65
66/******************************************************************************/
67/* a 2 l l */
68/******************************************************************************/
69
70int XrdOuca2x::a2ll(XrdSysError &Eroute, const char *emsg, const char *item,
71 long long *val, long long minv, long long maxv)
72{
73 char *eP;
74
75 if (!item || !*item)
76 {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
77
78 errno = 0;
79 *val = strtoll(item, &eP, 10);
80 if (errno || *eP)
81 {Eroute.Emsg("a2x", emsg, item, "is not a number");
82 return -1;
83 }
84 if (*val < minv)
85 return Emsg(Eroute, emsg, item, "may not be less than %lld", minv);
86 if (maxv >= 0 && *val > maxv)
87 return Emsg(Eroute, emsg, item, "may not be greater than %lld", maxv);
88 return 0;
89}
90
91/******************************************************************************/
92/* a 2 f m */
93/******************************************************************************/
94
95int XrdOuca2x::a2fm(XrdSysError &Eroute, const char *emsg, const char *item,
96 int *val, int minv, int maxv)
97{ int rc, num;
98 if ((rc = a2fm(Eroute, emsg, item, &num, minv))) return rc;
99 if ((*val | maxv) != maxv)
100 {Eroute.Emsg("a2fm", emsg, item, "is too inclusive.");
101 return -1;
102 }
103
104 *val = 0;
105 if (num & 0100) *val |= S_IXUSR; // execute permission: owner
106 if (num & 0200) *val |= S_IWUSR; // write permission: owner
107 if (num & 0400) *val |= S_IRUSR; // read permission: owner
108 if (num & 0010) *val |= S_IXGRP; // execute permission: group
109 if (num & 0020) *val |= S_IWGRP; // write permission: group
110 if (num & 0040) *val |= S_IRGRP; // read permission: group
111 if (num & 0001) *val |= S_IXOTH; // execute permission: other
112 if (num & 0002) *val |= S_IWOTH; // write permission: other
113 if (num & 0004) *val |= S_IROTH; // read permission: other
114 return 0;
115}
116
117int XrdOuca2x::a2fm(XrdSysError &Eroute, const char *emsg, const char *item,
118 int *val, int minv)
119{
120 if (!item || !*item)
121 {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
122
123 errno = 0;
124 *val = strtol(item, (char **)NULL, 8);
125 if (errno)
126 {Eroute.Emsg("a2x", emsg, item, "is not an octal number");
127 return -1;
128 }
129 if (!(*val & minv))
130 {Eroute.Emsg("a2x", emsg, item, "is too exclusive");;
131 return -1;
132 }
133 return 0;
134}
135
136/******************************************************************************/
137/* a 2 p */
138/******************************************************************************/
139
140int XrdOuca2x::a2p(XrdSysError &eDest, const char *ptype, const char *val,
141 bool anyOK)
142{
143 int pnum;
144
145 if (!strcmp("any", val))
146 {if (anyOK) return 0;
147 eDest.Emsg("Config", "port 'any' is not allowed");
148 return -1;
149 }
150
151 const char *invp = (*ptype == 't' ? "tcp port" : "udp port" );
152 const char *invs = (*ptype == 't' ? "Unable to find tcp service" :
153 "Unable to find udp service" );
154
155 if (isdigit(*val))
156 {if (XrdOuca2x::a2i(eDest,invp,val,&pnum,1,65535)) return -1;}
157 else if (!(pnum = XrdNetUtils::ServPort(val, (*ptype != 't'))))
158 {eDest.Emsg("Config", invs, val);
159 return -1;
160 }
161 return pnum;
162}
163
164/******************************************************************************/
165/* a 2 s n */
166/******************************************************************************/
167
168int XrdOuca2x::a2sn(XrdSysError &Eroute, const char *emsg, const char *item,
169 int *val, int nScale, int minv, int maxv)
170{
171 char *eP;
172 int nsVal = nScale;
173
174 if (!item || !*item)
175 {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
176
177 errno = 0;
178 *val = strtol(item, &eP, 10);
179 if (errno || (*eP && *eP != '.'))
180 {Eroute.Emsg("a2x", emsg, item, "is not a number");
181 return -1;
182 }
183
184 if (*eP == '.')
185 {eP++;
186 while(*eP >= '0' && *eP <= '9')
187 {if (nsVal > 1)
188 {*val = (*val * 10) + (*eP - int('0'));
189 nsVal /= 10;
190 }
191 eP++;
192 }
193 if (*eP)
194 {Eroute.Emsg("a2x", emsg, item, "is not a number");
195 return -1;
196 }
197 }
198 *val *= nsVal;
199
200 if (*val < minv)
201 return Emsg(Eroute, emsg, item, "may not be less than %f",
202 double(minv)/double(nScale));
203 if (maxv >= 0 && *val > maxv)
204 return Emsg(Eroute, emsg, item, "may not be greater than %d",
205 double(maxv)/double(nScale));
206 return 0;
207}
208
209/******************************************************************************/
210/* a 2 s p */
211/******************************************************************************/
212
213int XrdOuca2x::a2sp(XrdSysError &Eroute, const char *emsg, const char *item,
214 long long *val, long long minv, long long maxv)
215{
216 char *pp, buff[120];
217 int i;
218
219 if (!item || !*item)
220 {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
221
222 i = strlen(item);
223 if (item[i-1] != '%') return a2sz(Eroute, emsg, item, val, minv, maxv);
224
225 errno = 0;
226 *val = strtoll(item, &pp, 10);
227
228 if (errno || *pp != '%')
229 {Eroute.Emsg("a2x", emsg, item, "is not a number");
230 return -1;
231 }
232
233 if (maxv < 0) maxv = 100;
234
235 if (*val > maxv)
236 {sprintf(buff, "may not be greater than %lld%%", maxv);
237 Eroute.Emsg("a2x", emsg, item, buff);
238 return -1;
239 }
240
241 if (minv < 0) minv = 0;
242
243 if (*val > maxv)
244 {sprintf(buff, "may not be less than %lld%%", minv);
245 Eroute.Emsg("a2x", emsg, item, buff);
246 return -1;
247 }
248
249 *val = -*val;
250 return 0;
251}
252
253/******************************************************************************/
254/* a 2 s z */
255/******************************************************************************/
256
257int XrdOuca2x::a2sz(XrdSysError &Eroute, const char *emsg, const char *item,
258 long long *val, long long minv, long long maxv)
259{ long long qmult;
260 char *eP, *fP = (char *)item + strlen(item) - 1;
261
262 if (!item || !*item)
263 {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
264
265 if (*fP == 'k' || *fP == 'K') qmult = 1024LL;
266 else if (*fP == 'm' || *fP == 'M') qmult = 1024LL*1024LL;
267 else if (*fP == 'g' || *fP == 'G') qmult = 1024LL*1024LL*1024LL;
268 else if (*fP == 't' || *fP == 'T') qmult = 1024LL*1024LL*1024LL*1024LL;
269 else {qmult = 1; fP++;}
270 errno = 0;
271 double dval = strtod(item, &eP) * qmult;
272 if (errno || eP != fP)
273 {Eroute.Emsg("a2x", emsg, item, "is not a number");
274 return -1;
275 }
276 *val = (long long)dval;
277 if (*val < minv)
278 return Emsg(Eroute, emsg, item, "may not be less than %lld", minv);
279 if (maxv >= 0 && *val > maxv)
280 return Emsg(Eroute, emsg, item, "may not be greater than %lld", maxv);
281 return 0;
282}
283
284/******************************************************************************/
285/* a 2 t m */
286/******************************************************************************/
287
288int XrdOuca2x::a2tm(XrdSysError &Eroute, const char *emsg, const char *item, int *val,
289 int minv, int maxv)
290{ int qmult;
291 char *eP, *fP = (char *)item + strlen(item) - 1;
292
293 if (!item || !*item)
294 {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
295
296 if (*fP == 's' || *fP == 'S') qmult = 1;
297 else if (*fP == 'm' || *fP == 'M') qmult = 60;
298 else if (*fP == 'h' || *fP == 'H') qmult = 60*60;
299 else if (*fP == 'd' || *fP == 'D') qmult = 60*60*24;
300 else {qmult = 1; fP++;}
301
302 errno = 0;
303 *val = strtoll(item, &eP, 10) * qmult;
304 if (errno || eP != fP)
305 {Eroute.Emsg("a2x", emsg, item, "is not a number");
306 return -1;
307 }
308 if (*val < minv)
309 return Emsg(Eroute, emsg, item, "may not be less than %d", minv);
310 if (maxv >= 0 && *val > maxv)
311 return Emsg(Eroute, emsg, item, "may not be greater than %d", maxv);
312 return 0;
313}
314
315/******************************************************************************/
316/* a 2 v p */
317/******************************************************************************/
318
319int XrdOuca2x::a2vp(XrdSysError &Eroute, const char *emsg, const char *item,
320 int *val, int minv, int maxv)
321{
322 char *pp;
323
324 if (!item || !*item)
325 {Eroute.Emsg("a2x", emsg, "value not specified"); return -1;}
326
327 errno = 0;
328 *val = strtol(item, &pp, 10);
329
330 if (!errno && *pp == '%')
331 {if (*val < 0)
332 {Eroute.Emsg("a2x", emsg, item, "may not be negative.");
333 return -1;
334 }
335 if (*val > 100)
336 {Eroute.Emsg("a2x", emsg, item, "may not be greater than 100%.");
337 return -1;
338 }
339 else {*val = -*val; return 0;}
340 }
341
342 if (*val < minv)
343 return Emsg(Eroute, emsg, item, "may not be less than %d", minv);
344 if (maxv >= 0 && *val > maxv)
345 return Emsg(Eroute, emsg, item, "may not be greater than %d", maxv);
346 return 0;
347}
348
349/******************************************************************************/
350/* b 2 x */
351/******************************************************************************/
352
353int XrdOuca2x::b2x(const unsigned char* src, int slen, char* dst, int dlen)
354{
355 static const char *hv = "0123456789abcdef";
356
357// Make sure destination buffer is large enough (2*slen+1)
358//
359 if (dlen < slen*2+1) return 0;
360
361// Do conversion
362//
363 for (int i = 0; i < slen; i++)
364 {*dst++ = hv[(src[i] >> 4) & 0x0f];
365 *dst++ = hv[ src[i] & 0x0f];
366 }
367
368// End with null byte and return the full length
369//
370 *dst = '\0';
371 return slen*2+1;
372}
373
374/******************************************************************************/
375/* x 2 b */
376/******************************************************************************/
377
378int XrdOuca2x::x2b(const char* src, int slen, unsigned char* dst, int dlen,
379 bool radj)
380{
381 int n, len = (slen+1)/2;
382 bool odd = false;
383
384// Make sure we have enough destination bytes
385//
386 if (len > dlen) return 0;
387
388// If the length is odd then the first nibble is set to zero
389//
390 if (radj && slen & 0x01) {*dst = 0; odd = true;}
391
392// Perform conversion
393//
394 while(slen--)
395 { if (*src >= '0' && *src <= '9') n = *src-48;
396 else if (*src >= 'a' && *src <= 'f') n = *src-87;
397 else if (*src >= 'A' && *src <= 'F') n = *src-55;
398 else return 0;
399 if (odd) *dst++ |= n;
400 else *dst = n << 4;
401 src++; odd = !odd;
402 }
403 return len;
404}
405
406/******************************************************************************/
407/* P r i v a t e M e t h o d s */
408/******************************************************************************/
409
410int XrdOuca2x::Emsg(XrdSysError &Eroute, const char *etxt1, const char *item,
411 const char *etxt2, double val)
412{char buff[256];
413 sprintf(buff, etxt2, val);
414 Eroute.Emsg("a2x", etxt1, item, buff);
415 return -1;
416}
417
418int XrdOuca2x::Emsg(XrdSysError &Eroute, const char *etxt1, const char *item,
419 const char *etxt2, int val)
420{char buff[256];
421 sprintf(buff, etxt2, val);
422 Eroute.Emsg("a2x", etxt1, item, buff);
423 return -1;
424}
425
426int XrdOuca2x::Emsg(XrdSysError &Eroute, const char *etxt1, const char *item,
427 const char *etxt2, long long val)
428{char buff[256];
429 sprintf(buff, etxt2, val);
430 Eroute.Emsg("a2x", etxt1, item, buff);
431 return -1;
432}
static XrdSysError eDest(0,"crypto_")
int emsg(int rc, char *msg)
static int ServPort(const char *sName, bool isUDP=false, const char **eText=0)
static int x2b(const char *src, int slen, unsigned char *dst, int dlen, bool radj=false)
Definition XrdOuca2x.cc:378
static int a2fm(XrdSysError &, const char *emsg, const char *item, int *val, int minv)
Definition XrdOuca2x.cc:117
static int a2sp(XrdSysError &, const char *emsg, const char *item, long long *val, long long minv=-1, long long maxv=-1)
Definition XrdOuca2x.cc:213
static int a2i(XrdSysError &, const char *emsg, const char *item, int *val, int minv=-1, int maxv=-1)
Definition XrdOuca2x.cc:45
static int b2x(const unsigned char *src, int slen, char *dst, int dlen)
Definition XrdOuca2x.cc:353
static int a2sz(XrdSysError &, const char *emsg, const char *item, long long *val, long long minv=-1, long long maxv=-1)
Definition XrdOuca2x.cc:257
static int a2ll(XrdSysError &, const char *emsg, const char *item, long long *val, long long minv=-1, long long maxv=-1)
Definition XrdOuca2x.cc:70
static int a2tm(XrdSysError &, const char *emsg, const char *item, int *val, int minv=-1, int maxv=-1)
Definition XrdOuca2x.cc:288
static int a2sn(XrdSysError &, const char *emsg, const char *item, int *val, int nScale, int minv=-1, int maxv=-1)
Definition XrdOuca2x.cc:168
static int a2vp(XrdSysError &, const char *emsg, const char *item, int *val, int minv=-1, int maxv=-1)
Definition XrdOuca2x.cc:319
static int a2p(XrdSysError &, const char *ptype, const char *val, bool anyOK=true)
Definition XrdOuca2x.cc:140
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)