gen.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 []struct {
  97. Title string `xml:"title"`
  98. Records []struct {
  99. Value string `xml:"value"`
  100. Descr string `xml:"description"`
  101. } `xml:"record"`
  102. } `xml:"registry"`
  103. }
  104. type canonICMPv4ParamRecord struct {
  105. OrigDescr string
  106. Descr string
  107. Value int
  108. }
  109. func (icp *icmpv4Parameters) escape() []canonICMPv4ParamRecord {
  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([]canonICMPv4ParamRecord, 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.Descr, "Reserved") ||
  133. strings.Contains(pr.Descr, "Unassigned") ||
  134. strings.Contains(pr.Descr, "Deprecated") ||
  135. strings.Contains(pr.Descr, "Experiment") ||
  136. strings.Contains(pr.Descr, "experiment") {
  137. continue
  138. }
  139. ss := strings.Split(pr.Descr, "\n")
  140. if len(ss) > 1 {
  141. prs[i].Descr = strings.Join(ss, " ")
  142. } else {
  143. prs[i].Descr = ss[0]
  144. }
  145. s := strings.TrimSpace(prs[i].Descr)
  146. prs[i].OrigDescr = s
  147. prs[i].Descr = 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. prs = append([]canonProtocolRecord{{
  160. Name: "IP",
  161. Descr: "IPv4 encapsulation, pseudo protocol number",
  162. Value: 0,
  163. }}, prs...)
  164. fmt.Fprintf(w, "// %s, Updated: %s\n", pn.Title, pn.Updated)
  165. fmt.Fprintf(w, "const (\n")
  166. for _, pr := range prs {
  167. if pr.Name == "" {
  168. continue
  169. }
  170. fmt.Fprintf(w, "ianaProtocol%s = %d", pr.Name, pr.Value)
  171. s := pr.Descr
  172. if s == "" {
  173. s = pr.OrigName
  174. }
  175. fmt.Fprintf(w, "// %s\n", s)
  176. }
  177. fmt.Fprintf(w, ")\n")
  178. return nil
  179. }
  180. type protocolNumbers struct {
  181. XMLName xml.Name `xml:"registry"`
  182. Title string `xml:"title"`
  183. Updated string `xml:"updated"`
  184. RegTitle string `xml:"registry>title"`
  185. Note string `xml:"registry>note"`
  186. Records []struct {
  187. Value string `xml:"value"`
  188. Name string `xml:"name"`
  189. Descr string `xml:"description"`
  190. } `xml:"registry>record"`
  191. }
  192. type canonProtocolRecord struct {
  193. OrigName string
  194. Name string
  195. Descr string
  196. Value int
  197. }
  198. func (pn *protocolNumbers) escape() []canonProtocolRecord {
  199. prs := make([]canonProtocolRecord, len(pn.Records))
  200. sr := strings.NewReplacer(
  201. "-in-", "in",
  202. "-within-", "within",
  203. "-over-", "over",
  204. "+", "P",
  205. "-", "",
  206. "/", "",
  207. ".", "",
  208. " ", "",
  209. )
  210. for i, pr := range pn.Records {
  211. prs[i].OrigName = pr.Name
  212. s := strings.TrimSpace(pr.Name)
  213. switch pr.Name {
  214. case "ISIS over IPv4":
  215. prs[i].Name = "ISIS"
  216. case "manet":
  217. prs[i].Name = "MANET"
  218. default:
  219. prs[i].Name = sr.Replace(s)
  220. }
  221. ss := strings.Split(pr.Descr, "\n")
  222. for i := range ss {
  223. ss[i] = strings.TrimSpace(ss[i])
  224. }
  225. if len(ss) > 1 {
  226. prs[i].Descr = strings.Join(ss, " ")
  227. } else {
  228. prs[i].Descr = ss[0]
  229. }
  230. prs[i].Value, _ = strconv.Atoi(pr.Value)
  231. }
  232. return prs
  233. }