doc.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. // err := ipv4.NewConn(c).SetTOS(DiffServAF11)
  39. // if err != nil {
  40. // // error handling
  41. // }
  42. // _, err = c.Write(data)
  43. // if err != nil {
  44. // // error handling
  45. // }
  46. // }(c)
  47. // }
  48. //
  49. //
  50. // Multicasting
  51. //
  52. // The options for multicasting are available for net.UDPConn and
  53. // net.IPconn which are created as network connections that use the
  54. // IPv4 transport. A few network facilities must be prepared before
  55. // you begin multicasting, at a minimum joining network interfaces and
  56. // group addresses.
  57. //
  58. // en0, err := net.InterfaceByName("en0")
  59. // if err != nil {
  60. // // error handling
  61. // }
  62. // en1, err := net.InterfaceByIndex(911)
  63. // if err != nil {
  64. // // error handling
  65. // }
  66. // group := net.IPv4(224, 0, 0, 250)
  67. //
  68. // First, an application listens to an appropriate address with an
  69. // appropriate service port.
  70. //
  71. // c, err := net.ListenPacket("udp4", "0.0.0.0:1024")
  72. // if err != nil {
  73. // // error handling
  74. // }
  75. // defer c.Close()
  76. //
  77. // Second, the application joins groups, starts listening to the
  78. // group addresses on the specified network interfaces. Note that
  79. // the service port for transport layer protocol does not matter with
  80. // this operation as joining groups affects only network and link
  81. // layer protocols, such as IPv4 and Ethernet.
  82. //
  83. // p := ipv4.NewPacketConn(c)
  84. // err = p.JoinGroup(en0, &net.UDPAddr{IP: group})
  85. // if err != nil {
  86. // // error handling
  87. // }
  88. // err = p.JoinGroup(en1, &net.UDPAddr{IP: group})
  89. // if 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 ipv4.PacketConn is used to enable control
  97. // message transmissons.
  98. //
  99. // err = p.SetControlMessage(ipv4.FlagDst, true)
  100. // if err != nil {
  101. // // error handling
  102. // }
  103. //
  104. // The application could identify whether the received packets are
  105. // of interest by using the control message that contains the
  106. // destination address of the received packet.
  107. //
  108. // b := make([]byte, 1500)
  109. // for {
  110. // n, cm, src, err := p.ReadFrom(b)
  111. // if err != nil {
  112. // // error handling
  113. // }
  114. // if cm.Dst.IsMulticast() {
  115. // if cm.Dst.Equal(group)
  116. // // joined group, do something
  117. // } else {
  118. // // unknown group, discard
  119. // continue
  120. // }
  121. // }
  122. //
  123. // The application can also send both unicast and multicast packets.
  124. //
  125. // p.SetTOS(DiffServCS0)
  126. // p.SetTTL(16)
  127. // _, err = p.WriteTo(data, nil, src)
  128. // if err != nil {
  129. // // error handling
  130. // }
  131. // dst := &net.UDPAddr{IP: group, Port: 1024}
  132. // for _, ifi := range []*net.Interface{en0, en1} {
  133. // err := p.SetMulticastInterface(ifi)
  134. // if err != nil {
  135. // // error handling
  136. // }
  137. // p.SetMulticastTTL(2)
  138. // _, err = p.WriteTo(data, nil, dst)
  139. // if err != nil {
  140. // // error handling
  141. // }
  142. // }
  143. // }
  144. //
  145. //
  146. // More multicasting
  147. //
  148. // An application that uses PacketConn or RawConn might join the
  149. // multiple group addresses. For example, a UDP listener with port
  150. // 1024 might join two different groups across over two different
  151. // network interfaces by using:
  152. //
  153. // c, err := net.ListenPacket("udp4", "0.0.0.0:1024")
  154. // if err != nil {
  155. // // error handling
  156. // }
  157. // defer c.Close()
  158. // p := ipv4.NewPacketConn(c)
  159. // err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)})
  160. // if err != nil {
  161. // // error handling
  162. // }
  163. // err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)})
  164. // if err != nil {
  165. // // error handling
  166. // }
  167. // err = p.JoinGroup(en1, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)})
  168. // if err != nil {
  169. // // error handling
  170. // }
  171. //
  172. // It is possible for multiple UDP listeners that listen on the same
  173. // UDP port to join the same group address. The net package will
  174. // provide a socket that listens to a wildcard address with reusable
  175. // UDP port when an appropriate multicast address prefix is passed to
  176. // the net.ListenPacket or net.ListenUDP.
  177. //
  178. // c1, err := net.ListenPacket("udp4", "224.0.0.0:1024")
  179. // if err != nil {
  180. // // error handling
  181. // }
  182. // defer c1.Close()
  183. // c2, err := net.ListenPacket("udp4", "224.0.0.0:1024")
  184. // if err != nil {
  185. // // error handling
  186. // }
  187. // defer c2.Close()
  188. // p1 := ipv4.NewPacketConn(c1)
  189. // err = p1.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)})
  190. // if err != nil {
  191. // // error handling
  192. // }
  193. // p2 := ipv4.NewPacketConn(c2)
  194. // err = p2.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)})
  195. // if err != nil {
  196. // // error handling
  197. // }
  198. //
  199. // Also it is possible for the application to leave or rejoin a
  200. // multicast group on the network interface.
  201. //
  202. // err = p.LeaveGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)})
  203. // if err != nil {
  204. // // error handling
  205. // }
  206. // err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 250)})
  207. // if err != nil {
  208. // // error handling
  209. // }
  210. package ipv4