/*************************************************************************** * Copyright (C) 2003 by Torben Nielsen * * torben.nielsen@rocketmail.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #include "dnshelper.h" #include #include #include #include #include using std::string; std::string DnsHelper::getIp(const std::string& hostname) { if ( hostname == "" ) return ""; hostent *hostptr = gethostbyname( hostname.c_str() ); if (hostptr == 0) return (""); in_addr *ptr; char **listptr = hostptr->h_addr_list; ptr = (struct in_addr *) *listptr; if (ptr == 0) return "not found"; else return inet_ntoa(*ptr); } std::string DnsHelper::getHostname(const std::string& ipaddress) { if ( ipaddress == "" ) return ""; sockaddr_in sin; bzero( (caddr_t *) &sin,sizeof( sin ) ); // clear out the structure sin.sin_family = AF_INET; // set the family (only AF_INET is valid sin.sin_addr.s_addr = inet_addr( ipaddress.c_str() ); // convert from dottet decimal to 32 bit long int hostent *hostptr = gethostbyaddr( (char *)&(sin.sin_addr), sizeof(sin.sin_addr), (int)sin.sin_family); if (hostptr == 0) return "not found"; else return hostptr->h_name; }