XrdOucReqID.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                        X r d O u c R e q I D . c c                         */
00004 /*                                                                            */
00005 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*       All Rights Reserved. See XrdInfo.cc for complete License Terms       */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC03-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010 
00011 //         $Id: XrdOucReqID.cc 24468 2008-06-22 16:47:03Z ganis $
00012 
00013 const char *XrdOucReqIDCVSID = "$Id: XrdOucReqID.cc 24468 2008-06-22 16:47:03Z ganis $";
00014 
00015 #include <limits.h>
00016 #include <stdio.h>
00017 #include <string.h>
00018 #ifndef WIN32
00019 #include <strings.h>
00020 #else
00021 #include "XrdSys/XrdWin32.hh"
00022 #endif
00023 #include <time.h>
00024 #include <sys/types.h>
00025   
00026 #include "XrdOucReqID.hh"
00027 
00028 #include "XrdOuc/XrdOucCRC.hh"
00029 
00030 /******************************************************************************/
00031 /*                      S t a t i c   V a r i a b l e s                       */
00032 /******************************************************************************/
00033   
00034 XrdSysMutex  XrdOucReqID::myMutex;
00035 char        *XrdOucReqID::reqFMT;
00036 char        *XrdOucReqID::reqPFX;
00037 int          XrdOucReqID::reqPFXlen = 0;
00038 int          XrdOucReqID::reqNum = 0;
00039 
00040 /******************************************************************************/
00041 /*                           C o n s t r u c t o r                            */
00042 /******************************************************************************/
00043   
00044 XrdOucReqID::XrdOucReqID(int inst, const char *myHost, unsigned int myIP)
00045 {
00046    time_t eNow = time(0);
00047    char xbuff[256];
00048 
00049    snprintf(xbuff, sizeof(xbuff)-1, "%08x:%04x.%08x:%%d", myIP, inst,
00050                                     static_cast<unsigned int>(eNow));
00051    reqFMT = strdup(xbuff);
00052    reqPFXlen = 13;
00053    xbuff[reqPFXlen] = '\0';
00054    reqPFX = strdup(xbuff);
00055 }
00056  
00057 /******************************************************************************/
00058 /*                                i s M i n e                                 */
00059 /******************************************************************************/
00060  
00061 int XrdOucReqID::isMine(char *reqid, int &hport, char *hname, int hlen)
00062 {
00063    unsigned int ipaddr, ipport;
00064    char *cp, *ep, *ip;
00065 
00066 // Determine whether this is our host
00067 //
00068    if (isMine(reqid)) return 1;
00069 
00070 // Not ours, try to tell the caller who it is
00071 //
00072    if (!hlen) return 0;
00073 
00074 // Get the IP address of his id
00075 //
00076    hport = 0;
00077    if (!(cp = index(reqid, int(':'))) || cp-reqid != 8) return 0;
00078    if (!(ipaddr = strtol(reqid, &ep, 16)) || ep != cp)  return 0;
00079 
00080 // Get the port number
00081 //
00082    ep++;
00083    if (!(cp = index(ep, int('.')))     || cp-ep != 4) return 0;
00084    if (!(ipport = strtol(ep, &cp, 16)) || ep != cp)   return 0;
00085 
00086 // Format the address and return the port
00087 //
00088    ip = (char *)&ipaddr;
00089    snprintf(hname, hlen-1, "%d.%d.%d.%d",
00090                    (int)ip[0], (int)ip[1], (int)ip[2], (int)ip[3]);
00091    hname[hlen-1] = '\0';
00092    hport = (int)ipport;
00093    return 0;
00094 }
00095   
00096 /******************************************************************************/
00097 /*                                    I D                                     */
00098 /******************************************************************************/
00099   
00100 char *XrdOucReqID::ID(char *buff, int blen)
00101 {
00102    int myNum;
00103 
00104 // Get a new sequence number
00105 //
00106    myMutex.Lock();
00107    myNum = (reqNum += 1);
00108    myMutex.UnLock();
00109 
00110 // Generate the request id and return it
00111 //
00112    snprintf(buff, blen-1, reqFMT, myNum);
00113    return buff;
00114 }
00115 
00116 /******************************************************************************/
00117 /*                                 I n d e x                                  */
00118 /******************************************************************************/
00119   
00120 int XrdOucReqID::Index(int KeyMax, const char *KeyVal, int KeyLen)
00121 {
00122    unsigned int pHash;
00123 
00124 // Get hash value for the key and return modulo of the KeyMax value
00125 //
00126    pHash = XrdOucCRC::CRC32((const unsigned char *)KeyVal,
00127                             (KeyLen ? KeyLen : strlen(KeyVal)));
00128    return (int)(pHash % KeyMax);
00129 }

Generated on Tue Jul 5 14:46:50 2011 for ROOT_528-00b_version by  doxygen 1.5.1