3 # Make-manuf - Creates a file containing ethernet OUIs and their
4 # company IDs. It merges the databases at IEEE and
5 # http://www.cavebear.com/archive/cavebear/Ethernet/Ethernet.txt
6 # with entries in our template file.
8 # The script reads the comments at the top of "manuf.tmpl" and writes
9 # them to "manuf". It then joins the manufacturer listing in "manuf.tmpl"
10 # with the listing in "oui.txt", "iab.txt", etc, with the entries in
11 # "manuf.tmpl" taking precedence.
13 # LWP is part of the standard Perl module libwww
15 # As of April 2012 the IEEE content is mostly UTF-8 encoded although some
16 # of the entries feature sequences listed at
17 # http://www.i18nqa.com/debug/utf8-debug.html
20 use open ':encoding(utf8)';
22 eval "require LWP::UserAgent;";
24 die "LWP isn't installed. It is part of the standard Perl\n" .
25 " module libwww. Bailing.\n";
28 $agent = LWP::UserAgent->new;
30 $agent->agent("Wireshark make-manuf");
32 $template = "manuf.tmpl";
33 $wkatmpl = "wka.tmpl";
36 $oui_url = "http://standards.ieee.org/develop/regauth/oui/oui.txt";
37 $iab_url = "http://standards.ieee.org/develop/regauth/iab/iab.txt";
38 $oui28_url = "http://standards.ieee.org/develop/regauth/oui28/oui28.txt";
39 $oui36_url = "http://standards.ieee.org/develop/regauth/oui36/oui36.txt";
40 $cb_url = "http://www.cavebear.com/archive/cavebear/Ethernet/Ethernet.txt";
42 $hp = "[0-9a-fA-F]{2}";
43 $oui_re = "$hp:$hp:$hp";
45 $ieee_re = "$hp-$hp-$hp";
68 my $origmanuf = shift;
69 my $manuf = " " . $origmanuf . " ";
70 # Remove any punctuation
71 $manuf =~ tr/',.()/ /;
72 # & isn't needed when Standalone
74 # Remove any "the", "inc", "plc" ...
75 $manuf =~ s/\s(the|inc|incorporated|plc||systems|corp|corporation|s\/a|a\/s|ab|ag|kg|gmbh|co|company|limited|ltd|holding|spa)(?= )//gi;
76 # Convert to consistent case
77 $manuf =~ s/(\w+)/\u\L$1/g;
80 # Truncate all names to a reasonable length, say, 8 characters.
81 # If the string contains UTF-8, this may be substantially more than 8 bytes.
82 $manuf = substr($manuf, 0, 8);
84 if ($manuf =~ /\Q$origmanuf\E/i) {
87 return sprintf("%-22s # %s", $manuf, $origmanuf);
94 print "Fetching $url.\n";
95 $request = HTTP::Request->new(GET => $url);
96 $result = $agent->request($request);
98 if (!$result->is_success) {
99 die ("Error fetching $url: " . $result->status_line . "\n");
101 return decode("utf-8", $result->content);
104 # Write out the header and populate the OUI list with our entries.
106 open (TMPL, "< $template") ||
107 die "Couldn't open template file for reading ($template)\n";
109 while ($line = <TMPL>) {
111 if ($line !~ /^$oui_re\s+\S/ && $inheader) {
112 $header .= "$line\n";
113 } elsif (($oui, $manuf) = ($line =~ /^($oui_re)\s+(\S.*)$/)) {
115 # Ensure OUI is all upper-case
117 # $oui_list{$oui} = &shorten($manuf);
118 $oui_list{$oui} = $manuf;
123 # Add IEEE entries for IABs
125 $ieee_list = fetch($iab_url);
127 foreach $line (split(/\n/, $ieee_list)) {
128 # determine the OUI used for IAB (currently only 00-50-C2)
129 if (($iab_tmp, $manuf) = ($line =~ /^\s*($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
130 $iab_base = $iab_tmp;
132 # determine next two bytes
133 if (($iab4, $iab5, $manuf) = ($line =~ /^\s*($hp)($hp)$hp-$hp$hp$hp\s+\(base\s16\)\s+(\S.*)$/)) {
134 $iab = "$iab_base:$iab4:$iab5:00/36";
135 $iab =~ tr /-/:/; # The IEEE bytes are separated by dashes.
136 # Ensure IAB is all upper-case
138 if (exists $oui_list{$iab}) {
139 printf "$iab - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$iab}\"\n";
142 $oui_list{$iab} = &shorten($manuf);
149 if ($iab_total < $min_entries) { die "Too few IAB entries ($iab_total)\n"; }
151 # Add IEEE entries for OUI-28
153 $ieee_list = fetch($oui28_url);
155 foreach $line (split(/\n/, $ieee_list)) {
156 # determine the OUI used for OUI-28
157 if (($oui28_tmp, $manuf) = ($line =~ /^\s*($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
158 $oui28_base = $oui28_tmp;
160 # determine next two bytes
161 if (($oui28_4, $oui28_5, $manuf) = ($line =~ /^\s*($hp)($hp)$hp-$hp$hp$hp\s+\(base\s16\)\s+(\S.*)$/)) {
162 $oui28 = "$oui28_base:$oui28_4:$oui28_5:00/28";
163 $oui28 =~ tr /-/:/; # The IEEE bytes are separated by dashes.
164 # Ensure OUI-28 is all upper-case
165 $oui28 =~ tr/a-f/A-F/;
166 if (exists $oui_list{$oui28}) {
167 printf "$oui28 - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui28}\"\n";
170 $oui_list{$oui28} = &shorten($manuf);
177 if ($oui28_total < $min_entries) { die "Too few OUI-28 entries ($oui28_total)\n"; }
179 # Add IEEE entries for OUI-36
181 $ieee_list = fetch($oui36_url);
183 foreach $line (split(/\n/, $ieee_list)) {
184 # determine the OUI used for OUI-36 (currently only 00-1B-C5)
185 if (($oui36_tmp, $manuf) = ($line =~ /^\s*($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
186 $oui36_base = $oui36_tmp;
188 # determine next two bytes
189 if (($oui36_4, $oui36_5, $manuf) = ($line =~ /^\s*($hp)($hp)$hp-$hp$hp$hp\s+\(base\s16\)\s+(\S.*)$/)) {
190 $oui36 = "$oui36_base:$oui36_4:$oui36_5:00/36";
191 $oui36 =~ tr /-/:/; # The IEEE bytes are separated by dashes.
192 # Ensure OUI-36 is all upper-case
193 $oui36 =~ tr/a-f/A-F/;
194 if (exists $oui_list{$oui36}) {
195 printf "$oui36 - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui36}\"\n";
198 $oui_list{$oui36} = &shorten($manuf);
205 if ($oui36_total < $min_entries) { die "Too few OUI-36 entries ($oui36_total)\n"; }
207 # Add IEEE entries for OUIs not yet known.
209 $ieee_list = fetch($oui_url);
211 foreach $line (split(/\n/, $ieee_list)) {
212 if (($oui, $manuf) = ($line =~ /^\s*($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
213 $oui =~ tr /-/:/; # The IEEE bytes are separated by dashes.
214 # Ensure OUI is all upper-case
216 if (exists $oui_list{$oui}) {
217 printf "$oui - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
220 $oui_list{$oui} = &shorten($manuf);
227 if ($oui_total < $min_entries) { die "Too few OUI entries ($oui_total)\n"; }
229 # Add CaveBear entries for OUIs not yet known.
231 $cb_list = fetch($cb_url);
233 foreach $line (split(/\n/, $cb_list)) {
234 if (($oui, $manuf) = ($line =~ /^($cb_re)\s+(\S.*)$/)) {
235 ($h1, $h2, $h3) = ($oui =~ /($hp)($hp)($hp)/); # The CaveBear bytes have no separators
236 $oui = "$h1:$h2:$h3";
237 # Ensure OUI is all upper-case
239 if (exists $oui_list{$oui}) {
240 # printf "$oui - Skipping CaveBear \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
243 printf "$oui - adding \"$manuf\" from CaveBear\n";
244 $oui_list{$oui} = &shorten($manuf);
251 if ($cb_total < $min_entries) { die "Too few CaveBear entries ($cb_total)\n"; }
255 open (OUT, "> $outfile") ||
256 die "Couldn't open output file for writing ($outfile)\n";
258 print(OUT "# This file was generated by running ./make-manuf.\n");
259 print(OUT "# Don't change it directly, change manuf.tmpl and wka.tmpl instead.\n#\n");
260 print(OUT "$header");
262 foreach $oui (sort(keys %oui_list)) {
263 print(OUT "$oui\t$oui_list{$oui}\n");
266 # Write out a blank line separating the OUIs from the well-known
267 # addresses, and then read the well-known address template file
268 # and write it to the manuf file.
270 open (WKATMPL, "< $wkatmpl") ||
271 die "Couldn't open well-known address template file for reading ($wkatmpl)\n";
273 # XXX - it'd be nice to get this from the Cavebear file, but inferring
274 # the address mask from entries in that file involves some work.
277 while ($line = <WKATMPL>) {
279 print(OUT "$line\n");
282 $total_added = $tmpl_added + $cb_added + $oui_added + $iab_added;
284 Original entries : $tmpl_added
285 IEEE OUI added : $oui_added
286 IEEE IAB added : $iab_added
287 IEEE OUI28 added : $oui28_added
288 IEEE OUI36 added : $oui36_added
289 CaveBear added : $cb_added
292 IEEE OUI skipped : $oui_skipped
293 IEEE IAB skipped : $iab_skipped
294 IEEE OUI28 skipd : $oui28_skipped
295 IEEE OUI36 skipd : $oui36_skipped
296 CaveBear skipped : $cb_skipped