1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 //go:generate go run gen.go
9 // This program generates internet protocol constants and tables by
10 // reading IANA protocol registries.
26 var registries = []struct {
28 parse func(io.Writer, io.Reader) error
31 "http://www.iana.org/assignments/dscp-registry/dscp-registry.xml",
35 "http://www.iana.org/assignments/ipv4-tos-byte/ipv4-tos-byte.xml",
39 "http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml",
46 fmt.Fprintf(&bb, "// go generate gen.go\n")
47 fmt.Fprintf(&bb, "// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT\n\n")
48 fmt.Fprintf(&bb, "// Package iana provides protocol number resources managed by the Internet Assigned Numbers Authority (IANA).\n")
49 fmt.Fprintf(&bb, `package iana // import "golang.org/x/net/internal/iana"`+"\n\n")
50 for _, r := range registries {
51 resp, err := http.Get(r.url)
53 fmt.Fprintln(os.Stderr, err)
56 defer resp.Body.Close()
57 if resp.StatusCode != http.StatusOK {
58 fmt.Fprintf(os.Stderr, "got HTTP status code %v for %v\n", resp.StatusCode, r.url)
61 if err := r.parse(&bb, resp.Body); err != nil {
62 fmt.Fprintln(os.Stderr, err)
65 fmt.Fprintf(&bb, "\n")
67 b, err := format.Source(bb.Bytes())
69 fmt.Fprintln(os.Stderr, err)
72 if err := ioutil.WriteFile("const.go", b, 0644); err != nil {
73 fmt.Fprintln(os.Stderr, err)
78 func parseDSCPRegistry(w io.Writer, r io.Reader) error {
79 dec := xml.NewDecoder(r)
81 if err := dec.Decode(&dr); err != nil {
85 fmt.Fprintf(w, "// %s, Updated: %s\n", dr.Title, dr.Updated)
86 fmt.Fprintf(w, "const (\n")
87 for _, dr := range drs {
88 fmt.Fprintf(w, "DiffServ%s = %#x", dr.Name, dr.Value)
89 fmt.Fprintf(w, "// %s\n", dr.OrigName)
95 type dscpRegistry struct {
96 XMLName xml.Name `xml:"registry"`
97 Title string `xml:"title"`
98 Updated string `xml:"updated"`
99 Note string `xml:"note"`
100 RegTitle string `xml:"registry>title"`
101 PoolRecords []struct {
102 Name string `xml:"name"`
103 Space string `xml:"space"`
104 } `xml:"registry>record"`
106 Name string `xml:"name"`
107 Space string `xml:"space"`
108 } `xml:"registry>registry>record"`
111 type canonDSCPRecord struct {
117 func (drr *dscpRegistry) escape() []canonDSCPRecord {
118 drs := make([]canonDSCPRecord, len(drr.Records))
119 sr := strings.NewReplacer(
126 for i, dr := range drr.Records {
127 s := strings.TrimSpace(dr.Name)
129 drs[i].Name = sr.Replace(s)
130 n, err := strconv.ParseUint(dr.Space, 2, 8)
134 drs[i].Value = int(n) << 2
139 func parseTOSTCByte(w io.Writer, r io.Reader) error {
140 dec := xml.NewDecoder(r)
142 if err := dec.Decode(&ttb); err != nil {
146 fmt.Fprintf(w, "// %s, Updated: %s\n", ttb.Title, ttb.Updated)
147 fmt.Fprintf(w, "const (\n")
148 for _, tr := range trs {
149 fmt.Fprintf(w, "%s = %#x", tr.Keyword, tr.Value)
150 fmt.Fprintf(w, "// %s\n", tr.OrigKeyword)
152 fmt.Fprintf(w, ")\n")
156 type tosTCByte struct {
157 XMLName xml.Name `xml:"registry"`
158 Title string `xml:"title"`
159 Updated string `xml:"updated"`
160 Note string `xml:"note"`
161 RegTitle string `xml:"registry>title"`
163 Binary string `xml:"binary"`
164 Keyword string `xml:"keyword"`
165 } `xml:"registry>record"`
168 type canonTOSTCByteRecord struct {
174 func (ttb *tosTCByte) escape() []canonTOSTCByteRecord {
175 trs := make([]canonTOSTCByteRecord, len(ttb.Records))
176 sr := strings.NewReplacer(
186 for i, tr := range ttb.Records {
187 s := strings.TrimSpace(tr.Keyword)
188 trs[i].OrigKeyword = s
189 ss := strings.Split(s, " ")
191 trs[i].Keyword = strings.Join(ss[1:], " ")
193 trs[i].Keyword = ss[0]
195 trs[i].Keyword = sr.Replace(trs[i].Keyword)
196 n, err := strconv.ParseUint(tr.Binary, 2, 8)
200 trs[i].Value = int(n)
205 func parseProtocolNumbers(w io.Writer, r io.Reader) error {
206 dec := xml.NewDecoder(r)
207 var pn protocolNumbers
208 if err := dec.Decode(&pn); err != nil {
212 prs = append([]canonProtocolRecord{{
214 Descr: "IPv4 encapsulation, pseudo protocol number",
217 fmt.Fprintf(w, "// %s, Updated: %s\n", pn.Title, pn.Updated)
218 fmt.Fprintf(w, "const (\n")
219 for _, pr := range prs {
223 fmt.Fprintf(w, "Protocol%s = %d", pr.Name, pr.Value)
228 fmt.Fprintf(w, "// %s\n", s)
230 fmt.Fprintf(w, ")\n")
234 type protocolNumbers struct {
235 XMLName xml.Name `xml:"registry"`
236 Title string `xml:"title"`
237 Updated string `xml:"updated"`
238 RegTitle string `xml:"registry>title"`
239 Note string `xml:"registry>note"`
241 Value string `xml:"value"`
242 Name string `xml:"name"`
243 Descr string `xml:"description"`
244 } `xml:"registry>record"`
247 type canonProtocolRecord struct {
254 func (pn *protocolNumbers) escape() []canonProtocolRecord {
255 prs := make([]canonProtocolRecord, len(pn.Records))
256 sr := strings.NewReplacer(
258 "-within-", "within",
266 for i, pr := range pn.Records {
267 if strings.Contains(pr.Name, "Deprecated") ||
268 strings.Contains(pr.Name, "deprecated") {
271 prs[i].OrigName = pr.Name
272 s := strings.TrimSpace(pr.Name)
274 case "ISIS over IPv4":
277 prs[i].Name = "MANET"
279 prs[i].Name = sr.Replace(s)
281 ss := strings.Split(pr.Descr, "\n")
283 ss[i] = strings.TrimSpace(ss[i])
286 prs[i].Descr = strings.Join(ss, " ")
290 prs[i].Value, _ = strconv.Atoi(pr.Value)