gen.go 5.7 KB

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