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