doc.go 7.5 KB

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