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