diff options
author | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2017-07-11 10:45:30 +0900 |
---|---|---|
committer | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2017-07-18 15:11:01 +0900 |
commit | 178e65f8f113da82f8dfcc56b5fa7682da1bd932 (patch) | |
tree | 52b423ccf52b64d14b278fb39d8e6a3e399488b2 /server/sockopt.go | |
parent | eb95f000bf4ab70c2dad7589e58045fbce1addf1 (diff) |
server: Set outgoing TTL before dialing
Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
Diffstat (limited to 'server/sockopt.go')
-rw-r--r-- | server/sockopt.go | 60 |
1 files changed, 54 insertions, 6 deletions
diff --git a/server/sockopt.go b/server/sockopt.go index 2f6a8bb6..e053bef0 100644 --- a/server/sockopt.go +++ b/server/sockopt.go @@ -19,20 +19,68 @@ package server import ( "fmt" "net" + + log "github.com/sirupsen/logrus" ) -func SetTcpMD5SigSockopts(l *net.TCPListener, address string, key string) error { - return fmt.Errorf("md5 not supported") +func SetTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error { + return fmt.Errorf("setting md5 is not supported") } -func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error { +func SetTcpTTLSockopt(conn *net.TCPConn, ttl int) error { return fmt.Errorf("setting ttl is not supported") } -func SetTcpMinTTLSockopts(conn *net.TCPConn, ttl int) error { +func SetTcpMinTTLSockopt(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") +type TCPDialer struct { + net.Dialer + + // MD5 authentication password. + AuthPassword string + + // The TTL value to set outgoing connection. + Ttl uint8 + + // The minimum TTL value for incoming packets. + TtlMin uint8 +} + +func (d *TCPDialer) DialTCP(addr string, port int) (*net.TCPConn, error) { + if d.AuthPassword != "" { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": addr, + }).Warn("setting md5 for active connection is not supported") + } + if d.Ttl != 0 { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": addr, + }).Warn("setting ttl for active connection is not supported") + } + if d.TtlMin != 0 { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": addr, + }).Warn("setting min ttl for active connection is not supported") + } + + raddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(addr, fmt.Sprintf("%d", port))) + if err != nil { + return nil, fmt.Errorf("invalid remote address: %s", err) + } + laddr, err := net.ResolveTCPAddr("tcp", d.LocalAddr.String()) + if err != nil { + return nil, fmt.Errorf("invalid local address: %s", err) + } + + dialer := net.Dialer{LocalAddr: laddr, Timeout: d.Timeout} + conn, err := dialer.Dial("tcp", raddr.String()) + if err != nil { + return nil, err + } + return conn.(*net.TCPConn), nil } |