gen.go 8.66 KB
Newer Older
zhangweiwei's avatar
init  
zhangweiwei committed
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
// 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

//go:generate go run gen.go

// This program generates internet protocol constants and tables by
// reading IANA protocol registries.
package main

import (
	"bytes"
	"encoding/xml"
	"fmt"
	"go/format"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"strconv"
	"strings"
)

var registries = []struct {
	url   string
	parse func(io.Writer, io.Reader) error
}{
	{
		"https://www.iana.org/assignments/dscp-registry/dscp-registry.xml",
		parseDSCPRegistry,
	},
	{
		"https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml",
		parseProtocolNumbers,
	},
	{
		"https://www.iana.org/assignments/address-family-numbers/address-family-numbers.xml",
		parseAddrFamilyNumbers,
	},
}

func main() {
	var bb bytes.Buffer
	fmt.Fprintf(&bb, "// go generate gen.go\n")
	fmt.Fprintf(&bb, "// Code generated by the command above; DO NOT EDIT.\n\n")
	fmt.Fprintf(&bb, "// Package iana provides protocol number resources managed by the Internet Assigned Numbers Authority (IANA).\n")
	fmt.Fprintf(&bb, `package iana // import "golang.org/x/net/internal/iana"`+"\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)
	}
	if err := ioutil.WriteFile("const.go", b, 0644); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

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
	}
	fmt.Fprintf(w, "// %s, Updated: %s\n", dr.Title, dr.Updated)
	fmt.Fprintf(w, "const (\n")
	for _, dr := range dr.escapeDSCP() {
		fmt.Fprintf(w, "DiffServ%s = %#02x", dr.Name, dr.Value)
		fmt.Fprintf(w, "// %s\n", dr.OrigName)
	}
	for _, er := range dr.escapeECN() {
		fmt.Fprintf(w, "%s = %#02x", er.Descr, er.Value)
		fmt.Fprintf(w, "// %s\n", er.OrigDescr)
	}
	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"`
	Registries []struct {
		Title      string `xml:"title"`
		Registries []struct {
			Title   string `xml:"title"`
			Records []struct {
				Name  string `xml:"name"`
				Space string `xml:"space"`
			} `xml:"record"`
		} `xml:"registry"`
		Records []struct {
			Value string `xml:"value"`
			Descr string `xml:"description"`
		} `xml:"record"`
	} `xml:"registry"`
}

type canonDSCPRecord struct {
	OrigName string
	Name     string
	Value    int
}

func (drr *dscpRegistry) escapeDSCP() []canonDSCPRecord {
	var drs []canonDSCPRecord
	for _, preg := range drr.Registries {
		if !strings.Contains(preg.Title, "Differentiated Services Field Codepoints") {
			continue
		}
		for _, reg := range preg.Registries {
			if !strings.Contains(reg.Title, "Pool 1 Codepoints") {
				continue
			}
			drs = make([]canonDSCPRecord, len(reg.Records))
			sr := strings.NewReplacer(
				"+", "",
				"-", "",
				"/", "",
				".", "",
				" ", "",
			)
			for i, dr := range reg.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
}

type canonECNRecord struct {
	OrigDescr string
	Descr     string
	Value     int
}

func (drr *dscpRegistry) escapeECN() []canonECNRecord {
	var ers []canonECNRecord
	for _, reg := range drr.Registries {
		if !strings.Contains(reg.Title, "ECN Field") {
			continue
		}
		ers = make([]canonECNRecord, len(reg.Records))
		sr := strings.NewReplacer(
			"Capable", "",
			"Not-ECT", "",
			"ECT(1)", "",
			"ECT(0)", "",
			"CE", "",
			"(", "",
			")", "",
			"+", "",
			"-", "",
			"/", "",
			".", "",
			" ", "",
		)
		for i, er := range reg.Records {
			s := strings.TrimSpace(er.Descr)
			ers[i].OrigDescr = s
			ss := strings.Split(s, " ")
			if len(ss) > 1 {
				ers[i].Descr = strings.Join(ss[1:], " ")
			} else {
				ers[i].Descr = ss[0]
			}
			ers[i].Descr = sr.Replace(er.Descr)
			n, err := strconv.ParseUint(er.Value, 2, 8)
			if err != nil {
				continue
			}
			ers[i].Value = int(n)
		}
	}
	return ers
}

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()
	prs = append([]canonProtocolRecord{{
		Name:  "IP",
		Descr: "IPv4 encapsulation, pseudo protocol number",
		Value: 0,
	}}, prs...)
	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, "Protocol%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 {
		if strings.Contains(pr.Name, "Deprecated") ||
			strings.Contains(pr.Name, "deprecated") {
			continue
		}
		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
}

func parseAddrFamilyNumbers(w io.Writer, r io.Reader) error {
	dec := xml.NewDecoder(r)
	var afn addrFamilylNumbers
	if err := dec.Decode(&afn); err != nil {
		return err
	}
	afrs := afn.escape()
	fmt.Fprintf(w, "// %s, Updated: %s\n", afn.Title, afn.Updated)
	fmt.Fprintf(w, "const (\n")
	for _, afr := range afrs {
		if afr.Name == "" {
			continue
		}
		fmt.Fprintf(w, "AddrFamily%s = %d", afr.Name, afr.Value)
		fmt.Fprintf(w, "// %s\n", afr.Descr)
	}
	fmt.Fprintf(w, ")\n")
	return nil
}

type addrFamilylNumbers 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"`
		Descr string `xml:"description"`
	} `xml:"registry>record"`
}

type canonAddrFamilyRecord struct {
	Name  string
	Descr string
	Value int
}

func (afn *addrFamilylNumbers) escape() []canonAddrFamilyRecord {
	afrs := make([]canonAddrFamilyRecord, len(afn.Records))
	sr := strings.NewReplacer(
		"IP version 4", "IPv4",
		"IP version 6", "IPv6",
		"Identifier", "ID",
		"-", "",
		"-", "",
		"/", "",
		".", "",
		" ", "",
	)
	for i, afr := range afn.Records {
		if strings.Contains(afr.Descr, "Unassigned") ||
			strings.Contains(afr.Descr, "Reserved") {
			continue
		}
		afrs[i].Descr = afr.Descr
		s := strings.TrimSpace(afr.Descr)
		switch s {
		case "IP (IP version 4)":
			afrs[i].Name = "IPv4"
		case "IP6 (IP version 6)":
			afrs[i].Name = "IPv6"
		case "AFI for L2VPN information":
			afrs[i].Name = "L2VPN"
		case "E.164 with NSAP format subaddress":
			afrs[i].Name = "E164withSubaddress"
		case "MT IP: Multi-Topology IP version 4":
			afrs[i].Name = "MTIPv4"
		case "MAC/24":
			afrs[i].Name = "MACFinal24bits"
		case "MAC/40":
			afrs[i].Name = "MACFinal40bits"
		case "IPv6/64":
			afrs[i].Name = "IPv6Initial64bits"
		default:
			n := strings.Index(s, "(")
			if n > 0 {
				s = s[:n]
			}
			n = strings.Index(s, ":")
			if n > 0 {
				s = s[:n]
			}
			afrs[i].Name = sr.Replace(s)
		}
		afrs[i].Value, _ = strconv.Atoi(afr.Value)
	}
	return afrs
}