|
From: <gb...@us...> - 2009-06-13 13:14:42
|
Revision: 424
http://gearbox.svn.sourceforge.net/gearbox/?rev=424&view=rev
Author: gbiggs
Date: 2009-06-13 12:25:55 +0000 (Sat, 13 Jun 2009)
Log Message:
-----------
Made it actually work, and enabled broadcast sending and receiving
Modified Paths:
--------------
gearbox/trunk/src/flexiport/udpport.cpp
Modified: gearbox/trunk/src/flexiport/udpport.cpp
===================================================================
--- gearbox/trunk/src/flexiport/udpport.cpp 2009-06-12 13:21:47 UTC (rev 423)
+++ gearbox/trunk/src/flexiport/udpport.cpp 2009-06-13 12:25:55 UTC (rev 424)
@@ -109,6 +109,19 @@
#endif
#endif
+void SetBroadcastFlag (int sock)
+{
+ int broadcastFlag = 1;
+ if (setsockopt (sock, SOL_SOCKET, SO_BROADCAST,
+ &broadcastFlag, sizeof (broadcastFlag)) < 0)
+ {
+ stringstream ss;
+ ss << __func__ << "() setsockopt() error: (" << ErrNo () << ") " <<
+ StrError (ErrNo ());
+ throw PortException (ss.str ());
+ }
+}
+
////////////////////////////////////////////////////////////////////////////////////////////////////
// Constructor/destructor
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -195,9 +208,9 @@
ssize_t receivedBytes = 0;
struct sockaddr_storage from;
#if defined (WIN32)
- int fromLen = 0;
+ int fromLen = sizeof (from);
#else
- socklen_t fromLen = 0;
+ socklen_t fromLen = sizeof (from);
#endif
CheckPort (true);
@@ -268,7 +281,7 @@
{
// General error
stringstream ss;
- ss << "UDPPort::" << __func__ << "() recv() error: (" << ErrNo () << ") " <<
+ ss << "UDPPort::" << __func__ << "() recvfrom() error: (" << ErrNo () << ") " <<
StrError (ErrNo ());
throw PortException (ss.str ());
}
@@ -321,13 +334,13 @@
{
// Timed out (which probably shouldn't happen)
throw PortException (string ("UDPPort::") + __func__ +
- string ("() recv() timed out, probably shouldn't happen."));
+ string ("() recvfrom() timed out, probably shouldn't happen."));
}
else
{
// General error
stringstream ss;
- ss << "UDPPort::" << __func__ << "() recv() error: (" << ErrNo () << ") " <<
+ ss << "UDPPort::" << __func__ << "() recvfrom() error: (" << ErrNo () << ") " <<
StrError (ErrNo ());
throw PortException (ss.str ());
}
@@ -536,25 +549,28 @@
return -1;
}
}
+
#if defined (WIN32)
if ((numSent = sendto (_sendSock, reinterpret_cast<const char*> (buffer), count, 0,
- reinterpret_cast<struct sockaddr*> (&_destSockAddr), sizeof (_destSockAddr))) < 0)
+ reinterpret_cast<struct sockaddr*> (&_destSockAddr),
+ sizeof (_destSockAddr))) < 0)
#else
if ((numSent = sendto (_sendSock, buffer, count, 0,
- reinterpret_cast<struct sockaddr*> (&_destSockAddr), sizeof (_destSockAddr))) < 0)
+ reinterpret_cast<struct sockaddr*> (&_destSockAddr),
+ sizeof (_destSockAddr))) < 0)
#endif
{
if (ErrNo () == ERRNO_EAGAIN)
{
if (_debug >= 2)
- cerr << "UDPPort::" << __func__ << "() Timed out while in send()" << endl;
+ cerr << "UDPPort::" << __func__ << "() Timed out while in sendto()" << endl;
return -1; // Timed out
}
else
{
// General error
stringstream ss;
- ss << "UDPPort::" << __func__ << "() send() error: (" << ErrNo () << ") " <<
+ ss << "UDPPort::" << __func__ << "() sendto() error: (" << ErrNo () << ") " <<
StrError (ErrNo ());
throw PortException (ss.str ());
}
@@ -588,7 +604,7 @@
if (numRead < 0 && ErrNo () != ERRNO_EAGAIN)
{
stringstream ss;
- ss << "UDPPort::" << __func__ << "() recv() error: (" << ErrNo () << ") " <<
+ ss << "UDPPort::" << __func__ << "() recvfrom() error: (" << ErrNo () << ") " <<
StrError (ErrNo ());
throw PortException (ss.str ());
}
@@ -698,7 +714,7 @@
// If getaddrinfo() is available, much less stuff needs to be hard-coded or copied around.
#if defined (FLEXIPORT_HAVE_GETADDRINFO)
- struct addrinfo *res = NULL, hints;
+ struct addrinfo *res = NULL, *goodAI = NULL, hints;
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
@@ -722,26 +738,37 @@
throw PortException (ss.str ());
}
- _sendSock = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
+ stringstream ss;
+ ss << "UDPPort::" << __func__ << "() socket() errors on all interfaces:";
+ for (goodAI = res; goodAI != NULL; goodAI = goodAI->ai_next)
+ {
+ _sendSock = socket (goodAI->ai_family, goodAI->ai_socktype, goodAI->ai_protocol);
#if defined (WIN32)
- if (_sendSock == INVALID_SOCKET)
+ if (_sendSock == INVALID_SOCKET)
#else
- if (_sendSock < 0)
+ if (_sendSock < 0)
#endif
+ {
+ ss << " (" << ErrNo () << ") " << StrError (ErrNo ()) << ";";
+ }
+ else
+ break;
+ }
+ if (goodAI == NULL)
{
- stringstream ss;
- ss << "UDPPort::" << __func__ << "() socket() error: (" << ErrNo () << ") " <<
- StrError (ErrNo ());
+ freeaddrinfo (res);
throw PortException (ss.str ());
}
+ SetBroadcastFlag (_sendSock);
+
if (_debug >= 1)
{
cerr << "UDPPort::" << __func__ << "() Socket created, will send to " << _destIP << ":" <<
_destPort << "." << endl;
}
- memcpy (&_destSockAddr, res, sizeof (*res));
+ memcpy (&_destSockAddr, goodAI->ai_addr, goodAI->ai_addrlen);
freeaddrinfo (res);
#else // defined (FLEXIPORT_HAVE_GETADDRINFO)
// Do it the ugly old way.
@@ -761,6 +788,8 @@
throw PortException (ss.str ());
}
+ SetBroadcastFlag (_sendSock);
+
struct hostent *hp = NULL;
if ((hp = gethostbyname (_destIP.c_str ())) == NULL)
{
@@ -804,7 +833,7 @@
CloseReceiver (); // To make sure
#if defined (FLEXIPORT_HAVE_GETADDRINFO)
- struct addrinfo *res = NULL, hints;
+ struct addrinfo *res = NULL, *goodAI = NULL, hints;
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
@@ -846,28 +875,39 @@
}
}
- _recvSock = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
+ stringstream ss;
+ ss << "UDPPort::" << __func__ << "() Errors on all interfaces:";
+ for (goodAI = res; goodAI != NULL; goodAI = goodAI->ai_next)
+ {
+ _recvSock = socket (goodAI->ai_family, goodAI->ai_socktype, goodAI->ai_protocol);
#if defined (WIN32)
- if (_recvSock == INVALID_SOCKET)
+ if (_sendSock == INVALID_SOCKET)
#else
- if (_recvSock < 0)
+ if (_sendSock < 0)
#endif
- {
- stringstream ss;
- ss << "UDPPort::" << __func__ << "() socket() error: (" << ErrNo () << ") " <<
- StrError (ErrNo ());
- throw PortException (ss.str ());
+ {
+ ss << "socket(): (" << ErrNo () << ") " << StrError (ErrNo ()) << ";";
+ continue;
+ }
+
+ if (bind (_recvSock, goodAI->ai_addr, goodAI->ai_addrlen) < 0)
+ {
+ CloseReceiver ();
+ stringstream ss;
+ ss << "bind(): (" << ErrNo () << ") " << StrError (ErrNo ()) << ";";
+ continue;
+ }
+
+ break;
}
-
- if (bind (_recvSock, res->ai_addr, res->ai_addrlen) < 0)
+ if (goodAI == NULL)
{
- CloseReceiver ();
- stringstream ss;
- ss << "UDPPort::" << __func__ << "() bind() error: (" << ErrNo () << ") " <<
- StrError (ErrNo ());
+ freeaddrinfo (res);
throw PortException (ss.str ());
}
+ SetBroadcastFlag (_recvSock);
+
freeaddrinfo (res);
#else // defined (FLEXIPORT_HAVE_GETADDRINFO)
char hostName[HOST_NAME_MAX + 1] = {'\0'};
@@ -928,6 +968,8 @@
StrError (ErrNo ());
throw PortException (ss.str ());
}
+
+ SetBroadcastFlag (_recvSock);
#endif // defined (FLEXIPORT_HAVE_GETADDRINFO)
if (_debug >= 1)
@@ -1021,7 +1063,7 @@
{
// General error
stringstream ss;
- ss << "UDPPort::" << __func__ << "() recv() error: (" << ErrNo () << ") " <<
+ ss << "UDPPort::" << __func__ << "() recvfrom() error: (" << ErrNo () << ") " <<
StrError (ErrNo ());
throw PortException (ss.str ());
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|