Fix various typos and spelling errors.
[obnox/wireshark/wip.git] / 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
7 # http://standards.ieee.org/regauth/oui/index.shtml and
8 # http://www.cavebear.com/CaveBear/Ethernet/
9 # with entries in our template file.
10 #
11 # The script reads the comments at the top of "manuf.tmpl" and writes
12 # them to "manuf".  It then joins the manufacturer listing in "manuf.tmpl"
13 # with the listing in "oui.txt", with the entries in "manuf.tmpl" taking
14 # precedence.
15
16 # LWP is part of the standard Perl module libwww
17
18 use Encode;
19 use open ':encoding(utf8)';
20
21 eval "require LWP::UserAgent;";
22 if( $@ ) {
23   die "LWP isn't installed. It is part of the standard Perl\n" .
24         " module libwww.  Bailing.\n";
25 }
26 $agent    = LWP::UserAgent->new;
27
28 $template = "manuf.tmpl";
29 $wkatmpl  = "wka.tmpl";
30 $outfile  = "manuf";
31 $inheader = 1;
32 $oui_url  = "http://standards.ieee.org/regauth/oui/oui.txt";
33 $iab_url  = "http://standards.ieee.org/regauth/oui/iab.txt";
34 $cb_url   = "http://www.cavebear.com/CaveBear/Ethernet/Ethernet.txt";
35 %oui_list = ();
36 $hp       = "[0-9a-fA-F]{2}";
37 $oui_re   = "$hp:$hp:$hp";
38 $cb_re    = "$hp$hp$hp";
39 $ieee_re  = "$hp-$hp-$hp";
40
41 $tmpl_added   = 0;
42 $cb_added     = 0;
43 $cb_skipped   = 0;
44 $oui_added   = 0;
45 $oui_skipped = 0;
46 $iab_added   = 0;
47 $iab_skipped = 0;
48
49 sub shorten
50 {
51   my $origmanuf = shift;
52   my $manuf = " " . $origmanuf . " ";
53   # Remove any punctuation
54   $manuf =~ tr/',.()/    /;
55   # & isn't needed when Standalone
56   $manuf =~ s/ \& / /g;
57   # Remove any "the", "inc", "plc" ...
58   $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;
59   # Convert to consistent case
60   $manuf =~ s/(\w+)/\u\L$1/g;
61   # Remove all spaces
62   $manuf =~ s/\s+//g;
63   # Truncate all names to a reasonable length, say, 8 characters.
64   # If the string contains UTF-8, this may be substantially more than 8 bytes.
65   $manuf = substr($manuf, 0, 8);
66
67   if ($manuf =~ /\Q$origmanuf\E/i) {
68     return $manuf;
69   } else {
70     return sprintf("%-22s # %s", $manuf, $origmanuf);
71   }
72 }
73
74 sub fetch
75 {
76   my $url = shift;
77   print "Fetching $url.\n";
78   $request  = HTTP::Request->new(GET => $url);
79   $result   = $agent->request($request);
80
81   if (!$result->is_success) {
82     die ("Error fetching $url: " . $result->status_line . "\n");
83   }
84   return decode("iso-8859-1", $result->content);
85 }
86
87 # Write out the header and populate the OUI list with our entries.
88
89 open (TMPL, "< $template") ||
90   die "Couldn't open template file for reading ($template)\n";
91
92 while ($line = <TMPL>) {
93   chomp($line);
94   if ($line !~ /^$oui_re\s+\S/ && $inheader) {
95     $header .= "$line\n";
96   } elsif (($oui, $manuf) = ($line =~ /^($oui_re)\s+(\S.*)$/)) {
97     $inheader = 0;
98     # Ensure OUI is all upper-case
99     $oui =~ tr/a-f/A-F/;
100     # $oui_list{$oui} = &shorten($manuf);
101     $oui_list{$oui} = $manuf;
102     $tmpl_added++;
103   }
104 }
105
106 # Add IEEE entries for IABs
107
108 $ieee_list = fetch($iab_url);
109
110 foreach $line (split(/\n/, $ieee_list)) {
111   # determine the OUI used for IAB (currently only 00-50-C2)
112   if (($iab_tmp, $manuf) = ($line =~ /^($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
113       $iab_base = $iab_tmp;
114   }
115   # determine next two bytes
116   if (($iab4, $iab5, $manuf) = ($line =~ /^($hp)($hp)$hp-$hp$hp$hp\s+\(base\s16\)\s+(\S.*)$/)) {
117     $iab = "$iab_base:$iab4:$iab5:00/36";
118     $iab =~ tr /-/:/; # The IEEE bytes are separated by dashes.
119     # Ensure IAB is all upper-case
120     $iab =~ tr/a-f/A-F/;
121     if (exists $oui_list{$iab}) {
122       printf "$iab - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$iab}\"\n";
123       $iab_skipped++;
124     } else {
125       $oui_list{$iab} = &shorten($manuf);
126       $iab_added++;
127     }
128   }
129 }
130
131 # Add IEEE entries for OUIs not yet known.
132
133 $ieee_list = fetch($oui_url);
134
135 foreach $line (split(/\n/, $ieee_list)) {
136   if (($oui, $manuf) = ($line =~ /^($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
137     $oui =~ tr /-/:/;  # The IEEE bytes are separated by dashes.
138     # Ensure OUI is all upper-case
139     $oui =~ tr/a-f/A-F/;
140     if (exists $oui_list{$oui}) {
141       printf "$oui - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
142       $oui_skipped++;
143     } else {
144       $oui_list{$oui} = &shorten($manuf);
145       $oui_added++;
146     }
147   }
148 }
149
150 # Add CaveBear entries for OUIs not yet known.
151
152 $cb_list = fetch($cb_url);
153
154 foreach $line (split(/\n/, $cb_list)) {
155   if (($oui, $manuf) = ($line =~ /^($cb_re)\s+(\S.*)$/)) {
156     ($h1, $h2, $h3) = ($oui =~ /($hp)($hp)($hp)/);  # The CaveBear bytes have no separators
157     $oui = "$h1:$h2:$h3";
158     # Ensure OUI is all upper-case
159     $oui =~ tr/a-f/A-F/;
160     if (exists $oui_list{$oui}) {
161       # printf "$oui - Skipping CaveBear \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
162       $cb_skipped++;
163     } else {
164       printf "$oui - adding \"$manuf\" from CaveBear\n";
165       $oui_list{$oui} = &shorten($manuf);
166       $cb_added++;
167     }
168   }
169 }
170
171 # Write output file
172
173 open (OUT, "> $outfile") ||
174   die "Couldn't open output file for writing ($outfile)\n";
175
176 print(OUT "# This file was generated by running ./make-manuf.\n");
177 print(OUT "# Don't change it directly, change manuf.tmpl and wka.tmpl instead.\n#\n");
178 print(OUT "$header");
179
180 foreach $oui (sort(keys %oui_list)) {
181   print(OUT "$oui\t$oui_list{$oui}\n");
182 }
183
184 # Write out a blank line separating the OUIs from the well-known
185 # addresses, and then read the well-known address template file
186 # and write it to the manuf file.
187
188 open (WKATMPL, "< $wkatmpl") ||
189   die "Couldn't open well-known address template file for reading ($wkatmpl)\n";
190
191 # XXX - it'd be nice to get this from the Cavebear file, but inferring
192 # the address mask from entries in that file involves some work.
193 #
194 print(OUT "\n");
195 while ($line = <WKATMPL>) {
196   chomp($line);
197   print(OUT "$line\n");
198 }
199
200 $total_added = $tmpl_added + $cb_added + $oui_added + $iab_added;
201 print <<"Fin"
202 Original entries : $tmpl_added
203 IEEE OUI added   : $oui_added
204 IEEE IAB added   : $iab_added
205 CaveBear added   : $cb_added
206 Total            : $total_added
207
208 IEEE OUI skipped : $oui_skipped
209 IEEE IAB skipped : $iab_skipped
210 CaveBear skipped : $cb_skipped
211 Fin