1 // Copyright 2012 Google, Inc. All rights reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
9 // This binary pulls the list of known MAC
10 // prefixes from IEEE and writes them out to a go file which is compiled
11 // into gopacket. It should be run as follows:
13 // go run gen.go | gofmt > valid_mac_prefixes.go
30 const header = `// Copyright 2012 Google, Inc. All rights reserved.
32 // Use of this source code is governed by a BSD-style license
33 // that can be found in the LICENSE file in the root of the source
38 // Created by gen.go, don't edit manually
42 // ValidMACPrefixMap maps a valid MAC address prefix to the name of the
43 // organization that owns the rights to use it. We map it to a hidden
44 // variable so it won't show up in godoc, since it's a very large map.
45 var ValidMACPrefixMap = validMACPrefixMap
46 var validMACPrefixMap = map[[3]byte]string{
49 var url = flag.String("url", "http://standards.ieee.org/develop/regauth/oui/oui.txt", "URL to fetch MACs from")
58 func (m macs) Len() int { return len(m) }
59 func (m macs) Less(i, j int) bool { return bytes.Compare(m[i].prefix[:], m[j].prefix[:]) < 0 }
60 func (m macs) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
63 fmt.Fprintf(os.Stderr, "Fetching MACs from %q\n", *url)
64 resp, err := http.Get(*url)
68 defer resp.Body.Close()
69 buffered := bufio.NewReader(resp.Body)
70 finder := regexp.MustCompile(`^\s*([0-9A-F]{6})\s+\(base 16\)\s+(.*\S)`)
73 line, err := buffered.ReadString('\n')
76 } else if err != nil {
79 if matches := finder.FindStringSubmatch(line); matches != nil {
81 hex.Decode(prefix[:], []byte(matches[1]))
86 fmt.Fprint(os.Stderr, "*")
87 got = append(got, mac{prefix: prefix, company: company})
90 fmt.Fprintln(os.Stderr, "\nSorting macs")
92 fmt.Fprintln(os.Stderr, "Starting write to standard output")
93 fmt.Printf(header, time.Now(), *url)
94 for _, m := range got {
95 fmt.Printf("\t[3]byte{%d, %d, %d}: %q,\n", m.prefix[0], m.prefix[1], m.prefix[2], m.company)