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

<< MV Smart Downloader >>

Torna indietro



Versione: 3.03.02 Win32 & GNU/Linux

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