Retro68/gcc/libgo/go/syscall/sockcmsg_linux.go

40 lines
1.1 KiB
Go
Raw Normal View History

// Copyright 2011 The Go Authors. All rights reserved.
2012-03-27 23:13:14 +00:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Socket control messages
package syscall
2014-09-21 17:33:12 +00:00
import "unsafe"
2012-03-27 23:13:14 +00:00
// UnixCredentials encodes credentials into a socket control message
// for sending to another process. This can be used for
// authentication.
func UnixCredentials(ucred *Ucred) []byte {
2014-09-21 17:33:12 +00:00
b := make([]byte, CmsgSpace(SizeofUcred))
h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
h.Level = SOL_SOCKET
h.Type = SCM_CREDENTIALS
h.SetLen(CmsgLen(SizeofUcred))
*((*Ucred)(cmsgData(h))) = *ucred
return b
2012-03-27 23:13:14 +00:00
}
// ParseUnixCredentials decodes a socket control message that contains
// credentials in a Ucred structure. To receive such a message, the
// SO_PASSCRED option must be enabled on the socket.
2014-09-21 17:33:12 +00:00
func ParseUnixCredentials(m *SocketControlMessage) (*Ucred, error) {
if m.Header.Level != SOL_SOCKET {
2012-03-27 23:13:14 +00:00
return nil, EINVAL
}
2014-09-21 17:33:12 +00:00
if m.Header.Type != SCM_CREDENTIALS {
2012-03-27 23:13:14 +00:00
return nil, EINVAL
}
if uintptr(len(m.Data)) < unsafe.Sizeof(Ucred{}) {
return nil, EINVAL
}
2014-09-21 17:33:12 +00:00
ucred := *(*Ucred)(unsafe.Pointer(&m.Data[0]))
2012-03-27 23:13:14 +00:00
return &ucred, nil
}