OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sub_cluster.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 
7 #include <algorithm>
8 
9 #include "base/address.h"
10 
11 using std::copy;
12 using std::string;
13 
15 
17  data_.fill(0);
18 }
19 
21  copy(data.begin(), data.end(), data_.begin());
22 }
23 
24 SubCluster::SubCluster(as_t asn, const uint32_t id) {
25  if (asn <= AS2_MAX) {
27  put_value(&data_[2], 2, asn);
28  put_value(&data_[4], 4, id);
29  } else {
31  put_value(&data_[2], 4, asn);
32  put_value(&data_[6], SubCluster::kSize - 6, id);
33  }
35 }
36 
37 uint32_t SubCluster::GetId() const {
39  return get_value(&data_[4], 4);
40  }
41 
43  return get_value(&data_[6], 2);
44  }
45  return 0;
46 }
47 
48 uint32_t SubCluster::GetAsn() const {
50  return get_value(&data_[2], 2);
51  }
52 
54  return get_value(&data_[2], 4);
55  }
56  return 0;
57 }
58 
59 string SubCluster::ToString() const {
60  uint8_t data[SubCluster::kSize];
61  copy(data_.begin(), data_.end(), &data[0]);
62  char temp[50];
63 
65  uint16_t asn = get_value(data + 2, 2);
66  uint32_t id = get_value(data + 4, 4);
67  snprintf(temp, sizeof(temp), "subcluster:%u:%u", asn, id);
68  return string(temp);
69  }
70 
72  uint32_t asn = get_value(data + 2, 4);
73  uint16_t id = get_value(data + 6, 2);
74  snprintf(temp, sizeof(temp), "subcluster:%u:%u", asn, id);
75  return string(temp);
76  }
77  return "";
78 }
79 
81  boost::system::error_code *errorp) {
82  SubCluster sc;
83  uint8_t data[SubCluster::kSize];
84 
85  // subcluster:1:2
86  size_t pos = str.find(':');
87  if (pos == string::npos) {
88  if (errorp != NULL) {
89  *errorp = make_error_code(boost::system::errc::invalid_argument);
90  }
92  }
93 
94  string first(str.substr(0, pos));
95  if (first != "subcluster") {
96  if (errorp != NULL) {
97  *errorp = make_error_code(boost::system::errc::invalid_argument);
98  }
100  }
101 
102  string rest(str.substr(pos+1));
103 
104  pos = rest.find(':');
105  if (pos == string::npos) {
106  if (errorp != NULL) {
107  *errorp = make_error_code(boost::system::errc::invalid_argument);
108  }
110  }
111 
112  boost::system::error_code ec;
113  string second(rest.substr(0, pos));
114  int offset = 6;
115  char *endptr;
116  // Get ASN
117  uint64_t asn = strtoll(second.c_str(), &endptr, 10);
118  if (asn == 0 || asn > 0xFFFFFFFF || *endptr != '\0') {
119  if (errorp != NULL) {
120  *errorp = make_error_code(boost::system::errc::invalid_argument);
121  }
123  }
124 
125  string third(rest.substr(pos+1));
126  uint64_t id = strtoll(third.c_str(), &endptr, 10);
127  if (*endptr != '\0') {
128  if (errorp != NULL) {
129  *errorp = make_error_code(boost::system::errc::invalid_argument);
130  }
132  }
133 
134  // Check assigned number.
135  if ((asn > AS2_MAX && id > 0xFFFF) || id == 0 || id > 0xFFFFFFFF) {
136  if (errorp != NULL) {
137  *errorp = make_error_code(boost::system::errc::invalid_argument);
138  }
140  }
141 
142  if (asn <= AS2_MAX) {
144  put_value(&data[2], 2, asn);
145  offset = 4;
146  } else {
148  put_value(&data[2], 4, asn);
149  offset = 6;
150  }
152  put_value(&data[offset], SubCluster::kSize - offset, id);
153  copy(&data[0], &data[SubCluster::kSize], sc.data_.begin());
154  return sc;
155 }
static SubCluster null_sub_cluster
Definition: sub_cluster.h:20
static const int kSize
Definition: sub_cluster.h:19
uint32_t as_t
Definition: bgp_common.h:21
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:39
uint32_t GetId() const
Definition: sub_cluster.cc:37
boost::array< uint8_t, kSize > bytes_type
Definition: sub_cluster.h:21
uint32_t GetAsn() const
Definition: sub_cluster.cc:48
static SubCluster FromString(const std::string &str, boost::system::error_code *error=NULL)
Definition: sub_cluster.cc:80
std::string ToString() const
Definition: sub_cluster.cc:59
bytes_type data_
Definition: sub_cluster.h:56
#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