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