]>
jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscancache.c
168100293b7daeda1000cf6cb555fc654b53c1cc
3 * This file deals with the cache of known hosts, clean or otherwise.
9 #include "../core/error.h"
12 #define HOSTCACHEHASHSIZE 50000
14 cachehost
*cachetable
[HOSTCACHEHASHSIZE
];
15 time_t cleanscaninterval
;
16 time_t dirtyscaninterval
;
18 void cachehostinit(time_t ri
) {
20 dirtyscaninterval
=ri
*7;
21 memset(cachetable
,0,sizeof(cachetable
));
24 cachehost
*addcleanhost(unsigned long IP
, time_t timestamp
) {
28 hash
=(IP%HOSTCACHEHASHSIZE
);
32 chp
->lastscan
=timestamp
;
35 chp
->next
=cachetable
[hash
];
42 void delcachehost(cachehost
*chp
) {
44 foundproxy
*fpp
, *nfpp
;
47 hash
=(chp
->IP%HOSTCACHEHASHSIZE
);
49 for (chh
=&(cachetable
[hash
]);*chh
;chh
=&((*chh
)->next
)) {
56 for (fpp
=chp
->proxies
;fpp
;fpp
=nfpp
) {
64 * Returns a cachehost * for the named IP
67 cachehost
*findcachehost(unsigned long IP
) {
71 hash
=(IP%HOSTCACHEHASHSIZE
);
73 for (chp
=cachetable
[hash
];chp
;chp
=chp
->next
) {
76 if(chp
->lastscan
< (time(NULL
)-(chp
->proxies
? dirtyscaninterval
: cleanscaninterval
))) {
77 /* Needs rescan; delete and return 1 */
81 /* valid: return it */
87 /* Not found: return NULL */
93 * Dumps all cached hosts to a savefile. Expires hosts as it goes along
96 void dumpcachehosts(void *arg
) {
100 time_t now
=time(NULL
);
103 if ((fp
=fopen("cleanhosts","w"))==NULL
) {
104 Error("proxyscan",ERR_ERROR
,"Unable to open cleanhosts file for writing!");
108 for(i
=0;i
<HOSTCACHEHASHSIZE
;i
++) {
109 for(chp
=cachetable
[i
];chp
;chp
=nchp
) {
112 if (chp
->lastscan
< (now
-dirtyscaninterval
)) {
117 for (fpp
=chp
->proxies
;fpp
;fpp
=fpp
->next
)
118 fprintf(fp
, "%lu %lu %u %i %u\n",chp
->IP
,chp
->lastscan
,chp
->glineid
,fpp
->type
,fpp
->port
);
120 if (chp
->lastscan
< (now
-cleanscaninterval
)) {
121 /* Needs rescan anyway, so delete it */
125 fprintf(fp
,"%lu %lu\n",chp
->IP
,chp
->lastscan
);
135 * Loads clean hosts in from database.
138 void loadcachehosts() {
140 unsigned long IP
,timestamp
,glineid
,ptype
,pport
;
146 if ((fp
=fopen("cleanhosts","r"))==NULL
) {
147 Error("proxyscan",ERR_ERROR
,"Unable to open cleanhosts file for reading!");
157 res
=sscanf(buf
,"%lu %lu %lu %lu %lu",&IP
,×tamp
,&glineid
,&ptype
,&pport
);
162 if (!chp
|| (chp
->IP
!= IP
))
163 chp
=addcleanhost(IP
, timestamp
);
166 chp
->glineid
=glineid
;
170 fpp
->next
=chp
->proxies
;
178 * Returns the number of "clean" host entries present
181 unsigned int cleancount() {
183 unsigned int total
=0;
186 for(i
=0;i
<HOSTCACHEHASHSIZE
;i
++) {
187 for (chp
=cachetable
[i
];chp
;chp
=chp
->next
) {
196 unsigned int dirtycount() {
198 unsigned int total
=0;
201 for(i
=0;i
<HOSTCACHEHASHSIZE
;i
++) {
202 for (chp
=cachetable
[i
];chp
;chp
=chp
->next
) {
213 * Scans all hosts on the network for a given proxy, and updates the cache accordingly
216 void scanall(int type
, int port
) {
218 cachehost
*chp
, *nchp
;
220 unsigned int hostmarker
;
222 hostmarker
=nexthostmarker();
224 for (i
=0;i
<HOSTCACHEHASHSIZE
;i
++)
225 for (chp
=cachetable
[i
];chp
;chp
=chp
->next
)
228 for (i
=0;i
<NICKHASHSIZE
;i
++) {
229 for (np
=nicktable
[i
];np
;np
=np
->next
) {
230 if (np
->host
->marker
==hostmarker
)
233 np
->host
->marker
=hostmarker
;
235 if ((chp
=findcachehost(np
->ipaddress
)))
238 queuescan(np
->ipaddress
, type
, port
, SCLASS_NORMAL
, 0);
242 for (i
=0;i
<HOSTCACHEHASHSIZE
;i
++) {
243 for (chp
=cachetable
[i
];chp
;chp
=nchp
) {
245 if (!chp
->proxies
&& !chp
->marker
)