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