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