Line data Source code
1 : /* 2 : * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "net/community_type.h" 6 : 7 : #include <map> 8 : 9 : #include <boost/assign/list_of.hpp> 10 : 11 : #include "base/string_util.h" 12 : 13 : using std::map; 14 : using std::string; 15 : 16 0 : CommunityType::CommunityType() { 17 0 : } 18 : 19 : static const map<string, CommunityType::WellKnownCommunity> 20 : fromString = boost::assign::map_list_of 21 : ("no-advertise", CommunityType::NoAdvertise) 22 : ("no-export", CommunityType::NoExport) 23 : ("no-export-subconfed", CommunityType::NoExportSubconfed) 24 : ("LlgrStale", CommunityType::LlgrStale) 25 : ("NoLlgr", CommunityType::NoLlgr) 26 : ("no-reoriginate", CommunityType::NoReOriginate) 27 : ("accept-own", CommunityType::AcceptOwn) 28 : ("accept-own-nexthop", CommunityType::AcceptOwnNexthop); 29 : 30 : static const map<CommunityType::WellKnownCommunity, string> 31 : toString = boost::assign::map_list_of 32 : (CommunityType::NoAdvertise, "no-advertise") 33 : (CommunityType::NoReOriginate, "no-reoriginate") 34 : (CommunityType::NoExportSubconfed, "no-export-subconfed") 35 : (CommunityType::LlgrStale, "llgr-stale") 36 : (CommunityType::NoLlgr, "no-llgr") 37 : (CommunityType::AcceptOwn, "accept-own") 38 : (CommunityType::AcceptOwnNexthop, "accept-own-nexthop") 39 : (CommunityType::NoExport, "no-export"); 40 : 41 1035 : uint32_t CommunityType::CommunityFromString( 42 : const string &comm, boost::system::error_code *perr) { 43 : map<string, CommunityType::WellKnownCommunity>::const_iterator it = 44 1035 : fromString.find(comm); 45 1035 : if (it != fromString.end()) { 46 8 : return it->second; 47 : } 48 1027 : size_t pos = comm.rfind(':'); 49 1027 : if (pos == string::npos) { 50 0 : if (perr != NULL) { 51 0 : *perr = make_error_code(boost::system::errc::invalid_argument); 52 : } 53 0 : return 0; 54 : } 55 1027 : string first(comm.substr(0, pos)); 56 1027 : long asn = -1; 57 : char *endptr; 58 1027 : asn = strtol(first.c_str(), &endptr, 10); 59 1027 : if (asn >= 65535 || *endptr != '\0') { 60 1 : if (perr != NULL) { 61 1 : *perr = make_error_code(boost::system::errc::invalid_argument); 62 : } 63 1 : return 0; 64 : } 65 1026 : string second(comm, pos + 1); 66 1026 : long num = -1; 67 1026 : num = strtol(second.c_str(), &endptr, 10); 68 1026 : if (num >= 65535 || *endptr != '\0') { 69 118 : if (perr != NULL) { 70 0 : *perr = make_error_code(boost::system::errc::invalid_argument); 71 : } 72 118 : return 0; 73 : } 74 908 : return (asn*65536 + num); 75 1027 : } 76 : 77 12502 : const string CommunityType::CommunityToString(uint32_t comm) { 78 : map<CommunityType::WellKnownCommunity, string>::const_iterator it = 79 12502 : toString.find((CommunityType::WellKnownCommunity)comm); 80 12501 : if (it != toString.end()) { 81 11952 : return it->second; 82 : } 83 1098 : return integerToString(comm / 65536) + ":" + 84 1647 : integerToString(comm % 65536); 85 : }