gen.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/icmpv6-parameters.xml",
  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()
  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 []struct {
  97. Title string `xml:"title"`
  98. Records []struct {
  99. Value string `xml:"value"`
  100. Name string `xml:"name"`
  101. } `xml:"record"`
  102. } `xml:"registry"`
  103. }
  104. type canonICMPv6ParamRecord struct {
  105. OrigName string
  106. Name string
  107. Value int
  108. }
  109. func (icp *icmpv6Parameters) escape() []canonICMPv6ParamRecord {
  110. id := -1
  111. for i, r := range icp.Registries {
  112. if strings.Contains(r.Title, "Type") || strings.Contains(r.Title, "type") {
  113. id = i
  114. break
  115. }
  116. }
  117. if id < 0 {
  118. return nil
  119. }
  120. prs := make([]canonICMPv6ParamRecord, len(icp.Registries[id].Records))
  121. sr := strings.NewReplacer(
  122. "Messages", "",
  123. "Message", "",
  124. "ICMP", "",
  125. "+", "P",
  126. "-", "",
  127. "/", "",
  128. ".", "",
  129. " ", "",
  130. )
  131. for i, pr := range icp.Registries[id].Records {
  132. if strings.Contains(pr.Name, "Reserved") ||
  133. strings.Contains(pr.Name, "Unassigned") ||
  134. strings.Contains(pr.Name, "Deprecated") ||
  135. strings.Contains(pr.Name, "Experiment") ||
  136. strings.Contains(pr.Name, "experiment") {
  137. continue
  138. }
  139. ss := strings.Split(pr.Name, "\n")
  140. if len(ss) > 1 {
  141. prs[i].Name = strings.Join(ss, " ")
  142. } else {
  143. prs[i].Name = ss[0]
  144. }
  145. s := strings.TrimSpace(prs[i].Name)
  146. prs[i].OrigName = s
  147. prs[i].Name = sr.Replace(s)
  148. prs[i].Value, _ = strconv.Atoi(pr.Value)
  149. }
  150. return prs
  151. }
  152. func parseProtocolNumbers(w io.Writer, r io.Reader) error {
  153. dec := xml.NewDecoder(r)
  154. var pn protocolNumbers
  155. if err := dec.Decode(&pn); err != nil {
  156. return err
  157. }
  158. prs := pn.escape()
  159. fmt.Fprintf(w, "// %s, Updated: %s\n", pn.Title, pn.Updated)
  160. fmt.Fprintf(w, "const (\n")
  161. for _, pr := range prs {
  162. if pr.Name == "" {
  163. continue
  164. }
  165. fmt.Fprintf(w, "ianaProtocol%s = %d", pr.Name, pr.Value)
  166. s := pr.Descr
  167. if s == "" {
  168. s = pr.OrigName
  169. }
  170. fmt.Fprintf(w, "// %s\n", s)
  171. }
  172. fmt.Fprintf(w, ")\n")
  173. return nil
  174. }
  175. type protocolNumbers struct {
  176. XMLName xml.Name `xml:"registry"`
  177. Title string `xml:"title"`
  178. Updated string `xml:"updated"`
  179. RegTitle string `xml:"registry>title"`
  180. Note string `xml:"registry>note"`
  181. Records []struct {
  182. Value string `xml:"value"`
  183. Name string `xml:"name"`
  184. Descr string `xml:"description"`
  185. } `xml:"registry>record"`
  186. }
  187. type canonProtocolRecord struct {
  188. OrigName string
  189. Name string
  190. Descr string
  191. Value int
  192. }
  193. func (pn *protocolNumbers) escape() []canonProtocolRecord {
  194. prs := make([]canonProtocolRecord, len(pn.Records))
  195. sr := strings.NewReplacer(
  196. "-in-", "in",
  197. "-within-", "within",
  198. "-over-", "over",
  199. "+", "P",
  200. "-", "",
  201. "/", "",
  202. ".", "",
  203. " ", "",
  204. )
  205. for i, pr := range pn.Records {
  206. prs[i].OrigName = pr.Name
  207. s := strings.TrimSpace(pr.Name)
  208. switch pr.Name {
  209. case "ISIS over IPv4":
  210. prs[i].Name = "ISIS"
  211. case "manet":
  212. prs[i].Name = "MANET"
  213. default:
  214. prs[i].Name = sr.Replace(s)
  215. }
  216. ss := strings.Split(pr.Descr, "\n")
  217. for i := range ss {
  218. ss[i] = strings.TrimSpace(ss[i])
  219. }
  220. if len(ss) > 1 {
  221. prs[i].Descr = strings.Join(ss, " ")
  222. } else {
  223. prs[i].Descr = ss[0]
  224. }
  225. prs[i].Value, _ = strconv.Atoi(pr.Value)
  226. }
  227. return prs
  228. }