doc.go 7.8 KB

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