]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * address_helper.cc | |
3 | * | |
4 | * Created on: Oct 27, 2013 | |
5 | * Author: matt | |
6 | */ | |
7 | ||
7c673cae FG |
8 | #include <netdb.h> |
9 | ||
7c673cae | 10 | #include "common/address_helper.h" |
31f18b77 | 11 | #include "boost/regex.hpp" |
7c673cae | 12 | |
31f18b77 | 13 | using namespace std; |
7c673cae FG |
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 |