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) );
457 return (handle != -1);
461 void Socket::setBlocking ( bool blocking )
463 assert ( handle != -1 ) ;
466 u_long nblocking = blocking? 0: 1;
467 ::ioctlsocket(handle, FIONBIO, &nblocking);
470 int delay_flag = ::fcntl (handle, F_GETFL, 0);
473 delay_flag &= (~O_NDELAY);
475 delay_flag |= O_NDELAY;
477 ::fcntl (handle, F_SETFL, delay_flag);
482 void Socket::setBroadcast ( bool broadcast )
484 assert ( handle != -1 ) ;
488 #if defined(_WIN32) || defined(__CYGWIN__)
489 result = ::setsockopt( handle, SOL_SOCKET, SO_BROADCAST, (char*)&one, sizeof(one) );
491 result = ::setsockopt( handle, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one) );
494 result = ::setsockopt( handle, SOL_SOCKET, SO_BROADCAST, NULL, 0 );
498 throw sg_exception("Socket::setBroadcast failed");
503 int Socket::bind ( const char* host, int port )
506 assert ( handle != -1 ) ;
507 IPAddress addr ( host, port ) ;
509 #if !defined(WINSOCK)
510 if( (result = ::bind(handle, addr.getAddr(), addr.getAddrLen() ) ) < 0 ) {
511 SG_LOG(SG_IO, SG_ALERT, "bind(" << addr.getHost() << ":" << addr.getPort() << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
516 // 224.0.0.0 - 239.255.255.255 are multicast
517 // Usage of 239.x.x.x is recommended for local scope
518 // Reference: http://tools.ietf.org/html/rfc5771
519 if( ntohl(addr.getIP()) >= 0xe0000000 && ntohl(addr.getIP()) <= 0xefffffff ) {
522 struct sockaddr_in a;
523 a.sin_addr.S_un.S_addr = INADDR_ANY;
524 a.sin_family = AF_INET;
525 a.sin_port = htons(port);
527 if( (result = ::bind(handle,(const sockaddr*)&a,sizeof(a))) < 0 ) {
528 SG_LOG(SG_IO, SG_ALERT, "bind(any:" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
534 mreq.imr_multiaddr.s_addr = addr.getIP();
535 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
536 if( (result=::setsockopt(getHandle(), IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq))) != 0 ) {
537 SG_LOG(SG_IO, SG_ALERT, "setsockopt(IP_ADD_MEMBERSHIP) failed. Errno " << errno << " (" << strerror(errno) << ")");
542 else if( (result = ::bind(handle,addr.getAddr(), addr.getAddrLen())) < 0 ) {
543 SG_LOG(SG_IO, SG_ALERT, "bind(" << host << ":" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
552 int Socket::listen ( int backlog )
554 assert ( handle != -1 ) ;
555 return ::listen(handle,backlog);
559 int Socket::accept ( IPAddress* addr )
561 assert ( handle != -1 ) ;
565 return ::accept(handle,NULL,NULL);
569 socklen_t addr_len = addr->getAddrLen(); ;
570 return ::accept(handle, addr->getAddr(), &addr_len);
575 int Socket::connect ( const char* host, int port )
577 IPAddress addr ( host, port ) ;
578 return connect ( &addr );
582 int Socket::connect ( IPAddress* addr )
584 assert ( handle != -1 ) ;
585 if ( addr->getBroadcast() ) {
586 setBroadcast( true );
588 return ::connect(handle, addr->getAddr(), addr->getAddrLen() );
592 int Socket::send (const void * buffer, int size, int flags)
594 assert ( handle != -1 ) ;
595 return ::send (handle, (const char*)buffer, size, flags);
599 int Socket::sendto ( const void * buffer, int size,
600 int flags, const IPAddress* to )
602 assert ( handle != -1 ) ;
603 return ::sendto(handle,(const char*)buffer,size,flags,
604 (const sockaddr*) to->getAddr(), to->getAddrLen());
608 int Socket::recv (void * buffer, int size, int flags)
610 assert ( handle != -1 ) ;
611 return ::recv (handle, (char*)buffer, size, flags);
615 int Socket::recvfrom ( void * buffer, int size,
616 int flags, IPAddress* from )
618 assert ( handle != -1 ) ;
619 socklen_t fromlen = (socklen_t) from->getAddrLen() ;
620 return ::recvfrom(handle,(char*)buffer,size,flags, from->getAddr(),&fromlen);
624 void Socket::close (void)
629 ::closesocket( handle );
638 bool Socket::isNonBlockingError ()
641 int wsa_errno = WSAGetLastError();
642 if ( wsa_errno != 0 )
645 SG_LOG(SG_IO, SG_WARN, "isNonBlockingError: WSAGetLastError():" << wsa_errno);
647 case WSAEWOULDBLOCK: // always == NET_EAGAIN?
656 case EWOULDBLOCK: // always == NET_EAGAIN?
667 //////////////////////////////////////////////////////////////////////
669 // modified version by os
671 //////////////////////////////////////////////////////////////////////
672 int Socket::select ( Socket** reads, Socket** writes, int timeout )
685 for ( i=0; reads[i]; i++ )
687 int fd = reads[i]->getHandle();
695 for ( i=0; writes[i]; i++ )
697 int fd = writes[i]->getHandle();
706 /* Set up the timeout */
708 tv.tv_sec = timeout/1000;
709 tv.tv_usec = (timeout%1000)*1000;
711 // It bothers me that select()'s first argument does not appear to
712 // work as advertised... [it hangs like this if called with
713 // anything less than FD_SETSIZE, which seems wasteful?]
715 // Note: we ignore the 'exception' fd_set - I have never had a
716 // need to use it. The name is somewhat misleading - the only
717 // thing I have ever seen it used for is to detect urgent data -
718 // which is an unportable feature anyway.
720 retval = ::select (FD_SETSIZE, &r, &w, 0, &tv);
722 //remove sockets that had no activity
728 for ( k=i=0; reads[i]; i++ )
730 int fd = reads[i]->getHandle();
731 if ( FD_ISSET (fd, &r) )
733 reads[k++] = reads[i];
742 for ( k=i=0; writes[i]; i++ )
744 int fd = writes[i]->getHandle();
745 if ( FD_ISSET (fd, &w) )
747 writes[k++] = writes[i];
754 if (retval == 0) // timeout
756 if (retval == -1)// error
763 /* Init/Exit functions */
765 static void netExit ( void )
768 /* Clean up windows networking */
769 if ( WSACleanup() == SOCKET_ERROR ) {
770 if ( WSAGetLastError() == WSAEINPROGRESS ) {
771 WSACancelBlockingCall();
778 int Socket::initSockets()
781 /* Start up the windows networking */
782 WORD version_wanted = MAKEWORD(1,1);
785 if ( WSAStartup(version_wanted, &wsaData) != 0 ) {
786 throw sg_exception("WinSock initialization failed");
795 } // of namespace simgear