sockcmsg_unix.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "runtime"
  9. "unsafe"
  10. )
  11. // Round the length of a raw sockaddr up to align it properly.
  12. func cmsgAlignOf(salen int) int {
  13. salign := SizeofPtr
  14. switch runtime.GOOS {
  15. case "aix":
  16. // There is no alignment on AIX.
  17. salign = 1
  18. case "darwin", "dragonfly", "solaris", "illumos":
  19. // NOTE: It seems like 64-bit Darwin, DragonFly BSD,
  20. // illumos, and Solaris kernels still require 32-bit
  21. // aligned access to network subsystem.
  22. if SizeofPtr == 8 {
  23. salign = 4
  24. }
  25. case "netbsd", "openbsd":
  26. // NetBSD and OpenBSD armv7 require 64-bit alignment.
  27. if runtime.GOARCH == "arm" {
  28. salign = 8
  29. }
  30. }
  31. return (salen + salign - 1) & ^(salign - 1)
  32. }
  33. // CmsgLen returns the value to store in the Len field of the Cmsghdr
  34. // structure, taking into account any necessary alignment.
  35. func CmsgLen(datalen int) int {
  36. return cmsgAlignOf(SizeofCmsghdr) + datalen
  37. }
  38. // CmsgSpace returns the number of bytes an ancillary element with
  39. // payload of the passed data length occupies.
  40. func CmsgSpace(datalen int) int {
  41. return cmsgAlignOf(SizeofCmsghdr) + cmsgAlignOf(datalen)
  42. }
  43. func cmsgData(h *Cmsghdr) unsafe.Pointer {
  44. return unsafe.Pointer(uintptr(unsafe.Pointer(h)) + uintptr(cmsgAlignOf(SizeofCmsghdr)))
  45. }
  46. // SocketControlMessage represents a socket control message.
  47. type SocketControlMessage struct {
  48. Header Cmsghdr
  49. Data []byte
  50. }
  51. // ParseSocketControlMessage parses b as an array of socket control
  52. // messages.
  53. func ParseSocketControlMessage(b []byte) ([]SocketControlMessage, error) {
  54. var msgs []SocketControlMessage
  55. i := 0
  56. for i+CmsgLen(0) <= len(b) {
  57. h, dbuf, err := socketControlMessageHeaderAndData(b[i:])
  58. if err != nil {
  59. return nil, err
  60. }
  61. m := SocketControlMessage{Header: *h, Data: dbuf}
  62. msgs = append(msgs, m)
  63. i += cmsgAlignOf(int(h.Len))
  64. }
  65. return msgs, nil
  66. }
  67. func socketControlMessageHeaderAndData(b []byte) (*Cmsghdr, []byte, error) {
  68. h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
  69. if h.Len < SizeofCmsghdr || uint64(h.Len) > uint64(len(b)) {
  70. return nil, nil, EINVAL
  71. }
  72. return h, b[cmsgAlignOf(SizeofCmsghdr):h.Len], nil
  73. }
  74. // UnixRights encodes a set of open file descriptors into a socket
  75. // control message for sending to another process.
  76. func UnixRights(fds ...int) []byte {
  77. datalen := len(fds) * 4
  78. b := make([]byte, CmsgSpace(datalen))
  79. h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
  80. h.Level = SOL_SOCKET
  81. h.Type = SCM_RIGHTS
  82. h.SetLen(CmsgLen(datalen))
  83. data := cmsgData(h)
  84. for _, fd := range fds {
  85. *(*int32)(data) = int32(fd)
  86. data = unsafe.Pointer(uintptr(data) + 4)
  87. }
  88. return b
  89. }
  90. // ParseUnixRights decodes a socket control message that contains an
  91. // integer array of open file descriptors from another process.
  92. func ParseUnixRights(m *SocketControlMessage) ([]int, error) {
  93. if m.Header.Level != SOL_SOCKET {
  94. return nil, EINVAL
  95. }
  96. if m.Header.Type != SCM_RIGHTS {
  97. return nil, EINVAL
  98. }
  99. fds := make([]int, len(m.Data)>>2)
  100. for i, j := 0, 0; i < len(m.Data); i += 4 {
  101. fds[j] = int(*(*int32)(unsafe.Pointer(&m.Data[i])))
  102. j++
  103. }
  104. return fds, nil
  105. }