OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
site_of_origin.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 
9 #include "base/address.h"
10 #include "bgp/bgp_common.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 
25 string SiteOfOrigin::ToString() const {
26  uint8_t data[SiteOfOrigin::kSize];
27  copy(data_.begin(), data_.end(), &data[0]);
28  if (data[0] == 0) {
29  uint16_t asn = get_value(data + 2, 2);
30  uint32_t num = get_value(data + 4, 4);
31  char temp[50];
32  snprintf(temp, sizeof(temp), "soo:%u:%u", asn, num);
33  return string(temp);
34  } else if (data[0] == 2) {
35  uint32_t asn = get_value(data + 2, 4);
36  uint16_t num = get_value(data + 6, 2);
37  char temp[50];
38  snprintf(temp, sizeof(temp), "soo:%u:%u", asn, num);
39  return string(temp);
40  } else {
41  Ip4Address addr(get_value(data + 2, 4));
42  uint16_t num = get_value(data + 6, 2);
43  char temp[50];
44  snprintf(temp, sizeof(temp), "soo:%s:%u",
45  addr.to_string().c_str(), num);
46  return string(temp);
47  }
48 }
49 
51  boost::system::error_code *errorp) {
52  SiteOfOrigin soo;
53  uint8_t data[SiteOfOrigin::kSize];
54 
55  // soo:1:2 OR soo:1.2.3.4:3
56  size_t pos = str.find(':');
57  if (pos == string::npos) {
58  if (errorp != NULL) {
59  *errorp = make_error_code(boost::system::errc::invalid_argument);
60  }
62  }
63 
64  string first(str.substr(0, pos));
65  if (first != "soo") {
66  if (errorp != NULL) {
67  *errorp = make_error_code(boost::system::errc::invalid_argument);
68  }
70  }
71 
72  string rest(str.substr(pos+1));
73 
74  pos = rest.find(':');
75  if (pos == string::npos) {
76  if (errorp != NULL) {
77  *errorp = make_error_code(boost::system::errc::invalid_argument);
78  }
80  }
81 
82  boost::system::error_code ec;
83  string second(rest.substr(0, pos));
84  Ip4Address addr = Ip4Address::from_string(second, ec);
85  int offset;
86  char *endptr;
87  if (ec.failed()) {
88  // Not an IP address. Try ASN
89  int64_t asn = strtol(second.c_str(), &endptr, 10);
90  if (asn == 0 || asn > 0xFFFFFFFF || *endptr != '\0') {
91  if (errorp != NULL) {
92  *errorp =
93  make_error_code(boost::system::errc::invalid_argument);
94  }
96  }
97 
98  if (asn > AS2_MAX) {
100  put_value(&data[2], 4, asn);
101  offset = 6;
102  } else {
104  put_value(&data[2], 2, asn);
105  offset = 4;
106  }
108  } else {
111  uint32_t l_addr = addr.to_ulong();
112  put_value(&data[2], 4, l_addr);
113  offset = 6;
114  }
115 
116  string third(rest.substr(pos+1));
117  uint64_t value = strtol(third.c_str(), &endptr, 10);
118  if (*endptr != '\0') {
119  if (errorp != NULL) {
120  *errorp = make_error_code(boost::system::errc::invalid_argument);
121  }
122  return SiteOfOrigin::null_soo;
123  }
124 
125  // Check assigned number for type 0.
126  if (offset == 4 && value > 0xFFFFFFFF) {
127  if (errorp != NULL) {
128  *errorp = make_error_code(boost::system::errc::invalid_argument);
129  }
130  return SiteOfOrigin::null_soo;
131  }
132 
133  // Check assigned number for type 1.
134  if (offset == 6 && value > 0xFFFF) {
135  if (errorp != NULL) {
136  *errorp = make_error_code(boost::system::errc::invalid_argument);
137  }
138  return SiteOfOrigin::null_soo;
139  }
140 
141  put_value(&data[offset], SiteOfOrigin::kSize - offset, value);
142  copy(&data[0], &data[SiteOfOrigin::kSize], soo.data_.begin());
143  return soo;
144 }
std::string ToString() const
static SiteOfOrigin FromString(const std::string &str, boost::system::error_code *error=NULL)
boost::array< uint8_t, kSize > bytes_type
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:39
static SiteOfOrigin null_soo
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
static const int kSize
bytes_type data_
#define AS2_MAX
Definition: bgp_common.h:24
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:55