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