2002-06-10 06:14:18 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
|
|
|
// under the License.
|
|
|
|
//
|
2002-08-07 22:52:10 +00:00
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
2002-06-10 06:14:18 +00:00
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2002-08-09 21:43:19 +00:00
|
|
|
#include "windows_cpp.h"
|
|
|
|
|
2002-08-07 22:52:10 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "client_state.h"
|
|
|
|
#include "client_types.h"
|
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "file_names.h"
|
|
|
|
#include "log_flags.h"
|
|
|
|
#include "parse.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2002-06-10 06:14:18 +00:00
|
|
|
// PERS_FILE_XFER represents a persistent file transfer.
|
2002-08-20 00:30:13 +00:00
|
|
|
// A set of URLs is given.
|
2003-02-26 00:47:57 +00:00
|
|
|
//
|
2002-06-10 06:14:18 +00:00
|
|
|
// For download, the object attempts to download the file
|
|
|
|
// from any of the URLs.
|
|
|
|
// If one fails or is not available, try another,
|
|
|
|
// using an exponential backoff policy to avoid flooding servers.
|
2003-02-26 00:47:57 +00:00
|
|
|
//
|
2002-06-10 06:14:18 +00:00
|
|
|
// For upload, try to upload the file to the first URL;
|
|
|
|
// if that fails try the others.
|
2003-02-04 21:47:12 +00:00
|
|
|
|
|
|
|
PERS_FILE_XFER::PERS_FILE_XFER() {
|
2002-08-07 22:52:10 +00:00
|
|
|
nretry = 0;
|
2002-12-06 07:33:45 +00:00
|
|
|
first_request_time = time(0);
|
2002-08-07 22:52:10 +00:00
|
|
|
next_request_time = first_request_time;
|
2003-02-06 03:39:10 +00:00
|
|
|
time_so_far = 0;
|
2003-02-04 21:47:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int PERS_FILE_XFER::init(FILE_INFO* f, bool is_file_upload) {
|
|
|
|
fxp = NULL;
|
|
|
|
fip = f;
|
2002-08-07 22:52:10 +00:00
|
|
|
is_upload = is_file_upload;
|
|
|
|
xfer_done = false;
|
2002-06-10 06:14:18 +00:00
|
|
|
|
2002-08-07 22:52:10 +00:00
|
|
|
return 0;
|
2002-06-10 06:14:18 +00:00
|
|
|
}
|
|
|
|
|
2002-08-07 22:52:10 +00:00
|
|
|
// Try to start the file transfer associated with this persistent file transfer.
|
|
|
|
// Returns true if transfer was successfully started, false otherwise
|
|
|
|
//
|
|
|
|
bool PERS_FILE_XFER::start_xfer() {
|
|
|
|
FILE_XFER *file_xfer;
|
|
|
|
int retval;
|
2002-11-22 23:25:19 +00:00
|
|
|
struct tm *newtime;
|
2003-02-26 00:47:57 +00:00
|
|
|
time_t now;
|
|
|
|
|
2003-02-27 09:21:22 +00:00
|
|
|
now = time(0);
|
2002-08-07 22:52:10 +00:00
|
|
|
|
|
|
|
// Decide whether to start a new file transfer
|
2002-08-14 20:31:27 +00:00
|
|
|
//
|
2002-08-21 19:12:42 +00:00
|
|
|
if (!gstate.start_new_file_xfer()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new FILE_XFER object and initialize a
|
|
|
|
// download or upload for the persistent file transfer
|
|
|
|
//
|
|
|
|
file_xfer = new FILE_XFER;
|
2002-12-19 00:10:39 +00:00
|
|
|
if (gstate.use_http_proxy) {
|
|
|
|
file_xfer->use_http_proxy = true;
|
2002-12-09 09:36:40 +00:00
|
|
|
strcpy(file_xfer->proxy_server_name, gstate.proxy_server_name);
|
|
|
|
file_xfer->proxy_server_port = gstate.proxy_server_port;
|
|
|
|
}
|
2002-08-21 19:12:42 +00:00
|
|
|
if (is_upload) {
|
|
|
|
retval = file_xfer->init_upload(*fip);
|
|
|
|
} else {
|
|
|
|
retval = file_xfer->init_download(*fip);
|
|
|
|
}
|
|
|
|
if (retval) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "couldn't start %s for %s: error %d\n",
|
|
|
|
(is_upload ? "upload" : "download"), fip->get_url(), retval
|
|
|
|
);
|
|
|
|
} else {
|
2002-08-26 22:57:17 +00:00
|
|
|
retval = gstate.file_xfers->insert(file_xfer);
|
2002-08-21 19:12:42 +00:00
|
|
|
fxp = file_xfer;
|
2003-02-04 21:47:12 +00:00
|
|
|
if (retval) {
|
|
|
|
if (log_flags.file_xfer) {
|
|
|
|
printf( "file_xfer insert failed\n" );
|
|
|
|
}
|
|
|
|
fxp->file_xfer_retval = retval;
|
2003-02-26 00:47:57 +00:00
|
|
|
handle_xfer_failure(now);
|
2003-02-04 21:47:12 +00:00
|
|
|
fxp = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
2002-08-21 19:12:42 +00:00
|
|
|
if (log_flags.file_xfer) {
|
2003-02-26 00:47:57 +00:00
|
|
|
now = time(0);
|
|
|
|
newtime = localtime(&now);
|
2002-08-21 19:12:42 +00:00
|
|
|
printf(
|
2002-11-22 23:25:19 +00:00
|
|
|
"started %s of %s to %s at time: %s\n",
|
2003-02-26 00:47:57 +00:00
|
|
|
(is_upload ? "upload" : "download"), fip->name, fip->get_url(),
|
|
|
|
asctime(newtime)
|
2002-08-20 00:30:13 +00:00
|
|
|
);
|
2002-08-07 22:52:10 +00:00
|
|
|
}
|
2002-08-21 19:12:42 +00:00
|
|
|
return true;
|
2002-08-07 22:52:10 +00:00
|
|
|
}
|
|
|
|
return false;
|
2002-06-10 06:14:18 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 20:31:27 +00:00
|
|
|
// Poll the status of this persistent file transfer.
|
|
|
|
// If it's time to start it, then attempt to start it.
|
|
|
|
// If it has finished or failed, then deal with it appropriately
|
2002-08-07 22:52:10 +00:00
|
|
|
//
|
2002-08-12 00:51:42 +00:00
|
|
|
bool PERS_FILE_XFER::poll(unsigned int now) {
|
2002-08-07 22:52:10 +00:00
|
|
|
int retval;
|
|
|
|
char pathname[256];
|
2002-08-21 19:12:42 +00:00
|
|
|
|
2002-08-22 00:16:04 +00:00
|
|
|
if (xfer_done) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!fxp) {
|
2002-08-14 20:31:27 +00:00
|
|
|
// No file xfer is active.
|
|
|
|
// We must be waiting after a failure.
|
|
|
|
// See if it's time to try again.
|
|
|
|
//
|
|
|
|
if (now >= (unsigned int)next_request_time) {
|
2003-02-04 21:47:12 +00:00
|
|
|
bool ok = start_xfer();
|
|
|
|
last_time = dtime();
|
|
|
|
return ok;
|
2002-08-14 20:31:27 +00:00
|
|
|
} else {
|
2002-08-20 00:30:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-08-14 20:31:27 +00:00
|
|
|
}
|
2002-08-07 22:52:10 +00:00
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
if (dtime() - last_time <= 2) {
|
2003-02-04 21:47:12 +00:00
|
|
|
time_so_far += dtime() - last_time;
|
|
|
|
}
|
|
|
|
last_time = dtime();
|
2003-02-03 22:47:08 +00:00
|
|
|
|
2002-08-14 20:31:27 +00:00
|
|
|
if (fxp->file_xfer_done) {
|
2002-08-21 19:12:42 +00:00
|
|
|
if (log_flags.file_xfer) {
|
2002-08-28 21:50:51 +00:00
|
|
|
printf(
|
|
|
|
"file transfer done for %s; retval %d\n",
|
|
|
|
fip->get_url(), fxp->file_xfer_retval
|
|
|
|
);
|
2002-08-21 19:12:42 +00:00
|
|
|
}
|
2002-08-20 00:30:13 +00:00
|
|
|
if (fxp->file_xfer_retval == 0) {
|
2002-08-21 19:12:42 +00:00
|
|
|
// The transfer finished with no errors.
|
2003-02-26 00:47:57 +00:00
|
|
|
//
|
2002-08-20 00:30:13 +00:00
|
|
|
if (fip->generated_locally) {
|
2002-08-21 19:12:42 +00:00
|
|
|
// file has been uploaded - delete if not sticky
|
2003-02-26 00:47:57 +00:00
|
|
|
//
|
2002-08-21 19:12:42 +00:00
|
|
|
if (!fip->sticky) {
|
|
|
|
fip->delete_file();
|
2002-08-20 00:30:13 +00:00
|
|
|
}
|
2002-08-21 19:12:42 +00:00
|
|
|
fip->uploaded = true;
|
|
|
|
xfer_done = true;
|
2002-08-20 00:30:13 +00:00
|
|
|
} else {
|
2003-02-26 00:47:57 +00:00
|
|
|
|
|
|
|
// verify the file with RSA or MD5, and change permissions
|
|
|
|
//
|
2002-08-20 00:30:13 +00:00
|
|
|
get_pathname(fip, pathname);
|
|
|
|
retval = verify_downloaded_file(pathname, *fip);
|
|
|
|
if (retval) {
|
2003-02-26 00:47:57 +00:00
|
|
|
printf("checksum or signature error for %s\n", fip->name);
|
2002-08-20 00:30:13 +00:00
|
|
|
fip->status = retval;
|
|
|
|
} else {
|
|
|
|
if (log_flags.file_xfer_debug) {
|
|
|
|
printf("MD5 checksum validated for %s\n", pathname);
|
|
|
|
}
|
|
|
|
// Set the appropriate permissions depending on whether
|
|
|
|
// it's an executable or normal file
|
2003-02-26 00:47:57 +00:00
|
|
|
//
|
2002-08-21 19:12:42 +00:00
|
|
|
retval = fip->set_permissions();
|
2002-08-20 00:30:13 +00:00
|
|
|
fip->status = FILE_PRESENT;
|
|
|
|
}
|
2002-08-21 19:12:42 +00:00
|
|
|
xfer_done = true;
|
2002-08-20 00:30:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2002-08-21 19:12:42 +00:00
|
|
|
handle_xfer_failure(now);
|
2002-08-20 00:30:13 +00:00
|
|
|
}
|
2003-02-26 00:47:57 +00:00
|
|
|
// remove fxp from file_xfer_set and deallocate it
|
|
|
|
//
|
2002-08-21 19:12:42 +00:00
|
|
|
gstate.file_xfers->remove(fxp);
|
|
|
|
delete fxp;
|
2002-08-20 00:30:13 +00:00
|
|
|
fxp = NULL;
|
|
|
|
|
|
|
|
return true;
|
2002-06-10 06:14:18 +00:00
|
|
|
}
|
2002-08-07 22:52:10 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-08-21 19:12:42 +00:00
|
|
|
// Handle a transfer failure
|
|
|
|
//
|
|
|
|
void PERS_FILE_XFER::handle_xfer_failure(unsigned int cur_time) {
|
|
|
|
// If it was a bad range request, delete the file and start over
|
2002-08-26 22:57:17 +00:00
|
|
|
if (fxp->file_xfer_retval == HTTP_STATUS_RANGE_REQUEST_ERROR) {
|
2002-08-21 19:12:42 +00:00
|
|
|
fip->delete_file();
|
|
|
|
}
|
2003-02-04 21:47:12 +00:00
|
|
|
|
2002-08-21 19:12:42 +00:00
|
|
|
retry_and_backoff(cur_time);
|
|
|
|
|
|
|
|
// See if it's time to give up on the persistent file xfer
|
|
|
|
//
|
|
|
|
if ((cur_time - first_request_time) > gstate.giveup_after) {
|
2003-02-26 00:47:57 +00:00
|
|
|
if (is_upload) {
|
2003-02-04 21:47:12 +00:00
|
|
|
fip->status = ERR_GIVEUP_UPLOAD;
|
2003-02-26 00:47:57 +00:00
|
|
|
} else {
|
2003-02-04 21:47:12 +00:00
|
|
|
fip->status = ERR_GIVEUP_DOWNLOAD;
|
2003-02-26 00:47:57 +00:00
|
|
|
}
|
2003-02-04 21:47:12 +00:00
|
|
|
xfer_done = true;
|
2002-08-21 19:12:42 +00:00
|
|
|
}
|
2002-08-30 20:56:02 +00:00
|
|
|
if (log_flags.file_xfer_debug) {
|
|
|
|
printf("Error: transfer failure for %s: %d\n", fip->name, fip->status);
|
|
|
|
}
|
2002-08-21 19:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cycle to the next URL, or if we've hit all URLs in this cycle,
|
|
|
|
// backoff and try again later
|
|
|
|
//
|
|
|
|
int PERS_FILE_XFER::retry_and_backoff(unsigned int cur_time) {
|
|
|
|
double exp_backoff;
|
2002-11-22 23:25:19 +00:00
|
|
|
struct tm *newtime;
|
|
|
|
time_t aclock;
|
|
|
|
|
|
|
|
time( &aclock ); /* Get time in seconds */
|
2002-08-21 19:12:42 +00:00
|
|
|
|
2002-11-22 23:25:19 +00:00
|
|
|
newtime = localtime( &aclock ); /* Convert time to struct */
|
2003-02-26 00:47:57 +00:00
|
|
|
|
2002-08-21 19:12:42 +00:00
|
|
|
// Cycle to the next URL to try
|
2003-02-26 00:47:57 +00:00
|
|
|
//
|
2002-08-21 19:12:42 +00:00
|
|
|
fip->current_url = (fip->current_url + 1)%fip->urls.size();
|
|
|
|
|
|
|
|
// If we reach the URL that we started at, then we have tried all
|
|
|
|
// servers without success
|
2003-02-26 00:47:57 +00:00
|
|
|
//
|
2002-08-21 19:12:42 +00:00
|
|
|
if (fip->current_url == fip->start_url) {
|
|
|
|
nretry++;
|
|
|
|
exp_backoff = exp(((double)rand()/(double)RAND_MAX)*nretry);
|
2003-02-26 00:47:57 +00:00
|
|
|
|
|
|
|
// Do an exponential backoff of e^nretry seconds,
|
|
|
|
// keeping within the bounds of PERS_RETRY_DELAY_MIN and
|
2002-08-21 19:12:42 +00:00
|
|
|
// PERS_RETRY_DELAY_MAX
|
2003-02-26 00:47:57 +00:00
|
|
|
//
|
2002-08-21 19:12:42 +00:00
|
|
|
next_request_time = cur_time+(int)max(PERS_RETRY_DELAY_MIN,min(PERS_RETRY_DELAY_MAX,exp_backoff));
|
|
|
|
}
|
2002-11-22 23:25:19 +00:00
|
|
|
if (log_flags.file_xfer_debug) {
|
2003-02-26 00:47:57 +00:00
|
|
|
printf(
|
|
|
|
"exponential back off is %d, current_time is %s\n",
|
|
|
|
(int) exp_backoff,asctime(newtime)
|
|
|
|
);
|
2002-11-22 23:25:19 +00:00
|
|
|
}
|
2002-08-21 19:12:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-07 22:52:10 +00:00
|
|
|
// Parse XML information about a single persistent file transfer
|
|
|
|
//
|
|
|
|
int PERS_FILE_XFER::parse(FILE* fin) {
|
|
|
|
char buf[256];
|
2002-08-15 22:03:41 +00:00
|
|
|
|
2002-08-07 22:52:10 +00:00
|
|
|
while (fgets(buf, 256, fin)) {
|
|
|
|
if (match_tag(buf, "</persistent_file_xfer>")) return 0;
|
|
|
|
else if (parse_int(buf, "<num_retries>", nretry)) continue;
|
|
|
|
else if (parse_int(buf, "<first_request_time>", first_request_time)) continue;
|
|
|
|
else if (parse_int(buf, "<next_request_time>", next_request_time)) continue;
|
2003-02-03 22:47:08 +00:00
|
|
|
else if (parse_double(buf, "<time_so_far>", time_so_far)) continue;
|
2002-08-07 22:52:10 +00:00
|
|
|
else fprintf(stderr, "PERS_FILE_XFER::parse(): unrecognized: %s\n", buf);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write XML information about a particular persistent file transfer
|
|
|
|
//
|
|
|
|
int PERS_FILE_XFER::write(FILE* fout) {
|
|
|
|
fprintf(fout,
|
2002-08-20 00:30:13 +00:00
|
|
|
" <persistent_file_xfer>\n"
|
|
|
|
" <num_retries>%d</num_retries>\n"
|
|
|
|
" <first_request_time>%d</first_request_time>\n"
|
|
|
|
" <next_request_time>%d</next_request_time>\n"
|
2003-02-03 22:47:08 +00:00
|
|
|
" <time_so_far>%f</time_so_far>\n"
|
2002-08-20 00:30:13 +00:00
|
|
|
" </persistent_file_xfer>\n",
|
2003-02-03 22:47:08 +00:00
|
|
|
nretry, first_request_time, next_request_time, time_so_far
|
2002-08-14 20:31:27 +00:00
|
|
|
);
|
2002-08-07 22:52:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PERS_FILE_XFER_SET::PERS_FILE_XFER_SET(FILE_XFER_SET* p) {
|
|
|
|
file_xfers = p;
|
2002-06-10 06:14:18 +00:00
|
|
|
}
|
|
|
|
|
2002-08-21 19:12:42 +00:00
|
|
|
// Run through the set, starting any transfers that need to be
|
|
|
|
// started and deleting any that have finished
|
|
|
|
//
|
2002-08-12 00:51:42 +00:00
|
|
|
bool PERS_FILE_XFER_SET::poll() {
|
2002-08-07 22:52:10 +00:00
|
|
|
unsigned int i;
|
2002-06-10 06:14:18 +00:00
|
|
|
bool action = false;
|
2002-12-06 07:33:45 +00:00
|
|
|
int now = time(0);
|
2002-06-10 06:14:18 +00:00
|
|
|
|
|
|
|
for (i=0; i<pers_file_xfers.size(); i++) {
|
2002-08-07 22:52:10 +00:00
|
|
|
action |= pers_file_xfers[i]->poll(now);
|
|
|
|
}
|
|
|
|
|
2002-08-22 21:29:58 +00:00
|
|
|
if (action) gstate.set_client_state_dirty("pers_file_xfer_set poll");
|
2002-08-21 19:12:42 +00:00
|
|
|
|
2002-08-07 22:52:10 +00:00
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2002-08-14 20:31:27 +00:00
|
|
|
// Insert a PERS_FILE_XFER object into the set.
|
|
|
|
// We will decide which ones to start when we hit the polling loop
|
2002-08-07 22:52:10 +00:00
|
|
|
//
|
|
|
|
int PERS_FILE_XFER_SET::insert(PERS_FILE_XFER* pfx) {
|
|
|
|
pers_file_xfers.push_back(pfx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-14 20:31:27 +00:00
|
|
|
// Remove a PERS_FILE_XFER object from the set.
|
|
|
|
// What should the action here be?
|
2002-08-07 22:52:10 +00:00
|
|
|
//
|
|
|
|
int PERS_FILE_XFER_SET::remove(PERS_FILE_XFER* pfx) {
|
|
|
|
vector<PERS_FILE_XFER*>::iterator iter;
|
|
|
|
|
|
|
|
iter = pers_file_xfers.begin();
|
|
|
|
while (iter != pers_file_xfers.end()) {
|
|
|
|
if (*iter == pfx) {
|
|
|
|
pers_file_xfers.erase(iter);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
iter++;
|
2002-06-10 06:14:18 +00:00
|
|
|
}
|
2002-08-07 22:52:10 +00:00
|
|
|
fprintf(stderr, "PERS_FILE_XFER_SET::remove(): not found\n");
|
|
|
|
return 1;
|
2002-06-10 06:14:18 +00:00
|
|
|
}
|