]> jfr.im git - irc/quakenet/newserv.git/blame - proxyscan/proxyscancache.c
attempt to tidy P a little:
[irc/quakenet/newserv.git] / proxyscan / proxyscancache.c
CommitLineData
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 12time_t cleanscaninterval;
13time_t dirtyscaninterval;
14
15void cachehostinit(time_t ri) {
16 cleanscaninterval=ri;
17 dirtyscaninterval=ri*7;
905c2ba2 18}
19
557c8cb2 20cachehost *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
31void 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 45cachehost *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
71void 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
7ab80d0c 78 if ((fp=fopen("data/cleanhosts","w"))==NULL) {
905c2ba2 79 Error("proxyscan",ERR_ERROR,"Unable to open cleanhosts file for writing!");
80 return;
81 }
82
7ab80d0c 83 PATRICIA_WALK_CLEAR (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)) {
7ab80d0c
P
89 derefnode(iptree,node);
90 delcachehost(chp);
91 node->exts[ps_cache_ext] = NULL;
a8ba1373 92 } else
905c2ba2 93
a8ba1373
P
94 for (fpp=chp->proxies;fpp;fpp=fpp->next)
95 fprintf(fp, "%s %lu %u %i %u\n",IPtostr(node->prefix->sin),chp->lastscan,chp->glineid,fpp->type,fpp->port);
96 } else {
97 if (chp->lastscan < (now-cleanscaninterval)) {
98 /* Needs rescan anyway, so delete it */
7ab80d0c
P
99 derefnode(iptree,node);
100 delcachehost(chp);
101 node->exts[ps_cache_ext] = NULL;
a8ba1373
P
102 } else
103 fprintf(fp,"%s %lu\n",IPtostr(node->prefix->sin),chp->lastscan);
905c2ba2 104 }
905c2ba2 105 }
106 }
7ab80d0c
P
107 } PATRICIA_WALK_CLEAR_END;
108
109// patricia_tidy_tree(iptree);
905c2ba2 110
111 fclose(fp);
112}
113
114/*
115 * loadcleanhosts:
116 * Loads clean hosts in from database.
117 */
118
119void loadcachehosts() {
120 FILE *fp;
edd0553f 121 unsigned long timestamp,glineid,ptype,pport;
905c2ba2 122 char buf[512];
123 cachehost *chp=NULL;
124 foundproxy *fpp;
557c8cb2 125 char ip[512];
905c2ba2 126 int res;
557c8cb2
P
127 struct irc_in_addr sin;
128 unsigned char bits;
129 patricia_node_t *node;
7ab80d0c 130 int i=0;
557c8cb2 131
7ab80d0c 132 if ((fp=fopen("data/cleanhosts","r"))==NULL) {
905c2ba2 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,&timestamp,&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 ) {
7ab80d0c 154 i++;
557c8cb2 155 chp=addcleanhost(timestamp);
a8ba1373 156 node->exts[ps_cache_ext] = chp;
905c2ba2 157
557c8cb2
P
158 if (res==5) {
159 chp->glineid=glineid;
160 fpp=getfoundproxy();
161 fpp->type=ptype;
162 fpp->port=pport;
163 fpp->next=chp->proxies;
164 chp->proxies=fpp;
165 }
166 }
905c2ba2 167 }
168 }
7ab80d0c
P
169
170 Error("proxyscan",ERR_INFO, "Loaded %d entries from cache", i);
905c2ba2 171}
172
173/*
174 * cleancount:
175 * Returns the number of "clean" host entries present
176 */
177
178unsigned int cleancount() {
905c2ba2 179 unsigned int total=0;
180 cachehost *chp;
a8ba1373
P
181 patricia_node_t *head, *node;
182 head = iptree->head;
183 PATRICIA_WALK (head, node) {
184 if ( node->exts[ps_cache_ext] ) {
185 chp = (cachehost *) node->exts[ps_cache_ext];
905c2ba2 186
905c2ba2 187 if (!chp->proxies)
188 total++;
189 }
557c8cb2 190 } PATRICIA_WALK_END;
905c2ba2 191
192 return total;
193}
194
195unsigned int dirtycount() {
905c2ba2 196 unsigned int total=0;
197 cachehost *chp;
557c8cb2 198 patricia_node_t *node;
905c2ba2 199
557c8cb2 200 PATRICIA_WALK (iptree->head, node) {
a8ba1373
P
201 if ( node->exts[ps_cache_ext] ) {
202 chp = (cachehost *) node->exts[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
216void scanall(int type, int port) {
217 int i;
edd0553f 218 cachehost *chp;
905c2ba2 219 nick *np;
220 unsigned int hostmarker;
557c8cb2 221 patricia_node_t *node;
905c2ba2 222
223 hostmarker=nexthostmarker();
224
557c8cb2 225 PATRICIA_WALK (iptree->head, node) {
a8ba1373
P
226 if ( node->exts[ps_cache_ext] ) {
227 chp = (cachehost *) node->exts[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}