@@ -2102,66 +2102,53 @@ Check if machine is hosted on Cloud.
21022102BOOL hosting_check ()
21032103{
21042104 TCHAR msg[256 ] = _T (" Checking if Machine is hosted on Cloud" );
2105-
21062105 WSADATA wsaData;
2106+ SOCKET sock = INVALID_SOCKET;
2107+ addrinfo* result = nullptr ;
2108+ addrinfo hints;
2109+ BOOL retVal = FALSE ;
2110+ std::string request;
2111+ std::string response;
2112+ const int bufferSize = 512 ;
2113+ char buffer[bufferSize];
2114+ int bytesReceived = 0 ;
2115+
21072116 if (WSAStartup (MAKEWORD (2 , 2 ), &wsaData) != 0 )
21082117 {
2109- // print_results(FALSE, msg);
2110- return -1 ;
2118+ goto cleanup;
21112119 }
21122120
2113- // Create socket
2114- SOCKET sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
2121+ sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
21152122 if (sock == INVALID_SOCKET)
21162123 {
2117- WSACleanup ();
2118- // print_results(FALSE, msg);
2119- return -1 ;
2124+ goto cleanup;
21202125 }
21212126
2122- addrinfo* result = nullptr ;
2123- addrinfo hints = {} ;
2127+
2128+ memset (& hints, 0 , sizeof (hints)) ;
21242129 hints.ai_family = AF_INET;
21252130 hints.ai_socktype = SOCK_STREAM;
21262131 hints.ai_protocol = IPPROTO_TCP;
21272132
21282133 if (getaddrinfo (" ip-api.com" , " 80" , &hints, &result) != 0 )
21292134 {
2130- closesocket (sock);
2131- WSACleanup ();
2132- // print_results(FALSE, msg);
2133- return -1 ;
2135+ goto cleanup;
21342136 }
21352137
21362138 if (connect (sock, result->ai_addr , static_cast <int >(result->ai_addrlen )) == SOCKET_ERROR)
21372139 {
2138- freeaddrinfo (result);
2139- closesocket (sock);
2140- WSACleanup ();
2141- // print_results(FALSE, msg);
2142- return -1 ;
2140+ goto cleanup;
21432141 }
21442142
2145- freeaddrinfo (result);
2146-
2147- std::string request = " GET /json/?fields=hosting HTTP/1.1\r\n " ;
2143+ request = " GET /json/?fields=hosting HTTP/1.1\r\n " ;
21482144 request += " Host: ip-api.com\r\n " ;
21492145 request += " Connection: close\r\n\r\n " ;
21502146
21512147 if (send (sock, request.c_str (), static_cast <int >(request.length ()), 0 ) == SOCKET_ERROR)
21522148 {
2153- closesocket (sock);
2154- WSACleanup ();
2155- // print_results(FALSE, msg);
2156- return -1 ;
2149+ goto cleanup;
21572150 }
21582151
2159- // Receive response
2160- const int bufferSize = 512 ;
2161- char buffer[bufferSize];
2162- int bytesReceived;
2163- std::string response;
2164-
21652152 do
21662153 {
21672154 bytesReceived = recv (sock, buffer, bufferSize - 1 , 0 );
@@ -2174,26 +2161,18 @@ BOOL hosting_check()
21742161
21752162 if (bytesReceived == SOCKET_ERROR)
21762163 {
2177- closesocket (sock);
2178- WSACleanup ();
2179- // print_results(FALSE, msg);
2180- return -1 ;
2164+ goto cleanup;
21812165 }
21822166
2183- // Check if the response indicates hosting
21842167 if (response.find (" \" hosting\" :true" ) != std::string::npos)
21852168 {
2186- // print_results(TRUE, msg);
2187- closesocket (sock);
2188- WSACleanup ();
2189- return TRUE ;
2190- }
2191- else
2192- {
2193- closesocket (sock);
2194- WSACleanup ();
2195- return FALSE ;
2169+ retVal = TRUE ;
21962170 }
21972171
2172+ cleanup:
2173+ if (result) freeaddrinfo (result);
2174+ if (sock != INVALID_SOCKET) closesocket (sock);
2175+ WSACleanup ();
2176+ return retVal;
2177+ }
21982178
2199- }
0 commit comments