forked from ajkeeton/fragroute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_ip_ttl.c
73 lines (59 loc) · 1.24 KB
/
mod_ip_ttl.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
/*
* mod_ip_ttl.c
*
* Copyright (c) 2001 Dug Song <[email protected]>
*
* $Id: mod_ip_ttl.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 "argv.h"
#include "mod.h"
#include "pkt.h"
struct ip_ttl_data {
int ttl;
};
void *
ip_ttl_close(void *d)
{
if (d != NULL)
free(d);
return (NULL);
}
void *
ip_ttl_open(int argc, char *argv[])
{
struct ip_ttl_data *data;
if (argc != 2)
return (NULL);
if ((data = calloc(1, sizeof(*data))) == NULL)
return (NULL);
if ((data->ttl = atoi(argv[1])) <= 0 || data->ttl > 255)
return (ip_ttl_close(data));
return (data);
}
int
ip_ttl_apply(void *d, struct pktq *pktq)
{
struct ip_ttl_data *data = (struct ip_ttl_data *)d;
struct pkt *pkt;
int ttldec;
TAILQ_FOREACH(pkt, pktq, pkt_next) {
ttldec = pkt->pkt_ip->ip_ttl - data->ttl;
pkt->pkt_ip->ip_ttl = data->ttl;
if (pkt->pkt_ip->ip_sum >= htons(0xffff - (ttldec << 8)))
pkt->pkt_ip->ip_sum += htons(ttldec << 8) + 1;
else
pkt->pkt_ip->ip_sum += htons(ttldec << 8);
}
return (0);
}
struct mod mod_ip_ttl = {
"ip_ttl", /* name */
"ip_ttl <ttl>", /* usage */
ip_ttl_open, /* open */
ip_ttl_apply, /* apply */
ip_ttl_close /* close */
};