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