3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 #define WIN32_LEAN_AND_MEAN
24 // Without this some of the network functions are not found on mingw
26 #define _WIN32_WINNT 0x0501
32 #pragma comment(lib, "ws2_32.lib")
34 typedef SOCKET socket_t;
35 typedef int socklen_t;
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
43 #include <arpa/inet.h>
47 #include "constants.h"
53 #include "util/string.h"
54 #include "util/numeric.h"
56 bool socket_enable_debug_output = false;
57 #define DP socket_enable_debug_output
58 // This is prepended to everything printed here
61 bool g_sockets_initialized = false;
67 if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR)
68 throw SocketException("WSAStartup failed");
71 g_sockets_initialized = true;
74 void sockets_cleanup()
87 Address::Address(unsigned int address, unsigned short port)
93 Address::Address(unsigned int a, unsigned int b,
94 unsigned int c, unsigned int d,
97 m_address = (a<<24) | (b<<16) | ( c<<8) | d;
101 bool Address::operator==(Address &address)
103 return (m_address == address.m_address
104 && m_port == address.m_port);
107 bool Address::operator!=(Address &address)
109 return !(*this == address);
112 void Address::Resolve(const char *name)
114 struct addrinfo *resolved;
115 int e = getaddrinfo(name, NULL, NULL, &resolved);
117 throw ResolveError("");
119 FIXME: This is an ugly hack; change the whole class
120 to store the address as sockaddr
122 struct sockaddr_in *t = (struct sockaddr_in*)resolved->ai_addr;
123 m_address = ntohl(t->sin_addr.s_addr);
124 freeaddrinfo(resolved);
127 std::string Address::serializeString() const
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);
137 unsigned int Address::getAddress() const
142 unsigned short Address::getPort() const
147 void Address::setAddress(unsigned int address)
152 void Address::setAddress(unsigned int a, unsigned int b,
153 unsigned int c, unsigned int d)
155 m_address = (a<<24) | (b<<16) | ( c<<8) | d;
158 void Address::setPort(unsigned short port)
163 void Address::print(std::ostream *s) const
165 (*s)<<((m_address>>24)&0xff)<<"."
166 <<((m_address>>16)&0xff)<<"."
167 <<((m_address>>8)&0xff)<<"."
168 <<((m_address>>0)&0xff)<<":"
172 void Address::print() const
177 UDPSocket::UDPSocket()
179 if(g_sockets_initialized == false)
180 throw SocketException("Sockets not initialized");
182 m_handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
185 dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::UDPSocket()"<<std::endl;
189 throw SocketException("Failed to create socket");
193 DWORD nonblocking = 0;
194 if(ioctlsocket(m_handle, FIONBIO, &nonblocking) != 0)
196 throw SocketException("Failed set non-blocking mode");
200 if(fcntl(m_handle, F_SETFL, O_NONBLOCK, nonblocking) == -1)
202 throw SocketException("Failed set non-blocking mode");
209 UDPSocket::~UDPSocket()
212 dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::~UDPSocket()"<<std::endl;
215 closesocket(m_handle);
221 void UDPSocket::Bind(unsigned short port)
224 dstream<<DPS<<"UDPSocket("<<(int)m_handle
225 <<")::Bind(): port="<<port<<std::endl;
228 address.sin_family = AF_INET;
229 address.sin_addr.s_addr = INADDR_ANY;
230 address.sin_port = htons(port);
232 if(bind(m_handle, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
234 #ifndef DISABLE_ERRNO
235 dstream<<(int)m_handle<<": Bind failed: "<<strerror(errno)<<std::endl;
237 throw SocketException("Failed to bind socket");
241 void UDPSocket::Send(const Address & destination, const void * data, int size)
243 bool dumping_packet = false;
244 if(INTERNET_SIMULATOR)
245 dumping_packet = (myrand()%10==0); //easy
246 //dumping_packet = (myrand()%4==0); // hard
249 /*dstream<<DPS<<"UDPSocket("<<(int)m_handle
250 <<")::Send(): destination=";*/
252 dstream<<(int)m_handle<<" -> ";
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);
263 dstream<<" (DUMPED BY INTERNET_SIMULATOR)";
266 else if(dumping_packet)
268 // Lol let's forget it
269 dstream<<"UDPSocket::Send(): "
270 "INTERNET_SIMULATOR: dumping packet."
278 address.sin_family = AF_INET;
279 address.sin_addr.s_addr = htonl(destination.getAddress());
280 address.sin_port = htons(destination.getPort());
282 int sent = sendto(m_handle, (const char*)data, size,
283 0, (sockaddr*)&address, sizeof(sockaddr_in));
287 throw SendFailedException("Failed to send packet");
291 int UDPSocket::Receive(Address & sender, void * data, int size)
293 if(WaitData(m_timeout_ms) == false)
299 socklen_t address_len = sizeof(address);
301 int received = recvfrom(m_handle, (char*)data,
302 size, 0, (sockaddr*)&address, &address_len);
307 unsigned int address_ip = ntohl(address.sin_addr.s_addr);
308 unsigned int address_port = ntohs(address.sin_port);
310 sender = Address(address_ip, address_port);
313 //dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::Receive(): sender=";
314 dstream<<DPS<<(int)m_handle<<" <- ";
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);
331 int UDPSocket::GetHandle()
336 void UDPSocket::setTimeoutMs(int timeout_ms)
338 m_timeout_ms = timeout_ms;
341 bool UDPSocket::WaitData(int timeout_ms)
346 // Initialize the set
348 FD_SET(m_handle, &readset);
350 // Initialize time out struct
353 tv.tv_usec = timeout_ms * 1000;
355 result = select(m_handle+1, &readset, NULL, NULL, &tv);
359 /*dstream<<"Select timed out (timeout_ms="
360 <<timeout_ms<<")"<<std::endl;*/
363 else if(result < 0 && errno == EINTR){
368 #ifndef DISABLE_ERRNO
369 dstream<<(int)m_handle<<": Select failed: "<<strerror(errno)<<std::endl;
372 int e = WSAGetLastError();
373 dstream<<(int)m_handle<<": WSAGetLastError()="<<e<<std::endl;
374 if(e == 10004 /*=WSAEINTR*/)
376 dstream<<"WARNING: Ignoring WSAEINTR."<<std::endl;
380 throw SocketException("Select failed");
382 else if(FD_ISSET(m_handle, &readset) == false){
384 //dstream<<"Select reported no data in m_handle"<<std::endl;
389 //dstream<<"Select reported data in m_handle"<<std::endl;