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