diff options
Diffstat (limited to 'indra/llmessage')
-rw-r--r-- | indra/llmessage/llpacketring.cpp | 23 | ||||
-rw-r--r-- | indra/llmessage/llsocks5.cpp | 295 | ||||
-rw-r--r-- | indra/llmessage/llsocks5.h | 184 | ||||
-rw-r--r-- | indra/llmessage/net.cpp | 61 | ||||
-rw-r--r-- | indra/llmessage/net.h | 4 |
5 files changed, 288 insertions, 279 deletions
diff --git a/indra/llmessage/llpacketring.cpp b/indra/llmessage/llpacketring.cpp index 0c8c5f763c..62aaca0672 100644 --- a/indra/llmessage/llpacketring.cpp +++ b/indra/llmessage/llpacketring.cpp @@ -28,22 +28,26 @@ #include "llpacketring.h" +#if LL_WINDOWS + #include <winsock2.h> +#else + #include <sys/socket.h> + #include <netinet/in.h> +#endif + // linden library includes #include "llerror.h" +#include "message.h" +#include "llsocks5.h" #include "lltimer.h" #include "timing.h" #include "llrand.h" #include "u64.h" -#include "llsocks5.h" -#include "message.h" -#if LL_WINDOWS - #include <winsock2.h> -#else - #include <sys/socket.h> - #include <netinet/in.h> -#endif + + + /////////////////////////////////////////////////////////// @@ -241,8 +245,7 @@ S32 LLPacketRing::receivePacket (S32 socket, char *datap) packet_size=0; } - proxywrap_t * header; - header = (proxywrap_t *)buffer; + proxywrap_t * header = (proxywrap_t *)buffer; mLastSender.setAddress(header->addr); mLastSender.setPort(ntohs(header->port)); } diff --git a/indra/llmessage/llsocks5.cpp b/indra/llmessage/llsocks5.cpp index 8a63287f22..7eac27d4bb 100644 --- a/indra/llmessage/llsocks5.cpp +++ b/indra/llmessage/llsocks5.cpp @@ -35,184 +35,185 @@ // Static class variable instances // We want this to be static to avoid excessive indirection on every -// incomming packet just to do a simple bool test. The getter for this +// incoming packet just to do a simple bool test. The getter for this // member is also static bool LLSocks::sUdpProxyEnabled; bool LLSocks::sHttpProxyEnabled; LLSocks::LLSocks() { - sUdpProxyEnabled = false; - sHttpProxyEnabled = false; - hProxyControlChannel = 0; - mProxyType = LLPROXY_SOCKS; + sUdpProxyEnabled = false; + sHttpProxyEnabled = false; + mProxyControlChannel = 0; + mProxyType = LLPROXY_SOCKS; } -// Perform a Socks5 authentication and UDP assioacation to the proxy -// specified by proxy, and assiocate UDP port message_port +// Perform a Socks5 authentication and UDP association to the proxy +// specified by proxy, and associate UDP port message_port int LLSocks::proxyHandshake(LLHost proxy, U32 message_port) { - int result; - - /* Socks 5 Auth request */ - socks_auth_request_t socks_auth_request; - socks_auth_response_t socks_auth_response; - - socks_auth_request.version = SOCKS_VERSION; // Socks version 5 - socks_auth_request.num_methods = 1; // Sending 1 method - socks_auth_request.methods = mAuthMethodSelected; // send only the selected metho - - result = tcp_handshake(hProxyControlChannel, (char*)&socks_auth_request, sizeof(socks_auth_request_t), (char*)&socks_auth_response, sizeof(socks_auth_response_t)); - if (result != 0) - { - llwarns << "Socks authentication request failed, error on TCP control channel : " << result << llendl; - stopProxy(); - return SOCKS_CONNECT_ERROR; - } - - if (socks_auth_response.method == AUTH_NOT_ACCEPTABLE) - { - llwarns << "Socks5 server refused all our authentication methods" << llendl; - stopProxy(); - return SOCKS_NOT_ACCEPTABLE; - } - - // SOCKS5 USERNAME/PASSWORD authentication - if (socks_auth_response.method == METHOD_PASSWORD) - { - // The server has requested a username/password combination - U32 request_size = mSocksUsername.size() + mSocksPassword.size() + 3; - char * password_auth = (char *)malloc(request_size); - password_auth[0] = 0x01; - password_auth[1] = mSocksUsername.size(); - memcpy(&password_auth[2],mSocksUsername.c_str(), mSocksUsername.size()); - password_auth[mSocksUsername.size()+2] = mSocksPassword.size(); - memcpy(&password_auth[mSocksUsername.size()+3], mSocksPassword.c_str(), mSocksPassword.size()); - - authmethod_password_reply_t password_reply; - - result = tcp_handshake(hProxyControlChannel, password_auth, request_size, (char*)&password_reply, sizeof(authmethod_password_reply_t)); - free (password_auth); - - if (result != 0) - { - llwarns << "Socks authentication failed, error on TCP control channel : " << result << llendl; - stopProxy(); - return SOCKS_CONNECT_ERROR; - } - - if (password_reply.status != AUTH_SUCCESS) - { - llwarns << "Socks authentication failed" << llendl; - stopProxy(); - return SOCKS_AUTH_FAIL; - } - } - - /* SOCKS5 connect request */ - - socks_command_request_t connect_request; - socks_command_response_t connect_reply; - - connect_request.version = SOCKS_VERSION; //Socks V5 - connect_request.command = COMMAND_UDP_ASSOCIATE; // Associate UDP - connect_request.flag = FIELD_RESERVED; - connect_request.atype = ADDRESS_IPV4; - connect_request.address = 0; // 0.0.0.0 We are not fussy about address - // UDP is promiscious receive for our protocol - connect_request.port = 0; // Port must be 0 if you ever want to connect via NAT and your router does port rewrite for you - - result = tcp_handshake(hProxyControlChannel, (char*)&connect_request, sizeof(socks_command_request_t), (char*)&connect_reply, sizeof(socks_command_response_t)); - if (result != 0) - { - llwarns << "Socks connect request failed, error on TCP control channel : " << result << llendl; - stopProxy(); - return SOCKS_CONNECT_ERROR; - } - - if (connect_reply.reply != REPLY_REQUEST_GRANTED) - { - //Something went wrong - llwarns << "Connection to SOCKS5 server failed, UDP forward request not granted" << llendl; - stopProxy(); - return SOCKS_UDP_FWD_NOT_GRANTED; - } - - mUDPProxy.setPort(ntohs(connect_reply.port)); // reply port is in network byte order - mUDPProxy.setAddress(proxy.getAddress()); - // All good now we have been given the UDP port to send requests that need forwarding. - llinfos << "Socks 5 UDP proxy connected on " << mUDPProxy << llendl; - return SOCKS_OK; + int result; + + /* Socks 5 Auth request */ + socks_auth_request_t socks_auth_request; + socks_auth_response_t socks_auth_response; + + socks_auth_request.version = SOCKS_VERSION; // Socks version 5 + socks_auth_request.num_methods = 1; // Sending 1 method + socks_auth_request.methods = mAuthMethodSelected; // send only the selected method + + result = tcp_handshake(mProxyControlChannel, (char*)&socks_auth_request, sizeof(socks_auth_request_t), (char*)&socks_auth_response, sizeof(socks_auth_response_t)); + if (result != 0) + { + llwarns << "Socks authentication request failed, error on TCP control channel : " << result << llendl; + stopProxy(); + return SOCKS_CONNECT_ERROR; + } + + if (socks_auth_response.method == AUTH_NOT_ACCEPTABLE) + { + llwarns << "Socks5 server refused all our authentication methods" << llendl; + stopProxy(); + return SOCKS_NOT_ACCEPTABLE; + } + + // SOCKS5 USERNAME/PASSWORD authentication + if (socks_auth_response.method == METHOD_PASSWORD) + { + // The server has requested a username/password combination + U32 request_size = mSocksUsername.size() + mSocksPassword.size() + 3; + // char * password_auth = (char *)malloc(request_size); + char * password_auth = new char[request_size]; + password_auth[0] = 0x01; + password_auth[1] = mSocksUsername.size(); + memcpy(&password_auth[2], mSocksUsername.c_str(), mSocksUsername.size()); + password_auth[mSocksUsername.size()+2] = mSocksPassword.size(); + memcpy(&password_auth[mSocksUsername.size()+3], mSocksPassword.c_str(), mSocksPassword.size()); + + authmethod_password_reply_t password_reply; + + result = tcp_handshake(mProxyControlChannel, password_auth, request_size, (char*)&password_reply, sizeof(authmethod_password_reply_t)); + delete[] password_auth; + + if (result != 0) + { + llwarns << "Socks authentication failed, error on TCP control channel : " << result << llendl; + stopProxy(); + return SOCKS_CONNECT_ERROR; + } + + if (password_reply.status != AUTH_SUCCESS) + { + llwarns << "Socks authentication failed" << llendl; + stopProxy(); + return SOCKS_AUTH_FAIL; + } + } + + /* SOCKS5 connect request */ + + socks_command_request_t connect_request; + socks_command_response_t connect_reply; + + connect_request.version = SOCKS_VERSION; //Socks V5 + connect_request.command = COMMAND_UDP_ASSOCIATE; // Associate UDP + connect_request.flag = FIELD_RESERVED; + connect_request.atype = ADDRESS_IPV4; + connect_request.address = 0; // 0.0.0.0 We are not fussy about address + // UDP is promiscuous receive for our protocol + connect_request.port = 0; // Port must be 0 if you ever want to connect via NAT and your router does port rewrite for you + + result = tcp_handshake(mProxyControlChannel, (char*)&connect_request, sizeof(socks_command_request_t), (char*)&connect_reply, sizeof(socks_command_response_t)); + if (result != 0) + { + llwarns << "Socks connect request failed, error on TCP control channel : " << result << llendl; + stopProxy(); + return SOCKS_CONNECT_ERROR; + } + + if (connect_reply.reply != REPLY_REQUEST_GRANTED) + { + //Something went wrong + llwarns << "Connection to SOCKS5 server failed, UDP forward request not granted" << llendl; + stopProxy(); + return SOCKS_UDP_FWD_NOT_GRANTED; + } + + mUDPProxy.setPort(ntohs(connect_reply.port)); // reply port is in network byte order + mUDPProxy.setAddress(proxy.getAddress()); + // All good now we have been given the UDP port to send requests that need forwarding. + llinfos << "Socks 5 UDP proxy connected on " << mUDPProxy << llendl; + return SOCKS_OK; } int LLSocks::startProxy(LLHost proxy, U32 message_port) { - int status; - - mTCPProxy = proxy; - - if (hProxyControlChannel) - { - tcp_close_channel(hProxyControlChannel); - hProxyControlChannel=0; - } - - hProxyControlChannel = tcp_open_channel(proxy); - if (hProxyControlChannel == -1) - { - return SOCKS_HOST_CONNECT_FAILED; - } - - status = proxyHandshake(proxy, message_port); - if (status == SOCKS_OK) - { - sUdpProxyEnabled=true; - } - return status; + int status; + + mTCPProxy = proxy; + + if (mProxyControlChannel) + { + tcp_close_channel(mProxyControlChannel); + mProxyControlChannel = 0; + } + + mProxyControlChannel = tcp_open_channel(proxy); + if (mProxyControlChannel == -1) + { + return SOCKS_HOST_CONNECT_FAILED; + } + + status = proxyHandshake(proxy, message_port); + if (status == SOCKS_OK) + { + sUdpProxyEnabled = true; + } + return status; } int LLSocks::startProxy(std::string host, U32 port) { - mTCPProxy.setHostByName(host); - mTCPProxy.setPort(port); - return startProxy(mTCPProxy, (U32)gMessageSystem->mPort); + mTCPProxy.setHostByName(host); + mTCPProxy.setPort(port); + return startProxy(mTCPProxy, (U32)gMessageSystem->mPort); } void LLSocks::stopProxy() { - sUdpProxyEnabled = false; - - // If the Socks proxy is requested to stop and we are using that for http as well - // then we must shut down any http proxy operations. But it is allowable if web - // proxy is being used to continue proxying http. - - if(LLPROXY_SOCKS == mProxyType) - { - sHttpProxyEnabled = false; - } - - if (hProxyControlChannel) - { - tcp_close_channel(hProxyControlChannel); - hProxyControlChannel=0; - } + sUdpProxyEnabled = false; + + // If the Socks proxy is requested to stop and we are using that for http as well + // then we must shut down any http proxy operations. But it is allowable if web + // proxy is being used to continue proxying http. + + if(LLPROXY_SOCKS == mProxyType) + { + sHttpProxyEnabled = false; + } + + if (mProxyControlChannel) + { + tcp_close_channel(mProxyControlChannel); + mProxyControlChannel = 0; + } } void LLSocks::setAuthNone() { - mAuthMethodSelected = METHOD_NOAUTH; + mAuthMethodSelected = METHOD_NOAUTH; } void LLSocks::setAuthPassword(std::string username, std::string password) { - mAuthMethodSelected = METHOD_PASSWORD; - mSocksUsername = username; - mSocksPassword = password; + mAuthMethodSelected = METHOD_PASSWORD; + mSocksUsername = username; + mSocksPassword = password; } -void LLSocks::EnableHttpProxy(LLHost httpHost, LLHttpProxyType type) +void LLSocks::enableHttpProxy(LLHost httpHost, LLHttpProxyType type) { - sHttpProxyEnabled = true; - mHTTPProxy = httpHost; - mProxyType = type; + sHttpProxyEnabled = true; + mHTTPProxy = httpHost; + mProxyType = type; } diff --git a/indra/llmessage/llsocks5.h b/indra/llmessage/llsocks5.h index a78acb8b23..171a933d32 100644 --- a/indra/llmessage/llsocks5.h +++ b/indra/llmessage/llsocks5.h @@ -35,12 +35,12 @@ // Error codes returned from the StartProxy method #define SOCKS_OK 0 -#define SOCKS_CONNECT_ERROR -1 -#define SOCKS_NOT_PERMITTED -2 -#define SOCKS_NOT_ACCEPTABLE -3 -#define SOCKS_AUTH_FAIL -4 -#define SOCKS_UDP_FWD_NOT_GRANTED -5 -#define SOCKS_HOST_CONNECT_FAILED -6 +#define SOCKS_CONNECT_ERROR (-1) +#define SOCKS_NOT_PERMITTED (-2) +#define SOCKS_NOT_ACCEPTABLE (-3) +#define SOCKS_AUTH_FAIL (-4) +#define SOCKS_UDP_FWD_NOT_GRANTED (-5) +#define SOCKS_HOST_CONNECT_FAILED (-6) #ifndef MAXHOSTNAMELEN #define MAXHOSTNAMELEN (255 + 1) /* socks5: 255, +1 for len. */ @@ -56,8 +56,8 @@ // Lets just use our own ipv4 struct rather than dragging in system // specific headers union ipv4_address_t { - unsigned char octects[4]; - U32 addr32; + unsigned char octets[4]; + U32 addr32; }; // Socks 5 control channel commands @@ -86,53 +86,53 @@ union ipv4_address_t { // Socks5 command packet struct socks_command_request_t { - unsigned char version; - unsigned char command; - unsigned char flag; - unsigned char atype; - U32 address; - U16 port; + unsigned char version; + unsigned char command; + unsigned char flag; + unsigned char atype; + U32 address; + U16 port; }; // Standard socks5 reply packet struct socks_command_response_t { - unsigned char version; - unsigned char reply; - unsigned char flag; - unsigned char atype; - unsigned char add_bytes[4]; - U16 port; + unsigned char version; + unsigned char reply; + unsigned char flag; + unsigned char atype; + unsigned char add_bytes[4]; + U16 port; }; -#define AUTH_NOT_ACCEPTABLE 0xFF // reply if prefered methods are not avaiable -#define AUTH_SUCCESS 0x00 // reply if authentication successfull +#define AUTH_NOT_ACCEPTABLE 0xFF // reply if preferred methods are not available +#define AUTH_SUCCESS 0x00 // reply if authentication successful // socks 5 authentication request, stating which methods the client supports struct socks_auth_request_t { - unsigned char version; - unsigned char num_methods; - unsigned char methods; // We are only using a single method currently + unsigned char version; + unsigned char num_methods; + unsigned char methods; // We are only using a single method currently }; // socks 5 authentication response packet, stating server prefered method struct socks_auth_response_t { - unsigned char version; - unsigned char method; + unsigned char version; + unsigned char method; }; // socks 5 password reply packet struct authmethod_password_reply_t { - unsigned char version; - unsigned char status; + unsigned char version; + unsigned char status; }; // socks 5 UDP packet header struct proxywrap_t { - U16 rsv; - U8 frag; - U8 atype; - U32 addr; - U16 port; + U16 rsv; + U8 frag; + U8 atype; + U32 addr; + U16 port; }; #pragma pack(pop) /* restore original alignment from stack */ @@ -141,97 +141,97 @@ struct proxywrap_t { // Currently selected http proxy type enum LLHttpProxyType { - LLPROXY_SOCKS = 0, - LLPROXY_HTTP = 1 + LLPROXY_SOCKS = 0, + LLPROXY_HTTP = 1 }; // Auth types enum LLSocks5AuthType { - METHOD_NOAUTH = 0x00, // Client supports no auth - METHOD_GSSAPI = 0x01, // Client supports GSSAPI (Not currently supported) - METHOD_PASSWORD = 0x02 // Client supports username/password + METHOD_NOAUTH = 0x00, // Client supports no auth + METHOD_GSSAPI = 0x01, // Client supports GSSAPI (Not currently supported) + METHOD_PASSWORD = 0x02 // Client supports username/password }; class LLSocks: public LLSingleton<LLSocks> { public: - LLSocks(); + LLSocks(); - // Start a connection to the socks 5 proxy - int startProxy(std::string host,U32 port); - int startProxy(LLHost proxy,U32 messagePort); + // Start a connection to the socks 5 proxy + int startProxy(std::string host,U32 port); + int startProxy(LLHost proxy,U32 messagePort); - // Disconnect and clean up any connection to the socks 5 proxy - void stopProxy(); + // Disconnect and clean up any connection to the socks 5 proxy + void stopProxy(); - // Set up to use Password auth when connecting to the socks proxy - void setAuthPassword(std::string username,std::string password); + // Set up to use Password auth when connecting to the socks proxy + void setAuthPassword(std::string username,std::string password); - // Set up to use No Auth when connecting to the socks proxy; - void setAuthNone(); + // Set up to use No Auth when connecting to the socks proxy + void setAuthNone(); - // get the currently selected auth method - LLSocks5AuthType getSelectedAuthMethod() { return mAuthMethodSelected; }; + // get the currently selected auth method + LLSocks5AuthType getSelectedAuthMethod() const { return mAuthMethodSelected; } - // static check for enabled status for UDP packets - static bool isEnabled(){return sUdpProxyEnabled;}; + // static check for enabled status for UDP packets + static bool isEnabled() { return sUdpProxyEnabled; } - // static check for enabled status for http packets - static bool isHttpProxyEnabled(){return sHttpProxyEnabled;}; + // static check for enabled status for http packets + static bool isHttpProxyEnabled() { return sHttpProxyEnabled; } - // Proxy http packets via httpHost, which can be a Socks5 or a http proxy - // as specified in type - void EnableHttpProxy(LLHost httpHost,LLHttpProxyType type); + // Proxy http packets via httpHost, which can be a Socks5 or a http proxy + // as specified in type + void enableHttpProxy(LLHost httpHost, LLHttpProxyType type); - // Stop proxying http packets - void DisableHttpProxy() {sHttpProxyEnabled = false;}; + // Stop proxying http packets + void disableHttpProxy() { sHttpProxyEnabled = false; }; - // get the UDP proxy address and port - LLHost getUDPProxy(){return mUDPProxy;}; + // Get the UDP proxy address and port + LLHost getUDPProxy() const { return mUDPProxy; } - // get the socks 5 TCP control channel address and port - LLHost getTCPProxy(){return mTCPProxy;}; + // Get the socks 5 TCP control channel address and port + LLHost getTCPProxy() const { return mTCPProxy; } - //get the http proxy address and port - LLHost getHTTPProxy(){return mHTTPProxy;}; + // Get the http proxy address and port + LLHost getHTTPProxy() const { return mHTTPProxy; } - // get the currently selected http proxy type - LLHttpProxyType getHttpProxyType(){return mProxyType;}; + // Get the currently selected http proxy type + LLHttpProxyType getHttpProxyType() const { return mProxyType; } - //Get the username password in a curl compatible format - std::string getProxyUserPwd(){ return (mSocksUsername + ":" + mSocksPassword);}; + // Get the username password in a curl compatible format + std::string getProxyUserPwd() const { return (mSocksUsername + ":" + mSocksPassword); } private: - // Open a communication channel to the socks5 proxy proxy, at port messagePort - int proxyHandshake(LLHost proxy,U32 messagePort); + // Open a communication channel to the socks5 proxy proxy, at port messagePort + int proxyHandshake(LLHost proxy,U32 messagePort); - // socket handle to proxy tcp control channel - S32 hProxyControlChannel; + // socket handle to proxy tcp control channel + S32 mProxyControlChannel; - // is the UDP proxy enabled - static bool sUdpProxyEnabled; - // is the http proxy enabled - static bool sHttpProxyEnabled; + // is the UDP proxy enabled? + static bool sUdpProxyEnabled; + // is the http proxy enabled? + static bool sHttpProxyEnabled; - // currently selected http proxy type - LLHttpProxyType mProxyType; + // currently selected http proxy type + LLHttpProxyType mProxyType; - // UDP proxy address and port - LLHost mUDPProxy; - // TCP Proxy control channel address and port - LLHost mTCPProxy; - // HTTP proxy address and port - LLHost mHTTPProxy; + // UDP proxy address and port + LLHost mUDPProxy; + // TCP Proxy control channel address and port + LLHost mTCPProxy; + // HTTP proxy address and port + LLHost mHTTPProxy; - // socks 5 auth method selected - LLSocks5AuthType mAuthMethodSelected; + // socks 5 auth method selected + LLSocks5AuthType mAuthMethodSelected; - // socks 5 username - std::string mSocksUsername; - // socks 5 password - std::string mSocksPassword; + // socks 5 username + std::string mSocksUsername; + // socks 5 password + std::string mSocksPassword; }; #endif 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) { diff --git a/indra/llmessage/net.h b/indra/llmessage/net.h index d93ed20c98..047e8ce646 100644 --- a/indra/llmessage/net.h +++ b/indra/llmessage/net.h @@ -46,10 +46,10 @@ S32 receive_packet(int hSocket, char * receiveBuffer); BOOL send_packet(int hSocket, const char *sendBuffer, int size, U32 recipient, int nPort); // Returns TRUE on success. //void get_sender(char * tmp); -LLHost get_sender(); +LLHost get_sender(); U32 get_sender_port(); U32 get_sender_ip(void); -LLHost get_receiving_interface(); +LLHost get_receiving_interface(); U32 get_receiving_interface_ip(void); // Some helpful tcp functions added for the socks 5 proxy support |