]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/address_helper.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / common / address_helper.cc
CommitLineData
7c673cae
FG
1/*
2 * address_helper.cc
3 *
4 * Created on: Oct 27, 2013
5 * Author: matt
6 */
7
8#include <sys/types.h>
9#include <sys/socket.h>
10#include <netdb.h>
11
12#include <iostream>
13#include <string>
14
15using namespace std;
16
17#include "common/config.h"
18#include "boost/regex.hpp"
19
20#include "common/address_helper.h"
21
22#include <arpa/inet.h>
23
24// decode strings like "tcp://<host>:<port>"
25int entity_addr_from_url(entity_addr_t *addr /* out */, const char *url)
26{
27 using namespace boost;
28 using std::endl;
29
30 regex expr("(tcp|rdma)://([^:]*):([\\d]+)");
31 cmatch m;
32
33 if (regex_match(url, m, expr)) {
34 string host(m[2].first, m[2].second);
35 string port(m[3].first, m[3].second);
36 addrinfo hints;
37 memset(&hints, 0, sizeof(hints));
38 hints.ai_family = PF_UNSPEC;
39 addrinfo *res;
40 int error = getaddrinfo(host.c_str(), NULL, &hints, &res);
41 if (! error) {
42 addr->set_sockaddr((sockaddr*)res->ai_addr);
43 addr->set_port(std::atoi(port.c_str()));
44 freeaddrinfo(res);
45 return 0;
46 }
47 }
48
49 return 1;
50}
51