forked from jwhited/bgpls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_notification.go
98 lines (82 loc) · 2.05 KB
/
packet_notification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package bgpls
import "errors"
// NotifErrCode is a notifcation message error code.
type NotifErrCode uint8
// NotifErrCode values
const (
_ NotifErrCode = iota
NotifErrCodeMessageHeader
NotifErrCodeOpenMessage
NotifErrCodeUpdateMessage
NotifErrCodeHoldTimerExpired
NotifErrCodeFsmError
NotifErrCodeCease
)
// NotifErrSubcode is a notification message error subcode.
type NotifErrSubcode uint8
// message header subcodes
const (
_ NotifErrSubcode = iota
NotifErrSubcodeConnNotSynch
NotifErrSubcodeBadLength
NotifErrSubcodeBadType
)
// open message subcodes
const (
_ NotifErrSubcode = iota
NotifErrSubcodeUnsupportedVersionNumber
NotifErrSubcodeBadPeerAS
NotifErrSubcodeBadBgpID
NotifErrSubcodeUnsupportedOptParam
_
NotifErrSubcodeUnacceptableHoldTime
NotifErrSubcodeUnsupportedCapability
)
// update message subcodes
const (
_ NotifErrSubcode = iota
NotifErrSubcodeMalformedAttr
NotifErrSubcodeUnrecognizedWellKnownAttr
NotifErrSubcodeMissingWellKnownAttr
NotifErrSubcodeAttrFlagsError
NotifErrSubcodeAttrLenError
NotifErrSubcodeInvalidOrigin
_
NotifErrSubcodeInvalidNextHop
NotifErrSubcodeOptionalAttrError
NotifErrSubcodeInvalidNetworkField
NotifErrSubcodeMalformedAsPath
)
// NotificationMessage is a bgp message.
//
// https://tools.ietf.org/html/rfc4271#section-4.5
type NotificationMessage struct {
Code NotifErrCode
Subcode NotifErrSubcode
Data []byte
}
// MessageType returns the appropriate MessageType for NotificationMessage.
func (n *NotificationMessage) MessageType() MessageType {
return NotificationMessageType
}
func (n *NotificationMessage) serialize() ([]byte, error) {
buff := make([]byte, 2)
buff[0] = uint8(n.Code)
buff[1] = uint8(n.Subcode)
if len(n.Data) > 0 {
buff = append(buff, n.Data...)
}
buff = prependHeader(buff, NotificationMessageType)
return buff, nil
}
func (n *NotificationMessage) deserialize(b []byte) error {
if len(b) < 2 {
return errors.New("incomplete notification message")
}
n.Code = NotifErrCode(b[0])
n.Subcode = NotifErrSubcode(b[1])
if len(b) > 2 {
n.Data = b[2:]
}
return nil
}