1 | |
---|---|
2 | // vim:sw=2:ai |
3 | |
4 | /* |
5 | * Copyright (C) 2010-2011 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 | #ifndef __WIN__ |
13 | #include <netdb.h> |
14 | #endif |
15 | |
16 | #include "util.hpp" |
17 | |
18 | namespace dena { |
19 | |
20 | struct auto_addrinfo : private noncopyable { |
21 | auto_addrinfo() : addr(0) { } |
22 | ~auto_addrinfo() { |
23 | reset(); |
24 | } |
25 | void reset(addrinfo *a = 0) { |
26 | if (addr != 0) { |
27 | freeaddrinfo(addr); |
28 | } |
29 | addr = a; |
30 | } |
31 | const addrinfo *get() const { return addr; } |
32 | int resolve(const char *node, const char *service, int flags = 0, |
33 | int family = AF_UNSPEC, int socktype = SOCK_STREAM, int protocol = 0) { |
34 | reset(); |
35 | addrinfo hints; |
36 | hints.ai_flags = flags; |
37 | hints.ai_family = family; |
38 | hints.ai_socktype = socktype; |
39 | hints.ai_protocol = protocol; |
40 | return getaddrinfo(node, service, &hints, &addr); |
41 | } |
42 | private: |
43 | addrinfo *addr; |
44 | }; |
45 | |
46 | }; |
47 | |
48 | #endif |
49 | |
50 |