Clean up error handling in shitty G_GetVersionFromWebsite() function

git-svn-id: https://svn.eduke32.com/eduke32@7055 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-10-10 19:14:41 +00:00
parent e6c36442bd
commit 1d3f77b46b

View file

@ -33,8 +33,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
int32_t G_GetVersionFromWebsite(char *buffer) int32_t G_GetVersionFromWebsite(char *buffer)
{ {
int32_t wsainitialized = 0; static int32_t wsainitialized = 0;
int32_t bytes_sent, i=0, j=0; int32_t i=0, j=0, r=0;
struct sockaddr_in dest_addr; struct sockaddr_in dest_addr;
struct hostent *h; struct hostent *h;
char const *host = "www.eduke32.com"; char const *host = "www.eduke32.com";
@ -42,24 +42,21 @@ int32_t G_GetVersionFromWebsite(char *buffer)
char *tok; char *tok;
char tempbuf[2048],otherbuf[16],ver[16]; char tempbuf[2048],otherbuf[16],ver[16];
SOCKET mysock; SOCKET mysock;
WSADATA ws;
#ifdef _WIN32 #ifdef _WIN32
if (wsainitialized == 0) if (wsainitialized == 0)
{ {
WSADATA ws;
if (WSAStartup(0x101, &ws) == SOCKET_ERROR) if (WSAStartup(0x101, &ws) == SOCKET_ERROR)
{
// initprintf("update: Winsock error in G_GetVersionFromWebsite() (%d)\n",errno);
return 0; return 0;
}
wsainitialized = 1; wsainitialized = 1;
} }
#endif #endif
if ((h = gethostbyname(host)) == NULL) if ((h = gethostbyname(host)) == NULL)
{ {
// initprintf("update: gethostbyname() error in G_GetVersionFromWebsite() (%d)\n",h_errno); initprintf("Couldn't resolve %s!\n", host);
return 0; return 0;
} }
@ -69,50 +66,35 @@ int32_t G_GetVersionFromWebsite(char *buffer)
memset(&(dest_addr.sin_zero), '\0', 8); memset(&(dest_addr.sin_zero), '\0', 8);
mysock = socket(PF_INET, SOCK_STREAM, 0); mysock = socket(PF_INET, SOCK_STREAM, 0);
if (mysock == INVALID_SOCKET) if (mysock == INVALID_SOCKET)
{ {
// initprintf("update: socket() error in G_GetVersionFromWebsite() (%d)\n",errno); WSACleanup();
return 0; return 0;
} }
initprintf("Connecting to http://%s\n",host); initprintf("Connecting to http://%s\n",host);
if (connect(mysock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == SOCKET_ERROR) if (connect(mysock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == SOCKET_ERROR)
{ goto done;
// initprintf("update: connect() error in G_GetVersionFromWebsite() (%d)\n",errno);
closesocket(mysock);
return 0;
}
bytes_sent = send(mysock, req, strlen(req), 0); i = send(mysock, req, strlen(req), 0);
if (bytes_sent == SOCKET_ERROR)
{ if (i == SOCKET_ERROR)
// initprintf("update: send() error in G_GetVersionFromWebsite() (%d)\n",errno); goto done;
closesocket(mysock);
return 0;
}
// initprintf("sent %d bytes\n",bytes_sent);
i = recv(mysock, (char *)&tempbuf, sizeof(tempbuf), 0); i = recv(mysock, (char *)&tempbuf, sizeof(tempbuf), 0);
if (i < 0)
{
// initprintf("update: recv() returned %d\n", i);
closesocket(mysock);
return 0;
}
closesocket(mysock); if (i < 0)
goto done;
Bmemcpy(&otherbuf, &tempbuf, sizeof(otherbuf)); Bmemcpy(&otherbuf, &tempbuf, sizeof(otherbuf));
strtok(otherbuf, " "); strtok(otherbuf, " ");
tok = strtok(NULL," ");
if (tok == NULL) if ((tok = strtok(NULL, " ")) == NULL)
{ goto done;
// initprintf("update: strtok() produced no token\n");
return 0;
}
if (Batol(tok) == 200) if (Batol(tok) == 200)
{ {
@ -135,9 +117,15 @@ int32_t G_GetVersionFromWebsite(char *buffer)
if (j) if (j)
{ {
strcpy(buffer, ver); strcpy(buffer, ver);
return 1; r = 1;
goto done;
} }
} }
return 0;
done:
closesocket(mysock);
WSACleanup();
return r;
} }
#endif #endif