2 simgear::Socket, adapted from PLIB Socket by James Turner
3 Copyright (C) 2010 James Turner
5 PLIB - A Suite of Portable Game Libraries
6 Copyright (C) 1998,2002 Steve Baker
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 # include <simgear_config.h>
27 #include <simgear/compiler.h>
29 #include "sg_socket.hxx"
31 #if defined(_WIN32) && !defined(__CYGWIN__)
32 # define WINSOCK // use winsock convetions, otherwise standard POSIX
39 #include <cstdio> // for snprintf
46 # include <sys/types.h>
47 # include <sys/socket.h>
48 # include <netinet/in.h>
49 # include <arpa/inet.h>
50 # include <sys/time.h>
56 #if defined(_MSC_VER) && !defined(socklen_t)
60 #include <simgear/debug/logstream.hxx>
61 #include <simgear/structure/exception.hxx>
66 IPAddress::IPAddress ( const char* host, int port )
71 IPAddress::IPAddress ( struct sockaddr* addr, size_t len )
73 memset(&_raw, 0, sizeof(struct sockaddr_in));
74 if ((addr->sa_family != AF_INET) ||
75 (len != sizeof(struct sockaddr_in)))
77 std::cout << "address size mismatch" << std::endl;
81 memcpy(&_raw, addr, len);
84 void IPAddress::setPort(unsigned int port)
86 _raw.sin_port = htons(port);
89 void IPAddress::set ( const char* host, int port )
91 memset(&_raw, 0, sizeof(struct sockaddr_in));
93 _raw.sin_family = AF_INET ;
94 _raw.sin_port = htons (port);
96 /* Convert a string specifying a host name or one of a few symbolic
97 ** names to a numeric IP address. This usually calls gethostbyname()
98 ** to do the work; the names "" and "<broadcast>" are special.
101 if (!host || host[0] == '\0') {
102 _raw.sin_addr.s_addr = INADDR_ANY;
106 if (strcmp(host, "<broadcast>") == 0) {
107 _raw.sin_addr.s_addr = INADDR_BROADCAST;
111 _raw.sin_addr.s_addr = inet_addr ( host ) ;
112 if (_raw.sin_addr.s_addr != INADDR_NONE) {
115 // finally, try gethostbyname
116 struct hostent *hp = gethostbyname ( host ) ;
118 SG_LOG(SG_IO, SG_WARN, "gethostbyname failed for " << host);
119 _raw.sin_addr.s_addr = INADDR_ANY ;
123 memcpy ( (char *) &_raw.sin_addr, hp->h_addr, hp->h_length ) ;
127 /* Create a string object representing an IP address.
128 This is always a string of the form 'dd.dd.dd.dd' (with variable
131 const char* IPAddress::getHost () const
134 const char* buf = inet_ntoa ( sin_addr ) ;
136 static char buf [32];
137 long x = ntohl(_raw.sin_addr.s_addr);
138 sprintf(buf, "%d.%d.%d.%d",
139 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
140 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff );
145 unsigned int IPAddress::getIP () const
147 return _raw.sin_addr.s_addr;
150 unsigned int IPAddress::getPort() const
152 return ntohs(_raw.sin_port);
155 unsigned int IPAddress::getFamily () const
157 return _raw.sin_family;
160 const char* IPAddress::getLocalHost ()
162 //gethostbyname(gethostname())
165 memset(buf, 0, sizeof(buf));
166 gethostname(buf, sizeof(buf)-1);
167 const hostent *hp = gethostbyname(buf);
169 if (hp && *hp->h_addr_list)
171 in_addr addr = *((in_addr*)*hp->h_addr_list);
172 const char* host = inet_ntoa(addr);
182 bool IPAddress::getBroadcast () const
184 return (_raw.sin_addr.s_addr == INADDR_BROADCAST);
200 void Socket::setHandle (int _handle)
207 bool Socket::open ( bool stream )
210 handle = ::socket ( AF_INET, (stream? SOCK_STREAM: SOCK_DGRAM), 0 ) ;
212 // Jan 26, 2010: Patch to avoid the problem of the socket resource not
213 // yet being available if the program is restarted quickly after being
216 // Reference: http://www.unixguide.net/network/socketfaq/4.5.shtml
218 // Also required for joining multicast domains
221 #if defined(_WIN32) || defined(__CYGWIN__)
222 setsockopt( handle, SOL_SOCKET, SO_REUSEADDR, (char *)&opt_boolean,
223 sizeof(opt_boolean) );
225 setsockopt( handle, SOL_SOCKET, SO_REUSEADDR, &opt_boolean,
226 sizeof(opt_boolean) );
230 return (handle != -1);
234 void Socket::setBlocking ( bool blocking )
236 assert ( handle != -1 ) ;
239 u_long nblocking = blocking? 0: 1;
240 ::ioctlsocket(handle, FIONBIO, &nblocking);
243 int delay_flag = ::fcntl (handle, F_GETFL, 0);
246 delay_flag &= (~O_NDELAY);
248 delay_flag |= O_NDELAY;
250 ::fcntl (handle, F_SETFL, delay_flag);
255 void Socket::setBroadcast ( bool broadcast )
257 assert ( handle != -1 ) ;
261 #if defined(_WIN32) || defined(__CYGWIN__)
262 result = ::setsockopt( handle, SOL_SOCKET, SO_BROADCAST, (char*)&one, sizeof(one) );
264 result = ::setsockopt( handle, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one) );
267 result = ::setsockopt( handle, SOL_SOCKET, SO_BROADCAST, NULL, 0 );
271 throw sg_exception("Socket::setBroadcast failed");
276 int Socket::bind ( const char* host, int port )
279 assert ( handle != -1 ) ;
280 IPAddress addr ( host, port ) ;
282 #if !defined(WINSOCK)
283 if( (result = ::bind(handle,(const sockaddr*)&addr,sizeof(IPAddress))) < 0 ) {
284 SG_LOG(SG_IO, SG_ALERT, "bind(" << host << ":" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
289 // 224.0.0.0 - 239.255.255.255 are multicast
290 // Usage of 239.x.x.x is recommended for local scope
291 // Reference: http://tools.ietf.org/html/rfc5771
292 if( ntohl(addr.getIP()) >= 0xe0000000 && ntohl(addr.getIP()) <= 0xefffffff ) {
295 struct sockaddr_in a;
296 a.sin_addr.S_un.S_addr = INADDR_ANY;
297 a.sin_family = AF_INET;
298 a.sin_port = htons(port);
300 if( (result = ::bind(handle,(const sockaddr*)&a,sizeof(a))) < 0 ) {
301 SG_LOG(SG_IO, SG_ALERT, "bind(any:" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
307 mreq.imr_multiaddr.s_addr = addr.getIP();
308 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
309 if( (result=::setsockopt(getHandle(), IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq))) != 0 ) {
310 SG_LOG(SG_IO, SG_ALERT, "setsockopt(IP_ADD_MEMBERSHIP) failed. Errno " << errno << " (" << strerror(errno) << ")");
315 else if( (result = ::bind(handle,(const sockaddr*)&addr,sizeof(IPAddress))) < 0 ) {
316 SG_LOG(SG_IO, SG_ALERT, "bind(" << host << ":" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
325 int Socket::listen ( int backlog )
327 assert ( handle != -1 ) ;
328 return ::listen(handle,backlog);
332 int Socket::accept ( IPAddress* addr )
334 assert ( handle != -1 ) ;
338 return ::accept(handle,NULL,NULL);
342 socklen_t addr_len = addr->getAddressSize();
343 return ::accept(handle,(sockaddr*)addr->getAddress(),&addr_len);
348 int Socket::connect ( const char* host, int port )
350 assert ( handle != -1 ) ;
351 IPAddress addr ( host, port ) ;
352 if ( addr.getBroadcast() ) {
353 setBroadcast( true );
355 return ::connect(handle,(const sockaddr*) addr.getAddress(), addr.getAddressSize());
358 int Socket::connect ( const IPAddress& addr )
360 assert ( handle != -1 ) ;
361 if ( addr.getBroadcast() ) {
362 setBroadcast( true );
365 return ::connect(handle,(const sockaddr*) addr.getAddress(), addr.getAddressSize());
368 int Socket::send (const void * buffer, int size, int flags)
370 assert ( handle != -1 ) ;
371 return ::send (handle, (const char*)buffer, size, flags);
375 int Socket::sendto ( const void * buffer, int size,
376 int flags, const IPAddress* to )
378 assert ( handle != -1 ) ;
379 return ::sendto(handle,(const char*)buffer,size,flags,
380 (const sockaddr*)to,sizeof(IPAddress));
384 int Socket::recv (void * buffer, int size, int flags)
386 assert ( handle != -1 ) ;
387 return ::recv (handle, (char*)buffer, size, flags);
391 int Socket::recvfrom ( void * buffer, int size,
392 int flags, IPAddress* from )
394 assert ( handle != -1 ) ;
395 socklen_t fromlen = (socklen_t) sizeof(IPAddress) ;
396 return ::recvfrom(handle,(char*)buffer,size,flags,(sockaddr*)from,&fromlen);
400 void Socket::close (void)
405 ::closesocket( handle );
414 bool Socket::isNonBlockingError ()
417 int wsa_errno = WSAGetLastError();
418 if ( wsa_errno != 0 )
421 SG_LOG(SG_IO, SG_WARN, "isNonBlockingError: WSAGetLastError():" << wsa_errno);
423 case WSAEWOULDBLOCK: // always == NET_EAGAIN?
432 case EWOULDBLOCK: // always == NET_EAGAIN?
443 //////////////////////////////////////////////////////////////////////
445 // modified version by os
447 //////////////////////////////////////////////////////////////////////
448 int Socket::select ( Socket** reads, Socket** writes, int timeout )
461 for ( i=0; reads[i]; i++ )
463 int fd = reads[i]->getHandle();
471 for ( i=0; writes[i]; i++ )
473 int fd = writes[i]->getHandle();
482 /* Set up the timeout */
484 tv.tv_sec = timeout/1000;
485 tv.tv_usec = (timeout%1000)*1000;
487 // It bothers me that select()'s first argument does not appear to
488 // work as advertised... [it hangs like this if called with
489 // anything less than FD_SETSIZE, which seems wasteful?]
491 // Note: we ignore the 'exception' fd_set - I have never had a
492 // need to use it. The name is somewhat misleading - the only
493 // thing I have ever seen it used for is to detect urgent data -
494 // which is an unportable feature anyway.
496 retval = ::select (FD_SETSIZE, &r, &w, 0, &tv);
498 //remove sockets that had no activity
504 for ( k=i=0; reads[i]; i++ )
506 int fd = reads[i]->getHandle();
507 if ( FD_ISSET (fd, &r) )
509 reads[k++] = reads[i];
518 for ( k=i=0; writes[i]; i++ )
520 int fd = writes[i]->getHandle();
521 if ( FD_ISSET (fd, &w) )
523 writes[k++] = writes[i];
530 if (retval == 0) // timeout
532 if (retval == -1)// error
539 /* Init/Exit functions */
541 static void netExit ( void )
544 /* Clean up windows networking */
545 if ( WSACleanup() == SOCKET_ERROR ) {
546 if ( WSAGetLastError() == WSAEINPROGRESS ) {
547 WSACancelBlockingCall();
554 int Socket::initSockets()
556 assert ( sizeof(sockaddr_in) == sizeof(IPAddress) ) ;
559 /* Start up the windows networking */
560 WORD version_wanted = MAKEWORD(1,1);
563 if ( WSAStartup(version_wanted, &wsaData) != 0 ) {
564 throw sg_exception("WinSock initialization failed");
573 } // of namespace simgear