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