1 |
/* Copyright (C) 2003 Stephane Thiell |
2 |
* |
3 |
* This file is part of pxyservd (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: ipcache.h,v 1.1.1.1 2003/12/30 17:08:35 mbuna Exp $ |
20 |
*/ |
21 |
#ifndef INCLUDED_IPCACHE_H |
22 |
#define INCLUDED_IPCACHE_H |
23 |
|
24 |
#include <sys/types.h> |
25 |
#include <sys/socket.h> |
26 |
#include <netinet/in.h> |
27 |
#include <peak/stdint.h> |
28 |
|
29 |
/* IP LRU cache module for pxyservd and pxyscand, optimized using libpeak. |
30 |
*/ |
31 |
|
32 |
|
33 |
/* Opaque type for IP cache objects |
34 |
*/ |
35 |
typedef struct __ipcache * ipcache_t; |
36 |
|
37 |
|
38 |
#if defined(__cplusplus) |
39 |
extern "C" { |
40 |
#endif |
41 |
|
42 |
|
43 |
/* IP cache creation/destruction methods. IP cache uses a fixed number of max |
44 |
* entries. This API offers two methods when needed, to distinguish IPv4 and |
45 |
* IPv6 IP caches. You can NOT mix both in one IP cache object (ipcache_t). |
46 |
* You can associate a fixed amount of data with an IP, specify it here (can |
47 |
* be 0). |
48 |
*/ |
49 |
extern ipcache_t ipcache_create_in4(int size, size_t datasize); |
50 |
extern ipcache_t ipcache_create_in6(int size, size_t datasize); |
51 |
extern void ipcache_dispose(ipcache_t cp); |
52 |
|
53 |
|
54 |
/* You can explicitly clear all entries in the cache with the following method. |
55 |
*/ |
56 |
extern int ipcache_clear(ipcache_t cp); |
57 |
|
58 |
|
59 |
/* Add an IP entry in the cache. Will drop the oldest entry if the cache is |
60 |
* full. So always the least recently used IPs are in the cache. |
61 |
* "Add if absent, update timestamp if present" |
62 |
* PRECONDITION: "t" must be >= "t" of previous call! Usually, pass time(0), |
63 |
* but it's ok to load them from a file too, if they are ordered |
64 |
* (ipcache_swapin() uses this feature). |
65 |
*/ |
66 |
extern void ipcache_add_in4(ipcache_t cp, const struct in_addr *addr, |
67 |
time_t t, const void *data); |
68 |
extern void ipcache_add_in6(ipcache_t cp, const struct in6_addr *addr, |
69 |
time_t t, const void *data); |
70 |
|
71 |
|
72 |
/* Mark an IP entry in the cache as invalid (that's the only way to "remove" |
73 |
* an IP from the cache). |
74 |
*/ |
75 |
extern int ipcache_invalidate_in4(ipcache_t cp, const struct in_addr *addr); |
76 |
extern int ipcache_invalidate_in6(ipcache_t cp, const struct in6_addr *addr); |
77 |
|
78 |
|
79 |
/* Find function, returns 0 if not found or the last seen timestamp otherwise. |
80 |
* If found and a non-NULL output data (od) pointer is provided, the data |
81 |
* associated with the IP, if any, will be returned. Data is mutable and its |
82 |
* content can be modified (eg. to update a cache hit counter, etc.). |
83 |
*/ |
84 |
extern time_t ipcache_find_in4(ipcache_t cp, const struct in_addr *addr, |
85 |
void **od); |
86 |
extern time_t ipcache_find_in6(ipcache_t cp, const struct in6_addr *addr, |
87 |
void **od); |
88 |
|
89 |
|
90 |
extern size_t ipcache_get_count(ipcache_t cp); |
91 |
|
92 |
/* Special cleanup method. Delete all cache entries with ts <= expire_ts. |
93 |
*/ |
94 |
extern size_t ipcache_cleanup(ipcache_t cp, time_t expire_ts); |
95 |
|
96 |
/* Cache media storage methods. |
97 |
*/ |
98 |
extern int ipcache_swapin(ipcache_t cp, const char *file, uint32_t sig); |
99 |
extern int ipcache_swapout(ipcache_t cp, const char *file, uint32_t sig); |
100 |
|
101 |
#if defined(__cplusplus) |
102 |
} |
103 |
#endif |
104 |
|
105 |
#endif /* INCLUDED_IPCACHE_H */ |