File: ip_address_getsock.cpp

package info (click to toggle)
clanlib 1.0~svn3827-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 24,312 kB
  • ctags: 16,513
  • sloc: cpp: 101,606; xml: 6,410; makefile: 1,747; ansic: 463; perl: 424; php: 247; sh: 53
file content (186 lines) | stat: -rw-r--r-- 4,837 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
**  ClanLib SDK
**  Copyright (c) 1997-2005 The ClanLib Team
**
**  This software is provided 'as-is', without any express or implied
**  warranty.  In no event will the authors be held liable for any damages
**  arising from the use of this software.
**
**  Permission is granted to anyone to use this software for any purpose,
**  including commercial applications, and to alter it and redistribute it
**  freely, subject to the following restrictions:
**
**  1. The origin of this software must not be misrepresented; you must not
**     claim that you wrote the original software. If you use this software
**     in a product, an acknowledgment in the product documentation would be
**     appreciated but is not required.
**  2. Altered source versions must be plainly marked as such, and must not be
**     misrepresented as being the original software.
**  3. This notice may not be removed or altered from any source distribution.
**
**  Note: Some of the libraries ClanLib may link to may have additional
**  requirements or restrictions.
**
**  File Author(s):
**
**    Magnus Norddahl
**    (if your name is missing here, please add it)
*/

#ifdef USE_GETADDR

// msvc got a strange bug that makes #ifndef USE_GETADDR sometimes not work!!
// thats why we check for USE_GETADDR and not the opposite.
// -- mbn 20 dec 2002

#else

#include "API/Core/System/clanstring.h"
#include "API/Core/System/error.h"
#include "ip_address_getsock.h"

#include <cstdlib>

#ifndef WIN32
	#include <sys/socket.h>
	#include <netinet/in.h>
	#include <arpa/inet.h>
	#include <netdb.h>
#else
	#include <windows.h>
#endif

/////////////////////////////////////////////////////////////////////////////
// CL_IPAddress construction:

CL_IPAddress_GetSock::CL_IPAddress_GetSock()
: ip(INADDR_ANY), port(0)
{
}

CL_IPAddress_GetSock::CL_IPAddress_GetSock(const std::string &new_port)
: ip(INADDR_ANY)
{
	port = CL_String::to_int(new_port);
}

CL_IPAddress_GetSock::CL_IPAddress_GetSock(const std::string &hostname, const std::string &new_port)
{
	port = CL_String::to_int(new_port);

	unsigned int addr = 0;

	addr = inet_addr(hostname.c_str());
	if (addr == INADDR_NONE)
	{
		hostent *host = gethostbyname(hostname.c_str());
		if (host == NULL) throw CL_Error("Could not lookup DNS name");

		addr = *((unsigned int*) host->h_addr_list[0]);
	}

	ip = addr;
}

CL_IPAddress_GetSock::CL_IPAddress_GetSock(const CL_IPAddress &copy)
{
	set_address(copy.get_address(),copy.get_port());
}

/////////////////////////////////////////////////////////////////////////////
// CL_IPAddress attributes:

void CL_IPAddress_GetSock::get_addrinfo(int type, sockaddr &addr, int &len, int domain) const
{
	// Domain ignored since it will always be ipv4
	sockaddr_in ret;
	ret.sin_family = AF_INET;
	ret.sin_port = htons(port);
	ret.sin_addr.s_addr = ip;
	len = sizeof(sockaddr_in);
	memcpy(&addr, &ret, len);
}

std::string CL_IPAddress_GetSock::get_address() const
{
	struct in_addr foo;
	foo.s_addr = ip;
	char *addr = inet_ntoa(foo);
	return std::string(addr);	
}

std::string CL_IPAddress_GetSock::get_port() const
{
	return CL_String::from_int(port);
}

bool CL_IPAddress_GetSock::operator == (const CL_IPAddress &other) const
{
	return (get_address() == other.get_address()) && (get_port() == other.get_port());
}

bool CL_IPAddress_GetSock::operator < (const CL_IPAddress &other) const
{
	if (get_address() == other.get_address())
		return atoi(get_port().c_str()) < atoi(other.get_port().c_str());
	return get_address() < other.get_address();
}

bool CL_IPAddress_GetSock::operator > (const CL_IPAddress &other) const
{
	if (get_address() == other.get_address())
		return atoi(get_port().c_str()) > atoi(other.get_port().c_str());
	return get_address() > other.get_address();
}

/////////////////////////////////////////////////////////////////////////////
// CL_IPAddress operations:


void CL_IPAddress_GetSock::set_address(const std::string &hostname)
{
	unsigned int addr = 0;

	addr = inet_addr(hostname.c_str());
	if (addr == INADDR_NONE)
	{
		hostent *host = gethostbyname(hostname.c_str());
		if (host == NULL) throw CL_Error("Could not lookup DNS name");

		addr = *((unsigned int*) host->h_addr_list[0]);
	}

	ip = addr;
}


void CL_IPAddress_GetSock::set_address(const std::string &hostname, const std::string &port)
{
	set_port(port);
	set_address(hostname);
}

void CL_IPAddress_GetSock::set_port(const std::string &new_port)
{
	port = atoi(new_port.c_str());
	ip = INADDR_ANY;
}

std::string CL_IPAddress_GetSock::dns_lookup() const
{
	hostent *host = gethostbyaddr((const char *) &ip, sizeof(ip), AF_INET);
	if (host == NULL) throw CL_Error("Unable to lookup IP address");
	return std::string(host->h_name);
}

bool CL_IPAddress_GetSock::is_ipv4() const
{
	return true;
}

bool CL_IPAddress_GetSock::is_ipv6() const
{
	return false;
}

#endif