]>
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; |
22 | int hash; | |
23 | ||
905c2ba2 | 24 | chp=getcachehost(); |
905c2ba2 | 25 | chp->lastscan=timestamp; |
26 | chp->proxies=NULL; | |
27 | chp->glineid=0; | |
905c2ba2 | 28 | |
29 | return chp; | |
30 | } | |
31 | ||
32 | void delcachehost(cachehost *chp) { | |
33 | cachehost **chh; | |
34 | foundproxy *fpp, *nfpp; | |
905c2ba2 | 35 | |
905c2ba2 | 36 | for (fpp=chp->proxies;fpp;fpp=nfpp) { |
37 | nfpp=fpp->next; | |
38 | freefoundproxy(fpp); | |
39 | } | |
40 | freecachehost(chp); | |
41 | } | |
42 | ||
43 | /* | |
44 | * Returns a cachehost * for the named IP | |
45 | */ | |
46 | ||
557c8cb2 | 47 | cachehost *findcachehost(patricia_node_t *node) { |
905c2ba2 | 48 | int hash; |
49 | cachehost *chp; | |
50 | ||
557c8cb2 P |
51 | if( (cachehost *)node->slots[ps_cache_ext] ) { |
52 | chp = (cachehost *)node->slots[ps_cache_ext]; | |
53 | if(chp->lastscan < (time(NULL)-(chp->proxies ? dirtyscaninterval : cleanscaninterval))) { | |
54 | /* Needs rescan; delete and return 1 */ | |
55 | delcachehost(chp); | |
56 | patricia_deref_prefix(node->prefix); | |
57 | node->slots[ps_cache_ext] = NULL; | |
58 | return NULL; | |
59 | } else { | |
60 | /* valid: return it */ | |
61 | return chp; | |
905c2ba2 | 62 | } |
63 | } | |
64 | ||
65 | /* Not found: return NULL */ | |
66 | return NULL; | |
67 | } | |
68 | ||
69 | /* | |
70 | * dumpcleanhosts: | |
71 | * Dumps all cached hosts to a savefile. Expires hosts as it goes along | |
72 | */ | |
73 | ||
74 | void dumpcachehosts(void *arg) { | |
75 | int i; | |
76 | FILE *fp; | |
557c8cb2 | 77 | cachehost *chp; |
905c2ba2 | 78 | time_t now=time(NULL); |
79 | foundproxy *fpp; | |
557c8cb2 | 80 | patricia_node_t *node; |
905c2ba2 | 81 | |
82 | if ((fp=fopen("cleanhosts","w"))==NULL) { | |
83 | Error("proxyscan",ERR_ERROR,"Unable to open cleanhosts file for writing!"); | |
84 | return; | |
85 | } | |
86 | ||
557c8cb2 P |
87 | PATRICIA_WALK (iptree->head, node) { |
88 | if ( node->slots[ps_cache_ext] ) { | |
89 | chp = (cachehost *) node->slots[ps_cache_ext]; | |
905c2ba2 | 90 | if (chp->proxies) { |
91 | if (chp->lastscan < (now-dirtyscaninterval)) { | |
557c8cb2 P |
92 | patricia_deref_prefix(node->prefix); |
93 | delcachehost(chp); | |
905c2ba2 | 94 | continue; |
95 | } | |
96 | ||
97 | for (fpp=chp->proxies;fpp;fpp=fpp->next) | |
557c8cb2 | 98 | fprintf(fp, "%s %lu %u %i %u\n",IPtostr(node->prefix->sin),chp->lastscan,chp->glineid,fpp->type,fpp->port); |
905c2ba2 | 99 | } else { |
100 | if (chp->lastscan < (now-cleanscaninterval)) { | |
101 | /* Needs rescan anyway, so delete it */ | |
557c8cb2 | 102 | patricia_deref_prefix(node->prefix); |
905c2ba2 | 103 | delcachehost(chp); |
104 | continue; | |
105 | } | |
557c8cb2 | 106 | fprintf(fp,"%s %lu\n",IPtostr(node->prefix->sin),chp->lastscan); |
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; | |
122 | unsigned long IP,timestamp,glineid,ptype,pport; | |
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 | ||
557c8cb2 | 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); | |
155 | node->slots[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() { | |
177 | int i; | |
178 | unsigned int total=0; | |
179 | cachehost *chp; | |
557c8cb2 P |
180 | patricia_node_t *node; |
181 | ||
182 | PATRICIA_WALK (iptree->head, node) { | |
183 | if ( node->slots[ps_cache_ext] ) { | |
184 | chp = (cachehost *) node->slots[ps_cache_ext]; | |
905c2ba2 | 185 | |
905c2ba2 | 186 | if (!chp->proxies) |
187 | total++; | |
188 | } | |
557c8cb2 | 189 | } PATRICIA_WALK_END; |
905c2ba2 | 190 | |
191 | return total; | |
192 | } | |
193 | ||
194 | unsigned int dirtycount() { | |
195 | int i; | |
196 | unsigned int total=0; | |
197 | cachehost *chp; | |
557c8cb2 | 198 | patricia_node_t *node; |
905c2ba2 | 199 | |
557c8cb2 P |
200 | PATRICIA_WALK (iptree->head, node) { |
201 | if ( node->slots[ps_cache_ext] ) { | |
202 | chp = (cachehost *) node->slots[ps_cache_ext]; | |
905c2ba2 | 203 | if (chp->proxies) |
204 | total++; | |
205 | } | |
557c8cb2 | 206 | } PATRICIA_WALK_END; |
905c2ba2 | 207 | |
208 | return total; | |
209 | } | |
210 | ||
211 | /* | |
212 | * scanall: | |
213 | * Scans all hosts on the network for a given proxy, and updates the cache accordingly | |
214 | */ | |
215 | ||
216 | void scanall(int type, int port) { | |
217 | int i; | |
218 | cachehost *chp, *nchp; | |
219 | nick *np; | |
220 | unsigned int hostmarker; | |
557c8cb2 | 221 | patricia_node_t *node; |
905c2ba2 | 222 | |
223 | hostmarker=nexthostmarker(); | |
224 | ||
557c8cb2 P |
225 | PATRICIA_WALK (iptree->head, node) { |
226 | if ( node->slots[ps_cache_ext] ) { | |
227 | chp = (cachehost *) node->slots[ps_cache_ext]; | |
905c2ba2 | 228 | chp->marker=0; |
557c8cb2 P |
229 | } |
230 | } PATRICIA_WALK_END; | |
905c2ba2 | 231 | |
232 | for (i=0;i<NICKHASHSIZE;i++) { | |
233 | for (np=nicktable[i];np;np=np->next) { | |
234 | if (np->host->marker==hostmarker) | |
235 | continue; | |
236 | ||
237 | np->host->marker=hostmarker; | |
238 | ||
c9db668b P |
239 | if (!irc_in_addr_is_ipv4(&np->p_ipaddr)) |
240 | continue; | |
241 | ||
557c8cb2 | 242 | if ((chp=findcachehost(np->ipnode))) |
905c2ba2 | 243 | chp->marker=1; |
244 | ||
557c8cb2 | 245 | queuescan(np->ipnode, type, port, SCLASS_NORMAL, 0); |
905c2ba2 | 246 | } |
247 | } | |
248 | } |