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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 # include <simgear_config.h>
27 #include <simgear/compiler.h>
28 #include "raw_socket.hxx"
30 #if defined(_WIN32) && !defined(__CYGWIN__)
31 # define WINSOCK // use winsock convetions, otherwise standard POSIX
38 #include <cstdio> // for snprintf
42 # include <winsock2.h>
43 # include <ws2tcpip.h>
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)
62 #include <simgear/debug/logstream.hxx>
63 #include <simgear/structure/exception.hxx>
64 #include <simgear/threads/SGThread.hxx>
70 class Resolver : public SGThread
73 static Resolver* instance()
75 if (!static_instance) {
76 static_instance = new Resolver;
77 atexit(&Resolver::cleanup);
78 static_instance->start();
81 return static_instance;
86 static_instance->shutdown();
87 static_instance->join();
103 simgear::IPAddress* lookup(const string& host)
105 simgear::IPAddress* result = NULL;
107 AddressCache::iterator it = _cache.find(host);
108 if (it == _cache.end()) {
109 _cache[host] = NULL; // mark as needing looked up
110 _wait.signal(); // if the thread was sleeping, poke it
118 simgear::IPAddress* lookupSync(const string& host)
120 simgear::IPAddress* result = NULL;
122 AddressCache::iterator it = _cache.find(host);
123 if (it == _cache.end()) {
125 result = new simgear::IPAddress;
126 bool ok = lookupHost(host.c_str(), *result);
129 _cache[host] = result; // mark as needing looked up
134 } else { // found in cache, easy
142 * run method waits on a condition (_wait), and when awoken,
143 * finds any unresolved entries in _cache, resolves them, and goes
150 AddressCache::iterator it;
152 for (it = _cache.begin(); it != _cache.end(); ++it) {
153 if (it->second == NULL) {
154 string h = it->first;
157 simgear::IPAddress* addr = new simgear::IPAddress;
158 // may take seconds or even minutes!
159 lookupHost(h.c_str(), *addr);
162 // cahce may have changed while we had the lock released -
163 // so iterators may be invalid: restart the traversal
166 } // of found un-resolved entry
167 } // of un-resolved address iteration
169 } // of thread run loop
173 static Resolver* static_instance;
176 * The actual synchronous, blocking host lookup function
177 * do *not* call this with any locks (mutexs) held, since depending
178 * on local system configuration / network availability, it
179 * may block for seconds or minutes.
181 bool lookupHost(const char* host, simgear::IPAddress& addr)
183 struct addrinfo hints;
184 memset(&hints, 0, sizeof(struct addrinfo));
185 hints.ai_family = AF_INET;
188 struct addrinfo* result0 = NULL;
189 int err = getaddrinfo(host, NULL, &hints, &result0);
191 SG_LOG(SG_IO, SG_WARN, "getaddrinfo failed for '" << host << "' : " << gai_strerror(err));
194 struct addrinfo* result;
195 for (result = result0; result != NULL; result = result->ai_next) {
196 if (result->ai_family != AF_INET) { // only accept IP4 for the moment
200 if (result->ai_addrlen != addr.getAddrLen()) {
201 SG_LOG(SG_IO, SG_ALERT, "mismatch in socket address sizes: got " <<
202 result->ai_addrlen << ", expected " << addr.getAddrLen());
206 memcpy(addr.getAddr(), result->ai_addr, result->ai_addrlen);
209 } // of getaddrinfo results iteration
210 } // of getaddrinfo succeeded
212 freeaddrinfo(result0);
217 SGWaitCondition _wait;
219 typedef std::map<string, simgear::IPAddress*> AddressCache;
224 Resolver* Resolver::static_instance = NULL;
226 } // of anonymous namespace
231 IPAddress::IPAddress ( const char* host, int port )
236 IPAddress::IPAddress( const IPAddress& other ) :
240 addr = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));
241 memcpy(addr, other.addr, sizeof(struct sockaddr_in));
245 const IPAddress& IPAddress::operator=(const IPAddress& other)
253 addr = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));
254 memcpy(addr, other.addr, sizeof(struct sockaddr_in));
260 void IPAddress::set ( const char* host, int port )
262 addr = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));
263 memset(addr, 0, sizeof(struct sockaddr_in));
265 addr->sin_family = AF_INET ;
266 addr->sin_port = htons (port);
268 /* Convert a string specifying a host name or one of a few symbolic
269 ** names to a numeric IP address. This usually calls getaddrinfo()
270 ** to do the work; the names "" and "<broadcast>" are special.
273 if (!host || host[0] == '\0') {
274 addr->sin_addr.s_addr = INADDR_ANY;
278 if (strcmp(host, "<broadcast>") == 0) {
279 addr->sin_addr.s_addr = INADDR_BROADCAST;
284 IPAddress* cached = Resolver::instance()->lookupSync(host);
286 memcpy(addr, cached->getAddr(), cached->getAddrLen());
289 addr->sin_port = htons (port); // fix up port after getaddrinfo
292 IPAddress::~IPAddress()
299 bool IPAddress::lookupNonblocking(const char* host, IPAddress& addr)
301 IPAddress* cached = Resolver::instance()->lookup(host);
310 /* Create a string object representing an IP address.
311 This is always a string of the form 'dd.dd.dd.dd' (with variable
314 const char* IPAddress::getHost () const
320 static char buf [32];
321 long x = ntohl(addr->sin_addr.s_addr);
322 sprintf(buf, "%d.%d.%d.%d",
323 (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
324 (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff );
328 unsigned int IPAddress::getIP () const
334 return addr->sin_addr.s_addr;
337 unsigned int IPAddress::getPort() const
343 return ntohs(addr->sin_port);
346 void IPAddress::setPort(int port)
352 addr->sin_port = htons(port);
355 unsigned int IPAddress::getFamily () const
361 return addr->sin_family;
364 const char* IPAddress::getLocalHost ()
366 //gethostbyname(gethostname())
369 memset(buf, 0, sizeof(buf));
370 gethostname(buf, sizeof(buf)-1);
371 const hostent *hp = gethostbyname(buf);
373 if (hp && *hp->h_addr_list)
375 in_addr addr = *((in_addr*)*hp->h_addr_list);
376 const char* host = inet_ntoa(addr);
386 bool IPAddress::getBroadcast () const
392 return (addr->sin_addr.s_addr == INADDR_BROADCAST);
395 unsigned int IPAddress::getAddrLen() const
397 return sizeof(struct sockaddr_in);
400 struct sockaddr* IPAddress::getAddr() const
403 addr = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));
404 memset(addr, 0, sizeof(struct sockaddr_in));
407 return (struct sockaddr*) addr;
410 bool IPAddress::isValid() const
412 return (addr != NULL);
427 void Socket::setHandle (int _handle)
434 bool Socket::open ( bool stream )
437 handle = ::socket ( AF_INET, (stream? SOCK_STREAM: SOCK_DGRAM), 0 ) ;
439 // Jan 26, 2010: Patch to avoid the problem of the socket resource not
440 // yet being available if the program is restarted quickly after being
443 // Reference: http://www.unixguide.net/network/socketfaq/4.5.shtml
445 // Also required for joining multicast domains
448 #if defined(_WIN32) || defined(__CYGWIN__)
449 setsockopt( handle, SOL_SOCKET, SO_REUSEADDR, (char *)&opt_boolean,
450 sizeof(opt_boolean) );
452 setsockopt( handle, SOL_SOCKET, SO_REUSEADDR, &opt_boolean,
453 sizeof(opt_boolean) );
458 // Do not generate SIGPIPE signal (which immediately terminates the program),
459 // instead ::send() will return -1 and errno will be set to EPIPE.
460 // SO_NOSIGPIPE should be available on Mac/BSD systems, but is not available
461 // within Posix/Linux.
462 // This only works for calls to ::send() but not for ::write():
463 // http://freebsd.1045724.n5.nabble.com/is-setsockopt-SO-NOSIGPIPE-work-tp4011054p4011055.html
465 setsockopt(handle, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(set));
469 # define MSG_NOSIGNAL 0
471 // TODO supress SIGPIPE if neither SO_NOSIGPIPE nor MSG_NOSIGNAL is available
472 // http://krokisplace.blogspot.co.at/2010/02/suppressing-sigpipe-in-library.html
474 return (handle != -1);
478 void Socket::setBlocking ( bool blocking )
480 assert ( handle != -1 ) ;
483 u_long nblocking = blocking? 0: 1;
484 ::ioctlsocket(handle, FIONBIO, &nblocking);
487 int delay_flag = ::fcntl (handle, F_GETFL, 0);
490 delay_flag &= (~O_NDELAY);
492 delay_flag |= O_NDELAY;
494 ::fcntl (handle, F_SETFL, delay_flag);
499 void Socket::setBroadcast ( bool broadcast )
501 assert ( handle != -1 ) ;
505 #if defined(_WIN32) || defined(__CYGWIN__)
506 result = ::setsockopt( handle, SOL_SOCKET, SO_BROADCAST, (char*)&one, sizeof(one) );
508 result = ::setsockopt( handle, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one) );
511 result = ::setsockopt( handle, SOL_SOCKET, SO_BROADCAST, NULL, 0 );
515 throw sg_exception("Socket::setBroadcast failed");
520 int Socket::bind ( const char* host, int port )
523 assert ( handle != -1 ) ;
524 IPAddress addr ( host, port ) ;
526 #if !defined(WINSOCK)
527 if( (result = ::bind(handle, addr.getAddr(), addr.getAddrLen() ) ) < 0 ) {
528 SG_LOG(SG_IO, SG_ALERT, "bind(" << addr.getHost() << ":" << addr.getPort() << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
533 // 224.0.0.0 - 239.255.255.255 are multicast
534 // Usage of 239.x.x.x is recommended for local scope
535 // Reference: http://tools.ietf.org/html/rfc5771
536 if( ntohl(addr.getIP()) >= 0xe0000000 && ntohl(addr.getIP()) <= 0xefffffff ) {
539 struct sockaddr_in a;
540 a.sin_addr.S_un.S_addr = INADDR_ANY;
541 a.sin_family = AF_INET;
542 a.sin_port = htons(port);
544 if( (result = ::bind(handle,(const sockaddr*)&a,sizeof(a))) < 0 ) {
545 SG_LOG(SG_IO, SG_ALERT, "bind(any:" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
551 mreq.imr_multiaddr.s_addr = addr.getIP();
552 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
553 if( (result=::setsockopt(getHandle(), IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq))) != 0 ) {
554 SG_LOG(SG_IO, SG_ALERT, "setsockopt(IP_ADD_MEMBERSHIP) failed. Errno " << errno << " (" << strerror(errno) << ")");
559 else if( (result = ::bind(handle,addr.getAddr(), addr.getAddrLen())) < 0 ) {
560 SG_LOG(SG_IO, SG_ALERT, "bind(" << host << ":" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
569 int Socket::listen ( int backlog )
571 assert ( handle != -1 ) ;
572 return ::listen(handle,backlog);
576 int Socket::accept ( IPAddress* addr )
578 assert ( handle != -1 ) ;
582 return ::accept(handle,NULL,NULL);
586 socklen_t addr_len = addr->getAddrLen(); ;
587 return ::accept(handle, addr->getAddr(), &addr_len);
592 int Socket::connect ( const char* host, int port )
594 IPAddress addr ( host, port ) ;
595 return connect ( &addr );
599 int Socket::connect ( IPAddress* addr )
601 assert ( handle != -1 ) ;
602 if ( addr->getBroadcast() ) {
603 setBroadcast( true );
605 return ::connect(handle, addr->getAddr(), addr->getAddrLen() );
609 int Socket::send (const void * buffer, int size, int flags)
611 assert ( handle != -1 ) ;
612 return ::send (handle, (const char*)buffer, size, flags | MSG_NOSIGNAL);
616 int Socket::sendto ( const void * buffer, int size,
617 int flags, const IPAddress* to )
619 assert ( handle != -1 ) ;
620 return ::sendto( handle,
623 flags | MSG_NOSIGNAL,
624 (const sockaddr*)to->getAddr(),
629 int Socket::recv (void * buffer, int size, int flags)
631 assert ( handle != -1 ) ;
632 return ::recv (handle, (char*)buffer, size, flags);
636 int Socket::recvfrom ( void * buffer, int size,
637 int flags, IPAddress* from )
639 assert ( handle != -1 ) ;
640 socklen_t fromlen = (socklen_t) from->getAddrLen() ;
641 return ::recvfrom(handle,(char*)buffer,size,flags, from->getAddr(),&fromlen);
645 void Socket::close (void)
650 ::closesocket( handle );
659 bool Socket::isNonBlockingError ()
662 int wsa_errno = WSAGetLastError();
663 if ( wsa_errno != 0 )
666 SG_LOG(SG_IO, SG_WARN, "isNonBlockingError: WSAGetLastError():" << wsa_errno);
668 case WSAEWOULDBLOCK: // always == NET_EAGAIN?
677 case EWOULDBLOCK: // always == NET_EAGAIN?
688 //////////////////////////////////////////////////////////////////////
690 // modified version by os
692 //////////////////////////////////////////////////////////////////////
693 int Socket::select ( Socket** reads, Socket** writes, int timeout )
706 for ( i=0; reads[i]; i++ )
708 int fd = reads[i]->getHandle();
716 for ( i=0; writes[i]; i++ )
718 int fd = writes[i]->getHandle();
727 /* Set up the timeout */
729 tv.tv_sec = timeout/1000;
730 tv.tv_usec = (timeout%1000)*1000;
732 // It bothers me that select()'s first argument does not appear to
733 // work as advertised... [it hangs like this if called with
734 // anything less than FD_SETSIZE, which seems wasteful?]
736 // Note: we ignore the 'exception' fd_set - I have never had a
737 // need to use it. The name is somewhat misleading - the only
738 // thing I have ever seen it used for is to detect urgent data -
739 // which is an unportable feature anyway.
741 retval = ::select (FD_SETSIZE, &r, &w, 0, &tv);
743 //remove sockets that had no activity
749 for ( k=i=0; reads[i]; i++ )
751 int fd = reads[i]->getHandle();
752 if ( FD_ISSET (fd, &r) )
754 reads[k++] = reads[i];
763 for ( k=i=0; writes[i]; i++ )
765 int fd = writes[i]->getHandle();
766 if ( FD_ISSET (fd, &w) )
768 writes[k++] = writes[i];
775 if (retval == 0) // timeout
777 if (retval == -1)// error
784 /* Init/Exit functions */
786 static void netExit ( void )
789 /* Clean up windows networking */
790 if ( WSACleanup() == SOCKET_ERROR ) {
791 if ( WSAGetLastError() == WSAEINPROGRESS ) {
792 WSACancelBlockingCall();
799 int Socket::initSockets()
802 /* Start up the windows networking */
803 WORD version_wanted = MAKEWORD(1,1);
806 if ( WSAStartup(version_wanted, &wsaData) != 0 ) {
807 throw sg_exception("WinSock initialization failed");
816 } // of namespace simgear