Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 1 | // Copyright 2012 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 ipv4 implements IP-level socket options for the Internet |
| 6 | // Protocol version 4. |
| 7 | // |
| 8 | // The package provides IP-level socket options that allow |
| 9 | // manipulation of IPv4 facilities. The IPv4 and basic host |
Mikio Hara | 90b3e53 | 2012-10-05 11:21:34 +0900 | [diff] [blame] | 10 | // requirements for IPv4 are defined in RFC 791, RFC 1112 and RFC |
Mikio Hara | 8108b4b | 2013-06-07 14:52:58 +0900 | [diff] [blame] | 11 | // 1122. |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 12 | // |
| 13 | // |
| 14 | // Unicasting |
| 15 | // |
| 16 | // The options for unicasting are available for net.TCPConn, |
| 17 | // net.UDPConn and net.IPConn which are created as network connections |
| 18 | // that use the IPv4 transport. When a single TCP connection carrying |
| 19 | // a data flow of multiple packets needs to indicate the flow is |
| 20 | // important, ipv4.Conn is used to set the type-of-service field on |
| 21 | // the IPv4 header for each packet. |
| 22 | // |
Mikio Hara | 811c2f5 | 2012-11-06 22:00:43 +0900 | [diff] [blame] | 23 | // ln, err := net.Listen("tcp4", "0.0.0.0:1024") |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 24 | // if err != nil { |
| 25 | // // error handling |
| 26 | // } |
| 27 | // defer ln.Close() |
| 28 | // for { |
| 29 | // c, err := ln.Accept() |
| 30 | // if err != nil { |
| 31 | // // error handling |
| 32 | // } |
| 33 | // go func(c net.Conn) { |
| 34 | // defer c.Close() |
| 35 | // |
| 36 | // The outgoing packets will be labeled DiffServ assured forwarding |
| 37 | // class 1 low drop precedence, as known as AF11 packets. |
| 38 | // |
Mikio Hara | 8108b4b | 2013-06-07 14:52:58 +0900 | [diff] [blame] | 39 | // err := ipv4.NewConn(c).SetTOS(DiffServAF11) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 40 | // if err != nil { |
| 41 | // // error handling |
| 42 | // } |
| 43 | // _, err = c.Write(data) |
| 44 | // if err != nil { |
| 45 | // // error handling |
| 46 | // } |
| 47 | // }(c) |
| 48 | // } |
| 49 | // |
| 50 | // |
| 51 | // Multicasting |
| 52 | // |
| 53 | // The options for multicasting are available for net.UDPConn and |
| 54 | // net.IPconn which are created as network connections that use the |
| 55 | // IPv4 transport. A few network facilities must be prepared before |
| 56 | // you begin multicasting, at a minimum joining network interfaces and |
| 57 | // group addresses. |
| 58 | // |
| 59 | // en0, err := net.InterfaceByName("en0") |
| 60 | // if err != nil { |
| 61 | // // error handling |
| 62 | // } |
| 63 | // en1, err := net.InterfaceByIndex(911) |
| 64 | // if err != nil { |
| 65 | // // error handling |
| 66 | // } |
| 67 | // group := net.IPv4(224, 0, 0, 250) |
| 68 | // |
| 69 | // First, an application listens to an appropriate address with an |
| 70 | // appropriate service port. |
| 71 | // |
| 72 | // c, err := net.ListenPacket("udp4", "0.0.0.0:1024") |
| 73 | // if err != nil { |
| 74 | // // error handling |
| 75 | // } |
| 76 | // defer c.Close() |
| 77 | // |
| 78 | // Second, the application joins groups, starts listening to the |
| 79 | // group addresses on the specified network interfaces. Note that |
| 80 | // the service port for transport layer protocol does not matter with |
| 81 | // this operation as joining groups affects only network and link |
| 82 | // layer protocols, such as IPv4 and Ethernet. |
| 83 | // |
| 84 | // p := ipv4.NewPacketConn(c) |
| 85 | // err = p.JoinGroup(en0, &net.UDPAddr{IP: group}) |
| 86 | // if err != nil { |
| 87 | // // error handling |
| 88 | // } |
| 89 | // err = p.JoinGroup(en1, &net.UDPAddr{IP: group}) |
| 90 | // if err != nil { |
| 91 | // // error handling |
| 92 | // } |
| 93 | // |
| 94 | // The application might set per packet control message transmissions |
| 95 | // between the protocol stack within the kernel. When the application |
| 96 | // needs a destination address on an incoming packet, |
| 97 | // SetControlMessage of ipv4.PacketConn is used to enable control |
| 98 | // message transmissons. |
| 99 | // |
| 100 | // err = p.SetControlMessage(ipv4.FlagDst, true) |
| 101 | // if 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 { |
Mikio Hara | 2832339 | 2012-12-21 13:18:56 +0900 | [diff] [blame] | 111 | // n, cm, src, err := p.ReadFrom(b) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 112 | // if err != nil { |
| 113 | // // error handling |
| 114 | // } |
| 115 | // if cm.Dst.IsMulticast() { |
| 116 | // if cm.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 | // |
Mikio Hara | 8108b4b | 2013-06-07 14:52:58 +0900 | [diff] [blame] | 126 | // p.SetTOS(DiffServCS0) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 127 | // p.SetTTL(16) |
Mikio Hara | 2832339 | 2012-12-21 13:18:56 +0900 | [diff] [blame] | 128 | // _, err = p.WriteTo(data, nil, src) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 129 | // if err != nil { |
| 130 | // // error handling |
| 131 | // } |
| 132 | // dst := &net.UDPAddr{IP: group, Port: 1024} |
| 133 | // for _, ifi := range []*net.Interface{en0, en1} { |
| 134 | // err := p.SetMulticastInterface(ifi) |
| 135 | // if err != nil { |
| 136 | // // error handling |
| 137 | // } |
| 138 | // p.SetMulticastTTL(2) |
Mikio Hara | 2832339 | 2012-12-21 13:18:56 +0900 | [diff] [blame] | 139 | // _, err = p.WriteTo(data, nil, dst) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 140 | // if err != nil { |
| 141 | // // error handling |
| 142 | // } |
| 143 | // } |
| 144 | // } |
| 145 | // |
| 146 | // |
| 147 | // More multicasting |
| 148 | // |
| 149 | // An application that uses PacketConn or RawConn might join the |
| 150 | // multiple group addresses. For example, a UDP listener with port |
| 151 | // 1024 might join two different groups across over two different |
| 152 | // network interfaces by using: |
| 153 | // |
| 154 | // c, err := net.ListenPacket("udp4", "0.0.0.0:1024") |
| 155 | // if err != nil { |
| 156 | // // error handling |
| 157 | // } |
| 158 | // defer c.Close() |
| 159 | // p := ipv4.NewPacketConn(c) |
| 160 | // err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}) |
| 161 | // if err != nil { |
| 162 | // // error handling |
| 163 | // } |
| 164 | // err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}) |
| 165 | // if err != nil { |
| 166 | // // error handling |
| 167 | // } |
| 168 | // err = p.JoinGroup(en1, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}) |
| 169 | // if err != nil { |
| 170 | // // error handling |
| 171 | // } |
| 172 | // |
| 173 | // It is possible for multiple UDP listeners that listen on the same |
| 174 | // UDP port to join the same group address. The net package will |
| 175 | // provide a socket that listens to a wildcard address with reusable |
| 176 | // UDP port when an appropriate multicast address prefix is passed to |
| 177 | // the net.ListenPacket or net.ListenUDP. |
| 178 | // |
| 179 | // c1, err := net.ListenPacket("udp4", "224.0.0.0:1024") |
| 180 | // if err != nil { |
| 181 | // // error handling |
| 182 | // } |
| 183 | // defer c1.Close() |
| 184 | // c2, err := net.ListenPacket("udp4", "224.0.0.0:1024") |
| 185 | // if err != nil { |
| 186 | // // error handling |
| 187 | // } |
| 188 | // defer c2.Close() |
| 189 | // p1 := ipv4.NewPacketConn(c1) |
| 190 | // err = p1.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}) |
| 191 | // if err != nil { |
| 192 | // // error handling |
| 193 | // } |
| 194 | // p2 := ipv4.NewPacketConn(c2) |
| 195 | // err = p2.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}) |
| 196 | // if err != nil { |
| 197 | // // error handling |
| 198 | // } |
| 199 | // |
| 200 | // Also it is possible for the application to leave or rejoin a |
| 201 | // multicast group on the network interface. |
| 202 | // |
| 203 | // err = p.LeaveGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}) |
| 204 | // if err != nil { |
| 205 | // // error handling |
| 206 | // } |
| 207 | // err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 250)}) |
| 208 | // if err != nil { |
| 209 | // // error handling |
| 210 | // } |
| 211 | package ipv4 |