OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
origin_vn.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
7 
8 #include <stdio.h>
9 
10 #include <algorithm>
11 
12 
13 using std::copy;
14 using std::string;
15 
17 
19  data_.fill(0);
20 }
21 
22 OriginVn::OriginVn(as_t asn, uint32_t vn_index) {
23  if (asn <= AS2_MAX) {
25  put_value(&data_[2], 2, asn);
26  put_value(&data_[4], 4, vn_index);
27  } else {
29  put_value(&data_[2], 4, asn);
30  put_value(&data_[6], 2, vn_index);
31  }
33 }
34 
36  copy(data.begin(), data.end(), data_.begin());
37 }
38 
39 OriginVn OriginVn::FromString(const string &str,
40  boost::system::error_code *errorp) {
41  OriginVn origin_vn;
42  uint8_t data[OriginVn::kSize];
43  size_t pos = str.find(':');
44  if (pos == string::npos) {
45  if (errorp != NULL) {
46  *errorp = make_error_code(boost::system::errc::invalid_argument);
47  }
49  }
50 
51  string first(str.substr(0, pos));
52  if (first != "originvn") {
53  if (errorp != NULL) {
54  *errorp = make_error_code(boost::system::errc::invalid_argument);
55  }
57  }
58 
59  string rest(str.substr(pos+1));
60 
61  pos = rest.find(':');
62  if (pos == string::npos) {
63  if (errorp != NULL) {
64  *errorp = make_error_code(boost::system::errc::invalid_argument);
65  }
67  }
68 
69  string second(rest.substr(0, pos));
70  char *endptr;
71  int64_t asn = strtol(second.c_str(), &endptr, 10);
72  if (asn == 0 || asn > 0xFFFFFFFF || *endptr != '\0') {
73  if (errorp != NULL) {
74  *errorp = make_error_code(boost::system::errc::invalid_argument);
75  }
77  }
78 
79  string third(rest.substr(pos+1));
80  uint64_t value = strtol(third.c_str(), &endptr, 10);
81  if (*endptr != '\0') {
82  if (errorp != NULL) {
83  *errorp = make_error_code(boost::system::errc::invalid_argument);
84  }
86  }
87 
88  // Check assigned number.
89  if ((asn > AS2_MAX && value > 0xFFFF) || value > 0xFFFFFFFF) {
90  if (errorp != NULL) {
91  *errorp = make_error_code(boost::system::errc::invalid_argument);
92  }
94  }
95 
96  if (asn <= AS2_MAX) {
98  put_value(&data[2], 2, asn);
99  put_value(&data[4], 4, value);
100  } else {
102  put_value(&data[2], 4, asn);
103  put_value(&data[6], 2, value);
104  }
106  copy(&data[0], &data[OriginVn::kSize], origin_vn.data_.begin());
107  return origin_vn;
108 }
109 
112  as2_t as_number = get_value(data_.data() + 2, 2);
113  return as_number;
114  }
116  as_t as_number = get_value(data_.data() + 2, 4);
117  return as_number;
118  }
119  return 0;
120 }
121 
122 int OriginVn::vn_index() const {
124  int vn_index = get_value(data_.data() + 4, 4);
125  return vn_index;
126  }
128  int vn_index = get_value(data_.data() + 6, 2);
129  return vn_index;
130  }
131  return 0;
132 }
133 
134 bool OriginVn::IsGlobal() const {
135  return (vn_index() >= kMinGlobalId);
136 }
137 
139  char temp[50];
140  snprintf(temp, sizeof(temp), "originvn:%u:%u", as_number(), vn_index());
141  return string(temp);
142 }
as_t as_number() const
Definition: origin_vn.cc:110
static OriginVn null_originvn
Definition: origin_vn.h:20
static const int kSize
Definition: origin_vn.h:18
bytes_type data_
Definition: origin_vn.h:53
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
OriginVn()
Definition: origin_vn.cc:18
boost::array< uint8_t, kSize > bytes_type
Definition: origin_vn.h:21
uint16_t as2_t
Definition: bgp_common.h:22
bool IsGlobal() const
Definition: origin_vn.cc:134
int vn_index() const
Definition: origin_vn.cc:122
static const int kMinGlobalId
Definition: origin_vn.h:19
static OriginVn FromString(const std::string &str, boost::system::error_code *error=NULL)
Definition: origin_vn.cc:39
std::string ToString()
Definition: origin_vn.cc:138
#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