Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

ip_address.cpp

Go to the documentation of this file.
00001 
00002 #include "API/Network/ip_address.h"
00003 #include "API/Core/System/error.h"
00004 
00005 #ifndef WIN32
00006         #include <sys/socket.h>
00007         #include <netinet/in.h>
00008         #include <arpa/inet.h>
00009         #include <netdb.h>
00010 #else
00011         #include <windows.h>
00012 #endif
00013 
00015 // CL_IPAddress construction:
00016 
00017 CL_IPAddress::CL_IPAddress()
00018 : ip(INADDR_ANY), port(0)
00019 {
00020 }
00021 
00022 CL_IPAddress::CL_IPAddress(unsigned int address, unsigned short port)
00023 : ip(address), port(port)
00024 {
00025 }
00026 
00027 CL_IPAddress::CL_IPAddress(const std::string &hostname, unsigned short port)
00028 : ip(INADDR_ANY), port(port)
00029 {
00030         set_address(hostname);
00031 }
00032 
00033 CL_IPAddress::CL_IPAddress(const CL_IPAddress &copy)
00034 : ip(copy.ip), port(copy.port)
00035 {
00036 }
00037 
00039 // CL_IPAddress attributes:
00040 
00042 // CL_IPAddress operations:
00043 
00044 void CL_IPAddress::set_address(const std::string &hostname)
00045 {
00046         unsigned int addr = 0;
00047 
00048         addr = inet_addr(hostname.c_str());
00049         if (addr == INADDR_NONE)
00050         {
00051                 hostent *host = gethostbyname(hostname.c_str());
00052                 if (host == NULL) throw CL_Error("Could not lookup DNS name");
00053 
00054                 addr = *((unsigned int*) host->h_addr_list[0]);
00055         }
00056 
00057         set_address(addr);
00058 }
00059 
00060 std::string CL_IPAddress::dns_lookup() const
00061 {
00062         sockaddr_in addr_in;
00063         memset(&addr_in, 0, sizeof(addr_in));
00064         addr_in.sin_family = AF_INET;
00065         addr_in.sin_addr.s_addr = ip;
00066         addr_in.sin_port = htons(port);
00067 
00068         hostent *host = gethostbyaddr((const char *) &addr_in, sizeof(addr_in), AF_INET);
00069         if (host == NULL) throw CL_Error("Unable to lookup IP address");
00070         return std::string(host->h_name);
00071 }

Generated at Wed Apr 4 19:54:01 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001