Translation to Portuguese of Brazil for Minetest
[oweals/minetest.git] / src / socket.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 "util/string.h"
54 #include "util/numeric.h"
55
56 bool socket_enable_debug_output = false;
57 #define DP socket_enable_debug_output
58 // This is prepended to everything printed here
59 #define DPS ""
60
61 bool g_sockets_initialized = false;
62
63 void sockets_init()
64 {
65 #ifdef _WIN32
66         WSADATA WsaData;
67         if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR)
68                 throw SocketException("WSAStartup failed");
69 #else
70 #endif
71         g_sockets_initialized = true;
72 }
73
74 void sockets_cleanup()
75 {
76 #ifdef _WIN32
77         WSACleanup();
78 #endif
79 }
80
81 Address::Address()
82 {
83         m_address = 0;
84         m_port = 0;
85 }
86
87 Address::Address(unsigned int address, unsigned short port)
88 {
89         m_address = address;
90         m_port = port;
91 }
92
93 Address::Address(unsigned int a, unsigned int b,
94                 unsigned int c, unsigned int d,
95                 unsigned short port)
96 {
97         m_address = (a<<24) | (b<<16) | ( c<<8) | d;
98         m_port = port;
99 }
100
101 bool Address::operator==(Address &address)
102 {
103         return (m_address == address.m_address
104                         && m_port == address.m_port);
105 }
106
107 bool Address::operator!=(Address &address)
108 {
109         return !(*this == address);
110 }
111
112 void Address::Resolve(const char *name)
113 {
114         struct addrinfo *resolved;
115         int e = getaddrinfo(name, NULL, NULL, &resolved);
116         if(e != 0)
117                 throw ResolveError("");
118         /*
119                 FIXME: This is an ugly hack; change the whole class
120                 to store the address as sockaddr
121         */
122         struct sockaddr_in *t = (struct sockaddr_in*)resolved->ai_addr;
123         m_address = ntohl(t->sin_addr.s_addr);
124         freeaddrinfo(resolved);
125 }
126
127 std::string Address::serializeString() const
128 {
129         unsigned int a, b, c, d;
130         a = (m_address & 0xFF000000)>>24;
131         b = (m_address & 0x00FF0000)>>16;
132         c = (m_address & 0x0000FF00)>>8;
133         d = (m_address & 0x000000FF);
134         return itos(a)+"."+itos(b)+"."+itos(c)+"."+itos(d);
135 }
136
137 unsigned int Address::getAddress() const
138 {
139         return m_address;
140 }
141
142 unsigned short Address::getPort() const
143 {
144         return m_port;
145 }
146
147 void Address::setAddress(unsigned int address)
148 {
149         m_address = address;
150 }
151
152 void Address::setAddress(unsigned int a, unsigned int b,
153                 unsigned int c, unsigned int d)
154 {
155         m_address = (a<<24) | (b<<16) | ( c<<8) | d;
156 }
157
158 void Address::setPort(unsigned short port)
159 {
160         m_port = port;
161 }
162
163 void Address::print(std::ostream *s) const
164 {
165         (*s)<<((m_address>>24)&0xff)<<"."
166                         <<((m_address>>16)&0xff)<<"."
167                         <<((m_address>>8)&0xff)<<"."
168                         <<((m_address>>0)&0xff)<<":"
169                         <<m_port;
170 }
171
172 void Address::print() const
173 {
174         print(&dstream);
175 }
176
177 UDPSocket::UDPSocket()
178 {
179         if(g_sockets_initialized == false)
180                 throw SocketException("Sockets not initialized");
181         
182     m_handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
183         
184         if(DP)
185         dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::UDPSocket()"<<std::endl;
186         
187     if(m_handle <= 0)
188     {
189                 throw SocketException("Failed to create socket");
190     }
191
192 /*#ifdef _WIN32
193         DWORD nonblocking = 0;
194         if(ioctlsocket(m_handle, FIONBIO, &nonblocking) != 0)
195         {
196                 throw SocketException("Failed set non-blocking mode");
197         }
198 #else
199         int nonblocking = 0;
200         if(fcntl(m_handle, F_SETFL, O_NONBLOCK, nonblocking) == -1)
201         {
202                 throw SocketException("Failed set non-blocking mode");
203         }
204 #endif*/
205
206         setTimeoutMs(0);
207 }
208
209 UDPSocket::~UDPSocket()
210 {
211         if(DP)
212         dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::~UDPSocket()"<<std::endl;
213
214 #ifdef _WIN32
215         closesocket(m_handle);
216 #else
217         close(m_handle);
218 #endif
219 }
220
221 void UDPSocket::Bind(unsigned short port)
222 {
223         if(DP)
224         dstream<<DPS<<"UDPSocket("<<(int)m_handle
225                         <<")::Bind(): port="<<port<<std::endl;
226
227     sockaddr_in address;
228     address.sin_family = AF_INET;
229     address.sin_addr.s_addr = INADDR_ANY;
230     address.sin_port = htons(port);
231
232     if(bind(m_handle, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
233     {
234 #ifndef DISABLE_ERRNO
235                 dstream<<(int)m_handle<<": Bind failed: "<<strerror(errno)<<std::endl;
236 #endif
237                 throw SocketException("Failed to bind socket");
238     }
239 }
240
241 void UDPSocket::Send(const Address & destination, const void * data, int size)
242 {
243         bool dumping_packet = false;
244         if(INTERNET_SIMULATOR)
245                 dumping_packet = (myrand()%10==0); //easy
246                 //dumping_packet = (myrand()%4==0); // hard
247
248         if(DP){
249                 /*dstream<<DPS<<"UDPSocket("<<(int)m_handle
250                                 <<")::Send(): destination=";*/
251                 dstream<<DPS;
252                 dstream<<(int)m_handle<<" -> ";
253                 destination.print();
254                 dstream<<", size="<<size<<", data=";
255                 for(int i=0; i<size && i<20; i++){
256                         if(i%2==0) DEBUGPRINT(" ");
257                         unsigned int a = ((const unsigned char*)data)[i];
258                         DEBUGPRINT("%.2X", a);
259                 }
260                 if(size>20)
261                         dstream<<"...";
262                 if(dumping_packet)
263                         dstream<<" (DUMPED BY INTERNET_SIMULATOR)";
264                 dstream<<std::endl;
265         }
266         else if(dumping_packet)
267         {
268                 // Lol let's forget it
269                 dstream<<"UDPSocket::Send(): "
270                                 "INTERNET_SIMULATOR: dumping packet."
271                                 <<std::endl;
272         }
273
274         if(dumping_packet)
275                 return;
276
277         sockaddr_in address;
278         address.sin_family = AF_INET;
279         address.sin_addr.s_addr = htonl(destination.getAddress());
280         address.sin_port = htons(destination.getPort());
281
282         int sent = sendto(m_handle, (const char*)data, size,
283                 0, (sockaddr*)&address, sizeof(sockaddr_in));
284
285     if(sent != size)
286     {
287                 throw SendFailedException("Failed to send packet");
288     }
289 }
290
291 int UDPSocket::Receive(Address & sender, void * data, int size)
292 {
293         if(WaitData(m_timeout_ms) == false)
294         {
295                 return -1;
296         }
297
298         sockaddr_in address;
299         socklen_t address_len = sizeof(address);
300
301         int received = recvfrom(m_handle, (char*)data,
302                         size, 0, (sockaddr*)&address, &address_len);
303
304         if(received < 0)
305                 return -1;
306
307         unsigned int address_ip = ntohl(address.sin_addr.s_addr);
308         unsigned int address_port = ntohs(address.sin_port);
309
310         sender = Address(address_ip, address_port);
311
312         if(DP){
313                 //dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::Receive(): sender=";
314                 dstream<<DPS<<(int)m_handle<<" <- ";
315                 sender.print();
316                 //dstream<<", received="<<received<<std::endl;
317                 dstream<<", size="<<received<<", data=";
318                 for(int i=0; i<received && i<20; i++){
319                         if(i%2==0) DEBUGPRINT(" ");
320                         unsigned int a = ((const unsigned char*)data)[i];
321                         DEBUGPRINT("%.2X", a);
322                 }
323                 if(received>20)
324                         dstream<<"...";
325                 dstream<<std::endl;
326         }
327
328         return received;
329 }
330
331 int UDPSocket::GetHandle()
332 {
333         return m_handle;
334 }
335
336 void UDPSocket::setTimeoutMs(int timeout_ms)
337 {
338         m_timeout_ms = timeout_ms;
339 }
340
341 bool UDPSocket::WaitData(int timeout_ms)
342 {
343         fd_set readset;
344         int result;
345
346         // Initialize the set
347         FD_ZERO(&readset);
348         FD_SET(m_handle, &readset);
349
350         // Initialize time out struct
351         struct timeval tv;
352         tv.tv_sec = 0;
353         tv.tv_usec = timeout_ms * 1000;
354         // select()
355         result = select(m_handle+1, &readset, NULL, NULL, &tv);
356
357         if(result == 0){
358                 // Timeout
359                 /*dstream<<"Select timed out (timeout_ms="
360                                 <<timeout_ms<<")"<<std::endl;*/
361                 return false;
362         }
363         else if(result < 0 && errno == EINTR){
364                 return false;
365         }
366         else if(result < 0){
367                 // Error
368 #ifndef DISABLE_ERRNO
369                 dstream<<(int)m_handle<<": Select failed: "<<strerror(errno)<<std::endl;
370 #endif
371 #ifdef _WIN32
372                 int e = WSAGetLastError();
373                 dstream<<(int)m_handle<<": WSAGetLastError()="<<e<<std::endl;
374                 if(e == 10004 /*=WSAEINTR*/)
375                 {
376                         dstream<<"WARNING: Ignoring WSAEINTR."<<std::endl;
377                         return false;
378                 }
379 #endif
380                 throw SocketException("Select failed");
381         }
382         else if(FD_ISSET(m_handle, &readset) == false){
383                 // No data
384                 //dstream<<"Select reported no data in m_handle"<<std::endl;
385                 return false;
386         }
387         
388         // There is data
389         //dstream<<"Select reported data in m_handle"<<std::endl;
390         return true;
391 }
392
393