Add newlines to the AsciiDoc output.
[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 $oui_url  = "http://standards.ieee.org/develop/regauth/oui/oui.txt";
42 $iab_url  = "http://standards.ieee.org/develop/regauth/iab/iab.txt";
43 $oui36_url  = "http://standards.ieee.org/develop/regauth/oui36/oui36.txt";
44 $cb_url   = "http://www.cavebear.com/archive/cavebear/Ethernet/Ethernet.txt";
45 %oui_list = ();
46 $hp       = "[0-9a-fA-F]{2}";
47 $oui_re   = "$hp:$hp:$hp";
48 $cb_re    = "$hp$hp$hp";
49 $ieee_re  = "$hp-$hp-$hp";
50
51 $tmpl_added   = 0;
52 $cb_added     = 0;
53 $cb_skipped   = 0;
54 $oui_added   = 0;
55 $oui_skipped = 0;
56 $iab_added   = 0;
57 $iab_skipped = 0;
58 $oui36_added   = 0;
59 $oui36_skipped = 0;
60
61 sub shorten
62 {
63   my $origmanuf = shift;
64   my $manuf = " " . $origmanuf . " ";
65   # Remove any punctuation
66   $manuf =~ tr/',.()/    /;
67   # & isn't needed when Standalone
68   $manuf =~ s/ \& / /g;
69   # Remove any "the", "inc", "plc" ...
70   $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;
71   # Convert to consistent case
72   $manuf =~ s/(\w+)/\u\L$1/g;
73   # Remove all spaces
74   $manuf =~ s/\s+//g;
75   # Truncate all names to a reasonable length, say, 8 characters.
76   # If the string contains UTF-8, this may be substantially more than 8 bytes.
77   $manuf = substr($manuf, 0, 8);
78
79   if ($manuf =~ /\Q$origmanuf\E/i) {
80     return $manuf;
81   } else {
82     return sprintf("%-22s # %s", $manuf, $origmanuf);
83   }
84 }
85
86 sub fetch
87 {
88   my $url = shift;
89   print "Fetching $url.\n";
90   $request  = HTTP::Request->new(GET => $url);
91   $result   = $agent->request($request);
92
93   if (!$result->is_success) {
94     die ("Error fetching $url: " . $result->status_line . "\n");
95   }
96   return decode("utf-8", $result->content);
97 }
98
99 # Write out the header and populate the OUI list with our entries.
100
101 open (TMPL, "< $template") ||
102   die "Couldn't open template file for reading ($template)\n";
103
104 while ($line = <TMPL>) {
105   chomp($line);
106   if ($line !~ /^$oui_re\s+\S/ && $inheader) {
107     $header .= "$line\n";
108   } elsif (($oui, $manuf) = ($line =~ /^($oui_re)\s+(\S.*)$/)) {
109     $inheader = 0;
110     # Ensure OUI is all upper-case
111     $oui =~ tr/a-f/A-F/;
112     # $oui_list{$oui} = &shorten($manuf);
113     $oui_list{$oui} = $manuf;
114     $tmpl_added++;
115   }
116 }
117
118 # Add IEEE entries for IABs
119
120 $ieee_list = fetch($iab_url);
121
122 foreach $line (split(/\n/, $ieee_list)) {
123   # determine the OUI used for IAB (currently only 00-50-C2)
124   if (($iab_tmp, $manuf) = ($line =~ /^\s*($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
125       $iab_base = $iab_tmp;
126   }
127   # determine next two bytes
128   if (($iab4, $iab5, $manuf) = ($line =~ /^\s*($hp)($hp)$hp-$hp$hp$hp\s+\(base\s16\)\s+(\S.*)$/)) {
129     $iab = "$iab_base:$iab4:$iab5:00/36";
130     $iab =~ tr /-/:/; # The IEEE bytes are separated by dashes.
131     # Ensure IAB is all upper-case
132     $iab =~ tr/a-f/A-F/;
133     if (exists $oui_list{$iab}) {
134       printf "$iab - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$iab}\"\n";
135       $iab_skipped++;
136     } else {
137       $oui_list{$iab} = &shorten($manuf);
138       $iab_added++;
139     }
140   }
141 }
142
143 # Add IEEE entries for OUI-36
144
145 $ieee_list = fetch($oui36_url);
146
147 foreach $line (split(/\n/, $ieee_list)) {
148   # determine the OUI used for OUI-36 (currently only 00-1B-C5)
149   if (($oui36_tmp, $manuf) = ($line =~ /^\s*($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
150       $oui36_base = $oui36_tmp;
151   }
152   # determine next two bytes
153   if (($oui36_4, $oui36_5, $manuf) = ($line =~ /^\s*($hp)($hp)$hp-$hp$hp$hp\s+\(base\s16\)\s+(\S.*)$/)) {
154     $oui36 = "$oui36_base:$oui36_4:$oui36_5:00/36";
155     $oui36 =~ tr /-/:/; # The IEEE bytes are separated by dashes.
156     # Ensure OUI-36 is all upper-case
157     $oui36 =~ tr/a-f/A-F/;
158     if (exists $oui_list{$oui36}) {
159       printf "$oui36 - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui36}\"\n";
160       $oui36_skipped++;
161     } else {
162       $oui_list{$oui36} = &shorten($manuf);
163       $oui36_added++;
164     }
165   }
166 }
167
168 # Add IEEE entries for OUIs not yet known.
169
170 $ieee_list = fetch($oui_url);
171
172 foreach $line (split(/\n/, $ieee_list)) {
173   if (($oui, $manuf) = ($line =~ /^\s*($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
174     $oui =~ tr /-/:/;  # The IEEE bytes are separated by dashes.
175     # Ensure OUI is all upper-case
176     $oui =~ tr/a-f/A-F/;
177     if (exists $oui_list{$oui}) {
178       printf "$oui - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
179       $oui_skipped++;
180     } else {
181       $oui_list{$oui} = &shorten($manuf);
182       $oui_added++;
183     }
184   }
185 }
186
187 # Add CaveBear entries for OUIs not yet known.
188
189 $cb_list = fetch($cb_url);
190
191 foreach $line (split(/\n/, $cb_list)) {
192   if (($oui, $manuf) = ($line =~ /^($cb_re)\s+(\S.*)$/)) {
193     ($h1, $h2, $h3) = ($oui =~ /($hp)($hp)($hp)/);  # The CaveBear bytes have no separators
194     $oui = "$h1:$h2:$h3";
195     # Ensure OUI is all upper-case
196     $oui =~ tr/a-f/A-F/;
197     if (exists $oui_list{$oui}) {
198       # printf "$oui - Skipping CaveBear \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
199       $cb_skipped++;
200     } else {
201       printf "$oui - adding \"$manuf\" from CaveBear\n";
202       $oui_list{$oui} = &shorten($manuf);
203       $cb_added++;
204     }
205   }
206 }
207
208 # Write output file
209
210 open (OUT, "> $outfile") ||
211   die "Couldn't open output file for writing ($outfile)\n";
212
213 print(OUT "# This file was generated by running ./make-manuf.\n");
214 print(OUT "# Don't change it directly, change manuf.tmpl and wka.tmpl instead.\n#\n");
215 print(OUT "$header");
216
217 foreach $oui (sort(keys %oui_list)) {
218   print(OUT "$oui\t$oui_list{$oui}\n");
219 }
220
221 # Write out a blank line separating the OUIs from the well-known
222 # addresses, and then read the well-known address template file
223 # and write it to the manuf file.
224
225 open (WKATMPL, "< $wkatmpl") ||
226   die "Couldn't open well-known address template file for reading ($wkatmpl)\n";
227
228 # XXX - it'd be nice to get this from the Cavebear file, but inferring
229 # the address mask from entries in that file involves some work.
230 #
231 print(OUT "\n");
232 while ($line = <WKATMPL>) {
233   chomp($line);
234   print(OUT "$line\n");
235 }
236
237 $total_added = $tmpl_added + $cb_added + $oui_added + $iab_added;
238 print <<"Fin"
239 Original entries : $tmpl_added
240 IEEE OUI added   : $oui_added
241 IEEE IAB added   : $iab_added
242 IEEE OUI36 added : $oui36_added
243 CaveBear added   : $cb_added
244 Total            : $total_added
245
246 IEEE OUI skipped : $oui_skipped
247 IEEE IAB skipped : $iab_skipped
248 IEEE OUI36 skipd : $oui36_skipped
249 CaveBear skipped : $cb_skipped
250 Fin