]>
Commit | Line | Data |
---|---|---|
905c2ba2 | 1 | /* |
2 | * proxyscancache.c: | |
3 | * This file deals with the cache of known hosts, clean or otherwise. | |
4 | */ | |
5 | ||
6 | #include "proxyscan.h" | |
7 | #include <time.h> | |
8 | #include <stdio.h> | |
9 | #include "../core/error.h" | |
10 | #include <string.h> | |
11 | ||
905c2ba2 | 12 | time_t cleanscaninterval; |
13 | time_t dirtyscaninterval; | |
14 | ||
15 | void cachehostinit(time_t ri) { | |
16 | cleanscaninterval=ri; | |
17 | dirtyscaninterval=ri*7; | |
905c2ba2 | 18 | } |
19 | ||
557c8cb2 | 20 | cachehost *addcleanhost(time_t timestamp) { |
905c2ba2 | 21 | cachehost *chp; |
905c2ba2 | 22 | |
905c2ba2 | 23 | chp=getcachehost(); |
905c2ba2 | 24 | chp->lastscan=timestamp; |
25 | chp->proxies=NULL; | |
26 | chp->glineid=0; | |
905c2ba2 | 27 | |
28 | return chp; | |
29 | } | |
30 | ||
31 | void delcachehost(cachehost *chp) { | |
905c2ba2 | 32 | foundproxy *fpp, *nfpp; |
905c2ba2 | 33 | |
905c2ba2 | 34 | for (fpp=chp->proxies;fpp;fpp=nfpp) { |
35 | nfpp=fpp->next; | |
36 | freefoundproxy(fpp); | |
37 | } | |
38 | freecachehost(chp); | |
39 | } | |
40 | ||
41 | /* | |
42 | * Returns a cachehost * for the named IP | |
43 | */ | |
44 | ||
557c8cb2 | 45 | cachehost *findcachehost(patricia_node_t *node) { |
905c2ba2 | 46 | cachehost *chp; |
47 | ||
a8ba1373 P |
48 | if( (cachehost *)node->exts[ps_cache_ext] ) { |
49 | chp = (cachehost *)node->exts[ps_cache_ext]; | |
557c8cb2 P |
50 | if(chp->lastscan < (time(NULL)-(chp->proxies ? dirtyscaninterval : cleanscaninterval))) { |
51 | /* Needs rescan; delete and return 1 */ | |
52 | delcachehost(chp); | |
a8ba1373 P |
53 | derefnode(iptree,node); |
54 | node->exts[ps_cache_ext] = NULL; | |
557c8cb2 P |
55 | return NULL; |
56 | } else { | |
57 | /* valid: return it */ | |
58 | return chp; | |
905c2ba2 | 59 | } |
60 | } | |
61 | ||
62 | /* Not found: return NULL */ | |
63 | return NULL; | |
64 | } | |
65 | ||
66 | /* | |
67 | * dumpcleanhosts: | |
68 | * Dumps all cached hosts to a savefile. Expires hosts as it goes along | |
69 | */ | |
70 | ||
71 | void dumpcachehosts(void *arg) { | |
905c2ba2 | 72 | FILE *fp; |
557c8cb2 | 73 | cachehost *chp; |
905c2ba2 | 74 | time_t now=time(NULL); |
75 | foundproxy *fpp; | |
557c8cb2 | 76 | patricia_node_t *node; |
905c2ba2 | 77 | |
78 | if ((fp=fopen("cleanhosts","w"))==NULL) { | |
79 | Error("proxyscan",ERR_ERROR,"Unable to open cleanhosts file for writing!"); | |
80 | return; | |
81 | } | |
82 | ||
557c8cb2 | 83 | PATRICIA_WALK (iptree->head, node) { |
a8ba1373 P |
84 | if (node->exts[ps_cache_ext] ) { |
85 | chp = (cachehost *) node->exts[ps_cache_ext]; | |
86 | if (chp) { | |
87 | if (chp->proxies) { | |
88 | if (chp->lastscan < (now-dirtyscaninterval)) { | |
89 | //derefnode(iptree,node); | |
90 | //delcachehost(chp); | |
91 | //node->exts[ps_cache_ext] = NULL; | |
92 | //continue; | |
93 | } else | |
905c2ba2 | 94 | |
a8ba1373 P |
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); | |
97 | } else { | |
98 | if (chp->lastscan < (now-cleanscaninterval)) { | |
99 | /* Needs rescan anyway, so delete it */ | |
100 | //derefnode(iptree,node); | |
101 | //delcachehost(chp); | |
102 | //node->exts[ps_cache_ext] = NULL; | |
103 | //continue; | |
104 | } else | |
105 | fprintf(fp,"%s %lu\n",IPtostr(node->prefix->sin),chp->lastscan); | |
905c2ba2 | 106 | } |
905c2ba2 | 107 | } |
108 | } | |
557c8cb2 P |
109 | } PATRICIA_WALK_END; |
110 | ||
905c2ba2 | 111 | |
112 | fclose(fp); | |
113 | } | |
114 | ||
115 | /* | |
116 | * loadcleanhosts: | |
117 | * Loads clean hosts in from database. | |
118 | */ | |
119 | ||
120 | void loadcachehosts() { | |
121 | FILE *fp; | |
edd0553f | 122 | unsigned long timestamp,glineid,ptype,pport; |
905c2ba2 | 123 | char buf[512]; |
124 | cachehost *chp=NULL; | |
125 | foundproxy *fpp; | |
557c8cb2 | 126 | char ip[512]; |
905c2ba2 | 127 | int res; |
557c8cb2 P |
128 | struct irc_in_addr sin; |
129 | unsigned char bits; | |
130 | patricia_node_t *node; | |
131 | ||
905c2ba2 | 132 | if ((fp=fopen("cleanhosts","r"))==NULL) { |
133 | Error("proxyscan",ERR_ERROR,"Unable to open cleanhosts file for reading!"); | |
134 | return; | |
135 | } | |
136 | ||
557c8cb2 | 137 | |
905c2ba2 | 138 | while (!feof(fp)) { |
139 | fgets(buf,512,fp); | |
140 | if (feof(fp)) { | |
141 | break; | |
142 | } | |
143 | ||
edd0553f | 144 | res=sscanf(buf,"%s %lu %lu %lu %lu",ip,×tamp,&glineid,&ptype,&pport); |
905c2ba2 | 145 | |
146 | if (res<2) | |
147 | continue; | |
148 | ||
557c8cb2 P |
149 | if (0 == ipmask_parse(ip,&sin, &bits)) { |
150 | /* invalid mask */ | |
151 | } else { | |
152 | node = refnode(iptree, &sin, bits); | |
153 | if( node ) { | |
154 | chp=addcleanhost(timestamp); | |
a8ba1373 | 155 | node->exts[ps_cache_ext] = chp; |
905c2ba2 | 156 | |
557c8cb2 P |
157 | if (res==5) { |
158 | chp->glineid=glineid; | |
159 | fpp=getfoundproxy(); | |
160 | fpp->type=ptype; | |
161 | fpp->port=pport; | |
162 | fpp->next=chp->proxies; | |
163 | chp->proxies=fpp; | |
164 | } | |
165 | } | |
905c2ba2 | 166 | } |
167 | } | |
557c8cb2 | 168 | |
905c2ba2 | 169 | } |
170 | ||
171 | /* | |
172 | * cleancount: | |
173 | * Returns the number of "clean" host entries present | |
174 | */ | |
175 | ||
176 | unsigned int cleancount() { | |
905c2ba2 | 177 | unsigned int total=0; |
178 | cachehost *chp; | |
a8ba1373 P |
179 | patricia_node_t *head, *node; |
180 | head = iptree->head; | |
181 | PATRICIA_WALK (head, node) { | |
182 | if ( node->exts[ps_cache_ext] ) { | |
183 | chp = (cachehost *) node->exts[ps_cache_ext]; | |
905c2ba2 | 184 | |
905c2ba2 | 185 | if (!chp->proxies) |
186 | total++; | |
187 | } | |
557c8cb2 | 188 | } PATRICIA_WALK_END; |
905c2ba2 | 189 | |
190 | return total; | |
191 | } | |
192 | ||
193 | unsigned int dirtycount() { | |
905c2ba2 | 194 | unsigned int total=0; |
195 | cachehost *chp; | |
557c8cb2 | 196 | patricia_node_t *node; |
905c2ba2 | 197 | |
557c8cb2 | 198 | PATRICIA_WALK (iptree->head, node) { |
a8ba1373 P |
199 | if ( node->exts[ps_cache_ext] ) { |
200 | chp = (cachehost *) node->exts[ps_cache_ext]; | |
905c2ba2 | 201 | if (chp->proxies) |
202 | total++; | |
203 | } | |
557c8cb2 | 204 | } PATRICIA_WALK_END; |
905c2ba2 | 205 | |
206 | return total; | |
207 | } | |
208 | ||
209 | /* | |
210 | * scanall: | |
211 | * Scans all hosts on the network for a given proxy, and updates the cache accordingly | |
212 | */ | |
213 | ||
214 | void scanall(int type, int port) { | |
215 | int i; | |
edd0553f | 216 | cachehost *chp; |
905c2ba2 | 217 | nick *np; |
218 | unsigned int hostmarker; | |
557c8cb2 | 219 | patricia_node_t *node; |
905c2ba2 | 220 | |
221 | hostmarker=nexthostmarker(); | |
222 | ||
557c8cb2 | 223 | PATRICIA_WALK (iptree->head, node) { |
a8ba1373 P |
224 | if ( node->exts[ps_cache_ext] ) { |
225 | chp = (cachehost *) node->exts[ps_cache_ext]; | |
905c2ba2 | 226 | chp->marker=0; |
557c8cb2 P |
227 | } |
228 | } PATRICIA_WALK_END; | |
905c2ba2 | 229 | |
230 | for (i=0;i<NICKHASHSIZE;i++) { | |
231 | for (np=nicktable[i];np;np=np->next) { | |
232 | if (np->host->marker==hostmarker) | |
233 | continue; | |
234 | ||
235 | np->host->marker=hostmarker; | |
236 | ||
c9db668b P |
237 | if (!irc_in_addr_is_ipv4(&np->p_ipaddr)) |
238 | continue; | |
239 | ||
557c8cb2 | 240 | if ((chp=findcachehost(np->ipnode))) |
905c2ba2 | 241 | chp->marker=1; |
242 | ||
557c8cb2 | 243 | queuescan(np->ipnode, type, port, SCLASS_NORMAL, 0); |
905c2ba2 | 244 | } |
245 | } | |
246 | } |