socket.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2017 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 socket provides a portable interface for socket system
  5. // calls.
  6. package socket // import "golang.org/x/net/internal/socket"
  7. import (
  8. "errors"
  9. "net"
  10. "runtime"
  11. "unsafe"
  12. )
  13. var errNotImplemented = errors.New("not implemented on " + runtime.GOOS + "/" + runtime.GOARCH)
  14. // An Option represents a sticky socket option.
  15. type Option struct {
  16. Level int // level
  17. Name int // name; must be equal or greater than 1
  18. Len int // length of value in bytes; must be equal or greater than 1
  19. }
  20. // Get reads a value for the option from the kernel.
  21. // It returns the number of bytes written into b.
  22. func (o *Option) Get(c *Conn, b []byte) (int, error) {
  23. if o.Name < 1 || o.Len < 1 {
  24. return 0, errors.New("invalid option")
  25. }
  26. if len(b) < o.Len {
  27. return 0, errors.New("short buffer")
  28. }
  29. return o.get(c, b)
  30. }
  31. // GetInt returns an integer value for the option.
  32. //
  33. // The Len field of Option must be either 1 or 4.
  34. func (o *Option) GetInt(c *Conn) (int, error) {
  35. if o.Len != 1 && o.Len != 4 {
  36. return 0, errors.New("invalid option")
  37. }
  38. var b []byte
  39. var bb [4]byte
  40. if o.Len == 1 {
  41. b = bb[:1]
  42. } else {
  43. b = bb[:4]
  44. }
  45. n, err := o.get(c, b)
  46. if err != nil {
  47. return 0, err
  48. }
  49. if n != o.Len {
  50. return 0, errors.New("invalid option length")
  51. }
  52. if o.Len == 1 {
  53. return int(b[0]), nil
  54. }
  55. return int(NativeEndian.Uint32(b[:4])), nil
  56. }
  57. // Set writes the option and value to the kernel.
  58. func (o *Option) Set(c *Conn, b []byte) error {
  59. if o.Name < 1 || o.Len < 1 {
  60. return errors.New("invalid option")
  61. }
  62. if len(b) < o.Len {
  63. return errors.New("short buffer")
  64. }
  65. return o.set(c, b)
  66. }
  67. // SetInt writes the option and value to the kernel.
  68. //
  69. // The Len field of Option must be either 1 or 4.
  70. func (o *Option) SetInt(c *Conn, v int) error {
  71. if o.Len != 1 && o.Len != 4 {
  72. return errors.New("invalid option")
  73. }
  74. var b []byte
  75. if o.Len == 1 {
  76. b = []byte{byte(v)}
  77. } else {
  78. var bb [4]byte
  79. NativeEndian.PutUint32(bb[:o.Len], uint32(v))
  80. b = bb[:4]
  81. }
  82. return o.set(c, b)
  83. }
  84. // ControlMessageSpace returns the whole length of control message.
  85. func ControlMessageSpace(dataLen int) int {
  86. return controlMessageSpace(dataLen)
  87. }
  88. // A ControlMessage represents the head message in a stream of control
  89. // messages.
  90. //
  91. // A control message comprises of a header, data and a few padding
  92. // fields to conform to the interface to the kernel.
  93. //
  94. // See RFC 3542 for further information.
  95. type ControlMessage []byte
  96. // Data returns the data field of the control message at the head on
  97. // m.
  98. func (m ControlMessage) Data(dataLen int) []byte {
  99. l := controlHeaderLen()
  100. if len(m) < l || len(m) < l+dataLen {
  101. return nil
  102. }
  103. return m[l : l+dataLen]
  104. }
  105. // Next returns the control message at the next on m.
  106. //
  107. // Next works only for standard control messages.
  108. func (m ControlMessage) Next(dataLen int) ControlMessage {
  109. l := ControlMessageSpace(dataLen)
  110. if len(m) < l {
  111. return nil
  112. }
  113. return m[l:]
  114. }
  115. // MarshalHeader marshals the header fields of the control message at
  116. // the head on m.
  117. func (m ControlMessage) MarshalHeader(lvl, typ, dataLen int) error {
  118. if len(m) < controlHeaderLen() {
  119. return errors.New("short message")
  120. }
  121. h := (*cmsghdr)(unsafe.Pointer(&m[0]))
  122. h.set(controlMessageLen(dataLen), lvl, typ)
  123. return nil
  124. }
  125. // ParseHeader parses and returns the header fields of the control
  126. // message at the head on m.
  127. func (m ControlMessage) ParseHeader() (lvl, typ, dataLen int, err error) {
  128. l := controlHeaderLen()
  129. if len(m) < l {
  130. return 0, 0, 0, errors.New("short message")
  131. }
  132. h := (*cmsghdr)(unsafe.Pointer(&m[0]))
  133. return h.lvl(), h.typ(), int(uint64(h.len()) - uint64(l)), nil
  134. }
  135. // Marshal marshals the control message at the head on m, and returns
  136. // the next control message.
  137. func (m ControlMessage) Marshal(lvl, typ int, data []byte) (ControlMessage, error) {
  138. l := len(data)
  139. if len(m) < ControlMessageSpace(l) {
  140. return nil, errors.New("short message")
  141. }
  142. h := (*cmsghdr)(unsafe.Pointer(&m[0]))
  143. h.set(controlMessageLen(l), lvl, typ)
  144. if l > 0 {
  145. copy(m.Data(l), data)
  146. }
  147. return m.Next(l), nil
  148. }
  149. // Parse parses m as a single or multiple control messages.
  150. //
  151. // Parse works for both standard and compatible messages.
  152. func (m ControlMessage) Parse() ([]ControlMessage, error) {
  153. var ms []ControlMessage
  154. for len(m) >= controlHeaderLen() {
  155. h := (*cmsghdr)(unsafe.Pointer(&m[0]))
  156. l := h.len()
  157. if l <= 0 {
  158. return nil, errors.New("invalid header length")
  159. }
  160. if uint64(l) < uint64(controlHeaderLen()) {
  161. return nil, errors.New("invalid message length")
  162. }
  163. if uint64(l) > uint64(len(m)) {
  164. return nil, errors.New("short buffer")
  165. }
  166. // On message reception:
  167. //
  168. // |<- ControlMessageSpace --------------->|
  169. // |<- controlMessageLen ---------->| |
  170. // |<- controlHeaderLen ->| | |
  171. // +---------------+------+---------+------+
  172. // | Header | PadH | Data | PadD |
  173. // +---------------+------+---------+------+
  174. //
  175. // On compatible message reception:
  176. //
  177. // | ... |<- controlMessageLen ----------->|
  178. // | ... |<- controlHeaderLen ->| |
  179. // +-----+---------------+------+----------+
  180. // | ... | Header | PadH | Data |
  181. // +-----+---------------+------+----------+
  182. ms = append(ms, ControlMessage(m[:l]))
  183. ll := l - controlHeaderLen()
  184. if len(m) >= ControlMessageSpace(ll) {
  185. m = m[ControlMessageSpace(ll):]
  186. } else {
  187. m = m[controlMessageLen(ll):]
  188. }
  189. }
  190. return ms, nil
  191. }
  192. // NewControlMessage returns a new stream of control messages.
  193. func NewControlMessage(dataLen []int) ControlMessage {
  194. var l int
  195. for i := range dataLen {
  196. l += ControlMessageSpace(dataLen[i])
  197. }
  198. return make([]byte, l)
  199. }
  200. // A Message represents an IO message.
  201. type Message struct {
  202. // When writing, the Buffers field must contain at least one
  203. // byte to write.
  204. // When reading, the Buffers field will always contain a byte
  205. // to read.
  206. Buffers [][]byte
  207. // OOB contains protocol-specific control or miscellaneous
  208. // ancillary data known as out-of-band data.
  209. OOB []byte
  210. // Addr specifies a destination address when writing.
  211. // It can be nil when the underlying protocol of the raw
  212. // connection uses connection-oriented communication.
  213. // After a successful read, it may contain the source address
  214. // on the received packet.
  215. Addr net.Addr
  216. N int // # of bytes read or written from/to Buffers
  217. NN int // # of bytes read or written from/to OOB
  218. Flags int // protocol-specific information on the received message
  219. }
  220. // RecvMsg wraps recvmsg system call.
  221. //
  222. // The provided flags is a set of platform-dependent flags, such as
  223. // syscall.MSG_PEEK.
  224. func (c *Conn) RecvMsg(m *Message, flags int) error {
  225. return c.recvMsg(m, flags)
  226. }
  227. // SendMsg wraps sendmsg system call.
  228. //
  229. // The provided flags is a set of platform-dependent flags, such as
  230. // syscall.MSG_DONTROUTE.
  231. func (c *Conn) SendMsg(m *Message, flags int) error {
  232. return c.sendMsg(m, flags)
  233. }
  234. // RecvMsgs wraps recvmmsg system call.
  235. //
  236. // It returns the number of processed messages.
  237. //
  238. // The provided flags is a set of platform-dependent flags, such as
  239. // syscall.MSG_PEEK.
  240. //
  241. // Only Linux supports this.
  242. func (c *Conn) RecvMsgs(ms []Message, flags int) (int, error) {
  243. return c.recvMsgs(ms, flags)
  244. }
  245. // SendMsgs wraps sendmmsg system call.
  246. //
  247. // It returns the number of processed messages.
  248. //
  249. // The provided flags is a set of platform-dependent flags, such as
  250. // syscall.MSG_DONTROUTE.
  251. //
  252. // Only Linux supports this.
  253. func (c *Conn) SendMsgs(ms []Message, flags int) (int, error) {
  254. return c.sendMsgs(ms, flags)
  255. }