00001 /* 00002 ** ClanLib SDK 00003 ** Copyright (c) 1997-2005 The ClanLib Team 00004 ** 00005 ** This software is provided 'as-is', without any express or implied 00006 ** warranty. In no event will the authors be held liable for any damages 00007 ** arising from the use of this software. 00008 ** 00009 ** Permission is granted to anyone to use this software for any purpose, 00010 ** including commercial applications, and to alter it and redistribute it 00011 ** freely, subject to the following restrictions: 00012 ** 00013 ** 1. The origin of this software must not be misrepresented; you must not 00014 ** claim that you wrote the original software. If you use this software 00015 ** in a product, an acknowledgment in the product documentation would be 00016 ** appreciated but is not required. 00017 ** 2. Altered source versions must be plainly marked as such, and must not be 00018 ** misrepresented as being the original software. 00019 ** 3. This notice may not be removed or altered from any source distribution. 00020 ** 00021 ** Note: Some of the libraries ClanLib link to may have additional 00022 ** requirements or restrictions. 00023 ** 00024 ** File Author(s): 00025 ** 00026 ** Magnus Norddahl 00027 */ 00028 00029 #include "precomp.h" 00030 #include "ip_address.h" 00031 #include "string_help.h" 00032 #include "exception.h" 00033 #ifdef WIN32 00034 #include <winsock.h> 00035 #else 00036 #include <sys/types.h> 00037 #include <sys/socket.h> 00038 #include <sys/time.h> 00039 #include <sys/types.h> 00040 #include <unistd.h> 00041 #include <netinet/in.h> 00042 #include <arpa/inet.h> 00043 #include <netdb.h> 00044 #endif 00045 00047 // CL_IPAddress Construction: 00048 00049 CL_IPAddress::CL_IPAddress() 00050 { 00051 memset(&address, 0, sizeof(sockaddr_in)); 00052 } 00053 00054 CL_IPAddress::CL_IPAddress(const CL_String &server, const CL_String &port) 00055 { 00056 CL_StringA server_a = CL_StringHelp::text_to_local8(server); 00057 int port_i = CL_StringHelp::text_to_int(port); 00058 00059 int ip = inet_addr(server_a.c_str()); 00060 if (ip == INADDR_NONE) 00061 { 00062 hostent *host_info = gethostbyname(server_a.c_str()); 00063 if (host_info == NULL) 00064 throw CL_Exception(TEXT("Could not lookup ") + server); 00065 00066 ip = *((int *) host_info->h_addr_list[0]); 00067 } 00068 00069 if (ip == 0) 00070 throw CL_Exception(TEXT("Invalid IP address")); 00071 00072 memset(&address, 0, sizeof(sockaddr_in)); 00073 address.sin_family = AF_INET; 00074 address.sin_addr.s_addr = ip; 00075 address.sin_port = htons(port_i); 00076 } 00077 00078 CL_IPAddress::CL_IPAddress(const CL_IPAddress &other) 00079 { 00080 memcpy(&address, &other.address, sizeof(sockaddr_in)); 00081 } 00082 00083 CL_IPAddress::~CL_IPAddress() 00084 { 00085 } 00086 00088 // CL_IPAddress Attributes: 00089 00090 CL_String CL_IPAddress::get_address() const 00091 { 00092 return CL_String(); 00093 } 00094 00095 CL_String CL_IPAddress::get_port() const 00096 { 00097 return CL_String(); 00098 } 00099 00100 const sockaddr_in &CL_IPAddress::get_sockaddr_v4() const 00101 { 00102 return address; 00103 } 00104 00105 sockaddr_in &CL_IPAddress::get_sockaddr_v4() 00106 { 00107 return address; 00108 } 00109 00111 // CL_IPAddress Operations: 00112 00113 CL_IPAddress &CL_IPAddress::operator =(const CL_IPAddress &other) 00114 { 00115 memcpy(&address, &other.address, sizeof(sockaddr_in)); 00116 return *this; 00117 } 00118 00120 // CL_IPAddress Implementation:
1.4.1