OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
source_as.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 #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 uint32_t SourceAs::GetAsn() const {
27  return get_value(&data_[2], 2);
28  }
29 
31  return get_value(&data_[2], 4);
32  }
33 
34  return 0;
35 }
36 
37 SourceAs::SourceAs(const uint32_t asn, const uint32_t ri_index) {
38  if (asn <= AS2_MAX) {
40  put_value(&data_[2], 2, asn);
41  put_value(&data_[4], 4, ri_index);
42  } else {
44  put_value(&data_[2], 4, asn);
45  put_value(&data_[6], 2, ri_index);
46  }
48 }
49 
50 string SourceAs::ToString() const {
51  uint8_t data[SourceAs::kSize];
52  copy(data_.begin(), data_.end(), &data[0]);
53  char temp[50];
54  if (data[0] == BgpExtendedCommunityType::TwoOctetAS) {
55  uint16_t asn = get_value(data + 2, 2);
56  uint32_t num = get_value(data + 4, 4);
57  snprintf(temp, sizeof(temp), "source-as:%u:%u", asn, num);
58  return string(temp);
59  } else if (data[0] == BgpExtendedCommunityType::FourOctetAS) {
60  uint32_t asn = get_value(data + 2, 4);
61  uint16_t num = get_value(data + 6, 2);
62  snprintf(temp, sizeof(temp), "source-as:%u:%u", asn, num);
63  return string(temp);
64  }
65  return "";
66 }
67 
68 SourceAs SourceAs::FromString(const string &str,
69  boost::system::error_code *errorp) {
70  SourceAs sas;
71  uint8_t data[SourceAs::kSize];
72 
73  // source-as:1:2
74  size_t pos = str.find(':');
75  if (pos == string::npos) {
76  if (errorp != NULL) {
77  *errorp = make_error_code(boost::system::errc::invalid_argument);
78  }
79  return SourceAs::null_sas;
80  }
81 
82  string first(str.substr(0, pos));
83  if (first != "source-as") {
84  if (errorp != NULL) {
85  *errorp = make_error_code(boost::system::errc::invalid_argument);
86  }
87  return SourceAs::null_sas;
88  }
89 
90  string rest(str.substr(pos+1));
91 
92  pos = rest.find(':');
93  if (pos == string::npos) {
94  if (errorp != NULL) {
95  *errorp = make_error_code(boost::system::errc::invalid_argument);
96  }
97  return SourceAs::null_sas;
98  }
99 
100  boost::system::error_code ec;
101  string second(rest.substr(0, pos));
102  int offset;
103  char *endptr;
104  // Get ASN
105  int64_t asn = strtol(second.c_str(), &endptr, 10);
106  if (asn == 0 || asn > 0xffffffff || *endptr != '\0') {
107  if (errorp != NULL) {
108  *errorp = make_error_code(boost::system::errc::invalid_argument);
109  }
110  return SourceAs::null_sas;
111  }
112 
113  string third(rest.substr(pos+1));
114  uint64_t value = strtol(third.c_str(), &endptr, 10);
115  if (*endptr != '\0') {
116  if (errorp != NULL) {
117  *errorp = make_error_code(boost::system::errc::invalid_argument);
118  }
119  return SourceAs::null_sas;
120  }
121 
122  // Check assigned number.
123  if ((asn > AS2_MAX && value > 0xFFFF) || value > 0xFFFFFFFF) {
124  if (errorp != NULL) {
125  *errorp = make_error_code(boost::system::errc::invalid_argument);
126  }
127  return SourceAs::null_sas;
128  }
129 
130  if (asn <= AS2_MAX) {
132  put_value(&data[2], 2, asn);
133  offset = 4;
134  } else {
136  put_value(&data[2], 4, asn);
137  offset = 6;
138  }
139 
141  put_value(&data[offset], SourceAs::kSize - offset, value);
142  copy(&data[0], &data[SourceAs::kSize], sas.data_.begin());
143  return sas;
144 }
bytes_type data_
Definition: source_as.h:54
boost::array< uint8_t, kSize > bytes_type
Definition: source_as.h:20
SourceAs()
Definition: source_as.cc:17
static SourceAs null_sas
Definition: source_as.h:19
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:39
std::string ToString() const
Definition: source_as.cc:50
static SourceAs FromString(const std::string &str, boost::system::error_code *error=NULL)
Definition: source_as.cc:68
uint32_t GetAsn() const
Definition: source_as.cc:25
static const int kSize
Definition: source_as.h:18
#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