1
0

verchk.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author:
  6. ** Created:
  7. **/
  8. #include "Main.h"
  9. #if 0
  10. static int (__stdcall *p_recv)(SOCKET s, char FAR* buf, int len, int flags);
  11. static int g_nvck;
  12. static int recv_string(SOCKET s, char *str, int maxlen)
  13. {
  14. int p=0;
  15. do
  16. {
  17. int t=0;
  18. while (t!=1)
  19. {
  20. t=p_recv(s,str+p,1,0);
  21. if (t != 1)
  22. {
  23. str[0]=0;
  24. return -1;
  25. }
  26. if (str[p] == '\r') t=0;
  27. }
  28. } while (str[p] != '\n' && ++p < maxlen-1);
  29. str[p--]=0;
  30. while (str[p] == '\n' && p > 0)
  31. {
  32. str[p--]=0;
  33. }
  34. if (p < 0) p = 0;
  35. return p;
  36. }
  37. static DWORD WINAPI _Thread(void *p666)
  38. {
  39. char *rf_url = (char *)p666;
  40. HINSTANCE hws = LoadLibrary("wsock32.dll");
  41. SOCKET sock;
  42. char *proxy;
  43. char connect_host[MAX_PATH];
  44. unsigned short connect_port;
  45. int success=0;
  46. int (__stdcall *select)(int nfds,fd_set FAR * readfds,fd_set FAR * writefds,fd_set FAR * exceptfds,const struct timeval FAR * timeout);
  47. int (__stdcall *WSAGetLastError)(void);
  48. int (__stdcall *WSACleanup)(void);
  49. int (__stdcall *WSAStartup)(WORD wVersionRequested,LPWSADATA lpWSAData);
  50. int (__stdcall *closesocket)(SOCKET s);
  51. int (__stdcall *send)(SOCKET s,const char FAR *buf,int len,int flags);
  52. SOCKET (__stdcall *socket)(int af, int type,int protocol);
  53. int (__stdcall *connect)( SOCKET s, const struct sockaddr FAR *name, int namelen );
  54. unsigned long (__stdcall *inet_addr)(const char FAR *cp );
  55. struct hostent FAR * (__stdcall *gethostbyname)(const char FAR *name);
  56. int (__stdcall *ioctlsocket)(SOCKET s,long cmd,u_long FAR *argp);
  57. u_short (__stdcall *htons)(u_short hostshort);
  58. if (hws)
  59. {
  60. WSAGetLastError=(void*)GetProcAddress(hws,"WSAGetLastError");
  61. WSACleanup=(void*)GetProcAddress(hws,"WSACleanup");
  62. WSAStartup=(void*)GetProcAddress(hws,"WSAStartup");
  63. closesocket=(void*)GetProcAddress(hws,"closesocket");
  64. send=(void*)GetProcAddress(hws,"send");
  65. p_recv=(void*)GetProcAddress(hws,"recv");
  66. select=(void*)GetProcAddress(hws,"select");
  67. connect=(void*)GetProcAddress(hws,"connect");
  68. socket=(void*)GetProcAddress(hws,"socket");
  69. inet_addr=(void*)GetProcAddress(hws,"inet_addr");
  70. gethostbyname=(void*)GetProcAddress(hws,"gethostbyname");
  71. ioctlsocket=(void*)GetProcAddress(hws,"ioctlsocket");
  72. htons=(void*)GetProcAddress(hws,"htons");
  73. }
  74. if (!hws || !p_recv || !WSACleanup ||
  75. !WSAStartup || !closesocket || !send ||
  76. !connect || !socket || !inet_addr ||
  77. !gethostbyname || !ioctlsocket || !htons || !select || !WSAGetLastError)
  78. {
  79. if (hws) FreeLibrary(hws);
  80. return 0;
  81. }
  82. {
  83. WSADATA wsaData;
  84. if (WSAStartup(MAKEWORD(1, 1), &wsaData))
  85. {
  86. FreeLibrary(hws);
  87. return 0;
  88. }
  89. }
  90. // determine if proxy server used
  91. proxy=config_proxy;
  92. while (*proxy == ' ' || *proxy == '\t') proxy++;
  93. if (*proxy)
  94. {
  95. lstrcpyn(connect_host,proxy,sizeof(connect_host)/sizeof(*connect_host));
  96. }
  97. else
  98. {
  99. lstrcpyn(connect_host,"www.winamp.com",sizeof(connect_host)/sizeof(*connect_host));
  100. }
  101. connect_port=80;
  102. if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))==INVALID_SOCKET)
  103. {
  104. WSACleanup();
  105. FreeLibrary(hws);
  106. return 0;
  107. }
  108. {
  109. int t;
  110. struct sockaddr_in blah;
  111. struct hostent *he;
  112. memset((char *)&blah,0,sizeof(blah));
  113. blah.sin_family=AF_INET;
  114. blah.sin_addr.s_addr=inet_addr(connect_host);
  115. blah.sin_port=htons(connect_port);
  116. if (blah.sin_addr.s_addr == INADDR_NONE)
  117. {
  118. if ((he = gethostbyname(connect_host)) != NULL)
  119. memcpy((char *)&blah.sin_addr, he->h_addr, he->h_length);
  120. else if ((blah.sin_addr.s_addr = inet_addr(connect_host))==INADDR_NONE)
  121. {
  122. closesocket(sock);
  123. WSACleanup();
  124. FreeLibrary(hws);
  125. return 0;
  126. }
  127. }
  128. t=connect(sock,(struct sockaddr *)&blah,16);
  129. if (t==-1) goto cleanup;
  130. }
  131. {
  132. char send_string[2048];
  133. char request_file[1024];
  134. StringCchCopy(send_string,2048,rf_url);
  135. if (*proxy)
  136. {
  137. StringCchPrintf(request_file,1024, "http://www.winamp.com%s",rf_url);
  138. }
  139. else StringCchCopy(request_file,1024,rf_url);
  140. StringCchPrintf(send_string,2048,"GET %s HTTP/1.0\r\n"
  141. "User-Agent: Winamp/%s\r\n"
  142. "Host: www.winamp.com\r\n"
  143. "Accept: */*\r\n\r\n",request_file,app_version);
  144. // MessageBox(NULL,send_string,"SENDING:",0);
  145. send(sock,send_string,lstrlen(send_string),0);
  146. }
  147. { // get the standard HTTP 1.0 200 OK
  148. char buf[1024];
  149. int x = recv_string(sock,buf,sizeof(buf));
  150. // MessageBox(NULL,buf,buf,0);
  151. if (x < 0 || !strstr(buf,"OK")) goto cleanup;
  152. }
  153. if (g_nvck&2) stats_clear();
  154. while (1)
  155. {
  156. char buf[1024],*p;
  157. int x = recv_string(sock,buf,sizeof(buf));
  158. if (x < 0) goto cleanup;
  159. if (buf[0] == '\r' || !buf[0]) break;
  160. {
  161. p=buf;
  162. while (*p && *p != ':') p++;
  163. if (*p == ':')
  164. {
  165. *p++=0;
  166. while (*p == ' ' || *p == '\t') p++;
  167. }
  168. else p=NULL;
  169. }
  170. }
  171. {
  172. int is_upd=0;
  173. char obuf[32768]="";
  174. char buf[1024];
  175. int x = recv_string(sock,buf,sizeof(buf));
  176. if (x < 0 || buf[1] != '.') goto cleanup;
  177. if (buf[0] > app_version[0]) is_upd=1;
  178. else if (buf[0] == app_version[0])
  179. {
  180. int r2;
  181. char oldver[4];
  182. int oldlen;
  183. oldlen = lstrlen(app_version+2);
  184. if ( oldlen == 3 )
  185. {
  186. oldver[0]=*(app_version+2);
  187. oldver[1]=*(app_version+3);
  188. oldver[2]=*(app_version+4);
  189. }
  190. if ( oldlen == 2 )
  191. {
  192. oldver[0]=*(app_version+2);
  193. oldver[1]=*(app_version+3);
  194. oldver[2]='0';
  195. }
  196. if ( oldlen == 1 )
  197. {
  198. oldver[0]=*(app_version+2);
  199. oldver[1]='0';
  200. oldver[2]='0';
  201. }
  202. oldver[3]=0;
  203. r2=atoi(oldver);
  204. if (atoi(buf+2)>r2) is_upd=1;
  205. }
  206. if (!is_upd)
  207. {
  208. if ((g_nvck&1)&&config_newverchk) config_newverchk=getDay();
  209. goto cleanup;
  210. }
  211. while (lstrlen(obuf)<32768-sizeof(buf)*2)
  212. {
  213. x=recv_string(sock,buf,sizeof(buf));
  214. if (x < 0) break;
  215. StringCchCat(obuf,32768,buf);
  216. StringCchCat(obuf,32768,"\r\n");
  217. }
  218. if (g_nvck&1)
  219. {
  220. if (MessageBox(NULL,obuf,getString(IDS_WINAMP_UPDATE_MSG,NULL,0),MB_YESNO) == IDYES)
  221. {
  222. myOpenURL(NULL, L"http://www.winamp.com/getwinamp/");
  223. }
  224. else
  225. {
  226. if ((g_nvck&1)&&config_newverchk) config_newverchk=getDay();
  227. }
  228. }
  229. }
  230. cleanup:
  231. closesocket(sock);
  232. WSACleanup();
  233. FreeLibrary(hws);
  234. return 0;
  235. }
  236. void newversioncheck(void)
  237. {
  238. DWORD id;
  239. static char s[512];
  240. int stats[NUM_STATS]={0,};
  241. int x;
  242. g_nvck=(config_newverchk2?2:0)|(config_newverchk?1:0);
  243. if (g_nvck&3)
  244. {
  245. StringCchPrintf(s,512,"/update/latest-version.jhtml?v=%s",app_version);
  246. if (g_nvck&2)
  247. {
  248. stats_get(stats);
  249. StringCchCat(s,512,"&ID=");
  250. stats_getuidstr(s+lstrlen(s));
  251. for (x = 0; x < NUM_STATS; x ++)
  252. {
  253. StringCchPrintf(s+lstrlen(s),512-lstrlen(s),"&st%d=%d",x+1,stats[x]);
  254. }
  255. StringCchPrintf(s+lstrlen(s),512-lstrlen(s),"&regv=%d",g_regver);
  256. }
  257. // MessageBox(NULL,s,"metric",0);
  258. CloseHandle(CreateThread(NULL,0,_Thread,(LPVOID)s,0,&id));
  259. }
  260. }
  261. #endif