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.cpp61
1 files changed, 33 insertions, 28 deletions
diff --git a/indra/llmessage/net.cpp b/indra/llmessage/net.cpp
index ab5c1950c6..a51d80ff48 100644
--- a/indra/llmessage/net.cpp
+++ b/indra/llmessage/net.cpp
@@ -175,7 +175,7 @@ U32 ip_string_to_u32(const char* ip_string)
// use wildcard addresses. -Ambroff
U32 ip = inet_addr(ip_string);
if (ip == INADDR_NONE
- && strncmp(ip_string, BROADCAST_ADDRESS_STRING, MAXADDRSTR) != 0)
+ && 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;
@@ -220,9 +220,10 @@ S32 tcp_open_channel(LLHost host)
S32 handle;
handle = socket(AF_INET, SOCK_STREAM, 0);
- if (!handle)
+ if (INVALID_SOCKET == handle)
{
- llwarns << "Error opening TCP control socket, socket() returned " << handle << llendl;
+ llwarns << "Error opening TCP control socket, socket() returned "
+ << WSAGetLastError() << ", " << DecodeError(WSAGetLastError()) << llendl;
return -1;
}
@@ -232,15 +233,15 @@ S32 tcp_open_channel(LLHost host)
address.sin_addr.s_addr = host.getAddress();
// Non blocking
- WSAEVENT hEvent=WSACreateEvent();
+ WSAEVENT hEvent = WSACreateEvent();
WSAEventSelect(handle, hEvent, FD_CONNECT) ;
connect(handle, (struct sockaddr*)&address, sizeof(address)) ;
- // Wait fot 5 seconds, if we can't get a TCP channel open in this
+ // Wait for 5 seconds, if we can't get a TCP channel open in this
// time frame then there is something badly wrong.
- WaitForSingleObject(hEvent, 1000*5); // 5 seconds time out
+ WaitForSingleObject(hEvent, 1000 * 5); // 5 seconds time out
WSANETWORKEVENTS netevents;
- WSAEnumNetworkEvents(handle,hEvent,&netevents);
+ WSAEnumNetworkEvents(handle, hEvent, &netevents);
// Check the async event status to see if we connected
if ((netevents.lNetworkEvents & FD_CONNECT) == FD_CONNECT)
@@ -249,6 +250,7 @@ S32 tcp_open_channel(LLHost host)
{
llwarns << "Unable to open TCP channel, WSA returned an error code of " << netevents.iErrorCode[FD_CONNECT_BIT] << llendl;
WSACloseEvent(hEvent);
+ tcp_close_channel(handle);
return -1;
}
@@ -264,6 +266,7 @@ S32 tcp_open_channel(LLHost host)
}
llwarns << "Unable to open TCP channel, Timeout is the host up?" << netevents.iErrorCode[FD_CONNECT_BIT] << llendl;
+ tcp_close_channel(handle);
return -1;
}
@@ -277,7 +280,7 @@ void tcp_close_channel(S32 handle)
S32 start_net(S32& socket_out, int& nPort)
{
// Create socket, make non-blocking
- // Init WinSock
+ // Init WinSock
int nRet;
int hSocket;
@@ -286,7 +289,7 @@ S32 start_net(S32& socket_out, int& nPort)
int buff_size = 4;
// Initialize windows specific stuff
- if(WSAStartup(0x0202, &stWSAData))
+ if (WSAStartup(0x0202, &stWSAData))
{
S32 err = WSAGetLastError();
WSACleanup();
@@ -295,8 +298,8 @@ S32 start_net(S32& socket_out, int& nPort)
}
// Get a datagram socket
- hSocket = (int)socket(AF_INET, SOCK_DGRAM, 0);
- if (hSocket == INVALID_SOCKET)
+ hSocket = (int)socket(AF_INET, SOCK_DGRAM, 0);
+ if (hSocket == INVALID_SOCKET)
{
S32 err = WSAGetLastError();
WSACleanup();
@@ -389,7 +392,7 @@ S32 start_net(S32& socket_out, int& nPort)
// Setup a destination address
stDstAddr.sin_family = AF_INET;
stDstAddr.sin_addr.s_addr = INVALID_HOST_IP_ADDRESS;
- stDstAddr.sin_port = htons(nPort);
+ stDstAddr.sin_port = htons(nPort);
socket_out = hSocket;
return 0;
@@ -492,9 +495,9 @@ S32 tcp_open_channel(LLHost host)
{
S32 handle;
handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (!handle)
+ if (-1 == handle)
{
- llwarns << "Error opening TCP control socket, socket() returned " << handle << llendl;
+ llwarns << "Error opening TCP control socket, socket() returned " << handle << "error code: " << errno << llendl;
return -1;
}
@@ -511,6 +514,7 @@ S32 tcp_open_channel(LLHost host)
if (error && (errno != EINPROGRESS))
{
llwarns << "Unable to open TCP channel, error code: " << errno << llendl;
+ tcp_close_channel(handle);
return -1;
}
@@ -521,12 +525,13 @@ S32 tcp_open_channel(LLHost host)
FD_ZERO(&fds);
FD_SET(handle, &fds);
- // See if we have connectde or time out after 5 seconds
- U32 rc = select(sizeof(fds)*8, NULL, &fds, NULL, &timeout);
+ // See if we have connected or time out after 5 seconds
+ S32 rc = select(sizeof(fds)*8, NULL, &fds, NULL, &timeout);
if (rc != 1) // we require exactly one descriptor to be set
{
llwarns << "Unable to open TCP channel" << llendl;
+ tcp_close_channel(handle);
return -1;
}
@@ -549,10 +554,10 @@ S32 start_net(S32& socket_out, int& nPort)
int rec_size = RECEIVE_BUFFER_SIZE;
socklen_t buff_size = 4;
-
+
// Create socket
- hSocket = socket(AF_INET, SOCK_DGRAM, 0);
- if (hSocket < 0)
+ hSocket = socket(AF_INET, SOCK_DGRAM, 0);
+ if (hSocket < 0)
{
llwarns << "socket() failed" << llendl;
return 1;
@@ -585,7 +590,7 @@ S32 start_net(S32& socket_out, int& nPort)
}
else
{
- // Name the socket (assign the local port number to receive on)
+ // Name the socket (assign the local port number to receive on)
stLclAddr.sin_family = AF_INET;
stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
stLclAddr.sin_port = htons(nPort);
@@ -630,7 +635,7 @@ S32 start_net(S32& socket_out, int& nPort)
nPort = attempt_port;
}
// Set socket to be non-blocking
- fcntl(hSocket, F_SETFL, O_NONBLOCK);
+ fcntl(hSocket, F_SETFL, O_NONBLOCK);
// set a large receive buffer
nRet = setsockopt(hSocket, SOL_SOCKET, SO_RCVBUF, (char *)&rec_size, buff_size);
if (nRet)
@@ -666,8 +671,8 @@ S32 start_net(S32& socket_out, int& nPort)
// Setup a destination address
char achMCAddr[MAXADDRSTR] = "127.0.0.1"; /* Flawfinder: ignore */
stDstAddr.sin_family = AF_INET;
- stDstAddr.sin_addr.s_addr = ip_string_to_u32(achMCAddr);
- stDstAddr.sin_port = htons(nPort);
+ stDstAddr.sin_addr.s_addr = ip_string_to_u32(achMCAddr);
+ stDstAddr.sin_port = htons(nPort);
socket_out = hSocket;
return 0;
@@ -693,7 +698,7 @@ static int recvfrom_destip( int socket, void *buf, int len, struct sockaddr *fro
iov[0].iov_base = buf;
iov[0].iov_len = len;
- memset( &msg, 0, sizeof msg );
+ memset(&msg, 0, sizeof msg);
msg.msg_name = from;
msg.msg_namelen = *fromlen;
msg.msg_iov = iov;
@@ -701,14 +706,14 @@ static int recvfrom_destip( int socket, void *buf, int len, struct sockaddr *fro
msg.msg_control = &cmsg;
msg.msg_controllen = sizeof(cmsg);
- size = recvmsg( socket, &msg, 0 );
+ size = recvmsg(socket, &msg, 0);
- if( size == -1 )
+ if (size == -1)
{
return -1;
}
- for( cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR( &msg, cmsgptr ) )
+ for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR( &msg, cmsgptr))
{
if( cmsgptr->cmsg_level == SOL_IP && cmsgptr->cmsg_type == IP_PKTINFO )
{
@@ -806,7 +811,7 @@ BOOL send_packet(int hSocket, const char * sendBuffer, int size, U32 recipient,
}
}
}
- while ( resend && send_attempts < 3);
+ while (resend && send_attempts < 3);
if (send_attempts >= 3)
{