1
2
3
4
5
6
7 package unix
8
9 import "unsafe"
10
11
12
13
14 func UnixCredentials(ucred *Ucred) []byte {
15 b := make([]byte, CmsgSpace(SizeofUcred))
16 h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
17 h.Level = SOL_SOCKET
18 h.Type = SCM_CREDENTIALS
19 h.SetLen(CmsgLen(SizeofUcred))
20 *(*Ucred)(h.data(0)) = *ucred
21 return b
22 }
23
24
25
26
27 func ParseUnixCredentials(m *SocketControlMessage) (*Ucred, error) {
28 if m.Header.Level != SOL_SOCKET {
29 return nil, EINVAL
30 }
31 if m.Header.Type != SCM_CREDENTIALS {
32 return nil, EINVAL
33 }
34 ucred := *(*Ucred)(unsafe.Pointer(&m.Data[0]))
35 return &ucred, nil
36 }
37
38
39 func PktInfo4(info *Inet4Pktinfo) []byte {
40 b := make([]byte, CmsgSpace(SizeofInet4Pktinfo))
41 h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
42 h.Level = SOL_IP
43 h.Type = IP_PKTINFO
44 h.SetLen(CmsgLen(SizeofInet4Pktinfo))
45 *(*Inet4Pktinfo)(h.data(0)) = *info
46 return b
47 }
48
49
50 func PktInfo6(info *Inet6Pktinfo) []byte {
51 b := make([]byte, CmsgSpace(SizeofInet6Pktinfo))
52 h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
53 h.Level = SOL_IPV6
54 h.Type = IPV6_PKTINFO
55 h.SetLen(CmsgLen(SizeofInet6Pktinfo))
56 *(*Inet6Pktinfo)(h.data(0)) = *info
57 return b
58 }
59
View as plain text