doc.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, 1112 and 1122. A
  10. // series of RFCs 2474, 2475, 2597, 2598 and 3168 describe how to use
  11. // the type-of-service field in a DiffServ, differentiated services
  12. // environment.
  13. //
  14. //
  15. // Unicasting
  16. //
  17. // The options for unicasting are available for net.TCPConn,
  18. // net.UDPConn and net.IPConn which are created as network connections
  19. // that use the IPv4 transport. When a single TCP connection carrying
  20. // a data flow of multiple packets needs to indicate the flow is
  21. // important, ipv4.Conn is used to set the type-of-service field on
  22. // the IPv4 header for each packet.
  23. //
  24. // ln, err := net.Listen("tcp4", "0.0.0.0:1024")
  25. // if err != nil {
  26. // // error handling
  27. // }
  28. // defer ln.Close()
  29. // for {
  30. // c, err := ln.Accept()
  31. // if err != nil {
  32. // // error handling
  33. // }
  34. // go func(c net.Conn) {
  35. // defer c.Close()
  36. //
  37. // The outgoing packets will be labeled DiffServ assured forwarding
  38. // class 1 low drop precedence, as known as AF11 packets.
  39. //
  40. // err := ipv4.NewConn(c).SetTOS(ipv4.DSCP_AF11)
  41. // if err != nil {
  42. // // error handling
  43. // }
  44. // _, err = c.Write(data)
  45. // if err != nil {
  46. // // error handling
  47. // }
  48. // }(c)
  49. // }
  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. // group addresses.
  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 groups, starts listening to the
  80. // group addresses on the specified network interfaces. Note that
  81. // the service port for transport layer protocol does not matter with
  82. // this operation as joining groups affects only network and link
  83. // layer protocols, such as IPv4 and Ethernet.
  84. //
  85. // p := ipv4.NewPacketConn(c)
  86. // err = p.JoinGroup(en0, &net.UDPAddr{IP: group})
  87. // if err != nil {
  88. // // error handling
  89. // }
  90. // err = p.JoinGroup(en1, &net.UDPAddr{IP: group})
  91. // if 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 ipv4.PacketConn is used to enable control
  99. // message transmissons.
  100. //
  101. // err = p.SetControlMessage(ipv4.FlagDst, true)
  102. // if err != nil {
  103. // // error handling
  104. // }
  105. //
  106. // The application could identify whether the received packets are
  107. // of interest by using the control message that contains the
  108. // destination address of the received packet.
  109. //
  110. // b := make([]byte, 1500)
  111. // for {
  112. // n, cm, src, err := p.Read(b)
  113. // if err != nil {
  114. // // error handling
  115. // }
  116. // if cm.Dst.IsMulticast() {
  117. // if cm.Dst.Equal(group)
  118. // // joined group, do something
  119. // } else {
  120. // // unknown group, discard
  121. // continue
  122. // }
  123. // }
  124. //
  125. // The application can also send both unicast and multicast packets.
  126. //
  127. // p.SetTOS(ipv4.DSCP_CS0)
  128. // p.SetTTL(16)
  129. // _, err = p.Write(data, nil, src)
  130. // if err != nil {
  131. // // error handling
  132. // }
  133. // dst := &net.UDPAddr{IP: group, Port: 1024}
  134. // for _, ifi := range []*net.Interface{en0, en1} {
  135. // err := p.SetMulticastInterface(ifi)
  136. // if err != nil {
  137. // // error handling
  138. // }
  139. // p.SetMulticastTTL(2)
  140. // _, err = p.Write(data, nil, dst)
  141. // if err != nil {
  142. // // error handling
  143. // }
  144. // }
  145. // }
  146. //
  147. //
  148. // More multicasting
  149. //
  150. // An application that uses PacketConn or RawConn might join the
  151. // multiple group addresses. For example, a UDP listener with port
  152. // 1024 might join two different groups across over two different
  153. // network interfaces by using:
  154. //
  155. // c, err := net.ListenPacket("udp4", "0.0.0.0:1024")
  156. // if err != nil {
  157. // // error handling
  158. // }
  159. // defer c.Close()
  160. // p := ipv4.NewPacketConn(c)
  161. // err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)})
  162. // if err != nil {
  163. // // error handling
  164. // }
  165. // err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)})
  166. // if err != nil {
  167. // // error handling
  168. // }
  169. // err = p.JoinGroup(en1, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)})
  170. // if err != nil {
  171. // // error handling
  172. // }
  173. //
  174. // It is possible for multiple UDP listeners that listen on the same
  175. // UDP port to join the same group address. The net package will
  176. // provide a socket that listens to a wildcard address with reusable
  177. // UDP port when an appropriate multicast address prefix is passed to
  178. // the net.ListenPacket or net.ListenUDP.
  179. //
  180. // c1, err := net.ListenPacket("udp4", "224.0.0.0:1024")
  181. // if err != nil {
  182. // // error handling
  183. // }
  184. // defer c1.Close()
  185. // c2, err := net.ListenPacket("udp4", "224.0.0.0:1024")
  186. // if err != nil {
  187. // // error handling
  188. // }
  189. // defer c2.Close()
  190. // p1 := ipv4.NewPacketConn(c1)
  191. // err = p1.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)})
  192. // if err != nil {
  193. // // error handling
  194. // }
  195. // p2 := ipv4.NewPacketConn(c2)
  196. // err = p2.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)})
  197. // if err != nil {
  198. // // error handling
  199. // }
  200. //
  201. // Also it is possible for the application to leave or rejoin a
  202. // multicast group on the network interface.
  203. //
  204. // err = p.LeaveGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)})
  205. // if err != nil {
  206. // // error handling
  207. // }
  208. // err = p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 250)})
  209. // if err != nil {
  210. // // error handling
  211. // }
  212. package ipv4