Italiano
HOME | PROGETTI | CLASSI C++ | Sorgenti c\c++ | Galleria | Guestbook | Server BW | Contattami | Login

<< MV Smart Downloader >>

Torna indietro



Versione: 3.03.01 Win32

  1. #define VERSIONE_PROGRAMMA "MV Smart Downloader v 3.03.01 by mamo139"
  2.  
  3. #define console_mode
  4. //#define console_mode_grafica
  5.  
  6. #define _CRT_SECURE_NO_WARNINGS
  7.  
  8. #include <gtk/gtk.h>
  9.  
  10. #include <stdio.h>
  11. #include <winsock2.h>
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include <math.h>
  17.  
  18. #pragma comment(lib,"wsock32.lib")
  19.  
  20. #pragma comment(lib,"atk-1.0.lib")
  21. #pragma comment(lib,"cairo.lib")
  22. #pragma comment(lib,"gio-2.0.lib")
  23. #pragma comment(lib,"glib-2.0.lib")
  24. #pragma comment(lib,"gmodule-2.0.lib")
  25. #pragma comment(lib,"gobject-2.0.lib")
  26. #pragma comment(lib,"gthread-2.0.lib")
  27. #pragma comment(lib,"gailutil.lib")
  28. #pragma comment(lib,"gdk_pixbuf-2.0.lib")
  29. #pragma comment(lib,"gdk-win32-2.0.lib")
  30. #pragma comment(lib,"gtk-win32-2.0.lib")
  31. #pragma comment(lib,"pango-1.0.lib")
  32. #pragma comment(lib,"pangocairo-1.0.lib")
  33. #pragma comment(lib,"pangoft2-1.0.lib")
  34. #pragma comment(lib,"pangowin32-1.0.lib")
  35.  
  36. const int processi_num = 2;
  37. const int download_buffer_size = 1024;
  38.  
  39. const int socket_timeout = 10000; //timeout per la conessione, in millisecondi direi
  40. const int backup_time = 0; //ogni quanto effettuare il backup in millisecondi
  41. const int speed_info_time = 5; //ogni quanto viene comunicata la velocità
  42. const int possibilit_speed = 2; //moltiplicato per speed_info_time da il numero di secondi prima della disconnessione per lentezza
  43.  
  44. const int limite_speed = 1; //disconnette proxy piu lenti, velocità in kb/s
  45.  
  46. char *proxylist_file = "proxylist.txt";
  47.  
  48. long proxy_scarica(char *fileremoto, char *host,char *video_buffer, char *proxy_ip, int timeout_sec, long video_time, long video_lunghezza, float *proxy_speed, long *video_current_point, bool *reset);
  49. bool proxy_scarica_su_stringa(char *fileremoto, char *host,char *stringa, char *proxy_ip, int timeout_sec);
  50.  
  51. long scarica_mv_video(char *video_id, long video_time, long video_lunghezza, char *proxy_ip, char *video_buffer, float* proxy_speed, long *video_current_point, bool *reset);
  52. int mv_decode(char *string, long key1, long key2);
  53. long mv_video_lunghezza(char *video_id, char *proxy_ip);
  54.  
  55. int proxylist_counter(char *file);
  56. int proxylist_loader(char **proxylist,char * file);
  57.  
  58. int funzione(LPVOID param);
  59. int video_backup(LPVOID param);
  60. #ifdef console_mode
  61. int tell_me(LPVOID param);
  62. #endif
  63.  
  64. int main_thread();
  65. int main_grafica_uploader();
  66.  
  67. char * read_clipboard();
  68.  
  69. struct connection_data {
  70. int processo_id;
  71. char processo_status;
  72. char ** proxylist;
  73. float proxy_speed;
  74. long proxy_num;
  75. long proxy_point;
  76. char * video_id;
  77. char * video_buffer;
  78. long video_point;
  79. long video_starting_point;
  80. long video_current_point;
  81. long video_lunghezza;
  82. bool reset;
  83. float percentage;
  84. };
  85.  
  86. //***************** gtk widget
  87. GtkWidget *finestra1;
  88. GtkWidget *tabella1;
  89.  
  90. GtkWidget *label_mvid, *label_processi_scaricanti, *label_processi_terminati;
  91. GtkWidget *label_time, *label_global_speed;
  92.  
  93. GtkWidget *progressbar_main;
  94. GtkWidget **progressbar_parziale, **label_download_parziale, *tabella_download_parziale, *scrolledwindow_download_parziale;
  95.  
  96. GtkWidget *textview_console, *scrolledwindow_console;
  97. GtkTextBuffer *textview_console_buffer;
  98.  
  99. int console_book_size = 100;
  100. char *console_booking, **console_book;
  101. int book_manager();
  102. void textview_printf(char *string);
  103.  
  104. //variabili globali
  105. char main_grafica_uploader_start = 0;
  106.  
  107. double global_speed = 0;
  108. double global_percentage = 0;
  109. long global_processi_terminati = 0;
  110. long global_processi_scaricanti = 0;
  111. clock_t global_time_start, global_time_end;
  112. struct connection_data *global_dati;
  113.  
  114. //*********************************************//
  115. //******************* MAIN ********************//
  116. //*********************************************//
  117. int main(int argc, char *argv[]){
  118.  
  119. long x;
  120. GError ** thread_error = NULL;
  121.  
  122. g_type_init();
  123.  
  124. /* Secure glib */
  125. if( ! g_thread_supported() )
  126. g_thread_init( NULL );
  127. /* Secure gtk */
  128. gdk_threads_init();
  129. /* Obtain gtk's global lock */
  130. gdk_threads_enter();
  131.  
  132. gtk_init (&argc, &argv);//inizializzo l'ambiente gtk
  133.  
  134. //********** FINESTRA1 *************//
  135. finestra1 = gtk_window_new(GTK_WINDOW_TOPLEVEL); //creo la finestra principale
  136. gtk_window_set_title(GTK_WINDOW(finestra1), VERSIONE_PROGRAMMA); //setto il titolo della finestra principale
  137. gtk_container_set_border_width(GTK_CONTAINER(finestra1), 3); //setto il bordo della finestra principale
  138. gtk_window_set_resizable(GTK_WINDOW(finestra1), FALSE); //rendo non resizable la finestra principale
  139. gtk_window_set_position(GTK_WINDOW(finestra1), GTK_WIN_POS_CENTER); //posizione iniziale finestra
  140. g_signal_connect(G_OBJECT(finestra1), "delete_event", G_CALLBACK(gtk_main_quit), NULL); //evento chiusura finestra princ
  141.  
  142. //********** TABELLA1 *************//
  143. tabella1 = gtk_table_new(25, 10, TRUE); //creo la tabella principale
  144. gtk_container_add(GTK_CONTAINER(finestra1), tabella1); //inserisco la tabella princ. nella finestra princ.
  145.  
  146. //label_mvid, label_processi_scaricanti, label_processi_terminati
  147. label_mvid = gtk_label_new("Waiting for MegaVideo ID...");
  148. gtk_table_attach_defaults(GTK_TABLE(tabella1), label_mvid, 0, 2, 0, 1);
  149. label_processi_scaricanti = gtk_label_new("...");
  150. gtk_table_attach_defaults(GTK_TABLE(tabella1), label_processi_scaricanti, 6, 8, 3, 4);
  151. label_processi_terminati = gtk_label_new("...");
  152. gtk_table_attach_defaults(GTK_TABLE(tabella1), label_processi_terminati, 8, 10, 3, 4);
  153. //label_time
  154. global_time_start = clock();
  155. label_time = gtk_label_new("time...");
  156. gtk_table_attach_defaults(GTK_TABLE(tabella1), label_time, 8, 10, 0, 1);
  157. //label_global_speed
  158. label_global_speed = gtk_label_new("speed...");
  159. gtk_table_attach_defaults(GTK_TABLE(tabella1), label_global_speed, 4, 6, 3, 4);
  160.  
  161. //progressbar_main
  162. progressbar_main = gtk_progress_bar_new();
  163. gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar_main), "Initializing...");
  164. gtk_table_attach_defaults(GTK_TABLE(tabella1), progressbar_main, 0, 10, 1, 3);
  165.  
  166. //textview_console
  167. textview_console = gtk_text_view_new();
  168. gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview_console), GTK_WRAP_WORD_CHAR);
  169. gtk_text_view_set_editable(GTK_TEXT_VIEW(textview_console),FALSE);
  170.  
  171. textview_console_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview_console));
  172. gtk_text_buffer_set_text(textview_console_buffer, "textview_console ready...\n", -1);
  173.  
  174. scrolledwindow_console = gtk_scrolled_window_new(NULL,NULL);
  175. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_console), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  176. gtk_container_add(GTK_CONTAINER(scrolledwindow_console), textview_console);
  177. gtk_table_attach_defaults(GTK_TABLE(tabella1), scrolledwindow_console, 0, 10, 18, 25);
  178.  
  179. //console booking system
  180. console_booking = (char *) malloc(console_book_size * sizeof(char));
  181. console_book = (char **) malloc(console_book_size * sizeof(char *));
  182. for(x=0;x<console_book_size;x++){
  183. console_book[x] = (char *) malloc(1000 * sizeof(char));
  184. console_booking[x] = 1;
  185. }
  186. g_timeout_add(50, (GSourceFunc) book_manager, NULL);
  187.  
  188. //scrolledwindow download parziali
  189. tabella_download_parziale = gtk_table_new(processi_num*2, 10, TRUE);
  190. scrolledwindow_download_parziale = gtk_scrolled_window_new(NULL,NULL);
  191. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_download_parziale), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  192. gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledwindow_download_parziale), tabella_download_parziale);
  193. gtk_table_attach_defaults(GTK_TABLE(tabella1), scrolledwindow_download_parziale, 0, 4, 3, 18);
  194.  
  195. progressbar_parziale = (GtkWidget **) malloc(processi_num * sizeof(GtkWidget *));
  196. for(x=0;x<processi_num;x++){
  197. progressbar_parziale[x] = gtk_progress_bar_new();
  198. gtk_table_attach_defaults(GTK_TABLE(tabella_download_parziale), progressbar_parziale[x], 0, 10, x*2, x*2+1);
  199. }
  200.  
  201. label_download_parziale = (GtkWidget **) malloc(processi_num * sizeof(GtkWidget *));
  202. for(x=0;x<processi_num;x++){
  203. label_download_parziale[x] = gtk_label_new("label...");
  204. gtk_table_attach_defaults(GTK_TABLE(tabella_download_parziale), label_download_parziale[x], 0, 10, x*2+1, x*2+2);
  205. }
  206.  
  207. gtk_widget_show_all(finestra1);
  208.  
  209. //********** lancio main_thread() *************//
  210. g_thread_create((GThreadFunc) main_thread, NULL, 0, thread_error);
  211.  
  212. // main_grafica_uploader() si occupa di aggiornare la grafica durante il download
  213. //g_idle_add((GSourceFunc) main_grafica_uploader, NULL);
  214. g_timeout_add(100, (GSourceFunc) main_grafica_uploader, NULL);
  215.  
  216. gtk_main();
  217.  
  218. /* Release gtk's global lock */
  219. gdk_threads_leave();
  220.  
  221. return 0;
  222. }
  223.  
  224. int main_grafica_uploader(){
  225. if(main_grafica_uploader_start == 0)
  226. return 1;
  227.  
  228. long x;
  229. long time;
  230. char *console_string;
  231.  
  232. if(global_percentage < 1)
  233. global_time_end = clock();
  234.  
  235. console_string = (char *) malloc(1000*sizeof(char));
  236. time = (global_time_end-global_time_start)/CLOCKS_PER_SEC;
  237.  
  238. gdk_threads_enter();
  239.  
  240. //titolo finestra
  241. sprintf(console_string,"%.2f%% %s", global_percentage*100, VERSIONE_PROGRAMMA);
  242. gtk_window_set_title(GTK_WINDOW(finestra1), console_string);
  243.  
  244. //labels
  245. sprintf(console_string,"%d su %d scaricanti", global_processi_scaricanti, (processi_num-global_processi_terminati));
  246. gtk_label_set_text(GTK_LABEL(label_processi_scaricanti), console_string);
  247.  
  248. sprintf(console_string,"%d su %d terminati", global_processi_terminati, processi_num);
  249. gtk_label_set_text(GTK_LABEL(label_processi_terminati), console_string);
  250.  
  251. sprintf(console_string,"%02d:%02d:%02d", (time/60/60),(time/60)%60, time%60);
  252. gtk_label_set_text(GTK_LABEL(label_time), console_string);
  253.  
  254. sprintf(console_string,"%.2f KB/sec", global_speed);
  255. gtk_label_set_text(GTK_LABEL(label_global_speed), console_string);
  256.  
  257. //progressbar_main
  258. if(global_percentage >= 0 && global_percentage <=1)
  259. gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_main),((gdouble)global_percentage));
  260. else{
  261. global_percentage = 0;
  262. gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_main),((gdouble)0));
  263. }
  264.  
  265. sprintf(console_string,"%.2f%% (%.2f KB/sec)",global_percentage*100,global_speed);
  266. gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar_main),console_string);
  267.  
  268. for(x=0;x<processi_num;x++){
  269. if(global_dati[x].percentage>1 || global_percentage>=1) global_dati[x].percentage = 1;
  270.  
  271. if(global_dati[x].percentage >= 0 && global_dati[x].percentage <=1)
  272. gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_parziale[x]),((gdouble)global_dati[x].percentage));
  273. else{
  274. global_dati[x].percentage = 0;
  275. gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_parziale[x]),((gdouble)0));
  276. }
  277.  
  278. sprintf(console_string,"%d: proxy %d (%.2f KB/sec)",global_dati[x].processo_id,global_dati[x].proxy_point, global_dati[x].proxy_speed);
  279. gtk_label_set_text(GTK_LABEL(label_download_parziale[x]), console_string);
  280.  
  281. //sprintf(console_string,"%.2f%%",global_dati[x].percentage*100);
  282. //gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar_parziale[x]),console_string);
  283. }
  284.  
  285. while(gtk_events_pending())
  286. gtk_main_iteration();
  287.  
  288. gdk_threads_leave();
  289.  
  290. free(console_string);
  291.  
  292. return 1;
  293. }
  294.  
  295. int main_thread(){
  296. FILE *stream;
  297. long x, proxy_num, video_bytes=0;
  298. char **proxylist;
  299. char *video_buffer;
  300. bool wait=1;
  301. int processi_terminati;
  302. float overall_speed = 0;
  303. float download_percentage = 0;
  304. float tot_download_percentage = 0;
  305. int download_attivi = 0;
  306. struct connection_data dati_b;
  307. char *console_string;
  308.  
  309. console_string = (char *) malloc(1000 * sizeof(char));
  310.  
  311. DWORD ThreadId;
  312.  
  313. char *video_id;
  314.  
  315. //********* VARIABILI *********//
  316. video_id = read_clipboard();
  317.  
  318. if(video_id == 0 || strlen(video_id) != 8){
  319. #ifdef console_mode
  320. printf("La stringa che hai in memoria non è un ID megavideo valido!\n");
  321. #endif
  322. gtk_label_set_text(GTK_LABEL(label_mvid), "ID megavideo non valido...");
  323. textview_printf("ID megavideo non valido...\n");
  324.  
  325. return 0;
  326. }
  327. else{
  328. #ifdef console_mode
  329. printf("ID video letto dalla clipboard: %s\n",video_id);
  330. #endif
  331. sprintf(console_string, "MegaVideo ID: %s",video_id);
  332. gtk_label_set_text(GTK_LABEL(label_mvid), console_string);
  333. sprintf(console_string, "MegaVideo ID: %s\n",video_id);
  334. textview_printf(console_string);
  335. }
  336.  
  337.  
  338. //******************************//
  339. //*** CARICAMENTO PROXY LIST ***//
  340. //******************************//
  341. #ifdef console_mode
  342. printf("PROXYLIST: caricamento proxylist...\n");
  343. #endif
  344. textview_printf("PROXYLIST: caricamento proxylist...\n");
  345.  
  346. proxy_num = proxylist_counter(proxylist_file);
  347.  
  348. #ifdef console_mode
  349. printf("PROXYLIST: numero dei proxy: %d\n",proxy_num);
  350. #endif
  351. sprintf(console_string, "PROXYLIST: numero dei proxy: %d\n",proxy_num);
  352. textview_printf(console_string);
  353.  
  354. proxylist = (char **) malloc(proxy_num * sizeof(char *));
  355. for(x=0;x<proxy_num;x++)
  356. proxylist[x] = (char *) malloc(25 * sizeof(char));
  357. proxylist_loader(proxylist,proxylist_file);
  358. //for(x=0;x<proxy_num;x++)
  359. // printf("%d: %s\n",x,proxylist[x]);
  360. #ifdef console_mode
  361. printf("PROXYLIST: caricamento proxylist completato!\n\n");
  362. #endif
  363. textview_printf("PROXYLIST: caricamento proxylist completato!\n\n");
  364.  
  365. //*********************************//
  366. //*** CONTROLLO LUNGHEZZA VIDEO ***//
  367. //*********************************//
  368. #ifdef console_mode
  369. printf("VIDEO: id: %s\n",video_id);
  370. #endif
  371. sprintf(console_string, "VIDEO: id: %s\n",video_id);
  372. textview_printf(console_string);
  373. #ifdef console_mode
  374. printf("VIDEO: connessione per trovare lunghezza video...\n");
  375. #endif
  376. textview_printf("VIDEO: connessione per trovare lunghezza video...\n");
  377. x=0;
  378. while(video_bytes > 1000000000 || video_bytes < 100){
  379. video_bytes = 0;
  380. video_bytes = mv_video_lunghezza(video_id, proxylist[x]);
  381. x++;
  382. }
  383. #ifdef console_mode
  384. printf("\n");
  385. #endif
  386. textview_printf("\n");
  387.  
  388. //******************************//
  389. //*** CREAZIONE BUFFER VIDEO ***//
  390. //******************************//
  391. video_buffer = (char *) calloc (video_bytes, sizeof(char));
  392. for(x=0;x<video_bytes;x++)
  393. video_buffer[x] = '\0';
  394. #ifdef console_mode
  395. printf("BUFFER: buffer caricato!\n\n");
  396. #endif
  397. textview_printf("BUFFER: buffer caricato!\n\n");
  398.  
  399. //**********************************//
  400. //*** CARICAMENTO BACKUP MANAGER ***//
  401. //**********************************//
  402. #ifdef console_mode
  403. printf("BACKUP: attivazione...\n\n");
  404. #endif
  405. textview_printf("BACKUP: attivazione...\n\n");
  406. dati_b.video_buffer = video_buffer;
  407. dati_b.video_lunghezza = video_bytes;
  408. dati_b.processo_status = 1;
  409. CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) video_backup, &dati_b, 0, &ThreadId);
  410.  
  411. //***********************************************//
  412. //*** CARICAMENTO PROXY E DOWNLOAD SIMULTANEI ***//
  413. //***********************************************//
  414. #ifdef console_mode
  415. printf("CONNECTION MANAGER: attivazione in corso...\n");
  416. printf("CONNECTION MANAGER: numero di processi da eseguire: %d\n",processi_num);
  417. #endif
  418.  
  419. textview_printf("CONNECTION MANAGER: attivazione in corso...\n");
  420. sprintf(console_string, "CONNECTION MANAGER: numero di processi da eseguire: %d\n",processi_num);
  421. textview_printf(console_string);
  422. struct connection_data *dati;//strumento di trasmissione informazioni alle funzioni asincrone
  423. dati = (struct connection_data *) malloc(processi_num * sizeof(struct connection_data));
  424. for(x=0;x<processi_num;x++){//attivazione processi
  425. dati[x].processo_id = x;
  426. dati[x].processo_status = 1;
  427. dati[x].proxylist = proxylist;
  428. dati[x].proxy_speed = 0;
  429. dati[x].proxy_num = proxy_num;
  430. dati[x].proxy_point = (unsigned int) 0;//x*proxy_num/processi_num;
  431. dati[x].video_id = video_id;
  432. dati[x].video_buffer = video_buffer;
  433. dati[x].video_point = x*((unsigned long)video_bytes/processi_num);
  434. dati[x].video_starting_point = dati[x].video_point;
  435. dati[x].video_current_point = dati[x].video_point;
  436. dati[x].video_lunghezza = video_bytes;
  437. dati[x].reset = 0;
  438. dati[x].percentage = 0;
  439. CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) funzione, &dati[x], 0, &ThreadId);
  440. #ifdef console_mode
  441. printf("CONNECTION MANAGER: attivazione ID: %d\n",dati[x].processo_id);
  442. #endif
  443. sprintf(console_string, "CONNECTION MANAGER: attivazione ID: %d\n",dati[x].processo_id);
  444. textview_printf(console_string);
  445. }
  446. #ifdef console_mode
  447. printf("CONNECTION MANAGER: tutti i processi sono stati attivati\n\n",dati[x].processo_id);
  448. #endif
  449. sprintf(console_string, "CONNECTION MANAGER: tutti i processi sono stati attivati\n\n",dati[x].processo_id);
  450. textview_printf(console_string);
  451.  
  452. #ifdef console_mode
  453. //attivazione tell_me
  454. CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) tell_me, dati, 0, &ThreadId);
  455. #endif
  456.  
  457. global_dati = dati;
  458. main_grafica_uploader_start = 1; //attivo main_grafica_uploader()
  459.  
  460. while(wait){ //attività di controllo funzioni asincrone
  461. Sleep(1000);
  462. //system("cls");
  463.  
  464. processi_terminati=0;
  465. for(x=0;x<processi_num;x++)
  466. if(dati[x].processo_status == 0) processi_terminati++;
  467. if(processi_terminati == processi_num){
  468. global_speed = 0;
  469. global_percentage = 1;
  470. break;
  471. }
  472.  
  473. #ifdef console_mode_grafica
  474. printf("\nCONNECTION MANAGER: %d su %d segmenti terminati.\n\n",processi_terminati,processi_num);
  475. #endif
  476.  
  477. overall_speed = 0; download_attivi=0;
  478. tot_download_percentage = 0;
  479. for(x=0;x<processi_num;x++){
  480. overall_speed = overall_speed + dati[x].proxy_speed;
  481. if(dati[x].proxy_speed >0) download_attivi++;
  482.  
  483. //****************************//
  484. //***** DOWNLOAD GRAFICA *****//
  485. //****************************//
  486. #ifdef console_mode_grafica
  487. if(dati[x].processo_id < 10) printf(" ");
  488. printf("%d: ",dati[x].processo_id);
  489. #endif
  490.  
  491. download_percentage = (float)100*(dati[x].video_current_point-dati[x].video_starting_point)/dati[1].video_starting_point;
  492. if(dati[x].processo_status == 0) download_percentage = 100;
  493. tot_download_percentage = tot_download_percentage+download_percentage/processi_num;
  494.  
  495. dati[x].percentage = (float) download_percentage/100;
  496.  
  497. #ifdef console_mode_grafica
  498. printf("|");
  499. for(y=0;y<33;y++)
  500. if (download_percentage == 100) printf("X");
  501. else if(y*100/33 < download_percentage) printf("x");
  502. else printf("-");
  503. printf("| %.2f%%\x09%.3f\x09proxy %d\n", download_percentage, dati[x].proxy_speed,dati[x].proxy_point);
  504. #endif
  505.  
  506. }
  507. #ifdef console_mode_grafica
  508. printf("\nCONNECTION MANAGER: %.2f%% SPEED: %.3f kbs (%d su %d scaricanti)\n\n",tot_download_percentage, overall_speed,download_attivi,(processi_num-processi_terminati));
  509. #endif
  510.  
  511. //salvo i dati nelle variabili globali
  512. global_speed = overall_speed;
  513. global_percentage = ((double)tot_download_percentage/100);
  514. global_processi_scaricanti = download_attivi;
  515. global_processi_terminati = processi_terminati;
  516.  
  517.  
  518. #ifdef console_mode_grafica
  519. printf("\ntell me:\n");
  520. #endif
  521. }
  522. #ifdef console_mode
  523. printf("CONNECTION MANAGER: Tutti i processi sono terminati.\n\n");
  524. #endif
  525. textview_printf("CONNECTION MANAGER: Tutti i processi sono terminati.\n\n");
  526. dati_b.processo_status = 0;//termino il sistema di backup
  527.  
  528. //********************************//
  529. //*** SCRITTURA FILE SCARICATO ***//
  530. //********************************//
  531. #ifdef console_mode
  532. printf("BUFFER: scrittura buffer su file in corso...\n");
  533. #endif
  534. textview_printf("BUFFER: scrittura buffer su file in corso...\n");
  535.  
  536. sprintf(console_string, "%s.flv", video_id);
  537. stream = fopen(console_string,"wb");
  538. fwrite(video_buffer,1,video_bytes,stream);
  539. fclose(stream);
  540. #ifdef console_mode
  541. printf("BUFFER: buffer scritto!\n\n");
  542. #endif
  543. textview_printf("BUFFER: buffer scritto!\n\n");
  544.  
  545. return 1;
  546. }
  547.  
  548. //********************************************************************//
  549. #ifdef console_mode
  550. int tell_me(LPVOID param){
  551. struct connection_data *dati = (struct connection_data *) param;
  552. int proc, proxy;
  553. char comando;
  554. while(1){
  555. printf(">");
  556. scanf("%c",&comando);
  557. if(comando == 'r'){ //chiude connessione del processo e la riapre con il proxy selezionato
  558. printf("RESET ");
  559. printf("processo:");
  560. scanf("%d",&proc);
  561. printf("proxy:",proc);
  562. scanf("%d",&proxy);
  563. printf("\nRESET OK: processo %d proxy %d\n",proc,proxy);
  564. if(proxy>0)proxy=proxy-1;
  565.  
  566. dati[proc].reset = 1;
  567. dati[proc].proxy_point = proxy;
  568. }
  569. }
  570. return 1;
  571. }
  572. #endif
  573.  
  574. //********************************************************************//
  575. int funzione(LPVOID param){
  576. long video_point_now;
  577. struct connection_data *dati = (struct connection_data *) param;
  578. char *console_string;
  579.  
  580. console_string = (char *) malloc(1000 * sizeof(char));
  581.  
  582. video_point_now = dati->video_point;
  583.  
  584. #ifdef console_mode
  585. printf("PROCESSO %d: inizializzazione in corso...\n", dati->processo_id);
  586. printf("PROCESSO %d: video id: %s starting point: %d\n", dati->processo_id, dati->video_id, video_point_now);
  587. printf("PROCESSO %d: proxy point: %d\n", dati->processo_id, dati->proxy_point);
  588. #endif
  589.  
  590. sprintf(console_string, "PROCESSO %d: inizializzazione in corso...\n", dati->processo_id);
  591. textview_printf(console_string);
  592. sprintf(console_string, "PROCESSO %d: video id: %s starting point: %d\n", dati->processo_id, dati->video_id, video_point_now);
  593. textview_printf(console_string);
  594. sprintf(console_string, "PROCESSO %d: proxy point: %d\n", dati->processo_id, dati->proxy_point);
  595. textview_printf(console_string);
  596.  
  597. while(1){
  598. if(video_point_now < 0) break;
  599. if(video_point_now >= dati->video_lunghezza) break;
  600. #ifdef console_mode
  601. printf("PROCESSO %d: start download, point %d, proxy %d\n", dati->processo_id,video_point_now,dati->proxy_point);
  602. #endif
  603. sprintf(console_string, "PROCESSO %d: start download, point %d, proxy %d\n", dati->processo_id,video_point_now,dati->proxy_point);
  604. textview_printf(console_string);
  605.  
  606. video_point_now = scarica_mv_video(dati->video_id, video_point_now, dati->video_lunghezza, dati->proxylist[dati->proxy_point], dati->video_buffer, &dati->proxy_speed, &dati->video_current_point, &dati->reset);
  607.  
  608. if(video_point_now >= 0)dati->proxy_point++;
  609. if(dati->proxy_point >= dati->proxy_num) dati->proxy_point = 0;//riavvolgi lista proxy
  610. }
  611. dati->processo_status = 0; //segnalo chiusura processo
  612. //printf("PROCESSO %d: terminato!\n", dati->processo_id);
  613. return 1;
  614. }
  615.  
  616. //********************************************************************//
  617. int video_backup(LPVOID param){
  618. struct connection_data *dati = (struct connection_data *) param;
  619. #ifdef console_mode
  620. printf("BACKUP: attivato...\n");
  621. #endif
  622. textview_printf("BACKUP: attivato...\n");
  623.  
  624. while(1){
  625. if(backup_time > 0)
  626. Sleep(backup_time);
  627. else break;
  628.  
  629. if(dati->processo_status == 0){
  630. #ifdef console_mode
  631. printf("BACKUP: TERMINATO!\n");
  632. #endif
  633. textview_printf("BACKUP: TERMINATO!\n");
  634. break;
  635. }
  636.  
  637. FILE *stream;
  638. //printf("\nBACKUP: scrittura buffer su file in corso...\n");
  639. stream = fopen("video_temp.flv","wb");
  640. fwrite(dati->video_buffer,1,dati->video_lunghezza,stream);
  641. fclose(stream);
  642. //printf("BACKUP: buffer scritto!\n\n");
  643. }
  644.  
  645. #ifdef console_mode
  646. printf("BACKUP: terminato!\n");
  647. #endif
  648. textview_printf("BACKUP: terminato!\n");
  649. return 1;
  650. }
  651.  
  652. //********************************************************************//
  653. int proxylist_counter(char *file){
  654. FILE *in;
  655. long in_size;
  656. in = fopen(file,"rb");
  657. fseek(in,0,SEEK_END);
  658. in_size = ftell(in);
  659. rewind(in);
  660. char *buffer;
  661. buffer = (char *) malloc(in_size * sizeof(char));
  662. fread(buffer,1,in_size,in);
  663.  
  664. int numero_proxy=0,x,y=0,z=0,c1=0;
  665. for(x=0;x<in_size;x++)
  666. if(buffer[x]== '\x0a') numero_proxy++;
  667.  
  668. return numero_proxy;
  669. }
  670.  
  671. //********************************************************************//
  672. int proxylist_loader(char **proxylist, char *file){
  673. FILE *in;
  674. long in_size;
  675. in = fopen(file,"rb");
  676. fseek(in,0,SEEK_END);
  677. in_size = ftell(in);
  678. rewind(in);
  679. char *buffer;
  680. buffer = (char *) malloc(in_size * sizeof(char));
  681. fread(buffer,1,in_size,in);
  682.  
  683. int numero_proxy=0,x,y=0,z=0,c1=0;
  684. for(x=0;x<in_size;x++)
  685. if(buffer[x]== '\x0a') numero_proxy++;
  686.  
  687. for(x=0;x<in_size;x++){
  688. if(z>26)printf("ERRORE! Sintassi proxylist non corretta...\n");
  689. if(y>=numero_proxy)break;
  690. if(buffer[x]=='\x0a'){y++;c1=0;z=0;}
  691. else if( buffer[x] > 58 || buffer[x]< 46 )
  692. c1=1;
  693. else if(c1==0){
  694. proxylist[y][z] = buffer[x];
  695. proxylist[y][z+1] = '\0';
  696. z++;
  697. }
  698. }
  699.  
  700.  
  701. return numero_proxy;
  702. }
  703.  
  704. //********************************************************************//
  705. long scarica_mv_video(char *video_id, long video_time, long video_lunghezza, char *proxy_ip, char *video_buffer, float* proxy_speed, long *video_current_point, bool *reset){
  706.  
  707. char *mv_xml_file;
  708. mv_xml_file = (char *) malloc(10000 * sizeof(char));
  709.  
  710. char ckey1[10];
  711. char ckey2[10];
  712. char hash[40];
  713. char serverid[10];
  714.  
  715. char svideo_time[20];
  716. if(video_time>0)
  717. itoa(video_time,svideo_time,10);
  718. else
  719. svideo_time[0]='\0';
  720.  
  721. time_t itempo;
  722. char stempo[20];
  723. itempo = time(NULL);
  724. itoa(itempo,stempo,10);
  725. //printf("time: %s\n",stempo);
  726.  
  727. char url[1000]="http://www.megavideo.com/xml/videolink.php?v=";
  728. char url2[]="&width=1278&id=";
  729. char url3[]="&u=";
  730. strcat(url,video_id);
  731. strcat(url,url2);
  732. strcat(url,stempo);
  733. strcat(url,url3);
  734. //printf("%s\n",url);
  735.  
  736. proxy_scarica_su_stringa(url,"www.megavideo.com",mv_xml_file,proxy_ip,15); //ricevo file di configurazione megavideo
  737.  
  738.  
  739. #ifdef console_mode
  740. printf("%s\n\n",mv_xml_file);
  741. printf("xml strlen: %d\n",strlen(mv_xml_file));
  742. #endif
  743. if(strlen(mv_xml_file) < 100) return video_time;
  744. #ifdef console_mode
  745. printf("inizio analisi xml file...\n");
  746. #endif
  747.  
  748. int x,ap_counter=0,i=0, key1,key2;
  749. for(x=0;x<strlen(mv_xml_file);x++){
  750. if(mv_xml_file[x] == 34) ap_counter++;
  751.  
  752. if(ap_counter == 17){ //caricamento key1
  753. if(i>0)ckey1[i-1]=mv_xml_file[x];
  754. i++;
  755. }
  756. if(ap_counter == 18){
  757. ckey1[i-1] = '\0';
  758. i=0;
  759. key1=atoi(ckey1);
  760. }
  761.  
  762. if(ap_counter == 19){ //caricamento key2
  763. if(i>0)ckey2[i-1]=mv_xml_file[x];
  764. i++;
  765. }
  766. if(ap_counter == 20){
  767. ckey2[i-1] = '\0';
  768. i=0;
  769. key2=atoi(ckey2);
  770. }
  771.  
  772. if(ap_counter == 21){ //caricamento hash
  773. if(i>0)hash[i-1]=mv_xml_file[x];
  774. i++;
  775. }
  776. if(ap_counter == 22){
  777. hash[i-1] = '\0';
  778. i=0;
  779. }
  780.  
  781. if(ap_counter == 23){ //caricamento serverid
  782. if(i>0)serverid[i-1]=mv_xml_file[x];
  783. i++;
  784. }
  785. if(ap_counter == 24){
  786. serverid[i-1] = '\0';
  787. i=0;
  788. }
  789. }
  790.  
  791. //printf("key1: %d key2: %d serverid: %s\nhash: %s %d\n",key1,key2,serverid,hash,strlen(hash));
  792. if(strlen(hash)!=32){
  793. #ifdef console_mode
  794. printf("errore estrazione dati per decoding...\n");
  795. #endif
  796. return video_time;
  797. }
  798.  
  799. #ifdef console_mode
  800. printf("decoding in: %s\n",hash);
  801. #endif
  802. mv_decode(hash, key1, key2);
  803. #ifdef console_mode
  804. printf("decoding ok: %s\n",hash);
  805. #endif
  806.  
  807. char furl[1000]="http://www";
  808. char furl2[]=".megavideo.com/files/";
  809. char furl3[]="/";
  810. strcat(furl,serverid);
  811. strcat(furl,furl2);
  812. strcat(furl,hash);
  813. strcat(furl,furl3);
  814. strcat(furl,svideo_time);
  815.  
  816.  
  817. char fhost[1000]="www";
  818. char fhost2[]=".megavideo.com";
  819. strcat(fhost,serverid);
  820. strcat(fhost,fhost2);
  821.  
  822. #ifdef console_mode
  823. printf("FINAL DOWNLOAD URL: %s\n",furl);
  824. #endif
  825.  
  826. Sleep(5000);
  827.  
  828. video_time = proxy_scarica(furl, fhost,video_buffer, proxy_ip, 15, video_time, video_lunghezza, proxy_speed, video_current_point, reset);
  829.  
  830. free(mv_xml_file);
  831. return video_time;
  832. }
  833.  
  834. //********************************************************************//
  835. long mv_video_lunghezza(char *video_id, char *proxy_ip){
  836.  
  837. char *mv_xml_file;
  838. char *console_string;
  839. char *ckey1;
  840.  
  841. mv_xml_file = (char *) malloc(10000 * sizeof(char));
  842. mv_xml_file[0] = '\0';
  843. ckey1 = (char *) malloc(10*sizeof(char));
  844. console_string = (char *) malloc(1000 * sizeof(char));
  845.  
  846. time_t itempo;
  847. char stempo[20];
  848. itempo = time(NULL);
  849. itoa(itempo,stempo,10);
  850. //printf("time: %s\n",stempo);
  851.  
  852. char *url;
  853. url = (char *) malloc(1000*sizeof(char));
  854. sprintf(url,"http://www.megavideo.com/xml/videolink.php?v=%s&width=1278&id=%s&u=",video_id,stempo);
  855.  
  856. #ifdef console_mode
  857. printf("VIDEO: %s\n",url);
  858. #endif
  859.  
  860.  
  861.  
  862. sprintf(console_string, "VIDEO: %s\n",url);
  863. textview_printf(console_string);
  864.  
  865. proxy_scarica_su_stringa(url,"www.megavideo.com",mv_xml_file,proxy_ip,15); //ricevo file di configurazione megavideo
  866.  
  867. #ifdef console_mode
  868. printf("%s\n\n",mv_xml_file);
  869. printf("xml strlen: %d\n",strlen(mv_xml_file));
  870. #endif
  871.  
  872. if(strlen(mv_xml_file) < 10)
  873. return 0;
  874.  
  875. int x,ap_counter=0,i=0, key1;
  876. for(x=0;x<strlen(mv_xml_file);x++){
  877. if(mv_xml_file[x] == 34) ap_counter++;
  878.  
  879. if(ap_counter == 11){ //caricamento key1
  880. if(i>0)ckey1[i-1]=mv_xml_file[x];
  881. i++;
  882. }
  883. if(ap_counter == 12){
  884. ckey1[i-1] = '\0';
  885. i=0;
  886. key1=atoi(ckey1);
  887. }
  888. }
  889. #ifdef console_mode
  890. printf("VIDEO: bytes: %d\n",key1);
  891. #endif
  892. sprintf(console_string, "VIDEO: bytes: %d\n",key1);
  893. textview_printf(console_string);
  894.  
  895. return key1;
  896. }
  897.  
  898. //********************************************************************//
  899. bool proxy_scarica_su_stringa(char *fileremoto, char *host,char *stringa, char *proxy_ip, int timeout_sec){
  900.  
  901.  
  902. struct sockaddr_in sock; //la struttura per il socket
  903. struct hostent ip; //la struttura per l'indirizzo
  904. int s;
  905.  
  906. unsigned long cmd;
  907. fd_set fds;
  908. struct timeval timeout;
  909.  
  910.  
  911. bool header = true; //variabili per header
  912. bool invio = false;
  913. char ultimo_carattere = '\0';
  914.  
  915. char msg_response[] = "\0";
  916. char modo[4];
  917.  
  918. //proxy ip
  919. char ipn[10];
  920. int k=0,i=0,nip=0;
  921. int ip1=0,ip2=0,ip3=0,ip4=0,ipport=0;
  922. for(k=0;k<(strlen(proxy_ip)+1);k++){
  923. if(proxy_ip[k] != 46 && proxy_ip[k] != 58 && k!=strlen(proxy_ip)){
  924. ipn[i]=proxy_ip[k];
  925. ipn[i+1]='\0';
  926. i++;
  927. }
  928. else{
  929. if(nip==0)ip1=atoi(ipn);
  930. if(nip==1)ip2=atoi(ipn);
  931. if(nip==2)ip3=atoi(ipn);
  932. if(nip==3)ip4=atoi(ipn);
  933. if(nip==4)ipport=atoi(ipn);
  934. nip++;
  935. i=0;
  936. }
  937. }
  938. #ifdef console_mode
  939. printf("risoluzione indirizzo ip: %d.%d.%d.%d:%d\n",ip1,ip2,ip3,ip4,ipport);
  940. #endif
  941.  
  942. //********* CONNESSIONE *********//
  943. WORD wVersion;
  944. WSADATA wsaData;
  945. wVersion = MAKEWORD(2, 2);
  946. WSAStartup(wVersion, &wsaData); //winsock startup
  947.  
  948. s = socket(AF_INET, SOCK_STREAM, 0);
  949. cmd=1; // imposto modalità socket non boccante (andrà gestita con select())
  950. ioctlsocket( s, FIONBIO, &cmd);
  951. // dati per Select
  952. timeout.tv_sec = timeout_sec; // numero di secondi per il timeout
  953. timeout.tv_usec = 0; // numero di microsecondi per il timeout
  954. FD_ZERO(&fds);
  955. FD_SET(s, &fds); // aggiungo sd all'elenco dei socket gestiti da select
  956.  
  957.  
  958. sock.sin_addr.S_un.S_un_b.s_b1=ip1; // indico l'indirizzo IP
  959. sock.sin_addr.S_un.S_un_b.s_b2=ip2;
  960. sock.sin_addr.S_un.S_un_b.s_b3=ip3;
  961. sock.sin_addr.S_un.S_un_b.s_b4=ip4;
  962. sock.sin_port = htons(ipport); //porta
  963. //ip = *(gethostbyname(host)); // in dico l'indirizzo
  964. //memcpy(&sock.sin_addr, ip.h_addr, ip.h_length); //copia l'indirizzo nella struttura socket
  965. sock.sin_family = AF_INET;
  966.  
  967. connect(s, (struct sockaddr *)&sock, sizeof(sock)); //ci connettiamo
  968. if (select(s+1, 0, &fds, 0, &timeout)>0){ //riusciamo a stabilire la connessione
  969.  
  970. //printf("Connessione al server riuscita...\n");
  971. cmd=0;//ripristino socket bloccante
  972. ioctlsocket( s, FIONBIO, &cmd);
  973.  
  974. struct timeval tv;
  975.  
  976. tv.tv_sec = socket_timeout;
  977. tv.tv_usec = 0;
  978. setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
  979.  
  980.  
  981. //********* MESSAGGIO *********//
  982.  
  983.  
  984. //messaggio per il server...
  985. char msg_enter[600] = "GET ";
  986. char protocollo[] = " HTTP/1.1\r\n";
  987. char protocollo2[] = "Host: ";
  988. char protocollo3[] = "\r\n";
  989. //char dettagli1[] = "Range: bytes=0-20\r\n";
  990.  
  991. strcat(msg_enter,fileremoto);
  992. strcat(msg_enter,protocollo);
  993. strcat(msg_enter,protocollo2);
  994. strcat(msg_enter,host);
  995. strcat(msg_enter,protocollo3);
  996. //strcat(msg_enter,dettagli1); //header
  997. strcat(msg_enter,"\r\n");
  998.  
  999.  
  1000.  
  1001. //printf("HTTP PROTOCOL: %s\n", msg_enter);
  1002. send(s, msg_enter, strlen(msg_enter), 0); //invia dati
  1003. //printf("Download in corso...\n\n");
  1004.  
  1005. k = 0;
  1006. stringa[0] = '\0';
  1007. while(recv(s, msg_response, 1, 0) > 0) {
  1008.  
  1009. if (!header){ stringa[k] = msg_response[0]; k++; }
  1010. //else printf("%c",msg_response[0]);
  1011.  
  1012. //controllo per non scrivere header nel file.
  1013. if (ultimo_carattere == 13 && msg_response[0] == 10 && invio==true) header=false;
  1014. ((ultimo_carattere == 13 && msg_response[0] == 10) || (ultimo_carattere==10 && invio))?invio=true:invio=false;
  1015. ultimo_carattere = msg_response[0];
  1016.  
  1017. //fprintf(fo,"%c",msg_response[0]);
  1018. }
  1019. //printf("recv fine!\n");
  1020.  
  1021. stringa[k] = '\0';
  1022.  
  1023. //printf("File Ricevuto\n");
  1024.  
  1025.  
  1026. }//fine if(select())
  1027. else{ //timeout
  1028. //printf("Connessione timeout!\n");
  1029. return false;
  1030. }
  1031.  
  1032. //operazioni di chiusura!
  1033. shutdown(s, 2);
  1034. closesocket(s);
  1035. WSACleanup();
  1036.  
  1037. return true;
  1038. }
  1039.  
  1040. //********************************************************************//
  1041. long proxy_scarica(char *fileremoto, char *host,char *video_buffer, char *proxy_ip, int timeout_sec, long video_time, long video_lunghezza, float *proxy_speed, long *video_current_point, bool *reset){
  1042.  
  1043. long vid_byte_count = video_time;
  1044.  
  1045. struct sockaddr_in sock; //la struttura per il socket
  1046. struct hostent ip; //la struttura per l'indirizzo
  1047. int s;
  1048.  
  1049. unsigned long cmd;
  1050. fd_set fds;
  1051. struct timeval timeout;
  1052.  
  1053. long x;
  1054.  
  1055. bool header = true; //variabili per header
  1056. bool invio = false;
  1057. char ultimo_carattere = '\0';
  1058.  
  1059. char msg_response[] = "\0";
  1060. char modo[4];
  1061.  
  1062. //proxy ip
  1063. char ipn[10];
  1064. int k=0,i=0,nip=0;
  1065. int ip1=0,ip2=0,ip3=0,ip4=0,ipport=0;
  1066. for(k=0;k<(strlen(proxy_ip)+1);k++){
  1067. if(proxy_ip[k] != 46 && proxy_ip[k] != 58 && k!=strlen(proxy_ip)){
  1068. ipn[i]=proxy_ip[k];
  1069. ipn[i+1]='\0';
  1070. i++;
  1071. }
  1072. else{
  1073. if(nip==0)ip1=atoi(ipn);
  1074. if(nip==1)ip2=atoi(ipn);
  1075. if(nip==2)ip3=atoi(ipn);
  1076. if(nip==3)ip4=atoi(ipn);
  1077. if(nip==4)ipport=atoi(ipn);
  1078. nip++;
  1079. i=0;
  1080. }
  1081. }
  1082. #ifdef console_mode
  1083. printf("risoluzione indirizzo ip: %d.%d.%d.%d:%d\n",ip1,ip2,ip3,ip4,ipport);
  1084. #endif
  1085.  
  1086. //********* CONNESSIONE *********//
  1087. WORD wVersion;
  1088. WSADATA wsaData;
  1089. wVersion = MAKEWORD(2, 2);
  1090. WSAStartup(wVersion, &wsaData); //winsock startup
  1091.  
  1092. s = socket(AF_INET, SOCK_STREAM, 0);
  1093. cmd=1; // imposto modalità socket non boccante (andrà gestita con select())
  1094. ioctlsocket( s, FIONBIO, &cmd);
  1095. // dati per Select
  1096. timeout.tv_sec = timeout_sec; // numero di secondi per il timeout
  1097. timeout.tv_usec = 0; // numero di microsecondi per il timeout
  1098. FD_ZERO(&fds);
  1099. FD_SET(s, &fds); // aggiungo s all'elenco dei socket gestiti da select
  1100.  
  1101.  
  1102. sock.sin_addr.S_un.S_un_b.s_b1=ip1; // indico l'indirizzo IP
  1103. sock.sin_addr.S_un.S_un_b.s_b2=ip2;
  1104. sock.sin_addr.S_un.S_un_b.s_b3=ip3;
  1105. sock.sin_addr.S_un.S_un_b.s_b4=ip4;
  1106. sock.sin_port = htons(ipport); //porta
  1107. //ip = *(gethostbyname(host)); // in dico l'indirizzo
  1108. //memcpy(&sock.sin_addr, ip.h_addr, ip.h_length); //copia l'indirizzo nella struttura socket
  1109. sock.sin_family = AF_INET;
  1110.  
  1111. connect(s, (struct sockaddr *)&sock, sizeof(sock)); //ci connettiamo
  1112. if (select(s+1, 0, &fds, 0, &timeout)>0){ //riusciamo a stabilire la connessione
  1113.  
  1114. //printf("Connessione al server riuscita...\n");
  1115. cmd=0;//ripristino socket bloccante
  1116. ioctlsocket( s, FIONBIO, &cmd);
  1117.  
  1118. struct timeval tv;
  1119.  
  1120. tv.tv_sec = socket_timeout;
  1121. tv.tv_usec = 0;
  1122. setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv) );
  1123.  
  1124. //********* MESSAGGIO *********//
  1125.  
  1126. //messaggio per il server...
  1127. char msg_enter[600] = "GET ";
  1128. char protocollo[] = " HTTP/1.1\r\n";
  1129. char protocollo2[] = "Host: ";
  1130. char protocollo3[] = "\r\n";
  1131. //char dettagli1[] = "Range: bytes=0-20\r\n";
  1132.  
  1133. strcat(msg_enter,fileremoto);
  1134. strcat(msg_enter,protocollo);
  1135. strcat(msg_enter,protocollo2);
  1136. strcat(msg_enter,host);
  1137. strcat(msg_enter,protocollo3);
  1138. //strcat(msg_enter,dettagli1); //header
  1139. strcat(msg_enter,"\r\n");
  1140.  
  1141.  
  1142. #ifdef console_mode
  1143. printf("Invio al server:\n\n%s\n", msg_enter);
  1144. #endif
  1145. send(s, msg_enter, strlen(msg_enter), 0); //invia dati
  1146.  
  1147. //orologio
  1148. clock_t start,end;
  1149. double tempo_trascorso = 0;
  1150. long cicli=0;
  1151. long cicli2 = 0;
  1152. int possib = 0;
  1153. bool lentezza = 0;
  1154.  
  1155. long downloaded_bytes = 0;
  1156. char *download_buffer;
  1157. download_buffer = (char *) malloc(download_buffer_size * sizeof(char));
  1158.  
  1159. char *http_header;
  1160. long lhead = 0;
  1161. http_header = (char *) malloc(10000*sizeof(char));
  1162.  
  1163. while(downloaded_bytes = recv(s, msg_response, 1, 0) > 0){
  1164.  
  1165. http_header[lhead] = msg_response[0];
  1166. lhead++;
  1167.  
  1168. //controllo per non scrivere header nel file.
  1169. if (ultimo_carattere == 13 && msg_response[0] == 10 && invio==true) header=false;
  1170. ((ultimo_carattere == 13 && msg_response[0] == 10) || (ultimo_carattere==10 && invio))?invio=true:invio=false;
  1171. ultimo_carattere = msg_response[0];
  1172.  
  1173. if(!header) break; //ricevuto header esci dal ciclo
  1174.  
  1175. }
  1176. http_header[lhead] = '\0';
  1177.  
  1178. #ifdef console_mode
  1179. printf("Header risposta:\n\n%s\n", http_header);
  1180. #endif
  1181. free(http_header);
  1182.  
  1183. if(downloaded_bytes > 0)
  1184. while(downloaded_bytes = recv(s, msg_response, 1, 0) > 0){
  1185. if(cicli == 0 && msg_response[0] != '\x46'){
  1186. break;
  1187. }//se non ricevo header flv chiudo download
  1188. if( cicli>=9 || video_time == 0 ){
  1189. video_buffer[vid_byte_count] = msg_response[0];
  1190. vid_byte_count++;
  1191. break;
  1192. }
  1193. cicli++;
  1194. }
  1195.  
  1196. start = clock();
  1197. if(downloaded_bytes > 0){
  1198. if(cicli>=9 || video_time == 0) {
  1199. while(1){
  1200. downloaded_bytes = recv(s, download_buffer, download_buffer_size, 0); //0x0008
  1201. if(downloaded_bytes < 0){
  1202. int wsaerrore = WSAGetLastError();
  1203. #ifdef console_mode
  1204. printf("recv %d error code: %d\n", downloaded_bytes, wsaerrore);
  1205. #endif
  1206. }
  1207. if(downloaded_bytes <= 0) break;
  1208.  
  1209. if(video_buffer[vid_byte_count] != '\0'){
  1210. vid_byte_count = -1; //disattivo processo
  1211. break; //se i dati sono gia stati scritti mi fermo
  1212. }
  1213.  
  1214. for(x=0;x<downloaded_bytes;x++){
  1215. video_buffer[vid_byte_count] = download_buffer[x]; //scrivo dati nel video_buffer
  1216. vid_byte_count++;
  1217. }
  1218.  
  1219. cicli= cicli + downloaded_bytes; //byte del file scaricati in questa connessione
  1220.  
  1221. end = clock();
  1222. tempo_trascorso = ((double)(end-start))/CLOCKS_PER_SEC;
  1223. if( tempo_trascorso >= speed_info_time ){ //sistema di misurazione velocità
  1224. //printf("SPEED:%d kbs\n",((cicli-cicli2)/(speed_info_time*1024)));
  1225. *proxy_speed = ((double)(cicli-cicli2)/(tempo_trascorso*1024));
  1226.  
  1227. *video_current_point = vid_byte_count;
  1228.  
  1229. //limitatore di lentezza
  1230. //if(((cicli-cicli2)/(speed_info_time*1024)) < limite_speed) printf("SPEED: troppo lento %d avvertimento n %d\n",((cicli-cicli2)/(speed_info_time*1024)),possib);
  1231. if(*proxy_speed < limite_speed && possib >= possibilit_speed){ lentezza=1; break; } //printf("SPEED: disconnesso per lentezza!!\n");
  1232. if(*proxy_speed < limite_speed && possib < possibilit_speed) possib++;
  1233. if(*proxy_speed >= limite_speed) possib=0;
  1234.  
  1235. //reset control
  1236. if(*reset == 1){
  1237. *reset = 0;
  1238. break;
  1239. }
  1240.  
  1241. start = clock();
  1242. cicli2 = cicli;
  1243. }
  1244. }
  1245. }
  1246. }
  1247.  
  1248. //printf("recv fine!\n");
  1249. *proxy_speed = 0; //azzero la velocità
  1250.  
  1251. //cancello ultimi 200 byte
  1252. long j,bersaglio;
  1253. if(video_lunghezza > vid_byte_count && vid_byte_count>=0){
  1254. if(vid_byte_count-200 < video_time)
  1255. bersaglio = video_time;
  1256. else
  1257. bersaglio = vid_byte_count-200;
  1258.  
  1259. for(j=vid_byte_count;j>=bersaglio;j--)
  1260. video_buffer[j] = '\0';
  1261.  
  1262. vid_byte_count=bersaglio;
  1263. }
  1264.  
  1265.  
  1266. }//fine if(select())
  1267. else{ //timeout
  1268. #ifdef console_mode
  1269. printf("Connessione timeout!\n");
  1270. #endif
  1271. return vid_byte_count;
  1272. }
  1273.  
  1274. //operazioni di chiusura!
  1275. shutdown(s, 2);
  1276. closesocket(s);
  1277. WSACleanup();
  1278. return vid_byte_count;
  1279. }
  1280.  
  1281. //********************************************************************//
  1282. int mv_decode(char *string, long key1, long key2){
  1283.  
  1284. int string_len = strlen(string);
  1285.  
  1286. char *decoded;
  1287. decoded = (char *) malloc(200 * sizeof(char));
  1288. char *binblock;
  1289. binblock = (char *) malloc(200);
  1290. binblock[0] = '\0';
  1291.  
  1292. int i;
  1293. for( i=0; i < string_len; ++i ){
  1294. switch( string[i] ){
  1295. case '0':
  1296. strcat(binblock,"0000");
  1297. break;
  1298. case '1':
  1299. strcat(binblock,"0001");
  1300. break;
  1301. case '2':
  1302. strcat(binblock,"0010");
  1303. break;
  1304. case '3':
  1305. strcat(binblock,"0011");
  1306. break;
  1307. case '4':
  1308. strcat(binblock,"0100");
  1309. break;
  1310. case '5':
  1311. strcat(binblock,"0101");
  1312. break;
  1313. case '6':
  1314. strcat(binblock,"0110");
  1315. break;
  1316. case '7':
  1317. strcat(binblock,"0111");
  1318. break;
  1319. case '8':
  1320. strcat(binblock,"1000");
  1321. break;
  1322. case '9':
  1323. strcat(binblock,"1001");
  1324. break;
  1325. case 'a':
  1326. strcat(binblock,"1010");
  1327. break;
  1328. case 'b':
  1329. strcat(binblock,"1011");
  1330. break;
  1331. case 'c':
  1332. strcat(binblock,"1100");
  1333. break;
  1334. case 'd':
  1335. strcat(binblock,"1101");
  1336. break;
  1337. case 'e':
  1338. strcat(binblock,"1110");
  1339. break;
  1340. case 'f':
  1341. strcat(binblock,"1111");
  1342. break;
  1343. }
  1344. }
  1345.  
  1346. long *ciphers;
  1347. ciphers = (long *) malloc(384 * sizeof(long));
  1348. for( i = 0; i < 384; ++i ){
  1349. key1 = (key1 * 11 + 77213) % 81371;
  1350. key2 = (key2 * 17 + 92717) % 192811;
  1351. ciphers[i] = (key1 + key2) % 128;
  1352. }
  1353.  
  1354. long cipher,offset,tmp;
  1355. for( i = 256; i >= 0; --i ){
  1356. cipher = ciphers[i];
  1357. offset = i % 128;
  1358. tmp = binblock[cipher];
  1359. binblock[cipher] = binblock[offset];
  1360. binblock[offset] = tmp;
  1361. }
  1362.  
  1363. for( i = 0; i < 128; ++i ){
  1364. binblock[i] = binblock[i] ^ ciphers[i + 256] & 1;
  1365. }
  1366.  
  1367. int a=0;
  1368. for(i=0;i < string_len*4 ; i=i+4 ){
  1369.  
  1370. if(binblock[i]== '0' &&
  1371. binblock[i+1]== '0' &&
  1372. binblock[i+2]== '0' &&
  1373. binblock[i+3]== '0' ) decoded[a] = '0';
  1374.  
  1375. else if(binblock[i]== '0' &&
  1376. binblock[i+1]== '0' &&
  1377. binblock[i+2]== '0' &&
  1378. binblock[i+3]== '1' ) decoded[a] = '1';
  1379.  
  1380. else if(binblock[i]== '0' &&
  1381. binblock[i+1]== '0' &&
  1382. binblock[i+2]== '1' &&
  1383. binblock[i+3]== '0' ) decoded[a] = '2';
  1384.  
  1385. else if(binblock[i]== '0' &&
  1386. binblock[i+1]== '0' &&
  1387. binblock[i+2]== '1' &&
  1388. binblock[i+3]== '1' ) decoded[a] = '3';
  1389.  
  1390. else if(binblock[i]== '0' &&
  1391. binblock[i+1]== '1' &&
  1392. binblock[i+2]== '0' &&
  1393. binblock[i+3]== '0' ) decoded[a] = '4';
  1394.  
  1395. else if(binblock[i]== '0' &&
  1396. binblock[i+1]== '1' &&
  1397. binblock[i+2]== '0' &&
  1398. binblock[i+3]== '1' ) decoded[a] = '5';
  1399.  
  1400. else if(binblock[i]== '0' &&
  1401. binblock[i+1]== '1' &&
  1402. binblock[i+2]== '1' &&
  1403. binblock[i+3]== '0' ) decoded[a] = '6';
  1404.  
  1405. else if(binblock[i]== '0' &&
  1406. binblock[i+1]== '1' &&
  1407. binblock[i+2]== '1' &&
  1408. binblock[i+3]== '1' ) decoded[a] = '7';
  1409.  
  1410. else if(binblock[i]== '1' &&
  1411. binblock[i+1]== '0' &&
  1412. binblock[i+2]== '0' &&
  1413. binblock[i+3]== '0' ) decoded[a] = '8';
  1414.  
  1415. else if(binblock[i]== '1' &&
  1416. binblock[i+1]== '0' &&
  1417. binblock[i+2]== '0' &&
  1418. binblock[i+3]== '1' ) decoded[a] = '9';
  1419.  
  1420. else if(binblock[i]== '1' &&
  1421. binblock[i+1]== '0' &&
  1422. binblock[i+2]== '1' &&
  1423. binblock[i+3]== '0' ) decoded[a] = 'a';
  1424.  
  1425. else if(binblock[i]== '1' &&
  1426. binblock[i+1]== '0' &&
  1427. binblock[i+2]== '1' &&
  1428. binblock[i+3]== '1' ) decoded[a] = 'b';
  1429.  
  1430. else if(binblock[i]== '1' &&
  1431. binblock[i+1]== '1' &&
  1432. binblock[i+2]== '0' &&
  1433. binblock[i+3]== '0' ) decoded[a] = 'c';
  1434.  
  1435. else if(binblock[i]== '1' &&
  1436. binblock[i+1]== '1' &&
  1437. binblock[i+2]== '0' &&
  1438. binblock[i+3]== '1' ) decoded[a] = 'd';
  1439.  
  1440. else if(binblock[i]== '1' &&
  1441. binblock[i+1]== '1' &&
  1442. binblock[i+2]== '1' &&
  1443. binblock[i+3]== '0' ) decoded[a] = 'e';
  1444.  
  1445. else if(binblock[i]== '1' &&
  1446. binblock[i+1]== '1' &&
  1447. binblock[i+2]== '1' &&
  1448. binblock[i+3]== '1' ) decoded[a] = 'f';
  1449. else return 0;
  1450. decoded[a+1] = '\0';
  1451. a++;
  1452.  
  1453. }
  1454.  
  1455. for(i=0;i<string_len;i++)string[i]=decoded[i];
  1456. free(binblock);
  1457. free(decoded);
  1458. return 1;
  1459.  
  1460. }
  1461.  
  1462. //********************************************************************//
  1463. char * read_clipboard(){
  1464.  
  1465. HGLOBAL clip;
  1466. char *str;
  1467.  
  1468. if (!IsClipboardFormatAvailable(CF_TEXT))
  1469. return 0;
  1470.  
  1471. OpenClipboard(NULL);
  1472.  
  1473. clip = GetClipboardData(CF_TEXT);
  1474. str = (char *) GlobalLock(clip);
  1475.  
  1476. CloseClipboard();
  1477.  
  1478. return str;
  1479. }
  1480.  
  1481.  
  1482.  
  1483. int book_manager(){
  1484. long x;
  1485. for(x=0;x<console_book_size;x++){
  1486. if(console_booking[x]==0){
  1487.  
  1488. GtkTextMark *mark;
  1489. GtkTextIter console_end;
  1490.  
  1491. gtk_text_buffer_get_end_iter(textview_console_buffer, &console_end);
  1492. gtk_text_buffer_insert(textview_console_buffer, &console_end, console_book[x], -1);
  1493.  
  1494. gtk_text_buffer_place_cursor(textview_console_buffer, &console_end);
  1495.  
  1496. mark = gtk_text_buffer_get_insert(textview_console_buffer);
  1497. gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(textview_console), mark, 0.0, TRUE, 0.0, 1.0);
  1498.  
  1499. console_booking[x] = 1;
  1500.  
  1501. }
  1502. }
  1503. return 1;
  1504.  
  1505. }
  1506. void textview_printf(char *string){
  1507.  
  1508.  
  1509. long x;
  1510. for(x=0;x<console_book_size;x++){
  1511. if(console_booking[x] == 1){
  1512. console_booking[x] = 0;
  1513. sprintf(console_book[x],"%s",string);
  1514. break;
  1515. }
  1516. if(x == console_book_size-1)
  1517. x=0;
  1518. }
  1519.  
  1520.  
  1521. }
  1522.  
  1523.  
Versione sito: 1.03.01 by mamo139. - Tutti i sorgenti presenti su questo sito sono rilasciati sotto licenza "GNU GPL version 3".