]>
Commit | Line | Data |
---|---|---|
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 | ||
15 | using 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>" | |
25 | int 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 |