]>
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
;
31 void delcachehost(cachehost
*chp
) {
32 foundproxy
*fpp
, *nfpp
;
34 for (fpp
=chp
->proxies
;fpp
;fpp
=nfpp
) {
42 * Returns a cachehost * for the named IP
45 cachehost
*findcachehost(patricia_node_t
*node
) {
48 if( (cachehost
*)node
->exts
[ps_cache_ext
] ) {
49 chp
= (cachehost
*)node
->exts
[ps_cache_ext
];
50 if(chp
->lastscan
< (time(NULL
)-(chp
->proxies
? dirtyscaninterval
: cleanscaninterval
))) {
51 /* Needs rescan; delete and return 1 */
53 derefnode(iptree
,node
);
54 node
->exts
[ps_cache_ext
] = NULL
;
57 /* valid: return it */
62 /* Not found: return NULL */
68 * Dumps all cached hosts to a savefile. Expires hosts as it goes along
71 void dumpcachehosts(void *arg
) {
74 time_t now
=time(NULL
);
76 patricia_node_t
*node
;
78 if ((fp
=fopen("data/cleanhosts","w"))==NULL
) {
79 Error("proxyscan",ERR_ERROR
,"Unable to open cleanhosts file for writing!");
83 PATRICIA_WALK_CLEAR (iptree
->head
, node
) {
84 if (node
->exts
[ps_cache_ext
] ) {
85 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
88 if (chp
->lastscan
< (now
-dirtyscaninterval
)) {
89 derefnode(iptree
,node
);
91 node
->exts
[ps_cache_ext
] = NULL
;
94 for (fpp
=chp
->proxies
;fpp
;fpp
=fpp
->next
)
95 fprintf(fp
, "%s %lu %u %i %u\n",IPtostr(node
->prefix
->sin
),chp
->lastscan
,chp
->glineid
,fpp
->type
,fpp
->port
);
97 if (chp
->lastscan
< (now
-cleanscaninterval
)) {
98 /* Needs rescan anyway, so delete it */
99 derefnode(iptree
,node
);
101 node
->exts
[ps_cache_ext
] = NULL
;
103 fprintf(fp
,"%s %lu\n",IPtostr(node
->prefix
->sin
),chp
->lastscan
);
107 } PATRICIA_WALK_CLEAR_END
;
109 // patricia_tidy_tree(iptree);
116 * Loads clean hosts in from database.
119 void loadcachehosts() {
121 unsigned long timestamp
,glineid
,ptype
,pport
;
127 struct irc_in_addr sin
;
129 patricia_node_t
*node
;
132 if ((fp
=fopen("data/cleanhosts","r"))==NULL
) {
133 Error("proxyscan",ERR_ERROR
,"Unable to open cleanhosts file for reading!");
144 res
=sscanf(buf
,"%s %lu %lu %lu %lu",ip
,×tamp
,&glineid
,&ptype
,&pport
);
149 if (0 == ipmask_parse(ip
,&sin
, &bits
)) {
152 node
= refnode(iptree
, &sin
, bits
);
155 chp
=addcleanhost(timestamp
);
156 node
->exts
[ps_cache_ext
] = chp
;
159 chp
->glineid
=glineid
;
163 fpp
->next
=chp
->proxies
;
170 Error("proxyscan",ERR_INFO
, "Loaded %d entries from cache", i
);
175 * Returns the number of "clean" host entries present
178 unsigned int cleancount() {
179 unsigned int total
=0;
181 patricia_node_t
*head
, *node
;
183 PATRICIA_WALK (head
, node
) {
184 if ( node
->exts
[ps_cache_ext
] ) {
185 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
195 unsigned int dirtycount() {
196 unsigned int total
=0;
198 patricia_node_t
*node
;
200 PATRICIA_WALK (iptree
->head
, node
) {
201 if ( node
->exts
[ps_cache_ext
] ) {
202 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
213 * Scans all hosts on the network for a given proxy, and updates the cache accordingly
216 void scanall(int type
, int port
) {
220 unsigned int hostmarker
;
221 patricia_node_t
*node
;
223 hostmarker
=nexthostmarker();
225 PATRICIA_WALK (iptree
->head
, node
) {
226 if ( node
->exts
[ps_cache_ext
] ) {
227 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
232 for (i
=0;i
<NICKHASHSIZE
;i
++) {
233 for (np
=nicktable
[i
];np
;np
=np
->next
) {
234 if (np
->host
->marker
==hostmarker
)
237 np
->host
->marker
=hostmarker
;
239 if (!irc_in_addr_is_ipv4(&np
->p_ipaddr
))
242 if ((chp
=findcachehost(np
->ipnode
)))
245 queuescan(np
->ipnode
, type
, port
, SCLASS_NORMAL
, 0);