]>
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("cleanhosts","w"))==NULL
) {
79 Error("proxyscan",ERR_ERROR
,"Unable to open cleanhosts file for writing!");
83 PATRICIA_WALK (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;
95 for (fpp
=chp
->proxies
;fpp
;fpp
=fpp
->next
)
96 fprintf(fp
, "%s %lu %u %i %u\n",IPtostr(node
->prefix
->sin
),chp
->lastscan
,chp
->glineid
,fpp
->type
,fpp
->port
);
98 if (chp
->lastscan
< (now
-cleanscaninterval
)) {
99 /* Needs rescan anyway, so delete it */
100 //derefnode(iptree,node);
102 //node->exts[ps_cache_ext] = NULL;
105 fprintf(fp
,"%s %lu\n",IPtostr(node
->prefix
->sin
),chp
->lastscan
);
117 * Loads clean hosts in from database.
120 void loadcachehosts() {
122 unsigned long timestamp
,glineid
,ptype
,pport
;
128 struct irc_in_addr sin
;
130 patricia_node_t
*node
;
132 if ((fp
=fopen("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
);
154 chp
=addcleanhost(timestamp
);
155 node
->exts
[ps_cache_ext
] = chp
;
158 chp
->glineid
=glineid
;
162 fpp
->next
=chp
->proxies
;
173 * Returns the number of "clean" host entries present
176 unsigned int cleancount() {
177 unsigned int total
=0;
179 patricia_node_t
*head
, *node
;
181 PATRICIA_WALK (head
, node
) {
182 if ( node
->exts
[ps_cache_ext
] ) {
183 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
193 unsigned int dirtycount() {
194 unsigned int total
=0;
196 patricia_node_t
*node
;
198 PATRICIA_WALK (iptree
->head
, node
) {
199 if ( node
->exts
[ps_cache_ext
] ) {
200 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
211 * Scans all hosts on the network for a given proxy, and updates the cache accordingly
214 void scanall(int type
, int port
) {
218 unsigned int hostmarker
;
219 patricia_node_t
*node
;
221 hostmarker
=nexthostmarker();
223 PATRICIA_WALK (iptree
->head
, node
) {
224 if ( node
->exts
[ps_cache_ext
] ) {
225 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
230 for (i
=0;i
<NICKHASHSIZE
;i
++) {
231 for (np
=nicktable
[i
];np
;np
=np
->next
) {
232 if (np
->host
->marker
==hostmarker
)
235 np
->host
->marker
=hostmarker
;
237 if (!irc_in_addr_is_ipv4(&np
->p_ipaddr
))
240 if ((chp
=findcachehost(np
->ipnode
)))
243 queuescan(np
->ipnode
, type
, port
, SCLASS_NORMAL
, 0);