]> jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscanclean.c
Initial Import
[irc/quakenet/newserv.git] / proxyscan / proxyscanclean.c
1 /*
2 * proxyscanclean.c:
3 * This file deals with the "clean" hosts.
4 */
5
6 #include "proxyscan.h"
7 #include <time.h>
8 #include <stdio.h>
9 #include "../core/error.h"
10
11 #define CLEANHOSTHASHSIZE 20000
12
13 cleanhost *cleantable[CLEANHOSTHASHSIZE];
14 time_t rescaninterval;
15
16 void cleanhostinit(time_t ri) {
17 rescaninterval=ri;
18 memset(cleantable,0,sizeof(cleantable));
19 }
20
21 void addcleanhost(unsigned long IP, time_t timestamp) {
22 cleanhost *chp;
23 int hash;
24
25 hash=(IP%CLEANHOSTHASHSIZE);
26
27 chp=getcleanhost();
28 chp->IP=IP;
29 chp->lastscan=timestamp;
30 chp->next=cleantable[hash];
31
32 cleantable[hash]=chp;
33 }
34
35 void delcleanhost(cleanhost *chp) {
36 cleanhost **chh;
37 int hash;
38
39 hash=(chp->IP%CLEANHOSTHASHSIZE);
40
41 for (chh=&(cleantable[hash]);*chh;chh=&((*chh)->next)) {
42 if (*chh==chp) {
43 *chh=chp->next;
44 break;
45 }
46 }
47
48 freecleanhost(chp);
49 }
50
51 /*
52 * checkcleanhost:
53 * Returns:
54 * 0 - Host has been checked recently and is clean
55 * 1 - Host has not been checked recently, or was not clean
56 */
57
58 int checkcleanhost(unsigned long IP) {
59 int hash;
60 cleanhost *chp;
61
62 hash=(IP%CLEANHOSTHASHSIZE);
63
64 for (chp=cleantable[hash];chp;chp=chp->next) {
65 if (chp->IP==IP) {
66 /* match */
67 if(chp->lastscan < (time(NULL)-rescaninterval)) {
68 /* Needs rescan; delete and return 1 */
69 delcleanhost(chp);
70 return 1;
71 } else {
72 /* Clean */
73 return 0;
74 }
75 }
76 }
77
78 /* Not found: return 1 */
79 return 1;
80 }
81
82 /*
83 * dumpcleanhosts:
84 * Dumps all clean hosts to a savefile. Expires hosts as it goes along
85 */
86
87 void dumpcleanhosts(void *arg) {
88 int i;
89 FILE *fp;
90 cleanhost *chp,*nchp;
91
92 if ((fp=fopen("cleanhosts","w"))==NULL) {
93 Error("proxyscan",ERR_ERROR,"Unable to open cleanhosts file for writing!");
94 return;
95 }
96
97 for(i=0;i<CLEANHOSTHASHSIZE;i++) {
98 for(chp=cleantable[i];chp;chp=nchp) {
99 nchp=chp->next;
100 if (chp->lastscan < (time(NULL)-rescaninterval)) {
101 /* Needs rescan anyway, so delete it */
102 delcleanhost(chp);
103 } else {
104 fprintf(fp,"%lu %lu\n",chp->IP,chp->lastscan);
105 }
106 }
107 }
108
109 fclose(fp);
110 }
111
112 /*
113 * loadcleanhosts:
114 * Loads clean hosts in from database.
115 */
116
117 void loadcleanhosts() {
118 FILE *fp;
119 unsigned long IP,timestamp;
120 char buf[512];
121
122 if ((fp=fopen("cleanhosts","r"))==NULL) {
123 Error("proxyscan",ERR_ERROR,"Unable to open cleanhosts file for reading!");
124 return;
125 }
126
127 while (!feof(fp)) {
128 fgets(buf,512,fp);
129 if (feof(fp)) {
130 break;
131 }
132
133 if ((sscanf(buf,"%lu %lu",&IP,&timestamp))==2) {
134 addcleanhost(IP,timestamp);
135 }
136 }
137 }
138
139 /*
140 * cleancount:
141 * Returns the number of "clean" host entries present
142 */
143
144 unsigned int cleancount() {
145 int i;
146 unsigned int total=0;
147 cleanhost *chp;
148
149 for(i=0;i<CLEANHOSTHASHSIZE;i++) {
150 for (chp=cleantable[i];chp;chp=chp->next) {
151 total++;
152 }
153 }
154
155 return total;
156 }