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