00001 #ifndef __XRDNETCACHE_HH__ 00002 #define __XRDNETCACHE_HH__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d N e t C a c h e . h h */ 00006 /* */ 00007 /* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved */ 00009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00011 /* */ 00012 /* This file is part of the XRootD software suite. */ 00013 /* */ 00014 /* XRootD is free software: you can redistribute it and/or modify it under */ 00015 /* the terms of the GNU Lesser General Public License as published by the */ 00016 /* Free Software Foundation, either version 3 of the License, or (at your */ 00017 /* option) any later version. */ 00018 /* */ 00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00022 /* License for more details. */ 00023 /* */ 00024 /* You should have received a copy of the GNU Lesser General Public License */ 00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00027 /* */ 00028 /* The copyright holder's institutional names and contributor's names may not */ 00029 /* be used to endorse or promote products derived from this software without */ 00030 /* specific prior written permission of the institution or contributor. */ 00031 /******************************************************************************/ 00032 00033 #include <string.h> 00034 #include <time.h> 00035 #include <sys/types.h> 00036 00037 #include "XrdSys/XrdSysPthread.hh" 00038 00039 class XrdNetAddrInfo; 00040 00041 class XrdNetCache 00042 { 00043 public: 00044 00045 //------------------------------------------------------------------------------ 00051 //------------------------------------------------------------------------------ 00052 00053 void Add(XrdNetAddrInfo *hAddr, const char *hName); 00054 00055 //------------------------------------------------------------------------------ 00062 //------------------------------------------------------------------------------ 00063 00064 char *Find(XrdNetAddrInfo *hAddr); 00065 00066 //------------------------------------------------------------------------------ 00070 //------------------------------------------------------------------------------ 00071 static 00072 void SetKT(int ktval) {keepTime = ktval;} 00073 00074 //------------------------------------------------------------------------------ 00080 //------------------------------------------------------------------------------ 00081 00082 XrdNetCache(int psize = 987, int csize = 1597); 00083 00084 //------------------------------------------------------------------------------ 00087 //------------------------------------------------------------------------------ 00088 00089 ~XrdNetCache() {} // Never gets deleted 00090 00091 private: 00092 00093 static const int LoadMax = 80; 00094 00095 struct anItem 00096 {union {long long aV6[2]; 00097 int aV4[4]; 00098 char aVal[16]; // Enough for IPV4 or IPV6 00099 }; 00100 anItem *Next; 00101 char *hName; 00102 time_t expTime; // Expiration time 00103 unsigned int aHash; // Hash value 00104 int aLen; // Actual length 4 or 16 00105 00106 inline int operator!=(const anItem &oth) 00107 {return aLen != oth.aLen || aHash != oth.aHash 00108 || memcmp(aVal, oth.aVal, aLen); 00109 } 00110 00111 anItem() : Next(0), hName(0), aLen(0) {} 00112 00113 anItem(anItem &Item, const char *hn, int kt) 00114 : Next(0), hName(strdup(hn)), expTime(time(0)+kt), 00115 aHash(Item.aHash), aLen(Item.aLen) 00116 {memcpy(aVal, Item.aVal, Item.aLen);} 00117 ~anItem() {if (hName) free(hName);} 00118 }; 00119 00120 void Expand(); 00121 int GenKey(anItem &Item, XrdNetAddrInfo *hAddr); 00122 anItem *Locate(anItem &Item); 00123 00124 static int keepTime; 00125 00126 XrdSysMutex myMutex; 00127 anItem **nashtable; 00128 int prevtablesize; 00129 int nashtablesize; 00130 int nashnum; 00131 int Threshold; 00132 }; 00133 #endif