fix: tproxy start error

This commit is contained in:
wwqgtxx 2024-02-07 21:07:41 +08:00
parent 324c0bde7d
commit 0c384b1e42
2 changed files with 30 additions and 8 deletions

View File

@ -36,13 +36,21 @@ func setsockopt(rc syscall.RawConn, addr string) error {
}
if err == nil {
err = syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_RECVTOS, 1)
}
if err == nil {
err = syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, syscall.IPV6_RECVTCLASS, 1)
_ = setDSCPsockopt(fd, isIPv6)
}
})
return err
}
func setDSCPsockopt(fd uintptr, isIPv6 bool) (err error) {
if err == nil {
err = syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_RECVTOS, 1)
}
if err == nil && isIPv6 {
err = syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, syscall.IPV6_RECVTCLASS, 1)
}
return
}

View File

@ -104,7 +104,14 @@ func getOrigDst(oob []byte) (netip.AddrPort, error) {
}
// retrieve the destination address from the SCM.
sa, err := unix.ParseOrigDstAddr(&scms[1])
var sa unix.Sockaddr
for i := range scms {
sa, err = unix.ParseOrigDstAddr(&scms[i])
if err == nil {
break
}
}
if err != nil {
return netip.AddrPort{}, fmt.Errorf("retrieve destination: %w", err)
}
@ -128,7 +135,14 @@ func getDSCP (oob []byte) (uint8, error) {
if err != nil {
return 0, fmt.Errorf("parse control message: %w", err)
}
dscp, err := parseDSCP(&scms[0])
var dscp uint8
for i := range scms {
dscp, err = parseDSCP(&scms[i])
if err == nil {
break
}
}
if err != nil {
return 0, fmt.Errorf("retrieve DSCP: %w", err)
}