Line data Source code
1 : /* 2 : * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "bgp/extended-community/tag.h" 6 : 7 : #include <stdio.h> 8 : 9 : #include <algorithm> 10 : #include <string> 11 : 12 : #include "base/parse_object.h" 13 : #include "bgp/extended-community/types.h" 14 : 15 : using std::copy; 16 : using std::string; 17 : 18 3 : Tag::Tag(const bytes_type &data) { 19 3 : copy(data.begin(), data.end(), data_.begin()); 20 3 : } 21 : 22 9 : Tag::Tag(as2_t asn, int tag) { 23 9 : data_[0] = BgpExtendedCommunityType::Experimental; 24 9 : data_[1] = BgpExtendedCommunityExperimentalSubType::Tag; 25 9 : put_value(&data_[2], 2, asn); // ASN 26 9 : put_value(&data_[4], 4, tag); // Tag value 27 9 : } 28 : 29 4 : as2_t Tag::as_number() const { 30 8 : if (data_[0] == BgpExtendedCommunityType::Experimental && 31 4 : data_[1] == BgpExtendedCommunityExperimentalSubType::Tag) { 32 4 : as2_t as_number = get_value(data_.data() + 2, 2); 33 4 : return as_number; 34 : } 35 0 : return 0; 36 : } 37 : 38 4 : int Tag::tag() const { 39 8 : if (data_[0] == BgpExtendedCommunityType::Experimental && 40 4 : data_[1] == BgpExtendedCommunityExperimentalSubType::Tag) { 41 4 : int value = get_value(&data_[4], 4); 42 4 : return value; 43 : } 44 0 : return 0; 45 : } 46 : 47 0 : bool Tag::IsGlobal() const { 48 0 : return (tag() >= kMinGlobalId); 49 : } 50 : 51 4 : string Tag::ToString() const { 52 : char temp[50]; 53 4 : snprintf(temp, sizeof(temp), "tag:%u:%u", as_number(), tag()); 54 4 : return string(temp); 55 : } 56 : 57 0 : Tag4ByteAs::Tag4ByteAs(const bytes_type &data) { 58 0 : copy(data.begin(), data.end(), data_.begin()); 59 0 : } 60 : 61 1 : Tag4ByteAs::Tag4ByteAs(as_t asn, int tag) { 62 1 : data_[0] = BgpExtendedCommunityType::Experimental4ByteAs; 63 1 : data_[1] = BgpExtendedCommunityExperimentalSubType::Tag; 64 1 : put_value(&data_[2], 4, asn); // ASN 65 1 : put_value(&data_[6], 2, tag); // Tag value 66 1 : } 67 : 68 1 : as_t Tag4ByteAs::as_number() const { 69 2 : if (data_[0] == BgpExtendedCommunityType::Experimental4ByteAs && 70 1 : data_[1] == BgpExtendedCommunityExperimentalSubType::Tag) { 71 1 : as_t as_number = get_value(data_.data() + 2, 4); 72 1 : return as_number; 73 : } 74 0 : return 0; 75 : } 76 : 77 1 : int Tag4ByteAs::tag() const { 78 2 : if (data_[0] == BgpExtendedCommunityType::Experimental4ByteAs && 79 1 : data_[1] == BgpExtendedCommunityExperimentalSubType::Tag) { 80 1 : int value = get_value(&data_[6], 2); 81 1 : return value; 82 : } 83 0 : return 0; 84 : } 85 : 86 0 : bool Tag4ByteAs::IsGlobal() const { 87 0 : return (tag() >= kMinGlobalId); 88 : } 89 : 90 1 : string Tag4ByteAs::ToString() const { 91 : char temp[50]; 92 1 : snprintf(temp, sizeof(temp), "tag:%u:%u", as_number(), tag()); 93 1 : return string(temp); 94 : }