Commit c241f696 authored by Mikio Hara's avatar Mikio Hara

syscall: simplify socket control messages

R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/7016044
parent 0822a62c
...@@ -6,33 +6,31 @@ ...@@ -6,33 +6,31 @@
package syscall package syscall
import ( import "unsafe"
"unsafe"
)
// UnixCredentials encodes credentials into a socket control message // UnixCredentials encodes credentials into a socket control message
// for sending to another process. This can be used for // for sending to another process. This can be used for
// authentication. // authentication.
func UnixCredentials(ucred *Ucred) []byte { func UnixCredentials(ucred *Ucred) []byte {
buf := make([]byte, CmsgSpace(SizeofUcred)) b := make([]byte, CmsgSpace(SizeofUcred))
cmsg := (*Cmsghdr)(unsafe.Pointer(&buf[0])) h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
cmsg.Level = SOL_SOCKET h.Level = SOL_SOCKET
cmsg.Type = SCM_CREDENTIALS h.Type = SCM_CREDENTIALS
cmsg.SetLen(CmsgLen(SizeofUcred)) h.SetLen(CmsgLen(SizeofUcred))
*((*Ucred)(cmsgData(cmsg))) = *ucred *((*Ucred)(cmsgData(h))) = *ucred
return buf return b
} }
// ParseUnixCredentials decodes a socket control message that contains // ParseUnixCredentials decodes a socket control message that contains
// credentials in a Ucred structure. To receive such a message, the // credentials in a Ucred structure. To receive such a message, the
// SO_PASSCRED option must be enabled on the socket. // SO_PASSCRED option must be enabled on the socket.
func ParseUnixCredentials(msg *SocketControlMessage) (*Ucred, error) { func ParseUnixCredentials(m *SocketControlMessage) (*Ucred, error) {
if msg.Header.Level != SOL_SOCKET { if m.Header.Level != SOL_SOCKET {
return nil, EINVAL return nil, EINVAL
} }
if msg.Header.Type != SCM_CREDENTIALS { if m.Header.Type != SCM_CREDENTIALS {
return nil, EINVAL return nil, EINVAL
} }
ucred := *(*Ucred)(unsafe.Pointer(&msg.Data[0])) ucred := *(*Ucred)(unsafe.Pointer(&m.Data[0]))
return &ucred, nil return &ucred, nil
} }
...@@ -8,9 +8,7 @@ ...@@ -8,9 +8,7 @@
package syscall package syscall
import ( import "unsafe"
"unsafe"
)
// Round the length of a raw sockaddr up to align it propery. // Round the length of a raw sockaddr up to align it propery.
func cmsgAlignOf(salen int) int { func cmsgAlignOf(salen int) int {
...@@ -38,77 +36,69 @@ func CmsgSpace(datalen int) int { ...@@ -38,77 +36,69 @@ func CmsgSpace(datalen int) int {
return cmsgAlignOf(SizeofCmsghdr) + cmsgAlignOf(datalen) return cmsgAlignOf(SizeofCmsghdr) + cmsgAlignOf(datalen)
} }
func cmsgData(cmsg *Cmsghdr) unsafe.Pointer { func cmsgData(h *Cmsghdr) unsafe.Pointer {
return unsafe.Pointer(uintptr(unsafe.Pointer(cmsg)) + SizeofCmsghdr) return unsafe.Pointer(uintptr(unsafe.Pointer(h)) + SizeofCmsghdr)
} }
// SocketControlMessage represents a socket control message.
type SocketControlMessage struct { type SocketControlMessage struct {
Header Cmsghdr Header Cmsghdr
Data []byte Data []byte
} }
func ParseSocketControlMessage(buf []byte) ([]SocketControlMessage, error) { // ParseSocketControlMessage parses b as an array of socket control
var ( // messages.
h *Cmsghdr func ParseSocketControlMessage(b []byte) ([]SocketControlMessage, error) {
dbuf []byte var msgs []SocketControlMessage
e error for len(b) >= CmsgLen(0) {
cmsgs []SocketControlMessage h, dbuf, err := socketControlMessageHeaderAndData(b)
) if err != nil {
return nil, err
for len(buf) >= CmsgLen(0) {
h, dbuf, e = socketControlMessageHeaderAndData(buf)
if e != nil {
break
} }
m := SocketControlMessage{} m := SocketControlMessage{Header: *h, Data: dbuf[:int(h.Len)-cmsgAlignOf(SizeofCmsghdr)]}
m.Header = *h msgs = append(msgs, m)
m.Data = dbuf[:int(h.Len)-cmsgAlignOf(SizeofCmsghdr)] b = b[cmsgAlignOf(int(h.Len)):]
cmsgs = append(cmsgs, m)
buf = buf[cmsgAlignOf(int(h.Len)):]
} }
return msgs, nil
return cmsgs, e
} }
func socketControlMessageHeaderAndData(buf []byte) (*Cmsghdr, []byte, error) { func socketControlMessageHeaderAndData(b []byte) (*Cmsghdr, []byte, error) {
h := (*Cmsghdr)(unsafe.Pointer(&buf[0])) h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
if h.Len < SizeofCmsghdr || int(h.Len) > len(buf) { if h.Len < SizeofCmsghdr || int(h.Len) > len(b) {
return nil, nil, EINVAL return nil, nil, EINVAL
} }
return h, buf[cmsgAlignOf(SizeofCmsghdr):], nil return h, b[cmsgAlignOf(SizeofCmsghdr):], nil
} }
// UnixRights encodes a set of open file descriptors into a socket // UnixRights encodes a set of open file descriptors into a socket
// control message for sending to another process. // control message for sending to another process.
func UnixRights(fds ...int) []byte { func UnixRights(fds ...int) []byte {
datalen := len(fds) * 4 datalen := len(fds) * 4
buf := make([]byte, CmsgSpace(datalen)) b := make([]byte, CmsgSpace(datalen))
cmsg := (*Cmsghdr)(unsafe.Pointer(&buf[0])) h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
cmsg.Level = SOL_SOCKET h.Level = SOL_SOCKET
cmsg.Type = SCM_RIGHTS h.Type = SCM_RIGHTS
cmsg.SetLen(CmsgLen(datalen)) h.SetLen(CmsgLen(datalen))
data := uintptr(cmsgData(h))
data := uintptr(cmsgData(cmsg))
for _, fd := range fds { for _, fd := range fds {
*(*int32)(unsafe.Pointer(data)) = int32(fd) *(*int32)(unsafe.Pointer(data)) = int32(fd)
data += 4 data += 4
} }
return b
return buf
} }
// ParseUnixRights decodes a socket control message that contains an // ParseUnixRights decodes a socket control message that contains an
// integer array of open file descriptors from another process. // integer array of open file descriptors from another process.
func ParseUnixRights(msg *SocketControlMessage) ([]int, error) { func ParseUnixRights(m *SocketControlMessage) ([]int, error) {
if msg.Header.Level != SOL_SOCKET { if m.Header.Level != SOL_SOCKET {
return nil, EINVAL return nil, EINVAL
} }
if msg.Header.Type != SCM_RIGHTS { if m.Header.Type != SCM_RIGHTS {
return nil, EINVAL return nil, EINVAL
} }
fds := make([]int, len(msg.Data)>>2) fds := make([]int, len(m.Data)>>2)
for i, j := 0, 0; i < len(msg.Data); i += 4 { for i, j := 0, 0; i < len(m.Data); i += 4 {
fds[j] = int(*(*int32)(unsafe.Pointer(&msg.Data[i]))) fds[j] = int(*(*int32)(unsafe.Pointer(&m.Data[i])))
j++ j++
} }
return fds, nil return fds, nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment