]>
git.proxmox.com Git - ceph.git/blob - ceph/src/msg/msg_types.cc
7d29713538b6e3fdee0420bbb51612b6084a851a
9 #include "common/Formatter.h"
11 void entity_name_t::dump(Formatter
*f
) const
13 f
->dump_string("type", type_str());
14 f
->dump_unsigned("num", num());
17 void entity_addr_t::dump(Formatter
*f
) const
19 f
->dump_string("type", get_type_name(type
));
20 f
->dump_stream("addr") << get_sockaddr();
21 f
->dump_unsigned("nonce", nonce
);
24 void entity_inst_t::dump(Formatter
*f
) const
26 f
->dump_object("name", name
);
27 f
->dump_object("addr", addr
);
30 void entity_name_t::generate_test_instances(list
<entity_name_t
*>& o
)
32 o
.push_back(new entity_name_t(entity_name_t::MON()));
33 o
.push_back(new entity_name_t(entity_name_t::MON(1)));
34 o
.push_back(new entity_name_t(entity_name_t::OSD(1)));
35 o
.push_back(new entity_name_t(entity_name_t::CLIENT(1)));
38 void entity_addr_t::generate_test_instances(list
<entity_addr_t
*>& o
)
40 o
.push_back(new entity_addr_t());
41 entity_addr_t
*a
= new entity_addr_t();
44 entity_addr_t
*b
= new entity_addr_t();
45 b
->set_type(entity_addr_t::TYPE_LEGACY
);
47 b
->set_family(AF_INET
);
48 b
->set_in4_quad(0, 127);
49 b
->set_in4_quad(1, 0);
50 b
->set_in4_quad(2, 1);
51 b
->set_in4_quad(3, 2);
56 void entity_inst_t::generate_test_instances(list
<entity_inst_t
*>& o
)
58 o
.push_back(new entity_inst_t());
61 entity_inst_t
*a
= new entity_inst_t(name
, addr
);
65 bool entity_addr_t::parse(const char *s
, const char **end
, int default_type
)
67 *this = entity_addr_t();
69 const char *start
= s
;
75 if (strncmp("v1:", s
, 3) == 0) {
77 newtype
= TYPE_LEGACY
;
78 } else if (strncmp("v2:", s
, 3) == 0) {
81 } else if (strncmp("any:", s
, 4) == 0) {
84 } else if (*s
== '-') {
91 newtype
= default_type
? default_type
: TYPE_DEFAULT
;
94 bool brackets
= false;
100 // inet_pton() requires a null terminated input, so let's fill two
101 // buffers, one with ipv4 allowed characters, and one with ipv6, and
102 // then see which parses.
105 const char *p
= start
;
106 while (o
< buf4
+ sizeof(buf4
) &&
107 *p
&& ((*p
== '.') ||
108 (*p
>= '0' && *p
<= '9'))) {
113 char buf6
[64]; // actually 39 + null is sufficient.
116 while (o
< buf6
+ sizeof(buf6
) &&
117 *p
&& ((*p
== ':') ||
118 (*p
>= '0' && *p
<= '9') ||
119 (*p
>= 'a' && *p
<= 'f') ||
120 (*p
>= 'A' && *p
<= 'F'))) {
124 //cout << "buf4 is '" << buf4 << "', buf6 is '" << buf6 << "'" << std::endl;
129 if (inet_pton(AF_INET
, buf4
, &a4
)) {
130 u
.sin
.sin_addr
.s_addr
= a4
.s_addr
;
131 u
.sa
.sa_family
= AF_INET
;
132 p
= start
+ strlen(buf4
);
133 } else if (inet_pton(AF_INET6
, buf6
, &a6
)) {
134 u
.sa
.sa_family
= AF_INET6
;
135 memcpy(&u
.sin6
.sin6_addr
, &a6
, sizeof(a6
));
136 p
= start
+ strlen(buf6
);
147 //cout << "p is " << *p << std::endl;
149 // parse a port, too!
152 if (port
> MAX_PORT_NUMBER
) {
156 while (*p
&& *p
>= '0' && *p
<= '9')
165 while (*p
&& *p
>= '0' && *p
<= '9')
174 //cout << *this << std::endl;
178 ostream
& operator<<(ostream
& out
, const entity_addr_t
&addr
)
180 if (addr
.type
== entity_addr_t::TYPE_NONE
) {
183 if (addr
.type
!= entity_addr_t::TYPE_ANY
) {
184 out
<< entity_addr_t::get_type_name(addr
.type
) << ":";
186 out
<< addr
.get_sockaddr() << '/' << addr
.nonce
;
190 ostream
& operator<<(ostream
& out
, const sockaddr
*psa
)
192 char buf
[NI_MAXHOST
] = { 0 };
194 switch (psa
->sa_family
) {
197 const sockaddr_in
*sa
= (const sockaddr_in
*)psa
;
198 inet_ntop(AF_INET
, &sa
->sin_addr
, buf
, NI_MAXHOST
);
199 return out
<< buf
<< ':'
200 << ntohs(sa
->sin_port
);
204 const sockaddr_in6
*sa
= (const sockaddr_in6
*)psa
;
205 inet_ntop(AF_INET6
, &sa
->sin6_addr
, buf
, NI_MAXHOST
);
206 return out
<< '[' << buf
<< "]:"
207 << ntohs(sa
->sin6_port
);
210 return out
<< "(unrecognized address family " << psa
->sa_family
<< ")";
214 ostream
& operator<<(ostream
& out
, const sockaddr_storage
&ss
)
216 return out
<< (const sockaddr
*)&ss
;
222 bool entity_addrvec_t::parse(const char *s
, const char **end
)
224 const char *orig_s
= s
;
225 const char *static_end
;
232 bool brackets
= false;
234 // weirdness: make sure this isn't an IPV6 addr!
237 if (!a
.parse(s
, &p
) || !a
.is_ipv6()) {
245 bool r
= a
.parse(s
, end
);
277 void entity_addrvec_t::encode(bufferlist
& bl
, uint64_t features
) const
280 if ((features
& CEPH_FEATURE_MSG_ADDR2
) == 0) {
281 // encode a single legacy entity_addr_t for unfeatured peers
282 encode(legacy_addr(), bl
, 0);
286 encode(v
, bl
, features
);
289 void entity_addrvec_t::decode(bufferlist::const_iterator
& bl
)
297 addr
.decode_legacy_addr_after_marker(bl
);
305 decode(addr
.type
, bl
);
306 decode(addr
.nonce
, bl
);
310 bl
.copy(elen
, (char*)addr
.get_sockaddr());
318 throw buffer::malformed_input("entity_addrvec_marker > 2");
322 void entity_addrvec_t::dump(Formatter
*f
) const
324 f
->open_array_section("addrvec");
325 for (vector
<entity_addr_t
>::const_iterator p
= v
.begin();
327 f
->dump_object("addr", *p
);
332 void entity_addrvec_t::generate_test_instances(list
<entity_addrvec_t
*>& ls
)
334 ls
.push_back(new entity_addrvec_t());
335 ls
.push_back(new entity_addrvec_t());
336 ls
.back()->v
.push_back(entity_addr_t());
337 ls
.push_back(new entity_addrvec_t());
338 ls
.back()->v
.push_back(entity_addr_t());
339 ls
.back()->v
.push_back(entity_addr_t());
342 std::string
entity_addr_t::ip_only_to_str() const
344 const char *host_ip
= NULL
;
345 char addr_buf
[INET6_ADDRSTRLEN
];
346 switch (get_family()) {
348 host_ip
= inet_ntop(AF_INET
, &in4_addr().sin_addr
,
349 addr_buf
, INET_ADDRSTRLEN
);
352 host_ip
= inet_ntop(AF_INET6
, &in6_addr().sin6_addr
,
353 addr_buf
, INET6_ADDRSTRLEN
);
358 return host_ip
? host_ip
: "";