]>
git.proxmox.com Git - ceph.git/blob - ceph/src/msg/msg_types.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
11 #include "common/Formatter.h"
13 void entity_name_t::dump(ceph::Formatter
*f
) const
15 f
->dump_string("type", type_str());
16 f
->dump_unsigned("num", num());
19 void entity_addr_t::dump(ceph::Formatter
*f
) const
21 f
->dump_string("type", get_type_name(type
));
22 f
->dump_stream("addr") << get_sockaddr();
23 f
->dump_unsigned("nonce", nonce
);
26 void entity_inst_t::dump(ceph::Formatter
*f
) const
28 f
->dump_object("name", name
);
29 f
->dump_object("addr", addr
);
32 void entity_name_t::generate_test_instances(std::list
<entity_name_t
*>& o
)
34 o
.push_back(new entity_name_t(entity_name_t::MON()));
35 o
.push_back(new entity_name_t(entity_name_t::MON(1)));
36 o
.push_back(new entity_name_t(entity_name_t::OSD(1)));
37 o
.push_back(new entity_name_t(entity_name_t::CLIENT(1)));
40 void entity_addr_t::generate_test_instances(std::list
<entity_addr_t
*>& o
)
42 o
.push_back(new entity_addr_t());
43 entity_addr_t
*a
= new entity_addr_t();
46 entity_addr_t
*b
= new entity_addr_t();
47 b
->set_type(entity_addr_t::TYPE_LEGACY
);
49 b
->set_family(AF_INET
);
50 b
->set_in4_quad(0, 127);
51 b
->set_in4_quad(1, 0);
52 b
->set_in4_quad(2, 1);
53 b
->set_in4_quad(3, 2);
58 void entity_inst_t::generate_test_instances(std::list
<entity_inst_t
*>& o
)
60 o
.push_back(new entity_inst_t());
63 entity_inst_t
*a
= new entity_inst_t(name
, addr
);
67 bool entity_addr_t::parse(const std::string_view s
)
69 const char* start
= s
.data();
70 const char* end
= nullptr;
71 bool got
= parse(start
, &end
);
72 return got
&& end
== start
+ s
.size();
75 bool entity_addr_t::parse(const char *s
, const char **end
, int default_type
)
77 *this = entity_addr_t();
79 const char *start
= s
;
85 if (strncmp("v1:", s
, 3) == 0) {
87 newtype
= TYPE_LEGACY
;
88 } else if (strncmp("v2:", s
, 3) == 0) {
91 } else if (strncmp("any:", s
, 4) == 0) {
94 } else if (*s
== '-') {
101 newtype
= default_type
? default_type
: TYPE_DEFAULT
;
104 bool brackets
= false;
110 // inet_pton() requires a null terminated input, so let's fill two
111 // buffers, one with ipv4 allowed characters, and one with ipv6, and
112 // then see which parses.
115 const char *p
= start
;
116 while (o
< buf4
+ sizeof(buf4
) &&
117 *p
&& ((*p
== '.') ||
118 (*p
>= '0' && *p
<= '9'))) {
123 char buf6
[64]; // actually 39 + null is sufficient.
126 while (o
< buf6
+ sizeof(buf6
) &&
127 *p
&& ((*p
== ':') ||
128 (*p
>= '0' && *p
<= '9') ||
129 (*p
>= 'a' && *p
<= 'f') ||
130 (*p
>= 'A' && *p
<= 'F'))) {
134 //cout << "buf4 is '" << buf4 << "', buf6 is '" << buf6 << "'" << std::endl;
139 if (inet_pton(AF_INET
, buf4
, &a4
)) {
140 u
.sin
.sin_addr
.s_addr
= a4
.s_addr
;
141 u
.sa
.sa_family
= AF_INET
;
142 p
= start
+ strlen(buf4
);
143 } else if (inet_pton(AF_INET6
, buf6
, &a6
)) {
144 u
.sa
.sa_family
= AF_INET6
;
145 memcpy(&u
.sin6
.sin6_addr
, &a6
, sizeof(a6
));
146 p
= start
+ strlen(buf6
);
157 //cout << "p is " << *p << std::endl;
159 // parse a port, too!
162 if (port
> MAX_PORT_NUMBER
) {
166 while (*p
&& *p
>= '0' && *p
<= '9')
175 while (*p
&& *p
>= '0' && *p
<= '9')
184 //cout << *this << std::endl;
188 std::ostream
& operator<<(std::ostream
& out
, const entity_addr_t
&addr
)
190 if (addr
.type
== entity_addr_t::TYPE_NONE
) {
193 if (addr
.type
!= entity_addr_t::TYPE_ANY
) {
194 out
<< entity_addr_t::get_type_name(addr
.type
) << ":";
196 out
<< addr
.get_sockaddr() << '/' << addr
.nonce
;
200 std::ostream
& operator<<(std::ostream
& out
, const sockaddr
*psa
)
202 char buf
[NI_MAXHOST
] = { 0 };
204 switch (psa
->sa_family
) {
207 const sockaddr_in
*sa
= (const sockaddr_in
*)psa
;
208 inet_ntop(AF_INET
, &sa
->sin_addr
, buf
, NI_MAXHOST
);
209 return out
<< buf
<< ':'
210 << ntohs(sa
->sin_port
);
214 const sockaddr_in6
*sa
= (const sockaddr_in6
*)psa
;
215 inet_ntop(AF_INET6
, &sa
->sin6_addr
, buf
, NI_MAXHOST
);
216 return out
<< '[' << buf
<< "]:"
217 << ntohs(sa
->sin6_port
);
220 return out
<< "(unrecognized address family " << psa
->sa_family
<< ")";
224 std::ostream
& operator<<(std::ostream
& out
, const sockaddr_storage
&ss
)
226 return out
<< (const sockaddr
*)&ss
;
232 bool entity_addrvec_t::parse(const char *s
, const char **end
)
234 const char *orig_s
= s
;
235 const char *static_end
;
242 bool brackets
= false;
244 // weirdness: make sure this isn't an IPV6 addr!
247 if (!a
.parse(s
, &p
) || !a
.is_ipv6()) {
255 bool r
= a
.parse(s
, end
);
287 void entity_addrvec_t::encode(ceph::buffer::list
& bl
, uint64_t features
) const
290 if ((features
& CEPH_FEATURE_MSG_ADDR2
) == 0) {
291 // encode a single legacy entity_addr_t for unfeatured peers
292 encode(legacy_addr(), bl
, 0);
296 encode(v
, bl
, features
);
299 void entity_addrvec_t::decode(ceph::buffer::list::const_iterator
& bl
)
307 addr
.decode_legacy_addr_after_marker(bl
);
315 decode(addr
.type
, bl
);
316 decode(addr
.nonce
, bl
);
320 struct sockaddr
*sa
= (struct sockaddr
*)addr
.get_sockaddr();
321 #if defined(__FreeBSD__) || defined(__APPLE__)
325 if (elen
< sizeof(ss_family
)) {
326 throw ceph::buffer::malformed_input("elen smaller than family len");
328 decode(ss_family
, bl
);
329 sa
->sa_family
= ss_family
;
330 elen
-= sizeof(ss_family
);
331 if (elen
> addr
.get_sockaddr_len() - sizeof(sa
->sa_family
)) {
332 throw ceph::buffer::malformed_input("elen exceeds sockaddr len");
334 bl
.copy(elen
, sa
->sa_data
);
342 throw ceph::buffer::malformed_input("entity_addrvec_marker > 2");
346 void entity_addrvec_t::dump(ceph::Formatter
*f
) const
348 f
->open_array_section("addrvec");
349 for (auto p
= v
.begin(); p
!= v
.end(); ++p
) {
350 f
->dump_object("addr", *p
);
355 void entity_addrvec_t::generate_test_instances(std::list
<entity_addrvec_t
*>& ls
)
357 ls
.push_back(new entity_addrvec_t());
358 ls
.push_back(new entity_addrvec_t());
359 ls
.back()->v
.push_back(entity_addr_t());
360 ls
.push_back(new entity_addrvec_t());
361 ls
.back()->v
.push_back(entity_addr_t());
362 ls
.back()->v
.push_back(entity_addr_t());
365 std::string
entity_addr_t::ip_only_to_str() const
367 const char *host_ip
= NULL
;
368 char addr_buf
[INET6_ADDRSTRLEN
];
369 switch (get_family()) {
371 host_ip
= inet_ntop(AF_INET
, &in4_addr().sin_addr
,
372 addr_buf
, INET_ADDRSTRLEN
);
375 host_ip
= inet_ntop(AF_INET6
, &in6_addr().sin6_addr
,
376 addr_buf
, INET6_ADDRSTRLEN
);
381 return host_ip
? host_ip
: "";