doc.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // Package ipv6 implements IP-level socket options for the Internet
  5. // Protocol version 6.
  6. //
  7. // The package provides IP-level socket options that allow
  8. // manipulation of IPv6 facilities. The IPv6 and socket options for
  9. // IPv6 are defined in RFC 2460, RFC 3493 and RFC 3542.
  10. //
  11. //
  12. // Unicasting
  13. //
  14. // The options for unicasting are available for net.TCPConn,
  15. // net.UDPConn and net.IPConn which are created as network connections
  16. // that use the IPv6 transport. When a single TCP connection carrying
  17. // a data flow of multiple packets needs to indicate the flow is
  18. // important, ipv6.Conn is used to set the traffic class field on the
  19. // IPv6 header for each packet.
  20. //
  21. // ln, err := net.Listen("tcp6", "[::]:1024")
  22. // if err != nil {
  23. // // error handling
  24. // }
  25. // defer ln.Close()
  26. // for {
  27. // c, err := ln.Accept()
  28. // if err != nil {
  29. // // error handling
  30. // }
  31. // go func(c net.Conn) {
  32. // defer c.Close()
  33. //
  34. // The outgoing packets will be labeled DiffServ assured forwarding
  35. // class 1 low drop precedence, as known as AF11 packets.
  36. //
  37. // if err := ipv6.NewConn(c).SetTrafficClass(DiffServAF11); err != nil {
  38. // // error handling
  39. // }
  40. // if _, err := c.Write(data); err != nil {
  41. // // error handling
  42. // }
  43. // }(c)
  44. // }
  45. //
  46. //
  47. // Multicasting
  48. //
  49. // The options for multicasting are available for net.UDPConn and
  50. // net.IPconn which are created as network connections that use the
  51. // IPv6 transport. A few network facilities must be prepared before
  52. // you begin multicasting, at a minimum joining network interfaces and
  53. // multicast groups.
  54. //
  55. // en0, err := net.InterfaceByName("en0")
  56. // if err != nil {
  57. // // error handling
  58. // }
  59. // en1, err := net.InterfaceByIndex(911)
  60. // if err != nil {
  61. // // error handling
  62. // }
  63. // group := net.ParseIP("ff02::114")
  64. //
  65. // First, an application listens to an appropriate address with an
  66. // appropriate service port.
  67. //
  68. // c, err := net.ListenPacket("udp6", "[::]:1024")
  69. // if err != nil {
  70. // // error handling
  71. // }
  72. // defer c.Close()
  73. //
  74. // Second, the application joins multicast groups, starts listening to
  75. // the groups on the specified network interfaces. Note that the
  76. // service port for transport layer protocol does not matter with this
  77. // operation as joining groups affects only network and link layer
  78. // protocols, such as IPv6 and Ethernet.
  79. //
  80. // p := ipv6.NewPacketConn(c)
  81. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: group}); err != nil {
  82. // // error handling
  83. // }
  84. // if err := p.JoinGroup(en1, &net.UDPAddr{IP: group}); err != nil {
  85. // // error handling
  86. // }
  87. //
  88. // The application might set per packet control message transmissions
  89. // between the protocol stack within the kernel. When the application
  90. // needs a destination address on an incoming packet,
  91. // SetControlMessage of ipv6.PacketConn is used to enable control
  92. // message transmissons.
  93. //
  94. // if err := p.SetControlMessage(ipv6.FlagDst, true); err != nil {
  95. // // error handling
  96. // }
  97. //
  98. // The application could identify whether the received packets are
  99. // of interest by using the control message that contains the
  100. // destination address of the received packet.
  101. //
  102. // b := make([]byte, 1500)
  103. // for {
  104. // n, rcm, src, err := p.ReadFrom(b)
  105. // if err != nil {
  106. // // error handling
  107. // }
  108. // if rcm.Dst.IsMulticast() {
  109. // if rcm.Dst.Equal(group)
  110. // // joined group, do something
  111. // } else {
  112. // // unknown group, discard
  113. // continue
  114. // }
  115. // }
  116. //
  117. // The application can also send both unicast and multicast packets.
  118. //
  119. // p.SetTrafficClass(DiffServCS0)
  120. // p.SetHopLimit(16)
  121. // if _, err := p.WriteTo(data[:n], nil, src); err != nil {
  122. // // error handling
  123. // }
  124. // dst := &net.UDPAddr{IP: group, Port: 1024}
  125. // wcm := ipv6.ControlMessage{TrafficClass: DiffServCS7, HopLimit: 1}
  126. // for _, ifi := range []*net.Interface{en0, en1} {
  127. // wcm.IfIndex = ifi.Index
  128. // if _, err := p.WriteTo(data[:n], &wcm, dst); err != nil {
  129. // // error handling
  130. // }
  131. // }
  132. // }
  133. //
  134. //
  135. // More multicasting
  136. //
  137. // An application that uses PacketConn may join multiple multicast
  138. // groups. For example, a UDP listener with port 1024 might join two
  139. // different groups across over two different network interfaces by
  140. // using:
  141. //
  142. // c, err := net.ListenPacket("udp6", "[::]:1024")
  143. // if err != nil {
  144. // // error handling
  145. // }
  146. // defer c.Close()
  147. // p := ipv6.NewPacketConn(c)
  148. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::1:114")}); err != nil {
  149. // // error handling
  150. // }
  151. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::2:114")}); err != nil {
  152. // // error handling
  153. // }
  154. // if err := p.JoinGroup(en1, &net.UDPAddr{IP: net.ParseIP("ff02::2:114")}); err != nil {
  155. // // error handling
  156. // }
  157. //
  158. // It is possible for multiple UDP listeners that listen on the same
  159. // UDP port to join the same multicast group. The net package will
  160. // provide a socket that listens to a wildcard address with reusable
  161. // UDP port when an appropriate multicast address prefix is passed to
  162. // the net.ListenPacket or net.ListenUDP.
  163. //
  164. // c1, err := net.ListenPacket("udp6", "[ff02::]:1024")
  165. // if err != nil {
  166. // // error handling
  167. // }
  168. // defer c1.Close()
  169. // c2, err := net.ListenPacket("udp6", "[ff02::]:1024")
  170. // if err != nil {
  171. // // error handling
  172. // }
  173. // defer c2.Close()
  174. // p1 := ipv6.NewPacketConn(c1)
  175. // if err := p1.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil {
  176. // // error handling
  177. // }
  178. // p2 := ipv6.NewPacketConn(c2)
  179. // if err := p2.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil {
  180. // // error handling
  181. // }
  182. //
  183. // Also it is possible for the application to leave or rejoin a
  184. // multicast group on the network interface.
  185. //
  186. // if err := p.LeaveGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil {
  187. // // error handling
  188. // }
  189. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff01::114")}); err != nil {
  190. // // error handling
  191. // }
  192. package ipv6