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 <cstring> 13 #include <sys/types.h> 14 #include <sys/socket.h> 15 #include <netdb.h> 16 17 #include "util.hpp" 18 19 namespace dena { 20 21 struct auto_addrinfo : private noncopyable { auto_addrinfodena::auto_addrinfo22 auto_addrinfo() : addr(0) { } ~auto_addrinfodena::auto_addrinfo23 ~auto_addrinfo() { 24 reset(); 25 } resetdena::auto_addrinfo26 void reset(addrinfo *a = 0) { 27 if (addr != 0) { 28 freeaddrinfo(addr); 29 } 30 addr = a; 31 } getdena::auto_addrinfo32 const addrinfo *get() const { return addr; } resolvedena::auto_addrinfo33 int resolve(const char *node, const char *service, int flags = 0, 34 int family = AF_UNSPEC, int socktype = SOCK_STREAM, int protocol = 0) { 35 reset(); 36 addrinfo hints; 37 memset(&hints, 0, sizeof(hints)); 38 hints.ai_flags = flags; 39 hints.ai_family = family; 40 hints.ai_socktype = socktype; 41 hints.ai_protocol = protocol; 42 return getaddrinfo(node, service, &hints, &addr); 43 } 44 private: 45 addrinfo *addr; 46 }; 47 48 }; 49 50 #endif 51 52