gen.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.
  4. // +build ignore
  5. // This program generates internet protocol constatns and tables by
  6. // reading IANA protocol registries.
  7. //
  8. // Usage:
  9. // go run gen.go > iana.go
  10. package main
  11. import (
  12. "bytes"
  13. "encoding/xml"
  14. "fmt"
  15. "go/format"
  16. "io"
  17. "net/http"
  18. "os"
  19. "strconv"
  20. "strings"
  21. )
  22. var registries = []struct {
  23. url string
  24. parse func(io.Writer, io.Reader) error
  25. }{
  26. {
  27. "http://www.iana.org/assignments/icmpv6-parameters",
  28. parseICMPv6Parameters,
  29. },
  30. {
  31. "http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml",
  32. parseProtocolNumbers,
  33. },
  34. }
  35. func main() {
  36. var bb bytes.Buffer
  37. fmt.Fprintf(&bb, "// go run gen.go\n")
  38. fmt.Fprintf(&bb, "// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT\n\n")
  39. fmt.Fprintf(&bb, "package ipv6\n\n")
  40. for _, r := range registries {
  41. resp, err := http.Get(r.url)
  42. if err != nil {
  43. fmt.Fprintln(os.Stderr, err)
  44. os.Exit(1)
  45. }
  46. defer resp.Body.Close()
  47. if resp.StatusCode != http.StatusOK {
  48. fmt.Fprintf(os.Stderr, "got HTTP status code %v for %v\n", resp.StatusCode, r.url)
  49. os.Exit(1)
  50. }
  51. if err := r.parse(&bb, resp.Body); err != nil {
  52. fmt.Fprintln(os.Stderr, err)
  53. os.Exit(1)
  54. }
  55. fmt.Fprintf(&bb, "\n")
  56. }
  57. b, err := format.Source(bb.Bytes())
  58. if err != nil {
  59. fmt.Fprintln(os.Stderr, err)
  60. os.Exit(1)
  61. }
  62. os.Stdout.Write(b)
  63. }
  64. func parseICMPv6Parameters(w io.Writer, r io.Reader) error {
  65. dec := xml.NewDecoder(r)
  66. var icp icmpv6Parameters
  67. if err := dec.Decode(&icp); err != nil {
  68. return err
  69. }
  70. prs := icp.escape(1)
  71. fmt.Fprintf(w, "// %s, Updated: %s\n", icp.Title, icp.Updated)
  72. fmt.Fprintf(w, "const (\n")
  73. for _, pr := range prs {
  74. if pr.Name == "" {
  75. continue
  76. }
  77. fmt.Fprintf(w, "ICMPType%s ICMPType = %d", pr.Name, pr.Value)
  78. fmt.Fprintf(w, "// %s\n", pr.OrigName)
  79. }
  80. fmt.Fprintf(w, ")\n\n")
  81. fmt.Fprintf(w, "// %s, Updated: %s\n", icp.Title, icp.Updated)
  82. fmt.Fprintf(w, "var icmpTypes = map[ICMPType]string{\n")
  83. for _, pr := range prs {
  84. if pr.Name == "" {
  85. continue
  86. }
  87. fmt.Fprintf(w, "%d: %q,\n", pr.Value, strings.ToLower(pr.OrigName))
  88. }
  89. fmt.Fprintf(w, "}\n")
  90. return nil
  91. }
  92. type icmpv6Parameters struct {
  93. XMLName xml.Name `xml:"registry"`
  94. Title string `xml:"title"`
  95. Updated string `xml:"updated"`
  96. Registries []icmpv6ParamRegistry `xml:"registry"`
  97. }
  98. type icmpv6ParamRegistry struct {
  99. Title string `xml:"title"`
  100. Records []icmpv6ParamRecord `xml:"record"`
  101. }
  102. type icmpv6ParamRecord struct {
  103. Value string `xml:"value"`
  104. Name string `xml:"name"`
  105. }
  106. type canonICMPv6ParamRecord struct {
  107. OrigName string
  108. Name string
  109. Value int
  110. }
  111. func (icp *icmpv6Parameters) escape(id int) []canonICMPv6ParamRecord {
  112. prs := make([]canonICMPv6ParamRecord, len(icp.Registries[id].Records))
  113. sr := strings.NewReplacer(
  114. "Messages", "",
  115. "Message", "",
  116. "ICMP", "",
  117. "+", "P",
  118. "-", "",
  119. "/", "",
  120. ".", "",
  121. " ", "",
  122. )
  123. for i, pr := range icp.Registries[id].Records {
  124. if strings.Contains(pr.Name, "Reserved") ||
  125. strings.Contains(pr.Name, "Unassigned") ||
  126. strings.Contains(pr.Name, "Deprecated") ||
  127. strings.Contains(pr.Name, "Experiment") ||
  128. strings.Contains(pr.Name, "experiment") {
  129. continue
  130. }
  131. ss := strings.Split(pr.Name, "\n")
  132. if len(ss) > 1 {
  133. prs[i].Name = strings.Join(ss, " ")
  134. } else {
  135. prs[i].Name = ss[0]
  136. }
  137. s := strings.TrimSpace(prs[i].Name)
  138. prs[i].OrigName = s
  139. prs[i].Name = sr.Replace(s)
  140. prs[i].Value, _ = strconv.Atoi(pr.Value)
  141. }
  142. return prs
  143. }
  144. func parseProtocolNumbers(w io.Writer, r io.Reader) error {
  145. dec := xml.NewDecoder(r)
  146. var pn protocolNumbers
  147. if err := dec.Decode(&pn); err != nil {
  148. return err
  149. }
  150. prs := pn.escape()
  151. fmt.Fprintf(w, "// %s, Updated: %s\n", pn.Title, pn.Updated)
  152. fmt.Fprintf(w, "const (\n")
  153. for _, pr := range prs {
  154. if pr.Name == "" {
  155. continue
  156. }
  157. fmt.Fprintf(w, "ianaProtocol%s = %d", pr.Name, pr.Value)
  158. s := pr.Descr
  159. if s == "" {
  160. s = pr.OrigName
  161. }
  162. fmt.Fprintf(w, "// %s\n", s)
  163. }
  164. fmt.Fprintf(w, ")\n")
  165. return nil
  166. }
  167. type protocolNumbers struct {
  168. XMLName xml.Name `xml:"registry"`
  169. Title string `xml:"title"`
  170. Updated string `xml:"updated"`
  171. RegTitle string `xml:"registry>title"`
  172. Note string `xml:"registry>note"`
  173. Records []protocolRecord `xml:"registry>record"`
  174. }
  175. type protocolRecord struct {
  176. Value string `xml:"value"`
  177. Name string `xml:"name"`
  178. Descr string `xml:"description"`
  179. }
  180. type canonProtocolRecord struct {
  181. OrigName string
  182. Name string
  183. Descr string
  184. Value int
  185. }
  186. func (pn *protocolNumbers) escape() []canonProtocolRecord {
  187. prs := make([]canonProtocolRecord, len(pn.Records))
  188. sr := strings.NewReplacer(
  189. "-in-", "in",
  190. "-within-", "within",
  191. "-over-", "over",
  192. "+", "P",
  193. "-", "",
  194. "/", "",
  195. ".", "",
  196. " ", "",
  197. )
  198. for i, pr := range pn.Records {
  199. prs[i].OrigName = pr.Name
  200. s := strings.TrimSpace(pr.Name)
  201. switch pr.Name {
  202. case "ISIS over IPv4":
  203. prs[i].Name = "ISIS"
  204. case "manet":
  205. prs[i].Name = "MANET"
  206. default:
  207. prs[i].Name = sr.Replace(s)
  208. }
  209. ss := strings.Split(pr.Descr, "\n")
  210. for i := range ss {
  211. ss[i] = strings.TrimSpace(ss[i])
  212. }
  213. if len(ss) > 1 {
  214. prs[i].Descr = strings.Join(ss, " ")
  215. } else {
  216. prs[i].Descr = ss[0]
  217. }
  218. prs[i].Value, _ = strconv.Atoi(pr.Value)
  219. }
  220. return prs
  221. }