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.

sockcmsg_unix.go 2.6KB

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