/
bgp_attr_base.cc
55 lines (45 loc) · 1.5 KB
/
bgp_attr_base.cc
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
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_attr_base.h"
#include "base/util.h"
const size_t BgpProtoPrefix::kLabelSize = 3;
BgpProtoPrefix::BgpProtoPrefix() : prefixlen(0), type(0) {
}
uint32_t BgpProtoPrefix::ReadLabel(size_t label_offset) const {
assert((label_offset + kLabelSize) <= prefix.size());
uint32_t label = (prefix[label_offset] << 16 |
prefix[label_offset + 1] << 8 |
prefix[label_offset + 2]) >> 4;
return label;
}
void BgpProtoPrefix::WriteLabel(size_t label_offset, uint32_t label) {
assert((label_offset + kLabelSize) <= prefix.size());
assert(label <= 0xFFFFF);
uint32_t tmp = (label << 4 | 0x1);
for (size_t idx = 0; idx < kLabelSize; ++idx) {
int offset = (kLabelSize - (idx + 1)) * 8;
prefix[label_offset + idx] = ((tmp >> offset) & 0xff);
}
}
int BgpAttribute::CompareTo(const BgpAttribute &rhs) const {
KEY_COMPARE(code, rhs.code);
KEY_COMPARE(subcode, rhs.subcode);
KEY_COMPARE(flags & ~ExtendedLength, rhs.flags & ~ExtendedLength);
return 0;
}
size_t BgpAttribute::EncodeLength() const {
return 0;
}
uint8_t BgpAttribute::GetEncodeFlags() const {
uint8_t value = flags;
if (EncodeLength() >= sizeof(uint8_t) << 8) {
value |= BgpAttribute::ExtendedLength;
}
return value;
}
std::string BgpAttribute::ToString() const {
char repr[80];
snprintf(repr, sizeof(repr), "<code: %d, flags: %02x>", code, flags);
return std::string(repr);
}