Fix a test.
[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 10 characters.
64   $manuf = substr($manuf, 0, 10);
65
66   if ($manuf =~ /\Q$origmanuf\E/i) {
67     return $manuf;
68   } else {
69     return sprintf("%-22s # %s", $manuf, $origmanuf);
70   }
71 }
72
73 sub fetch
74 {
75   my $url = shift;
76   print "Fetching $url.\n";
77   $request  = HTTP::Request->new(GET => $url);
78   $result   = $agent->request($request);
79
80   if (!$result->is_success) {
81     die ("Error fetching $url: " . $result->status_line . "\n");
82   }
83   return decode("iso-8859-1", $result->content);
84 }
85
86 # Write out the header and populate the OUI list with our entries.
87
88 open (TMPL, "< $template") || 
89   die "Couldn't open template file for reading ($template)\n";
90
91 while ($line = <TMPL>) {
92   chomp($line);
93   if ($line !~ /^$oui_re\s+\S/ && $inheader) {
94     $header .= "$line\n";
95   } elsif (($oui, $manuf) = ($line =~ /^($oui_re)\s+(\S.*)$/)) {
96     $inheader = 0;
97     # Ensure OUI is all upper-case
98     $oui =~ tr/a-f/A-F/;
99     # $oui_list{$oui} = &shorten($manuf);
100     $oui_list{$oui} = $manuf;
101     $tmpl_added++;
102   }
103 }
104
105 # Add IEEE entries for IABs
106
107 $ieee_list = fetch($iab_url);
108
109 foreach $line (split(/\n/, $ieee_list)) {
110   # determine the OUI used for IAB (currently only 00-50-C2)
111   if (($iab_tmp, $manuf) = ($line =~ /^($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
112       $iab_base = $iab_tmp;
113   }
114   # determine next two bytes
115   if (($iab4, $iab5, $manuf) = ($line =~ /^($hp)($hp)$hp-$hp$hp$hp\s+\(base\s16\)\s+(\S.*)$/)) {
116     $iab = "$iab_base:$iab4:$iab5:00/36";
117     $iab =~ tr /-/:/; # The IEEE bytes are separated by dashes.
118     # Ensure IAB is all upper-case
119     $iab =~ tr/a-f/A-F/;
120     if (exists $oui_list{$iab}) {
121       printf "$iab - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$iab}\"\n";
122       $iab_skipped++;
123     } else {
124       $oui_list{$iab} = &shorten($manuf);
125       $iab_added++;
126     }
127   }
128 }
129
130 # Add IEEE entries for OUIs not yet known.
131
132 $ieee_list = fetch($oui_url);
133
134 foreach $line (split(/\n/, $ieee_list)) {
135   if (($oui, $manuf) = ($line =~ /^($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
136     $oui =~ tr /-/:/;  # The IEEE bytes are separated by dashes.
137     # Ensure OUI is all upper-case
138     $oui =~ tr/a-f/A-F/;
139     if (exists $oui_list{$oui}) {
140       printf "$oui - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
141       $oui_skipped++;
142     } else {
143       $oui_list{$oui} = &shorten($manuf);
144       $oui_added++;
145     }
146   }
147 }
148
149 # Add CaveBear entries for OUIs not yet known.
150
151 $cb_list = fetch($cb_url);
152
153 foreach $line (split(/\n/, $cb_list)) {
154   if (($oui, $manuf) = ($line =~ /^($cb_re)\s+(\S.*)$/)) {
155     ($h1, $h2, $h3) = ($oui =~ /($hp)($hp)($hp)/);  # The CaveBear bytes have no separators
156     $oui = "$h1:$h2:$h3";
157     # Ensure OUI is all upper-case
158     $oui =~ tr/a-f/A-F/;
159     if (exists $oui_list{$oui}) {
160       # printf "$oui - Skipping CaveBear \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
161       $cb_skipped++;
162     } else {
163       printf "$oui - adding \"$manuf\" from CaveBear\n";
164       $oui_list{$oui} = &shorten($manuf);
165       $cb_added++;
166     }
167   }
168 }
169
170 # Write output file
171
172 open (OUT, "> $outfile") ||
173   die "Couldn't open output file for writing ($outfile)\n";
174
175 print(OUT "# This file was generated by running ./make-manuf.\n");
176 print(OUT "# Don't change it directly, change manuf.tmpl and wka.tmpl instead.\n#\n");
177 print(OUT "$header");
178
179 foreach $oui (sort(keys %oui_list)) {
180   print(OUT "$oui\t$oui_list{$oui}\n");
181 }
182
183 # Write out a blank line separating the OUIs from the well-known
184 # addresses, and then read the well-known address template file
185 # and write it to the manuf file.
186
187 open (WKATMPL, "< $wkatmpl") || 
188   die "Couldn't open well-known address template file for reading ($wkatmpl)\n";
189
190 # XXX - it'd be nice to get this from the Cavebear file, but inferring
191 # the address mask from entries in that file involves some work.
192 #
193 print(OUT "\n");
194 while ($line = <WKATMPL>) {
195   chomp($line);
196   print(OUT "$line\n");
197 }
198
199 $total_added = $tmpl_added + $cb_added + $oui_added + $iab_added;
200 print <<"Fin"
201 Original entries : $tmpl_added
202 IEEE OUI added   : $oui_added
203 IEEE IAB added   : $iab_added
204 CaveBear added   : $cb_added
205 Total            : $total_added
206
207 IEEE OUI skipped : $oui_skipped
208 IEEE IAB skipped : $iab_skipped
209 CaveBear skipped : $cb_skipped
210 Fin