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