source: Daodan/MSVC/Daodan_Update.c@ 712

Last change on this file since 712 was 589, checked in by gumby, 13 years ago

stuff

File size: 5.4 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4
5
6#include <WinSock2.h>
7#include <Windows.h>
8
9#include "Daodan.h"
10#include "Daodan_Utility.h"
11#include "Flatline_Net.h"
12
13#pragma comment(lib, "wininet.lib")
14
15#define DAODAN_BUILD 3
16
17int file_exists(const char *fname)
18{
19 FILE *file;
20 if (file = fopen(fname, "r"))
21 {
22 fclose(file);
23 return 1;
24 }
25 return 0;
26}
27
28void DDr_StartupNetworkError()
29{
30 int errorno = WSAGetLastError();
31 char Message[1024];
32 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
33 FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, errorno,
34 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
35 (LPSTR) Message, 1024, NULL);
36
37 DDrStartupMessage(Message);
38#ifdef _DEBUG
39 MessageBox(0, Message, "", 0);
40#endif
41}
42
43char* DDr_HttpGet( char* host, char* path )
44{
45 #define chunkSize 8192
46 char* file = 0;
47 char tempRaw[chunkSize] = {0};
48 char* rawData = 0;
49 char* filePtr;
50 char* sizePtr;
51 int rawLen = 0;
52 int tempLen = 0;
53 char getString[255];
54 sockaddr_in UpdateAddr = {0};
55 int UpdateSocket;
56 hostent* host_info = 0;
57 struct timeval tv;
58 tv.tv_sec = 5;
59 tv.tv_usec = 0;
60 sprintf(getString, "GET %s HTTP/1.0\r\n"
61 "User-Agent: Daodan DLL Updater\r\n"
62 "Host: %s\r\n"
63 "\r\n"
64 , path, host );
65
66 host_info = gethostbyname(host);
67
68 if(!host_info)
69 {
70 DDr_StartupNetworkError();
71 return 0;
72 }
73
74 UpdateAddr.sin_port = htons(80);
75 UpdateAddr.sin_family = AF_INET;
76 UpdateAddr.sin_addr.S_un.S_addr = *(ULONG*)host_info->h_addr_list[0];
77
78 UpdateSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) ;
79
80 if(UpdateSocket <= 0)
81 {
82 DDrStartupMessage("Could not create socket");
83 DDr_StartupNetworkError();
84 return 0;
85 }
86
87 //Set timeout to five seconds.
88 setsockopt(UpdateSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv);
89
90 if (connect(UpdateSocket, (struct sockaddr *) &UpdateAddr, sizeof(sockaddr_in)) < 0)
91 {
92 closesocket( UpdateSocket );
93 DDr_StartupNetworkError();
94 return 0;
95 }
96
97
98 send( UpdateSocket, getString, strlen(getString), 0 );
99
100 while((tempLen = recv( UpdateSocket, tempRaw, chunkSize, 0 )) > 0)
101 {
102 rawLen += tempLen;
103 rawData = (char*)realloc( rawData, rawLen + 1 );
104 if(rawData == 0)
105 {
106 closesocket( UpdateSocket );
107 return 0;
108 }
109 memcpy( rawData + rawLen - tempLen, tempRaw, tempLen );
110 }
111
112 closesocket( UpdateSocket );
113
114 if(tempLen < 0)
115 {
116 return 0;
117 }
118
119 //check for complete header
120 filePtr = strstr( rawData, "\r\n\r\n" ) + 4;
121 //get the data size
122 sizePtr = strstr( rawData, "Content-Length:") + strlen("Content-Length:");
123 //check for correct status code
124 if(filePtr > 4 && sizePtr && !strncmp("HTTP/1.0 200 OK", rawData, 15))
125 {
126 int fileSize = atoi(sizePtr);
127 file = (char*)malloc( fileSize + sizeof(int) + 1);
128 if(file)
129 {
130 *(int *)file = fileSize;
131 memcpy( file + 4, filePtr, fileSize );
132 file[fileSize + sizeof(int)] = 0;
133 }
134 }
135 else
136 {
137 if(sizePtr && filePtr) *(filePtr + atoi(sizePtr)) = 0;
138#ifdef _DEBUG
139 MessageBox( 0, rawData, "", 0);
140#endif
141 DDrStartupMessage("HTML Error!");
142 DDrStartupMessage( rawData );
143 }
144 free( rawData );
145 return file;
146 #undef chunkSize
147}
148
149int DDr_CheckForUpdates(int argc, char* argv[])
150{
151 int version = 0;
152 char* fileGet;
153 char args[8191] = {0};
154 int i;
155 WSADATA wsaData;
156
157 for(i = 1; i < argc; i++)
158 {
159 if(!stricmp(argv[i], "-noupdate")) return 1;
160 strcat( args, " ");
161 strcat( args, argv[i] );
162
163 }
164
165 //Start network stuff
166
167 if (WSAStartup(MAKEWORD(2, 2), &wsaData))
168 {
169 DDrStartupMessage("WSA initalization failed");
170 return 0;
171 }
172 DDrStartupMessage("net started using WSA version %d.%d", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
173
174 fileGet = DDr_HttpGet( "gumby.oni2.net", "/daodan/version.txt" );
175
176 if(fileGet && *(int*)fileGet)
177 {
178 version = atoi(fileGet + sizeof(int));
179 }
180 free( fileGet );
181
182 if(version > DAODAN_BUILD)
183 {
184 FILE* dllFile;
185 char pName[8191] = {0};
186 int returnval = 0;
187 STARTUPINFO siStartupInfo;
188 PROCESS_INFORMATION piProcessInfo;
189 memset(&siStartupInfo, 0, sizeof(siStartupInfo));
190 memset(&piProcessInfo, 0, sizeof(piProcessInfo));
191 siStartupInfo.cb = sizeof(siStartupInfo);
192
193 DDrStartupMessage("Update for the Daodan DLL is avaliable...");
194 returnval = MessageBox( NULL, "An update for the Daodan DLL is avaiable! Would you like to download it?", "Daodan DLL Alert", MB_YESNO);
195 if( returnval != IDYES) return 1;
196 MessageBox( NULL, "Update downloading, please do not reopen Oni, it will open itself.\nPress OK to continue", "Daodan DLL Alert", 0);
197 fileGet = DDr_HttpGet( "gumby.oni2.net", "/daodan/binkw32.dll" );
198 if(!(fileGet && *(int*)fileGet))
199 {
200 free(fileGet);
201 return 0;
202 }
203
204 //Remove the old file
205 remove( "binkw32.dll.bak" );
206 rename( "binkw32.dll", "binkw32.dll.bak" );
207 //rename failed!
208 if(file_exists("binkw32.dll")) return;
209
210 //write the new file
211 dllFile = fopen( "binkw32.dll", "wb" );
212 if(dllFile == NULL )
213 {
214 rename( "binkw32.dll.bak", "binkw32.dll" );
215 return 0;
216 }
217
218 fwrite( fileGet + 4, *(int *)fileGet, 1, dllFile );
219 fclose( dllFile );
220
221 free(fileGet);
222 //Restart Oni!
223
224 GetModuleFileName(NULL, pName, 8191 );
225 //GetProcessImageFileName()
226 //GetProcessImageFileName( 0, pName, 8191 );
227 CreateProcess(pName, args, 0, 0, 0, 0, 0, 0,&siStartupInfo,&piProcessInfo);
228
229 exit(0);
230
231
232 }
233 return 1;
234}
Note: See TracBrowser for help on using the repository browser.