XRootD
XrdOucReqID.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O u c R e q I D . 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 <limits.h>
31 #include <cstdio>
32 #include <cstring>
33 #ifndef WIN32
34 #include <strings.h>
35 #else
36 #include "XrdSys/XrdWin32.hh"
37 #endif
38 #include <ctime>
39 #include <unistd.h>
40 #include <netinet/in.h>
41 #include <sys/types.h>
42 
43 #include "XrdOucReqID.hh"
44 
45 #include "XrdNet/XrdNetAddr.hh"
46 #include "XrdNet/XrdNetUtils.hh"
47 #include "XrdOuc/XrdOucCRC.hh"
48 
49 /******************************************************************************/
50 /* C o n s t r u c t o r */
51 /******************************************************************************/
52 
54 {
55  char xbuff[256];
56  int eNow = static_cast<int>(time(0)), myPid = static_cast<int>(getpid());
57 
58 // Now format the formatting template
59 //
60  snprintf(xbuff, sizeof(xbuff)-1, "%08X:%08x.%%d", myPid, eNow);
61  reqFMT = strdup(xbuff);
62  xbuff[8] = 0;
63  reqPFX = strdup(xbuff);
64  reqPFXlen = 8;
65  reqIntern = 0;
66  reqNum = 0;
67 }
68 
69 /******************************************************************************/
70 
71 XrdOucReqID::XrdOucReqID(const XrdNetSockAddr *myAddr, int myPort)
72 {
73  char ybuff[256], xbuff[512];
74  unsigned int pHash;
75  int n, eNow = static_cast<unsigned int>(time(0));
76 
77 // Encode our address as the prefix
78 //
79  if ( (n = XrdNetUtils::Encode(myAddr, ybuff, sizeof(ybuff), myPort)) <= 0)
80  n = sprintf(ybuff, "%04X%08X", myPort, eNow);
81  reqPFX = strdup(ybuff);
82  reqPFXlen = n;
83  reqIntern = n+1;
84 
85 // Generate out hash
86 //
87  pHash = XrdOucCRC::CRC32((const unsigned char *)ybuff, n);
88 
89 // Now format the formatting template
90 //
91  snprintf(xbuff, sizeof(xbuff)-1, "%s:%08x.%08x:%%d", ybuff, pHash, eNow);
92  reqFMT = strdup(xbuff);
93  reqNum = 0;
94 }
95 
96 /******************************************************************************/
97 /* i s M i n e */
98 /******************************************************************************/
99 
100 char *XrdOucReqID::isMine(char *reqid, int &hport, char *hname, int hlen)
101 {
102  XrdNetAddr theAddr;
103  XrdNetSockAddr IP;
104  const char *theHost;
105  int thePort;
106  char *cp;
107 
108 // Determine whether this is our host
109 //
110  if (!strncmp(reqPFX,reqid,reqPFXlen) && (cp = index(reqid,':'))) return cp+1;
111 
112 // Not ours, try to tell the caller who it is
113 //
114  hport = 0;
115  if (!hlen) return 0;
116 
117 // Get the IP address of his id
118 //
119  thePort = XrdNetUtils::Decode(&IP, reqid, reqPFXlen);
120  if (thePort <= 0) return 0;
121 
122 // Convert this in the appropriate way
123 //
124  if (theAddr.Set(&IP.Addr)
125  || !(theHost = theAddr.Name())
126  || strlen(theHost) >= (unsigned int)hlen) return 0;
127 
128 // Return the alternate host
129 //
130  strcpy(hname, theHost);
131  hport = thePort;
132  return 0;
133 }
134 
135 /******************************************************************************/
136 /* I D */
137 /******************************************************************************/
138 
139 char *XrdOucReqID::ID(char *buff, int blen)
140 {
141  int myNum;
142 
143 // Get a new sequence number
144 //
145  myMutex.Lock();
146  myNum = (reqNum += 1);
147  myMutex.UnLock();
148 
149 // Generate the request id and return it
150 //
151  snprintf(buff, blen-1, reqFMT, myNum);
152  return buff+reqIntern;
153 }
154 
155 /******************************************************************************/
156 /* I n d e x */
157 /******************************************************************************/
158 
159 int XrdOucReqID::Index(int KeyMax, const char *KeyVal, int KeyLen)
160 {
161  unsigned int pHash;
162 
163 // Get hash value for the key and return modulo of the KeyMax value
164 //
165  pHash = XrdOucCRC::CRC32((const unsigned char *)KeyVal,
166  (KeyLen ? KeyLen : strlen(KeyVal)));
167  return (int)(pHash % KeyMax);
168 }
struct sockaddr Addr
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 int Encode(const XrdNetSockAddr *sadr, char *buff, int blen, int port=-1)
Definition: XrdNetUtils.cc:120
static int Decode(XrdNetSockAddr *sadr, const char *buff, int blen)
Definition: XrdNetUtils.cc:70
static uint32_t CRC32(const unsigned char *data, int count)
Definition: XrdOucCRC.cc:171
static int Index(int KeyMax, const char *KeyVal, int KeyLen=0)
Definition: XrdOucReqID.cc:159
char * ID(char *buff, int blen)
Definition: XrdOucReqID.cc:139
char * isMine(char *reqid, int &hport, char *hname, int hlen)
Definition: XrdOucReqID.cc:100