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