OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rtarget_prefix.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 
7 #include <algorithm>
8 #include "base/string_util.h"
9 
10 using boost::system::error_code;
11 using std::copy;
12 using std::string;
13 
14 const string RTargetPrefix::kDefaultPrefixString = "0:target:0:0";
15 
16 RTargetPrefix::RTargetPrefix() : as_(0), rtarget_(RouteTarget::null_rtarget) {
17 }
18 
20  RTargetPrefix *prefix) {
21  size_t nlri_size = proto_prefix.prefix.size();
22  if (nlri_size == 0) {
23  prefix->as_ = 0;
25  return 0;
26  }
27 
28  size_t expected_nlri_size = sizeof(as_t) + RouteTarget::kSize;
29  if (nlri_size != expected_nlri_size)
30  return -1;
31 
32  size_t as_offset = 0;
33  prefix->as_ = get_value(&proto_prefix.prefix[as_offset], sizeof(as_t));
34  size_t rtarget_offset = as_offset + sizeof(as_t);
35  RouteTarget::bytes_type bt = { { 0 } };
36  copy(proto_prefix.prefix.begin() + rtarget_offset,
37  proto_prefix.prefix.end(), bt.begin());
38  prefix->rtarget_ = RouteTarget(bt);
39 
40  return 0;
41 }
42 
44  const BgpProtoPrefix &proto_prefix,
45  const BgpAttr *attr,
46  const Address::Family family,
47  RTargetPrefix *prefix,
48  BgpAttrPtr *new_attr, uint32_t *label,
49  uint32_t *l3_label) {
50  return FromProtoPrefix(proto_prefix, prefix);
51 }
52 
54  proto_prefix->prefix.clear();
55  if (as_ == 0 && rtarget_ == RouteTarget::null_rtarget) {
56  proto_prefix->prefixlen = 0;
57  return;
58  }
59 
60  size_t nlri_size = sizeof(as_t) + RouteTarget::kSize;
61  proto_prefix->prefix.resize(nlri_size);
62  proto_prefix->prefixlen = nlri_size * 8;
63  size_t as_offset = 0;
64  put_value(&proto_prefix->prefix[as_offset], sizeof(as_t), as_);
65  size_t rtarget_offset = as_offset + sizeof(as_t);
66  put_value(&proto_prefix->prefix[rtarget_offset], RouteTarget::kSize,
68 }
69 
70 // as:rtarget
71 RTargetPrefix RTargetPrefix::FromString(const string &str, error_code *errorp) {
72  RTargetPrefix prefix;
73 
74  if (str == kDefaultPrefixString)
75  return RTargetPrefix();
76 
77  size_t pos = str.find(':');
78  if (pos == string::npos) {
79  if (errorp != NULL)
80  *errorp = make_error_code(boost::system::errc::invalid_argument);
81  return prefix;
82  }
83 
84  as_t as;
85  string asstr = str.substr(0, pos);
86  stringToInteger(asstr, as);
87 
88  string rtargetstr(str, pos + 1);
89  error_code rtarget_err;
91  rtarget = RouteTarget::FromString(rtargetstr, &rtarget_err);
92  if (rtarget_err.failed()) {
93  if (errorp != NULL)
94  *errorp = rtarget_err;
95  return prefix;
96  }
97 
98  prefix.rtarget_ = rtarget;
99  prefix.as_ = as;
100  return prefix;
101 }
102 
103 string RTargetPrefix::ToString() const {
104  return (integerToString(as_) + ":" + rtarget_.ToString());
105 }
106 
108  if (as_ < rhs.as_) {
109  return -1;
110  }
111  if (as_ > rhs.as_) {
112  return 1;
113  }
114  if (rtarget_ < rhs.rtarget_) {
115  return -1;
116  }
117  if (rtarget_ > rhs.rtarget_) {
118  return 1;
119  }
120  return 0;
121 }
122 
boost::array< uint8_t, kSize > bytes_type
int CompareTo(const RTargetPrefix &rhs) const
as_t as() const
RouteTarget rtarget() const
Family
Definition: address.h:24
bool stringToInteger(const std::string &str, NumberType &num)
Definition: string_util.h:71
static const int kSize
static RTargetPrefix FromString(const std::string &str, boost::system::error_code *errorp=NULL)
static int FromProtoPrefix(const BgpProtoPrefix &proto_prefix, RTargetPrefix *prefix)
uint32_t as_t
Definition: bgp_common.h:21
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:39
RouteTarget rtarget_
boost::intrusive_ptr< const BgpAttr > BgpAttrPtr
Definition: bgp_attr.h:991
static RouteTarget null_rtarget
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:19
std::vector< uint8_t > prefix
static const std::string kDefaultPrefixString
static RouteTarget FromString(const std::string &str, boost::system::error_code *error=NULL)
void BuildProtoPrefix(BgpProtoPrefix *prefix) const
const uint64_t GetExtCommunityValue() const
std::string ToString() const
std::string ToString() const
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:55