From Paul Moore:
[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 eval "require LWP::UserAgent;";
19 if( $@ ) {
20   die "LWP isn't installed. It is part of the standard Perl\n" .
21         " module libwww.  Bailing.\n";
22 }
23 $agent    = LWP::UserAgent->new;
24
25 $template = "manuf.tmpl";
26 $wkatmpl  = "wka.tmpl";
27 $outfile  = "manuf";
28 $inheader = 1;
29 $ieee_url = "http://standards.ieee.org/regauth/oui/oui_public.txt";
30 $cb_url   = "http://www.cavebear.com/CaveBear/Ethernet/Ethernet.txt";
31 %oui_list = ();
32 $hp       = "[0-9a-fA-F]{2}";
33 $oui_re   = "$hp:$hp:$hp";
34 $cb_re    = "$hp$hp$hp";
35 $ieee_re  = "$hp-$hp-$hp";
36
37 $tmpl_added   = 0;
38 $cb_added     = 0;
39 $cb_skipped   = 0;
40 $ieee_added   = 0;
41 $ieee_skipped = 0;
42
43 sub shorten
44 {
45   my $origmanuf = shift; 
46   my $manuf = " " . $origmanuf . " ";
47   # Remove any punctuation
48   $manuf =~ tr/',.()/    /;
49   # & isn't needed when Standalone
50   $manuf =~ s/ \& / /g;
51   # Remove any "the", "inc", "plc" ...
52   $manuf =~ s/\s(the|inc|incorporated|plc||systems|corp|corporation|s\/a|a\/s|ab|ag|kg|gmbh|co|company|limited|ltd)(?= )//gi;
53   # Convert to consistent case
54   $manuf =~ s/(\w+)/\u\L$1/g;
55   # Remove all spaces
56   $manuf =~ s/\s+//g;
57   # Truncate all names to a reasonable length, say 10 characters.
58   $manuf = substr($manuf, 0, 10);
59
60   if ($manuf =~ /\Q$origmanuf\E/i) {
61     return $manuf;
62   } else {
63     return sprintf("%-22s # %s", $manuf, $origmanuf);
64   }
65 }
66
67 # Write out the header and populate the OUI list with our entries.
68
69 open (TMPL, "< $template") || 
70   die "Couldn't open template file for reading ($template)\n";
71
72 while ($line = <TMPL>) {
73   chomp($line);
74   if ($line !~ /^$oui_re\s+\S/ && $inheader) {
75     $header .= "$line\n";
76   } elsif (($oui, $manuf) = ($line =~ /^($oui_re)\s+(\S.*)$/)) {
77     $inheader = 0;
78     # Ensure OUI is all upper-case
79     $oui =~ tr/a-f/A-F/;
80     # $oui_list{$oui} = &shorten($manuf);
81     $oui_list{$oui} = $manuf;
82     $tmpl_added++;
83   }
84 }
85
86 # Add IEEE entries for OUIs not yet known.
87
88 print "Fetching $ieee_url.\n";
89 $request  = HTTP::Request->new(GET => $ieee_url);
90 $result   = $agent->request($request);
91
92 if (!$result->is_success) {
93   die ("Error fetching $ieee_url: " . $result->status_line . "\n");
94 }
95 $ieee_list = $result->content;
96
97 foreach $line (split(/\n/, $ieee_list)) {
98   if (($oui, $manuf) = ($line =~ /^($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
99     $oui =~ tr /-/:/;  # The IEEE bytes are separated by dashes.
100     # Ensure OUI is all upper-case
101     $oui =~ tr/a-f/A-F/;
102     if (exists $oui_list{$oui}) {
103       printf "$oui - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
104       $ieee_skipped++;
105     } else {
106       $oui_list{$oui} = &shorten($manuf);
107       $ieee_added++;
108     }
109   }
110 }
111
112 # Add CaveBear entries for OUIs not yet known.
113
114 print "Fetching $cb_url.\n";
115 $request  = HTTP::Request->new(GET => $cb_url);
116 $result   = $agent->request($request);
117
118 if (!$result->is_success) {
119   die ("Error fetching $cb_url: " . $result->status_line . "\n");
120 }
121 $cb_list = $result->content;
122
123 foreach $line (split(/\n/, $cb_list)) {
124   if (($oui, $manuf) = ($line =~ /^($cb_re)\s+(\S.*)$/)) {
125     ($h1, $h2, $h3) = ($oui =~ /($hp)($hp)($hp)/);  # The CaveBear bytes have no separators
126     $oui = "$h1:$h2:$h3";
127     # Ensure OUI is all upper-case
128     $oui =~ tr/a-f/A-F/;
129     if (exists $oui_list{$oui}) {
130       # printf "$oui - Skipping CaveBear \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
131       $cb_skipped++;
132     } else {
133       printf "$oui - adding \"$manuf\" from CaveBear\n";
134       $oui_list{$oui} = &shorten($manuf);
135       $cb_added++;
136     }
137   }
138 }
139
140 # Write output file
141
142 open (OUT, "> $outfile") ||
143   die "Couldn't open output file for writing ($outfile)\n";
144
145 print(OUT "# This file was generated by running ./make-manuf.\n");
146 print(OUT "# Don't change it directly, change manuf.tmpl and wka.tmpl instead.\n#\n");
147 print(OUT "$header");
148
149 foreach $oui (sort(keys %oui_list)) {
150   print(OUT "$oui\t$oui_list{$oui}\n");
151 }
152
153 # Write out a blank line separating the OUIs from the well-known
154 # addresses, and then read the well-known address template file
155 # and write it to the manuf file.
156
157 open (WKATMPL, "< $wkatmpl") || 
158   die "Couldn't open well-known address template file for reading ($wkatmpl)\n";
159
160 # XXX - it'd be nice to get this from the Cavebear file, but inferring
161 # the address mask from entries in that file involves some work.
162 #
163 print(OUT "\n");
164 while ($line = <WKATMPL>) {
165   chomp($line);
166   print(OUT "$line\n");
167 }
168
169 $total_added = $tmpl_added + $cb_added + $ieee_added;
170 print <<"Fin"
171 Original entries : $tmpl_added
172 IEEE added       : $ieee_added
173 CaveBear added   : $cb_added
174 Total            : $total_added
175
176 IEEE skipped     : $ieee_skipped
177 CaveBear skipped : $cb_skipped
178 Fin