github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/unix/sockcmsg_unix.go (about) 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 9 package unix 10 11 import ( 12 "runtime" 13 "unsafe" 14 ) 15 16 // Round the length of a raw sockaddr up to align it properly. 17 func cmsgAlignOf(salen int) int { 18 salign := SizeofPtr 19 20 switch runtime.GOOS { 21 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. 28 if SizeofPtr == 8 { 29 salign = 4 30 } 31 case "netbsd", "openbsd": 32 // NetBSD and OpenBSD armv7 require 64-bit alignment. 33 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. 43 func 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. 49 func CmsgSpace(datalen int) int { 50 return cmsgAlignOf(SizeofCmsghdr) + cmsgAlignOf(datalen) 51 } 52 53 func 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. 58 type SocketControlMessage struct { 59 Header Cmsghdr 60 Data []byte 61 } 62 63 // ParseSocketControlMessage parses b as an array of socket control 64 // messages. 65 func 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 80 func 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. 90 func 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. 107 func 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 }