doc.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // Package ipv4 implements IP-level socket options for the Internet
  5. // Protocol version 4.
  6. //
  7. // The package provides IP-level socket options that allow
  8. // manipulation of IPv4 facilities. The IPv4 and basic host
  9. // requirements for IPv4 are defined in RFC 791, RFC 1112 and RFC
  10. // 1122.
  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 IPv4 transport. When a single TCP connection carrying
  18. // a data flow of multiple packets needs to indicate the flow is
  19. // important, ipv4.Conn is used to set the type-of-service field on
  20. // the IPv4 header for each packet.
  21. //
  22. // ln, err := net.Listen("tcp4", "0.0.0.0: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 := ipv4.NewConn(c).SetTOS(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. // IPv4 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.IPv4(224, 0, 0, 250)
  65. //
  66. // First, an application listens to an appropriate address with an
  67. // appropriate service port.
  68. //
  69. // c, err := net.ListenPacket("udp4", "0.0.0.0: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 IPv4 and Ethernet.
  80. //
  81. // p := ipv4.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 ipv4.PacketConn is used to enable control
  93. // message transmissons.
  94. //
  95. // if err := p.SetControlMessage(ipv4.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, cm, src, err := p.ReadFrom(b)
  106. // if err != nil {
  107. // // error handling
  108. // }
  109. // if cm.Dst.IsMulticast() {
  110. // if cm.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.SetTOS(DiffServCS0)
  121. // p.SetTTL(16)
  122. // if _, err := p.WriteTo(data, nil, src); err != nil {
  123. // // error handling
  124. // }
  125. // dst := &net.UDPAddr{IP: group, Port: 1024}
  126. // for _, ifi := range []*net.Interface{en0, en1} {
  127. // if err := p.SetMulticastInterface(ifi); err != nil {
  128. // // error handling
  129. // }
  130. // p.SetMulticastTTL(2)
  131. // if _, err := p.WriteTo(data, nil, dst); err != nil {
  132. // // error handling
  133. // }
  134. // }
  135. // }
  136. //
  137. //
  138. // More multicasting
  139. //
  140. // An application that uses PacketConn or RawConn may join multiple
  141. // multicast groups. For example, a UDP listener with port 1024 might
  142. // join two different groups across over two different network
  143. // interfaces by using:
  144. //
  145. // c, err := net.ListenPacket("udp4", "0.0.0.0:1024")
  146. // if err != nil {
  147. // // error handling
  148. // }
  149. // defer c.Close()
  150. // p := ipv4.NewPacketConn(c)
  151. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
  152. // // error handling
  153. // }
  154. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}); err != nil {
  155. // // error handling
  156. // }
  157. // if err := p.JoinGroup(en1, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}); err != nil {
  158. // // error handling
  159. // }
  160. //
  161. // It is possible for multiple UDP listeners that listen on the same
  162. // UDP port to join the same multicast group. The net package will
  163. // provide a socket that listens to a wildcard address with reusable
  164. // UDP port when an appropriate multicast address prefix is passed to
  165. // the net.ListenPacket or net.ListenUDP.
  166. //
  167. // c1, err := net.ListenPacket("udp4", "224.0.0.0:1024")
  168. // if err != nil {
  169. // // error handling
  170. // }
  171. // defer c1.Close()
  172. // c2, err := net.ListenPacket("udp4", "224.0.0.0:1024")
  173. // if err != nil {
  174. // // error handling
  175. // }
  176. // defer c2.Close()
  177. // p1 := ipv4.NewPacketConn(c1)
  178. // if err := p1.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
  179. // // error handling
  180. // }
  181. // p2 := ipv4.NewPacketConn(c2)
  182. // if err := p2.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
  183. // // error handling
  184. // }
  185. //
  186. // Also it is possible for the application to leave or rejoin a
  187. // multicast group on the network interface.
  188. //
  189. // if err := p.LeaveGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
  190. // // error handling
  191. // }
  192. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 250)}); err != nil {
  193. // // error handling
  194. // }
  195. package ipv4