aboutsummaryrefslogtreecommitdiffstats
path: root/make-manuf
blob: a9df3ec7f5e169172db105da33cb70df4d587d0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/perl -w
#
# $Id: make-manuf,v 1.7 2002/09/09 19:38:09 guy Exp $
#
# Make-manuf - Creates a file containing ethernet OUIs and their
# company IDs.  It merges the databases at
# http://standards.ieee.org/regauth/oui/index.shtml and
# http://www.cavebear.com/CaveBear/Ethernet/
# with entries in our template file.
#
# The script reads the comments at the top of "manuf.tmpl" and writes
# them to "manuf".  It then joins the manufacturer listing in "manuf.tmpl"
# with the listing in "oui.txt", with the entries in "manuf.tmpl" taking
# precedence.

# LWP is part of the standard Perl module libwww 
eval "require LWP::UserAgent;";
if( $@ ) {
  die "LWP isn't installed. It is part of the standard Perl\n" .
	" module libwww.  Bailing.\n";
}

$template = "manuf.tmpl";
$wkatmpl  = "wka.tmpl";
$outfile  = "manuf";
$inheader = 1;
$ieee_url = "http://standards.ieee.org/regauth/oui/oui_public.txt";
$cb_url   = "http://www.cavebear.com/CaveBear/Ethernet/Ethernet.txt";
%oui_list = ();
$hp       = "[0-9a-fA-F]{2}";
$oui_re   = "$hp:$hp:$hp";
$cb_re    = "$hp$hp$hp";
$ieee_re  = "$hp-$hp-$hp";

$tmpl_added   = 0;
$cb_added     = 0;
$cb_skipped   = 0;
$ieee_added   = 0;
$ieee_skipped = 0;

$agent    = LWP::UserAgent->new;

print "Fetching $cb_url.\n";
$request  = HTTP::Request->new(GET => $cb_url);
$result   = $agent->request($request);

if (!$result->is_success) {
  die ("Error fetching $cb_url: " . $result->status_line . "\n");
}
$cb_list = $result->content;

print "Fetching $ieee_url.\n";
$request  = HTTP::Request->new(GET => $ieee_url);
$result   = $agent->request($request);

if (!$result->is_success) {
  die ("Error fetching $ieee_url: " . $result->status_line . "\n");
}
$ieee_list = $result->content;

open (TMPL, "< $template") || 
  die "Couldn't open template file for reading ($template)\n";

open (WKATMPL, "< $wkatmpl") || 
  die "Couldn't open well-known address template file for reading ($wkatmpl)\n";

open (OUT, "> $outfile") ||
  die "Couldn't open output file for writing ($outfile)\n";

# Write out the header and populate the OUI list with our entries.
while ($line = <TMPL>) {
  chomp($line);
  if ($line !~ /^$oui_re\s+\S/ && $inheader) {
    print(OUT "$line\n");
  } elsif (($oui, $manuf) = ($line =~ /^($oui_re)\s+(\S.*)$/)) {
    $inheader = 0;
    # Ensure OUI is all upper-case
    $oui =~ tr/a-f/A-F/;
    $oui_list{$oui} = $manuf;
    $tmpl_added++;
  }
}

# Add IEEE entries for OUIs not yet known.
foreach $line (split(/\n/, $ieee_list)) {
  if (($oui, $manuf) = ($line =~ /^($ieee_re)\s+\(hex\)\s+(\S.*)$/)) {
    $oui =~ tr /-/:/;  # The IEEE bytes are separated by dashes.
    # Ensure OUI is all upper-case
    $oui =~ tr/a-f/A-F/;
    if (exists $oui_list{$oui}) {
      printf "$oui - Skipping IEEE \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
      $ieee_skipped++;
    } else {
      $oui_list{$oui} = $manuf;
      $ieee_added++;
    }
  }
}

# Add CaveBear entries for OUIs not yet known.
foreach $line (split(/\n/, $cb_list)) {
  if (($oui, $manuf) = ($line =~ /^($cb_re)\s+(\S.*)$/)) {
    ($h1, $h2, $h3) = ($oui =~ /($hp)($hp)($hp)/);  # The CaveBear bytes have no separators
    $oui = "$h1:$h2:$h3";
    # Ensure OUI is all upper-case
    $oui =~ tr/a-f/A-F/;
    if (exists $oui_list{$oui}) {
      printf "$oui - Skipping CaveBear \"$manuf\" in favor of \"$oui_list{$oui}\"\n";
      $cb_skipped++;
    } else {
      $oui_list{$oui} = $manuf;
      $cb_added++;
    }
  }
}

foreach $oui (sort(keys %oui_list)) {
  print(OUT "$oui\t$oui_list{$oui}\n");
}

#
# Write out a blank line separating the OUIs from the well-known
# addresses, and then read the well-known address template file
# and write it to the manuf file.
#
# XXX - it'd be nice to get this from the Cavebear file, but inferring
# the address mask from entries in that file involves some work.
#
print(OUT "\n");
while ($line = <WKATMPL>) {
  chomp($line);
  print(OUT "$line\n");
}

$total_added = $tmpl_added + $cb_added + $ieee_added;
print <<"Fin"
Original entries : $tmpl_added
IEEE added       : $ieee_added
CaveBear added   : $cb_added
Total            : $total_added

IEEE skipped     : $ieee_skipped
CaveBear skipped : $cb_skipped
Fin