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