Skip to content

Commit

Permalink
fix: tproxy start error
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Feb 7, 2024
1 parent 324c0bd commit 0c384b1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
18 changes: 13 additions & 5 deletions listener/tproxy/setsockopt_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 17 additions & 3 deletions listener/tproxy/udp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -123,12 +130,19 @@ func getOrigDst(oob []byte) (netip.AddrPort, error) {
return rAddr, nil
}

func getDSCP (oob []byte) (uint8, error) {
func getDSCP(oob []byte) (uint8, error) {
scms, err := unix.ParseSocketControlMessage(oob)
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)
}
Expand Down

0 comments on commit 0c384b1

Please sign in to comment.