summaryrefslogtreecommitdiff
path: root/indra/llmessage/net.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'indra/llmessage/net.cpp')
-rw-r--r--indra/llmessage/net.cpp189
1 files changed, 149 insertions, 40 deletions
diff --git a/indra/llmessage/net.cpp b/indra/llmessage/net.cpp
index 50b34148d4..97611c3b51 100644
--- a/indra/llmessage/net.cpp
+++ b/indra/llmessage/net.cpp
@@ -2,30 +2,25 @@
* @file net.cpp
* @brief Cross-platform routines for sending and receiving packets.
*
- * $LicenseInfo:firstyear=2000&license=viewergpl$
- *
- * Copyright (c) 2000-2007, Linden Research, Inc.
- *
+ * $LicenseInfo:firstyear=2000&license=viewerlgpl$
* Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlife.com/developers/opensource/gplv2
+ * Copyright (C) 2010, Linden Research, Inc.
*
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at http://secondlife.com/developers/opensource/flossexception
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License only.
*
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
*
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
@@ -50,7 +45,6 @@
#endif
// linden library includes
-#include "network.h"
#include "llerror.h"
#include "llhost.h"
#include "lltimer.h"
@@ -80,8 +74,10 @@ typedef int socklen_t;
#endif
+static U32 gsnReceivingIFAddr = INVALID_HOST_IP_ADDRESS; // Address to which datagram was sent
const char* LOOPBACK_ADDRESS_STRING = "127.0.0.1";
+const char* BROADCAST_ADDRESS_STRING = "255.255.255.255";
#if LL_DARWIN
// Mac OS X returns an error when trying to set these to 400000. Smaller values succeed.
@@ -109,6 +105,16 @@ U32 get_sender_port()
return ntohs(stSrcAddr.sin_port);
}
+LLHost get_receiving_interface()
+{
+ return LLHost(gsnReceivingIFAddr, INVALID_PORT);
+}
+
+U32 get_receiving_interface_ip(void)
+{
+ return gsnReceivingIFAddr;
+}
+
const char* u32_to_ip_string(U32 ip)
{
static char buffer[MAXADDRSTR]; /* Flawfinder: ignore */
@@ -159,7 +165,21 @@ char *u32_to_ip_string(U32 ip, char *ip_string)
// Wrapper for inet_addr()
U32 ip_string_to_u32(const char* ip_string)
{
- return inet_addr(ip_string);
+ // *NOTE: Windows doesn't support inet_aton(), so we are using
+ // inet_addr(). Unfortunately, INADDR_NONE == INADDR_BROADCAST, so
+ // we have to check whether the input is a broadcast address before
+ // deciding that @ip_string is invalid.
+ //
+ // Also, our definition of INVALID_HOST_IP_ADDRESS doesn't allow us to
+ // use wildcard addresses. -Ambroff
+ U32 ip = inet_addr(ip_string);
+ if (ip == INADDR_NONE
+ && strncmp(ip_string, BROADCAST_ADDRESS_STRING, MAXADDRSTR) != 0)
+ {
+ llwarns << "ip_string_to_u32() failed, Error: Invalid IP string '" << ip_string << "'" << llendl;
+ return INVALID_HOST_IP_ADDRESS;
+ }
+ return ip;
}
@@ -185,7 +205,7 @@ S32 start_net(S32& socket_out, int& nPort)
{
S32 err = WSAGetLastError();
WSACleanup();
- llwarns << "Windows Sockets initialization failed, err " << err << llendl;
+ LL_WARNS("AppInit") << "Windows Sockets initialization failed, err " << err << LL_ENDL;
return 1;
}
@@ -195,7 +215,7 @@ S32 start_net(S32& socket_out, int& nPort)
{
S32 err = WSAGetLastError();
WSACleanup();
- llwarns << "socket() failed, err " << err << llendl;
+ LL_WARNS("AppInit") << "socket() failed, err " << err << LL_ENDL;
return 2;
}
@@ -205,7 +225,7 @@ S32 start_net(S32& socket_out, int& nPort)
stLclAddr.sin_port = htons(nPort);
S32 attempt_port = nPort;
- llinfos << "attempting to connect on port " << attempt_port << llendl;
+ LL_DEBUGS("AppInit") << "attempting to connect on port " << attempt_port << LL_ENDL;
nRet = bind(hSocket, (struct sockaddr*) &stLclAddr, sizeof(stLclAddr));
if (nRet == SOCKET_ERROR)
@@ -219,7 +239,7 @@ S32 start_net(S32& socket_out, int& nPort)
attempt_port++)
{
stLclAddr.sin_port = htons(attempt_port);
- llinfos << "trying port " << attempt_port << llendl;
+ LL_DEBUGS("AppInit") << "trying port " << attempt_port << LL_ENDL;
nRet = bind(hSocket, (struct sockaddr*) &stLclAddr, sizeof(stLclAddr));
if (!(nRet == SOCKET_ERROR &&
@@ -231,7 +251,7 @@ S32 start_net(S32& socket_out, int& nPort)
if (nRet == SOCKET_ERROR)
{
- llwarns << "startNet() : Couldn't find available network port." << llendl;
+ LL_WARNS("AppInit") << "startNet() : Couldn't find available network port." << LL_ENDL;
// Fail gracefully here in release
return 3;
}
@@ -239,7 +259,7 @@ S32 start_net(S32& socket_out, int& nPort)
else
// Some other socket error
{
- llwarns << llformat("bind() port: %d failed, Err: %d\n", nPort, WSAGetLastError()) << llendl;
+ LL_WARNS("AppInit") << llformat("bind() port: %d failed, Err: %d\n", nPort, WSAGetLastError()) << LL_ENDL;
// Fail gracefully in release.
return 4;
}
@@ -250,7 +270,7 @@ S32 start_net(S32& socket_out, int& nPort)
getsockname(hSocket, (SOCKADDR*) &socket_address, &socket_address_size);
attempt_port = ntohs(socket_address.sin_port);
- llinfos << "connected on port " << attempt_port << llendl;
+ LL_INFOS("AppInit") << "connected on port " << attempt_port << LL_ENDL;
nPort = attempt_port;
// Set socket to be non-blocking
@@ -266,25 +286,24 @@ S32 start_net(S32& socket_out, int& nPort)
nRet = setsockopt(hSocket, SOL_SOCKET, SO_RCVBUF, (char *)&rec_size, buff_size);
if (nRet)
{
- llinfos << "Can't set receive buffer size!" << llendl;
+ LL_INFOS("AppInit") << "Can't set receive buffer size!" << LL_ENDL;
}
nRet = setsockopt(hSocket, SOL_SOCKET, SO_SNDBUF, (char *)&snd_size, buff_size);
if (nRet)
{
- llinfos << "Can't set send buffer size!" << llendl;
+ LL_INFOS("AppInit") << "Can't set send buffer size!" << LL_ENDL;
}
getsockopt(hSocket, SOL_SOCKET, SO_RCVBUF, (char *)&rec_size, &buff_size);
getsockopt(hSocket, SOL_SOCKET, SO_SNDBUF, (char *)&snd_size, &buff_size);
- llinfos << "startNet - receive buffer size : " << rec_size << llendl;
- llinfos << "startNet - send buffer size : " << snd_size << llendl;
+ LL_DEBUGS("AppInit") << "startNet - receive buffer size : " << rec_size << LL_ENDL;
+ LL_DEBUGS("AppInit") << "startNet - send buffer size : " << snd_size << LL_ENDL;
// Setup a destination address
- char achMCAddr[MAXADDRSTR] = " "; /* Flawfinder: ignore */
stDstAddr.sin_family = AF_INET;
- stDstAddr.sin_addr.s_addr = inet_addr(achMCAddr);
+ stDstAddr.sin_addr.s_addr = INVALID_HOST_IP_ADDRESS;
stDstAddr.sin_port = htons(nPort);
socket_out = hSocket;
@@ -383,11 +402,30 @@ S32 start_net(S32& socket_out, int& nPort)
return 1;
}
- // Don't bind() if we want the operating system to assign our ports for
- // us.
if (NET_USE_OS_ASSIGNED_PORT == nPort)
{
- // Do nothing; the operating system will do it for us.
+ // Although bind is not required it will tell us which port we were
+ // assigned to.
+ stLclAddr.sin_family = AF_INET;
+ stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ stLclAddr.sin_port = htons(0);
+ llinfos << "attempting to connect on OS assigned port" << llendl;
+ nRet = bind(hSocket, (struct sockaddr*) &stLclAddr, sizeof(stLclAddr));
+ if (nRet < 0)
+ {
+ llwarns << "Failed to bind on an OS assigned port error: "
+ << nRet << llendl;
+ }
+ else
+ {
+ sockaddr_in socket_info;
+ socklen_t len = sizeof(sockaddr_in);
+ int err = getsockname(hSocket, (sockaddr*)&socket_info, &len);
+ llinfos << "Get socket returned: " << err << " length " << len << llendl;
+ nPort = ntohs(socket_info.sin_port);
+ llinfos << "Assigned port: " << nPort << llendl;
+
+ }
}
else
{
@@ -454,10 +492,25 @@ S32 start_net(S32& socket_out, int& nPort)
llinfos << "startNet - receive buffer size : " << rec_size << llendl;
llinfos << "startNet - send buffer size : " << snd_size << llendl;
+#if LL_LINUX
+ // Turn on recipient address tracking
+ {
+ int use_pktinfo = 1;
+ if( setsockopt( hSocket, SOL_IP, IP_PKTINFO, &use_pktinfo, sizeof(use_pktinfo) ) == -1 )
+ {
+ llwarns << "No IP_PKTINFO available" << llendl;
+ }
+ else
+ {
+ llinfos << "IP_PKKTINFO enabled" << llendl;
+ }
+ }
+#endif
+
// Setup a destination address
char achMCAddr[MAXADDRSTR] = "127.0.0.1"; /* Flawfinder: ignore */
stDstAddr.sin_family = AF_INET;
- stDstAddr.sin_addr.s_addr = inet_addr(achMCAddr);
+ stDstAddr.sin_addr.s_addr = ip_string_to_u32(achMCAddr);
stDstAddr.sin_port = htons(nPort);
socket_out = hSocket;
@@ -472,6 +525,52 @@ void end_net(S32& socket_out)
}
}
+#if LL_LINUX
+static int recvfrom_destip( int socket, void *buf, int len, struct sockaddr *from, socklen_t *fromlen, U32 *dstip )
+{
+ int size;
+ struct iovec iov[1];
+ char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
+ struct cmsghdr *cmsgptr;
+ struct msghdr msg = {0};
+
+ iov[0].iov_base = buf;
+ iov[0].iov_len = len;
+
+ memset( &msg, 0, sizeof msg );
+ msg.msg_name = from;
+ msg.msg_namelen = *fromlen;
+ msg.msg_iov = iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = &cmsg;
+ msg.msg_controllen = sizeof(cmsg);
+
+ size = recvmsg( socket, &msg, 0 );
+
+ if( size == -1 )
+ {
+ return -1;
+ }
+
+ for( cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR( &msg, cmsgptr ) )
+ {
+ if( cmsgptr->cmsg_level == SOL_IP && cmsgptr->cmsg_type == IP_PKTINFO )
+ {
+ in_pktinfo *pktinfo = (in_pktinfo *)CMSG_DATA(cmsgptr);
+ if( pktinfo )
+ {
+ // Two choices. routed and specified. ipi_addr is routed, ipi_spec_dst is
+ // routed. We should stay with specified until we go to multiple
+ // interfaces
+ *dstip = pktinfo->ipi_spec_dst.s_addr;
+ }
+ }
+ }
+
+ return size;
+}
+#endif
+
int receive_packet(int hSocket, char * receiveBuffer)
{
// Receives data asynchronously from the socket set by initNet().
@@ -481,7 +580,14 @@ int receive_packet(int hSocket, char * receiveBuffer)
int nRet;
socklen_t addr_size = sizeof(struct sockaddr_in);
- nRet = recvfrom(hSocket, receiveBuffer, NET_BUFFER_SIZE, 0, (struct sockaddr*)&stSrcAddr, &addr_size);
+ gsnReceivingIFAddr = INVALID_HOST_IP_ADDRESS;
+
+#if LL_LINUX
+ nRet = recvfrom_destip(hSocket, receiveBuffer, NET_BUFFER_SIZE, (struct sockaddr*)&stSrcAddr, &addr_size, &gsnReceivingIFAddr);
+#else
+ int recv_flags = 0;
+ nRet = recvfrom(hSocket, receiveBuffer, NET_BUFFER_SIZE, recv_flags, (struct sockaddr*)&stSrcAddr, &addr_size);
+#endif
if (nRet == -1)
{
@@ -489,6 +595,9 @@ int receive_packet(int hSocket, char * receiveBuffer)
return 0;
}
+ // Uncomment for testing if/when implementing for Mac or Windows:
+ // llinfos << "Received datagram to in addr " << u32_to_ip_string(get_receiving_interface_ip()) << llendl;
+
return nRet;
}