Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / address_helper.cc
1 /*
2  * address_helper.cc
3  *
4  *  Created on: Oct 27, 2013
5  *      Author: matt
6  */
7
8 #include <netdb.h>
9
10 #include "common/address_helper.h"
11 #include "boost/regex.hpp"
12
13 using namespace std;
14
15 // decode strings like "tcp://<host>:<port>"
16 int entity_addr_from_url(entity_addr_t *addr /* out */, const char *url)
17 {
18         using namespace boost;
19         using std::endl;
20
21         regex expr("(tcp|rdma)://([^:]*):([\\d]+)");
22         cmatch m;
23
24         if (regex_match(url, m, expr)) {
25                 string host(m[2].first, m[2].second);
26                 string port(m[3].first, m[3].second);
27                 addrinfo hints;
28                 memset(&hints, 0, sizeof(hints));
29                 hints.ai_family = PF_UNSPEC;
30                 addrinfo *res;
31                 int error = getaddrinfo(host.c_str(), NULL, &hints, &res);
32                 if (! error) {
33                         addr->set_sockaddr((sockaddr*)res->ai_addr);
34                         addr->set_port(std::atoi(port.c_str()));
35                         freeaddrinfo(res);
36                         return 0;
37                 }
38         }
39
40         return 1;
41 }
42