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.
5
// Package ipv6 implements IP-level socket options for the Internet
8
// The package provides IP-level socket options that allow
9
// manipulation of IPv6 facilities. The IPv6 and socket options for
10
// IPv6 are defined in RFC 2460, RFC 3493 and RFC 3542.
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.
22
// ln, err := net.Listen("tcp6", "[::]:1024")
28
// c, err := ln.Accept()
32
// go func(c net.Conn) {
35
// The outgoing packets will be labeled DiffServ assured forwarding
36
// class 1 low drop precedence, as known as AF11 packets.
38
// if err := ipv6.NewConn(c).SetTrafficClass(DiffServAF11); err != nil {
41
// if _, err := c.Write(data); err != nil {
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
56
// en0, err := net.InterfaceByName("en0")
60
// en1, err := net.InterfaceByIndex(911)
64
// group := net.ParseIP("ff02::114")
66
// First, an application listens to an appropriate address with an
67
// appropriate service port.
69
// c, err := net.ListenPacket("udp6", "[::]:1024")
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.
81
// p := ipv6.NewPacketConn(c)
82
// if err := p.JoinGroup(en0, &net.UDPAddr{IP: group}); err != nil {
85
// if err := p.JoinGroup(en1, &net.UDPAddr{IP: group}); err != nil {
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.
95
// if err := p.SetControlMessage(ipv6.FlagDst, true); err != nil {
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.
103
// b := make([]byte, 1500)
105
// n, rcm, src, err := p.ReadFrom(b)
109
// if rcm.Dst.IsMulticast() {
110
// if rcm.Dst.Equal(group)
111
// // joined group, do something
113
// // unknown group, discard
118
// The application can also send both unicast and multicast packets.
120
// p.SetTrafficClass(DiffServCS0)
122
// if _, err := p.WriteTo(data[:n], nil, src); err != nil {
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 {
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
143
// c, err := net.ListenPacket("udp6", "[::]:1024")
148
// p := ipv6.NewPacketConn(c)
149
// if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::1:114")}); err != nil {
152
// if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::2:114")}); err != nil {
155
// if err := p.JoinGroup(en1, &net.UDPAddr{IP: net.ParseIP("ff02::2:114")}); err != nil {
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.
165
// c1, err := net.ListenPacket("udp6", "[ff02::]:1024")
170
// c2, err := net.ListenPacket("udp6", "[ff02::]:1024")
175
// p1 := ipv6.NewPacketConn(c1)
176
// if err := p1.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil {
179
// p2 := ipv6.NewPacketConn(c2)
180
// if err := p2.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil {
184
// Also it is possible for the application to leave or rejoin a
185
// multicast group on the network interface.
187
// if err := p.LeaveGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff02::114")}); err != nil {
190
// if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.ParseIP("ff01::114")}); err != nil {