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