doc.go 7.7 KB

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