sockcmsg_unix.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. func socketControlMessageHeaderAndData(b []byte) (*Cmsghdr, []byte, error) {
  46. h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
  47. if h.Len < SizeofCmsghdr || uint64(h.Len) > uint64(len(b)) {
  48. return nil, nil, EINVAL
  49. }
  50. return h, b[cmsgAlignOf(SizeofCmsghdr):h.Len], nil
  51. }
  52. // UnixRights encodes a set of open file descriptors into a socket
  53. // control message for sending to another process.
  54. func UnixRights(fds ...int) []byte {
  55. datalen := len(fds) * 4
  56. b := make([]byte, CmsgSpace(datalen))
  57. h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
  58. h.Level = SOL_SOCKET
  59. h.Type = SCM_RIGHTS
  60. h.SetLen(CmsgLen(datalen))
  61. for i, fd := range fds {
  62. *(*int32)(h.data(4 * uintptr(i))) = int32(fd)
  63. }
  64. return b
  65. }
  66. // ParseUnixRights decodes a socket control message that contains an
  67. // integer array of open file descriptors from another process.
  68. func ParseUnixRights(m *SocketControlMessage) ([]int, error) {
  69. if m.Header.Level != SOL_SOCKET {
  70. return nil, EINVAL
  71. }
  72. if m.Header.Type != SCM_RIGHTS {
  73. return nil, EINVAL
  74. }
  75. fds := make([]int, len(m.Data)>>2)
  76. for i, j := 0, 0; i < len(m.Data); i += 4 {
  77. fds[j] = int(*(*int32)(unsafe.Pointer(&m.Data[i])))
  78. j++
  79. }
  80. return fds, nil
  81. }