You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

108 lines
3.2 KiB

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