gen.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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",
  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(0)
  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(id int) []canonICMPv4ParamRecord {
  112. prs := make([]canonICMPv4ParamRecord, 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.Descr, "Reserved") ||
  125. strings.Contains(pr.Descr, "Unassigned") ||
  126. strings.Contains(pr.Descr, "Deprecated") ||
  127. strings.Contains(pr.Descr, "Experiment") ||
  128. strings.Contains(pr.Descr, "experiment") {
  129. continue
  130. }
  131. ss := strings.Split(pr.Descr, "\n")
  132. if len(ss) > 1 {
  133. prs[i].Descr = strings.Join(ss, " ")
  134. } else {
  135. prs[i].Descr = ss[0]
  136. }
  137. s := strings.TrimSpace(prs[i].Descr)
  138. prs[i].OrigDescr = s
  139. prs[i].Descr = 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. prs = append([]canonProtocolRecord{{
  152. Name: "IP",
  153. Descr: "IPv4 encapsulation, pseudo protocol number",
  154. Value: 0,
  155. }}, prs...)
  156. fmt.Fprintf(w, "// %s, Updated: %s\n", pn.Title, pn.Updated)
  157. fmt.Fprintf(w, "const (\n")
  158. for _, pr := range prs {
  159. if pr.Name == "" {
  160. continue
  161. }
  162. fmt.Fprintf(w, "ianaProtocol%s = %d", pr.Name, pr.Value)
  163. s := pr.Descr
  164. if s == "" {
  165. s = pr.OrigName
  166. }
  167. fmt.Fprintf(w, "// %s\n", s)
  168. }
  169. fmt.Fprintf(w, ")\n")
  170. return nil
  171. }
  172. type protocolNumbers struct {
  173. XMLName xml.Name `xml:"registry"`
  174. Title string `xml:"title"`
  175. Updated string `xml:"updated"`
  176. RegTitle string `xml:"registry>title"`
  177. Note string `xml:"registry>note"`
  178. Records []protocolRecord `xml:"registry>record"`
  179. }
  180. type protocolRecord struct {
  181. Value string `xml:"value"`
  182. Name string `xml:"name"`
  183. Descr string `xml:"description"`
  184. }
  185. type canonProtocolRecord struct {
  186. OrigName string
  187. Name string
  188. Descr string
  189. Value int
  190. }
  191. func (pn *protocolNumbers) escape() []canonProtocolRecord {
  192. prs := make([]canonProtocolRecord, len(pn.Records))
  193. sr := strings.NewReplacer(
  194. "-in-", "in",
  195. "-within-", "within",
  196. "-over-", "over",
  197. "+", "P",
  198. "-", "",
  199. "/", "",
  200. ".", "",
  201. " ", "",
  202. )
  203. for i, pr := range pn.Records {
  204. prs[i].OrigName = pr.Name
  205. s := strings.TrimSpace(pr.Name)
  206. switch pr.Name {
  207. case "ISIS over IPv4":
  208. prs[i].Name = "ISIS"
  209. case "manet":
  210. prs[i].Name = "MANET"
  211. default:
  212. prs[i].Name = sr.Replace(s)
  213. }
  214. ss := strings.Split(pr.Descr, "\n")
  215. for i := range ss {
  216. ss[i] = strings.TrimSpace(ss[i])
  217. }
  218. if len(ss) > 1 {
  219. prs[i].Descr = strings.Join(ss, " ")
  220. } else {
  221. prs[i].Descr = ss[0]
  222. }
  223. prs[i].Value, _ = strconv.Atoi(pr.Value)
  224. }
  225. return prs
  226. }