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 known ports from IANA, and uses them to populate
10 // iana_ports.go's TCPPortNames and UDPPortNames maps.
12 // go run gen.go | gofmt > iana_ports.go
27 const fmtString = `// Copyright 2012 Google, Inc. All rights reserved.
31 // Created by gen.go, don't edit manually
35 // TCPPortNames contains the port names for all TCP ports.
36 var TCPPortNames = tcpPortNames
38 // UDPPortNames contains the port names for all UDP ports.
39 var UDPPortNames = udpPortNames
41 // SCTPPortNames contains the port names for all SCTP ports.
42 var SCTPPortNames = sctpPortNames
44 var tcpPortNames = map[TCPPort]string{
46 var udpPortNames = map[UDPPort]string{
48 var sctpPortNames = map[SCTPPort]string{
52 var url = flag.String("url", "http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml", "URL to grab port numbers from")
55 fmt.Fprintf(os.Stderr, "Fetching ports from %q\n", *url)
56 resp, err := http.Get(*url)
60 defer resp.Body.Close()
61 body, err := ioutil.ReadAll(resp.Body)
65 fmt.Fprintln(os.Stderr, "Parsing XML")
68 Protocol string `xml:"protocol"`
69 Number string `xml:"number"`
70 Name string `xml:"name"`
73 xml.Unmarshal(body, ®istry)
74 var tcpPorts bytes.Buffer
75 var udpPorts bytes.Buffer
76 var sctpPorts bytes.Buffer
77 done := map[string]map[int]bool{
78 "tcp": map[int]bool{},
79 "udp": map[int]bool{},
80 "sctp": map[int]bool{},
82 for _, r := range registry.Records {
83 port, err := strconv.Atoi(r.Number)
101 if done[r.Protocol][port] {
104 done[r.Protocol][port] = true
105 fmt.Fprintf(b, "\t%d: %q,\n", port, r.Name)
107 fmt.Fprintln(os.Stderr, "Writing results to stdout")
108 fmt.Printf(fmtString, time.Now(), *url, tcpPorts.String(), udpPorts.String(), sctpPorts.String())