gen.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2012 Google, Inc. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree.
  6. // +build ignore
  7. // This binary pulls known ports from IANA, and uses them to populate
  8. // iana_ports.go's TCPPortNames and UDPPortNames maps.
  9. //
  10. // go run gen.go | gofmt > iana_ports.go
  11. package main
  12. import (
  13. "bytes"
  14. "encoding/xml"
  15. "flag"
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "os"
  20. "strconv"
  21. "time"
  22. )
  23. const fmtString = `// Copyright 2012 Google, Inc. All rights reserved.
  24. package layers
  25. // Created by gen.go, don't edit manually
  26. // Generated at %s
  27. // Fetched from %q
  28. // TCPPortNames contains the port names for all TCP ports.
  29. var TCPPortNames = tcpPortNames
  30. // UDPPortNames contains the port names for all UDP ports.
  31. var UDPPortNames = udpPortNames
  32. // SCTPPortNames contains the port names for all SCTP ports.
  33. var SCTPPortNames = sctpPortNames
  34. var tcpPortNames = map[TCPPort]string{
  35. %s}
  36. var udpPortNames = map[UDPPort]string{
  37. %s}
  38. var sctpPortNames = map[SCTPPort]string{
  39. %s}
  40. `
  41. var url = flag.String("url", "http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml", "URL to grab port numbers from")
  42. func main() {
  43. fmt.Fprintf(os.Stderr, "Fetching ports from %q\n", *url)
  44. resp, err := http.Get(*url)
  45. if err != nil {
  46. panic(err)
  47. }
  48. defer resp.Body.Close()
  49. body, err := ioutil.ReadAll(resp.Body)
  50. if err != nil {
  51. panic(err)
  52. }
  53. fmt.Fprintln(os.Stderr, "Parsing XML")
  54. var registry struct {
  55. Records []struct {
  56. Protocol string `xml:"protocol"`
  57. Number string `xml:"number"`
  58. Name string `xml:"name"`
  59. } `xml:"record"`
  60. }
  61. xml.Unmarshal(body, &registry)
  62. var tcpPorts bytes.Buffer
  63. var udpPorts bytes.Buffer
  64. var sctpPorts bytes.Buffer
  65. done := map[string]map[int]bool{
  66. "tcp": map[int]bool{},
  67. "udp": map[int]bool{},
  68. "sctp": map[int]bool{},
  69. }
  70. for _, r := range registry.Records {
  71. port, err := strconv.Atoi(r.Number)
  72. if err != nil {
  73. continue
  74. }
  75. if r.Name == "" {
  76. continue
  77. }
  78. var b *bytes.Buffer
  79. switch r.Protocol {
  80. case "tcp":
  81. b = &tcpPorts
  82. case "udp":
  83. b = &udpPorts
  84. case "sctp":
  85. b = &sctpPorts
  86. default:
  87. continue
  88. }
  89. if done[r.Protocol][port] {
  90. continue
  91. }
  92. done[r.Protocol][port] = true
  93. fmt.Fprintf(b, "\t%d: %q,\n", port, r.Name)
  94. }
  95. fmt.Fprintln(os.Stderr, "Writing results to stdout")
  96. fmt.Printf(fmtString, time.Now(), *url, tcpPorts.String(), udpPorts.String(), sctpPorts.String())
  97. }