1 | |
2 | // vim:sw=2:ai |
3 | |
4 | /* |
5 | * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. |
6 | * See COPYRIGHT.txt for details. |
7 | */ |
8 | |
9 | #ifndef DENA_AUTO_ADDRINFO_HPP |
10 | #define DENA_AUTO_ADDRINFO_HPP |
11 | |
12 | #include <my_global.h> |
13 | #include <sys/socket.h> |
14 | #include <netdb.h> |
15 | #include <string.h> |
16 | #include "util.hpp" |
17 | |
18 | typedef SOCKET_SIZE_TYPE size_socket; |
19 | |
20 | namespace dena { |
21 | |
22 | struct auto_addrinfo : private noncopyable { |
23 | auto_addrinfo() : addr(0) { } |
24 | ~auto_addrinfo() { |
25 | reset(); |
26 | } |
27 | void reset(addrinfo *a = 0) { |
28 | if (addr != 0) { |
29 | freeaddrinfo(addr); |
30 | } |
31 | addr = a; |
32 | } |
33 | const addrinfo *get() const { return addr; } |
34 | int resolve(const char *node, const char *service, int flags = 0, |
35 | int family = AF_UNSPEC, int socktype = SOCK_STREAM, int protocol = 0) { |
36 | addrinfo hints; |
37 | reset(); |
38 | memset(&hints, 0, sizeof(hints)); |
39 | hints.ai_flags = flags; |
40 | hints.ai_family = family; |
41 | hints.ai_socktype = socktype; |
42 | hints.ai_protocol = protocol; |
43 | return getaddrinfo(node, service, &hints, &addr); |
44 | } |
45 | private: |
46 | addrinfo *addr; |
47 | }; |
48 | |
49 | }; |
50 | |
51 | #endif |
52 | |
53 | |