example_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2014 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. package ipv6_test
  5. import (
  6. "fmt"
  7. "log"
  8. "net"
  9. "os"
  10. "time"
  11. "golang.org/x/net/icmp"
  12. "golang.org/x/net/ipv6"
  13. )
  14. func ExampleConn_markingTCP() {
  15. ln, err := net.Listen("tcp", "[::]:1024")
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. defer ln.Close()
  20. for {
  21. c, err := ln.Accept()
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. go func(c net.Conn) {
  26. defer c.Close()
  27. if c.RemoteAddr().(*net.TCPAddr).IP.To16() != nil && c.RemoteAddr().(*net.TCPAddr).IP.To4() == nil {
  28. p := ipv6.NewConn(c)
  29. if err := p.SetTrafficClass(0x28); err != nil { // DSCP AF11
  30. log.Fatal(err)
  31. }
  32. if err := p.SetHopLimit(128); err != nil {
  33. log.Fatal(err)
  34. }
  35. }
  36. if _, err := c.Write([]byte("HELLO-R-U-THERE-ACK")); err != nil {
  37. log.Fatal(err)
  38. }
  39. }(c)
  40. }
  41. }
  42. func ExamplePacketConn_servingOneShotMulticastDNS() {
  43. c, err := net.ListenPacket("udp6", "[::]:5353") // mDNS over UDP
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. defer c.Close()
  48. p := ipv6.NewPacketConn(c)
  49. en0, err := net.InterfaceByName("en0")
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. mDNSLinkLocal := net.UDPAddr{IP: net.ParseIP("ff02::fb")}
  54. if err := p.JoinGroup(en0, &mDNSLinkLocal); err != nil {
  55. log.Fatal(err)
  56. }
  57. defer p.LeaveGroup(en0, &mDNSLinkLocal)
  58. if err := p.SetControlMessage(ipv6.FlagDst|ipv6.FlagInterface, true); err != nil {
  59. log.Fatal(err)
  60. }
  61. var wcm ipv6.ControlMessage
  62. b := make([]byte, 1500)
  63. for {
  64. _, rcm, peer, err := p.ReadFrom(b)
  65. if err != nil {
  66. log.Fatal(err)
  67. }
  68. if !rcm.Dst.IsMulticast() || !rcm.Dst.Equal(mDNSLinkLocal.IP) {
  69. continue
  70. }
  71. wcm.IfIndex = rcm.IfIndex
  72. answers := []byte("FAKE-MDNS-ANSWERS") // fake mDNS answers, you need to implement this
  73. if _, err := p.WriteTo(answers, &wcm, peer); err != nil {
  74. log.Fatal(err)
  75. }
  76. }
  77. }
  78. func ExamplePacketConn_tracingIPPacketRoute() {
  79. // Tracing an IP packet route to www.google.com.
  80. const host = "www.google.com"
  81. ips, err := net.LookupIP(host)
  82. if err != nil {
  83. log.Fatal(err)
  84. }
  85. var dst net.IPAddr
  86. for _, ip := range ips {
  87. if ip.To16() != nil && ip.To4() == nil {
  88. dst.IP = ip
  89. fmt.Printf("using %v for tracing an IP packet route to %s\n", dst.IP, host)
  90. break
  91. }
  92. }
  93. if dst.IP == nil {
  94. log.Fatal("no AAAA record found")
  95. }
  96. c, err := net.ListenPacket("ip6:58", "::") // ICMP for IPv6
  97. if err != nil {
  98. log.Fatal(err)
  99. }
  100. defer c.Close()
  101. p := ipv6.NewPacketConn(c)
  102. if err := p.SetControlMessage(ipv6.FlagHopLimit|ipv6.FlagSrc|ipv6.FlagDst|ipv6.FlagInterface, true); err != nil {
  103. log.Fatal(err)
  104. }
  105. wm := icmp.Message{
  106. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  107. Body: &icmp.Echo{
  108. ID: os.Getpid() & 0xffff,
  109. Data: []byte("HELLO-R-U-THERE"),
  110. },
  111. }
  112. var f ipv6.ICMPFilter
  113. f.SetAll(true)
  114. f.Accept(ipv6.ICMPTypeTimeExceeded)
  115. f.Accept(ipv6.ICMPTypeEchoReply)
  116. if err := p.SetICMPFilter(&f); err != nil {
  117. log.Fatal(err)
  118. }
  119. var wcm ipv6.ControlMessage
  120. rb := make([]byte, 1500)
  121. for i := 1; i <= 64; i++ { // up to 64 hops
  122. wm.Body.(*icmp.Echo).Seq = i
  123. wb, err := wm.Marshal(nil)
  124. if err != nil {
  125. log.Fatal(err)
  126. }
  127. // In the real world usually there are several
  128. // multiple traffic-engineered paths for each hop.
  129. // You may need to probe a few times to each hop.
  130. begin := time.Now()
  131. wcm.HopLimit = i
  132. if _, err := p.WriteTo(wb, &wcm, &dst); err != nil {
  133. log.Fatal(err)
  134. }
  135. if err := p.SetReadDeadline(time.Now().Add(3 * time.Second)); err != nil {
  136. log.Fatal(err)
  137. }
  138. n, rcm, peer, err := p.ReadFrom(rb)
  139. if err != nil {
  140. if err, ok := err.(net.Error); ok && err.Timeout() {
  141. fmt.Printf("%v\t*\n", i)
  142. continue
  143. }
  144. log.Fatal(err)
  145. }
  146. rm, err := icmp.ParseMessage(58, rb[:n])
  147. if err != nil {
  148. log.Fatal(err)
  149. }
  150. rtt := time.Since(begin)
  151. // In the real world you need to determine whether the
  152. // received message is yours using ControlMessage.Src,
  153. // ControlMesage.Dst, icmp.Echo.ID and icmp.Echo.Seq.
  154. switch rm.Type {
  155. case ipv6.ICMPTypeTimeExceeded:
  156. names, _ := net.LookupAddr(peer.String())
  157. fmt.Printf("%d\t%v %+v %v\n\t%+v\n", i, peer, names, rtt, rcm)
  158. case ipv6.ICMPTypeEchoReply:
  159. names, _ := net.LookupAddr(peer.String())
  160. fmt.Printf("%d\t%v %+v %v\n\t%+v\n", i, peer, names, rtt, rcm)
  161. return
  162. }
  163. }
  164. }
  165. func ExamplePacketConn_advertisingOSPFHello() {
  166. c, err := net.ListenPacket("ip6:89", "::") // OSPF for IPv6
  167. if err != nil {
  168. log.Fatal(err)
  169. }
  170. defer c.Close()
  171. p := ipv6.NewPacketConn(c)
  172. en0, err := net.InterfaceByName("en0")
  173. if err != nil {
  174. log.Fatal(err)
  175. }
  176. allSPFRouters := net.IPAddr{IP: net.ParseIP("ff02::5")}
  177. if err := p.JoinGroup(en0, &allSPFRouters); err != nil {
  178. log.Fatal(err)
  179. }
  180. defer p.LeaveGroup(en0, &allSPFRouters)
  181. hello := make([]byte, 24) // fake hello data, you need to implement this
  182. ospf := make([]byte, 16) // fake ospf header, you need to implement this
  183. ospf[0] = 3 // version 3
  184. ospf[1] = 1 // hello packet
  185. ospf = append(ospf, hello...)
  186. if err := p.SetChecksum(true, 12); err != nil {
  187. log.Fatal(err)
  188. }
  189. cm := ipv6.ControlMessage{
  190. TrafficClass: 0xc0, // DSCP CS6
  191. HopLimit: 1,
  192. IfIndex: en0.Index,
  193. }
  194. if _, err := p.WriteTo(ospf, &cm, &allSPFRouters); err != nil {
  195. log.Fatal(err)
  196. }
  197. }