]>
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
) {
25 chp
->lastscan
=timestamp
;
32 void delcachehost(cachehost
*chp
) {
34 foundproxy
*fpp
, *nfpp
;
36 for (fpp
=chp
->proxies
;fpp
;fpp
=nfpp
) {
44 * Returns a cachehost * for the named IP
47 cachehost
*findcachehost(patricia_node_t
*node
) {
51 if( (cachehost
*)node
->exts
[ps_cache_ext
] ) {
52 chp
= (cachehost
*)node
->exts
[ps_cache_ext
];
53 if(chp
->lastscan
< (time(NULL
)-(chp
->proxies
? dirtyscaninterval
: cleanscaninterval
))) {
54 /* Needs rescan; delete and return 1 */
56 derefnode(iptree
,node
);
57 node
->exts
[ps_cache_ext
] = NULL
;
60 /* valid: return it */
65 /* Not found: return NULL */
71 * Dumps all cached hosts to a savefile. Expires hosts as it goes along
74 void dumpcachehosts(void *arg
) {
78 time_t now
=time(NULL
);
80 patricia_node_t
*node
;
82 if ((fp
=fopen("cleanhosts","w"))==NULL
) {
83 Error("proxyscan",ERR_ERROR
,"Unable to open cleanhosts file for writing!");
87 PATRICIA_WALK (iptree
->head
, node
) {
88 if (node
->exts
[ps_cache_ext
] ) {
89 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
92 if (chp
->lastscan
< (now
-dirtyscaninterval
)) {
93 //derefnode(iptree,node);
95 //node->exts[ps_cache_ext] = NULL;
99 for (fpp
=chp
->proxies
;fpp
;fpp
=fpp
->next
)
100 fprintf(fp
, "%s %lu %u %i %u\n",IPtostr(node
->prefix
->sin
),chp
->lastscan
,chp
->glineid
,fpp
->type
,fpp
->port
);
102 if (chp
->lastscan
< (now
-cleanscaninterval
)) {
103 /* Needs rescan anyway, so delete it */
104 //derefnode(iptree,node);
106 //node->exts[ps_cache_ext] = NULL;
109 fprintf(fp
,"%s %lu\n",IPtostr(node
->prefix
->sin
),chp
->lastscan
);
121 * Loads clean hosts in from database.
124 void loadcachehosts() {
126 unsigned long IP
,timestamp
,glineid
,ptype
,pport
;
132 struct irc_in_addr sin
;
134 patricia_node_t
*node
;
136 if ((fp
=fopen("cleanhosts","r"))==NULL
) {
137 Error("proxyscan",ERR_ERROR
,"Unable to open cleanhosts file for reading!");
148 res
=sscanf(buf
,"%s %lu %lu %lu %lu",&ip
,×tamp
,&glineid
,&ptype
,&pport
);
153 if (0 == ipmask_parse(ip
,&sin
, &bits
)) {
156 node
= refnode(iptree
, &sin
, bits
);
158 chp
=addcleanhost(timestamp
);
159 node
->exts
[ps_cache_ext
] = chp
;
162 chp
->glineid
=glineid
;
166 fpp
->next
=chp
->proxies
;
177 * Returns the number of "clean" host entries present
180 unsigned int cleancount() {
182 unsigned int total
=0;
184 patricia_node_t
*head
, *node
;
186 PATRICIA_WALK (head
, node
) {
187 if ( node
->exts
[ps_cache_ext
] ) {
188 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
198 unsigned int dirtycount() {
200 unsigned int total
=0;
202 patricia_node_t
*node
;
204 PATRICIA_WALK (iptree
->head
, node
) {
205 if ( node
->exts
[ps_cache_ext
] ) {
206 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
217 * Scans all hosts on the network for a given proxy, and updates the cache accordingly
220 void scanall(int type
, int port
) {
222 cachehost
*chp
, *nchp
;
224 unsigned int hostmarker
;
225 patricia_node_t
*node
;
227 hostmarker
=nexthostmarker();
229 PATRICIA_WALK (iptree
->head
, node
) {
230 if ( node
->exts
[ps_cache_ext
] ) {
231 chp
= (cachehost
*) node
->exts
[ps_cache_ext
];
236 for (i
=0;i
<NICKHASHSIZE
;i
++) {
237 for (np
=nicktable
[i
];np
;np
=np
->next
) {
238 if (np
->host
->marker
==hostmarker
)
241 np
->host
->marker
=hostmarker
;
243 if (!irc_in_addr_is_ipv4(&np
->p_ipaddr
))
246 if ((chp
=findcachehost(np
->ipnode
)))
249 queuescan(np
->ipnode
, type
, port
, SCLASS_NORMAL
, 0);