]> git.lizzy.rs Git - dragonfireclient.git/blob - src/socket.cpp
Switch the license to be LGPLv2/later, with small parts still remaining as GPLv2...
[dragonfireclient.git] / src / socket.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "socket.h"
21
22 #ifdef _WIN32
23         #define WIN32_LEAN_AND_MEAN
24         // Without this some of the network functions are not found on mingw
25         #ifndef _WIN32_WINNT
26                 #define _WIN32_WINNT 0x0501
27         #endif
28         #include <windows.h>
29         #include <winsock2.h>
30         #include <ws2tcpip.h>
31         #ifdef _MSC_VER
32                 #pragma comment(lib, "ws2_32.lib")
33         #endif
34 typedef SOCKET socket_t;
35 typedef int socklen_t;
36 #else
37         #include <sys/types.h>
38         #include <sys/socket.h>
39         #include <netinet/in.h>
40         #include <fcntl.h>
41         #include <netdb.h>
42         #include <unistd.h>
43         #include <arpa/inet.h>
44 typedef int socket_t;
45 #endif
46
47 #include "constants.h"
48 #include "debug.h"
49 #include <stdio.h>
50 #include <iostream>
51 #include <stdlib.h>
52 #include <errno.h>
53 #include "utility.h"
54
55 bool socket_enable_debug_output = false;
56 #define DP socket_enable_debug_output
57 // This is prepended to everything printed here
58 #define DPS ""
59
60 bool g_sockets_initialized = false;
61
62 void sockets_init()
63 {
64 #ifdef _WIN32
65         WSADATA WsaData;
66         if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR)
67                 throw SocketException("WSAStartup failed");
68 #else
69 #endif
70         g_sockets_initialized = true;
71 }
72
73 void sockets_cleanup()
74 {
75 #ifdef _WIN32
76         WSACleanup();
77 #endif
78 }
79
80 Address::Address()
81 {
82         m_address = 0;
83         m_port = 0;
84 }
85
86 Address::Address(unsigned int address, unsigned short port)
87 {
88         m_address = address;
89         m_port = port;
90 }
91
92 Address::Address(unsigned int a, unsigned int b,
93                 unsigned int c, unsigned int d,
94                 unsigned short port)
95 {
96         m_address = (a<<24) | (b<<16) | ( c<<8) | d;
97         m_port = port;
98 }
99
100 bool Address::operator==(Address &address)
101 {
102         return (m_address == address.m_address
103                         && m_port == address.m_port);
104 }
105
106 bool Address::operator!=(Address &address)
107 {
108         return !(*this == address);
109 }
110
111 void Address::Resolve(const char *name)
112 {
113         struct addrinfo *resolved;
114         int e = getaddrinfo(name, NULL, NULL, &resolved);
115         if(e != 0)
116                 throw ResolveError("");
117         /*
118                 FIXME: This is an ugly hack; change the whole class
119                 to store the address as sockaddr
120         */
121         struct sockaddr_in *t = (struct sockaddr_in*)resolved->ai_addr;
122         m_address = ntohl(t->sin_addr.s_addr);
123         freeaddrinfo(resolved);
124 }
125
126 std::string Address::serializeString() const
127 {
128         unsigned int a, b, c, d;
129         a = (m_address & 0xFF000000)>>24;
130         b = (m_address & 0x00FF0000)>>16;
131         c = (m_address & 0x0000FF00)>>8;
132         d = (m_address & 0x000000FF);
133         return itos(a)+"."+itos(b)+"."+itos(c)+"."+itos(d);
134 }
135
136 unsigned int Address::getAddress() const
137 {
138         return m_address;
139 }
140
141 unsigned short Address::getPort() const
142 {
143         return m_port;
144 }
145
146 void Address::setAddress(unsigned int address)
147 {
148         m_address = address;
149 }
150
151 void Address::setAddress(unsigned int a, unsigned int b,
152                 unsigned int c, unsigned int d)
153 {
154         m_address = (a<<24) | (b<<16) | ( c<<8) | d;
155 }
156
157 void Address::setPort(unsigned short port)
158 {
159         m_port = port;
160 }
161
162 void Address::print(std::ostream *s) const
163 {
164         (*s)<<((m_address>>24)&0xff)<<"."
165                         <<((m_address>>16)&0xff)<<"."
166                         <<((m_address>>8)&0xff)<<"."
167                         <<((m_address>>0)&0xff)<<":"
168                         <<m_port;
169 }
170
171 void Address::print() const
172 {
173         print(&dstream);
174 }
175
176 UDPSocket::UDPSocket()
177 {
178         if(g_sockets_initialized == false)
179                 throw SocketException("Sockets not initialized");
180         
181     m_handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
182         
183         if(DP)
184         dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::UDPSocket()"<<std::endl;
185         
186     if(m_handle <= 0)
187     {
188                 throw SocketException("Failed to create socket");
189     }
190
191 /*#ifdef _WIN32
192         DWORD nonblocking = 0;
193         if(ioctlsocket(m_handle, FIONBIO, &nonblocking) != 0)
194         {
195                 throw SocketException("Failed set non-blocking mode");
196         }
197 #else
198         int nonblocking = 0;
199         if(fcntl(m_handle, F_SETFL, O_NONBLOCK, nonblocking) == -1)
200         {
201                 throw SocketException("Failed set non-blocking mode");
202         }
203 #endif*/
204
205         setTimeoutMs(0);
206 }
207
208 UDPSocket::~UDPSocket()
209 {
210         if(DP)
211         dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::~UDPSocket()"<<std::endl;
212
213 #ifdef _WIN32
214         closesocket(m_handle);
215 #else
216         close(m_handle);
217 #endif
218 }
219
220 void UDPSocket::Bind(unsigned short port)
221 {
222         if(DP)
223         dstream<<DPS<<"UDPSocket("<<(int)m_handle
224                         <<")::Bind(): port="<<port<<std::endl;
225
226     sockaddr_in address;
227     address.sin_family = AF_INET;
228     address.sin_addr.s_addr = INADDR_ANY;
229     address.sin_port = htons(port);
230
231     if(bind(m_handle, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
232     {
233 #ifndef DISABLE_ERRNO
234                 dstream<<(int)m_handle<<": Bind failed: "<<strerror(errno)<<std::endl;
235 #endif
236                 throw SocketException("Failed to bind socket");
237     }
238 }
239
240 void UDPSocket::Send(const Address & destination, const void * data, int size)
241 {
242         bool dumping_packet = false;
243         if(INTERNET_SIMULATOR)
244                 dumping_packet = (myrand()%10==0); //easy
245                 //dumping_packet = (myrand()%4==0); // hard
246
247         if(DP){
248                 /*dstream<<DPS<<"UDPSocket("<<(int)m_handle
249                                 <<")::Send(): destination=";*/
250                 dstream<<DPS;
251                 dstream<<(int)m_handle<<" -> ";
252                 destination.print();
253                 dstream<<", size="<<size<<", data=";
254                 for(int i=0; i<size && i<20; i++){
255                         if(i%2==0) DEBUGPRINT(" ");
256                         unsigned int a = ((const unsigned char*)data)[i];
257                         DEBUGPRINT("%.2X", a);
258                 }
259                 if(size>20)
260                         dstream<<"...";
261                 if(dumping_packet)
262                         dstream<<" (DUMPED BY INTERNET_SIMULATOR)";
263                 dstream<<std::endl;
264         }
265         else if(dumping_packet)
266         {
267                 // Lol let's forget it
268                 dstream<<"UDPSocket::Send(): "
269                                 "INTERNET_SIMULATOR: dumping packet."
270                                 <<std::endl;
271         }
272
273         if(dumping_packet)
274                 return;
275
276         sockaddr_in address;
277         address.sin_family = AF_INET;
278         address.sin_addr.s_addr = htonl(destination.getAddress());
279         address.sin_port = htons(destination.getPort());
280
281         int sent = sendto(m_handle, (const char*)data, size,
282                 0, (sockaddr*)&address, sizeof(sockaddr_in));
283
284     if(sent != size)
285     {
286                 throw SendFailedException("Failed to send packet");
287     }
288 }
289
290 int UDPSocket::Receive(Address & sender, void * data, int size)
291 {
292         if(WaitData(m_timeout_ms) == false)
293         {
294                 return -1;
295         }
296
297         sockaddr_in address;
298         socklen_t address_len = sizeof(address);
299
300         int received = recvfrom(m_handle, (char*)data,
301                         size, 0, (sockaddr*)&address, &address_len);
302
303         if(received < 0)
304                 return -1;
305
306         unsigned int address_ip = ntohl(address.sin_addr.s_addr);
307         unsigned int address_port = ntohs(address.sin_port);
308
309         sender = Address(address_ip, address_port);
310
311         if(DP){
312                 //dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::Receive(): sender=";
313                 dstream<<DPS<<(int)m_handle<<" <- ";
314                 sender.print();
315                 //dstream<<", received="<<received<<std::endl;
316                 dstream<<", size="<<received<<", data=";
317                 for(int i=0; i<received && i<20; i++){
318                         if(i%2==0) DEBUGPRINT(" ");
319                         unsigned int a = ((const unsigned char*)data)[i];
320                         DEBUGPRINT("%.2X", a);
321                 }
322                 if(received>20)
323                         dstream<<"...";
324                 dstream<<std::endl;
325         }
326
327         return received;
328 }
329
330 int UDPSocket::GetHandle()
331 {
332         return m_handle;
333 }
334
335 void UDPSocket::setTimeoutMs(int timeout_ms)
336 {
337         m_timeout_ms = timeout_ms;
338 }
339
340 bool UDPSocket::WaitData(int timeout_ms)
341 {
342         fd_set readset;
343         int result;
344
345         // Initialize the set
346         FD_ZERO(&readset);
347         FD_SET(m_handle, &readset);
348
349         // Initialize time out struct
350         struct timeval tv;
351         tv.tv_sec = 0;
352         tv.tv_usec = timeout_ms * 1000;
353         // select()
354         result = select(m_handle+1, &readset, NULL, NULL, &tv);
355
356         if(result == 0){
357                 // Timeout
358                 /*dstream<<"Select timed out (timeout_ms="
359                                 <<timeout_ms<<")"<<std::endl;*/
360                 return false;
361         }
362         else if(result < 0 && errno == EINTR){
363                 return false;
364         }
365         else if(result < 0){
366                 // Error
367 #ifndef DISABLE_ERRNO
368                 dstream<<(int)m_handle<<": Select failed: "<<strerror(errno)<<std::endl;
369 #endif
370 #ifdef _WIN32
371                 int e = WSAGetLastError();
372                 dstream<<(int)m_handle<<": WSAGetLastError()="<<e<<std::endl;
373                 if(e == 10004 /*=WSAEINTR*/)
374                 {
375                         dstream<<"WARNING: Ignoring WSAEINTR."<<std::endl;
376                         return false;
377                 }
378 #endif
379                 throw SocketException("Select failed");
380         }
381         else if(FD_ISSET(m_handle, &readset) == false){
382                 // No data
383                 //dstream<<"Select reported no data in m_handle"<<std::endl;
384                 return false;
385         }
386         
387         // There is data
388         //dstream<<"Select reported data in m_handle"<<std::endl;
389         return true;
390 }
391
392