XRootD
XrdSutRndm Class Reference

#include <XrdSutRndm.hh>

+ Collaboration diagram for XrdSutRndm:

Public Member Functions

 XrdSutRndm ()
 
virtual ~XrdSutRndm ()
 

Static Public Member Functions

static char * GetBuffer (int len, int opt=-1)
 
static int GetRndmTag (XrdOucString &rtag)
 
static int GetString (const char *copt, int len, XrdOucString &s)
 
static int GetString (int opt, int len, XrdOucString &s)
 
static unsigned int GetUInt ()
 
static bool Init (bool force=0)
 

Static Public Attributes

static bool fgInit = 0
 

Detailed Description

Definition at line 43 of file XrdSutRndm.hh.

Constructor & Destructor Documentation

◆ XrdSutRndm()

XrdSutRndm::XrdSutRndm ( )
inline

Definition at line 48 of file XrdSutRndm.hh.

48 { if (!fgInit) fgInit = XrdSutRndm::Init(); }
static bool fgInit
Definition: XrdSutRndm.hh:46
static bool Init(bool force=0)
Definition: XrdSutRndm.cc:62

References fgInit, and Init().

+ Here is the call graph for this function:

◆ ~XrdSutRndm()

virtual XrdSutRndm::~XrdSutRndm ( )
inlinevirtual

Definition at line 49 of file XrdSutRndm.hh.

49 { }

Member Function Documentation

◆ GetBuffer()

char * XrdSutRndm::GetBuffer ( int  len,
int  opt = -1 
)
static

Definition at line 179 of file XrdSutRndm.cc.

180 {
181  // Static method to fill randomly a buffer.
182  // Returns the pointer to the buffer if ok, 0 in case of error.
183  // If opt has one of the following values, the random bytes are
184  // chosen between the corrsponding subset:
185  // opt = 0 any printable char
186  // 1 letters and numbers (upper and lower case)
187  // 2 hex characters (upper and lower case)
188  // 3 crypt like [a-zA-Z0-9./]
189  // The caller is responsible to destroy the buffer
190  EPNAME("Rndm::GetBuffer");
191 
192  DEBUG("enter: len: " <<len);
193 
194  // Init Random machinery ... if needed
195  if (!fgInit) {
196  Init();
197  fgInit = 1;
198  }
199 
200  // randomize
201  char *buf = new char[len];
202  if (!buf) {
203  errno = ENOSPC;
204  return 0;
205  }
206 
207  // Filtering ?
208  bool filter = (opt >= 0 && opt <= 3);
209 
210  kXR_int32 k = 0;
211  kXR_int32 i, m, frnd, j = 0, l = 0;
212  while (k < len) {
213  frnd = rand();
214  for (m = 0; m < 32; m += 8) {
215  i = 0xFF & (frnd >> m);
216  bool keep = 1;
217  if (filter) {
218  j = i / 32;
219  l = i - j * 32;
220  keep = (XrdSutCharMsk[opt][j%4] & (1 << l));
221  }
222  if (keep) {
223  buf[k] = i;
224  k++;
225  }
226  if (k == len)
227  break;
228  }
229  }
230 
231  return buf;
232 }
int kXR_int32
Definition: XPtypes.hh:89
#define DEBUG(x)
Definition: XrdBwmTrace.hh:54
#define EPNAME(x)
Definition: XrdBwmTrace.hh:56
static kXR_unt32 XrdSutCharMsk[4][4]
Definition: XrdSutRndm.cc:47

References DEBUG, EPNAME, fgInit, Init(), and XrdSutCharMsk.

Referenced by XrdCryptosslCipher::XrdCryptosslCipher(), and XrdCryptosslFactory::XrdCryptosslFactory().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetRndmTag()

int XrdSutRndm::GetRndmTag ( XrdOucString rtag)
static

Definition at line 235 of file XrdSutRndm.cc.

236 {
237  // Static method generating a 64 bit random tag (8 chars in [a-zA-Z0-9./])
238  // saved in rtag.
239  // Return 0 in case of success; in case of error, -1 is returned
240  // and errno set accordingly (see XrdSutRndm::GetString)
241 
242  return XrdSutRndm::GetString(3,8,rtag);
243 }
static int GetString(int opt, int len, XrdOucString &s)
Definition: XrdSutRndm.cc:120

References GetString().

+ Here is the call graph for this function:

◆ GetString() [1/2]

int XrdSutRndm::GetString ( const char *  copt,
int  len,
XrdOucString s 
)
static

Definition at line 97 of file XrdSutRndm.cc.

98 {
99  // Static method to fill string str with len random characters.
100  // Returns 0 if ok, -1 in case of error.
101  // copt = "Any" any printable char
102  // "LetNum" letters and numbers (upper and lower case)
103  // "Hex" hex characters (upper and lower case)
104  // "Crypt" crypt like [a-zA-Z0-9./]
105  //
106  // (opt is not case sensitive)
107 
108  int opt = 0;
109  if (!strncasecmp(copt,"LetNum",6))
110  opt = 1;
111  else if (!strncasecmp(copt,"Hex",3))
112  opt = 2;
113  else if (!strncasecmp(copt,"Crypt",5))
114  opt = 3;
115 
116  return XrdSutRndm::GetString(opt,len,str);
117 }

References GetString().

+ Here is the call graph for this function:

◆ GetString() [2/2]

int XrdSutRndm::GetString ( int  opt,
int  len,
XrdOucString s 
)
static

Definition at line 120 of file XrdSutRndm.cc.

121 {
122  // Static method to fill string str with len random characters.
123  // Returns 0 if ok, -1 in case of error.
124  // opt = 0 any printable char
125  // 1 letters and numbers (upper and lower case)
126  // 2 hex characters (upper and lower case)
127  // 3 crypt like [a-zA-Z0-9./]
128  EPNAME("Rndm::GetString");
129 
130  const char *cOpt[4] = { "Any", "LetNum", "Hex", "Crypt" };
131 
132  // Default option 0
133  if (opt < 0 || opt > 3) {
134  opt = 0;
135  DEBUG("unknown option: " <<opt <<": assume 0");
136  }
137  DEBUG("enter: len: " <<len <<" (type: " <<cOpt[opt] <<")");
138 
139  // Init Random machinery ... if needed
140  if (!XrdSutRndm::fgInit)
142 
143  // randomize
144  char *buf = new char[len+1];
145  if (!buf) {
146  errno = ENOSPC;
147  return -1;
148  }
149 
150  kXR_int32 k = 0;
151  kXR_int32 i, j, l, m, frnd;
152  while (k < len) {
153  frnd = rand();
154  for (m = 7; m < 32; m += 7) {
155  i = 0x7F & (frnd >> m);
156  j = i / 32;
157  l = i - j * 32;
158  if ((XrdSutCharMsk[opt][j] & (1 << l))) {
159  buf[k] = i;
160  k++;
161  }
162  if (k == len)
163  break;
164  }
165  }
166 
167  // null terminated
168  buf[len] = 0;
169  DEBUG("got: " <<buf);
170 
171  // Fill output
172  str = buf;
173  delete[] buf;
174 
175  return 0;
176 }

References DEBUG, EPNAME, fgInit, Init(), and XrdSutCharMsk.

Referenced by AddPassword(), GetRndmTag(), GetString(), main(), ParseArguments(), and XrdSutResolve().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetUInt()

unsigned int XrdSutRndm::GetUInt ( )
static

Definition at line 247 of file XrdSutRndm.cc.

248 {
249  // Static method to return an unsigned int.
250 
251  // Init Random machinery ... if needed
252  if (!fgInit) {
253  Init();
254  fgInit = 1;
255  }
256 
257  // As simple as this
258  return rand();
259 }

References fgInit, and Init().

+ Here is the call graph for this function:

◆ Init()

bool XrdSutRndm::Init ( bool  force = 0)
static

Definition at line 62 of file XrdSutRndm.cc.

63 {
64  // Initialize the random machinery; try using /dev/urandom to avoid
65  // hanging.
66  // The bool 'force' can be used to force re-initialization.
67  EPNAME("Rndm::Init");
68 
69  const char *randdev = "/dev/urandom";
70  bool rc = 0;
71 
72  // We do not do it twice
73  if (fgInit && !force)
74  return 1;
75 
76  int fd;
77  unsigned int seed = 0;
78  if ((fd = open(randdev, O_RDONLY)) != -1) {
79  DEBUG("taking seed from " <<randdev);
80  if (read(fd, &seed, sizeof(seed)) == sizeof(seed)) rc = 1;
81  close(fd);
82  }
83  if (rc == 0) {
84  DEBUG(randdev <<" not available: using time()");
85  seed = time(0); //better use times() + win32 equivalent
86  rc = 1;
87  }
88  srand(seed);
89 
90  // Flag as initialized
91  fgInit = 1;
92 
93  return rc;
94 }
int open(const char *path, int oflag,...)
ssize_t read(int fildes, void *buf, size_t nbyte)
#define close(a)
Definition: XrdPosix.hh:43

References close, DEBUG, EPNAME, fgInit, open(), and read().

Referenced by XrdSutRndm(), GetBuffer(), GetString(), GetUInt(), main(), and ParseArguments().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ fgInit

bool XrdSutRndm::fgInit = 0
static

Definition at line 46 of file XrdSutRndm.hh.

Referenced by XrdSutRndm(), GetBuffer(), GetString(), GetUInt(), and Init().


The documentation for this class was generated from the following files: