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