Skip to content

Commit

Permalink
server/sockopt: Support to set minimum TTL
Browse files Browse the repository at this point in the history
Signed-off-by: IWASE Yusuke <[email protected]>
  • Loading branch information
iwaseyusuke authored and fujita committed Jun 23, 2017
1 parent 5bb427a commit 613d8a1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
4 changes: 4 additions & 0 deletions server/sockopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error {
return fmt.Errorf("setting ttl is not supported")
}

func SetTcpMinTTLSockopts(conn *net.TCPConn, ttl int) error {
return fmt.Errorf("setting min ttl is not supported")
}

func DialTCPTimeoutWithMD5Sig(host string, port int, localAddr, key string, msec int) (*net.TCPConn, error) {
return nil, fmt.Errorf("md5 active connection unsupported")
}
33 changes: 24 additions & 9 deletions server/sockopt_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
)

const (
TCP_MD5SIG = 0x10
TCP_MD5SIG = 0x10 // TCP MD5 Signature (RFC2385)
IPV6_MINHOPCOUNT = 73 // Generalized TTL Security Mechanism (RFC5082)
)

func SetTcpMD5SigSockopts(l *net.TCPListener, address string, key string) error {
Expand All @@ -49,13 +50,7 @@ func SetTcpMD5SigSockopts(l *net.TCPListener, address string, key string) error
return nil
}

func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error {
level := syscall.IPPROTO_IP
name := syscall.IP_TTL
if strings.Contains(conn.RemoteAddr().String(), "[") {
level = syscall.IPPROTO_IPV6
name = syscall.IPV6_UNICAST_HOPS
}
func setTcpSockoptInt(conn *net.TCPConn, level int, name int, value int) error {
fi, err := conn.File()
defer fi.Close()
if err != nil {
Expand All @@ -64,7 +59,27 @@ func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error {
if conn, err := net.FileConn(fi); err == nil {
defer conn.Close()
}
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(int(fi.Fd()), level, name, ttl))
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(int(fi.Fd()), level, name, value))
}

func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error {
level := syscall.IPPROTO_IP
name := syscall.IP_TTL
if strings.Contains(conn.RemoteAddr().String(), "[") {
level = syscall.IPPROTO_IPV6
name = syscall.IPV6_UNICAST_HOPS
}
return setTcpSockoptInt(conn, level, name, ttl)
}

func SetTcpMinTTLSockopts(conn *net.TCPConn, ttl int) error {
level := syscall.IPPROTO_IP
name := syscall.IP_MINTTL
if strings.Contains(conn.RemoteAddr().String(), "[") {
level = syscall.IPPROTO_IPV6
name = IPV6_MINHOPCOUNT
}
return setTcpSockoptInt(conn, level, name, ttl)
}

func DialTCPTimeoutWithMD5Sig(host string, port int, localAddr, key string, msec int) (*net.TCPConn, error) {
Expand Down
33 changes: 24 additions & 9 deletions server/sockopt_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (
)

const (
TCP_MD5SIG = 14
TCP_MD5SIG = 14 // TCP MD5 Signature (RFC2385)
IPV6_MINHOPCOUNT = 73 // Generalized TTL Security Mechanism (RFC5082)
)

type tcpmd5sig struct {
Expand Down Expand Up @@ -74,13 +75,7 @@ func SetTcpMD5SigSockopts(l *net.TCPListener, address string, key string) error
return nil
}

func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error {
level := syscall.IPPROTO_IP
name := syscall.IP_TTL
if strings.Contains(conn.RemoteAddr().String(), "[") {
level = syscall.IPPROTO_IPV6
name = syscall.IPV6_UNICAST_HOPS
}
func setTcpSockoptInt(conn *net.TCPConn, level int, name int, value int) error {
fi, err := conn.File()
defer fi.Close()
if err != nil {
Expand All @@ -89,7 +84,27 @@ func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error {
if conn, err := net.FileConn(fi); err == nil {
defer conn.Close()
}
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(int(fi.Fd()), level, name, ttl))
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(int(fi.Fd()), level, name, value))
}

func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error {
level := syscall.IPPROTO_IP
name := syscall.IP_TTL
if strings.Contains(conn.RemoteAddr().String(), "[") {
level = syscall.IPPROTO_IPV6
name = syscall.IPV6_UNICAST_HOPS
}
return setTcpSockoptInt(conn, level, name, ttl)
}

func SetTcpMinTTLSockopts(conn *net.TCPConn, ttl int) error {
level := syscall.IPPROTO_IP
name := syscall.IP_MINTTL
if strings.Contains(conn.RemoteAddr().String(), "[") {
level = syscall.IPPROTO_IPV6
name = IPV6_MINHOPCOUNT
}
return setTcpSockoptInt(conn, level, name, ttl)
}

func DialTCPTimeoutWithMD5Sig(host string, port int, localAddr, key string, msec int) (*net.TCPConn, error) {
Expand Down
33 changes: 24 additions & 9 deletions server/sockopt_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ func saDelete(address string) error {
}

const (
TCP_MD5SIG = 0x4
TCP_MD5SIG = 0x4 // TCP MD5 Signature (RFC2385)
IPV6_MINHOPCOUNT = 73 // Generalized TTL Security Mechanism (RFC5082)
)

func SetTcpMD5SigSockopts(l *net.TCPListener, address string, key string) error {
Expand All @@ -373,13 +374,7 @@ func SetTcpMD5SigSockopts(l *net.TCPListener, address string, key string) error
return saDelete(address)
}

func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error {
level := syscall.IPPROTO_IP
name := syscall.IP_TTL
if strings.Contains(conn.RemoteAddr().String(), "[") {
level = syscall.IPPROTO_IPV6
name = syscall.IPV6_UNICAST_HOPS
}
func setTcpSockoptInt(conn *net.TCPConn, level int, name int, value int) error {
fi, err := conn.File()
defer fi.Close()
if err != nil {
Expand All @@ -388,7 +383,27 @@ func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error {
if conn, err := net.FileConn(fi); err == nil {
defer conn.Close()
}
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(int(fi.Fd()), level, name, ttl))
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(int(fi.Fd()), level, name, value))
}

func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error {
level := syscall.IPPROTO_IP
name := syscall.IP_TTL
if strings.Contains(conn.RemoteAddr().String(), "[") {
level = syscall.IPPROTO_IPV6
name = syscall.IPV6_UNICAST_HOPS
}
return setTcpSockoptInt(conn, level, name, ttl)
}

func SetTcpMinTTLSockopts(conn *net.TCPConn, ttl int) error {
level := syscall.IPPROTO_IP
name := syscall.IP_MINTTL
if strings.Contains(conn.RemoteAddr().String(), "[") {
level = syscall.IPPROTO_IPV6
name = IPV6_MINHOPCOUNT
}
return setTcpSockoptInt(conn, level, name, ttl)
}

func DialTCPTimeoutWithMD5Sig(host string, port int, localAddr, key string, msec int) (*net.TCPConn, error) {
Expand Down

0 comments on commit 613d8a1

Please sign in to comment.