vendor/golang.org/x/sys/unix/sockcmsg_linux.go
author Mikael Berthe <mikael@lilotux.net>
Tue, 23 Aug 2022 22:39:43 +0200
changeset 260 445e01aede7e
parent 251 1c52a0eeb952
permissions -rw-r--r--
Update vendor directory
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
// Copyright 2011 The Go Authors. All rights reserved.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
// Socket control messages
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
package unix
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
import "unsafe"
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
// UnixCredentials encodes credentials into a socket control message
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
// for sending to another process. This can be used for
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
// authentication.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
func UnixCredentials(ucred *Ucred) []byte {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	b := make([]byte, CmsgSpace(SizeofUcred))
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
	h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
	h.Level = SOL_SOCKET
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	h.Type = SCM_CREDENTIALS
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
	h.SetLen(CmsgLen(SizeofUcred))
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
    20
	*(*Ucred)(h.data(0)) = *ucred
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	return b
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
// ParseUnixCredentials decodes a socket control message that contains
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
// credentials in a Ucred structure. To receive such a message, the
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
// SO_PASSCRED option must be enabled on the socket.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
func ParseUnixCredentials(m *SocketControlMessage) (*Ucred, error) {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	if m.Header.Level != SOL_SOCKET {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
		return nil, EINVAL
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	if m.Header.Type != SCM_CREDENTIALS {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
		return nil, EINVAL
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
	ucred := *(*Ucred)(unsafe.Pointer(&m.Data[0]))
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	return &ucred, nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
}
260
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    37
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    38
// PktInfo4 encodes Inet4Pktinfo into a socket control message of type IP_PKTINFO.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    39
func PktInfo4(info *Inet4Pktinfo) []byte {
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    40
	b := make([]byte, CmsgSpace(SizeofInet4Pktinfo))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    41
	h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    42
	h.Level = SOL_IP
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    43
	h.Type = IP_PKTINFO
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    44
	h.SetLen(CmsgLen(SizeofInet4Pktinfo))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    45
	*(*Inet4Pktinfo)(h.data(0)) = *info
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    46
	return b
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    47
}
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    48
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    49
// PktInfo6 encodes Inet6Pktinfo into a socket control message of type IPV6_PKTINFO.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    50
func PktInfo6(info *Inet6Pktinfo) []byte {
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    51
	b := make([]byte, CmsgSpace(SizeofInet6Pktinfo))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    52
	h := (*Cmsghdr)(unsafe.Pointer(&b[0]))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    53
	h.Level = SOL_IPV6
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    54
	h.Type = IPV6_PKTINFO
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    55
	h.SetLen(CmsgLen(SizeofInet6Pktinfo))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    56
	*(*Inet6Pktinfo)(h.data(0)) = *info
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    57
	return b
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    58
}
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    59
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    60
// ParseOrigDstAddr decodes a socket control message containing the original
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    61
// destination address. To receive such a message the IP_RECVORIGDSTADDR or
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    62
// IPV6_RECVORIGDSTADDR option must be enabled on the socket.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    63
func ParseOrigDstAddr(m *SocketControlMessage) (Sockaddr, error) {
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    64
	switch {
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    65
	case m.Header.Level == SOL_IP && m.Header.Type == IP_ORIGDSTADDR:
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    66
		pp := (*RawSockaddrInet4)(unsafe.Pointer(&m.Data[0]))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    67
		sa := new(SockaddrInet4)
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    68
		p := (*[2]byte)(unsafe.Pointer(&pp.Port))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    69
		sa.Port = int(p[0])<<8 + int(p[1])
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    70
		sa.Addr = pp.Addr
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    71
		return sa, nil
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    72
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    73
	case m.Header.Level == SOL_IPV6 && m.Header.Type == IPV6_ORIGDSTADDR:
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    74
		pp := (*RawSockaddrInet6)(unsafe.Pointer(&m.Data[0]))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    75
		sa := new(SockaddrInet6)
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    76
		p := (*[2]byte)(unsafe.Pointer(&pp.Port))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    77
		sa.Port = int(p[0])<<8 + int(p[1])
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    78
		sa.ZoneId = pp.Scope_id
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    79
		sa.Addr = pp.Addr
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    80
		return sa, nil
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    81
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    82
	default:
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    83
		return nil, EINVAL
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    84
	}
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    85
}