OpenSDN source code
sub_cluster.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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 
25 SubCluster::SubCluster(as_t asn, const uint32_t id) {
26  if (asn <= AS2_MAX) {
28  put_value(&data_[2], 2, asn);
29  put_value(&data_[4], 4, id);
30  } else {
32  put_value(&data_[2], 4, asn);
33  put_value(&data_[6], SubCluster::kSize - 6, id);
34  }
36 }
37 
38 uint32_t SubCluster::GetId() const {
40  return get_value(&data_[4], 4);
41  }
42 
44  return get_value(&data_[6], 2);
45  }
46  return 0;
47 }
48 
49 uint32_t SubCluster::GetAsn() const {
51  return get_value(&data_[2], 2);
52  }
53 
55  return get_value(&data_[2], 4);
56  }
57  return 0;
58 }
59 
60 string SubCluster::ToString() const {
61  uint8_t data[SubCluster::kSize];
62  copy(data_.begin(), data_.end(), &data[0]);
63  char temp[50];
64 
66  uint16_t asn = get_value(data + 2, 2);
67  uint32_t id = get_value(data + 4, 4);
68  snprintf(temp, sizeof(temp), "subcluster:%u:%u", asn, id);
69  return string(temp);
70  }
71 
73  uint32_t asn = get_value(data + 2, 4);
74  uint16_t id = get_value(data + 6, 2);
75  snprintf(temp, sizeof(temp), "subcluster:%u:%u", asn, id);
76  return string(temp);
77  }
78  return "";
79 }
80 
82  boost::system::error_code *errorp) {
83  SubCluster sc;
84  uint8_t data[SubCluster::kSize];
85 
86  // subcluster:1:2
87  size_t pos = str.find(':');
88  if (pos == string::npos) {
89  if (errorp != NULL) {
90  *errorp = make_error_code(boost::system::errc::invalid_argument);
91  }
93  }
94 
95  string first(str.substr(0, pos));
96  if (first != "subcluster") {
97  if (errorp != NULL) {
98  *errorp = make_error_code(boost::system::errc::invalid_argument);
99  }
101  }
102 
103  string rest(str.substr(pos+1));
104 
105  pos = rest.find(':');
106  if (pos == string::npos) {
107  if (errorp != NULL) {
108  *errorp = make_error_code(boost::system::errc::invalid_argument);
109  }
111  }
112 
113  boost::system::error_code ec;
114  string second(rest.substr(0, pos));
115  int offset = 6;
116  char *endptr;
117  // Get ASN
118  uint64_t asn = strtoll(second.c_str(), &endptr, 10);
119  if (asn == 0 || asn > 0xFFFFFFFF || *endptr != '\0') {
120  if (errorp != NULL) {
121  *errorp = make_error_code(boost::system::errc::invalid_argument);
122  }
124  }
125 
126  string third(rest.substr(pos+1));
127  uint64_t id = strtoll(third.c_str(), &endptr, 10);
128  if (*endptr != '\0') {
129  if (errorp != NULL) {
130  *errorp = make_error_code(boost::system::errc::invalid_argument);
131  }
133  }
134 
135  // Check assigned number.
136  if ((asn > AS2_MAX && id > 0xFFFF) || id == 0 || id > 0xFFFFFFFF) {
137  if (errorp != NULL) {
138  *errorp = make_error_code(boost::system::errc::invalid_argument);
139  }
141  }
142 
143  if (asn <= AS2_MAX) {
145  put_value(&data[2], 2, asn);
146  offset = 4;
147  } else {
149  put_value(&data[2], 4, asn);
150  offset = 6;
151  }
153  put_value(&data[offset], SubCluster::kSize - offset, id);
154  copy(&data[0], &data[SubCluster::kSize], sc.data_.begin());
155  return sc;
156 }
#define AS2_MAX
Definition: bgp_common.h:25
uint32_t as_t
Definition: bgp_common.h:22
uint32_t GetAsn() const
Definition: sub_cluster.cc:49
uint32_t GetId() const
Definition: sub_cluster.cc:38
boost::array< uint8_t, kSize > bytes_type
Definition: sub_cluster.h:22
std::string ToString() const
Definition: sub_cluster.cc:60
static SubCluster FromString(const std::string &str, boost::system::error_code *error=NULL)
Definition: sub_cluster.cc:81
static const int kSize
Definition: sub_cluster.h:20
bytes_type data_
Definition: sub_cluster.h:57
static SubCluster null_sub_cluster
Definition: sub_cluster.h:21
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