sockcmsg_unix.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2011 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. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
  5. // Socket control messages
  6. package unix
  7. import (
  8. "unsafe"
  9. )
  10. // CmsgLen returns the value to store in the Len field of the Cmsghdr
  11. // structure, taking into account any necessary alignment.
  12. func CmsgLen(datalen int) int {
  13. return cmsgAlignOf(SizeofCmsghdr) + datalen
  14. }
  15. // CmsgSpace returns the number of bytes an ancillary element with
  16. // payload of the passed data length occupies.
  17. func CmsgSpace(datalen int) int {
  18. return cmsgAlignOf(SizeofCmsghdr) + cmsgAlignOf(datalen)
  19. }
  20. func (h *Cmsghdr) data(offset uintptr) unsafe.Pointer {
  21. return unsafe.Pointer(uintptr(unsafe.Pointer(h)) + uintptr(cmsgAlignOf(SizeofCmsghdr)) + offset)
  22. }
  23. // SocketControlMessage represents a socket control message.
  24. type SocketControlMessage struct {
  25. Header Cmsghdr
  26. Data []byte
  27. }
  28. // ParseSocketControlMessage parses b as an array of socket control
  29. // messages.
  30. func ParseSocketControlMessage(b []byte) ([]SocketControlMessage, error) {
  31. var msgs []SocketControlMessage
  32. i := 0
  33. for i+CmsgLen(0) <= len(b) {
  34. h, dbuf, err := socketControlMessageHeaderAndData(b[i:])
  35. if err != nil {
  36. return nil, err
  37. }
  38. m := SocketControlMessage{Header: *h, Data: dbuf}
  39. msgs = append(msgs, m)
  40. i += cmsgAlignOf(int(h.Len))
  41. }
  42. return msgs, nil
  43. }
  44. // ParseOneSocketControlMessage parses a single socket control message from b, returning the message header,
  45. // message data (a slice of b), and the remainder of b after that single message.
  46. // When there are no remaining messages, len(remainder) == 0.
  47. func ParseOneSocketControlMessage(b []byte) (hdr Cmsghdr, data []byte, remainder []byte, err error) {
  48. h, dbuf, err := socketControlMessageHeaderAndData(b)
  49. if err != nil {
  50. return Cmsghdr{}, nil, nil, err
  51. }
  52. if i := cmsgAlignOf(int(h.Len)); i < len(b) {
  53. remainder = b[i:]
  54. }
  55. return *h, dbuf, remainder, nil
  56. }
  57. func socketControlMessageHeaderAndData(b []byte) (*Cmsghdr, []byte, error) {
  58. h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
  59. if h.Len < SizeofCmsghdr || uint64(h.Len) > uint64(len(b)) {
  60. return nil, nil, EINVAL
  61. }
  62. return h, b[cmsgAlignOf(SizeofCmsghdr):h.Len], nil
  63. }
  64. // UnixRights encodes a set of open file descriptors into a socket
  65. // control message for sending to another process.
  66. func UnixRights(fds ...int) []byte {
  67. datalen := len(fds) * 4
  68. b := make([]byte, CmsgSpace(datalen))
  69. h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
  70. h.Level = SOL_SOCKET
  71. h.Type = SCM_RIGHTS
  72. h.SetLen(CmsgLen(datalen))
  73. for i, fd := range fds {
  74. *(*int32)(h.data(4 * uintptr(i))) = int32(fd)
  75. }
  76. return b
  77. }
  78. // ParseUnixRights decodes a socket control message that contains an
  79. // integer array of open file descriptors from another process.
  80. func ParseUnixRights(m *SocketControlMessage) ([]int, error) {
  81. if m.Header.Level != SOL_SOCKET {
  82. return nil, EINVAL
  83. }
  84. if m.Header.Type != SCM_RIGHTS {
  85. return nil, EINVAL
  86. }
  87. fds := make([]int, len(m.Data)>>2)
  88. for i, j := 0, 0; i < len(m.Data); i += 4 {
  89. fds[j] = int(*(*int32)(unsafe.Pointer(&m.Data[i])))
  90. j++
  91. }
  92. return fds, nil
  93. }