doc.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.
  9. //
  10. // The IPv4 protocol and basic host requirements for IPv4 are defined
  11. // in RFC 791 and RFC 1122.
  12. // Host extensions for multicasting and socket interface extensions
  13. // for multicast source filters are defined in RFC 1112 and RFC 3678.
  14. // IGMPv1, IGMPv2 and IGMPv3 are defined in RFC 1112, RFC 2236 and RFC
  15. // 3376.
  16. // Source-specific multicast is defined in RFC 4607.
  17. //
  18. //
  19. // Unicasting
  20. //
  21. // The options for unicasting are available for net.TCPConn,
  22. // net.UDPConn and net.IPConn which are created as network connections
  23. // that use the IPv4 transport. When a single TCP connection carrying
  24. // a data flow of multiple packets needs to indicate the flow is
  25. // important, Conn is used to set the type-of-service field on the
  26. // IPv4 header for each packet.
  27. //
  28. // ln, err := net.Listen("tcp4", "0.0.0.0:1024")
  29. // if err != nil {
  30. // // error handling
  31. // }
  32. // defer ln.Close()
  33. // for {
  34. // c, err := ln.Accept()
  35. // if err != nil {
  36. // // error handling
  37. // }
  38. // go func(c net.Conn) {
  39. // defer c.Close()
  40. //
  41. // The outgoing packets will be labeled DiffServ assured forwarding
  42. // class 1 low drop precedence, known as AF11 packets.
  43. //
  44. // if err := ipv4.NewConn(c).SetTOS(0x28); err != nil {
  45. // // error handling
  46. // }
  47. // if _, err := c.Write(data); err != nil {
  48. // // error handling
  49. // }
  50. // }(c)
  51. // }
  52. //
  53. //
  54. // Multicasting
  55. //
  56. // The options for multicasting are available for net.UDPConn and
  57. // net.IPConn which are created as network connections that use the
  58. // IPv4 transport. A few network facilities must be prepared before
  59. // you begin multicasting, at a minimum joining network interfaces and
  60. // multicast groups.
  61. //
  62. // en0, err := net.InterfaceByName("en0")
  63. // if err != nil {
  64. // // error handling
  65. // }
  66. // en1, err := net.InterfaceByIndex(911)
  67. // if err != nil {
  68. // // error handling
  69. // }
  70. // group := net.IPv4(224, 0, 0, 250)
  71. //
  72. // First, an application listens to an appropriate address with an
  73. // appropriate service port.
  74. //
  75. // c, err := net.ListenPacket("udp4", "0.0.0.0:1024")
  76. // if err != nil {
  77. // // error handling
  78. // }
  79. // defer c.Close()
  80. //
  81. // Second, the application joins multicast groups, starts listening to
  82. // the groups on the specified network interfaces. Note that the
  83. // service port for transport layer protocol does not matter with this
  84. // operation as joining groups affects only network and link layer
  85. // protocols, such as IPv4 and Ethernet.
  86. //
  87. // p := ipv4.NewPacketConn(c)
  88. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: group}); err != nil {
  89. // // error handling
  90. // }
  91. // if err := p.JoinGroup(en1, &net.UDPAddr{IP: group}); err != nil {
  92. // // error handling
  93. // }
  94. //
  95. // The application might set per packet control message transmissions
  96. // between the protocol stack within the kernel. When the application
  97. // needs a destination address on an incoming packet,
  98. // SetControlMessage of PacketConn is used to enable control message
  99. // transmissions.
  100. //
  101. // if err := p.SetControlMessage(ipv4.FlagDst, true); 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 {
  111. // n, cm, src, err := p.ReadFrom(b)
  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. //
  126. // p.SetTOS(0x0)
  127. // p.SetTTL(16)
  128. // if _, err := p.WriteTo(data, nil, src); err != nil {
  129. // // error handling
  130. // }
  131. // dst := &net.UDPAddr{IP: group, Port: 1024}
  132. // for _, ifi := range []*net.Interface{en0, en1} {
  133. // if err := p.SetMulticastInterface(ifi); err != nil {
  134. // // error handling
  135. // }
  136. // p.SetMulticastTTL(2)
  137. // if _, err := p.WriteTo(data, nil, dst); err != nil {
  138. // // error handling
  139. // }
  140. // }
  141. // }
  142. //
  143. //
  144. // More multicasting
  145. //
  146. // An application that uses PacketConn or RawConn may join multiple
  147. // multicast groups. For example, a UDP listener with port 1024 might
  148. // join two different groups across over two different network
  149. // interfaces by using:
  150. //
  151. // c, err := net.ListenPacket("udp4", "0.0.0.0:1024")
  152. // if err != nil {
  153. // // error handling
  154. // }
  155. // defer c.Close()
  156. // p := ipv4.NewPacketConn(c)
  157. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
  158. // // error handling
  159. // }
  160. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}); err != nil {
  161. // // error handling
  162. // }
  163. // if err := p.JoinGroup(en1, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}); err != nil {
  164. // // error handling
  165. // }
  166. //
  167. // It is possible for multiple UDP listeners that listen on the same
  168. // UDP port to join the same multicast group. The net package will
  169. // provide a socket that listens to a wildcard address with reusable
  170. // UDP port when an appropriate multicast address prefix is passed to
  171. // the net.ListenPacket or net.ListenUDP.
  172. //
  173. // c1, err := net.ListenPacket("udp4", "224.0.0.0:1024")
  174. // if err != nil {
  175. // // error handling
  176. // }
  177. // defer c1.Close()
  178. // c2, err := net.ListenPacket("udp4", "224.0.0.0:1024")
  179. // if err != nil {
  180. // // error handling
  181. // }
  182. // defer c2.Close()
  183. // p1 := ipv4.NewPacketConn(c1)
  184. // if err := p1.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
  185. // // error handling
  186. // }
  187. // p2 := ipv4.NewPacketConn(c2)
  188. // if err := p2.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
  189. // // error handling
  190. // }
  191. //
  192. // Also it is possible for the application to leave or rejoin a
  193. // multicast group on the network interface.
  194. //
  195. // if err := p.LeaveGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil {
  196. // // error handling
  197. // }
  198. // if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 250)}); err != nil {
  199. // // error handling
  200. // }
  201. //
  202. //
  203. // Source-specific multicasting
  204. //
  205. // An application that uses PacketConn or RawConn on IGMPv3 supported
  206. // platform is able to join source-specific multicast groups.
  207. // The application may use JoinSourceSpecificGroup and
  208. // LeaveSourceSpecificGroup for the operation known as "include" mode,
  209. //
  210. // ssmgroup := net.UDPAddr{IP: net.IPv4(232, 7, 8, 9)}
  211. // ssmsource := net.UDPAddr{IP: net.IPv4(192, 168, 0, 1)}
  212. // if err := p.JoinSourceSpecificGroup(en0, &ssmgroup, &ssmsource); err != nil {
  213. // // error handling
  214. // }
  215. // if err := p.LeaveSourceSpecificGroup(en0, &ssmgroup, &ssmsource); err != nil {
  216. // // error handling
  217. // }
  218. //
  219. // or JoinGroup, ExcludeSourceSpecificGroup,
  220. // IncludeSourceSpecificGroup and LeaveGroup for the operation known
  221. // as "exclude" mode.
  222. //
  223. // exclsource := net.UDPAddr{IP: net.IPv4(192, 168, 0, 254)}
  224. // if err := p.JoinGroup(en0, &ssmgroup); err != nil {
  225. // // error handling
  226. // }
  227. // if err := p.ExcludeSourceSpecificGroup(en0, &ssmgroup, &exclsource); err != nil {
  228. // // error handling
  229. // }
  230. // if err := p.LeaveGroup(en0, &ssmgroup); err != nil {
  231. // // error handling
  232. // }
  233. //
  234. // Note that it depends on each platform implementation what happens
  235. // when an application which runs on IGMPv3 unsupported platform uses
  236. // JoinSourceSpecificGroup and LeaveSourceSpecificGroup.
  237. // In general the platform tries to fall back to conversations using
  238. // IGMPv1 or IGMPv2 and starts to listen to multicast traffic.
  239. // In the fallback case, ExcludeSourceSpecificGroup and
  240. // IncludeSourceSpecificGroup may return an error.
  241. package ipv4 // import "golang.org/x/net/ipv4"
  242. // BUG(mikio): This package is not implemented on JS, NaCl and Plan 9.