func (m *MockConnection) Write(buf []byte) (int, error) { time.Sleep(time.Duration(m.wait) * time.Millisecond) m.sendBuf = append(m.sendBuf, buf) msg, _ := bgp.ParseBGPMessage(buf) fmt.Printf("%d bytes written by gobgp message type : %s\n", len(buf), showMessageType(msg.Header.Type)) return len(buf), nil }
func TestFSMHandlerOpensent_HoldTimerExpired(t *testing.T) { assert := assert.New(t) m := NewMockConnection() p, h := makePeerAndHandler() // push mock connection p.fsm.conn = m p.fsm.h = h // set keepalive ticker p.fsm.pConf.Timers.State.NegotiatedHoldTime = 3 // set holdtime p.fsm.opensentHoldTime = 2 state, _ := h.opensent() assert.Equal(bgp.BGP_FSM_IDLE, state) lastMsg := m.sendBuf[len(m.sendBuf)-1] sent, _ := bgp.ParseBGPMessage(lastMsg) assert.Equal(uint8(bgp.BGP_MSG_NOTIFICATION), sent.Header.Type) assert.Equal(uint8(bgp.BGP_ERROR_HOLD_TIMER_EXPIRED), sent.Body.(*bgp.BGPNotification).ErrorCode) }
func (body *BMPRouteMonitoring) ParseBody(msg *BMPMessage, data []byte) error { update, err := bgp.ParseBGPMessage(data) if err != nil { return err } body.BGPUpdate = update return nil }
func (body *BMPPeerUpNotification) ParseBody(msg *BMPMessage, data []byte) error { if msg.PeerHeader.Flags&(1<<7) != 0 { body.LocalAddress = net.IP(data[:16]).To16() } else { body.LocalAddress = net.IP(data[12:16]).To4() } body.LocalPort = binary.BigEndian.Uint16(data[16:18]) body.RemotePort = binary.BigEndian.Uint16(data[18:20]) data = data[20:] sentopen, err := bgp.ParseBGPMessage(data) if err != nil { return err } body.SentOpenMsg = sentopen data = data[body.SentOpenMsg.Header.Len:] body.ReceivedOpenMsg, err = bgp.ParseBGPMessage(data) if err != nil { return err } return nil }
func (body *BMPPeerDownNotification) ParseBody(msg *BMPMessage, data []byte) error { body.Reason = data[0] data = data[1:] if body.Reason == BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION || body.Reason == BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION { notification, err := bgp.ParseBGPMessage(data) if err != nil { return err } body.BGPNotification = notification } else { body.Data = data } return nil }
func (m *BGP4MPMessage) DecodeFromBytes(data []byte) error { rest, err := m.decodeFromBytes(data) if err != nil { return err } if len(rest) < bgp.BGP_HEADER_LENGTH { return fmt.Errorf("Not all BGP4MPMessageAS4 bytes available") } msg, err := bgp.ParseBGPMessage(rest) if err != nil { return err } m.BGPMessage = msg return nil }
func (peer *Peer) ToConfig(getAdvertised bool) *config.Neighbor { // create copy which can be access to without mutex conf := *peer.fsm.pConf conf.AfiSafis = make([]config.AfiSafi, len(peer.fsm.pConf.AfiSafis)) for i := 0; i < len(peer.fsm.pConf.AfiSafis); i++ { conf.AfiSafis[i] = peer.fsm.pConf.AfiSafis[i] } remoteCap := make([]bgp.ParameterCapabilityInterface, 0, len(peer.fsm.capMap)) for _, c := range peer.fsm.capMap { for _, m := range c { // need to copy all values here buf, _ := m.Serialize() cap, _ := bgp.DecodeCapability(buf) remoteCap = append(remoteCap, cap) } } conf.State.RemoteCapabilityList = remoteCap conf.State.LocalCapabilityList = capabilitiesFromConfig(peer.fsm.pConf) conf.State.RemoteRouterId = peer.fsm.peerInfo.ID.To4().String() conf.State.SessionState = config.IntToSessionStateMap[int(peer.fsm.state)] conf.State.AdminState = config.IntToAdminStateMap[int(peer.fsm.adminState)] if peer.fsm.state == bgp.BGP_FSM_ESTABLISHED { rfList := peer.configuredRFlist() if getAdvertised { pathList, _ := peer.getBestFromLocal(rfList) conf.State.AdjTable.Advertised = uint32(len(pathList)) } else { conf.State.AdjTable.Advertised = 0 } conf.State.AdjTable.Received = uint32(peer.adjRibIn.Count(rfList)) conf.State.AdjTable.Accepted = uint32(peer.adjRibIn.Accepted(rfList)) conf.Transport.State.LocalAddress, conf.Transport.State.LocalPort = peer.fsm.LocalHostPort() _, conf.Transport.State.RemotePort = peer.fsm.RemoteHostPort() buf, _ := peer.fsm.recvOpen.Serialize() // need to copy all values here conf.State.ReceivedOpenMessage, _ = bgp.ParseBGPMessage(buf) } return &conf }
func TestFSMHandlerEstablish_HoldTimerExpired(t *testing.T) { assert := assert.New(t) m := NewMockConnection() p, h := makePeerAndHandler() // push mock connection p.fsm.conn = m p.fsm.h = h // set keepalive ticker p.fsm.pConf.Timers.State.NegotiatedHoldTime = 3 msg := keepalive() header, _ := msg.Header.Serialize() body, _ := msg.Body.Serialize() pushPackets := func() { // first keepalive from peer m.setData(header) m.setData(body) } // set holdtime p.fsm.pConf.Timers.Config.HoldTime = 2 p.fsm.pConf.Timers.State.NegotiatedHoldTime = 2 go pushPackets() state, _ := h.established() time.Sleep(time.Second * 1) assert.Equal(bgp.BGP_FSM_IDLE, state) lastMsg := m.sendBuf[len(m.sendBuf)-1] sent, _ := bgp.ParseBGPMessage(lastMsg) assert.Equal(uint8(bgp.BGP_MSG_NOTIFICATION), sent.Header.Type) assert.Equal(uint8(bgp.BGP_ERROR_HOLD_TIMER_EXPIRED), sent.Body.(*bgp.BGPNotification).ErrorCode) }