OpenSDN source code
esi.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "net/esi.h"
7 
8 #include "base/parse_object.h"
9 #include "base/util.h"
10 #include "base/string_util.h"
11 #include "base/address.h"
12 
13 using namespace std;
14 
15 static const uint8_t max_esi_bytes[] = {
16  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
17 };
18 
21 
23  memset(data_, 0, kSize);
24 }
25 
27  memcpy(data_, data, kSize);
28 }
29 
31  boost::system::error_code *errorp) {
32  if (str == "zero_esi")
34  if (str == "max_esi")
36 
37  size_t num_colons = count(str.begin(), str.end(), ':');
38  if (num_colons != 1 && num_colons != 9) {
39  if (errorp != NULL)
40  *errorp = make_error_code(boost::system::errc::invalid_argument);
42  }
43 
44  uint8_t data[kSize];
45  memset(data, 0, kSize);
46 
47  // AS based or IP based.
48  if (num_colons == 1) {
49  size_t pos = str.find(':');
50  assert(pos != string::npos);
51  string asn_or_ip(str.substr(0, pos));
52 
53  size_t num_dots = count(asn_or_ip.begin(), asn_or_ip.end(), '.');
54  if (num_dots != 0 && num_dots != 3) {
55  if (errorp != NULL)
56  *errorp = make_error_code(boost::system::errc::invalid_argument);
58  }
59 
60  // AS based.
61  if (num_dots == 0) {
62  uint32_t asn;
63  bool ret = stringToInteger(asn_or_ip, asn);
64  if (!ret) {
65  if (errorp != NULL) {
66  *errorp = make_error_code(boost::system::errc::invalid_argument);
68  }
69  }
70 
71  data[0] = AS_BASED;
72  put_value(&data[1], 4, asn);
73  }
74 
75  // IP based.
76  if (num_dots == 3) {
77  boost::system::error_code ec;
78  Ip4Address addr = Ip4Address::from_string(asn_or_ip, ec);
79  if (ec.value() != 0) {
80  if (errorp != NULL) {
81  *errorp = make_error_code(boost::system::errc::invalid_argument);
83  }
84  }
85 
86  data[0] = IP_BASED;
87  const Ip4Address::bytes_type &bytes = addr.to_bytes();
88  copy(bytes.begin(), bytes.begin() + 4, &data[1]);
89  }
90 
91  // Parse discriminator - common for AS based and IP based.
92  string disc_str(str, pos + 1);
93  uint32_t disc;
94  bool ret = stringToInteger(disc_str, disc);
95  if (!ret) {
96  if (errorp != NULL) {
97  *errorp = make_error_code(boost::system::errc::invalid_argument);
99  }
100  }
101  put_value(&data[5], 4, disc);
102  }
103 
104  // All other formats - raw colon separated bytes.
105  if (num_colons == 9) {
106  char extra;
107  int ret = sscanf(str.c_str(),
108  "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx%c",
109  &data[0], &data[1], &data[2], &data[3], &data[4],
110  &data[5], &data[6], &data[7], &data[8], &data[9], &extra);
111  if (ret != kSize || strchr(str.c_str(), 'x') || strchr(str.c_str(), 'X')) {
112  if (errorp != NULL)
113  *errorp = make_error_code(boost::system::errc::invalid_argument);
115  }
116  }
117 
118  return EthernetSegmentId(data);
119 }
120 
122  if (CompareTo(kZeroEsi) == 0)
123  return "zero_esi";
124  if (CompareTo(kMaxEsi) == 0)
125  return "max_esi";
126 
127  switch (Type()) {
128  case AS_BASED: {
129  uint32_t asn = get_value(data_ + 1, 4);
130  uint32_t value = get_value(data_ + 5, 4);
131  return integerToString(asn) + ":" + integerToString(value);
132  break;
133  }
134  case IP_BASED: {
135  Ip4Address addr(get_value(data_ + 1, 4));
136  uint32_t value = get_value(data_ + 5, 4);
137  return addr.to_string() + ":" + integerToString(value);
138  break;
139  }
140  case MAC_BASED:
141  case STP_BASED:
142  case LACP_BASED:
143  case CONFIGURED:
144  default: {
145  char temp[64];
146  snprintf(temp, sizeof(temp),
147  "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
148  data_[0], data_[1], data_[2], data_[3], data_[4],
149  data_[5], data_[6], data_[7], data_[8], data_[9]);
150  return temp;
151  }
152  }
153 
154  return "bad_esi";
155 }
156 
158  return memcmp(data_, rhs.data_, kSize);
159 }
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
static int CompareTo(const AuthenticationKey &lhs, const AuthenticationKey &rhs)
Definition: bgp_config.cc:32
std::string ToString() const
Definition: esi.cc:121
uint8_t data_[kSize]
Definition: esi.h:64
EthernetSegmentId()
Definition: esi.cc:22
int CompareTo(const EthernetSegmentId &rhs) const
Definition: esi.cc:157
static const EthernetSegmentId kZeroEsi
Definition: esi.h:17
static EthernetSegmentId FromString(const std::string &str, boost::system::error_code *errorp=NULL)
Definition: esi.cc:30
static const EthernetSegmentId kMaxEsi
Definition: esi.h:18
static const uint8_t max_esi_bytes[]
Definition: esi.cc:15
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
bool stringToInteger(const std::string &str, NumberType &num)
Definition: string_util.h:72
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:20