forked from appneta/tcpreplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlt2name.pl
executable file
·186 lines (154 loc) · 5.51 KB
/
dlt2name.pl
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
#!/usr/bin/perl -w
# Parses the bpf.h header file to generate the dlt_names.h header
# which maps the DLT types to the DLT string name
# run from the tcpreplay source base directory as:
# cat /usr/include/pcap-bpf.h | ./scripts/dlt2name.pl
use strict;
my $out_c = 'src/common/dlt_names.c';
my $out_h = 'src/common/dlt_names.h';
# open outfile
open(OUT_C, ">$out_c") or die("Unable to open $out_c for writing: $!");
open(OUT_H, ">$out_h") or die("Unable to open $out_h for writing: $!");
# read STDIN
# some DLT types aren't in a format we can parse easily or just doesn't
# exist in my /usr/include/net/bpf.h file so we list them here
my %known = (107 => 'BSD/OS Frame Relay',
108 => 'OpenBSD Loopback',
113 => 'Linux Cooked Sockets',
114 => 'Apple LocalTalk',
115 => 'Acorn Econet',
116 => 'OpenBSD IPFilter',
117 => 'OpenBSD PF Log/SuSE 6.3 LANE 802.3',
118 => 'Cisco IOS',
119 => '802.11 Prism Header',
120 => '802.11 Aironet Header',
121 => 'Siemens HiPath HDLC',
122 => 'IP over Fibre Channel'
);
my @names;
# put our known DLT types in names since the format of bpf.h is
# inconsistent
foreach my $dlt (keys %known) {
$names[$dlt]{name} = $known{$dlt};
}
while (my $line = <STDIN>) {
if ($line =~ /^\#define\s+(DLT_[a-zA-Z0-9_]+)\s+(\d+)/) {
my $key = $1;
my $dlt = $2;
my $name = $names[$dlt]{name} ? $names[$dlt]{name} : "";
if ($line =~ /\/\*\s+(.*)\s+\*\//) {
$name = $1;
}
$names[$dlt]{key} = $key;
$names[$dlt]{name} = $name;
}
}
# print the license info
while (my $line = <DATA>) {
print OUT_C $line;
print OUT_H $line;
}
# prep the header
print OUT_C <<HEADER;
#include <stdlib.h>
/* DLT to descriptions */
char *dlt2desc[] = {
HEADER
for (my $i = 0; $i < $#names; $i ++) {
if (! defined $names[$i]) {
print OUT_C "\t\t\"Unknown\",\n";
} else {
print OUT_C "\t\t\"$names[$i]->{name}\",\n";
}
}
print OUT_C <<FOOTER;
\t\tNULL
};
FOOTER
print OUT_H <<HEADER;
/* include all the DLT types form pcap-bpf.h */
extern const char *dlt2desc[];
extern const char *dlt2name[];
#define DLT2DESC_LEN $#names
#define DLT2NAME_LEN $#names
HEADER
for (my $i = 0; $i < 255; $i++) {
next if ! defined $names[$i];
print OUT_H "#ifndef $names[$i]{key}\n#define $names[$i]{key} $i\n#endif\n\n";
}
print OUT_C <<NAMES;
/* DLT to names */
char *dlt2name[] = {
NAMES
for (my $i = 0; $i < 255; $i++) {
if (! defined $names[$i]) {
print OUT_C "\t\t\"Unknown\",\n";
} else {
print OUT_C "\t\t\"$names[$i]{key}\",\n";
}
}
print OUT_C <<FOOTER;
\t\tNULL
};
FOOTER
close OUT_C;
close OUT_H;
exit 0;
__DATA__
/*
* Copyright (c) 2006 Aaron Turner
* All rights reserved.
*
* This file is generated by scripts/dlt2name.pl which converts your pcap-bpf.h
* header file which comes with libpcap into a header file
* which translates DLT values to their string names as well as a list of all
* of the available DLT types.
*
* Hence DO NOT EDIT THIS FILE!
* If your DLT type is not listed here, edit the %known hash in
* scripts/dlt2name.pl
*
* This file contains data which was taken from libpcap's pcap-bpf.h.
* The copyright/license is included below:
*/
/*-
* Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
* The Regents of the University of California. All rights reserved.
*
* This code is derived from the Stanford/CMU enet packet filter,
* (net/enet.c) distributed as part of 4.3BSD, and code contributed
* to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
* Berkeley Laboratory.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)bpf.h 7.1 (Berkeley) 5/7/91
*
* @(#) $Header: /tcpdump/master/libpcap/pcap-bpf.h,v 1.34.2.6 2005/08/13 22:29:47 hannes Exp $ (LBL)
*/