diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/server.go | 11 | ||||
-rw-r--r-- | server/sockopt.go | 31 |
2 files changed, 23 insertions, 19 deletions
diff --git a/server/server.go b/server/server.go index 47166602..930e1067 100644 --- a/server/server.go +++ b/server/server.go @@ -131,15 +131,14 @@ func (server *BgpServer) Serve() { os.Exit(1) } - listenFile := func(addr net.IP) *os.File { + listener := func(addr net.IP) *net.TCPListener { var l *net.TCPListener if addr.To4() != nil { l = listenerMap["tcp4"] } else { l = listenerMap["tcp6"] } - f, _ := l.File() - return f + return l } server.peerMap = make(map[string]peerMapInfo) @@ -157,8 +156,7 @@ func (server *BgpServer) Serve() { } case peer := <-server.addedPeerCh: addr := peer.NeighborAddress.String() - f := listenFile(peer.NeighborAddress) - SetTcpMD5SigSockopts(int(f.Fd()), addr, peer.AuthPassword) + SetTcpMD5SigSockopts(listener(peer.NeighborAddress), addr, peer.AuthPassword) sch := make(chan *serverMsg, 8) pch := make(chan *peerMsg, 4096) var l []*serverMsgDataPeer @@ -199,8 +197,7 @@ func (server *BgpServer) Serve() { } case peer := <-server.deletedPeerCh: addr := peer.NeighborAddress.String() - f := listenFile(peer.NeighborAddress) - SetTcpMD5SigSockopts(int(f.Fd()), addr, "") + SetTcpMD5SigSockopts(listener(peer.NeighborAddress), addr, "") info, found := server.peerMap[addr] if found { log.Info("Delete a peer configuration for ", addr) diff --git a/server/sockopt.go b/server/sockopt.go index 2d26c588..429063b2 100644 --- a/server/sockopt.go +++ b/server/sockopt.go @@ -39,23 +39,30 @@ func buildTcpMD5Sig(address string, key string) (tcpmd5sig, error) { return t, nil } -func SetTcpMD5SigSockopts(fd int, address string, key string) error { +func connToFd(v reflect.Value) int { + fd := v.FieldByName("fd") + p := reflect.Indirect(fd) + sysfd := p.FieldByName("sysfd") + return int(sysfd.Int()) +} + +func listenerToFd(l *net.TCPListener) int { + return connToFd(reflect.ValueOf(*l)) +} + +func tcpConnToFd(tcp *net.TCPConn) int { + n := reflect.ValueOf(*tcp) + return connToFd(n.FieldByName("conn")) +} + +func SetTcpMD5SigSockopts(l *net.TCPListener, address string, key string) error { t, _ := buildTcpMD5Sig(address, key) - _, _, e := syscall.Syscall6(syscall.SYS_SETSOCKOPT, uintptr(fd), + _, _, e := syscall.Syscall6(syscall.SYS_SETSOCKOPT, uintptr(listenerToFd(l)), uintptr(syscall.IPPROTO_TCP), uintptr(TCP_MD5SIG), uintptr(unsafe.Pointer(&t)), unsafe.Sizeof(t), 0) return e } -func TCPConnToFd(tcp *net.TCPConn) int { - n := reflect.ValueOf(*tcp) - conn := n.FieldByName("conn") - fd := conn.FieldByName("fd") - p := reflect.Indirect(fd) - sysfd := p.FieldByName("sysfd") - return int(sysfd.Int()) -} - func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error { level := syscall.IPPROTO_IP name := syscall.IP_TTL @@ -63,5 +70,5 @@ func SetTcpTTLSockopts(conn *net.TCPConn, ttl int) error { level = syscall.IPPROTO_IPV6 name = syscall.IPV6_UNICAST_HOPS } - return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(TCPConnToFd(conn), level, name, ttl)) + return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(tcpConnToFd(conn), level, name, ttl)) } |