package socket
import (
"encoding/binary"
"net/netip"
"golang.org/x/sys/unix"
)
const (
sizeofInet4Pktinfo = 12
sizeofInet6Pktinfo = 20
)
func parsePacketInfo(oob []byte ) (localAddr , bool ) {
for len (oob ) > 0 {
h , data , rest , err := unix .ParseOneSocketControlMessage (oob )
if err != nil {
return localAddr {}, false
}
switch {
case h .Level == unix .IPPROTO_IP && h .Type == sysIPPktinfo && len (data ) >= sizeofInet4Pktinfo :
return localAddr {
addr : netip .AddrFrom4 ([4 ]byte (data [8 :12 ])),
ifIndex : int (binary .NativeEndian .Uint32 (data [0 :4 ])),
}, true
case h .Level == unix .IPPROTO_IPV6 && h .Type == unix .IPV6_PKTINFO && len (data ) >= sizeofInet6Pktinfo :
a := netip .AddrFrom16 ([16 ]byte (data [0 :16 ]))
ifIndex := 0
if a .IsLinkLocalUnicast () {
ifIndex = int (binary .NativeEndian .Uint32 (data [16 :20 ]))
}
return localAddr {addr : a .Unmap (), ifIndex : ifIndex }, true
}
oob = rest
}
return localAddr {}, false
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .