sockcmsg_unix.go 2.9 KB

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