]> jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscancache.c
81dd5f2f5e61ab415a840fabd8da099bc9f68f91
[irc/quakenet/newserv.git] / proxyscan / proxyscancache.c
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
28 return chp;
29 }
30
31 void delcachehost(cachehost *chp) {
32 foundproxy *fpp, *nfpp;
33
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
45 cachehost *findcachehost(patricia_node_t *node) {
46 cachehost *chp;
47
48 if( (cachehost *)node->exts[ps_cache_ext] ) {
49 chp = (cachehost *)node->exts[ps_cache_ext];
50 if(chp->lastscan < (time(NULL)-(chp->proxies ? dirtyscaninterval : cleanscaninterval))) {
51 /* Needs rescan; delete and return 1 */
52 delcachehost(chp);
53 derefnode(iptree,node);
54 node->exts[ps_cache_ext] = NULL;
55 return NULL;
56 } else {
57 /* valid: return it */
58 return chp;
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) {
72 FILE *fp;
73 cachehost *chp;
74 time_t now=time(NULL);
75 foundproxy *fpp;
76 patricia_node_t *node;
77
78 if ((fp=fopen("cleanhosts","w"))==NULL) {
79 Error("proxyscan",ERR_ERROR,"Unable to open cleanhosts file for writing!");
80 return;
81 }
82
83 PATRICIA_WALK (iptree->head, node) {
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
94
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);
106 }
107 }
108 }
109 } PATRICIA_WALK_END;
110
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;
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
132 if ((fp=fopen("cleanhosts","r"))==NULL) {
133 Error("proxyscan",ERR_ERROR,"Unable to open cleanhosts file for reading!");
134 return;
135 }
136
137
138 while (!feof(fp)) {
139 fgets(buf,512,fp);
140 if (feof(fp)) {
141 break;
142 }
143
144 res=sscanf(buf,"%s %lu %lu %lu %lu",ip,&timestamp,&glineid,&ptype,&pport);
145
146 if (res<2)
147 continue;
148
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->exts[ps_cache_ext] = chp;
156
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 }
166 }
167 }
168
169 }
170
171 /*
172 * cleancount:
173 * Returns the number of "clean" host entries present
174 */
175
176 unsigned int cleancount() {
177 unsigned int total=0;
178 cachehost *chp;
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];
184
185 if (!chp->proxies)
186 total++;
187 }
188 } PATRICIA_WALK_END;
189
190 return total;
191 }
192
193 unsigned int dirtycount() {
194 unsigned int total=0;
195 cachehost *chp;
196 patricia_node_t *node;
197
198 PATRICIA_WALK (iptree->head, node) {
199 if ( node->exts[ps_cache_ext] ) {
200 chp = (cachehost *) node->exts[ps_cache_ext];
201 if (chp->proxies)
202 total++;
203 }
204 } PATRICIA_WALK_END;
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;
216 cachehost *chp;
217 nick *np;
218 unsigned int hostmarker;
219 patricia_node_t *node;
220
221 hostmarker=nexthostmarker();
222
223 PATRICIA_WALK (iptree->head, node) {
224 if ( node->exts[ps_cache_ext] ) {
225 chp = (cachehost *) node->exts[ps_cache_ext];
226 chp->marker=0;
227 }
228 } PATRICIA_WALK_END;
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
237 if (!irc_in_addr_is_ipv4(&np->p_ipaddr))
238 continue;
239
240 if ((chp=findcachehost(np->ipnode)))
241 chp->marker=1;
242
243 queuescan(np->ipnode, type, port, SCLASS_NORMAL, 0);
244 }
245 }
246 }