OpenSDN source code
rd.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "net/rd.h"
7 
8 #include <string>
9 
10 #include "base/parse_object.h"
11 #include "base/address.h"
12 
13 using std::string;
14 
16 
18  memset(data_, 0, kSize);
19 }
20 
22  memcpy(data_, data, kSize);
23 }
24 
25 RouteDistinguisher::RouteDistinguisher(uint32_t address, uint16_t vrf_id) {
27  put_value(data_ + 2, 4, address);
28  put_value(data_ + 6, 2, vrf_id);
29 }
30 
31 RouteDistinguisher::RouteDistinguisher(bool is_bgpaas, uint32_t asn, uint16_t vmi_index) {
33  put_value(data_ + 2, 4, asn);
34  put_value(data_ + 6, 2, vmi_index);
35 }
36 
37 RouteDistinguisher::RouteDistinguisher(uint16_t cluster_seed, uint32_t address,
38  uint16_t vrf_id) {
40  put_value(data_ + 2, 2, cluster_seed);
41  put_value(data_ + 4, 2, (address & 0xFFFF));
42  put_value(data_ + 6, 2, vrf_id);
43 }
44 
45 uint16_t RouteDistinguisher::Type() const {
46  return get_value(data_, 2);
47 }
48 
50  return (Type() == TypeIpAddressBased ? get_value(data_ + 2, 4) : 0);
51 }
52 
53 uint16_t RouteDistinguisher::GetVrfId() const {
54  return (Type() == TypeIpAddressBased ? get_value(data_ + 6, 2) : 0);
55 }
56 
58  char temp[32];
59 
60  uint16_t rd_type = get_value(data_, 2);
61  if (rd_type == Type2ByteASBased) {
62  uint16_t asn = get_value(data_ + 2, 2);
63  uint32_t value = get_value(data_ + 4, 4);
64  snprintf(temp, sizeof(temp), "%u:%u", asn, value);
65  return string(temp);
66  } else if (rd_type == TypeIpAddressBased) {
67  Ip4Address ip(get_value(data_ + 2, 4));
68  uint16_t value = get_value(data_ + 6, 2);
69  snprintf(temp, sizeof(temp), ":%u", value);
70  return ip.to_string() + temp;
71  } else if (rd_type == Type4ByteASBased) {
72  uint32_t asn = get_value(data_ + 2, 4);
73  uint16_t value = get_value(data_ + 6, 2);
74  snprintf(temp, sizeof(temp), "%u:%u", asn, value);
75  return string(temp);
76  } else {
77  snprintf(temp, sizeof(temp), "%u:%02x:%02x:%02x:%02x:%02x:%02x",
78  rd_type, data_[2], data_[3], data_[4], data_[5], data_[6],
79  data_[7]);
80  return string(temp);
81  }
82 }
83 
85  const string &str, boost::system::error_code *errorp) {
87  size_t pos = str.rfind(':');
88  if (pos == string::npos) {
89  if (errorp != NULL) {
90  *errorp = make_error_code(boost::system::errc::invalid_argument);
91  }
93  }
94 
95  boost::system::error_code ec;
96  string first(str.substr(0, pos));
97  Ip4Address addr = Ip4Address::from_string(first, ec);
98  int offset;
99  char *endptr;
100  int32_t asn = -1;
101  if (ec.value() != 0) {
102  // Not an IP address, try ASN.
103  asn = strtol(first.c_str(), &endptr, 10);
104  if (asn >= 65535 || *endptr != '\0') {
105  if (errorp != NULL) {
106  *errorp =
107  make_error_code(boost::system::errc::invalid_argument);
108  }
110  }
111 
112  put_value(rd.data_, 2, 0);
113  put_value(rd.data_ + 2, 2, asn);
114  offset = 4;
115  } else {
116  put_value(rd.data_, 2, 1);
117  put_value(rd.data_ + 2, 4, addr.to_ulong());
118  offset = 6;
119  }
120 
121  string second(str, pos + 1);
122  uint64_t value = strtol(second.c_str(), &endptr, 10);
123  if (*endptr != '\0') {
124  if (errorp != NULL) {
125  *errorp = make_error_code(boost::system::errc::invalid_argument);
126  }
128  }
129 
130  // ASN 0 is not allowed if the assigned number is not 0.
131  if (asn == 0 && value != 0) {
132  if (errorp != NULL) {
133  *errorp = make_error_code(boost::system::errc::invalid_argument);
134  }
136  }
137 
138  // Check assigned number for type 0.
139  if (offset == 4 && value > 0xFFFFFFFF) {
140  if (errorp != NULL) {
141  *errorp = make_error_code(boost::system::errc::invalid_argument);
142  }
144  }
145 
146  // Check assigned number for type 1.
147  if (offset == 6 && value > 0xFFFF) {
148  if (errorp != NULL) {
149  *errorp = make_error_code(boost::system::errc::invalid_argument);
150  }
152  }
153 
154  put_value(rd.data_ + offset, 8 - offset, value);
155  return rd;
156 }
157 
159  return memcmp(data_, rhs.data_, kSize);
160 }
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
int CompareTo(const RouteDistinguisher &rhs) const
Definition: rd.cc:158
uint16_t Type() const
Definition: rd.cc:45
uint16_t GetVrfId() const
Definition: rd.cc:53
uint32_t GetAddress() const
Definition: rd.cc:49
static RouteDistinguisher FromString(const std::string &str, boost::system::error_code *error=NULL)
Definition: rd.cc:84
RouteDistinguisher()
Definition: rd.cc:17
static const size_t kSize
Definition: rd.h:14
@ Type4ByteASBased
Definition: rd.h:20
@ Type2ByteASBased
Definition: rd.h:18
@ TypeIpAddressBased
Definition: rd.h:19
uint8_t data_[kSize]
Definition: rd.h:63
static RouteDistinguisher kZeroRd
Definition: rd.h:15
std::string ToString() const
Definition: rd.cc:57
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:56
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:40