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
|
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
//go:build ignore
// +build ignore
// This binary pulls known ports from IANA, and uses them to populate
// iana_ports.go's TCPPortNames and UDPPortNames maps.
//
// go run gen.go | gofmt > iana_ports.go
package main
import (
"bytes"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
)
const fmtString = `// Copyright 2012 Google, Inc. All rights reserved.
package layers
// Created by gen.go, don't edit manually
// Generated at %s
// Fetched from %q
// TCPPortNames contains the port names for all TCP ports.
func TCPPortNames(port TCPPort) (string, bool) {
switch port {
%s
}
return "", false
}
// UDPPortNames contains the port names for all UDP ports.
func UDPPortNames(port UDPPort) (string, bool) {
switch port {
%s
}
return "", false
}
// SCTPPortNames contains the port names for all SCTP ports.
func SCTPPortNames(port SCTPPort) (string, bool) {
switch port {
%s
}
return "", false
}
`
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")
func main() {
fmt.Fprintf(os.Stderr, "Fetching ports from %q\n", *url)
resp, err := http.Get(*url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Fprintln(os.Stderr, "Parsing XML")
var registry struct {
Records []struct {
Protocol string `xml:"protocol"`
Number string `xml:"number"`
Name string `xml:"name"`
} `xml:"record"`
}
xml.Unmarshal(body, ®istry)
var tcpPorts bytes.Buffer
var udpPorts bytes.Buffer
var sctpPorts bytes.Buffer
done := map[string]map[int]bool{
"tcp": {},
"udp": {},
"sctp": {},
}
for _, r := range registry.Records {
port, err := strconv.Atoi(r.Number)
if err != nil {
continue
}
if r.Name == "" {
continue
}
var b *bytes.Buffer
switch r.Protocol {
case "tcp":
b = &tcpPorts
case "udp":
b = &udpPorts
case "sctp":
b = &sctpPorts
default:
continue
}
if done[r.Protocol][port] {
continue
}
done[r.Protocol][port] = true
fmt.Fprintf(b, "\tcase %d: return %q, true\n", port, r.Name)
}
fmt.Fprintln(os.Stderr, "Writing results to stdout")
fmt.Printf(fmtString, time.Now(), *url, tcpPorts.String(), udpPorts.String(), sctpPorts.String())
}
|