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
|
/*
* apt-spy (c) Steven Holmes, 2003.
* This software is licensed as detailed in the COPYRIGHT file
*/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include "include/parse.h"
#include "include/benchmark.h"
#include "include/protocols.h"
/*
* It is safer to keep track of the total amount of data read ourselves.
* This fixes a bug appearing when libcurl would not reset its own byte counter
*/
static size_t total_bytes = 0;
int benchmark(server_t *current, char *proxy, int timeout, char *file)
{
CURL *curl;
printf("\nSERVER:\t%s\n", current->hostname);
/* We use libcurl - here we setup some global options */
curl = curl_easy_init();
if (curl == NULL)
return 1;
/* Set proxy server to use */
if (proxy != NULL)
if (curl_easy_setopt(curl, CURLOPT_PROXY, proxy) != 0)
return 1;
/* Connection timeout */
if (curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, (long) timeout) != 0)
return 1;
/* Total timeout */
if (curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long) timeout) != 0)
return 1;
/* Select callback function to deal with data. */
if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, null_write_function) != 0)
return 1;
/* Turn off libcurl's progress indicator */
if (curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1) != 0)
return 1;
/* Fail on error */
if (curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1) != 0)
return 1;
/* Test for an FTP entry */
if (strlen(current->path[FTP]) != 0) {
printf("Benchmarking FTP...\n");
get_file(current, curl, file, FTP, &total_bytes);
}
/* Test for an HTTP entry */
if (strlen(current->path[HTTP]) != 0) {
printf("Benchmarking HTTP...\n");
get_file(current, curl, file, HTTP, &total_bytes);
}
curl_easy_cleanup(curl);
return 0;
}
void decide_best(server_t *current, server_t *best)
{
int i, j;
/* Inefficient sorting algorithm, but small number of entries so it doesn't matter. */
/* move 'i' to the correct place in the array to place the new entry */
for (i = 0; i < BESTNUMBER; ++i)
if (current->stats.speed > best[i].stats.speed)
break;
/* shove everything along one */
for (j = BESTNUMBER - 2; j >= i; --j)
memcpy(&best[j + 1], &best[j], sizeof(server_t));
/* copy the new entry into the correct place */
memcpy(&best[i], current, sizeof(server_t));
}
/* Callback from libcurl to allow us to save data. We discard it and return success. */
size_t null_write_function(double *bytes, size_t size, size_t nmemb, void *stream)
{
/* We update total_bytes */
total_bytes += size * nmemb;
return size * nmemb;
}
|