summaryrefslogtreecommitdiffstats
path: root/source/cSocket.cpp
blob: f53724216bad691da6dfb40181888f639ec32d4a (plain) (blame)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "cSocket.h"
#include "packets/cPacket.h"

#ifndef _WIN32
	#include <netdb.h>
	#include <unistd.h>
	#include <arpa/inet.h>		//inet_ntoa()
#else
	#define socklen_t int
#endif





 unsigned long cSocket::INTERNET_ADDRESS_LOCALHOST = 127; // TODO: Linux won't accept this: htonl((127 << 24) | 1);
 
 
 
 
 
cSocket::cSocket(xSocket a_Socket)
	: m_Socket(a_Socket)
{
}





cSocket::~cSocket()
{
}





cSocket::operator const cSocket::xSocket() const
{
	return m_Socket;
}





cSocket::xSocket cSocket::GetSocket() const
{
	return m_Socket;
}





bool cSocket::IsValid(void) const
{
	#ifdef _WIN32
	return (m_Socket != INVALID_SOCKET);
	#else  // _WIN32
	return (m_Socket >= 0);
	#endif  // else _WIN32
}





void cSocket::CloseSocket()
{
	#ifdef _WIN32
	
	closesocket(m_Socket);
	
	#else  // _WIN32
	
	if (shutdown(m_Socket, SHUT_RDWR) != 0)//SD_BOTH);
	{
		LOGWARN("Error on shutting down socket (%s)", m_IPString.c_str());
	}
	if (close(m_Socket) != 0)
	{
		LOGWARN("Error closing socket (%s)", m_IPString.c_str());
	}
	
	#endif  // else _WIN32

	// Invalidate the socket so that this object can be re-used for another connection
	m_Socket = INVALID_SOCKET;
}





AString cSocket::GetErrorString( int a_ErrNo )
{
	char buffer[ 1024 ];
	AString Out;

	#ifdef _WIN32

	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, a_ErrNo, 0, buffer, ARRAYCOUNT(buffer), NULL);
	Printf(Out, "%d: %s", a_ErrNo, buffer);
	return Out;
	
	#else  // _WIN32
	
	// According to http://linux.die.net/man/3/strerror_r there are two versions of strerror_r():
	
	#if (((_POSIX_C_SOURCE >= 200112L) || (_XOPEN_SOURCE >= 600)) && ! _GNU_SOURCE)  // XSI version of strerror_r():
	
	int res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) );
	if( res == 0 )
	{
		Printf(Out, "%d: %s", a_ErrNo, buffer);
		return Out;
	}
	
	#else  // GNU version of strerror_r()
	
	char * res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) );
	if( res != NULL )
	{
		Printf(Out, "%d: %s", a_ErrNo, res);
		return Out;
	}
	
	#endif  // strerror_r() version
	
	else
	{
		Printf(Out, "Error %d while getting error string for error #%d!", errno, a_ErrNo);
		return Out;
	}
	
	#endif  // else _WIN32
}




int cSocket::GetLastError()
{
#ifdef _WIN32
	return WSAGetLastError();
#else
	return errno;
#endif
}





int cSocket::SetReuseAddress()
{
#ifdef _WIN32
	char yes = 1;
#else
	int yes = 1;
#endif
	return setsockopt(m_Socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
}





int cSocket::WSAStartup()
{
#ifdef _WIN32
	WSADATA wsaData;
	memset(&wsaData, 0, sizeof(wsaData));
	return ::WSAStartup(MAKEWORD(2, 2),&wsaData);
#else
	return 0;
#endif
}





cSocket cSocket::CreateSocket()
{
	return socket(AF_INET,SOCK_STREAM,0);
}





int cSocket::Bind(SockAddr_In& a_Address)
{
	sockaddr_in local;

	local.sin_family = a_Address.Family;
	local.sin_addr.s_addr = a_Address.Address;
	local.sin_port = htons((u_short)a_Address.Port);

	return bind(m_Socket, (sockaddr*)&local, sizeof(local));
}





int cSocket::Listen(int a_Backlog)
{
	return listen(m_Socket, a_Backlog);
}





cSocket cSocket::Accept()
{
	sockaddr_in from;
	socklen_t fromlen=sizeof(from);

	cSocket SClient = accept(m_Socket, (sockaddr*)&from, &fromlen);

	if (from.sin_addr.s_addr && SClient.IsValid())	// Get IP in string form
	{
		SClient.m_IPString = inet_ntoa(from.sin_addr);
		//LOG("cSocket::Accept() %s", SClient.m_IPString);
	}

	return SClient;
}





int cSocket::Connect(SockAddr_In & a_Address)
{
	sockaddr_in local;

	local.sin_family = a_Address.Family;
	local.sin_addr.s_addr = a_Address.Address;
	local.sin_port = htons((u_short)a_Address.Port);

	return connect(m_Socket, (sockaddr *)&local, sizeof(local));
}





int cSocket::Receive(char* a_Buffer, unsigned int a_Length, unsigned int a_Flags)
{
	return recv(m_Socket, a_Buffer, a_Length, a_Flags);
}





int cSocket::Send(const char * a_Buffer, unsigned int a_Length)
{
	return send(m_Socket, a_Buffer, a_Length, 0);
}





int cSocket::Send(const cPacket * a_Packet)
{
	AString Serialized;
	a_Packet->Serialize(Serialized);
	return Send(Serialized.data(), Serialized.size());
}





int cSocket::Send(const cPacket & a_Packet)
{
	AString Serialized;
	a_Packet.Serialize(Serialized);
	return Send(Serialized.data(), Serialized.size());
}





unsigned short cSocket::GetPort(void) const
{
	assert(IsValid());
	
	sockaddr_in Addr;
	socklen_t AddrSize = sizeof(Addr);
	if (getsockname(m_Socket, (sockaddr *)&Addr, &AddrSize) != 0)
	{
		return 0;
	}
	return Addr.sin_port;
}