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

<< MV Smart Downloader >>

Torna indietro



Versione: 3.04.01 Windows 32

Screenshots:







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