]>
jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscancache.c
3 * This file deals with the cache of known hosts, clean or otherwise.
9 #include "../core/error.h"
12 time_t cleanscaninterval
;
13 time_t dirtyscaninterval
;
15 void cachehostinit(time_t ri
) {
17 dirtyscaninterval
=ri
*7;
20 cachehost
*addcleanhost(time_t timestamp
) {
24 chp
->lastscan
=timestamp
;
32 void delcachehost(cachehost
*chp
) {
33 foundproxy
*fpp
, *nfpp
;
35 for (fpp
=chp
->proxies
;fpp
;fpp
=nfpp
) {
43 * Returns a cachehost * for the named IP
46 cachehost
*findcachehost(patricia_node_t
*node
) {
49 if( (cachehost
*)node
->exts
[ps_cache_ext
] ) {
50 chp
= (cachehost
*)node
->exts
[ps_cache_ext
];
51 if(chp
->lastscan
< (time(NULL
)-(chp
->proxies
? dirtyscaninterval
: cleanscaninterval
))) {
52 /* Needs rescan; delete and return 1 */
54 derefnode(iptree
,node
);
55 node
->exts
[ps_cache_ext
] = NULL
;
58 /* valid: return it */
63 /* Not found: return NULL */
69 * Dumps all cached hosts to a savefile. Expires hosts as it goes along
72 void dumpcachehosts(void *arg
) {
75 time_t now
=time(NULL
);
77 patricia_node_t
*node
;
79 if ((fp
=fopen("data/cleanhosts","w"))==NULL
) {
80 Error("proxyscan",ERR_ERROR
,"Unable to open cleanhosts file for writing!");
84 PATRICIA_WALK_CLEAR (iptree
->head
, node
) {
85 if (node
->exts
[ps_cache_ext
] ) {
86 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
89 if (chp
->lastscan
< (now
-dirtyscaninterval
)) {
90 derefnode(iptree
,node
);
92 node
->exts
[ps_cache_ext
] = NULL
;
95 for (fpp
=chp
->proxies
;fpp
;fpp
=fpp
->next
)
96 fprintf(fp
, "%s %lu %u %i %u %lu\n",IPtostr(node
->prefix
->sin
),chp
->lastscan
,chp
->glineid
,fpp
->type
,fpp
->port
,chp
->lastgline
);
98 if (chp
->lastscan
< (now
-cleanscaninterval
)) {
99 /* Needs rescan anyway, so delete it */
100 derefnode(iptree
,node
);
102 node
->exts
[ps_cache_ext
] = NULL
;
104 fprintf(fp
,"%s %lu\n",IPtostr(node
->prefix
->sin
),chp
->lastscan
);
108 } PATRICIA_WALK_CLEAR_END
;
110 // patricia_tidy_tree(iptree);
117 * Loads clean hosts in from database.
120 void loadcachehosts() {
122 unsigned long timestamp
,glineid
,ptype
,pport
,lastgline
;
128 struct irc_in_addr sin
;
130 patricia_node_t
*node
;
133 if ((fp
=fopen("data/cleanhosts","r"))==NULL
) {
134 Error("proxyscan",ERR_ERROR
,"Unable to open cleanhosts file for reading!");
145 res
=sscanf(buf
,"%s %lu %lu %lu %lu %lu",ip
,×tamp
,&glineid
,&ptype
,&pport
,&lastgline
);
150 if (0 == ipmask_parse(ip
,&sin
, &bits
)) {
153 node
= refnode(iptree
, &sin
, bits
);
156 chp
=addcleanhost(timestamp
);
157 node
->exts
[ps_cache_ext
] = chp
;
160 chp
->glineid
=glineid
;
161 chp
->lastgline
=lastgline
;
165 fpp
->next
=chp
->proxies
;
172 Error("proxyscan",ERR_INFO
, "Loaded %d entries from cache", i
);
177 * Returns the number of "clean" host entries present
180 unsigned int cleancount() {
181 unsigned int total
=0;
183 patricia_node_t
*head
, *node
;
185 PATRICIA_WALK (head
, node
) {
186 if ( node
->exts
[ps_cache_ext
] ) {
187 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
197 unsigned int dirtycount() {
198 unsigned int total
=0;
200 patricia_node_t
*node
;
202 PATRICIA_WALK (iptree
->head
, node
) {
203 if ( node
->exts
[ps_cache_ext
] ) {
204 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
215 * Scans all hosts on the network for a given proxy, and updates the cache accordingly
218 void scanall(int type
, int port
) {
222 unsigned int hostmarker
;
223 patricia_node_t
*node
;
225 hostmarker
=nexthostmarker();
227 PATRICIA_WALK (iptree
->head
, node
) {
228 if ( node
->exts
[ps_cache_ext
] ) {
229 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
234 for (i
=0;i
<NICKHASHSIZE
;i
++) {
235 for (np
=nicktable
[i
];np
;np
=np
->next
) {
236 if (np
->host
->marker
==hostmarker
)
239 np
->host
->marker
=hostmarker
;
241 if ((chp
=findcachehost(np
->ipnode
)))
244 queuescan(np
->ipnode
, type
, port
, SCLASS_NORMAL
, 0);