]> jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscanqueue.c
merge
[irc/quakenet/newserv.git] / proxyscan / proxyscanqueue.c
1
2 /*
3 * Handle the scan queues
4 */
5
6 #include "proxyscan.h"
7 #include "../irc/irc.h"
8 #include "../core/error.h"
9
10 pendingscan *ps_normalqueue=NULL;
11 pendingscan *ps_prioqueue=NULL;
12 pendingscan *ps_normalqueueend=NULL;
13
14 unsigned int normalqueuedscans=0;
15 unsigned int prioqueuedscans=0;
16
17 unsigned long countpendingscan=0;
18
19 void queuescan(patricia_node_t *node, short scantype, unsigned short port, char class, time_t when) {
20 pendingscan *psp, *psp2;
21
22 /* check if the IP/port combo is already queued - don't queue up
23 * multiple identical scans
24 */
25 psp = ps_prioqueue;
26 while (psp != NULL)
27 {
28 if (psp->node == node && psp->type == scantype &&
29 psp->port == port && psp->class == class)
30 {
31 /* found it, ignore */
32 return;
33 }
34 psp = psp->next;
35 }
36
37 psp = ps_normalqueue;
38 while (psp != NULL)
39 {
40 if (psp->node == node && psp->type == scantype &&
41 psp->port == port && psp->class == class)
42 {
43 /* found it, ignore */
44 return;
45 }
46 psp = psp->next;
47 }
48
49 /* If there are scans spare, just start it immediately..
50 * provided we're not supposed to wait */
51 if (activescans < maxscans && when<=time(NULL) && (ps_start_ts+120 <= time(NULL))) {
52 startscan(node, scantype, port, class);
53 return;
54 }
55
56 /* We have to queue it */
57 if (!(psp=(struct pendingscan *)malloc(sizeof(pendingscan))))
58 Error("proxyscan",ERR_STOP,"Unable to allocate memory");
59
60 countpendingscan++;
61
62 psp->node=node;
63 psp->type=scantype;
64 psp->port=port;
65 psp->class=class;
66 psp->when=when;
67 psp->next=NULL;
68
69 if (!when) {
70 /* normal queue */
71 normalqueuedscans++;
72 if (ps_normalqueueend) {
73 ps_normalqueueend->next=psp;
74 ps_normalqueueend=psp;
75 } else {
76 ps_normalqueueend=ps_normalqueue=psp;
77 }
78 } else {
79 prioqueuedscans++;
80 if (!ps_prioqueue || ps_prioqueue->when > when) {
81 psp->next=ps_prioqueue;
82 ps_prioqueue=psp;
83 } else {
84 for (psp2=ps_prioqueue; ;psp2=psp2->next) {
85 if (!psp2->next || psp2->next->when > when) {
86 psp->next = psp2->next;
87 psp2->next = psp;
88 break;
89 }
90 }
91 }
92 }
93 }
94
95 void startqueuedscans() {
96 pendingscan *psp=NULL;
97
98 if (ps_start_ts+120 > time(NULL))
99 return;
100
101 while (activescans < maxscans) {
102 if (ps_prioqueue && (ps_prioqueue->when <= time(NULL))) {
103 psp=ps_prioqueue;
104 ps_prioqueue=psp->next;
105 prioqueuedscans--;
106 } else if (ps_normalqueue) {
107 psp=ps_normalqueue;
108 ps_normalqueue=psp->next;
109 if (!ps_normalqueue)
110 ps_normalqueueend=NULL;
111 normalqueuedscans--;
112 }
113
114 if (psp) {
115 startscan(psp->node, psp->type, psp->port, psp->class);
116 free(psp);
117 countpendingscan--;
118 psp=NULL;
119 } else {
120 break;
121 }
122 }
123 }
124
125