File: mod_delay.c

package info (click to toggle)
fragroute 1.2-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 692 kB
  • ctags: 520
  • sloc: ansic: 4,331; sh: 2,842; makefile: 74
file content (97 lines) | stat: -rw-r--r-- 1,751 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * mod_delay.c
 *
 * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
 *
 * $Id: mod_delay.c,v 1.4 2002/04/07 22:55:20 dugsong Exp $
 */

#include "config.h"

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

#include "pkt.h"
#include "mod.h"

#define DELAY_FIRST	1
#define DELAY_LAST	2
#define DELAY_RANDOM	3

struct delay_data {
	rand_t		*rnd;
	int		 which;
	struct timeval	 tv;
};

void *
delay_close(void *d)
{
	struct delay_data *data = (struct delay_data *)d;
	
	if (data != NULL) {
		rand_close(data->rnd);
		free(data);
	}
	return (NULL);
}

void *
delay_open(int argc, char *argv[])
{
	struct delay_data *data;
	uint64_t usec;
	
	if (argc != 3)
		return (NULL);
	
	if ((data = malloc(sizeof(*data))) == NULL)
		return (NULL);

	data->rnd = rand_open();
	
	if (strcasecmp(argv[1], "first") == 0)
		data->which = DELAY_FIRST;
	else if (strcasecmp(argv[1], "last") == 0) 
		data->which = DELAY_LAST;
	else if (strcasecmp(argv[1], "random") == 0) 
		data->which = DELAY_RANDOM;
	else 
		return (delay_close(data));
	
	if ((usec = atoi(argv[2])) <= 0)
		return (delay_close(data));
	
	usec *= 1000;
	data->tv.tv_sec = usec / 1000000;
	data->tv.tv_usec = usec % 1000000;
	
	return (data);
}

int
delay_apply(void *d, struct pktq *pktq)
{
	struct delay_data *data = (struct delay_data *)d;
	struct pkt *pkt;
	
	if (data->which == DELAY_FIRST)
		pkt = TAILQ_FIRST(pktq);
	else if (data->which == DELAY_LAST)
		pkt = TAILQ_LAST(pktq, pktq);
	else 
		pkt = pktq_random(data->rnd, pktq);
	
	memcpy(&pkt->pkt_ts, &data->tv, sizeof(pkt->pkt_ts));
	
	return (0);
}

struct mod mod_delay = {
	"delay",			/* name */
	"delay first|last|random <ms>",	/* usage */
	delay_open,			/* open */
	delay_apply,			/* apply */
	delay_close			/* close */
};