-
Notifications
You must be signed in to change notification settings - Fork 2
/
pqueue.c
243 lines (177 loc) · 5.89 KB
/
pqueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "util.h" // for log()
#include "pqueue.h"
#ifdef DEBUG_PQUEUE
#define DEBUG_ON 1
#else
#define DEBUG_ON 0
#endif
#define DEBUG_CMD(_a) if (DEBUG_ON) { _a }
#define MIN_CAPACITY 128 /* min allocated buffer for a packet */
static int pqueue_alloc (u_int32_t seq, unsigned char *packet, int packlen, pqueue_t **new);
int packet_timeout_usecs = DEFAULT_PACKET_TIMEOUT * 1000000;
static pqueue_t *pq_head = NULL, *pq_tail = NULL;
/* contains a list of free queue elements.*/
static pqueue_t *pq_freelist_head = NULL;
static void pq_freelist_add(pqueue_t *point) {
/* add point to the freelist */
point->next = pq_freelist_head;
point->prev = NULL;
if (point->next)
point->next->prev = point;
pq_freelist_head = point;
}
static int pqueue_alloc(u_int32_t seq, unsigned char *packet, int packlen, pqueue_t **new) {
pqueue_t *newent;
DEBUG_CMD(log("seq=%d, packlen=%d", seq, packlen););
/* search the freelist for one that has sufficient space */
if (pq_freelist_head) {
for (newent = pq_freelist_head; newent; newent = newent->next) {
if (newent->capacity >= packlen) {
/* unlink from freelist */
if (pq_freelist_head == newent)
pq_freelist_head = newent->next;
if (newent->prev)
newent->prev->next = newent->next;
if (newent->next)
newent->next->prev = newent->prev;
if (pq_freelist_head)
pq_freelist_head->prev = NULL;
break;
} /* end if capacity >= packlen */
} /* end for */
/* nothing found? Take first and reallocate it */
if (NULL == newent) {
newent = pq_freelist_head;
pq_freelist_head = pq_freelist_head->next;
if (pq_freelist_head)
pq_freelist_head->prev = NULL;
DEBUG_CMD(log("realloc capacity %d to %d",newent->capacity, packlen););
newent->packet = (unsigned char *)realloc(newent->packet, packlen);
if (!newent->packet) {
warn("error reallocating packet: %s", strerror(errno));
return -1;
}
newent->capacity = packlen;
}
DEBUG_CMD(log("Recycle entry from freelist. Capacity: %d", newent->capacity););
} else {
/* allocate a new one */
newent = (pqueue_t *)malloc( sizeof(pqueue_t) );
if (!newent) {
warn("error allocating newent: %s", strerror(errno));
return -1;
}
newent->capacity = 0;
DEBUG_CMD(log("Alloc new queue entry"););
}
if ( ! newent->capacity ) {
/* a new queue entry was allocated. Allocate the packet buffer */
int size = packlen < MIN_CAPACITY ? MIN_CAPACITY : packlen;
/* Allocate at least MIN_CAPACITY */
DEBUG_CMD(log("allocating for packet size %d", size););
newent->packet = (unsigned char *)malloc(size);
if (!newent->packet) {
warn("error allocating packet: %s", strerror(errno));
return -1;
}
newent->capacity = size;
} /* endif ! capacity */
assert( newent->capacity >= packlen );
/* store the contents into the buffer */
memcpy(newent->packet, packet, packlen);
newent->next = newent->prev = NULL;
newent->seq = seq;
newent->packlen = packlen;
gettimeofday(&newent->expires, NULL);
newent->expires.tv_usec += packet_timeout_usecs;
newent->expires.tv_sec += (newent->expires.tv_usec / 1000000);
newent->expires.tv_usec %= 1000000;
*new = newent;
return 0;
}
int pqueue_add (u_int32_t seq, unsigned char *packet, int packlen) {
pqueue_t *newent, *point;
/* get a new entry */
if ( 0 != pqueue_alloc(seq, packet, packlen, &newent) ) {
return -1;
}
for (point = pq_head; point != NULL; point = point->next) {
if (point->seq == seq) {
// queue already contains this packet
warn("discarding duplicate packet %d", seq);
pq_freelist_add(point);
return -1;
}
if (point->seq > seq) {
// gone too far: point->seq > seq and point->prev->seq < seq
if (point->prev) {
// insert between point->prev and point
DEBUG_CMD(log("adding %d between %d and %d",
seq, point->prev->seq, point->seq););
point->prev->next = newent;
} else {
// insert at head of queue, before point
DEBUG_CMD(log("adding %d before %d", seq, point->seq););
pq_head = newent;
}
newent->prev = point->prev; // will be NULL, at head of queue
newent->next = point;
point->prev = newent;
return 0;
}
}
/* We didn't find anywhere to insert the packet,
* so there are no packets in the queue with higher sequences than this one,
* so all the packets in the queue have lower sequences,
* so this packet belongs at the end of the queue (which might be empty)
*/
if (pq_head == NULL) {
DEBUG_CMD(log("adding %d to empty queue", seq););
pq_head = newent;
} else {
DEBUG_CMD(log("adding %d as tail, after %d", seq, pq_tail->seq););
pq_tail->next = newent;
}
newent->prev = pq_tail;
pq_tail = newent;
return 0;
}
int pqueue_del (pqueue_t *point) {
DEBUG_CMD(log("Move seq %d to freelist", point->seq););
/* unlink from pq */
if (pq_head == point) pq_head = point->next;
if (pq_tail == point) pq_tail = point->prev;
if (point->prev) point->prev->next = point->next;
if (point->next) point->next->prev = point->prev;
/* add point to the freelist */
pq_freelist_add(point);
DEBUG_CMD(
int pq_count = 0;
int pq_freelist_count = 0;
pqueue_t *dpoint;
for (dpoint = pq_head; dpoint ; dpoint = dpoint->next) {
++pq_count;
}
for (dpoint = pq_freelist_head; dpoint ; dpoint = dpoint->next) {
++pq_freelist_count;
}
log("queue length is %d, freelist length is %d", pq_count,
pq_freelist_count);
);
return 0;
}
pqueue_t *pqueue_head (void) {
return pq_head;
}
int pqueue_expiry_time (pqueue_t *entry) {
struct timeval tv;
int expiry_time;
gettimeofday(&tv, NULL);
expiry_time = (entry->expires.tv_sec - tv.tv_sec) * 1000000;
expiry_time += (entry->expires.tv_usec - tv.tv_usec);
return expiry_time;
}