diff options
author | tamihiro <dead6oy@gmail.com> | 2015-09-05 22:19:22 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-09-05 22:19:22 +0900 |
commit | c1229339972cc11d444acb9acadfe23e43ae00d7 (patch) | |
tree | 79bb2813936b1bc6de2399d0e52e6243761ca771 /server | |
parent | b8cd37c9d4f8c5b6519daa2effe08db0591df0ad (diff) |
server: support Transport.TransportConfig.LocalAddress for active connect
Diffstat (limited to 'server')
-rw-r--r-- | server/fsm.go | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/server/fsm.go b/server/fsm.go index 4c13edae..a2b04c6b 100644 --- a/server/fsm.go +++ b/server/fsm.go @@ -215,6 +215,8 @@ func (fsm *FSM) connectLoop() error { connect := func() { if fsm.state == bgp.BGP_FSM_ACTIVE { var host string + var lhost string + addr := fsm.pConf.NeighborConfig.NeighborAddress if addr.To4() != nil { @@ -223,14 +225,42 @@ func (fsm *FSM) connectLoop() error { host = "[" + addr.String() + "]:" + strconv.Itoa(bgp.BGP_PORT) } - conn, err := net.DialTimeout("tcp", host, time.Duration(MIN_CONNECT_RETRY-1)*time.Second) - if err == nil { - fsm.connCh <- conn + // check if LocalAddress has been configured in Neighbors.NeighborList.Transport.TransportConfig stanza. + laddr := fsm.pConf.Transport.TransportConfig.LocalAddress + if laddr != nil { + if laddr.To4() != nil { + lhost = laddr.String() + ":0" + } else { + lhost = "[" + laddr.String() + "]:0" + } + ltcpaddr, err := net.ResolveTCPAddr("tcp", lhost) + if err != nil { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": fsm.pConf.NeighborConfig.NeighborAddress, + }).Warnf("failed to resolve ltcpaddr: %s", err) + } else { + d := net.Dialer{LocalAddr: ltcpaddr, Timeout: time.Duration(MIN_CONNECT_RETRY-1) * time.Second} + if conn, err := d.Dial("tcp", host); err == nil { + fsm.connCh <- conn + } else { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": fsm.pConf.NeighborConfig.NeighborAddress, + }).Warnf("failed to connect from ltcpaddr", err) + } + } + } else { - log.WithFields(log.Fields{ - "Topic": "Peer", - "Key": fsm.pConf.NeighborConfig.NeighborAddress, - }).Debugf("failed to connect: %s", err) + conn, err := net.DialTimeout("tcp", host, time.Duration(MIN_CONNECT_RETRY-1)*time.Second) + if err == nil { + fsm.connCh <- conn + } else { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": fsm.pConf.NeighborConfig.NeighborAddress, + }).Warnf("failed to connect: %s", err) + } } } } |