3 Copyright (C) 2010 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 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.
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.
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.
28 // Debug printing options
29 // Set to 1 for debug output
31 // This is prepended to everything printed here
34 bool g_sockets_initialized = false;
40 if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR)
41 throw SocketException("WSAStartup failed");
44 g_sockets_initialized = true;
47 void sockets_cleanup()
58 Address::Address(unsigned int address, unsigned short port)
64 Address::Address(unsigned int a, unsigned int b,
65 unsigned int c, unsigned int d,
68 m_address = (a<<24) | (b<<16) | ( c<<8) | d;
72 bool Address::operator==(Address &address)
74 return (m_address == address.m_address
75 && m_port == address.m_port);
78 bool Address::operator!=(Address &address)
80 return !(*this == address);
83 void Address::Resolve(const char *name)
85 struct addrinfo *resolved;
86 int e = getaddrinfo(name, NULL, NULL, &resolved);
88 throw ResolveError("");
90 FIXME: This is an ugly hack; change the whole class
91 to store the address as sockaddr
93 struct sockaddr_in *t = (struct sockaddr_in*)resolved->ai_addr;
94 m_address = ntohl(t->sin_addr.s_addr);
95 freeaddrinfo(resolved);
98 unsigned int Address::getAddress() const
103 unsigned short Address::getPort() const
108 void Address::setAddress(unsigned int address)
113 void Address::setAddress(unsigned int a, unsigned int b,
114 unsigned int c, unsigned int d)
116 m_address = (a<<24) | (b<<16) | ( c<<8) | d;
119 void Address::setPort(unsigned short port)
124 void Address::print(std::ostream *s) const
126 (*s)<<((m_address>>24)&0xff)<<"."
127 <<((m_address>>16)&0xff)<<"."
128 <<((m_address>>8)&0xff)<<"."
129 <<((m_address>>0)&0xff)<<":"
133 void Address::print() const
138 UDPSocket::UDPSocket()
140 if(g_sockets_initialized == false)
141 throw SocketException("Sockets not initialized");
143 m_handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
146 dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::UDPSocket()"<<std::endl;
150 throw SocketException("Failed to create socket");
154 DWORD nonblocking = 0;
155 if(ioctlsocket(m_handle, FIONBIO, &nonblocking) != 0)
157 throw SocketException("Failed set non-blocking mode");
161 if(fcntl(m_handle, F_SETFL, O_NONBLOCK, nonblocking) == -1)
163 throw SocketException("Failed set non-blocking mode");
170 UDPSocket::~UDPSocket()
173 dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::~UDPSocket()"<<std::endl;
176 closesocket(m_handle);
182 void UDPSocket::Bind(unsigned short port)
185 dstream<<DPS<<"UDPSocket("<<(int)m_handle
186 <<")::Bind(): port="<<port<<std::endl;
189 address.sin_family = AF_INET;
190 address.sin_addr.s_addr = INADDR_ANY;
191 address.sin_port = htons(port);
193 if(bind(m_handle, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
195 #ifndef DISABLE_ERRNO
196 dstream<<(int)m_handle<<": Bind failed: "<<strerror(errno)<<std::endl;
198 throw SocketException("Failed to bind socket");
202 void UDPSocket::Send(const Address & destination, const void * data, int size)
204 bool dumping_packet = false;
205 if(INTERNET_SIMULATOR)
206 dumping_packet = (myrand()%10==0); //easy
207 //dumping_packet = (myrand()%4==0); // hard
210 /*dstream<<DPS<<"UDPSocket("<<(int)m_handle
211 <<")::Send(): destination=";*/
213 dstream<<(int)m_handle<<" -> ";
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);
223 dstream<<" (DUMPED BY INTERNET_SIMULATOR)";
226 else if(dumping_packet)
228 // Lol let's forget it
229 dstream<<"UDPSocket::Send(): "
230 "INTERNET_SIMULATOR: dumping packet."
238 address.sin_family = AF_INET;
239 address.sin_addr.s_addr = htonl(destination.getAddress());
240 address.sin_port = htons(destination.getPort());
242 int sent = sendto(m_handle, (const char*)data, size,
243 0, (sockaddr*)&address, sizeof(sockaddr_in));
247 throw SendFailedException("Failed to send packet");
251 int UDPSocket::Receive(Address & sender, void * data, int size)
253 if(WaitData(m_timeout_ms) == false)
259 socklen_t address_len = sizeof(address);
261 int received = recvfrom(m_handle, (char*)data,
262 size, 0, (sockaddr*)&address, &address_len);
267 unsigned int address_ip = ntohl(address.sin_addr.s_addr);
268 unsigned int address_port = ntohs(address.sin_port);
270 sender = Address(address_ip, address_port);
273 //dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::Receive(): sender=";
274 dstream<<DPS<<(int)m_handle<<" <- ";
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);
290 int UDPSocket::GetHandle()
295 void UDPSocket::setTimeoutMs(int timeout_ms)
297 m_timeout_ms = timeout_ms;
300 bool UDPSocket::WaitData(int timeout_ms)
305 // Initialize the set
307 FD_SET(m_handle, &readset);
309 // Initialize time out struct
312 tv.tv_usec = timeout_ms * 1000;
314 result = select(m_handle+1, &readset, NULL, NULL, &tv);
318 /*dstream<<"Select timed out (timeout_ms="
319 <<timeout_ms<<")"<<std::endl;*/
324 #ifndef DISABLE_ERRNO
325 dstream<<(int)m_handle<<": Select failed: "<<strerror(errno)<<std::endl;
328 int e = WSAGetLastError();
329 dstream<<(int)m_handle<<": WSAGetLastError()="<<e<<std::endl;
330 if(e == 10004 /*=WSAEINTR*/)
332 dstream<<"WARNING: Ignoring WSAEINTR."<<std::endl;
336 throw SocketException("Select failed");
338 else if(FD_ISSET(m_handle, &readset) == false){
340 //dstream<<"Select reported no data in m_handle"<<std::endl;
345 //dstream<<"Select reported data in m_handle"<<std::endl;