File: mod_order.c

package info (click to toggle)
fragroute 1.2-7.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 692 kB
  • ctags: 520
  • sloc: ansic: 4,331; sh: 2,968; makefile: 74
file content (79 lines) | stat: -rw-r--r-- 1,291 bytes parent folder | download | duplicates (2)
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
/*
 * mod_order.c
 *
 * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
 *
 * $Id: mod_order.c,v 1.9 2002/04/07 22:55:20 dugsong Exp $
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mod.h"

#define ORDER_RANDOM	1
#define ORDER_REVERSE	2

struct order_data {
	rand_t	*rnd;
	int	 type;
};

void *
order_close(void *d)
{
	struct order_data *data = (struct order_data *)d;

	if (data != NULL) {
		rand_close(data->rnd);
		free(data);
	}
	return (NULL);
}

void *
order_open(int argc, char *argv[])
{
	struct order_data *data;
	
	if (argc < 2)
		return (NULL);
	
	if ((data = malloc(sizeof(*data))) == NULL)
		return (NULL);

	data->rnd = rand_open();
	
	if (strcasecmp(argv[1], "random") == 0) {
		data->type = ORDER_RANDOM;
	} else if (strcasecmp(argv[1], "reverse") == 0) {
		data->type = ORDER_REVERSE;
	} else
		return (order_close(data));

	return (data);
}

int
order_apply(void *d, struct pktq *pktq)
{
	struct order_data *data = (struct order_data *)d;
	
	if (data->type == ORDER_RANDOM)
		pktq_shuffle(data->rnd, pktq);
	else
		pktq_reverse(pktq);
	
	return (0);
}

struct mod mod_order = {
	"order",			/* name */
	"order random|reverse",		/* usage */
	order_open,			/* open */
	order_apply,			/* apply */
	order_close			/* close */
};