doc.go 7.8 KB

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