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