OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vrf_route_import.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 
7 #include <algorithm>
8 
9 #include "base/address.h"
10 
11 using std::copy;
12 using std::string;
13 
15 
17  data_.fill(0);
18 }
19 
21  copy(data.begin(), data.end(), data_.begin());
22 }
23 
25  return Ip4Address(get_value(&data_[2], 4));
26 }
27 
28 uint16_t VrfRouteImport::GetNumber() const {
29  return get_value(&data_[6], 2);
30 }
31 
32 VrfRouteImport::VrfRouteImport(const uint32_t bgp_id, const uint32_t ri_index) {
35  put_value(&data_[2], 4, bgp_id);
36  put_value(&data_[6], VrfRouteImport::kSize - 6, ri_index);
37 }
38 
39 string VrfRouteImport::ToString() const {
40  uint8_t data[VrfRouteImport::kSize];
41  copy(data_.begin(), data_.end(), &data[0]);
43  Ip4Address addr(get_value(data + 2, 4));
44  uint16_t num = get_value(data + 6, 2);
45  char temp[50];
46  snprintf(temp, sizeof(temp), "rt-import:%s:%u",
47  addr.to_string().c_str(), num);
48  return string(temp);
49  }
50  return "";
51 }
52 
54  boost::system::error_code *errorp) {
55  VrfRouteImport rt_import;
56  uint8_t data[VrfRouteImport::kSize];
57 
58  // rt-import:1.2.3.4:3
59  size_t pos = str.find(':');
60  if (pos == string::npos) {
61  if (errorp != NULL) {
62  *errorp = make_error_code(boost::system::errc::invalid_argument);
63  }
65  }
66 
67  string first(str.substr(0, pos));
68  if (first != "rt-import") {
69  if (errorp != NULL) {
70  *errorp = make_error_code(boost::system::errc::invalid_argument);
71  }
73  }
74 
75  string rest(str.substr(pos+1));
76 
77  pos = rest.find(':');
78  if (pos == string::npos) {
79  if (errorp != NULL) {
80  *errorp = make_error_code(boost::system::errc::invalid_argument);
81  }
83  }
84 
85  boost::system::error_code ec;
86  string second(rest.substr(0, pos));
87  Ip4Address addr = Ip4Address::from_string(second, ec);
88  char *endptr;
89  if (ec.failed()) {
90  // Not an IP address.
91  if (errorp != NULL) {
92  *errorp = make_error_code(boost::system::errc::invalid_argument);
93  }
95  }
98  uint32_t l_addr = addr.to_ulong();
99  put_value(&data[2], 4, l_addr);
100  int offset = 6;
101 
102  string third(rest.substr(pos+1));
103  uint64_t value = strtol(third.c_str(), &endptr, 10);
104  if (*endptr != '\0') {
105  if (errorp != NULL) {
106  *errorp = make_error_code(boost::system::errc::invalid_argument);
107  }
109  }
110 
111  // Check assigned number.
112  if (value > 0xFFFF) {
113  if (errorp != NULL) {
114  *errorp = make_error_code(boost::system::errc::invalid_argument);
115  }
117  }
118 
119  put_value(&data[offset], VrfRouteImport::kSize - offset, value);
120  copy(&data[0], &data[VrfRouteImport::kSize], rt_import.data_.begin());
121  return rt_import;
122 }
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:39
static VrfRouteImport null_rt_import
uint16_t GetNumber() const
std::string ToString() const
boost::array< uint8_t, kSize > bytes_type
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
static VrfRouteImport FromString(const std::string &str, boost::system::error_code *error=NULL)
bytes_type data_
static const int kSize
Ip4Address GetIPv4Address() const
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:55