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