ccfcc34f4143499966e0080d66691cc6ee5cdf75
[govpp.git] / vendor / github.com / google / gopacket / macs / gen.go
1 // Copyright 2012 Google, Inc. All rights reserved.
2 //
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
5 // tree.
6
7 // +build ignore
8
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:
12 //
13 //  go run gen.go | gofmt > valid_mac_prefixes.go
14 package main
15
16 import (
17         "bufio"
18         "bytes"
19         "encoding/hex"
20         "flag"
21         "fmt"
22         "io"
23         "net/http"
24         "os"
25         "regexp"
26         "sort"
27         "time"
28 )
29
30 const header = `// Copyright 2012 Google, Inc. All rights reserved.
31 //
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
34 // tree.
35
36 package macs
37
38 // Created by gen.go, don't edit manually
39 // Generated at %s
40 // Fetched from %q
41
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{
47 `
48
49 var url = flag.String("url", "http://standards.ieee.org/develop/regauth/oui/oui.txt", "URL to fetch MACs from")
50
51 type mac struct {
52         prefix  [3]byte
53         company string
54 }
55
56 type macs []mac
57
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] }
61
62 func main() {
63         fmt.Fprintf(os.Stderr, "Fetching MACs from %q\n", *url)
64         resp, err := http.Get(*url)
65         if err != nil {
66                 panic(err)
67         }
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)`)
71         got := macs{}
72         for {
73                 line, err := buffered.ReadString('\n')
74                 if err == io.EOF {
75                         break
76                 } else if err != nil {
77                         panic(err)
78                 }
79                 if matches := finder.FindStringSubmatch(line); matches != nil {
80                         var prefix [3]byte
81                         hex.Decode(prefix[:], []byte(matches[1]))
82                         company := matches[2]
83                         if company == "" {
84                                 company = "PRIVATE"
85                         }
86                         fmt.Fprint(os.Stderr, "*")
87                         got = append(got, mac{prefix: prefix, company: company})
88                 }
89         }
90         fmt.Fprintln(os.Stderr, "\nSorting macs")
91         sort.Sort(got)
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)
96         }
97         fmt.Println("}")
98 }