Commit 080e6515 authored by Mikio Hara's avatar Mikio Hara

go.net/ipv4: update package documentation

- fix more multicasting section as pointed out in go.net/ipv6 review
- simplify example codes
- do word alignment; s/multicast group address/multicast group/

R=dave
CC=golang-dev
https://golang.org/cl/10322043
parent 23139109
...@@ -36,12 +36,10 @@ ...@@ -36,12 +36,10 @@
// The outgoing packets will be labeled DiffServ assured forwarding // The outgoing packets will be labeled DiffServ assured forwarding
// class 1 low drop precedence, as known as AF11 packets. // class 1 low drop precedence, as known as AF11 packets.
// //
// err := ipv4.NewConn(c).SetTOS(DiffServAF11) // if err := ipv4.NewConn(c).SetTOS(DiffServAF11); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// _, err = c.Write(data) // if _, err := c.Write(data); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// }(c) // }(c)
...@@ -54,7 +52,7 @@ ...@@ -54,7 +52,7 @@
// net.IPconn which are created as network connections that use the // net.IPconn which are created as network connections that use the
// IPv4 transport. A few network facilities must be prepared before // IPv4 transport. A few network facilities must be prepared before
// you begin multicasting, at a minimum joining network interfaces and // you begin multicasting, at a minimum joining network interfaces and
// group addresses. // multicast groups.
// //
// en0, err := net.InterfaceByName("en0") // en0, err := net.InterfaceByName("en0")
// if err != nil { // if err != nil {
...@@ -75,19 +73,17 @@ ...@@ -75,19 +73,17 @@
// } // }
// defer c.Close() // defer c.Close()
// //
// Second, the application joins groups, starts listening to the // Second, the application joins multicast groups, starts listening to
// group addresses on the specified network interfaces. Note that // the groups on the specified network interfaces. Note that the
// the service port for transport layer protocol does not matter with // service port for transport layer protocol does not matter with this
// this operation as joining groups affects only network and link // operation as joining groups affects only network and link layer
// layer protocols, such as IPv4 and Ethernet. // protocols, such as IPv4 and Ethernet.
// //
// p := ipv4.NewPacketConn(c) // p := ipv4.NewPacketConn(c)
// err = p.JoinGroup(en0, &net.UDPAddr{IP: group}) // if err := p.JoinGroup(en0, &net.UDPAddr{IP: group}); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// err = p.JoinGroup(en1, &net.UDPAddr{IP: group}) // if err := p.JoinGroup(en1, &net.UDPAddr{IP: group}); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// //
...@@ -97,8 +93,7 @@ ...@@ -97,8 +93,7 @@
// SetControlMessage of ipv4.PacketConn is used to enable control // SetControlMessage of ipv4.PacketConn is used to enable control
// message transmissons. // message transmissons.
// //
// err = p.SetControlMessage(ipv4.FlagDst, true) // if err := p.SetControlMessage(ipv4.FlagDst, true); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// //
...@@ -125,19 +120,16 @@ ...@@ -125,19 +120,16 @@
// //
// p.SetTOS(DiffServCS0) // p.SetTOS(DiffServCS0)
// p.SetTTL(16) // p.SetTTL(16)
// _, err = p.WriteTo(data, nil, src) // if _, err := p.WriteTo(data, nil, src); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// dst := &net.UDPAddr{IP: group, Port: 1024} // dst := &net.UDPAddr{IP: group, Port: 1024}
// for _, ifi := range []*net.Interface{en0, en1} { // for _, ifi := range []*net.Interface{en0, en1} {
// err := p.SetMulticastInterface(ifi) // if err := p.SetMulticastInterface(ifi); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// p.SetMulticastTTL(2) // p.SetMulticastTTL(2)
// _, err = p.WriteTo(data, nil, dst) // if _, err := p.WriteTo(data, nil, dst); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// } // }
...@@ -146,10 +138,10 @@ ...@@ -146,10 +138,10 @@
// //
// More multicasting // More multicasting
// //
// An application that uses PacketConn or RawConn might join the // An application that uses PacketConn or RawConn may join multiple
// multiple group addresses. For example, a UDP listener with port // multicast groups. For example, a UDP listener with port 1024 might
// 1024 might join two different groups across over two different // join two different groups across over two different network
// network interfaces by using: // interfaces by using:
// //
// c, err := net.ListenPacket("udp4", "0.0.0.0:1024") // c, err := net.ListenPacket("udp4", "0.0.0.0:1024")
// if err != nil { // if err != nil {
...@@ -157,21 +149,18 @@ ...@@ -157,21 +149,18 @@
// } // }
// defer c.Close() // defer c.Close()
// p := ipv4.NewPacketConn(c) // p := ipv4.NewPacketConn(c)
// err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}) // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}) // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// err = p.JoinGroup(en1, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}) // if err := p.JoinGroup(en1, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// //
// It is possible for multiple UDP listeners that listen on the same // It is possible for multiple UDP listeners that listen on the same
// UDP port to join the same group address. The net package will // UDP port to join the same multicast group. The net package will
// provide a socket that listens to a wildcard address with reusable // provide a socket that listens to a wildcard address with reusable
// UDP port when an appropriate multicast address prefix is passed to // UDP port when an appropriate multicast address prefix is passed to
// the net.ListenPacket or net.ListenUDP. // the net.ListenPacket or net.ListenUDP.
...@@ -187,25 +176,21 @@ ...@@ -187,25 +176,21 @@
// } // }
// defer c2.Close() // defer c2.Close()
// p1 := ipv4.NewPacketConn(c1) // p1 := ipv4.NewPacketConn(c1)
// err = p1.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}) // if err := p1.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// p2 := ipv4.NewPacketConn(c2) // p2 := ipv4.NewPacketConn(c2)
// err = p2.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}) // if err := p2.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// //
// Also it is possible for the application to leave or rejoin a // Also it is possible for the application to leave or rejoin a
// multicast group on the network interface. // multicast group on the network interface.
// //
// err = p.LeaveGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}) // if err := p.LeaveGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
// err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 250)}) // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 250)}); err != nil {
// if err != nil {
// // error handling // // error handling
// } // }
package ipv4 package ipv4
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment