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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*
* Fragment storage.
*
* Fragments are stored in keyed buckets.
* A packet bucket is an ordered list of unique, indexed packets,
* intended for eventual reassembly into a complete packet.
* It is up to the caller to free a bucket when it is
* no longer needed. Very old buckets are deleted automatically.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#if STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#endif
#include "compat.h"
#define LIST_REMOVE(o, pfx) do { \
if ((*(o)->pfx##_prevp = (o)->pfx##_next) != NULL) \
(o)->pfx##_next->pfx##_prevp = (o)->pfx##_prevp; \
/* (o)->pfx##_prevp = NULL; */ \
/* (o)->pfx##_next = NULL; */ \
} while (0)
#define LIST_INSERT(o, headp, pfx) do { \
(o)->pfx##_prevp = (headp); \
if (((o)->pfx##_next = *(headp)) != NULL) \
(o)->pfx##_next->pfx##_prevp = &(o)->pfx##_next; \
*(headp) = (o); \
} while (0)
#define LIST_NEXT(o, pfx) ((o)->pfx##_next)
#define LIST_DECL(t, pfx) t *pfx##_next, **pfx##_prevp
struct frag {
u_int32_t index, next_index;
LIST_DECL(struct frag, ordered);
size_t datalen;
};
#define FRAG_DATA(f) ((char *)((f)+1))
struct bucket {
LIST_DECL(struct bucket, hash);
struct bucket *lru_fwd, *lru_back;
struct frag *first_frag;
};
#define BUCKET_KEY(b) ((char *)((b)+1))
#define HASHLEN 257
struct fragtab {
int keylen;
int availbuckets;
struct bucket *lru_first, *lru_last;
struct bucket *buckets[HASHLEN];
};
/* Prototypes */
static unsigned int hash(const void *key, int keylen);
static void bucket_lru_remove(struct fragtab *fragtab, struct bucket *bucket);
static void bucket_lru_insert(struct fragtab *fragtab, struct bucket *bucket);
static void frag_delete(struct frag *frag);
static struct frag **frag_find(struct frag **fragp, u_int32_t index);
static struct bucket **bucket_find(struct fragtab *fragtab, const void *key);
static void bucket_create(struct fragtab *fragtab, const void *key,
struct bucket **dest);
static void frag_create(struct fragtab *fragtab, const void *data,
size_t datalen, u_int32_t index, u_int32_t next_index,
struct frag **dest);
static void bucket_delete(struct fragtab *fragtab, struct bucket *bucket);
/* Compute a hash index from a key string */
static unsigned int
hash(key, keylen)
const void *key;
int keylen;
{
unsigned int h = 0;
unsigned char *p;
for (p = (unsigned char *)key; keylen; keylen--, p++)
h = (h << 1) ^ *p;
return h % HASHLEN;
}
/* Remove a bucket from the lru list */
static void
bucket_lru_remove(fragtab, bucket)
struct fragtab *fragtab;
struct bucket *bucket;
{
if (bucket->lru_back)
bucket->lru_back->lru_fwd = bucket->lru_fwd;
else
fragtab->lru_first = bucket->lru_fwd;
if (bucket->lru_fwd)
bucket->lru_fwd->lru_back = bucket->lru_back;
else
fragtab->lru_last = bucket->lru_back;
}
/* Place a (removed) bucket at the front of the lru list */
static void
bucket_lru_insert(fragtab, bucket)
struct fragtab *fragtab;
struct bucket *bucket;
{
if (fragtab->lru_first) {
bucket->lru_fwd = fragtab->lru_first;
fragtab->lru_first->lru_back = bucket;
fragtab->lru_first = bucket;
bucket->lru_back = NULL;
} else {
fragtab->lru_first = bucket;
fragtab->lru_last = bucket;
bucket->lru_fwd = NULL;
bucket->lru_back = NULL;
}
}
/* Unlink and deallocate a fragment */
static void
frag_delete(frag)
struct frag *frag;
{
LIST_REMOVE(frag, ordered);
free(frag);
}
/*
* Returns a pointer to where the fragment of the given index
* should be.
*/
static struct frag **
frag_find(fragp, index)
struct frag **fragp;
u_int32_t index;
{
while (*fragp && (*fragp)->index < index)
fragp = &LIST_NEXT(*fragp, ordered);
return fragp;
}
/*
* Searches a chain in the hashtable for the bucket with the given key.
* Returns the end of the chain, if not found.
*/
static struct bucket **
bucket_find(fragtab, key)
struct fragtab *fragtab;
const void *key;
{
struct bucket **bucketp;
unsigned int keyhash;
keyhash = hash(key, fragtab->keylen);
bucketp = &fragtab->buckets[keyhash];
while (*bucketp && memcmp(BUCKET_KEY(*bucketp), key,
fragtab->keylen) != 0)
bucketp = &LIST_NEXT(*bucketp, hash);
return bucketp;
}
/* Create and insert a new bucket */
static void
bucket_create(fragtab, key, dest)
struct fragtab *fragtab;
const void *key;
struct bucket **dest;
{
struct bucket *bucket;
size_t size;
size = sizeof (struct bucket) + fragtab->keylen;
bucket = (struct bucket *)malloc(size);
if (!bucket)
errx(1, "malloc");
bucket->first_frag = NULL;
memcpy(BUCKET_KEY(bucket), key, fragtab->keylen);
LIST_INSERT(bucket, dest, hash);
bucket_lru_insert(fragtab, bucket);
fragtab->availbuckets--;
}
/* Create and insert a new fragment. */
static void
frag_create(fragtab, data, datalen, index, next_index, dest)
struct fragtab *fragtab;
const void *data;
size_t datalen;
u_int32_t index, next_index;
struct frag **dest;
{
size_t size;
struct frag* frag;
size = sizeof (struct fragtab) + datalen;
frag = (struct frag *)malloc(size);
if (!frag)
errx(1, "malloc");
frag->index = index;
frag->next_index = next_index;
frag->datalen = datalen;
memcpy(FRAG_DATA(frag), data, datalen);
LIST_INSERT(frag, dest, ordered);
}
/* Delete a bucket and all the fragments it contains */
static void
bucket_delete(fragtab, bucket)
struct fragtab *fragtab;
struct bucket *bucket;
{
while (bucket->first_frag)
frag_delete(bucket->first_frag);
bucket_lru_remove(fragtab, bucket);
LIST_REMOVE(bucket, hash);
free(bucket);
fragtab->availbuckets++;
}
/*
* Allocate an empty fragment table
*/
struct fragtab *
fragtab_new(keylen, maxbuckets)
int keylen, maxbuckets;
{
struct fragtab *fragtab;
int i;
fragtab = (struct fragtab *)malloc(sizeof (struct fragtab));
if (!fragtab)
errx(1, "malloc");
fragtab->keylen = keylen;
fragtab->availbuckets = maxbuckets;
fragtab->lru_first = NULL;
fragtab->lru_last = NULL;
for (i = 0; i < HASHLEN; i++)
fragtab->buckets[i] = NULL;
return fragtab;
}
/*
* Put a new fragment into the right bucket. A bucket is created
* if there isnt one for the fragment already. The bucket is moved
* to the young end of the lru list.
*/
void
fragtab_put(fragtab, key, data, datalen, index, next_index)
struct fragtab *fragtab;
const void *key;
const void *data;
size_t datalen;
u_int32_t index, next_index;
{
struct bucket **bucketp, *bucket;
struct frag **fragp;
bucketp = bucket_find(fragtab, key);
if (!*bucketp) {
if (fragtab->availbuckets < 0)
bucket_delete(fragtab, fragtab->lru_last);
bucket_create(fragtab, key, bucketp);
} else if (fragtab->lru_first != *bucketp) {
/* Move to young end of list */
bucket_lru_remove(fragtab, *bucketp);
bucket_lru_insert(fragtab, *bucketp);
}
bucket = *bucketp;
fragp = frag_find(&bucket->first_frag, index);
if (*fragp && (*fragp)->index == index)
frag_delete(*fragp);
frag_create(fragtab, data, datalen, index, next_index, fragp);
}
/*
* Return the data associated with a fragment at the given index,
* or return NULL if it doesn't exist yet.
*/
const void *
fragtab_get(fragtab, key, index, datalenp)
struct fragtab *fragtab;
const void *key;
u_int32_t index;
size_t *datalenp;
{
struct bucket *bucket;
struct frag *frag;
bucket = *bucket_find(fragtab, key);
if (bucket) {
frag = *frag_find(&bucket->first_frag, index);
if (frag && frag->index == index) {
*datalenp = frag->datalen;
return FRAG_DATA(frag);
}
}
return NULL;
}
/*
* Delete all fragments with the given key
*/
void
fragtab_del(fragtab, key)
struct fragtab *fragtab;
const void *key;
{
struct bucket *bucket;
bucket = *bucket_find(fragtab, key);
if (bucket)
bucket_delete(fragtab, bucket);
}
/*
* Return true if all the fragments of the bucket exist;
* ie each fragment's next_index is equal to the next fragment's index
*/
int
fragtab_check(fragtab, key, first_index, last_index)
struct fragtab *fragtab;
const void *key;
u_int32_t first_index, last_index;
{
struct bucket *bucket;
struct frag *frag;
bucket = *bucket_find(fragtab, key);
if (bucket) {
frag = bucket->first_frag;
if (frag && frag->index == first_index)
for (; frag; frag = LIST_NEXT(frag, ordered)) {
if (LIST_NEXT(frag, ordered) == NULL) {
if (frag->next_index == last_index)
/* at last fragment and it is last expected index */
return 1;
} else if (frag->next_index !=
LIST_NEXT(frag, ordered)->index) {
/* missing fragments */
break;
}
}
}
return 0;
}
/*
* Destroy all storage associated with the fragment table
*/
void
fragtab_free(fragtab)
struct fragtab *fragtab;
{
int i;
for (i = 0; i < HASHLEN; i++)
while (fragtab->buckets[i])
bucket_delete(fragtab, fragtab->buckets[i]);
free(fragtab);
}
|