Witness: enum witness_interface_state
[metze/wireshark/wip.git] / tools / make-manuf
1 #!/usr/bin/perl -w
2 #
3 # $Id$
4 #
5 # Make-manuf - Creates a file containing ethernet OUIs and their
6 # company IDs.  It merges the databases at IEEE and
7 # http://www.cavebear.com/archive/cavebear/Ethernet/Ethernet.txt
8 # with entries in our template file.
9 #
10 # The script reads the comments at the top of "manuf.tmpl" and writes
11 # them to "manuf".  It then joins the manufacturer listing in "manuf.tmpl"
12 # with the listing in "oui.txt", "iab.txt", etc, with the entries in 
13 # "manuf.tmpl" taking precedence.
14
15 # LWP is part of the standard Perl module libwww
16
17 # As of April 2012 the IEEE content is mostly UTF-8 encoded although some
18 # of the entries feature sequences listed at
19 # http://www.i18nqa.com/debug/utf8-debug.html
20
21 use Encode;
22 use open ':encoding(utf8)';
23
24 eval "require LWP::UserAgent;";
25 if( $@ ) {
26   die "LWP isn't installed. It is part of the standard Perl\n" .
27         " module libwww.  Bailing.\n";
28 }
29
30 $revision = '$Revision$';
31 if ($revision !~ /[0-9]/ ) { $revision = "unknown"; }
32
33 $agent    = LWP::UserAgent->new;
34 $agent->env_proxy;
35 $agent->agent("Wireshark make-manuf/$revision");
36
37 $template = "manuf.tmpl";
38 $wkatmpl  = "wka.tmpl";
39 $outfile  = "manuf";
40 $inheader = 1;
41 # According to http://standards.ieee.org/regauthmaint.html
42 # the IEEE is adding OUI-28 in January 2014.
43 $oui_url  = "http://standards.ieee.org/develop/regauth/oui/oui.txt";
44 $iab_url  = "http://standards.ieee.org/develop/regauth/iab/iab.txt";
45 $oui36_url = "http://standards.ieee.org/develop/regauth/oui36/oui36.txt";
46 $cb_url   = "http://www.cavebear.com/archive/cavebear/Ethernet/Ethernet.txt";
47 %oui_list = ();
48 $hp       = "[0-9a-fA-F]{2}";
49 $oui_re   = "$hp:$hp:$hp";
50 $cb_re    = "$hp$hp$hp";
51 $ieee_re  = "$hp-$hp-$hp";
52
53 $min_entries = 100;
54
55 $tmpl_added    = 0;
56 $cb_added      = 0;
57 $cb_skipped    = 0;
58 $cb_total      = 0;
59 $oui_added     = 0;
60 $oui_skipped   = 0;
61 $oui_total     = 0;
62 $iab_added     = 0;
63 $iab_skipped   = 0;
64 $iab_total     = 0;
65 $oui36_added   = 0;
66 $oui36_skipped = 0;
67 $oui36_total   = 0;
68
69 sub shorten
70 {
71   my $origmanuf = shift;
72   my $manuf = " " . $origmanuf . " ";
73   # Remove any punctuation
74   $manuf =~ tr/',.()/    /;
75   # & isn't needed when Standalone
76   $manuf =~ s/ \& / /g;
77   # Remove any "the", "inc", "plc" ...
78   $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;
79   # Convert to consistent case
80   $manuf =~ s/(\w+)/\u\L$1/g;
81   # Remove all spaces
82   $manuf =~ s/\s+//g;
83   # Truncate all names to a reasonable length, say, 8 characters.
84   # If the string contains UTF-8, this may be substantially more than 8 bytes.
85   $manuf = substr($manuf, 0, 8);
86
87   if ($manuf =~ /\Q$origmanuf\E/i) {
88     return $manuf;
89   } else {
90     return sprintf("%-22s # %s", $manuf, $origmanuf);
91   }
92 }
93
94 sub fetch
95 {
96   my $url = shift;
97   print "Fetching $url.\n";
98   $request  = HTTP::Request->new(GET => $url);
99   $result   = $agent->request($request);
100
101   if (!$result->is_success) {
102     die ("Error fetching $url: " . $result->status_line . "\n");
103   }
104   return decode("utf-8", $result->content);
105 }
106
107 # Write out the header and populate the OUI list with our entries.
108
109 open (TMPL, "< $template") ||
110   die "Couldn't open template file for reading ($template)\n";
111
112 while ($line = <TMPL>) {
113   chomp($line);
114   if ($line !~ /^$oui_re\s+\S/ && $inheader) {
115     $header .= "$line\n";
116   } elsif (($oui, $manuf) = ($line =~ /^($oui_re)\s+(\S.*)$/)) {
117     $inheader = 0;
118     # Ensure OUI is all upper-case
119     $oui =~ tr/a-f/A-F/;
120     # $oui_list{$oui} = &shorten($manuf);
121     $oui_list{$oui} = $manuf;
122     $tmpl_added++;
123   }
124 }
125
126 # Add IEEE entries for IABs
127
128 $ieee_list = fetch($iab_url);
129
130 foreach $line (split(/\n/, $ieee_list)) {
131   # determine the OUI used for IAB (currently only 00-50-C2)
132   if (($iab_tmp, $manuf) = ($line =~ /^\s*($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
133       $iab_base = $iab_tmp;
134   }
135   # determine next two bytes
136   if (($iab4, $iab5, $manuf) = ($line =~ /^\s*($hp)($hp)$hp-$hp$hp$hp\s+\(base\s16\)\s+(\S.*)$/)) {
137     $iab = "$iab_base:$iab4:$iab5:00/36";
138     $iab =~ tr /-/:/; # The IEEE bytes are separated by dashes.
139     # Ensure IAB is all upper-case
140     $iab =~ tr/a-f/A-F/;
141     if (exists $oui_list{$iab}) {
142       printf "$iab - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$iab}\"\n";
143       $iab_skipped++;
144     } else {
145       $oui_list{$iab} = &shorten($manuf);
146       $iab_added++;
147     }
148   }
149   $iab_total++;
150 }
151
152 if ($iab_total < $min_entries) { die "Too few IAB entries ($iab_total)\n"; }
153
154 # Add IEEE entries for OUI-36
155
156 $ieee_list = fetch($oui36_url);
157
158 foreach $line (split(/\n/, $ieee_list)) {
159   # determine the OUI used for OUI-36 (currently only 00-1B-C5)
160   if (($oui36_tmp, $manuf) = ($line =~ /^\s*($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
161       $oui36_base = $oui36_tmp;
162   }
163   # determine next two bytes
164   if (($oui36_4, $oui36_5, $manuf) = ($line =~ /^\s*($hp)($hp)$hp-$hp$hp$hp\s+\(base\s16\)\s+(\S.*)$/)) {
165     $oui36 = "$oui36_base:$oui36_4:$oui36_5:00/36";
166     $oui36 =~ tr /-/:/; # The IEEE bytes are separated by dashes.
167     # Ensure OUI-36 is all upper-case
168     $oui36 =~ tr/a-f/A-F/;
169     if (exists $oui_list{$oui36}) {
170       printf "$oui36 - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui36}\"\n";
171       $oui36_skipped++;
172     } else {
173       $oui_list{$oui36} = &shorten($manuf);
174       $oui36_added++;
175     }
176   }
177   $oui36_total++;
178 }
179
180 if ($oui36_total < $min_entries) { die "Too few OUI-36 entries ($oui36_total)\n"; }
181
182 # Add IEEE entries for OUIs not yet known.
183
184 $ieee_list = fetch($oui_url);
185
186 foreach $line (split(/\n/, $ieee_list)) {
187   if (($oui, $manuf) = ($line =~ /^\s*($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
188     $oui =~ tr /-/:/;  # The IEEE bytes are separated by dashes.
189     # Ensure OUI is all upper-case
190     $oui =~ tr/a-f/A-F/;
191     if (exists $oui_list{$oui}) {
192       printf "$oui - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
193       $oui_skipped++;
194     } else {
195       $oui_list{$oui} = &shorten($manuf);
196       $oui_added++;
197     }
198   }
199   $oui_total++;
200 }
201
202 if ($oui_total < $min_entries) { die "Too few OUI entries ($oui_total)\n"; }
203
204 # Add CaveBear entries for OUIs not yet known.
205
206 $cb_list = fetch($cb_url);
207
208 foreach $line (split(/\n/, $cb_list)) {
209   if (($oui, $manuf) = ($line =~ /^($cb_re)\s+(\S.*)$/)) {
210     ($h1, $h2, $h3) = ($oui =~ /($hp)($hp)($hp)/);  # The CaveBear bytes have no separators
211     $oui = "$h1:$h2:$h3";
212     # Ensure OUI is all upper-case
213     $oui =~ tr/a-f/A-F/;
214     if (exists $oui_list{$oui}) {
215       # printf "$oui - Skipping CaveBear \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
216       $cb_skipped++;
217     } else {
218       printf "$oui - adding \"$manuf\" from CaveBear\n";
219       $oui_list{$oui} = &shorten($manuf);
220       $cb_added++;
221     }
222   }
223   $cb_total++;
224 }
225
226 if ($cb_total < $min_entries) { die "Too few CaveBear entries ($cb_total)\n"; }
227
228 # Write output file
229
230 open (OUT, "> $outfile") ||
231   die "Couldn't open output file for writing ($outfile)\n";
232
233 print(OUT "# This file was generated by running ./make-manuf.\n");
234 print(OUT "# Don't change it directly, change manuf.tmpl and wka.tmpl instead.\n#\n");
235 print(OUT "$header");
236
237 foreach $oui (sort(keys %oui_list)) {
238   print(OUT "$oui\t$oui_list{$oui}\n");
239 }
240
241 # Write out a blank line separating the OUIs from the well-known
242 # addresses, and then read the well-known address template file
243 # and write it to the manuf file.
244
245 open (WKATMPL, "< $wkatmpl") ||
246   die "Couldn't open well-known address template file for reading ($wkatmpl)\n";
247
248 # XXX - it'd be nice to get this from the Cavebear file, but inferring
249 # the address mask from entries in that file involves some work.
250 #
251 print(OUT "\n");
252 while ($line = <WKATMPL>) {
253   chomp($line);
254   print(OUT "$line\n");
255 }
256
257 $total_added = $tmpl_added + $cb_added + $oui_added + $iab_added;
258 print <<"Fin"
259 Original entries : $tmpl_added
260 IEEE OUI added   : $oui_added
261 IEEE IAB added   : $iab_added
262 IEEE OUI36 added : $oui36_added
263 CaveBear added   : $cb_added
264 Total            : $total_added
265
266 IEEE OUI skipped : $oui_skipped
267 IEEE IAB skipped : $iab_skipped
268 IEEE OUI36 skipd : $oui36_skipped
269 CaveBear skipped : $cb_skipped
270 Fin