forked from angelj-a/axel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axel.c
628 lines (550 loc) · 17 KB
/
axel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/********************************************************************\
* Axel -- A lighter download accelerator for Linux and other Unices. *
* *
* Copyright 2001 Wilmer van der Gaast *
\********************************************************************/
/* Main control */
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License with
the Debian GNU/Linux distribution in file /usr/doc/copyright/GPL;
if not, write to the Free Software Foundation, Inc., 59 Temple Place,
Suite 330, Boston, MA 02111-1307 USA
*/
#include "axel.h"
/* Axel */
static void save_state( axel_t *axel );
static void *setup_thread( void * );
static void axel_message( axel_t *axel, char *format, ... );
static void axel_divide( axel_t *axel );
static char *buffer = NULL;
/* Create a new axel_t structure */
axel_t *axel_new( conf_t *conf, int count, void *url )
{
search_t *res;
axel_t *axel;
url_t *u;
char *s;
int i;
axel = malloc( sizeof( axel_t ) );
memset( axel, 0, sizeof( axel_t ) );
*axel->conf = *conf;
axel->conn = malloc( sizeof( conn_t ) * axel->conf->num_connections );
memset( axel->conn, 0, sizeof( conn_t ) * axel->conf->num_connections );
if( axel->conf->max_speed > 0 )
{
if( (float) axel->conf->max_speed / axel->conf->buffer_size < 0.5 )
{
if( axel->conf->verbose >= 2 )
axel_message( axel, _("Buffer resized for this speed.") );
axel->conf->buffer_size = axel->conf->max_speed;
}
axel->delay_time = (int) ( (float) 1000000 / axel->conf->max_speed * axel->conf->buffer_size * axel->conf->num_connections );
}
if( buffer == NULL )
buffer = malloc( max( MAX_STRING, axel->conf->buffer_size ) );
if( count == 0 )
{
axel->url = malloc( sizeof( url_t ) );
axel->url->next = axel->url;
strncpy( axel->url->text, (char *) url, MAX_STRING );
}
else
{
res = (search_t *) url;
u = axel->url = malloc( sizeof( url_t ) );
for( i = 0; i < count; i ++ )
{
strncpy( u->text, res[i].url, MAX_STRING );
if( i < count - 1 )
{
u->next = malloc( sizeof( url_t ) );
u = u->next;
}
else
{
u->next = axel->url;
}
}
}
axel->conn[0].conf = axel->conf;
if( !conn_set( &axel->conn[0], axel->url->text ) )
{
axel_message( axel, _("Could not parse URL.\n") );
axel->ready = -1;
return( axel );
}
axel->conn[0].local_if = axel->conf->interfaces->text;
axel->conf->interfaces = axel->conf->interfaces->next;
strncpy( axel->filename, axel->conn[0].file, MAX_STRING );
http_decode( axel->filename );
if( *axel->filename == 0 ) /* Index page == no fn */
strncpy( axel->filename, axel->conf->default_filename, MAX_STRING );
if( ( s = strchr( axel->filename, '?' ) ) != NULL && axel->conf->strip_cgi_parameters )
*s = 0; /* Get rid of CGI parameters */
if( !conn_init( &axel->conn[0] ) )
{
axel_message( axel, axel->conn[0].message );
axel->ready = -1;
return( axel );
}
/* This does more than just checking the file size, it all depends
on the protocol used. */
if( !conn_info( &axel->conn[0] ) )
{
axel_message( axel, axel->conn[0].message );
axel->ready = -1;
return( axel );
}
s = conn_url( axel->conn );
strncpy( axel->url->text, s, MAX_STRING );
if( ( axel->size = axel->conn[0].size ) != INT_MAX )
{
if( axel->conf->verbose > 0 )
axel_message( axel, _("File size: %lld bytes"), axel->size );
}
/* Wildcards in URL --> Get complete filename */
if( strchr( axel->filename, '*' ) || strchr( axel->filename, '?' ) )
strncpy( axel->filename, axel->conn[0].file, MAX_STRING );
return( axel );
}
/* Open a local file to store the downloaded data */
int axel_open( axel_t *axel )
{
int i, fd;
long long int j;
if( axel->conf->verbose > 0 )
axel_message( axel, _("Opening output file %s"), axel->filename );
snprintf( buffer, MAX_STRING, "%s.st", axel->filename );
axel->outfd = -1;
/* Check whether server knows about RESTart and switch back to
single connection download if necessary */
if( !axel->conn[0].supported )
{
axel_message( axel, _("Server unsupported, "
"starting from scratch with one connection.") );
axel->conf->num_connections = 1;
axel->conn = realloc( axel->conn, sizeof( conn_t ) );
axel_divide( axel );
}
else if( ( fd = open( buffer, O_RDONLY ) ) != -1 )
{
read( fd, &axel->conf->num_connections, sizeof( axel->conf->num_connections ) );
axel->conn = realloc( axel->conn, sizeof( conn_t ) * axel->conf->num_connections );
memset( axel->conn + 1, 0, sizeof( conn_t ) * ( axel->conf->num_connections - 1 ) );
axel_divide( axel );
read( fd, &axel->bytes_done, sizeof( axel->bytes_done ) );
for( i = 0; i < axel->conf->num_connections; i ++ )
read( fd, &axel->conn[i].currentbyte, sizeof( axel->conn[i].currentbyte ) );
axel_message( axel, _("State file found: %lld bytes downloaded, %lld to go."),
axel->bytes_done, axel->size - axel->bytes_done );
close( fd );
if( ( axel->outfd = open( axel->filename, O_WRONLY, 0666 ) ) == -1 )
{
axel_message( axel, _("Error opening local file") );
return( 0 );
}
}
/* If outfd == -1 we have to start from scrath now */
if( axel->outfd == -1 )
{
axel_divide( axel );
if( ( axel->outfd = open( axel->filename, O_CREAT | O_WRONLY, 0666 ) ) == -1 )
{
axel_message( axel, _("Error opening local file") );
return( 0 );
}
/* And check whether the filesystem can handle seeks to
past-EOF areas.. Speeds things up. :) AFAIK this
should just not happen: */
if( lseek( axel->outfd, axel->size, SEEK_SET ) == -1 && axel->conf->num_connections > 1 )
{
/* But if the OS/fs does not allow to seek behind
EOF, we have to fill the file with zeroes before
starting. Slow.. */
axel_message( axel, _("Crappy filesystem/OS.. Working around. :-(") );
lseek( axel->outfd, 0, SEEK_SET );
memset( buffer, 0, axel->conf->buffer_size );
j = axel->size;
while( j > 0 )
{
write( axel->outfd, buffer, min( j, axel->conf->buffer_size ) );
j -= axel->conf->buffer_size;
}
}
}
return( 1 );
}
/* Start downloading */
void axel_start( axel_t *axel )
{
int i;
url_t* next_u = axel->url->next;
url_t* current_u = axel->url;
/* HTTP might've redirected and FTP handles wildcards, so
re-scan the URL for every conn */
for( i = 0; i < axel->conf->num_connections; i ++ )
{
conn_set( &axel->conn[i], current_u->text );
current_u = next_u;
next_u = next_u->next;
axel->conn[i].local_if = axel->conf->interfaces->text;
axel->conf->interfaces = axel->conf->interfaces->next;
axel->conn[i].conf = axel->conf;
if( i ) axel->conn[i].supported = 1;
}
if( axel->conf->verbose > 0 )
axel_message( axel, _("Starting download") );
for( i = 0; i < axel->conf->num_connections; i ++ )
if( axel->conn[i].currentbyte <= axel->conn[i].lastbyte )
{
if( axel->conf->verbose >= 2 )
{
axel_message( axel, _("Connection %i downloading from %s:%i using interface %s"),
i, axel->conn[i].host, axel->conn[i].port, axel->conn[i].local_if );
}
axel->conn[i].state = 1;
if( pthread_create( axel->conn[i].setup_thread, NULL, setup_thread, &axel->conn[i] ) != 0 )
{
axel_message( axel, _("pthread error!!!") );
axel->ready = -1;
}
else
{
axel->conn[i].last_transfer = gettime();
}
}
/* The real downloading will start now, so let's start counting */
axel->start_time = gettime();
axel->ready = 0;
}
/* Main 'loop' */
void axel_do( axel_t *axel )
{
fd_set fds[1];
int hifd, i;
long long int remaining,size;
struct timeval timeval[1];
/* Create statefile if necessary */
if( gettime() > axel->next_state )
{
save_state( axel );
axel->next_state = gettime() + axel->conf->save_state_interval;
}
/* Wait for data on (one of) the connections */
FD_ZERO( fds );
hifd = 0;
for( i = 0; i < axel->conf->num_connections; i ++ )
{
if( axel->conn[i].enabled )
FD_SET( axel->conn[i].fd, fds );
hifd = max( hifd, axel->conn[i].fd );
}
if( hifd == 0 )
{
/* No connections yet. Wait... */
usleep( 100000 );
goto conn_check;
}
else
{
timeval->tv_sec = 0;
timeval->tv_usec = 100000;
/* A select() error probably means it was interrupted
by a signal, or that something else's very wrong... */
if( select( hifd + 1, fds, NULL, NULL, timeval ) == -1 )
{
axel->ready = -1;
return;
}
}
/* Handle connections which need attention */
for( i = 0; i < axel->conf->num_connections; i ++ )
if( axel->conn[i].enabled ) {
if( FD_ISSET( axel->conn[i].fd, fds ) )
{
axel->conn[i].last_transfer = gettime();
size = read( axel->conn[i].fd, buffer, axel->conf->buffer_size );
if( size == -1 )
{
if( axel->conf->verbose )
{
axel_message( axel, _("Error on connection %i! "
"Connection closed"), i );
}
axel->conn[i].enabled = 0;
conn_disconnect( &axel->conn[i] );
continue;
}
else if( size == 0 )
{
if( axel->conf->verbose )
{
/* Only abnormal behaviour if: */
if( axel->conn[i].currentbyte < axel->conn[i].lastbyte && axel->size != INT_MAX )
{
axel_message( axel, _("Connection %i unexpectedly closed"), i );
}
else
{
axel_message( axel, _("Connection %i finished"), i );
}
}
if( !axel->conn[0].supported )
{
axel->ready = 1;
}
axel->conn[i].enabled = 0;
conn_disconnect( &axel->conn[i] );
continue;
}
/* remaining == Bytes to go */
remaining = axel->conn[i].lastbyte - axel->conn[i].currentbyte + 1;
if( remaining < size )
{
if( axel->conf->verbose )
{
axel_message( axel, _("Connection %i finished"), i );
}
axel->conn[i].enabled = 0;
conn_disconnect( &axel->conn[i] );
size = remaining;
/* Don't terminate, still stuff to write! */
}
/* This should always succeed.. */
lseek( axel->outfd, axel->conn[i].currentbyte, SEEK_SET );
if( write( axel->outfd, buffer, size ) != size )
{
axel_message( axel, _("Write error!") );
axel->ready = -1;
return;
}
axel->conn[i].currentbyte += size;
axel->bytes_done += size;
}
else
{
if( gettime() > axel->conn[i].last_transfer + axel->conf->connection_timeout )
{
if( axel->conf->verbose )
axel_message( axel, _("Connection %i timed out"), i );
conn_disconnect( &axel->conn[i] );
axel->conn[i].enabled = 0;
}
} }
if( axel->ready )
return;
conn_check:
/* Look for aborted connections and attempt to restart them. */
for( i = 0; i < axel->conf->num_connections; i ++ )
{
if( !axel->conn[i].enabled && axel->conn[i].currentbyte < axel->conn[i].lastbyte )
{
if( axel->conn[i].state == 0 )
{
// Wait for termination of this thread
pthread_join(*(axel->conn[i].setup_thread), NULL);
conn_set( &axel->conn[i], axel->url->text );
axel->url = axel->url->next;
/* axel->conn[i].local_if = axel->conf->interfaces->text;
axel->conf->interfaces = axel->conf->interfaces->next; */
if( axel->conf->verbose >= 2 )
axel_message( axel, _("Connection %i downloading from %s:%i using interface %s"),
i, axel->conn[i].host, axel->conn[i].port, axel->conn[i].local_if );
axel->conn[i].state = 1;
if( pthread_create( axel->conn[i].setup_thread, NULL, setup_thread, &axel->conn[i] ) == 0 )
{
axel->conn[i].last_transfer = gettime();
}
else
{
axel_message( axel, _("pthread error!!!") );
axel->ready = -1;
}
}
else
{
if( gettime() > axel->conn[i].last_transfer + axel->conf->reconnect_delay )
{
pthread_cancel( *axel->conn[i].setup_thread );
axel->conn[i].state = 0;
}
}
}
}
/* Calculate current average speed and finish_time */
axel->bytes_per_second = (int) ( (double) ( axel->bytes_done - axel->start_byte ) / ( gettime() - axel->start_time ) );
axel->finish_time = (int) ( axel->start_time + (double) ( axel->size - axel->start_byte ) / axel->bytes_per_second );
/* Check speed. If too high, delay for some time to slow things
down a bit. I think a 5% deviation should be acceptable. */
if( axel->conf->max_speed > 0 )
{
if( (float) axel->bytes_per_second / axel->conf->max_speed > 1.05 )
axel->delay_time += 10000;
else if( ( (float) axel->bytes_per_second / axel->conf->max_speed < 0.95 ) && ( axel->delay_time >= 10000 ) )
axel->delay_time -= 10000;
else if( ( (float) axel->bytes_per_second / axel->conf->max_speed < 0.95 ) )
axel->delay_time = 0;
usleep( axel->delay_time );
}
/* Ready? */
if( axel->bytes_done == axel->size )
axel->ready = 1;
}
/* Close an axel connection */
void axel_close( axel_t *axel )
{
int i;
message_t *m;
/* Terminate any thread still running */
for( i = 0; i < axel->conf->num_connections; i ++ )
/* don't try to kill non existing thread */
if ( *axel->conn[i].setup_thread != 0 )
{
pthread_cancel( *axel->conn[i].setup_thread );
pthread_detach( *axel->conn[i].setup_thread );
}
/* Delete state file if necessary */
if( axel->ready == 1 )
{
snprintf( buffer, MAX_STRING, "%s.st", axel->filename );
unlink( buffer );
}
/* Else: Create it.. */
else if( axel->bytes_done > 0 )
{
save_state( axel );
}
/* Delete any message not processed yet */
while( axel->message )
{
m = axel->message;
axel->message = axel->message->next;
free( m );
}
/* Close all connections and local file */
close( axel->outfd );
for( i = 0; i < axel->conf->num_connections; i ++ )
conn_disconnect( &axel->conn[i] );
free_axel_t( axel );
free( buffer );
}
/* time() with more precision */
double gettime()
{
struct timeval time[1];
gettimeofday( time, 0 );
return( (double) time->tv_sec + (double) time->tv_usec / 1000000 );
}
/* Save the state of the current download */
void save_state( axel_t *axel )
{
int fd, i;
char fn[MAX_STRING+4];
/* No use for such a file if the server doesn't support
resuming anyway.. */
if( !axel->conn[0].supported )
return;
snprintf( fn, MAX_STRING, "%s.st", axel->filename );
if( ( fd = open( fn, O_CREAT | O_TRUNC | O_WRONLY, 0666 ) ) == -1 )
{
return; /* Not 100% fatal.. */
}
write( fd, &axel->conf->num_connections, sizeof( axel->conf->num_connections ) );
write( fd, &axel->bytes_done, sizeof( axel->bytes_done ) );
for( i = 0; i < axel->conf->num_connections; i ++ )
{
write( fd, &axel->conn[i].currentbyte, sizeof( axel->conn[i].currentbyte ) );
}
close( fd );
}
/* Thread used to set up a connection */
void *setup_thread( void *c )
{
conn_t *conn = c;
int oldstate;
/* Allow this thread to be killed at any time. */
pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, &oldstate );
pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate );
if( conn_setup( conn ) )
{
conn->last_transfer = gettime();
if( conn_exec( conn ) )
{
conn->last_transfer = gettime();
conn->enabled = 1;
conn->state = 0;
return( NULL );
}
}
conn_disconnect( conn );
conn->state = 0;
return( NULL );
}
/* Add a message to the axel->message structure */
static void axel_message( axel_t *axel, char *format, ... )
{
message_t *m = malloc( sizeof( message_t ) ), *n = axel->message;
va_list params;
memset( m, 0, sizeof( message_t ) );
va_start( params, format );
vsnprintf( m->text, MAX_STRING, format, params );
va_end( params );
if( axel->message == NULL )
{
axel->message = m;
}
else
{
while( n->next != NULL )
n = n->next;
n->next = m;
}
}
/* Divide the file and set the locations for each connection */
static void axel_divide( axel_t *axel )
{
int i;
axel->conn[0].currentbyte = 0;
axel->conn[0].lastbyte = axel->size / axel->conf->num_connections - 1;
for( i = 1; i < axel->conf->num_connections; i ++ )
{
#ifdef DEBUG
printf( "Downloading %lld-%lld using conn. %i\n", axel->conn[i-1].currentbyte, axel->conn[i-1].lastbyte, i - 1 );
#endif
axel->conn[i].currentbyte = axel->conn[i-1].lastbyte + 1;
axel->conn[i].lastbyte = axel->conn[i].currentbyte + axel->size / axel->conf->num_connections;
}
axel->conn[axel->conf->num_connections-1].lastbyte = axel->size - 1;
#ifdef DEBUG
printf( "Downloading %lld-%lld using conn. %i\n", axel->conn[i-1].currentbyte, axel->conn[i-1].lastbyte, i - 1 );
#endif
}
void free_axel_t ( axel_t *axel )
{
url_t *current_u = axel->url;
url_t *n_u = axel->url->next;
while ( current_u != axel->url ) {
free( current_u );
current_u = n_u;
n_u = n_u->next;
}
free( axel->url );
if_t *current_if = axel->conf->interfaces;
if_t *n_if = axel->conf->interfaces->next;
while ( current_if != axel->conf->interfaces ) {
free( current_if );
current_if = n_if;
n_if = n_if->next;
}
free( axel->conf->interfaces );
free( axel->conn );
free( axel );
}