Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 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 | |
| 5 | // Package ipv6 implements IP-level socket options for the Internet |
| 6 | // Protocol version 6. |
| 7 | // |
| 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. |
| 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, as 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 |
Mikio Hara | 6226a2f | 2013-06-29 10:23:53 +0900 | [diff] [blame] | 54 | // multicast groups. |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 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 | // |
Mikio Hara | 6226a2f | 2013-06-29 10:23:53 +0900 | [diff] [blame] | 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. |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 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 | // |
Mikio Hara | 6226a2f | 2013-06-29 10:23:53 +0900 | [diff] [blame] | 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: |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 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 |
Mikio Hara | 6226a2f | 2013-06-29 10:23:53 +0900 | [diff] [blame] | 160 | // UDP port to join the same multicast group. The net package will |
Mikio Hara | cdfc4ce | 2013-06-04 17:42:58 +0900 | [diff] [blame] | 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 | package ipv6 |