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_unsigned("nonce", nonce);
20 f->dump_stream("addr") << get_sockaddr();
23 void entity_inst_t::dump(Formatter *f) const
25 f->dump_object("name", name);
26 f->dump_object("addr", addr);
29 void entity_name_t::generate_test_instances(list<entity_name_t*>& o)
31 o.push_back(new entity_name_t(entity_name_t::MON()));
32 o.push_back(new entity_name_t(entity_name_t::MON(1)));
33 o.push_back(new entity_name_t(entity_name_t::OSD(1)));
34 o.push_back(new entity_name_t(entity_name_t::CLIENT(1)));
37 void entity_addr_t::generate_test_instances(list<entity_addr_t*>& o)
39 o.push_back(new entity_addr_t());
40 entity_addr_t *a = new entity_addr_t();
43 entity_addr_t *b = new entity_addr_t();
44 b->set_type(entity_addr_t::TYPE_LEGACY);
46 b->set_family(AF_INET);
47 b->set_in4_quad(0, 127);
48 b->set_in4_quad(1, 0);
49 b->set_in4_quad(2, 1);
50 b->set_in4_quad(3, 2);
55 void entity_inst_t::generate_test_instances(list<entity_inst_t*>& o)
57 o.push_back(new entity_inst_t());
60 entity_inst_t *a = new entity_inst_t(name, addr);
64 bool entity_addr_t::parse(const char *s, const char **end)
66 memset(this, 0, sizeof(*this));
68 const char *start = s;
70 int newtype = TYPE_DEFAULT;
71 if (strncmp("legacy:", s, 7) == 0) {
73 newtype = TYPE_LEGACY;
74 } else if (strncmp("msgr2:", s, 6) == 0) {
77 } else if (*s == '-') {
78 *this = entity_addr_t();
85 bool brackets = false;
91 // inet_pton() requires a null terminated input, so let's fill two
92 // buffers, one with ipv4 allowed characters, and one with ipv6, and
93 // then see which parses.
96 const char *p = start;
97 while (o < buf4 + sizeof(buf4) &&
99 (*p >= '0' && *p <= '9'))) {
104 char buf6[64]; // actually 39 + null is sufficient.
107 while (o < buf6 + sizeof(buf6) &&
108 *p && ((*p == ':') ||
109 (*p >= '0' && *p <= '9') ||
110 (*p >= 'a' && *p <= 'f') ||
111 (*p >= 'A' && *p <= 'F'))) {
115 //cout << "buf4 is '" << buf4 << "', buf6 is '" << buf6 << "'" << std::endl;
120 if (inet_pton(AF_INET, buf4, &a4)) {
121 u.sin.sin_addr.s_addr = a4.s_addr;
122 u.sa.sa_family = AF_INET;
123 p = start + strlen(buf4);
124 } else if (inet_pton(AF_INET6, buf6, &a6)) {
125 u.sa.sa_family = AF_INET6;
126 memcpy(&u.sin6.sin6_addr, &a6, sizeof(a6));
127 p = start + strlen(buf6);
138 //cout << "p is " << *p << std::endl;
140 // parse a port, too!
144 while (*p && *p >= '0' && *p <= '9')
153 while (*p && *p >= '0' && *p <= '9')
162 //cout << *this << std::endl;
166 ostream& operator<<(ostream& out, const entity_addr_t &addr)
168 if (addr.type == entity_addr_t::TYPE_NONE) {
171 if (addr.type != entity_addr_t::TYPE_DEFAULT) {
172 out << entity_addr_t::get_type_name(addr.type) << ":";
174 out << addr.get_sockaddr() << '/' << addr.nonce;
178 ostream& operator<<(ostream& out, const sockaddr_storage &ss)
180 char buf[NI_MAXHOST] = { 0 };
181 char serv[NI_MAXSERV] = { 0 };
184 if (ss.ss_family == AF_INET)
185 hostlen = sizeof(struct sockaddr_in);
186 else if (ss.ss_family == AF_INET6)
187 hostlen = sizeof(struct sockaddr_in6);
189 hostlen = sizeof(struct sockaddr_storage);
190 getnameinfo((struct sockaddr *)&ss, hostlen, buf, sizeof(buf),
192 NI_NUMERICHOST | NI_NUMERICSERV);
193 if (ss.ss_family == AF_INET6)
194 return out << '[' << buf << "]:" << serv;
195 return out << buf << ':' << serv;
198 ostream& operator<<(ostream& out, const sockaddr *sa)
200 char buf[NI_MAXHOST] = { 0 };
201 char serv[NI_MAXSERV] = { 0 };
204 if (sa->sa_family == AF_INET)
205 hostlen = sizeof(struct sockaddr_in);
206 else if (sa->sa_family == AF_INET6)
207 hostlen = sizeof(struct sockaddr_in6);
209 hostlen = sizeof(struct sockaddr_storage);
210 getnameinfo(sa, hostlen, buf, sizeof(buf),
212 NI_NUMERICHOST | NI_NUMERICSERV);
213 if (sa->sa_family == AF_INET6)
214 return out << '[' << buf << "]:" << serv;
215 return out << buf << ':' << serv;
220 void entity_addrvec_t::encode(bufferlist& bl, uint64_t features) const
222 if ((features & CEPH_FEATURE_MSG_ADDR2) == 0) {
223 // encode a single legacy entity_addr_t for unfeatured peers
225 for (vector<entity_addr_t>::const_iterator p = v.begin();
227 if ((*p).type == entity_addr_t::TYPE_LEGACY) {
232 ::encode(v[0], bl, 0);
234 ::encode(entity_addr_t(), bl, 0);
238 ::encode((__u8)2, bl);
239 ::encode(v, bl, features);
242 void entity_addrvec_t::decode(bufferlist::iterator& bl)
245 ::decode(marker, bl);
249 addr.decode_legacy_addr_after_marker(bl);
257 ::decode(addr.type, bl);
258 ::decode(addr.nonce, bl);
262 bl.copy(elen, (char*)addr.get_sockaddr());
270 throw buffer::malformed_input("entity_addrvec_marker > 2");
274 void entity_addrvec_t::dump(Formatter *f) const
276 f->open_array_section("addrvec");
277 for (vector<entity_addr_t>::const_iterator p = v.begin();
279 f->dump_object("addr", *p);
284 void entity_addrvec_t::generate_test_instances(list<entity_addrvec_t*>& ls)
286 ls.push_back(new entity_addrvec_t());
287 ls.push_back(new entity_addrvec_t());
288 ls.back()->v.push_back(entity_addr_t());
289 ls.push_back(new entity_addrvec_t());
290 ls.back()->v.push_back(entity_addr_t());
291 ls.back()->v.push_back(entity_addr_t());