forked from CAIDA/mper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scamper_icmpext.c
129 lines (110 loc) · 2.42 KB
/
scamper_icmpext.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
/*
* scamper_icmpext.c
*
* $Id: scamper_icmpext.c,v 1.4 2009/03/13 20:51:26 mjl Exp $
*
* Copyright (C) 2008 The University of Waikato
* Author: Matthew Luckie
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "internal.h"
#include "scamper_icmpext.h"
scamper_icmpext_t *scamper_icmpext_alloc(uint8_t cn, uint8_t ct, uint16_t dl,
const void *data)
{
scamper_icmpext_t *ie;
if((ie = malloc(sizeof(scamper_icmpext_t))) == NULL)
{
return NULL;
}
if(dl != 0)
{
if((ie->ie_data = malloc(dl)) != NULL)
{
memcpy(ie->ie_data, data, dl);
}
else
{
free(ie);
return NULL;
}
}
else
{
ie->ie_data = NULL;
}
ie->ie_next = NULL;
ie->ie_cn = cn;
ie->ie_ct = ct;
ie->ie_dl = dl;
return ie;
}
void scamper_icmpext_free(scamper_icmpext_t *ie)
{
scamper_icmpext_t *next;
while(ie != NULL)
{
next = ie->ie_next;
if(ie->ie_data != NULL)
free(ie->ie_data);
free(ie);
ie = next;
}
return;
}
int scamper_icmpext_parse(scamper_icmpext_t **exts, void *data, uint16_t len)
{
scamper_icmpext_t *ie, *next;
uint8_t *u8 = data;
uint16_t dl;
uint8_t cn, ct;
int off;
*exts = NULL;
next = *exts;
/* start at offset 4 so the extension header is skipped */
for(off = 4; off + 4 < len; off += dl)
{
/* extract the length field */
memcpy(&dl, u8+off, 2);
dl = ntohs(dl);
/* make sure there is enough in the packet left */
if(off + dl < len)
break;
cn = u8[off+2];
ct = u8[off+3];
if(dl < 8)
{
continue;
}
if((ie = scamper_icmpext_alloc(cn, ct, dl-4, u8+off+4)) == NULL)
{
return -1;
}
if(next == NULL)
{
*exts = ie;
}
else
{
next->ie_next = ie;
}
next = ie;
}
return 0;
}