sockcmsg_unix.go 3.1 KB

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