diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-11-16 22:54:07 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-11-16 22:54:07 +0900 |
commit | 3c831d7b6dbf486ff1850414c4694bc4b4783d94 (patch) | |
tree | abbb6b6ef24fdc1418920f15acca5e6bc05c33e0 /packet/bgp.go | |
parent | 764b065dc755c637f43b505b43fdd98c95b590fb (diff) |
packet: add helper functions for bgp speaker
The helper functions allows bgp speaker to easily read a header from a
socket, get the body length, read the body data, and parse it.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'packet/bgp.go')
-rw-r--r-- | packet/bgp.go | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/packet/bgp.go b/packet/bgp.go index 681c7a0b..942f2d6f 100644 --- a/packet/bgp.go +++ b/packet/bgp.go @@ -2085,6 +2085,10 @@ type BGPBody interface { Serialize() ([]byte, error) } +const ( + BGP_HEADER_LENGTH = 19 +) + type BGPHeader struct { Marker []byte Len uint16 @@ -2098,9 +2102,6 @@ func (msg *BGPHeader) DecodeFromBytes(data []byte) error { } msg.Len = binary.BigEndian.Uint16(data[16:18]) msg.Type = data[18] - if len(data) < int(msg.Len) { - return fmt.Errorf("Not all BGP message bytes available") - } return nil } @@ -2119,13 +2120,12 @@ type BGPMessage struct { Body BGPBody } -func ParseBGPMessage(data []byte) (*BGPMessage, error) { - msg := &BGPMessage{} - err := msg.Header.DecodeFromBytes(data) - if err != nil { - return nil, err +func parseBody(h *BGPHeader, data []byte) (*BGPMessage, error) { + if len(data) < int(h.Len)-BGP_HEADER_LENGTH { + return nil, fmt.Errorf("Not all BGP message bytes available") } - data = data[19:msg.Header.Len] + msg := &BGPMessage{Header: *h} + switch msg.Header.Type { case BGP_MSG_OPEN: msg.Body = &BGPOpen{} @@ -2138,13 +2138,26 @@ func ParseBGPMessage(data []byte) (*BGPMessage, error) { case BGP_MSG_ROUTE_REFRESH: msg.Body = &BGPRouteRefresh{} } - err = msg.Body.DecodeFromBytes(data) + err := msg.Body.DecodeFromBytes(data) if err != nil { return nil, err } return msg, nil } +func ParseBGPMessage(data []byte) (*BGPMessage, error) { + h := &BGPHeader{} + err := h.DecodeFromBytes(data) + if err != nil { + return nil, err + } + return parseBody(h, data[19:h.Len]) +} + +func ParseBGPBody(h *BGPHeader, data []byte) (*BGPMessage, error) { + return parseBody(h, data) +} + func (msg *BGPMessage) Serialize() ([]byte, error) { b, err := msg.Body.Serialize() if err != nil { |