doc.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.
  9. //
  10. // The IPv6 protocol is defined in RFC 8200.
  11. // Socket interface extensions are defined in RFC 3493, RFC 3542 and
  12. // RFC 3678.
  13. // MLDv1 and MLDv2 are defined in RFC 2710 and RFC 3810.
  14. // Source-specific multicast is defined in RFC 4607.
  15. //
  16. // On Darwin, this package requires OS X Mavericks version 10.9 or
  17. // above, or equivalent.
  18. //
  19. // # Unicasting
  20. //
  21. // The options for unicasting are available for net.TCPConn,
  22. // net.UDPConn and net.IPConn which are created as network connections
  23. // that use the IPv6 transport. When a single TCP connection carrying
  24. // a data flow of multiple packets needs to indicate the flow is
  25. // important, Conn is used to set the traffic class field on the IPv6
  26. // header for each packet.
  27. //
  28. // ln, err := net.Listen("tcp6", "[::]:1024")
  29. // if err != nil {
  30. // // error handling
  31. // }
  32. // defer ln.Close()
  33. // for {
  34. // c, err := ln.Accept()
  35. // if err != nil {
  36. // // error handling
  37. // }
  38. // go func(c net.Conn) {
  39. // defer c.Close()
  40. //
  41. // The outgoing packets will be labeled DiffServ assured forwarding
  42. // class 1 low drop precedence, known as AF11 packets.
  43. //
  44. // if err := ipv6.NewConn(c).SetTrafficClass(0x28); err != nil {
  45. // // error handling
  46. // }
  47. // if _, err := c.Write(data); err != nil {
  48. // // error handling
  49. // }
  50. // }(c)
  51. // }
  52. //
  53. // # Multicasting
  54. //
  55. // The options for multicasting are available for net.UDPConn and
  56. // net.IPConn which are created as network connections that use the
  57. // IPv6 transport. A few network facilities must be prepared before
  58. // you begin multicasting, at a minimum joining network interfaces and
  59. // multicast groups.
  60. //
  61. // en0, err := net.InterfaceByName("en0")
  62. // if err != nil {
  63. // // error handling
  64. // }
  65. // en1, err := net.InterfaceByIndex(911)
  66. // if err != nil {
  67. // // error handling
  68. // }
  69. // group := net.ParseIP("ff02::114")
  70. //
  71. // First, an application listens to an appropriate address with an
  72. // appropriate service port.
  73. //
  74. // c, err := net.ListenPacket("udp6", "[::]:1024")
  75. // if err != nil {
  76. // // error handling
  77. // }
  78. // defer c.Close()
  79. //
  80. // Second, the application joins multicast groups, starts listening to
  81. // the groups on the specified network interfaces. Note that the
  82. // service port for transport layer protocol does not matter with this
  83. // operation as joining groups affects only network and link layer
  84. // protocols, such as IPv6 and Ethernet.
  85. //
  86. // p := ipv6.NewPacketConn(c)
  87. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: group}); err != nil {
  88. // // error handling
  89. // }
  90. // if err := p.JoinGroup(en1, &net.UDPAddr{IP: group}); err != nil {
  91. // // error handling
  92. // }
  93. //
  94. // The application might set per packet control message transmissions
  95. // between the protocol stack within the kernel. When the application
  96. // needs a destination address on an incoming packet,
  97. // SetControlMessage of PacketConn is used to enable control message
  98. // transmissions.
  99. //
  100. // if err := p.SetControlMessage(ipv6.FlagDst, true); err != nil {
  101. // // error handling
  102. // }
  103. //
  104. // The application could identify whether the received packets are
  105. // of interest by using the control message that contains the
  106. // destination address of the received packet.
  107. //
  108. // b := make([]byte, 1500)
  109. // for {
  110. // n, rcm, src, err := p.ReadFrom(b)
  111. // if err != nil {
  112. // // error handling
  113. // }
  114. // if rcm.Dst.IsMulticast() {
  115. // if rcm.Dst.Equal(group) {
  116. // // joined group, do something
  117. // } else {
  118. // // unknown group, discard
  119. // continue
  120. // }
  121. // }
  122. //
  123. // The application can also send both unicast and multicast packets.
  124. //
  125. // p.SetTrafficClass(0x0)
  126. // p.SetHopLimit(16)
  127. // if _, err := p.WriteTo(data[:n], nil, src); err != nil {
  128. // // error handling
  129. // }
  130. // dst := &net.UDPAddr{IP: group, Port: 1024}
  131. // wcm := ipv6.ControlMessage{TrafficClass: 0xe0, HopLimit: 1}
  132. // for _, ifi := range []*net.Interface{en0, en1} {
  133. // wcm.IfIndex = ifi.Index
  134. // if _, err := p.WriteTo(data[:n], &wcm, dst); err != nil {
  135. // // error handling
  136. // }
  137. // }
  138. // }
  139. //
  140. // # More multicasting
  141. //
  142. // An application that uses PacketConn may join multiple multicast
  143. // groups. For example, a UDP listener with port 1024 might join two
  144. // different groups across over two different network interfaces by
  145. // using:
  146. //
  147. // c, err := net.ListenPacket("udp6", "[::]:1024")
  148. // if err != nil {
  149. // // error handling
  150. // }
  151. // defer c.Close()
  152. // p := ipv6.NewPacketConn(c)
  153. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::1:114")}); err != nil {
  154. // // error handling
  155. // }
  156. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::2:114")}); err != nil {
  157. // // error handling
  158. // }
  159. // if err := p.JoinGroup(en1, &net.UDPAddr{IP: net.ParseIP("ff02::2:114")}); err != nil {
  160. // // error handling
  161. // }
  162. //
  163. // It is possible for multiple UDP listeners that listen on the same
  164. // UDP port to join the same multicast group. The net package will
  165. // provide a socket that listens to a wildcard address with reusable
  166. // UDP port when an appropriate multicast address prefix is passed to
  167. // the net.ListenPacket or net.ListenUDP.
  168. //
  169. // c1, err := net.ListenPacket("udp6", "[ff02::]:1024")
  170. // if err != nil {
  171. // // error handling
  172. // }
  173. // defer c1.Close()
  174. // c2, err := net.ListenPacket("udp6", "[ff02::]:1024")
  175. // if err != nil {
  176. // // error handling
  177. // }
  178. // defer c2.Close()
  179. // p1 := ipv6.NewPacketConn(c1)
  180. // if err := p1.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil {
  181. // // error handling
  182. // }
  183. // p2 := ipv6.NewPacketConn(c2)
  184. // if err := p2.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil {
  185. // // error handling
  186. // }
  187. //
  188. // Also it is possible for the application to leave or rejoin a
  189. // multicast group on the network interface.
  190. //
  191. // if err := p.LeaveGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil {
  192. // // error handling
  193. // }
  194. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff01::114")}); err != nil {
  195. // // error handling
  196. // }
  197. //
  198. // # Source-specific multicasting
  199. //
  200. // An application that uses PacketConn on MLDv2 supported platform is
  201. // able to join source-specific multicast groups.
  202. // The application may use JoinSourceSpecificGroup and
  203. // LeaveSourceSpecificGroup for the operation known as "include" mode,
  204. //
  205. // ssmgroup := net.UDPAddr{IP: net.ParseIP("ff32::8000:9")}
  206. // ssmsource := net.UDPAddr{IP: net.ParseIP("fe80::cafe")}
  207. // if err := p.JoinSourceSpecificGroup(en0, &ssmgroup, &ssmsource); err != nil {
  208. // // error handling
  209. // }
  210. // if err := p.LeaveSourceSpecificGroup(en0, &ssmgroup, &ssmsource); err != nil {
  211. // // error handling
  212. // }
  213. //
  214. // or JoinGroup, ExcludeSourceSpecificGroup,
  215. // IncludeSourceSpecificGroup and LeaveGroup for the operation known
  216. // as "exclude" mode.
  217. //
  218. // exclsource := net.UDPAddr{IP: net.ParseIP("fe80::dead")}
  219. // if err := p.JoinGroup(en0, &ssmgroup); err != nil {
  220. // // error handling
  221. // }
  222. // if err := p.ExcludeSourceSpecificGroup(en0, &ssmgroup, &exclsource); err != nil {
  223. // // error handling
  224. // }
  225. // if err := p.LeaveGroup(en0, &ssmgroup); err != nil {
  226. // // error handling
  227. // }
  228. //
  229. // Note that it depends on each platform implementation what happens
  230. // when an application which runs on MLDv2 unsupported platform uses
  231. // JoinSourceSpecificGroup and LeaveSourceSpecificGroup.
  232. // In general the platform tries to fall back to conversations using
  233. // MLDv1 and starts to listen to multicast traffic.
  234. // In the fallback case, ExcludeSourceSpecificGroup and
  235. // IncludeSourceSpecificGroup may return an error.
  236. package ipv6 // import "golang.org/x/net/ipv6"
  237. // BUG(mikio): This package is not implemented on JS, NaCl and Plan 9.