2 * See the file LICENSE for redistribution information.
4 * Copyright: 2010- 1stclass.co.jp. All rights reserved.
6 * Created by Hajime Kurita
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
16 #include "1stclass.hpp"
19 string firstclass::host2ip(const string server_str){
23 server=new char[sizeof(char)*(server_str.length()+1)];
25 safe_strcat(server, server_str.c_str(), sizeof(char)*(server_str.length()+1));
28 int host2ip(const char *server, char *ip){
30 struct addrinfo hint, *res, *p;
32 char ipstr[INET6_ADDRSTRLEN];
34 memset(&hint, 0, sizeof(hint));
35 hint.ai_family = PF_INET;
36 hint.ai_socktype = SOCK_STREAM;
38 error = getaddrinfo(server, "http", &hint, &res);
41 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error));
51 for(p = res;p != NULL; p = p->ai_next) {
55 if (p->ai_family == AF_INET) { // IPv4
56 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
57 addr = &(ipv4->sin_addr);
60 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
61 addr = &(ipv6->sin6_addr);
65 // convert the IP to a string and print it:
66 inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
68 freeaddrinfo(res); // free the linked list
72 firstclass::safe_strcat(ip, ipstr, sizeof(ip));
82 safe_strcat(ip, ipstr, sizeof(ip));