summaryrefslogtreecommitdiffstats
path: root/src/msg/msg_types.cc
blob: ba088e84f3204bdc6cb6eda2686b963253409a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab

#include "msg_types.h"

#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>

#include "common/Formatter.h"

void entity_name_t::dump(ceph::Formatter *f) const
{
  f->dump_string("type", type_str());
  f->dump_unsigned("num", num());
}

void entity_addr_t::dump(ceph::Formatter *f) const
{
  f->dump_string("type", get_type_name(type));
  f->dump_stream("addr") << get_sockaddr();
  f->dump_unsigned("nonce", nonce);
}

void entity_inst_t::dump(ceph::Formatter *f) const
{
  f->dump_object("name", name);
  f->dump_object("addr", addr);
}

void entity_name_t::generate_test_instances(std::list<entity_name_t*>& o)
{
  o.push_back(new entity_name_t(entity_name_t::MON()));
  o.push_back(new entity_name_t(entity_name_t::MON(1)));
  o.push_back(new entity_name_t(entity_name_t::OSD(1)));
  o.push_back(new entity_name_t(entity_name_t::CLIENT(1)));
}

void entity_addr_t::generate_test_instances(std::list<entity_addr_t*>& o)
{
  o.push_back(new entity_addr_t());
  entity_addr_t *a = new entity_addr_t();
  a->set_nonce(1);
  o.push_back(a);
  entity_addr_t *b = new entity_addr_t();
  b->set_type(entity_addr_t::TYPE_LEGACY);
  b->set_nonce(5);
  b->set_family(AF_INET);
  b->set_in4_quad(0, 127);
  b->set_in4_quad(1, 0);
  b->set_in4_quad(2, 1);
  b->set_in4_quad(3, 2);
  b->set_port(2);
  o.push_back(b);
}

void entity_inst_t::generate_test_instances(std::list<entity_inst_t*>& o)
{
  o.push_back(new entity_inst_t());
  entity_name_t name;
  entity_addr_t addr;
  entity_inst_t *a = new entity_inst_t(name, addr);
  o.push_back(a);
}

bool entity_addr_t::parse(const std::string_view s)
{
  const char* start = s.data();
  const char* end = nullptr;
  bool got = parse(start, &end);
  return got && end == start + s.size();
}

bool entity_addr_t::parse(const char *s, const char **end, int default_type)
{
  *this = entity_addr_t();

  const char *start = s;
  if (end) {
    *end = s;
  }

  int newtype;
  if (strncmp("v1:", s, 3) == 0) {
    start += 3;
    newtype = TYPE_LEGACY;
  } else if (strncmp("v2:", s, 3) == 0) {
    start += 3;
    newtype = TYPE_MSGR2;
  } else if (strncmp("any:", s, 4) == 0) {
    start += 4;
    newtype = TYPE_ANY;
  } else if (*s == '-') {
    newtype = TYPE_NONE;
    if (end) {
      *end = s + 1;
    }
    return true;
  } else {
    newtype = default_type ? default_type : TYPE_DEFAULT;
  }

  bool brackets = false;
  if (*start == '[') {
    start++;
    brackets = true;
  }
  
  // inet_pton() requires a null terminated input, so let's fill two
  // buffers, one with ipv4 allowed characters, and one with ipv6, and
  // then see which parses.
  char buf4[39];
  char *o = buf4;
  const char *p = start;
  while (o < buf4 + sizeof(buf4) &&
	 *p && ((*p == '.') ||
		(*p >= '0' && *p <= '9'))) {
    *o++ = *p++;
  }
  *o = 0;

  char buf6[64];  // actually 39 + null is sufficient.
  o = buf6;
  p = start;
  while (o < buf6 + sizeof(buf6) &&
	 *p && ((*p == ':') ||
		(*p >= '0' && *p <= '9') ||
		(*p >= 'a' && *p <= 'f') ||
		(*p >= 'A' && *p <= 'F'))) {
    *o++ = *p++;
  }
  *o = 0;
  //cout << "buf4 is '" << buf4 << "', buf6 is '" << buf6 << "'" << std::endl;

  // ipv4?
  struct in_addr a4;
  struct in6_addr a6;
  if (inet_pton(AF_INET, buf4, &a4)) {
    u.sin.sin_addr.s_addr = a4.s_addr;
    u.sa.sa_family = AF_INET;
    p = start + strlen(buf4);
  } else if (inet_pton(AF_INET6, buf6, &a6)) {
    u.sa.sa_family = AF_INET6;
    memcpy(&u.sin6.sin6_addr, &a6, sizeof(a6));
    p = start + strlen(buf6);
  } else {
    return false;
  }

  if (brackets) {
    if (*p != ']')
      return false;
    p++;
  }
  
  //cout << "p is " << *p << std::endl;
  if (*p == ':') {
    // parse a port, too!
    p++;
    int port = atoi(p);
    if (port > MAX_PORT_NUMBER) {
      return false;
    }
    set_port(port);
    while (*p && *p >= '0' && *p <= '9')
      p++;
  }

  if (*p == '/') {
    // parse nonce, too
    p++;
    int non = atoi(p);
    set_nonce(non);
    while (*p && *p >= '0' && *p <= '9')
      p++;
  }

  if (end)
    *end = p;

  type = newtype;

  //cout << *this << std::endl;
  return true;
}

std::ostream& operator<<(std::ostream& out, const entity_addr_t &addr)
{
  if (addr.type == entity_addr_t::TYPE_NONE) {
    return out << "-";
  }
  if (addr.type != entity_addr_t::TYPE_ANY) {
    out << entity_addr_t::get_type_name(addr.type) << ":";
  }
  out << addr.get_sockaddr() << '/' << addr.nonce;
  return out;
}

std::ostream& operator<<(std::ostream& out, const sockaddr *psa)
{
  char buf[NI_MAXHOST] = { 0 };

  switch (psa->sa_family) {
  case AF_INET:
    {
      const sockaddr_in *sa = (const sockaddr_in*)psa;
      inet_ntop(AF_INET, &sa->sin_addr, buf, NI_MAXHOST);
      return out << buf << ':'
		 << ntohs(sa->sin_port);
    }
  case AF_INET6:
    {
      const sockaddr_in6 *sa = (const sockaddr_in6*)psa;
      inet_ntop(AF_INET6, &sa->sin6_addr, buf, NI_MAXHOST);
      return out << '[' << buf << "]:"
		 << ntohs(sa->sin6_port);
    }
  default:
    return out << "(unrecognized address family " << psa->sa_family << ")";
  }
}

std::ostream& operator<<(std::ostream& out, const sockaddr_storage &ss)
{
  return out << (const sockaddr*)&ss;
}


// entity_addrvec_t

bool entity_addrvec_t::parse(const char *s, const char **end)
{
  const char *orig_s = s;
  const char *static_end;
  if (!end) {
    end = &static_end;
  } else {
    *end = s;
  }
  v.clear();
  bool brackets = false;
  if (*s == '[') {
    // weirdness: make sure this isn't an IPV6 addr!
    entity_addr_t a;
    const char *p;
    if (!a.parse(s, &p) || !a.is_ipv6()) {
      // it's not
      brackets = true;
      ++s;
    }
  }
  while (*s) {
    entity_addr_t a;
    bool r = a.parse(s, end);
    if (!r) {
      if (brackets) {
	v.clear();
	*end = orig_s;
	return false;
      }
      break;
    }
    v.push_back(a);
    s = *end;
    if (!brackets) {
      break;
    }
    if (*s != ',') {
      break;
    }
    ++s;
  }
  if (brackets) {
    if (*s == ']') {
      ++s;
      *end = s;
    } else {
      *end = orig_s;
      v.clear();
      return false;
    }
  }
  return !v.empty();
}

void entity_addrvec_t::encode(ceph::buffer::list& bl, uint64_t features) const
{
  using ceph::encode;
  if ((features & CEPH_FEATURE_MSG_ADDR2) == 0) {
    // encode a single legacy entity_addr_t for unfeatured peers
    encode(legacy_addr(), bl, 0);
    return;
  }
  encode((__u8)2, bl);
  encode(v, bl, features);
}

void entity_addrvec_t::decode(ceph::buffer::list::const_iterator& bl)
{
  using ceph::decode;
  __u8 marker;
  decode(marker, bl);
  if (marker == 0) {
    // legacy!
    entity_addr_t addr;
    addr.decode_legacy_addr_after_marker(bl);
    v.clear();
    v.push_back(addr);
    return;
  }
  if (marker == 1) {
    entity_addr_t addr;
    DECODE_START(1, bl);
    decode(addr.type, bl);
    decode(addr.nonce, bl);
    __u32 elen;
    decode(elen, bl);
    if (elen) {
      struct sockaddr *sa = (struct sockaddr *)addr.get_sockaddr();
#if defined(__FreeBSD__) || defined(__APPLE__)
      sa->sa_len = 0;
#endif
      uint16_t ss_family;
      if (elen < sizeof(ss_family)) {
        throw ceph::buffer::malformed_input("elen smaller than family len");
      }
      decode(ss_family, bl);
      sa->sa_family = ss_family;
      elen -= sizeof(ss_family);
      if (elen > addr.get_sockaddr_len() - sizeof(sa->sa_family)) {
        throw ceph::buffer::malformed_input("elen exceeds sockaddr len");
      }
      bl.copy(elen, sa->sa_data);
    }
    DECODE_FINISH(bl);
    v.clear();
    v.push_back(addr);
    return;
  }
  if (marker > 2)
    throw ceph::buffer::malformed_input("entity_addrvec_marker > 2");
  decode(v, bl);
}

void entity_addrvec_t::dump(ceph::Formatter *f) const
{
  f->open_array_section("addrvec");
  for (auto p = v.begin(); p != v.end(); ++p) {
    f->dump_object("addr", *p);
  }
  f->close_section();
}

void entity_addrvec_t::generate_test_instances(std::list<entity_addrvec_t*>& ls)
{
  ls.push_back(new entity_addrvec_t());
  ls.push_back(new entity_addrvec_t());
  ls.back()->v.push_back(entity_addr_t());
  ls.push_back(new entity_addrvec_t());
  ls.back()->v.push_back(entity_addr_t());
  ls.back()->v.push_back(entity_addr_t());
}

std::string entity_addr_t::ip_only_to_str() const 
{
  const char *host_ip = NULL;
  char addr_buf[INET6_ADDRSTRLEN];
  switch (get_family()) {
  case AF_INET:
    host_ip = inet_ntop(AF_INET, &in4_addr().sin_addr, 
                        addr_buf, INET_ADDRSTRLEN);
    break;
  case AF_INET6:
    host_ip = inet_ntop(AF_INET6, &in6_addr().sin6_addr, 
                        addr_buf, INET6_ADDRSTRLEN);
    break;
  default:
    break;
  }
  return host_ip ? host_ip : "";
}

std::string entity_addr_t::ip_n_port_to_str() const
{
  std::string addr;
  addr += ip_only_to_str();
  if (is_ipv6()) {
    addr = '[' + addr + ']';
  }
  addr += ':';
  addr += std::to_string(get_port());
  return addr;
}