Commit 5f6a5012 authored by Mikio Hara's avatar Mikio Hara

x/net/ipv6: make use of go generate to create system adaptation files

LGTM=iant
R=iant
CC=golang-codereviews
https://golang.org/cl/171970043
parent b1772d92
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
// +build ignore // +build ignore
// This program generates internet protocol constants and tables by //go:generate go run gen.go
// reading IANA protocol registries.
// // This program generates system adaptation constants and types,
// Usage: // internet protocol constants and tables by reading template files
// go run gen.go > iana.go // and IANA protocol registries.
package main package main
import ( import (
...@@ -17,12 +17,58 @@ import ( ...@@ -17,12 +17,58 @@ import (
"fmt" "fmt"
"go/format" "go/format"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec"
"runtime"
"strconv" "strconv"
"strings" "strings"
) )
func main() {
if err := genzsys(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := geniana(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func genzsys() error {
defs := "defs_" + runtime.GOOS + ".go"
f, err := os.Open(defs)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
f.Close()
cmd := exec.Command("go", "tool", "cgo", "-godefs", defs)
b, err := cmd.Output()
if err != nil {
return err
}
switch runtime.GOOS {
case "dragonfly", "solaris":
// The ipv6 pacakge still supports go1.2, and so we
// need to take care of additional platforms in go1.3
// and above for working with go1.2.
b = bytes.Replace(b, []byte("package ipv6\n"), []byte("// +build "+runtime.GOOS+"\n\npackage ipv6\n"), 1)
}
b, err = format.Source(b)
if err != nil {
return err
}
if err := ioutil.WriteFile("zsys_"+runtime.GOOS+".go", b, 0644); err != nil {
return err
}
return nil
}
var registries = []struct { var registries = []struct {
url string url string
parse func(io.Writer, io.Reader) error parse func(io.Writer, io.Reader) error
...@@ -31,40 +77,35 @@ var registries = []struct { ...@@ -31,40 +77,35 @@ var registries = []struct {
"http://www.iana.org/assignments/icmpv6-parameters/icmpv6-parameters.xml", "http://www.iana.org/assignments/icmpv6-parameters/icmpv6-parameters.xml",
parseICMPv6Parameters, parseICMPv6Parameters,
}, },
{
"http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml",
parseProtocolNumbers,
},
} }
func main() { func geniana() error {
var bb bytes.Buffer var bb bytes.Buffer
fmt.Fprintf(&bb, "// go run gen.go\n") fmt.Fprintf(&bb, "// go generate gen.go\n")
fmt.Fprintf(&bb, "// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT\n\n") fmt.Fprintf(&bb, "// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT\n\n")
fmt.Fprintf(&bb, "package ipv6\n\n") fmt.Fprintf(&bb, "package ipv6\n\n")
for _, r := range registries { for _, r := range registries {
resp, err := http.Get(r.url) resp, err := http.Get(r.url)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) return err
os.Exit(1)
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "got HTTP status code %v for %v\n", resp.StatusCode, r.url) return fmt.Errorf("got HTTP status code %v for %v\n", resp.StatusCode, r.url)
os.Exit(1)
} }
if err := r.parse(&bb, resp.Body); err != nil { if err := r.parse(&bb, resp.Body); err != nil {
fmt.Fprintln(os.Stderr, err) return err
os.Exit(1)
} }
fmt.Fprintf(&bb, "\n") fmt.Fprintf(&bb, "\n")
} }
b, err := format.Source(bb.Bytes()) b, err := format.Source(bb.Bytes())
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) return err
os.Exit(1)
} }
os.Stdout.Write(b) if err := ioutil.WriteFile("iana.go", b, 0644); err != nil {
return err
}
return nil
} }
func parseICMPv6Parameters(w io.Writer, r io.Reader) error { func parseICMPv6Parameters(w io.Writer, r io.Reader) error {
...@@ -158,84 +199,3 @@ func (icp *icmpv6Parameters) escape() []canonICMPv6ParamRecord { ...@@ -158,84 +199,3 @@ func (icp *icmpv6Parameters) escape() []canonICMPv6ParamRecord {
} }
return prs return prs
} }
func parseProtocolNumbers(w io.Writer, r io.Reader) error {
dec := xml.NewDecoder(r)
var pn protocolNumbers
if err := dec.Decode(&pn); err != nil {
return err
}
prs := pn.escape()
fmt.Fprintf(w, "// %s, Updated: %s\n", pn.Title, pn.Updated)
fmt.Fprintf(w, "const (\n")
for _, pr := range prs {
if pr.Name == "" {
continue
}
fmt.Fprintf(w, "ianaProtocol%s = %d", pr.Name, pr.Value)
s := pr.Descr
if s == "" {
s = pr.OrigName
}
fmt.Fprintf(w, "// %s\n", s)
}
fmt.Fprintf(w, ")\n")
return nil
}
type protocolNumbers struct {
XMLName xml.Name `xml:"registry"`
Title string `xml:"title"`
Updated string `xml:"updated"`
RegTitle string `xml:"registry>title"`
Note string `xml:"registry>note"`
Records []struct {
Value string `xml:"value"`
Name string `xml:"name"`
Descr string `xml:"description"`
} `xml:"registry>record"`
}
type canonProtocolRecord struct {
OrigName string
Name string
Descr string
Value int
}
func (pn *protocolNumbers) escape() []canonProtocolRecord {
prs := make([]canonProtocolRecord, len(pn.Records))
sr := strings.NewReplacer(
"-in-", "in",
"-within-", "within",
"-over-", "over",
"+", "P",
"-", "",
"/", "",
".", "",
" ", "",
)
for i, pr := range pn.Records {
prs[i].OrigName = pr.Name
s := strings.TrimSpace(pr.Name)
switch pr.Name {
case "ISIS over IPv4":
prs[i].Name = "ISIS"
case "manet":
prs[i].Name = "MANET"
default:
prs[i].Name = sr.Replace(s)
}
ss := strings.Split(pr.Descr, "\n")
for i := range ss {
ss[i] = strings.TrimSpace(ss[i])
}
if len(ss) > 1 {
prs[i].Descr = strings.Join(ss, " ")
} else {
prs[i].Descr = ss[0]
}
prs[i].Value, _ = strconv.Atoi(pr.Value)
}
return prs
}
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
// This program generates internet protocol constants by reading IANA
// protocol registries.
//
// Usage:
// go run gentest.go > iana_test.go
package main
import (
"bytes"
"encoding/xml"
"fmt"
"go/format"
"io"
"net/http"
"os"
"strconv"
"strings"
)
var registries = []struct {
url string
parse func(io.Writer, io.Reader) error
}{
{
"http://www.iana.org/assignments/dscp-registry/dscp-registry.xml",
parseDSCPRegistry,
},
{
"http://www.iana.org/assignments/ipv4-tos-byte/ipv4-tos-byte.xml",
parseTOSTCByte,
},
}
func main() {
var bb bytes.Buffer
fmt.Fprintf(&bb, "// go run gentest.go\n")
fmt.Fprintf(&bb, "// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT\n\n")
fmt.Fprintf(&bb, "package ipv6_test\n\n")
for _, r := range registries {
resp, err := http.Get(r.url)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "got HTTP status code %v for %v\n", resp.StatusCode, r.url)
os.Exit(1)
}
if err := r.parse(&bb, resp.Body); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Fprintf(&bb, "\n")
}
b, err := format.Source(bb.Bytes())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Stdout.Write(b)
}
func parseDSCPRegistry(w io.Writer, r io.Reader) error {
dec := xml.NewDecoder(r)
var dr dscpRegistry
if err := dec.Decode(&dr); err != nil {
return err
}
drs := dr.escape()
fmt.Fprintf(w, "// %s, Updated: %s\n", dr.Title, dr.Updated)
fmt.Fprintf(w, "const (\n")
for _, dr := range drs {
fmt.Fprintf(w, "DiffServ%s = %#x", dr.Name, dr.Value)
fmt.Fprintf(w, "// %s\n", dr.OrigName)
}
fmt.Fprintf(w, ")\n")
return nil
}
type dscpRegistry struct {
XMLName xml.Name `xml:"registry"`
Title string `xml:"title"`
Updated string `xml:"updated"`
Note string `xml:"note"`
RegTitle string `xml:"registry>title"`
PoolRecords []struct {
Name string `xml:"name"`
Space string `xml:"space"`
} `xml:"registry>record"`
Records []struct {
Name string `xml:"name"`
Space string `xml:"space"`
} `xml:"registry>registry>record"`
}
type canonDSCPRecord struct {
OrigName string
Name string
Value int
}
func (drr *dscpRegistry) escape() []canonDSCPRecord {
drs := make([]canonDSCPRecord, len(drr.Records))
sr := strings.NewReplacer(
"+", "",
"-", "",
"/", "",
".", "",
" ", "",
)
for i, dr := range drr.Records {
s := strings.TrimSpace(dr.Name)
drs[i].OrigName = s
drs[i].Name = sr.Replace(s)
n, err := strconv.ParseUint(dr.Space, 2, 8)
if err != nil {
continue
}
drs[i].Value = int(n) << 2
}
return drs
}
func parseTOSTCByte(w io.Writer, r io.Reader) error {
dec := xml.NewDecoder(r)
var ttb tosTCByte
if err := dec.Decode(&ttb); err != nil {
return err
}
trs := ttb.escape()
fmt.Fprintf(w, "// %s, Updated: %s\n", ttb.Title, ttb.Updated)
fmt.Fprintf(w, "const (\n")
for _, tr := range trs {
fmt.Fprintf(w, "%s = %#x", tr.Keyword, tr.Value)
fmt.Fprintf(w, "// %s\n", tr.OrigKeyword)
}
fmt.Fprintf(w, ")\n")
return nil
}
type tosTCByte struct {
XMLName xml.Name `xml:"registry"`
Title string `xml:"title"`
Updated string `xml:"updated"`
Note string `xml:"note"`
RegTitle string `xml:"registry>title"`
Records []struct {
Binary string `xml:"binary"`
Keyword string `xml:"keyword"`
} `xml:"registry>record"`
}
type canonTOSTCByteRecord struct {
OrigKeyword string
Keyword string
Value int
}
func (ttb *tosTCByte) escape() []canonTOSTCByteRecord {
trs := make([]canonTOSTCByteRecord, len(ttb.Records))
sr := strings.NewReplacer(
"Capable", "",
"(", "",
")", "",
"+", "",
"-", "",
"/", "",
".", "",
" ", "",
)
for i, tr := range ttb.Records {
s := strings.TrimSpace(tr.Keyword)
trs[i].OrigKeyword = s
ss := strings.Split(s, " ")
if len(ss) > 1 {
trs[i].Keyword = strings.Join(ss[1:], " ")
} else {
trs[i].Keyword = ss[0]
}
trs[i].Keyword = sr.Replace(trs[i].Keyword)
n, err := strconv.ParseUint(tr.Binary, 2, 8)
if err != nil {
continue
}
trs[i].Value = int(n)
}
return trs
}
// go run gen.go // go generate gen.go
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
package ipv6 package ipv6
// Internet Control Message Protocol version 6 (ICMPv6) Parameters, Updated: 2013-07-03 // Internet Control Message Protocol version 6 (ICMPv6) Parameters, Updated: 2014-09-22
const ( const (
ICMPTypeDestinationUnreachable ICMPType = 1 // Destination Unreachable ICMPTypeDestinationUnreachable ICMPType = 1 // Destination Unreachable
ICMPTypePacketTooBig ICMPType = 2 // Packet Too Big ICMPTypePacketTooBig ICMPType = 2 // Packet Too Big
...@@ -41,7 +41,7 @@ const ( ...@@ -41,7 +41,7 @@ const (
ICMPTypeDuplicateAddressConfirmation ICMPType = 158 // Duplicate Address Confirmation ICMPTypeDuplicateAddressConfirmation ICMPType = 158 // Duplicate Address Confirmation
) )
// Internet Control Message Protocol version 6 (ICMPv6) Parameters, Updated: 2013-07-03 // Internet Control Message Protocol version 6 (ICMPv6) Parameters, Updated: 2014-09-22
var icmpTypes = map[ICMPType]string{ var icmpTypes = map[ICMPType]string{
1: "destination unreachable", 1: "destination unreachable",
2: "packet too big", 2: "packet too big",
...@@ -78,147 +78,3 @@ var icmpTypes = map[ICMPType]string{ ...@@ -78,147 +78,3 @@ var icmpTypes = map[ICMPType]string{
157: "duplicate address request", 157: "duplicate address request",
158: "duplicate address confirmation", 158: "duplicate address confirmation",
} }
// Protocol Numbers, Updated: 2013-02-17
const (
ianaProtocolHOPOPT = 0 // IPv6 Hop-by-Hop Option
ianaProtocolICMP = 1 // Internet Control Message
ianaProtocolIGMP = 2 // Internet Group Management
ianaProtocolGGP = 3 // Gateway-to-Gateway
ianaProtocolIPv4 = 4 // IPv4 encapsulation
ianaProtocolST = 5 // Stream
ianaProtocolTCP = 6 // Transmission Control
ianaProtocolCBT = 7 // CBT
ianaProtocolEGP = 8 // Exterior Gateway Protocol
ianaProtocolIGP = 9 // any private interior gateway (used by Cisco for their IGRP)
ianaProtocolBBNRCCMON = 10 // BBN RCC Monitoring
ianaProtocolNVPII = 11 // Network Voice Protocol
ianaProtocolPUP = 12 // PUP
ianaProtocolARGUS = 13 // ARGUS
ianaProtocolEMCON = 14 // EMCON
ianaProtocolXNET = 15 // Cross Net Debugger
ianaProtocolCHAOS = 16 // Chaos
ianaProtocolUDP = 17 // User Datagram
ianaProtocolMUX = 18 // Multiplexing
ianaProtocolDCNMEAS = 19 // DCN Measurement Subsystems
ianaProtocolHMP = 20 // Host Monitoring
ianaProtocolPRM = 21 // Packet Radio Measurement
ianaProtocolXNSIDP = 22 // XEROX NS IDP
ianaProtocolTRUNK1 = 23 // Trunk-1
ianaProtocolTRUNK2 = 24 // Trunk-2
ianaProtocolLEAF1 = 25 // Leaf-1
ianaProtocolLEAF2 = 26 // Leaf-2
ianaProtocolRDP = 27 // Reliable Data Protocol
ianaProtocolIRTP = 28 // Internet Reliable Transaction
ianaProtocolISOTP4 = 29 // ISO Transport Protocol Class 4
ianaProtocolNETBLT = 30 // Bulk Data Transfer Protocol
ianaProtocolMFENSP = 31 // MFE Network Services Protocol
ianaProtocolMERITINP = 32 // MERIT Internodal Protocol
ianaProtocolDCCP = 33 // Datagram Congestion Control Protocol
ianaProtocol3PC = 34 // Third Party Connect Protocol
ianaProtocolIDPR = 35 // Inter-Domain Policy Routing Protocol
ianaProtocolXTP = 36 // XTP
ianaProtocolDDP = 37 // Datagram Delivery Protocol
ianaProtocolIDPRCMTP = 38 // IDPR Control Message Transport Proto
ianaProtocolTPPP = 39 // TP++ Transport Protocol
ianaProtocolIL = 40 // IL Transport Protocol
ianaProtocolIPv6 = 41 // IPv6 encapsulation
ianaProtocolSDRP = 42 // Source Demand Routing Protocol
ianaProtocolIPv6Route = 43 // Routing Header for IPv6
ianaProtocolIPv6Frag = 44 // Fragment Header for IPv6
ianaProtocolIDRP = 45 // Inter-Domain Routing Protocol
ianaProtocolRSVP = 46 // Reservation Protocol
ianaProtocolGRE = 47 // Generic Routing Encapsulation
ianaProtocolDSR = 48 // Dynamic Source Routing Protocol
ianaProtocolBNA = 49 // BNA
ianaProtocolESP = 50 // Encap Security Payload
ianaProtocolAH = 51 // Authentication Header
ianaProtocolINLSP = 52 // Integrated Net Layer Security TUBA
ianaProtocolSWIPE = 53 // IP with Encryption
ianaProtocolNARP = 54 // NBMA Address Resolution Protocol
ianaProtocolMOBILE = 55 // IP Mobility
ianaProtocolTLSP = 56 // Transport Layer Security Protocol using Kryptonet key management
ianaProtocolSKIP = 57 // SKIP
ianaProtocolIPv6ICMP = 58 // ICMP for IPv6
ianaProtocolIPv6NoNxt = 59 // No Next Header for IPv6
ianaProtocolIPv6Opts = 60 // Destination Options for IPv6
ianaProtocolCFTP = 62 // CFTP
ianaProtocolSATEXPAK = 64 // SATNET and Backroom EXPAK
ianaProtocolKRYPTOLAN = 65 // Kryptolan
ianaProtocolRVD = 66 // MIT Remote Virtual Disk Protocol
ianaProtocolIPPC = 67 // Internet Pluribus Packet Core
ianaProtocolSATMON = 69 // SATNET Monitoring
ianaProtocolVISA = 70 // VISA Protocol
ianaProtocolIPCV = 71 // Internet Packet Core Utility
ianaProtocolCPNX = 72 // Computer Protocol Network Executive
ianaProtocolCPHB = 73 // Computer Protocol Heart Beat
ianaProtocolWSN = 74 // Wang Span Network
ianaProtocolPVP = 75 // Packet Video Protocol
ianaProtocolBRSATMON = 76 // Backroom SATNET Monitoring
ianaProtocolSUNND = 77 // SUN ND PROTOCOL-Temporary
ianaProtocolWBMON = 78 // WIDEBAND Monitoring
ianaProtocolWBEXPAK = 79 // WIDEBAND EXPAK
ianaProtocolISOIP = 80 // ISO Internet Protocol
ianaProtocolVMTP = 81 // VMTP
ianaProtocolSECUREVMTP = 82 // SECURE-VMTP
ianaProtocolVINES = 83 // VINES
ianaProtocolTTP = 84 // TTP
ianaProtocolIPTM = 84 // Protocol Internet Protocol Traffic Manager
ianaProtocolNSFNETIGP = 85 // NSFNET-IGP
ianaProtocolDGP = 86 // Dissimilar Gateway Protocol
ianaProtocolTCF = 87 // TCF
ianaProtocolEIGRP = 88 // EIGRP
ianaProtocolOSPFIGP = 89 // OSPFIGP
ianaProtocolSpriteRPC = 90 // Sprite RPC Protocol
ianaProtocolLARP = 91 // Locus Address Resolution Protocol
ianaProtocolMTP = 92 // Multicast Transport Protocol
ianaProtocolAX25 = 93 // AX.25 Frames
ianaProtocolIPIP = 94 // IP-within-IP Encapsulation Protocol
ianaProtocolMICP = 95 // Mobile Internetworking Control Pro.
ianaProtocolSCCSP = 96 // Semaphore Communications Sec. Pro.
ianaProtocolETHERIP = 97 // Ethernet-within-IP Encapsulation
ianaProtocolENCAP = 98 // Encapsulation Header
ianaProtocolGMTP = 100 // GMTP
ianaProtocolIFMP = 101 // Ipsilon Flow Management Protocol
ianaProtocolPNNI = 102 // PNNI over IP
ianaProtocolPIM = 103 // Protocol Independent Multicast
ianaProtocolARIS = 104 // ARIS
ianaProtocolSCPS = 105 // SCPS
ianaProtocolQNX = 106 // QNX
ianaProtocolAN = 107 // Active Networks
ianaProtocolIPComp = 108 // IP Payload Compression Protocol
ianaProtocolSNP = 109 // Sitara Networks Protocol
ianaProtocolCompaqPeer = 110 // Compaq Peer Protocol
ianaProtocolIPXinIP = 111 // IPX in IP
ianaProtocolVRRP = 112 // Virtual Router Redundancy Protocol
ianaProtocolPGM = 113 // PGM Reliable Transport Protocol
ianaProtocolL2TP = 115 // Layer Two Tunneling Protocol
ianaProtocolDDX = 116 // D-II Data Exchange (DDX)
ianaProtocolIATP = 117 // Interactive Agent Transfer Protocol
ianaProtocolSTP = 118 // Schedule Transfer Protocol
ianaProtocolSRP = 119 // SpectraLink Radio Protocol
ianaProtocolUTI = 120 // UTI
ianaProtocolSMP = 121 // Simple Message Protocol
ianaProtocolSM = 122 // SM
ianaProtocolPTP = 123 // Performance Transparency Protocol
ianaProtocolISIS = 124 // ISIS over IPv4
ianaProtocolFIRE = 125 // FIRE
ianaProtocolCRTP = 126 // Combat Radio Transport Protocol
ianaProtocolCRUDP = 127 // Combat Radio User Datagram
ianaProtocolSSCOPMCE = 128 // SSCOPMCE
ianaProtocolIPLT = 129 // IPLT
ianaProtocolSPS = 130 // Secure Packet Shield
ianaProtocolPIPE = 131 // Private IP Encapsulation within IP
ianaProtocolSCTP = 132 // Stream Control Transmission Protocol
ianaProtocolFC = 133 // Fibre Channel
ianaProtocolRSVPE2EIGNORE = 134 // RSVP-E2E-IGNORE
ianaProtocolMobilityHeader = 135 // Mobility Header
ianaProtocolUDPLite = 136 // UDPLite
ianaProtocolMPLSinIP = 137 // MPLS-in-IP
ianaProtocolMANET = 138 // MANET Protocols
ianaProtocolHIP = 139 // Host Identity Protocol
ianaProtocolShim6 = 140 // Shim6 Protocol
ianaProtocolWESP = 141 // Wrapped Encapsulating Security Payload
ianaProtocolROHC = 142 // Robust Header Compression
ianaProtocolReserved = 255 // Reserved
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment