example_test.go 5.2 KB

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