1 |
// Copyright (C) 2003 Stephane Thiell |
2 |
// |
3 |
// This file is part of pxyscand (from pxys) |
4 |
// |
5 |
// This program is free software; you can redistribute it and/or |
6 |
// modify it under the terms of the GNU General Public License |
7 |
// as published by the Free Software Foundation; either version 2 |
8 |
// of the License, or (at your option) any later version. |
9 |
// |
10 |
// This program is distributed in the hope that it will be useful, |
11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
// GNU General Public License for more details. |
14 |
// |
15 |
// You should have received a copy of the GNU General Public License |
16 |
// along with this program; if not, write to the Free Software |
17 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
// |
19 |
// $Id: PXIPCacheT.h,v 1.3 2006/09/04 12:47:18 spale Exp $ |
20 |
// |
21 |
#ifndef INCLUDED_PXIPCACHET_H_ |
22 |
#define INCLUDED_PXIPCACHET_H_ |
23 |
|
24 |
// IPCache template class wrapper on pxys2/ipcache for pxyscand. |
25 |
// To cache IPv4 or IPv6 associated with any binary info. Features basic |
26 |
// swapin/swapout methods to save/restore to a file. |
27 |
// mbuna, 2003-12-12 |
28 |
|
29 |
#include <cstdio> |
30 |
#include <ctime> |
31 |
#include <sys/types.h> |
32 |
#include <sys/param.h> |
33 |
#include <peak/alloc.h> |
34 |
#include <peak/time.h> |
35 |
|
36 |
#include "ipcache.h" |
37 |
|
38 |
template<typename Addr> |
39 |
class PXIPCacheT |
40 |
{ |
41 |
public: |
42 |
PXIPCacheT(const char *cachedir, uint32_t sig, int size, int expiredelay, |
43 |
size_t datasize); |
44 |
virtual ~PXIPCacheT(); |
45 |
|
46 |
virtual time_t Get(const Addr &addr, void **oinfo); |
47 |
virtual void Put(const Addr &addr, time_t t, const void *info); |
48 |
virtual bool Invalidate(const Addr &addr); |
49 |
virtual size_t GetCount(); |
50 |
virtual void Cleanup(); |
51 |
|
52 |
virtual int SwapIn(const char *inFileName); |
53 |
virtual int SwapOut(const char *inFileName); |
54 |
|
55 |
protected: |
56 |
|
57 |
ipcache_t mIPCache; |
58 |
char * mCacheDir; |
59 |
uint32_t mSig; |
60 |
int mExpireDelay; |
61 |
}; |
62 |
|
63 |
template<typename Addr> |
64 |
PXIPCacheT<Addr>::~PXIPCacheT() |
65 |
{ |
66 |
ipcache_dispose(mIPCache); |
67 |
peak_deallocate(mCacheDir); |
68 |
} |
69 |
|
70 |
template<typename Addr> |
71 |
inline size_t |
72 |
PXIPCacheT<Addr>::GetCount() |
73 |
{ |
74 |
this->Cleanup(); |
75 |
return ipcache_get_count(mIPCache); |
76 |
} |
77 |
|
78 |
template<typename Addr> |
79 |
inline void |
80 |
PXIPCacheT<Addr>::Cleanup() |
81 |
{ |
82 |
ipcache_cleanup(mIPCache, peak_time() - mExpireDelay); |
83 |
} |
84 |
|
85 |
template<typename Addr> |
86 |
int |
87 |
PXIPCacheT<Addr>::SwapIn(const char *inFileName) |
88 |
{ |
89 |
char path[MAXPATHLEN]; |
90 |
snprintf(path, sizeof(path), "%s/%s", mCacheDir, inFileName); |
91 |
return ipcache_swapin(mIPCache, path, mSig); |
92 |
} |
93 |
|
94 |
template<typename Addr> |
95 |
int |
96 |
PXIPCacheT<Addr>::SwapOut(const char *inFileName) |
97 |
{ |
98 |
char path[MAXPATHLEN]; |
99 |
snprintf(path, sizeof(path), "%s/%s", mCacheDir, inFileName); |
100 |
this->Cleanup(); |
101 |
return ipcache_swapout(mIPCache, path, mSig); |
102 |
} |
103 |
|
104 |
|
105 |
#endif // INCLUDED_PXIPCACHET_H_ |