1 |
torben |
328 |
/*************************************************************************** |
2 |
|
|
* Copyright (C) 2003 by Torben Nielsen * |
3 |
|
|
* torben.nielsen@rocketmail.com * |
4 |
|
|
* * |
5 |
|
|
* This program is free software; you can redistribute it and/or modify * |
6 |
|
|
* it under the terms of the GNU General Public License as published by * |
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
8 |
|
|
* (at your option) any later version. * |
9 |
|
|
***************************************************************************/ |
10 |
|
|
#include "dnshelper.h" |
11 |
|
|
|
12 |
|
|
#include <sys/types.h> |
13 |
|
|
#include <netdb.h> |
14 |
|
|
#include <sys/socket.h> |
15 |
|
|
#include <netinet/in.h> |
16 |
|
|
#include <arpa/inet.h> |
17 |
|
|
|
18 |
|
|
using std::string; |
19 |
|
|
|
20 |
|
|
std::string DnsHelper::getIp(const std::string& hostname) |
21 |
|
|
{ |
22 |
|
|
if ( hostname == "" ) |
23 |
|
|
return ""; |
24 |
|
|
hostent *hostptr = gethostbyname( hostname.c_str() ); |
25 |
|
|
|
26 |
|
|
if (hostptr == 0) |
27 |
|
|
return (""); |
28 |
|
|
|
29 |
|
|
in_addr *ptr; |
30 |
|
|
char **listptr = hostptr->h_addr_list; |
31 |
|
|
ptr = (struct in_addr *) *listptr; |
32 |
|
|
|
33 |
|
|
if (ptr == 0) |
34 |
|
|
return "not found"; |
35 |
|
|
else |
36 |
|
|
return inet_ntoa(*ptr); |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
std::string DnsHelper::getHostname(const std::string& ipaddress) |
40 |
|
|
{ |
41 |
|
|
if ( ipaddress == "" ) |
42 |
|
|
return ""; |
43 |
|
|
|
44 |
|
|
sockaddr_in sin; |
45 |
|
|
|
46 |
|
|
bzero( (caddr_t *) &sin,sizeof( sin ) ); // clear out the structure |
47 |
|
|
sin.sin_family = AF_INET; // set the family (only AF_INET is valid |
48 |
|
|
sin.sin_addr.s_addr = inet_addr( ipaddress.c_str() ); // convert from dottet decimal to 32 bit long int |
49 |
|
|
hostent *hostptr = gethostbyaddr( (char *)&(sin.sin_addr), sizeof(sin.sin_addr), (int)sin.sin_family); |
50 |
|
|
|
51 |
|
|
if (hostptr == 0) |
52 |
|
|
return "not found"; |
53 |
|
|
else |
54 |
|
|
return hostptr->h_name; |
55 |
|
|
} |