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