LCOV - code coverage report
Current view: top level - bgp - community.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 136 772 17.6 %
Date: 2026-11-16 02:06:09 Functions: 24 101 23.8 %
Legend: Lines: hit not hit

          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/community.h"
       7             : 
       8             : #include <boost/foreach.hpp>
       9             : 
      10             : #include <algorithm>
      11             : #include <string>
      12             : #include <map>
      13             : 
      14             : #include "base/string_util.h"
      15             : #include "bgp/bgp_proto.h"
      16             : #include "bgp/extended-community/tag.h"
      17             : #include "bgp/extended-community/default_gateway.h"
      18             : #include "bgp/extended-community/es_import.h"
      19             : #include "bgp/extended-community/esi_label.h"
      20             : #include "bgp/extended-community/etree.h"
      21             : #include "bgp/extended-community/load_balance.h"
      22             : #include "bgp/extended-community/mac_mobility.h"
      23             : #include "bgp/extended-community/local_sequence_number.h"
      24             : #include "bgp/extended-community/router_mac.h"
      25             : #include "bgp/extended-community/site_of_origin.h"
      26             : #include "bgp/extended-community/source_as.h"
      27             : #include "bgp/extended-community/sub_cluster.h"
      28             : #include "bgp/extended-community/tag.h"
      29             : #include "bgp/extended-community/vrf_route_import.h"
      30             : #include "bgp/large-community/tag.h"
      31             : #include "bgp/tunnel_encap/tunnel_encap.h"
      32             : #include "bgp/origin-vn/origin_vn.h"
      33             : #include "bgp/rtarget/rtarget_address.h"
      34             : #include "bgp/security_group/security_group.h"
      35             : #include "bgp/tunnel_encap/tunnel_encap.h"
      36             : #include "net/community_type.h"
      37             : 
      38             : using std::sort;
      39             : using std::string;
      40             : using std::unique;
      41             : using std::vector;
      42             : 
      43           0 : void CommunitySpec::ToCanonical(BgpAttr *attr) {
      44           0 :     attr->set_community(this);
      45           0 : }
      46             : 
      47           0 : string CommunitySpec::ToString() const {
      48           0 :     string repr;
      49             :     char start[32];
      50           0 :     snprintf(start, sizeof(start), "Communities: %zu [", communities.size());
      51           0 :     repr += start;
      52             : 
      53           0 :     for (size_t i = 0; i < communities.size(); ++i) {
      54             :         char community[12];
      55           0 :         snprintf(community, sizeof(community), " %X", communities[i]);
      56           0 :         repr += community;
      57             :     }
      58           0 :     repr += " ]";
      59             : 
      60           0 :     return repr;
      61           0 : }
      62             : 
      63           0 : Community::Community(CommunityDB *comm_db, const CommunitySpec spec)
      64           0 :     : comm_db_(comm_db), communities_(spec.communities) {
      65           0 :     refcount_ = 0;
      66           0 :     sort(communities_.begin(), communities_.end());
      67             :     vector<uint32_t>::iterator it =
      68           0 :         unique(communities_.begin(), communities_.end());
      69           0 :     communities_.erase(it, communities_.end());
      70           0 : }
      71             : 
      72           0 : int Community::CompareTo(const Community &rhs) const {
      73           0 :     KEY_COMPARE(communities_, rhs.communities_);
      74           0 :     return 0;
      75             : }
      76             : 
      77           0 : size_t CommunitySpec::EncodeLength() const {
      78           0 :     return communities.size() * sizeof(uint32_t);
      79             : }
      80             : 
      81           0 : void Community::Append(uint32_t value) {
      82           0 :     if (ContainsValue(value))
      83           0 :         return;
      84           0 :     communities_.push_back(value);
      85           0 :     sort(communities_.begin(), communities_.end());
      86             : }
      87             : 
      88           0 : void Community::Append(const std::vector<uint32_t> &communities) {
      89           0 :     for (auto community : communities) {
      90           0 :         communities_.push_back(community);
      91             :     }
      92           0 :     sort(communities_.begin(), communities_.end());
      93             :     vector<uint32_t>::iterator it =
      94           0 :         unique(communities_.begin(), communities_.end());
      95           0 :     communities_.erase(it, communities_.end());
      96           0 : }
      97             : 
      98           0 : void Community::Set(const std::vector<uint32_t> &communities) {
      99           0 :     communities_.clear();
     100           0 :     for (auto community : communities) {
     101           0 :         communities_.push_back(community);
     102             :     }
     103           0 : }
     104             : 
     105           0 : void Community::Remove(const std::vector<uint32_t> &communities) {
     106           0 :     for (auto community : communities) {
     107           0 :         communities_.erase(
     108           0 :                std::remove(communities_.begin(), communities_.end(), community),
     109           0 :                communities_.end());
     110             :     }
     111           0 : }
     112           0 : void Community::Remove() {
     113           0 :     comm_db_->Delete(this);
     114           0 : }
     115             : 
     116           0 : bool Community::ContainsValue(uint32_t value) const {
     117           0 :     for (auto community : communities_) {
     118           0 :         if (community == value)
     119           0 :             return true;
     120             :     }
     121           0 :     return false;
     122             : }
     123             : 
     124           0 : void Community::BuildStringList(vector<string> *list) const {
     125           0 :     for (auto community : communities_) {
     126           0 :         string name = CommunityType::CommunityToString(community);
     127           0 :         list->push_back(name);
     128           0 :     }
     129           0 : }
     130             : 
     131          20 : CommunityDB::CommunityDB(BgpServer *server) {
     132          20 : }
     133             : 
     134           0 : CommunityPtr CommunityDB::AppendAndLocate(const Community *src,
     135             :     uint32_t value) {
     136             :     Community *clone;
     137           0 :     if (src) {
     138           0 :         clone = new Community(*src);
     139             :     } else {
     140           0 :         clone = new Community(this);
     141             :     }
     142             : 
     143           0 :     clone->Append(value);
     144           0 :     return Locate(clone);
     145             : }
     146             : 
     147           0 : CommunityPtr CommunityDB::AppendAndLocate(const Community *src,
     148             :     const std::vector<uint32_t> &value) {
     149             :     Community *clone;
     150           0 :     if (src) {
     151           0 :         clone = new Community(*src);
     152             :     } else {
     153           0 :         clone = new Community(this);
     154             :     }
     155             : 
     156           0 :     clone->Append(value);
     157           0 :     return Locate(clone);
     158             : }
     159             : 
     160           0 : CommunityPtr CommunityDB::SetAndLocate(const Community *src,
     161             :     const std::vector<uint32_t> &value) {
     162             :     Community *clone;
     163           0 :     if (src) {
     164           0 :         clone = new Community(*src);
     165             :     } else {
     166           0 :         clone = new Community(this);
     167             :     }
     168             : 
     169           0 :     clone->Set(value);
     170           0 :     return Locate(clone);
     171             : }
     172             : 
     173           0 : CommunityPtr CommunityDB::RemoveAndLocate(const Community *src,
     174             :     const std::vector<uint32_t> &value) {
     175             :     Community *clone;
     176           0 :     if (src) {
     177           0 :         clone = new Community(*src);
     178             :     } else {
     179           0 :         clone = new Community(this);
     180             :     }
     181             : 
     182           0 :     clone->Remove(value);
     183           0 :     return Locate(clone);
     184             : }
     185             : 
     186           0 : CommunityPtr CommunityDB::RemoveAndLocate(const Community *src,
     187             :                                           uint32_t value) {
     188           0 :     Community::CommunityList communities;
     189           0 :     communities.push_back(value);
     190           0 :     return RemoveAndLocate(src, communities);
     191           0 : }
     192             : 
     193        6036 : string ExtCommunitySpec::ToString() const {
     194             :     char repr[80];
     195        6036 :     snprintf(repr, sizeof(repr), "ExtCommunity <code: %d, flags: %02x>:%d",
     196        6036 :              code, flags, (uint32_t)communities.size());
     197        6036 :     return string(repr);
     198             : }
     199             : 
     200        8340 : LargeCommunitySpec LargeCommunitySpec::FromTag(
     201             :     const ExtCommunitySpec &ex_spec) {
     202        8340 :     LargeCommunitySpec large_spec;
     203             :     using extcomm_iterator = decltype(large_spec.communities)::iterator;
     204        8340 :     std::map<uint16_t, extcomm_iterator> tno_vs_tspec;
     205        8340 :     std::vector<std::pair<uint32_t,uint16_t> > asn4_vs_tno;
     206        8340 :     const uint64_t ExtCommTag = uint64_t(
     207             :         (BgpExtendedCommunityType::Experimental << 8) |
     208             :         (BgpExtendedCommunityExperimentalSubType::Tag)) << 48;
     209        8340 :     const uint64_t ExtCommTag4 = uint64_t(
     210             :         (BgpExtendedCommunityType::Experimental4ByteAs << 8) |
     211             :         (BgpExtendedCommunityExperimentalSubType::Tag)) << 48;
     212             :     uint16_t asn_or_tno;
     213             :     uint32_t asn4;
     214             :     uint16_t tno;
     215       54020 :     for (const auto &val64 : ex_spec.communities) {
     216       45680 :         if ((0xFFFF000000000000 & val64) == ExtCommTag) {
     217           0 :             asn_or_tno = (0x0000FFFF00000000 & val64) >> 32;
     218             :             std::vector<uint32_t> large_comm = {
     219           0 :                 asn_or_tno,
     220           0 :                 0x01000000 | uint32_t((val64 & 0x00000000FFFF0000) >> 16),
     221           0 :                 uint32_t(val64 & 0x000000000000FFFF)
     222           0 :             };
     223           0 :             extcomm_iterator it_tspec =  large_spec.communities.insert(
     224           0 :                 large_spec.communities.end(),
     225             :                 large_comm.begin(),
     226             :                 large_comm.end());
     227           0 :             tno_vs_tspec.insert({asn_or_tno,
     228             :                                 it_tspec});
     229           0 :             continue;
     230           0 :         }
     231             : 
     232       45680 :         if ((0xFFFF000000000000 & val64) == ExtCommTag4) {
     233           0 :             asn4 = (0x0000FFFFFFFF0000 & val64) >> 16;
     234           0 :             tno  = (0x000000000000FFFF & val64);
     235           0 :             asn4_vs_tno.push_back({asn4, tno});
     236             :         }
     237             :     }
     238             :     // loop over all 4-byte ASNs and modify their value in large_spec
     239        8340 :     for(const auto &[asn4, tno] : asn4_vs_tno) {
     240           0 :         auto &large_comm_asn = tno_vs_tspec[tno];
     241           0 :         if (large_comm_asn == large_spec.communities.end()) {
     242           0 :             continue;
     243             :         }
     244           0 :         *large_comm_asn = asn4;
     245             :     }
     246       16680 :     return large_spec;
     247        8340 : }
     248             : 
     249           0 : ExtCommunitySpec LargeCommunitySpec::RemoveTags(
     250             :     const ExtCommunitySpec& ex_spec) {
     251           0 :     const uint64_t ExtCommTag = uint64_t(
     252             :         (BgpExtendedCommunityType::Experimental << 8) |
     253             :         (BgpExtendedCommunityExperimentalSubType::Tag)) << 48;
     254           0 :     const uint64_t ExtCommTag4 = uint64_t(
     255             :         (BgpExtendedCommunityType::Experimental4ByteAs << 8) |
     256             :         (BgpExtendedCommunityExperimentalSubType::Tag)) << 48;
     257           0 :     ExtCommunitySpec new_ex_spec;
     258           0 :     for (const auto &val64 : ex_spec.communities) {
     259           0 :         if ((0xFFFF000000000000 & val64) != ExtCommTag &&
     260           0 :             (0xFFFF000000000000 & val64) != ExtCommTag4) {
     261           0 :             new_ex_spec.communities.push_back(val64);
     262             :         }
     263             :     }
     264           0 :     return new_ex_spec;
     265           0 : }
     266             : 
     267       26250 : size_t ExtCommunitySpec::EncodeLength() const {
     268       26250 :     return communities.size() * sizeof(uint64_t);
     269             : }
     270             : 
     271           0 : int ExtCommunitySpec::CompareTo(const BgpAttribute &rhs_attr) const {
     272           0 :     int ret = BgpAttribute::CompareTo(rhs_attr);
     273           0 :     if (ret != 0) return ret;
     274           0 :     KEY_COMPARE(communities,
     275             :         static_cast<const ExtCommunitySpec &>(rhs_attr).communities);
     276           0 :     return 0;
     277             : }
     278             : 
     279        8340 : void ExtCommunitySpec::ToCanonical(BgpAttr *attr) {
     280        8340 :     attr->set_ext_community(this);
     281        8340 : }
     282             : 
     283           0 : void ExtCommunitySpec::AddTunnelEncaps(vector<string> encaps) {
     284           0 :     for (vector<string>::size_type i = 0; i < encaps.size(); i++) {
     285           0 :         string encap_str = encaps[i];
     286           0 :         TunnelEncap tun_encap(encap_str);
     287           0 :         communities.push_back(tun_encap.GetExtCommunityValue());
     288           0 :     }
     289           0 : }
     290             : 
     291       59027 : int ExtCommunity::CompareTo(const ExtCommunity &rhs) const {
     292       59027 :     KEY_COMPARE(communities_.size(), rhs.communities_.size());
     293             : 
     294       42215 :     ExtCommunityList::const_iterator i, j;
     295       42215 :     for (i = communities_.begin(), j = rhs.communities_.begin();
     296      283928 :          i < communities_.end(); ++i, ++j) {
     297      241742 :         if (*i < *j) {
     298           0 :             return -1;
     299             :         }
     300      241750 :         if (*i > *j) {
     301           0 :             return 1;
     302             :         }
     303             :     }
     304       42218 :     return 0;
     305             : }
     306             : 
     307           0 : void ExtCommunity::Remove(const ExtCommunityList &list) {
     308           0 :     for (ExtCommunityList::const_iterator it = list.begin();
     309           0 :          it != list.end(); ++it) {
     310           0 :         communities_.erase(std::remove(communities_.begin(),
     311           0 :                     communities_.end(), *it), communities_.end());
     312             :     }
     313           0 : }
     314         440 : void ExtCommunity::Remove() {
     315         440 :     extcomm_db_->Delete(this);
     316         440 : }
     317             : 
     318           0 : void ExtCommunity::Set(const ExtCommunityList &list) {
     319           0 :     communities_.clear();
     320           0 :     for (ExtCommunityList::const_iterator it = list.begin();
     321           0 :          it != list.end(); ++it) {
     322           0 :         communities_.push_back(*it);
     323             :     }
     324           0 : }
     325             : 
     326        1152 : void ExtCommunity::Append(const ExtCommunityList &list) {
     327        1152 :     communities_.insert(communities_.end(), list.begin(), list.end());
     328        1151 :     sort(communities_.begin(), communities_.end());
     329             :     ExtCommunityList::iterator it =
     330        1151 :         unique(communities_.begin(), communities_.end());
     331        1152 :     communities_.erase(it, communities_.end());
     332        1152 : }
     333             : 
     334           0 : void ExtCommunity::Append(const ExtCommunityValue &value) {
     335           0 :     communities_.push_back(value);
     336           0 :     sort(communities_.begin(), communities_.end());
     337             :     ExtCommunityList::iterator it =
     338           0 :         unique(communities_.begin(), communities_.end());
     339           0 :     communities_.erase(it, communities_.end());
     340           0 : }
     341             : 
     342           0 : ExtCommunity::ExtCommunityValue ExtCommunity::FromHexString(
     343             :         const string &comm, boost::system::error_code *errorp) {
     344             :     ExtCommunityValue data;
     345           0 :     put_value(&data[0], 8, 0);
     346             :     char *end;
     347           0 :     uint64_t value = strtoull(comm.c_str(), &end, 16);
     348           0 :     if (value == 0 || *end) {
     349             :         // e.g. 0 or 12x34ff (invalid hex)
     350           0 :         if (errorp != NULL) {
     351           0 :             *errorp = make_error_code(
     352             :                     boost::system::errc::invalid_argument);
     353           0 :             return data;
     354             :         }
     355             :     }
     356           0 :     if (comm[0] == '0' && (comm[1] == 'x' || comm[1] == 'X')) {
     357           0 :         if (comm.length() > 18 && errorp != NULL) {
     358             :             // e.g. 0xabcdef0123456789f is an invalid 8byte hex value
     359           0 :             *errorp = make_error_code(
     360             :                     boost::system::errc::invalid_argument);
     361           0 :             return data;
     362             :         }
     363             :     } else {
     364           0 :         if (comm.length() > 16 && errorp != NULL) {
     365             :             // e.g. abcdef0123456789f is an invalid 8byte hex value
     366           0 :             *errorp = make_error_code(
     367             :                     boost::system::errc::invalid_argument);
     368           0 :             return data;
     369             :         }
     370             :     }
     371           0 :     put_value(&data[0], 8, value);
     372           0 :     return data;
     373             : }
     374             : 
     375           0 : ExtCommunity::ExtCommunityList ExtCommunity::ExtCommunityFromString(
     376             :         const string &comm) {
     377           0 :     ExtCommunityList commList;
     378             :     ExtCommunityValue value;
     379           0 :     size_t pos = comm.find(':');
     380           0 :     string first(comm.substr(0, pos));
     381           0 :     boost::system::error_code error;
     382           0 :     if (first == "soo") {
     383           0 :         SiteOfOrigin soo = SiteOfOrigin::FromString(comm, &error);
     384           0 :         if (error) {
     385           0 :             return commList;
     386             :         }
     387           0 :         commList.push_back(soo.GetExtCommunity());
     388           0 :     } else if (first == "target") {
     389           0 :         RouteTarget rt = RouteTarget::FromString(comm, &error);
     390           0 :         if (error) {
     391           0 :             return commList;
     392             :         }
     393           0 :         commList.push_back(rt.GetExtCommunity());
     394           0 :     } else if (first == "source-as") {
     395           0 :         SourceAs sas = SourceAs::FromString(comm, &error);
     396           0 :         if (error) {
     397           0 :             return commList;
     398             :         }
     399           0 :         commList.push_back(sas.GetExtCommunity());
     400           0 :     } else if (first == "rt-import") {
     401           0 :         VrfRouteImport vit = VrfRouteImport::FromString(comm, &error);
     402           0 :         if (error) {
     403           0 :             return commList;
     404             :         }
     405           0 :         commList.push_back(vit.GetExtCommunity());
     406           0 :     } else if (first == "subcluster") {
     407           0 :         SubCluster sc = SubCluster::FromString(comm, &error);
     408           0 :         if (error) {
     409           0 :             return commList;
     410             :         }
     411           0 :         commList.push_back(sc.GetExtCommunity());
     412             :     } else {
     413           0 :         value = FromHexString(comm, &error);
     414           0 :         if (error) {
     415           0 :             return commList;
     416             :         }
     417           0 :         commList.push_back(value);
     418             :     }
     419           0 :     return commList;
     420           0 : }
     421             : 
     422           0 : string ExtCommunity::ToHexString(const ExtCommunityValue &comm) {
     423             :     char temp[50];
     424           0 :     int len = 0;
     425           0 :     for (size_t i = 0; i < comm.size(); i++) {
     426           0 :         len += snprintf(temp+len, sizeof(temp) - len, "%02x", (comm)[i]);
     427             :     }
     428           0 :     return(string(temp));
     429             : }
     430             : 
     431           0 : string ExtCommunity::ToString(const ExtCommunityValue &comm) {
     432           0 :     if (is_route_target(comm)) {
     433           0 :         RouteTarget rt(comm);
     434           0 :         return(rt.ToString());
     435           0 :     } else if (is_default_gateway(comm)) {
     436           0 :         DefaultGateway dgw(comm);
     437           0 :         return(dgw.ToString());
     438           0 :     } else if (is_es_import(comm)) {
     439           0 :         EsImport es_import(comm);
     440           0 :         return(es_import.ToString());
     441           0 :     } else if (is_esi_label(comm)) {
     442           0 :         EsiLabel esi_label(comm);
     443           0 :         return(esi_label.ToString());
     444           0 :     } else if (is_mac_mobility(comm)) {
     445           0 :         MacMobility mm(comm);
     446           0 :         return(mm.ToString());
     447           0 :     } else if (is_local_sequence_number(comm)) {
     448           0 :         LocalSequenceNumber lsn(comm);
     449           0 :         return lsn.ToString();
     450           0 :     } else if (is_etree(comm)) {
     451           0 :         ETree etree(comm);
     452           0 :         return(etree.ToString());
     453           0 :     } else if (is_router_mac(comm)) {
     454           0 :         RouterMac router_mac(comm);
     455           0 :         return(router_mac.ToString());
     456           0 :     } else if (is_origin_vn(comm)) {
     457           0 :         OriginVn origin_vn(comm);
     458           0 :         return(origin_vn.ToString());
     459           0 :     } else if (is_security_group(comm)) {
     460           0 :         SecurityGroup sg(comm);
     461           0 :         return(sg.ToString());
     462           0 :     } else if (is_site_of_origin(comm)) {
     463           0 :         SiteOfOrigin soo(comm);
     464           0 :         return(soo.ToString());
     465           0 :     } else if (is_tunnel_encap(comm)) {
     466           0 :         TunnelEncap encap(comm);
     467           0 :         return(encap.ToString());
     468           0 :     } else if (is_load_balance(comm)) {
     469           0 :         LoadBalance load_balance(comm);
     470           0 :         return(load_balance.ToString());
     471           0 :     } else if (is_tag(comm)) {
     472           0 :         Tag tag(comm);
     473           0 :         return(tag.ToString());
     474           0 :     } else if (is_source_as(comm)) {
     475           0 :         SourceAs sas(comm);
     476           0 :         return(sas.ToString());
     477           0 :     } else if (is_vrf_route_import(comm)) {
     478           0 :         VrfRouteImport rt_import(comm);
     479           0 :         return(rt_import.ToString());
     480           0 :     } else if (is_sub_cluster(comm)) {
     481           0 :         SubCluster sc(comm);
     482           0 :         return(sc.ToString());
     483             :     }
     484           0 :     return ToHexString(comm);
     485             : }
     486             : 
     487           0 : bool ExtCommunity::ContainsRTarget(const ExtCommunityValue &val) const {
     488           0 :     for (ExtCommunityList::const_iterator it = communities_.begin();
     489           0 :          it != communities_.end(); ++it) {
     490           0 :         if (ExtCommunity::is_route_target(*it) && *it == val)
     491           0 :             return true;
     492             :     }
     493           0 :     return false;
     494             : }
     495             : 
     496        7552 : bool ExtCommunity::ContainsOriginVn(const ExtCommunityValue &val) const {
     497        7552 :     for (ExtCommunityList::const_iterator it = communities_.begin();
     498       67968 :          it != communities_.end(); ++it) {
     499       60416 :         if (ExtCommunity::is_origin_vn(*it) && *it == val)
     500           0 :             return true;
     501             :     }
     502        7552 :     return false;
     503             : }
     504             : 
     505        7552 : bool ExtCommunity::ContainsOriginVn(as_t asn, uint32_t vn_index) const {
     506             :     if (asn <= 0xffffffff) {
     507        7552 :         OriginVn origin_vn(asn, vn_index);
     508        7552 :         return ContainsOriginVn(origin_vn.GetExtCommunity());
     509             :     }
     510             :     OriginVn origin_vn4(asn, AS_TRANS);
     511             :     OriginVn origin_vn(AS_TRANS, vn_index);
     512             :     return (ContainsOriginVn(origin_vn.GetExtCommunity()) &&
     513             :                 ContainsOriginVn(origin_vn4.GetExtCommunity()));
     514             : }
     515             : 
     516           0 : bool ExtCommunity::ContainsSourceAs(const ExtCommunityValue &val) const {
     517           0 :     for (ExtCommunityList::const_iterator it = communities_.begin();
     518           0 :          it != communities_.end(); ++it) {
     519           0 :         if (ExtCommunity::is_source_as(*it) && *it == val)
     520           0 :             return true;
     521             :     }
     522           0 :     return false;
     523             : }
     524             : 
     525       27392 : uint32_t ExtCommunity::GetSubClusterId() const {
     526       27392 :     for (ExtCommunityList::const_iterator it = communities_.begin();
     527      177834 :             it != communities_.end(); ++it) {
     528      150450 :         if (ExtCommunity::is_sub_cluster(*it)) {
     529           0 :             SubCluster sc(*it);
     530           0 :             return sc.GetId();
     531             :         }
     532             :     }
     533       27380 :     return 0;
     534             : }
     535             : 
     536           0 : bool ExtCommunity::ContainsVrfRouteImport(const ExtCommunityValue &val) const {
     537           0 :     for (ExtCommunityList::const_iterator it = communities_.begin();
     538           0 :          it != communities_.end(); ++it) {
     539           0 :         if (ExtCommunity::is_vrf_route_import(*it) && *it == val)
     540           0 :             return true;
     541             :     }
     542           0 :     return false;
     543             : }
     544             : 
     545           0 : void ExtCommunity::RemoveMFlags() {
     546           0 :     for (ExtCommunityList::iterator it = communities_.begin();
     547           0 :          it != communities_.end(); ) {
     548           0 :         if (ExtCommunity::is_multicast_flags(*it)) {
     549           0 :             it = communities_.erase(it);
     550             :         } else {
     551           0 :             ++it;
     552             :         }
     553             :     }
     554           0 : }
     555             : 
     556           0 : void ExtCommunity::RemoveRTarget() {
     557           0 :     for (ExtCommunityList::iterator it = communities_.begin();
     558           0 :          it != communities_.end(); ) {
     559           0 :         if (ExtCommunity::is_route_target(*it)) {
     560           0 :             it = communities_.erase(it);
     561             :         } else {
     562           0 :             ++it;
     563             :         }
     564             :     }
     565           0 : }
     566             : 
     567           0 : void ExtCommunity::RemoveSGID() {
     568           0 :     for (ExtCommunityList::iterator it = communities_.begin();
     569           0 :          it != communities_.end(); ) {
     570           0 :         if (ExtCommunity::is_security_group(*it) ||
     571           0 :                 ExtCommunity::is_security_group4(*it)) {
     572           0 :             it = communities_.erase(it);
     573             :         } else {
     574           0 :             ++it;
     575             :         }
     576             :     }
     577           0 : }
     578             : 
     579           0 : void ExtCommunity::RemoveTag() {
     580           0 :     for (ExtCommunityList::iterator it = communities_.begin();
     581           0 :          it != communities_.end(); ) {
     582           0 :         if (ExtCommunity::is_tag(*it) || ExtCommunity::is_tag4(*it)) {
     583           0 :             it = communities_.erase(it);
     584             :         } else {
     585           0 :             ++it;
     586             :         }
     587             :     }
     588           0 : }
     589             : 
     590           0 : void ExtCommunity::RemoveSiteOfOrigin() {
     591           0 :     for (ExtCommunityList::iterator it = communities_.begin();
     592           0 :          it != communities_.end(); ) {
     593           0 :         if (ExtCommunity::is_site_of_origin(*it)) {
     594           0 :             it = communities_.erase(it);
     595             :         } else {
     596           0 :             ++it;
     597             :         }
     598             :     }
     599           0 : }
     600             : 
     601           0 : void ExtCommunity::RemoveSourceAS() {
     602           0 :     for (ExtCommunityList::iterator it = communities_.begin();
     603           0 :          it != communities_.end(); ) {
     604           0 :         if (ExtCommunity::is_source_as(*it)) {
     605           0 :             it = communities_.erase(it);
     606             :         } else {
     607           0 :             ++it;
     608             :         }
     609             :     }
     610           0 : }
     611           0 : void ExtCommunity::RemoveVrfRouteImport() {
     612           0 :     for (ExtCommunityList::iterator it = communities_.begin();
     613           0 :          it != communities_.end(); ) {
     614           0 :         if (ExtCommunity::is_vrf_route_import(*it)) {
     615           0 :             it = communities_.erase(it);
     616             :         } else {
     617           0 :             ++it;
     618             :         }
     619             :     }
     620           0 : }
     621             : 
     622       11558 : void ExtCommunity::RemoveOriginVn() {
     623       11558 :     for (ExtCommunityList::iterator it = communities_.begin();
     624       79074 :          it != communities_.end(); ) {
     625       67519 :         if (ExtCommunity::is_origin_vn(*it))
     626           0 :             it = communities_.erase(it);
     627             :         else
     628       67517 :             ++it;
     629             :     }
     630       11546 : }
     631             : 
     632           0 : void ExtCommunity::RemoveTunnelEncapsulation() {
     633           0 :     for (ExtCommunityList::iterator it = communities_.begin();
     634           0 :          it != communities_.end(); ) {
     635           0 :         if (ExtCommunity::is_tunnel_encap(*it)) {
     636           0 :             it = communities_.erase(it);
     637             :         } else {
     638           0 :             ++it;
     639             :         }
     640             :     }
     641           0 : }
     642             : 
     643           0 : void ExtCommunity::RemoveLoadBalance() {
     644           0 :     for (ExtCommunityList::iterator it = communities_.begin();
     645           0 :          it != communities_.end(); ) {
     646           0 :         if (ExtCommunity::is_load_balance(*it)) {
     647           0 :             it = communities_.erase(it);
     648             :         } else {
     649           0 :             ++it;
     650             :         }
     651             :     }
     652           0 : }
     653             : 
     654           0 : void ExtCommunity::RemoveSubCluster() {
     655           0 :     for (ExtCommunityList::iterator it = communities_.begin();
     656           0 :          it != communities_.end(); ) {
     657           0 :         if (ExtCommunity::is_sub_cluster(*it)) {
     658           0 :             it = communities_.erase(it);
     659             :         } else {
     660           0 :             ++it;
     661             :         }
     662             :     }
     663           0 : }
     664             : 
     665       28416 : vector<string> ExtCommunity::GetTunnelEncap() const {
     666       28416 :     vector<string> encap_list;
     667       28426 :     for (ExtCommunityList::const_iterator iter = communities_.begin();
     668      181281 :          iter != communities_.end(); ++iter) {
     669      152801 :         if (!ExtCommunity::is_tunnel_encap(*iter))
     670      124043 :             continue;
     671       28801 :         TunnelEncap encap(*iter);
     672       28821 :         if (encap.tunnel_encap() == TunnelEncapType::UNSPEC)
     673           0 :             continue;
     674       28798 :         encap_list.push_back(encap.ToXmppString());
     675             :     }
     676             : 
     677       28439 :     sort(encap_list.begin(), encap_list.end());
     678             :     vector<string>::iterator encap_iter =
     679       28475 :         unique(encap_list.begin(), encap_list.end());
     680       28459 :     encap_list.erase(encap_iter, encap_list.end());
     681       56908 :     return encap_list;
     682           0 : }
     683             : 
     684           0 : vector<int> ExtCommunity::GetTagList(as2_t asn) const {
     685           0 :     vector<int> tag_list;
     686           0 :     for (ExtCommunityList::const_iterator iter = communities_.begin();
     687           0 :          iter != communities_.end(); ++iter) {
     688           0 :         if (!ExtCommunity::is_tag(*iter))
     689           0 :             continue;
     690           0 :         Tag tag_comm(*iter);
     691           0 :         if (asn && tag_comm.as_number() != asn && !tag_comm.IsGlobal())
     692           0 :             continue;
     693           0 :         tag_list.push_back(tag_comm.tag());
     694             :     }
     695             : 
     696           0 :     sort(tag_list.begin(), tag_list.end());
     697           0 :     vector<int>::iterator tag_iter = unique(tag_list.begin(), tag_list.end());
     698           0 :     tag_list.erase(tag_iter, tag_list.end());
     699           0 :     return tag_list;
     700           0 : }
     701             : 
     702           0 : vector<int> ExtCommunity::GetTag4List(as_t asn) const {
     703           0 :     vector<int> tag_list;
     704           0 :     for (ExtCommunityList::const_iterator iter = communities_.begin();
     705           0 :          iter != communities_.end(); ++iter) {
     706           0 :         if (!ExtCommunity::is_tag4(*iter))
     707           0 :             continue;
     708           0 :         Tag4ByteAs tag_comm(*iter);
     709           0 :         if (asn && tag_comm.as_number() != asn && !tag_comm.IsGlobal())
     710           0 :             continue;
     711           0 :         vector<int> matching_tag_list = GetTagList(tag_comm.tag());
     712           0 :         tag_list.insert(tag_list.end(), matching_tag_list.begin(),
     713             :                         matching_tag_list.end());
     714           0 :         tag_list.push_back(tag_comm.tag());
     715           0 :     }
     716           0 :     if ((asn <= 0xffff) && tag_list.size() == 0)
     717           0 :         tag_list = GetTagList(asn);
     718             : 
     719           0 :     sort(tag_list.begin(), tag_list.end());
     720           0 :     vector<int>::iterator tag_iter = unique(tag_list.begin(), tag_list.end());
     721           0 :     tag_list.erase(tag_iter, tag_list.end());
     722           0 :     return tag_list;
     723           0 : }
     724             : 
     725        8508 : bool ExtCommunity::ContainsTunnelEncapVxlan() const {
     726        8508 :     for (ExtCommunityList::const_iterator iter = communities_.begin();
     727       62560 :          iter != communities_.end(); ++iter) {
     728       54054 :         if (!ExtCommunity::is_tunnel_encap(*iter))
     729       42668 :             continue;
     730       11388 :         TunnelEncap encap(*iter);
     731       11388 :         if (encap.tunnel_encap() == TunnelEncapType::VXLAN)
     732           0 :             return true;
     733             :     }
     734        8504 :     return false;
     735             : }
     736             : 
     737       28425 : int ExtCommunity::GetOriginVnIndex() const {
     738       28425 :     for (ExtCommunityList::const_iterator iter = communities_.begin();
     739      181275 :          iter != communities_.end(); ++iter) {
     740      152857 :         if (ExtCommunity::is_origin_vn(*iter)) {
     741           0 :             OriginVn origin_vn(*iter);
     742           0 :             return origin_vn.vn_index();
     743             :         }
     744             :     }
     745       28431 :     return -1;
     746             : }
     747             : 
     748        8339 : ExtCommunity::ExtCommunity(ExtCommunityDB *extcomm_db,
     749        8339 :         const ExtCommunitySpec spec) : extcomm_db_(extcomm_db) {
     750        8339 :     refcount_ = 0;
     751        8340 :     for (vector<uint64_t>::const_iterator it = spec.communities.begin();
     752       54018 :          it < spec.communities.end(); ++it) {
     753             :         ExtCommunityValue comm;
     754       45679 :         put_value(comm.data(), comm.size(), *it);
     755       45679 :         communities_.push_back(comm);
     756             :     }
     757        8340 :     sort(communities_.begin(), communities_.end());
     758             :     ExtCommunityList::iterator it =
     759        8340 :         unique(communities_.begin(), communities_.end());
     760        8340 :     communities_.erase(it, communities_.end());
     761        8340 : }
     762             : 
     763          20 : ExtCommunityDB::ExtCommunityDB(BgpServer *server) {
     764          20 : }
     765             : 
     766        1151 : ExtCommunityPtr ExtCommunityDB::AppendAndLocate(const ExtCommunity *src,
     767             :         const ExtCommunity::ExtCommunityList &list) {
     768             :     ExtCommunity *clone;
     769        1151 :     if (src) {
     770        1151 :         clone = new ExtCommunity(*src);
     771             :     } else {
     772           0 :         clone = new ExtCommunity(this);
     773             :     }
     774             : 
     775        1152 :     clone->Append(list);
     776        1152 :     return Locate(clone);
     777             : }
     778             : 
     779           0 : ExtCommunityPtr ExtCommunityDB::AppendAndLocate(const ExtCommunity *src,
     780             :         const ExtCommunity::ExtCommunityValue &value) {
     781           0 :     ExtCommunity::ExtCommunityList list;
     782           0 :     list.push_back(value);
     783           0 :     return AppendAndLocate(src, list);
     784           0 : }
     785             : 
     786           0 : ExtCommunityPtr ExtCommunityDB::RemoveAndLocate(const ExtCommunity *src,
     787             :         const ExtCommunity::ExtCommunityList &list) {
     788             :     ExtCommunity *clone;
     789           0 :     if (src) {
     790           0 :         clone = new ExtCommunity(*src);
     791             :     } else {
     792           0 :         clone = new ExtCommunity(this);
     793             :     }
     794             : 
     795           0 :     clone->Remove(list);
     796           0 :     return Locate(clone);
     797             : }
     798             : 
     799           0 : ExtCommunityPtr ExtCommunityDB::ReplaceMFlagsAndLocate(const ExtCommunity *src,
     800             :         const ExtCommunity::ExtCommunityList &export_list) {
     801             :     ExtCommunity *clone;
     802           0 :     if (src) {
     803           0 :         clone = new ExtCommunity(*src);
     804             :     } else {
     805           0 :         clone = new ExtCommunity(this);
     806             :     }
     807             : 
     808           0 :     clone->RemoveMFlags();
     809           0 :     clone->Append(export_list);
     810           0 :     return Locate(clone);
     811             : }
     812             : 
     813           0 : ExtCommunityPtr ExtCommunityDB::ReplaceRTargetAndLocate(const ExtCommunity *src,
     814             :         const ExtCommunity::ExtCommunityList &export_list) {
     815             :     ExtCommunity *clone;
     816           0 :     if (src) {
     817           0 :         clone = new ExtCommunity(*src);
     818             :     } else {
     819           0 :         clone = new ExtCommunity(this);
     820             :     }
     821             : 
     822           0 :     clone->RemoveRTarget();
     823           0 :     clone->Append(export_list);
     824           0 :     return Locate(clone);
     825             : }
     826             : 
     827           0 : ExtCommunityPtr ExtCommunityDB::ReplaceSGIDListAndLocate(
     828             :     const ExtCommunity *src,
     829             :     const ExtCommunity::ExtCommunityList &sgid_list) {
     830             :     ExtCommunity *clone;
     831           0 :     if (src) {
     832           0 :         clone = new ExtCommunity(*src);
     833             :     } else {
     834           0 :         clone = new ExtCommunity(this);
     835             :     }
     836             : 
     837           0 :     clone->RemoveSGID();
     838           0 :     clone->Append(sgid_list);
     839           0 :     return Locate(clone);
     840             : }
     841             : 
     842           0 : ExtCommunityPtr ExtCommunityDB::ReplaceTagListAndLocate(
     843             :     const ExtCommunity *src,
     844             :     const ExtCommunity::ExtCommunityList &tag_list) {
     845             :     ExtCommunity *clone;
     846           0 :     if (src) {
     847           0 :         clone = new ExtCommunity(*src);
     848             :     } else {
     849           0 :         clone = new ExtCommunity(this);
     850             :     }
     851             : 
     852           0 :     clone->RemoveTag();
     853           0 :     clone->Append(tag_list);
     854           0 :     return Locate(clone);
     855             : }
     856             : 
     857           0 : ExtCommunityPtr ExtCommunityDB::RemoveSiteOfOriginAndLocate(
     858             :         const ExtCommunity *src) {
     859             :     ExtCommunity *clone;
     860           0 :     if (src) {
     861           0 :         clone = new ExtCommunity(*src);
     862             :     } else {
     863           0 :         clone = new ExtCommunity(this);
     864             :     }
     865             : 
     866           0 :     clone->RemoveSiteOfOrigin();
     867           0 :     return Locate(clone);
     868             : }
     869             : 
     870           0 : ExtCommunityPtr ExtCommunityDB::ReplaceSiteOfOriginAndLocate(
     871             :         const ExtCommunity *src,
     872             :         const ExtCommunity::ExtCommunityValue &soo) {
     873             :     ExtCommunity *clone;
     874           0 :     if (src) {
     875           0 :         clone = new ExtCommunity(*src);
     876             :     } else {
     877           0 :         clone = new ExtCommunity(this);
     878             :     }
     879             : 
     880           0 :     clone->RemoveSiteOfOrigin();
     881           0 :     clone->Append(soo);
     882           0 :     return Locate(clone);
     883             : }
     884             : 
     885           0 : ExtCommunityPtr ExtCommunityDB::RemoveSourceASAndLocate(
     886             :         const ExtCommunity *src) {
     887             :     ExtCommunity *clone;
     888           0 :     if (src) {
     889           0 :         clone = new ExtCommunity(*src);
     890             :     } else {
     891           0 :         clone = new ExtCommunity(this);
     892             :     }
     893             : 
     894           0 :     clone->RemoveSourceAS();
     895           0 :     return Locate(clone);
     896             : }
     897             : 
     898           0 : ExtCommunityPtr ExtCommunityDB::ReplaceSourceASAndLocate(
     899             :         const ExtCommunity *src,
     900             :         const ExtCommunity::ExtCommunityValue &sas) {
     901             :     ExtCommunity *clone;
     902           0 :     if (src) {
     903           0 :         clone = new ExtCommunity(*src);
     904             :     } else {
     905           0 :         clone = new ExtCommunity(this);
     906             :     }
     907             : 
     908           0 :     clone->RemoveSourceAS();
     909           0 :     clone->Append(sas);
     910           0 :     return Locate(clone);
     911             : }
     912             : 
     913           0 : ExtCommunityPtr ExtCommunityDB::RemoveVrfRouteImportAndLocate(
     914             :         const ExtCommunity *src) {
     915             :     ExtCommunity *clone;
     916           0 :     if (src) {
     917           0 :         clone = new ExtCommunity(*src);
     918             :     } else {
     919           0 :         clone = new ExtCommunity(this);
     920             :     }
     921             : 
     922           0 :     clone->RemoveVrfRouteImport();
     923           0 :     return Locate(clone);
     924             : }
     925             : 
     926           0 : ExtCommunityPtr ExtCommunityDB::ReplaceVrfRouteImportAndLocate(
     927             :         const ExtCommunity *src,
     928             :         const ExtCommunity::ExtCommunityValue &vit) {
     929             :     ExtCommunity *clone;
     930           0 :     if (src) {
     931           0 :         clone = new ExtCommunity(*src);
     932             :     } else {
     933           0 :         clone = new ExtCommunity(this);
     934             :     }
     935             : 
     936           0 :     clone->RemoveVrfRouteImport();
     937           0 :     clone->Append(vit);
     938           0 :     return Locate(clone);
     939             : }
     940             : 
     941       11548 : ExtCommunityPtr ExtCommunityDB::RemoveOriginVnAndLocate(
     942             :         const ExtCommunity *src) {
     943             :     ExtCommunity *clone;
     944       11548 :     if (src) {
     945       11547 :         clone = new ExtCommunity(*src);
     946             :     } else {
     947           1 :         clone = new ExtCommunity(this);
     948             :     }
     949             : 
     950       11559 :     clone->RemoveOriginVn();
     951       11546 :     return Locate(clone);
     952             : }
     953             : 
     954           0 : ExtCommunityPtr ExtCommunityDB::ReplaceOriginVnAndLocate(
     955             :         const ExtCommunity *src,
     956             :         const ExtCommunity::ExtCommunityValue &origin_vn) {
     957             :     ExtCommunity *clone;
     958           0 :     if (src) {
     959           0 :         clone = new ExtCommunity(*src);
     960             :     } else {
     961           0 :         clone = new ExtCommunity(this);
     962             :     }
     963             : 
     964           0 :     clone->RemoveOriginVn();
     965           0 :     clone->Append(origin_vn);
     966           0 :     return Locate(clone);
     967             : }
     968             : 
     969           0 : ExtCommunityPtr ExtCommunityDB::ReplaceTunnelEncapsulationAndLocate(
     970             :         const ExtCommunity *src,
     971             :         const ExtCommunity::ExtCommunityList &tunnel_encaps) {
     972             :     ExtCommunity *clone;
     973           0 :     if (src) {
     974           0 :         clone = new ExtCommunity(*src);
     975             :     } else {
     976           0 :         clone = new ExtCommunity(this);
     977             :     }
     978             : 
     979           0 :     clone->RemoveTunnelEncapsulation();
     980           0 :     clone->Append(tunnel_encaps);
     981           0 :     return Locate(clone);
     982             : }
     983             : 
     984           0 : ExtCommunityPtr ExtCommunityDB::ReplaceLoadBalanceAndLocate(
     985             :         const ExtCommunity *src,
     986             :         const ExtCommunity::ExtCommunityValue &lb) {
     987             :     ExtCommunity *clone;
     988           0 :     if (src) {
     989           0 :         clone = new ExtCommunity(*src);
     990             :     } else {
     991           0 :         clone = new ExtCommunity(this);
     992             :     }
     993             : 
     994           0 :     clone->RemoveLoadBalance();
     995           0 :     clone->Append(lb);
     996           0 :     return Locate(clone);
     997             : }
     998             : 
     999           0 : ExtCommunityPtr ExtCommunityDB::ReplaceSubClusterAndLocate(
    1000             :         const ExtCommunity *src,
    1001             :         const ExtCommunity::ExtCommunityValue &sc) {
    1002             :     ExtCommunity *clone;
    1003           0 :     if (src) {
    1004           0 :         clone = new ExtCommunity(*src);
    1005             :     } else {
    1006           0 :         clone = new ExtCommunity(this);
    1007             :     }
    1008             : 
    1009           0 :     clone->RemoveSubCluster();
    1010           0 :     clone->Append(sc);
    1011           0 :     return Locate(clone);
    1012             : }
    1013             : 
    1014           0 : ExtCommunityPtr ExtCommunityDB::SetAndLocate(const ExtCommunity *src,
    1015             :         const ExtCommunity::ExtCommunityList &value) {
    1016             :     ExtCommunity *clone;
    1017           0 :     if (src) {
    1018           0 :         clone = new ExtCommunity(*src);
    1019             :     } else {
    1020           0 :         clone = new ExtCommunity(this);
    1021             :     }
    1022             : 
    1023           0 :     clone->Set(value);
    1024           0 :     return Locate(clone);
    1025             : }
    1026             : 
    1027           0 : string LargeCommunitySpec::ToString() const {
    1028             :     char repr[80];
    1029           0 :     snprintf(repr, sizeof(repr), "LargeCommunity <code: %d, flags: %02x>:%d",
    1030           0 :              code, flags, (uint32_t)communities.size() / 3);
    1031           0 :     return string(repr);
    1032             : }
    1033             : 
    1034           0 : size_t LargeCommunitySpec::EncodeLength() const {
    1035           0 :     return communities.size() * sizeof(uint32_t);
    1036             : }
    1037             : 
    1038           0 : int LargeCommunitySpec::CompareTo(const BgpAttribute &rhs_attr) const {
    1039           0 :     int ret = BgpAttribute::CompareTo(rhs_attr);
    1040           0 :     if (ret != 0) return ret;
    1041           0 :     KEY_COMPARE(communities,
    1042             :         static_cast<const LargeCommunitySpec &>(rhs_attr).communities);
    1043           0 :     return 0;
    1044             : }
    1045             : 
    1046           0 : void LargeCommunitySpec::ToCanonical(BgpAttr *attr) {
    1047             :     // add LarceComm to attr
    1048           0 :     attr->set_large_community(this);
    1049           0 : }
    1050             : 
    1051       16955 : int LargeCommunity::CompareTo(const LargeCommunity &rhs) const {
    1052       16955 :     KEY_COMPARE(communities_.size(), rhs.communities_.size());
    1053             : 
    1054       16954 :     LargeCommunityList::const_iterator i, j;
    1055       16954 :     for (i = communities_.begin(), j = rhs.communities_.begin();
    1056       16954 :          i < communities_.end(); ++i, ++j) {
    1057           0 :         if (*i < *j) {
    1058           0 :             return -1;
    1059             :         }
    1060           0 :         if (*i > *j) {
    1061           0 :             return 1;
    1062             :         }
    1063             :     }
    1064       16954 :     return 0;
    1065             : }
    1066             : 
    1067           0 : void LargeCommunity::Remove(const LargeCommunityList &list) {
    1068           0 :     for (LargeCommunityList::const_iterator it = list.begin();
    1069           0 :          it != list.end(); ++it) {
    1070           0 :         communities_.erase(std::remove(communities_.begin(),
    1071           0 :                     communities_.end(), *it), communities_.end());
    1072             :     }
    1073           0 : }
    1074         336 : void LargeCommunity::Remove() {
    1075         336 :     largecomm_db_->Delete(this);
    1076         336 : }
    1077             : 
    1078           0 : void LargeCommunity::Set(const LargeCommunityList &list) {
    1079           0 :     communities_.clear();
    1080           0 :     for (LargeCommunityList::const_iterator it = list.begin();
    1081           0 :          it != list.end(); ++it) {
    1082           0 :         communities_.push_back(*it);
    1083             :     }
    1084           0 : }
    1085             : 
    1086           0 : void LargeCommunity::Append(const LargeCommunityList &list) {
    1087           0 :     communities_.insert(communities_.end(), list.begin(), list.end());
    1088           0 :     sort(communities_.begin(), communities_.end());
    1089             :     LargeCommunityList::iterator it =
    1090           0 :         unique(communities_.begin(), communities_.end());
    1091           0 :     communities_.erase(it, communities_.end());
    1092           0 : }
    1093             : 
    1094           0 : void LargeCommunity::Append(const LargeCommunityValue &value) {
    1095           0 :     communities_.push_back(value);
    1096           0 :     sort(communities_.begin(), communities_.end());
    1097             :     LargeCommunityList::iterator it =
    1098           0 :         unique(communities_.begin(), communities_.end());
    1099           0 :     communities_.erase(it, communities_.end());
    1100           0 : }
    1101             : 
    1102           0 : LargeCommunity::LargeCommunityValue LargeCommunity::FromHexString(
    1103             :         const string &comm, boost::system::error_code *errorp) {
    1104             :     LargeCommunityValue data;
    1105           0 :     put_value(&data[0], 12, 0);
    1106             :     char *end;
    1107           0 :     uint64_t value = strtoull(comm.c_str(), &end, 24);
    1108           0 :     if (value == 0 || *end) {
    1109             :         // e.g. 0 or 12x34ff (invalid hex)
    1110           0 :         if (errorp != NULL) {
    1111           0 :             *errorp = make_error_code(
    1112             :                     boost::system::errc::invalid_argument);
    1113           0 :             return data;
    1114             :         }
    1115             :     }
    1116           0 :     if (comm[0] == '0' && (comm[1] == 'x' || comm[1] == 'X')) {
    1117           0 :         if (comm.length() > 26 && errorp != NULL) {
    1118             :             // e.g. 0xabcdef0123456789abcdef012 is an invalid 8byte hex value
    1119           0 :             *errorp = make_error_code(
    1120             :                     boost::system::errc::invalid_argument);
    1121           0 :             return data;
    1122             :         }
    1123             :     } else {
    1124           0 :         if (comm.length() > 24 && errorp != NULL) {
    1125             :             // e.g. abcdef0123456789abcdef012 is an invalid 8byte hex value
    1126           0 :             *errorp = make_error_code(
    1127             :                     boost::system::errc::invalid_argument);
    1128           0 :             return data;
    1129             :         }
    1130             :     }
    1131           0 :     put_value(&data[0], 12, value);
    1132           0 :     return data;
    1133             : }
    1134             : 
    1135           0 : LargeCommunity::LargeCommunityList LargeCommunity::LargeCommunityFromString(
    1136             :         const string &comm) {
    1137           0 :     LargeCommunityList commList;
    1138             :     LargeCommunityValue value;
    1139           0 :     size_t pos = comm.find(':');
    1140           0 :     string first(comm.substr(0, pos));
    1141           0 :     boost::system::error_code error;
    1142           0 :     value = FromHexString(comm, &error);
    1143           0 :     if (error) {
    1144           0 :         return commList;
    1145             :     }
    1146           0 :     commList.push_back(value);
    1147           0 :     return commList;
    1148           0 : }
    1149             : 
    1150           0 : string LargeCommunity::ToHexString(const LargeCommunityValue &comm) {
    1151             :     char temp[50];
    1152           0 :     int len = 0;
    1153           0 :     for (size_t i = 0; i < comm.size(); i++) {
    1154           0 :         len += snprintf(temp+len, sizeof(temp) - len, "%02x", (comm)[i]);
    1155             :     }
    1156           0 :     return(string(temp));
    1157             : }
    1158             : 
    1159           0 : string LargeCommunity::ToString(const LargeCommunityValue &comm) {
    1160           0 :     if (is_tag(comm)) {
    1161           0 :         TagLC tag(comm);
    1162           0 :         return(tag.ToString());
    1163             :     }
    1164           0 :     return ToHexString(comm);
    1165             : }
    1166             : 
    1167           0 : void LargeCommunity::RemoveTag() {
    1168           0 :     for (LargeCommunityList::iterator it = communities_.begin();
    1169           0 :          it != communities_.end(); ) {
    1170           0 :         if (LargeCommunity::is_tag(*it)) {
    1171           0 :             it = communities_.erase(it);
    1172             :         } else {
    1173           0 :             ++it;
    1174             :         }
    1175             :     }
    1176           0 : }
    1177             : 
    1178       28433 : vector<uint64_t> LargeCommunity::GetTagList(as_t asn) const {
    1179       28433 :     vector<uint64_t> tag_list;
    1180       28440 :     for (const auto &lc : communities_) {
    1181           0 :         if (!LargeCommunity::is_tag(lc))
    1182           0 :             continue;
    1183           0 :         TagLC tag_comm(lc);
    1184           0 :         if (asn && tag_comm.as_number() != asn && !tag_comm.IsGlobal())
    1185           0 :             continue;
    1186           0 :         tag_list.push_back(tag_comm.tag());
    1187             :     }
    1188             : 
    1189       28437 :     sort(tag_list.begin(), tag_list.end());
    1190       28445 :     vector<uint64_t>::iterator tag_iter = unique(tag_list.begin(),
    1191             :                                                  tag_list.end());
    1192       28437 :     tag_list.erase(tag_iter, tag_list.end());
    1193       56852 :     return tag_list;
    1194           0 : }
    1195             : 
    1196             : 
    1197        8339 : LargeCommunity::LargeCommunity(LargeCommunityDB *largecomm_db,
    1198        8339 :         const LargeCommunitySpec spec) : largecomm_db_(largecomm_db) {
    1199        8339 :     refcount_ = 0;
    1200        8340 :     std::vector<uint32_t>::const_iterator lcit = spec.communities.begin();
    1201        8340 :     while (lcit < spec.communities.end()) {
    1202             :         LargeCommunityValue comm;
    1203           0 :         put_value(comm.data(), 4, *lcit);
    1204           0 :         lcit++;
    1205           0 :         put_value(comm.data()+4, 4, *lcit);
    1206           0 :         lcit++;
    1207           0 :         put_value(comm.data()+8, 4, *lcit);
    1208           0 :         lcit++;
    1209           0 :         communities_.push_back(comm);
    1210             :     }
    1211        8340 :     sort(communities_.begin(), communities_.end());
    1212             :     LargeCommunityList::iterator it =
    1213        8340 :         unique(communities_.begin(), communities_.end());
    1214        8340 :     communities_.erase(it, communities_.end());
    1215        8339 : }
    1216             : 
    1217          20 : LargeCommunityDB::LargeCommunityDB(BgpServer *server) {
    1218          20 : }
    1219             : 
    1220           0 : LargeCommunityPtr LargeCommunityDB::AppendAndLocate(const LargeCommunity *src,
    1221             :         const LargeCommunity::LargeCommunityList &list) {
    1222             :     LargeCommunity *clone;
    1223           0 :     if (src) {
    1224           0 :         clone = new LargeCommunity(*src);
    1225             :     } else {
    1226           0 :         clone = new LargeCommunity(this);
    1227             :     }
    1228             : 
    1229           0 :     clone->Append(list);
    1230           0 :     return Locate(clone);
    1231             : }
    1232             : 
    1233           0 : LargeCommunityPtr LargeCommunityDB::AppendAndLocate(const LargeCommunity *src,
    1234             :         const LargeCommunity::LargeCommunityValue &value) {
    1235           0 :     LargeCommunity::LargeCommunityList list;
    1236           0 :     list.push_back(value);
    1237           0 :     return AppendAndLocate(src, list);
    1238           0 : }
    1239             : 
    1240           0 : LargeCommunityPtr LargeCommunityDB::ReplaceTagListAndLocate(
    1241             :     const LargeCommunity *src,
    1242             :     const LargeCommunity::LargeCommunityList &tag_list) {
    1243             :     LargeCommunity *clone;
    1244           0 :     if (src) {
    1245           0 :         clone = new LargeCommunity(*src);
    1246             :     } else {
    1247           0 :         clone = new LargeCommunity(this);
    1248             :     }
    1249             : 
    1250           0 :     clone->RemoveTag();
    1251           0 :     clone->Append(tag_list);
    1252           0 :     return Locate(clone);
    1253             : }
    1254             : 
    1255           0 : LargeCommunityPtr LargeCommunityDB::RemoveAndLocate(const LargeCommunity *src,
    1256             :         const LargeCommunity::LargeCommunityList &list) {
    1257             :     LargeCommunity *clone;
    1258           0 :     if (src) {
    1259           0 :         clone = new LargeCommunity(*src);
    1260             :     } else {
    1261           0 :         clone = new LargeCommunity(this);
    1262             :     }
    1263             : 
    1264           0 :     clone->Remove(list);
    1265           0 :     return Locate(clone);
    1266             : }
    1267             : 
    1268             : 
    1269           0 : LargeCommunityPtr LargeCommunityDB::SetAndLocate(const LargeCommunity *src,
    1270             :         const LargeCommunity::LargeCommunityList &list) {
    1271             :     LargeCommunity *clone;
    1272           0 :     if (src) {
    1273           0 :         clone = new LargeCommunity(*src);
    1274             :     } else {
    1275           0 :         clone = new LargeCommunity(this);
    1276             :     }
    1277             : 
    1278           0 :     clone->Set(list);
    1279           0 :     return Locate(clone);
    1280             : }
    1281             : 

Generated by: LCOV version 1.14