OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
community_type.cc
Go to the documentation of this file.
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 
17 }
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 
42  const string &comm, boost::system::error_code *perr) {
43  map<string, CommunityType::WellKnownCommunity>::const_iterator it =
44  fromString.find(comm);
45  if (it != fromString.end()) {
46  return it->second;
47  }
48  size_t pos = comm.rfind(':');
49  if (pos == string::npos) {
50  if (perr != NULL) {
51  *perr = make_error_code(boost::system::errc::invalid_argument);
52  }
53  return 0;
54  }
55  string first(comm.substr(0, pos));
56  long asn = -1;
57  char *endptr;
58  asn = strtol(first.c_str(), &endptr, 10);
59  if (asn >= 65535 || *endptr != '\0') {
60  if (perr != NULL) {
61  *perr = make_error_code(boost::system::errc::invalid_argument);
62  }
63  return 0;
64  }
65  string second(comm, pos + 1);
66  long num = -1;
67  num = strtol(second.c_str(), &endptr, 10);
68  if (num >= 65535 || *endptr != '\0') {
69  if (perr != NULL) {
70  *perr = make_error_code(boost::system::errc::invalid_argument);
71  }
72  return 0;
73  }
74  return (asn*65536 + num);
75 }
76 
77 const string CommunityType::CommunityToString(uint32_t comm) {
78  map<CommunityType::WellKnownCommunity, string>::const_iterator it =
80  if (it != toString.end()) {
81  return it->second;
82  }
83  return integerToString(comm / 65536) + ":" +
84  integerToString(comm % 65536);
85 }
static const std::string CommunityToString(uint32_t comm)
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:19
static const map< CommunityType::WellKnownCommunity, string > toString
static const map< string, CommunityType::WellKnownCommunity > fromString
static uint32_t CommunityFromString(const std::string &comm, boost::system::error_code *perr=NULL)