2017-09-24 21:35:25 +02:00
|
|
|
/* Copyright 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This implements userspace semantics of "sticky sockets", modeled after
|
|
|
|
* WireGuard's kernelspace implementation.
|
|
|
|
*/
|
|
|
|
|
2017-08-25 14:53:23 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-09-24 21:35:25 +02:00
|
|
|
"errors"
|
2017-10-17 16:50:23 +02:00
|
|
|
"fmt"
|
2017-08-25 14:53:23 +02:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"net"
|
2017-09-24 21:35:25 +02:00
|
|
|
"strconv"
|
|
|
|
"unsafe"
|
2017-08-25 14:53:23 +02:00
|
|
|
)
|
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
/* Supports source address caching
|
|
|
|
*
|
|
|
|
* Currently there is no way to achieve this within the net package:
|
|
|
|
* See e.g. https://github.com/golang/go/issues/17930
|
2017-10-08 22:03:32 +02:00
|
|
|
* So this code is remains platform dependent.
|
2017-09-24 21:35:25 +02:00
|
|
|
*/
|
2017-10-06 22:56:01 +02:00
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
type Endpoint struct {
|
2017-10-08 22:03:32 +02:00
|
|
|
src unix.RawSockaddrInet6
|
|
|
|
dst unix.RawSockaddrInet6
|
|
|
|
}
|
|
|
|
|
|
|
|
type IPv4Source struct {
|
|
|
|
src unix.RawSockaddrInet4
|
|
|
|
Ifindex int32
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-17 16:50:23 +02:00
|
|
|
type NativeBind struct {
|
2017-10-08 22:03:32 +02:00
|
|
|
sock4 int
|
|
|
|
sock6 int
|
|
|
|
}
|
2017-10-07 22:35:23 +02:00
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
func CreateUDPBind(port uint16) (UDPBind, uint16, error) {
|
|
|
|
var err error
|
2017-10-17 16:50:23 +02:00
|
|
|
var bind NativeBind
|
2017-10-08 22:03:32 +02:00
|
|
|
|
|
|
|
bind.sock6, port, err = create6(port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, port, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bind.sock4, port, err = create4(port)
|
|
|
|
if err != nil {
|
|
|
|
unix.Close(bind.sock6)
|
|
|
|
}
|
|
|
|
return &bind, port, err
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:50:23 +02:00
|
|
|
func (bind *NativeBind) SetMark(value uint32) error {
|
2017-10-08 22:03:32 +02:00
|
|
|
err := unix.SetsockoptInt(
|
|
|
|
bind.sock6,
|
|
|
|
unix.SOL_SOCKET,
|
|
|
|
unix.SO_MARK,
|
|
|
|
int(value),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return unix.SetsockoptInt(
|
|
|
|
bind.sock4,
|
|
|
|
unix.SOL_SOCKET,
|
|
|
|
unix.SO_MARK,
|
|
|
|
int(value),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:50:23 +02:00
|
|
|
func (bind *NativeBind) Close() error {
|
2017-10-08 22:03:32 +02:00
|
|
|
err1 := unix.Close(bind.sock6)
|
|
|
|
err2 := unix.Close(bind.sock4)
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:50:23 +02:00
|
|
|
func (bind *NativeBind) ReceiveIPv6(buff []byte, end *Endpoint) (int, error) {
|
2017-10-08 22:03:32 +02:00
|
|
|
return receive6(
|
|
|
|
bind.sock6,
|
|
|
|
buff,
|
|
|
|
end,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:50:23 +02:00
|
|
|
func (bind *NativeBind) ReceiveIPv4(buff []byte, end *Endpoint) (int, error) {
|
2017-10-08 22:03:32 +02:00
|
|
|
return receive4(
|
|
|
|
bind.sock4,
|
|
|
|
buff,
|
|
|
|
end,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:50:23 +02:00
|
|
|
func (bind *NativeBind) Send(buff []byte, end *Endpoint) error {
|
|
|
|
switch end.dst.Family {
|
2017-10-08 22:03:32 +02:00
|
|
|
case unix.AF_INET6:
|
|
|
|
return send6(bind.sock6, end, buff)
|
|
|
|
case unix.AF_INET:
|
|
|
|
return send4(bind.sock4, end, buff)
|
|
|
|
default:
|
2017-10-17 16:50:23 +02:00
|
|
|
return errors.New("Unknown address family of destination")
|
2017-10-08 22:03:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToString(addr unix.RawSockaddrInet6) string {
|
|
|
|
var udpAddr net.UDPAddr
|
|
|
|
|
|
|
|
switch addr.Family {
|
|
|
|
case unix.AF_INET6:
|
|
|
|
udpAddr.Port = int(addr.Port)
|
|
|
|
udpAddr.IP = addr.Addr[:]
|
|
|
|
return udpAddr.String()
|
|
|
|
|
|
|
|
case unix.AF_INET:
|
|
|
|
ptr := (*unix.RawSockaddrInet4)(unsafe.Pointer(&addr))
|
|
|
|
udpAddr.Port = int(ptr.Port)
|
|
|
|
udpAddr.IP = net.IPv4(
|
|
|
|
ptr.Addr[0],
|
|
|
|
ptr.Addr[1],
|
|
|
|
ptr.Addr[2],
|
|
|
|
ptr.Addr[3],
|
|
|
|
)
|
|
|
|
return udpAddr.String()
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "<unknown address family>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 21:33:47 +02:00
|
|
|
func (end *Endpoint) DstIP() net.IP {
|
2017-10-08 22:03:32 +02:00
|
|
|
switch end.dst.Family {
|
|
|
|
case unix.AF_INET6:
|
|
|
|
return end.dst.Addr[:]
|
|
|
|
case unix.AF_INET:
|
|
|
|
ptr := (*unix.RawSockaddrInet4)(unsafe.Pointer(&end.dst))
|
|
|
|
return net.IPv4(
|
|
|
|
ptr.Addr[0],
|
|
|
|
ptr.Addr[1],
|
|
|
|
ptr.Addr[2],
|
|
|
|
ptr.Addr[3],
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 21:33:47 +02:00
|
|
|
func (end *Endpoint) SrcToBytes() []byte {
|
2017-10-08 22:03:32 +02:00
|
|
|
ptr := unsafe.Pointer(&end.src)
|
|
|
|
arr := (*[unix.SizeofSockaddrInet6]byte)(ptr)
|
|
|
|
return arr[:]
|
|
|
|
}
|
|
|
|
|
2017-10-16 21:33:47 +02:00
|
|
|
func (end *Endpoint) SrcToString() string {
|
2017-10-08 22:03:32 +02:00
|
|
|
return sockaddrToString(end.src)
|
|
|
|
}
|
|
|
|
|
2017-10-16 21:33:47 +02:00
|
|
|
func (end *Endpoint) DstToString() string {
|
2017-10-08 22:03:32 +02:00
|
|
|
return sockaddrToString(end.dst)
|
|
|
|
}
|
|
|
|
|
2017-10-16 21:33:47 +02:00
|
|
|
func (end *Endpoint) ClearDst() {
|
|
|
|
end.dst = unix.RawSockaddrInet6{}
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
func (end *Endpoint) ClearSrc() {
|
|
|
|
end.src = unix.RawSockaddrInet6{}
|
2017-10-07 22:35:23 +02:00
|
|
|
}
|
2017-10-06 22:56:01 +02:00
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
func zoneToUint32(zone string) (uint32, error) {
|
|
|
|
if zone == "" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
if intr, err := net.InterfaceByName(zone); err == nil {
|
|
|
|
return uint32(intr.Index), nil
|
|
|
|
}
|
|
|
|
n, err := strconv.ParseUint(zone, 10, 32)
|
|
|
|
return uint32(n), err
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
func create4(port uint16) (int, uint16, error) {
|
2017-10-06 22:56:01 +02:00
|
|
|
|
|
|
|
// create socket
|
|
|
|
|
|
|
|
fd, err := unix.Socket(
|
|
|
|
unix.AF_INET,
|
|
|
|
unix.SOCK_DGRAM,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-10-07 22:35:23 +02:00
|
|
|
return -1, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := unix.SockaddrInet4{
|
|
|
|
Port: int(port),
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set sockopts and bind
|
|
|
|
|
|
|
|
if err := func() error {
|
|
|
|
if err := unix.SetsockoptInt(
|
|
|
|
fd,
|
|
|
|
unix.SOL_SOCKET,
|
|
|
|
unix.SO_REUSEADDR,
|
|
|
|
1,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := unix.SetsockoptInt(
|
|
|
|
fd,
|
|
|
|
unix.IPPROTO_IP,
|
|
|
|
unix.IP_PKTINFO,
|
|
|
|
1,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return unix.Bind(fd, &addr)
|
|
|
|
}(); err != nil {
|
|
|
|
unix.Close(fd)
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
return fd, uint16(addr.Port), err
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
func create6(port uint16) (int, uint16, error) {
|
2017-10-06 22:56:01 +02:00
|
|
|
|
|
|
|
// create socket
|
|
|
|
|
|
|
|
fd, err := unix.Socket(
|
|
|
|
unix.AF_INET,
|
|
|
|
unix.SOCK_DGRAM,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-10-07 22:35:23 +02:00
|
|
|
return -1, 0, err
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set sockopts and bind
|
|
|
|
|
2017-10-07 22:35:23 +02:00
|
|
|
addr := unix.SockaddrInet6{
|
|
|
|
Port: int(port),
|
|
|
|
}
|
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
if err := func() error {
|
|
|
|
|
|
|
|
if err := unix.SetsockoptInt(
|
|
|
|
fd,
|
|
|
|
unix.SOL_SOCKET,
|
|
|
|
unix.SO_REUSEADDR,
|
|
|
|
1,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := unix.SetsockoptInt(
|
|
|
|
fd,
|
|
|
|
unix.IPPROTO_IPV6,
|
|
|
|
unix.IPV6_RECVPKTINFO,
|
|
|
|
1,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := unix.SetsockoptInt(
|
|
|
|
fd,
|
|
|
|
unix.IPPROTO_IPV6,
|
|
|
|
unix.IPV6_V6ONLY,
|
|
|
|
1,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return unix.Bind(fd, &addr)
|
|
|
|
|
|
|
|
}(); err != nil {
|
|
|
|
unix.Close(fd)
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
return fd, uint16(addr.Port), err
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-17 16:50:23 +02:00
|
|
|
func (end *Endpoint) SetDst(s string) error {
|
2017-09-24 21:35:25 +02:00
|
|
|
addr, err := parseEndpoint(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:50:23 +02:00
|
|
|
fmt.Println(addr, err)
|
|
|
|
|
|
|
|
ipv4 := addr.IP.To4()
|
|
|
|
if ipv4 != nil {
|
|
|
|
dst := (*unix.RawSockaddrInet4)(unsafe.Pointer(&end.dst))
|
|
|
|
dst.Family = unix.AF_INET
|
|
|
|
dst.Port = uint16(addr.Port)
|
|
|
|
dst.Zero = [8]byte{}
|
|
|
|
copy(dst.Addr[:], ipv4)
|
|
|
|
end.ClearSrc()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
ipv6 := addr.IP.To16()
|
|
|
|
if ipv6 != nil {
|
|
|
|
zone, err := zoneToUint32(addr.Zone)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-08 22:03:32 +02:00
|
|
|
dst := &end.dst
|
|
|
|
dst.Family = unix.AF_INET6
|
|
|
|
dst.Port = uint16(addr.Port)
|
|
|
|
dst.Flowinfo = 0
|
|
|
|
dst.Scope_id = zone
|
|
|
|
copy(dst.Addr[:], ipv6[:])
|
2017-09-24 21:35:25 +02:00
|
|
|
end.ClearSrc()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("Failed to recognize IP address format")
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
func send6(sock int, end *Endpoint, buff []byte) error {
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// construct message header
|
|
|
|
|
|
|
|
var iovec unix.Iovec
|
2017-09-24 21:35:25 +02:00
|
|
|
iovec.Base = (*byte)(unsafe.Pointer(&buff[0]))
|
|
|
|
iovec.SetLen(len(buff))
|
|
|
|
|
|
|
|
cmsg := struct {
|
|
|
|
cmsghdr unix.Cmsghdr
|
|
|
|
pktinfo unix.Inet6Pktinfo
|
|
|
|
}{
|
|
|
|
unix.Cmsghdr{
|
|
|
|
Level: unix.IPPROTO_IPV6,
|
|
|
|
Type: unix.IPV6_PKTINFO,
|
|
|
|
Len: unix.SizeofInet6Pktinfo,
|
|
|
|
},
|
|
|
|
unix.Inet6Pktinfo{
|
2017-10-08 22:03:32 +02:00
|
|
|
Addr: end.src.Addr,
|
|
|
|
Ifindex: end.src.Scope_id,
|
2017-09-24 21:35:25 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
msghdr := unix.Msghdr{
|
|
|
|
Iov: &iovec,
|
|
|
|
Iovlen: 1,
|
|
|
|
Name: (*byte)(unsafe.Pointer(&end.dst)),
|
|
|
|
Namelen: unix.SizeofSockaddrInet6,
|
|
|
|
Control: (*byte)(unsafe.Pointer(&cmsg)),
|
|
|
|
}
|
|
|
|
|
|
|
|
msghdr.SetControllen(int(unsafe.Sizeof(cmsg)))
|
|
|
|
|
|
|
|
// sendmsg(sock, &msghdr, 0)
|
|
|
|
|
|
|
|
_, _, errno := unix.Syscall(
|
|
|
|
unix.SYS_SENDMSG,
|
2017-10-08 22:03:32 +02:00
|
|
|
uintptr(sock),
|
2017-09-24 21:35:25 +02:00
|
|
|
uintptr(unsafe.Pointer(&msghdr)),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
if errno == unix.EINVAL {
|
|
|
|
end.ClearSrc()
|
|
|
|
}
|
|
|
|
return errno
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
func send4(sock int, end *Endpoint, buff []byte) error {
|
2017-10-17 16:50:23 +02:00
|
|
|
println("send 4")
|
|
|
|
println(end.DstToString())
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// construct message header
|
|
|
|
|
|
|
|
var iovec unix.Iovec
|
2017-09-24 21:35:25 +02:00
|
|
|
iovec.Base = (*byte)(unsafe.Pointer(&buff[0]))
|
|
|
|
iovec.SetLen(len(buff))
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
src4 := (*IPv4Source)(unsafe.Pointer(&end.src))
|
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
cmsg := struct {
|
|
|
|
cmsghdr unix.Cmsghdr
|
|
|
|
pktinfo unix.Inet4Pktinfo
|
|
|
|
}{
|
|
|
|
unix.Cmsghdr{
|
|
|
|
Level: unix.IPPROTO_IP,
|
|
|
|
Type: unix.IP_PKTINFO,
|
2017-10-06 22:56:01 +02:00
|
|
|
Len: unix.SizeofInet4Pktinfo,
|
2017-09-24 21:35:25 +02:00
|
|
|
},
|
|
|
|
unix.Inet4Pktinfo{
|
2017-10-08 22:03:32 +02:00
|
|
|
Spec_dst: src4.src.Addr,
|
|
|
|
Ifindex: src4.Ifindex,
|
2017-09-24 21:35:25 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
msghdr := unix.Msghdr{
|
|
|
|
Iov: &iovec,
|
|
|
|
Iovlen: 1,
|
|
|
|
Name: (*byte)(unsafe.Pointer(&end.dst)),
|
|
|
|
Namelen: unix.SizeofSockaddrInet4,
|
|
|
|
Control: (*byte)(unsafe.Pointer(&cmsg)),
|
|
|
|
}
|
|
|
|
msghdr.SetControllen(int(unsafe.Sizeof(cmsg)))
|
|
|
|
|
|
|
|
// sendmsg(sock, &msghdr, 0)
|
|
|
|
|
|
|
|
_, _, errno := unix.Syscall(
|
|
|
|
unix.SYS_SENDMSG,
|
2017-10-08 22:03:32 +02:00
|
|
|
uintptr(sock),
|
2017-09-24 21:35:25 +02:00
|
|
|
uintptr(unsafe.Pointer(&msghdr)),
|
|
|
|
0,
|
|
|
|
)
|
2017-10-17 16:50:23 +02:00
|
|
|
|
|
|
|
println(sock)
|
|
|
|
fmt.Println(errno)
|
|
|
|
|
|
|
|
// clear source cache and try again
|
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
if errno == unix.EINVAL {
|
|
|
|
end.ClearSrc()
|
2017-10-17 16:50:23 +02:00
|
|
|
cmsg.pktinfo = unix.Inet4Pktinfo{}
|
|
|
|
_, _, errno = unix.Syscall(
|
|
|
|
unix.SYS_SENDMSG,
|
|
|
|
uintptr(sock),
|
|
|
|
uintptr(unsafe.Pointer(&msghdr)),
|
|
|
|
0,
|
|
|
|
)
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
2017-10-17 16:50:23 +02:00
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
return errno
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
func receive4(sock int, buff []byte, end *Endpoint) (int, error) {
|
2017-09-24 21:35:25 +02:00
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// contruct message header
|
|
|
|
|
|
|
|
var iovec unix.Iovec
|
|
|
|
iovec.Base = (*byte)(unsafe.Pointer(&buff[0]))
|
|
|
|
iovec.SetLen(len(buff))
|
|
|
|
|
|
|
|
var cmsg struct {
|
|
|
|
cmsghdr unix.Cmsghdr
|
|
|
|
pktinfo unix.Inet4Pktinfo
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
var msghdr unix.Msghdr
|
|
|
|
msghdr.Iov = &iovec
|
|
|
|
msghdr.Iovlen = 1
|
|
|
|
msghdr.Name = (*byte)(unsafe.Pointer(&end.dst))
|
|
|
|
msghdr.Namelen = unix.SizeofSockaddrInet4
|
|
|
|
msghdr.Control = (*byte)(unsafe.Pointer(&cmsg))
|
|
|
|
msghdr.SetControllen(int(unsafe.Sizeof(cmsg)))
|
|
|
|
|
|
|
|
// recvmsg(sock, &mskhdr, 0)
|
|
|
|
|
|
|
|
size, _, errno := unix.Syscall(
|
|
|
|
unix.SYS_RECVMSG,
|
|
|
|
uintptr(sock),
|
|
|
|
uintptr(unsafe.Pointer(&msghdr)),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
|
|
|
if errno != 0 {
|
|
|
|
return 0, errno
|
|
|
|
}
|
|
|
|
|
|
|
|
// update source cache
|
|
|
|
|
|
|
|
if cmsg.cmsghdr.Level == unix.IPPROTO_IP &&
|
|
|
|
cmsg.cmsghdr.Type == unix.IP_PKTINFO &&
|
|
|
|
cmsg.cmsghdr.Len >= unix.SizeofInet4Pktinfo {
|
2017-10-08 22:03:32 +02:00
|
|
|
src4 := (*IPv4Source)(unsafe.Pointer(&end.src))
|
|
|
|
src4.src.Family = unix.AF_INET
|
|
|
|
src4.src.Addr = cmsg.pktinfo.Spec_dst
|
|
|
|
src4.Ifindex = cmsg.pktinfo.Ifindex
|
2017-10-06 22:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return int(size), nil
|
|
|
|
}
|
|
|
|
|
2017-10-08 22:03:32 +02:00
|
|
|
func receive6(sock int, buff []byte, end *Endpoint) (int, error) {
|
2017-10-06 22:56:01 +02:00
|
|
|
|
|
|
|
// contruct message header
|
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
var iovec unix.Iovec
|
|
|
|
iovec.Base = (*byte)(unsafe.Pointer(&buff[0]))
|
|
|
|
iovec.SetLen(len(buff))
|
|
|
|
|
|
|
|
var cmsg struct {
|
|
|
|
cmsghdr unix.Cmsghdr
|
2017-10-06 22:56:01 +02:00
|
|
|
pktinfo unix.Inet6Pktinfo
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var msg unix.Msghdr
|
|
|
|
msg.Iov = &iovec
|
|
|
|
msg.Iovlen = 1
|
|
|
|
msg.Name = (*byte)(unsafe.Pointer(&end.dst))
|
2017-10-06 22:56:01 +02:00
|
|
|
msg.Namelen = uint32(unix.SizeofSockaddrInet6)
|
2017-09-24 21:35:25 +02:00
|
|
|
msg.Control = (*byte)(unsafe.Pointer(&cmsg))
|
|
|
|
msg.SetControllen(int(unsafe.Sizeof(cmsg)))
|
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// recvmsg(sock, &mskhdr, 0)
|
|
|
|
|
2017-10-07 22:35:23 +02:00
|
|
|
size, _, errno := unix.Syscall(
|
2017-09-24 21:35:25 +02:00
|
|
|
unix.SYS_RECVMSG,
|
2017-10-06 22:56:01 +02:00
|
|
|
uintptr(sock),
|
2017-09-24 21:35:25 +02:00
|
|
|
uintptr(unsafe.Pointer(&msg)),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
|
|
|
if errno != 0 {
|
2017-10-07 22:35:23 +02:00
|
|
|
return 0, errno
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 22:56:01 +02:00
|
|
|
// update source cache
|
|
|
|
|
2017-09-24 21:35:25 +02:00
|
|
|
if cmsg.cmsghdr.Level == unix.IPPROTO_IPV6 &&
|
|
|
|
cmsg.cmsghdr.Type == unix.IPV6_PKTINFO &&
|
|
|
|
cmsg.cmsghdr.Len >= unix.SizeofInet6Pktinfo {
|
2017-10-08 22:03:32 +02:00
|
|
|
end.src.Family = unix.AF_INET6
|
|
|
|
end.src.Addr = cmsg.pktinfo.Addr
|
|
|
|
end.src.Scope_id = cmsg.pktinfo.Ifindex
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 22:35:23 +02:00
|
|
|
return int(size), nil
|
2017-09-24 21:35:25 +02:00
|
|
|
}
|