Include "snprintf.h", if necessary, to declare "snprintf()".
[obnox/wireshark/wip.git] / make-manuf
1 #!/usr/bin/perl -w
2 #
3 # $Id: make-manuf,v 1.2 2000/11/29 04:11:48 gram Exp $
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 eval "require LWP::UserAgent;";
17 if( $@ ) {
18   die "LWP isn't installed.  Bailing.\n";
19 }
20
21 $template = "manuf.tmpl";
22 $outfile  = "manuf";
23 $inheader = 1;
24 $ieee_url = "http://standards.ieee.org/regauth/oui/oui.txt";
25 $cb_url   = "http://www.cavebear.com/CaveBear/Ethernet/Ethernet.txt";
26 %oui_list = ();
27 $hp       = "[0-9a-fA-F]{2}";
28 $oui_re   = "$hp:$hp:$hp";
29 $cb_re    = "$hp$hp$hp";
30 $ieee_re  = "$hp-$hp-$hp";
31
32 $tmpl_added   = 0;
33 $cb_added     = 0;
34 $cb_skipped   = 0;
35 $ieee_added   = 0;
36 $ieee_skipped = 0;
37
38 $agent    = LWP::UserAgent->new;
39
40 print "Fetching $cb_url.\n";
41 $request  = HTTP::Request->new(GET => $cb_url);
42 $result   = $agent->request($request);
43
44 if (!$result->is_success) {
45   die ("Error fetching $cb_url: " . $result->status_line . "\n");
46 }
47 $cb_list = $result->content;
48
49 print "Fetching $ieee_url.\n";
50 $request  = HTTP::Request->new(GET => $ieee_url);
51 $result   = $agent->request($request);
52
53 if (!$result->is_success) {
54   die ("Error fetching $ieee_url: " . $result->status_line . "\n");
55 }
56 $ieee_list = $result->content;
57
58 open (TMPL, "< $template") || 
59   die "Couldn't open template file for reading ($template)\n";
60
61 open (OUT, "> $outfile") ||
62   die "Couldn't open template file for writing ($template)\n";
63
64 # Write out the header and populate the OUI list with our entries.
65 while ($line = <TMPL>) {
66   chomp($line);
67   if ($line !~ /^$oui_re\s+\S/ && $inheader) {
68     print(OUT "$line\n");
69   } elsif (($oui, $manuf) = ($line =~ /^($oui_re)\s+(\S.*)$/)) {
70     $inheader = 0;
71     # Ensure OUI is all upper-case
72     $oui =~ tr/a-f/A-F/;
73     $oui_list{$oui} = $manuf;
74     $tmpl_added++;
75   }
76 }
77
78 foreach $line (split(/\n/, $cb_list)) {
79   if (($oui, $manuf) = ($line =~ /^($cb_re)\s+(\S.*)$/)) {
80     ($h1, $h2, $h3) = ($oui =~ /($hp)($hp)($hp)/);  # The CaveBear bytes have no separators
81     $oui = "$h1:$h2:$h3";
82     # Ensure OUI is all upper-case
83     $oui =~ tr/a-f/A-F/;
84     if (exists $oui_list{$oui}) {
85       printf "$oui - Skipping CaveBear \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
86       $cb_skipped++;
87     } else {
88       $oui_list{$oui} = $manuf;
89       $cb_added++;
90     }
91   }
92 }
93
94 foreach $line (split(/\n/, $ieee_list)) {
95   if (($oui, $manuf) = ($line =~ /^($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
96     $oui =~ tr /-/:/;  # The IEEE bytes are separated by dashes.
97     # Ensure OUI is all upper-case
98     $oui =~ tr/a-f/A-F/;
99     if (exists $oui_list{$oui}) {
100       printf "$oui - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
101       $ieee_skipped++;
102     } else {
103       $oui_list{$oui} = $manuf;
104       $ieee_added++;
105     }
106   }
107 }
108
109 foreach $oui (sort(keys %oui_list)) {
110   print(OUT "$oui\t$oui_list{$oui}\n");
111 }
112
113 $total_added = $tmpl_added + $cb_added + $ieee_added;
114 print <<"Fin"
115 Original entries : $tmpl_added
116 CaveBear added   : $cb_added
117 IEEE added       : $ieee_added
118 Total            : $total_added
119
120 CaveBear skipped : $cb_skipped
121 IEEE skipped     : $ieee_skipped
122 Fin