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
32 bool g_sockets_initialized = false;
38 if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR)
39 throw SocketException("WSAStartup failed");
42 g_sockets_initialized = true;
45 void sockets_cleanup()
56 Address::Address(unsigned int address, unsigned short port)
62 Address::Address(unsigned int a, unsigned int b,
63 unsigned int c, unsigned int d,
66 m_address = (a<<24) | (b<<16) | ( c<<8) | d;
70 bool Address::operator==(Address &address)
72 return (m_address == address.m_address
73 && m_port == address.m_port);
76 bool Address::operator!=(Address &address)
78 return !(*this == address);
81 void Address::Resolve(const char *name)
83 struct addrinfo *resolved;
84 int e = getaddrinfo(name, NULL, NULL, &resolved);
86 throw ResolveError("");
88 FIXME: This is an ugly hack; change the whole class
89 to store the address as sockaddr
91 struct sockaddr_in *t = (struct sockaddr_in*)resolved->ai_addr;
92 m_address = ntohl(t->sin_addr.s_addr);
93 freeaddrinfo(resolved);
96 unsigned int Address::getAddress() const
101 unsigned short Address::getPort() const
106 void Address::setAddress(unsigned int address)
111 void Address::setPort(unsigned short port)
116 void Address::print(std::ostream *s) const
118 (*s)<<((m_address>>24)&0xff)<<"."
119 <<((m_address>>16)&0xff)<<"."
120 <<((m_address>>8)&0xff)<<"."
121 <<((m_address>>0)&0xff)<<":"
125 void Address::print() const
130 UDPSocket::UDPSocket()
132 if(g_sockets_initialized == false)
133 throw SocketException("Sockets not initialized");
135 m_handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
138 dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::UDPSocket()"<<std::endl;
142 throw SocketException("Failed to create socket");
146 DWORD nonblocking = 0;
147 if(ioctlsocket(m_handle, FIONBIO, &nonblocking) != 0)
149 throw SocketException("Failed set non-blocking mode");
153 if(fcntl(m_handle, F_SETFL, O_NONBLOCK, nonblocking) == -1)
155 throw SocketException("Failed set non-blocking mode");
162 UDPSocket::~UDPSocket()
165 dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::~UDPSocket()"<<std::endl;
168 closesocket(m_handle);
174 void UDPSocket::Bind(unsigned short port)
177 dstream<<DPS<<"UDPSocket("<<(int)m_handle
178 <<")::Bind(): port="<<port<<std::endl;
181 address.sin_family = AF_INET;
182 address.sin_addr.s_addr = INADDR_ANY;
183 address.sin_port = htons(port);
185 if(bind(m_handle, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
187 #ifndef DISABLE_ERRNO
188 dstream<<(int)m_handle<<": Bind failed: "<<strerror(errno)<<std::endl;
190 throw SocketException("Failed to bind socket");
194 void UDPSocket::Send(const Address & destination, const void * data, int size)
196 bool dumping_packet = false;
197 if(INTERNET_SIMULATOR)
198 dumping_packet = (myrand()%10==0); //easy
199 //dumping_packet = (myrand()%4==0); // hard
202 /*dstream<<DPS<<"UDPSocket("<<(int)m_handle
203 <<")::Send(): destination=";*/
205 dstream<<(int)m_handle<<" -> ";
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);
215 dstream<<" (DUMPED BY INTERNET_SIMULATOR)";
218 else if(dumping_packet)
220 // Lol let's forget it
221 dstream<<"UDPSocket::Send(): "
222 "INTERNET_SIMULATOR: dumping packet."
230 address.sin_family = AF_INET;
231 address.sin_addr.s_addr = htonl(destination.getAddress());
232 address.sin_port = htons(destination.getPort());
234 int sent = sendto(m_handle, (const char*)data, size,
235 0, (sockaddr*)&address, sizeof(sockaddr_in));
239 throw SendFailedException("Failed to send packet");
243 int UDPSocket::Receive(Address & sender, void * data, int size)
245 if(WaitData(m_timeout_ms) == false)
251 socklen_t address_len = sizeof(address);
253 int received = recvfrom(m_handle, (char*)data,
254 size, 0, (sockaddr*)&address, &address_len);
259 unsigned int address_ip = ntohl(address.sin_addr.s_addr);
260 unsigned int address_port = ntohs(address.sin_port);
262 sender = Address(address_ip, address_port);
265 //dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::Receive(): sender=";
266 dstream<<DPS<<(int)m_handle<<" <- ";
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);
282 int UDPSocket::GetHandle()
287 void UDPSocket::setTimeoutMs(int timeout_ms)
289 m_timeout_ms = timeout_ms;
292 bool UDPSocket::WaitData(int timeout_ms)
297 // Initialize the set
299 FD_SET(m_handle, &readset);
301 // Initialize time out struct
304 tv.tv_usec = timeout_ms * 1000;
306 result = select(m_handle+1, &readset, NULL, NULL, &tv);
310 /*dstream<<"Select timed out (timeout_ms="
311 <<timeout_ms<<")"<<std::endl;*/
316 #ifndef DISABLE_ERRNO
317 dstream<<(int)m_handle<<": Select failed: "<<strerror(errno)<<std::endl;
320 int e = WSAGetLastError();
321 dstream<<(int)m_handle<<": WSAGetLastError()="<<e<<std::endl;
322 if(e == 10004 /*=WSAEINTR*/)
324 dstream<<"WARNING: Ignoring WSAEINTR."<<std::endl;
328 throw SocketException("Select failed");
330 else if(FD_ISSET(m_handle, &readset) == false){
332 //dstream<<"Select reported no data in m_handle"<<std::endl;
337 //dstream<<"Select reported data in m_handle"<<std::endl;