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