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.
27 // Debug printing options
31 bool g_sockets_initialized = false;
37 if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR)
38 throw SocketException("WSAStartup failed");
41 g_sockets_initialized = true;
44 void sockets_cleanup()
55 Address::Address(unsigned int address, unsigned short port)
61 Address::Address(unsigned int a, unsigned int b,
62 unsigned int c, unsigned int d,
65 m_address = (a<<24) | (b<<16) | ( c<<8) | d;
69 bool Address::operator==(Address &address)
71 return (m_address == address.m_address
72 && m_port == address.m_port);
75 bool Address::operator!=(Address &address)
77 return !(*this == address);
80 void Address::Resolve(const char *name)
82 struct addrinfo *resolved;
83 int e = getaddrinfo(name, NULL, NULL, &resolved);
85 throw ResolveError("");
87 FIXME: This is an ugly hack; change the whole class
88 to store the address as sockaddr
90 struct sockaddr_in *t = (struct sockaddr_in*)resolved->ai_addr;
91 m_address = ntohl(t->sin_addr.s_addr);
92 freeaddrinfo(resolved);
95 unsigned int Address::getAddress() const
100 unsigned short Address::getPort() const
105 void Address::setAddress(unsigned int address)
110 void Address::setPort(unsigned short port)
115 void Address::print(std::ostream *s) const
117 (*s)<<((m_address>>24)&0xff)<<"."
118 <<((m_address>>16)&0xff)<<"."
119 <<((m_address>>8)&0xff)<<"."
120 <<((m_address>>0)&0xff)<<":"
124 void Address::print() const
129 UDPSocket::UDPSocket()
131 if(g_sockets_initialized == false)
132 throw SocketException("Sockets not initialized");
134 m_handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
137 dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::UDPSocket()"<<std::endl;
141 throw SocketException("Failed to create socket");
145 DWORD nonblocking = 0;
146 if(ioctlsocket(m_handle, FIONBIO, &nonblocking) != 0)
148 throw SocketException("Failed set non-blocking mode");
152 if(fcntl(m_handle, F_SETFL, O_NONBLOCK, nonblocking) == -1)
154 throw SocketException("Failed set non-blocking mode");
161 UDPSocket::~UDPSocket()
164 dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::~UDPSocket()"<<std::endl;
167 closesocket(m_handle);
173 void UDPSocket::Bind(unsigned short port)
176 dstream<<DPS<<"UDPSocket("<<(int)m_handle
177 <<")::Bind(): port="<<port<<std::endl;
180 address.sin_family = AF_INET;
181 address.sin_addr.s_addr = INADDR_ANY;
182 address.sin_port = htons(port);
184 if(bind(m_handle, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
186 #ifndef DISABLE_ERRNO
187 dstream<<(int)m_handle<<": Bind failed: "<<strerror(errno)<<std::endl;
189 throw SocketException("Failed to bind socket");
193 void UDPSocket::Send(const Address & destination, const void * data, int size)
195 bool dumping_packet = false;
196 if(INTERNET_SIMULATOR)
197 dumping_packet = (rand()%10==0); //easy
198 //dumping_packet = (rand()%4==0); // hard
201 /*dstream<<DPS<<"UDPSocket("<<(int)m_handle
202 <<")::Send(): destination=";*/
204 dstream<<(int)m_handle<<" -> ";
206 dstream<<", size="<<size<<", data=";
207 for(int i=0; i<size && i<20; i++){
208 if(i%2==0) printf(" ");
209 DEBUGPRINT("%.2X", ((int)((const char*)data)[i])&0xff);
214 dstream<<" (DUMPED BY INTERNET_SIMULATOR)";
217 else if(dumping_packet)
219 // Lol let's forget it
220 dstream<<"UDPSocket::Send(): "
221 "INTERNET_SIMULATOR: dumping packet."
229 address.sin_family = AF_INET;
230 address.sin_addr.s_addr = htonl(destination.getAddress());
231 address.sin_port = htons(destination.getPort());
233 int sent = sendto(m_handle, (const char*)data, size,
234 0, (sockaddr*)&address, sizeof(sockaddr_in));
238 throw SendFailedException("Failed to send packet");
242 int UDPSocket::Receive(Address & sender, void * data, int size)
244 if(WaitData(m_timeout_ms) == false)
250 socklen_t address_len = sizeof(address);
252 int received = recvfrom(m_handle, (char*)data,
253 size, 0, (sockaddr*)&address, &address_len);
258 unsigned int address_ip = ntohl(address.sin_addr.s_addr);
259 unsigned int address_port = ntohs(address.sin_port);
261 sender = Address(address_ip, address_port);
264 //dstream<<DPS<<"UDPSocket("<<(int)m_handle<<")::Receive(): sender=";
265 dstream<<DPS<<(int)m_handle<<" <- ";
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) printf(" ");
271 DEBUGPRINT("%.2X", ((int)((const char*)data)[i])&0xff);
281 int UDPSocket::GetHandle()
286 void UDPSocket::setTimeoutMs(int timeout_ms)
288 m_timeout_ms = timeout_ms;
291 bool UDPSocket::WaitData(int timeout_ms)
296 // Initialize the set
298 FD_SET(m_handle, &readset);
300 // Initialize time out struct
303 tv.tv_usec = timeout_ms * 1000;
305 result = select(m_handle+1, &readset, NULL, NULL, &tv);
309 /*dstream<<"Select timed out (timeout_ms="
310 <<timeout_ms<<")"<<std::endl;*/
315 #ifndef DISABLE_ERRNO
316 dstream<<(int)m_handle<<": Select failed: "<<strerror(errno)<<std::endl;
319 int e = WSAGetLastError();
320 dstream<<(int)m_handle<<": WSAGetLastError()="<<e<<std::endl;
321 if(e == 10004 /*=WSAEINTR*/)
323 dstream<<"WARNING: Ignoring WSAEINTR."<<std::endl;
327 throw SocketException("Select failed");
329 else if(FD_ISSET(m_handle, &readset) == false){
331 //dstream<<"Select reported no data in m_handle"<<std::endl;
336 //dstream<<"Select reported data in m_handle"<<std::endl;