2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// 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
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-07-10 21:50:54 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// 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
|
2003-07-10 21:50:54 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-07-10 21:50:54 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2003-10-16 19:03:49 +00:00
|
|
|
#include "cpp.h"
|
2002-06-06 18:50:12 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <stdio.h>
|
2003-02-24 23:59:46 +00:00
|
|
|
#include <math.h>
|
2002-06-06 18:50:12 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2003-02-20 00:12:30 +00:00
|
|
|
#include <afxwin.h>
|
|
|
|
#include <winsock.h>
|
2002-12-18 20:17:35 +00:00
|
|
|
#include "Win_net.h"
|
2003-02-20 00:12:30 +00:00
|
|
|
#include "wingui_mainwindow.h"
|
2002-07-15 23:21:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_SYS_TIME_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/time.h>
|
2002-07-15 23:21:20 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_SOCKET_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/socket.h>
|
2002-07-15 23:21:20 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_SELECT_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/select.h>
|
2002-07-15 23:21:20 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_NETINET_IN_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <netinet/in.h>
|
2002-07-15 23:21:20 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_NETINET_TCP_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <netinet/tcp.h>
|
2002-07-15 23:21:20 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_NETDB_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <netdb.h>
|
2002-07-15 23:21:20 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_UNISTD_H
|
2002-06-06 18:50:12 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2002-07-15 23:21:20 +00:00
|
|
|
#if HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2002-06-06 18:50:12 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2002-06-06 18:50:12 +00:00
|
|
|
#include <time.h>
|
2002-05-08 22:21:27 +00:00
|
|
|
#include <string.h>
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "net_xfer.h"
|
2003-03-08 23:48:05 +00:00
|
|
|
#include "util.h"
|
2003-04-01 03:18:12 +00:00
|
|
|
#include "client_types.h"
|
2003-06-03 22:47:15 +00:00
|
|
|
#include "client_state.h"
|
2003-04-01 03:18:12 +00:00
|
|
|
#include "message.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-07-11 00:50:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
typedef int socklen_t;
|
|
|
|
#elif !GETSOCKOPT_SOCKLEN_T
|
2003-10-29 08:02:11 +00:00
|
|
|
typedef size_t socklen_t;
|
2002-05-25 15:03:53 +00:00
|
|
|
#endif
|
|
|
|
|
2003-09-04 20:11:47 +00:00
|
|
|
int get_socket_error(int fd) {
|
|
|
|
socklen_t intsize = sizeof(int);
|
|
|
|
int n;
|
|
|
|
#ifdef _WIN32
|
|
|
|
getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&n, &intsize);
|
|
|
|
#elif __APPLE__
|
|
|
|
getsockopt(fd, SOL_SOCKET, SO_ERROR, &n, (int *)&intsize);
|
|
|
|
#else
|
|
|
|
getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&n, &intsize);
|
|
|
|
#endif
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2003-09-04 00:41:51 +00:00
|
|
|
int NET_XFER::get_ip_addr(char *hostname, int &ip_addr) {
|
2002-04-30 22:22:54 +00:00
|
|
|
hostent* hep;
|
|
|
|
|
2002-12-18 20:17:35 +00:00
|
|
|
#ifdef _WIN32
|
2003-10-21 04:06:55 +00:00
|
|
|
int retval;
|
|
|
|
retval = NetOpen();
|
|
|
|
if (retval) return retval;
|
2002-12-18 20:17:35 +00:00
|
|
|
#endif
|
2002-12-09 09:36:40 +00:00
|
|
|
hep = gethostbyname(hostname);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!hep) {
|
2003-06-11 23:25:54 +00:00
|
|
|
char msg[256];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = sprintf(msg, "Can't resolve hostname %s ", hostname);
|
2002-12-18 20:17:35 +00:00
|
|
|
#ifdef _WIN32
|
2003-06-11 23:25:54 +00:00
|
|
|
|
|
|
|
switch (WSAGetLastError()) {
|
|
|
|
case WSANOTINITIALISED:
|
|
|
|
break;
|
|
|
|
case WSAENETDOWN:
|
|
|
|
sprintf(msg+n, "(the network subsystem has failed)");
|
|
|
|
break;
|
|
|
|
case WSAHOST_NOT_FOUND:
|
|
|
|
sprintf(msg+n, "(host name not found)");
|
|
|
|
break;
|
|
|
|
case WSATRY_AGAIN:
|
|
|
|
sprintf(msg+n, "(no response from server)");
|
|
|
|
break;
|
|
|
|
case WSANO_RECOVERY:
|
|
|
|
sprintf(msg+n, "(a nonrecoverable error occurred)");
|
|
|
|
break;
|
|
|
|
case WSANO_DATA:
|
|
|
|
sprintf(msg+n, "(valid name, no data record of requested type)");
|
|
|
|
break;
|
|
|
|
case WSAEINPROGRESS:
|
|
|
|
sprintf(msg+n, "(a blocking socket call in progress)");
|
|
|
|
break;
|
|
|
|
case WSAEFAULT:
|
|
|
|
sprintf(msg+n, "(invalid part of user address space)");
|
|
|
|
break;
|
|
|
|
case WSAEINTR:
|
|
|
|
sprintf(msg+n, "(a blocking socket call was canceled)");
|
|
|
|
break;
|
|
|
|
}
|
2002-12-18 23:56:25 +00:00
|
|
|
NetClose();
|
2003-06-11 23:25:54 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2003-06-12 07:48:48 +00:00
|
|
|
switch (h_errno) {
|
2003-06-11 23:25:54 +00:00
|
|
|
case HOST_NOT_FOUND:
|
2003-09-04 00:41:51 +00:00
|
|
|
sprintf(msg+n, "(host not found)");
|
2003-06-11 23:25:54 +00:00
|
|
|
break;
|
|
|
|
case NO_DATA:
|
|
|
|
sprintf(msg+n, "(valid name, no data record of requested type)");
|
|
|
|
break;
|
|
|
|
case NO_RECOVERY:
|
|
|
|
sprintf(msg+n, "(a nonrecoverable error occurred)");
|
|
|
|
break;
|
|
|
|
case TRY_AGAIN:
|
2003-09-04 00:41:51 +00:00
|
|
|
sprintf(msg+n, "(host not found or server failure)");
|
2003-06-11 23:25:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-12-18 20:17:35 +00:00
|
|
|
#endif
|
2003-06-11 23:25:54 +00:00
|
|
|
msg_printf(0, MSG_ERROR, "%s\n", msg);
|
2002-04-30 22:22:54 +00:00
|
|
|
return ERR_GETHOSTBYNAME;
|
|
|
|
}
|
2002-12-18 23:56:25 +00:00
|
|
|
ip_addr = *(int*)hep->h_addr_list[0];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Attempt to open a nonblocking socket to a server
|
|
|
|
//
|
|
|
|
int NET_XFER::open_server() {
|
|
|
|
sockaddr_in addr;
|
|
|
|
int fd=0, ipaddr, retval=0;
|
|
|
|
|
|
|
|
retval = get_ip_addr(hostname, ipaddr);
|
|
|
|
if (retval) return retval;
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
fd = ::socket(AF_INET, SOCK_STREAM, 0);
|
2002-12-18 20:17:35 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
#ifdef _WIN32
|
2002-12-18 23:56:25 +00:00
|
|
|
NetClose();
|
2002-12-18 20:17:35 +00:00
|
|
|
#endif
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_SOCKET;
|
2003-02-26 00:47:57 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-06-06 18:50:12 +00:00
|
|
|
#ifdef _WIN32
|
2002-08-22 20:19:18 +00:00
|
|
|
unsigned long one = 1;
|
|
|
|
ioctlsocket(fd, FIONBIO, &one);
|
2002-06-06 18:50:12 +00:00
|
|
|
#else
|
|
|
|
int flags;
|
2002-04-30 22:22:54 +00:00
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
2003-10-23 06:29:24 +00:00
|
|
|
if (flags < 0) {
|
|
|
|
return ERR_FCNTL;
|
|
|
|
}
|
|
|
|
if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0 ) {
|
|
|
|
return ERR_FCNTL;
|
|
|
|
}
|
2002-06-06 18:50:12 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
addr.sin_addr.s_addr = ((long)ipaddr);
|
|
|
|
retval = connect(fd, (sockaddr*)&addr, sizeof(addr));
|
|
|
|
if (retval) {
|
2002-06-06 18:50:12 +00:00
|
|
|
#ifdef _WIN32
|
2002-06-21 00:22:59 +00:00
|
|
|
errno = WSAGetLastError();
|
|
|
|
if (errno != WSAEINPROGRESS && errno != WSAEWOULDBLOCK) {
|
2002-06-06 18:50:12 +00:00
|
|
|
closesocket(fd);
|
2003-02-06 19:08:18 +00:00
|
|
|
NetClose();
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_CONNECT;
|
2002-06-06 18:50:12 +00:00
|
|
|
}
|
2003-04-29 19:53:18 +00:00
|
|
|
#ifndef WIN_CLI
|
2003-03-04 00:43:54 +00:00
|
|
|
if (WSAAsyncSelect( fd, g_myWnd->GetSafeHwnd(), g_myWnd->m_nNetActivityMsg, FD_READ|FD_WRITE )) {
|
2003-02-26 00:47:57 +00:00
|
|
|
errno = WSAGetLastError();
|
|
|
|
if (errno != WSAEINPROGRESS && errno != WSAEWOULDBLOCK) {
|
|
|
|
closesocket(fd);
|
|
|
|
NetClose();
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_ASYNCSELECT;
|
2003-02-26 00:47:57 +00:00
|
|
|
}
|
2003-02-20 00:12:30 +00:00
|
|
|
}
|
2003-04-29 19:53:18 +00:00
|
|
|
#endif
|
2002-06-06 18:50:12 +00:00
|
|
|
#else
|
2002-04-30 22:22:54 +00:00
|
|
|
if (errno != EINPROGRESS) {
|
|
|
|
close(fd);
|
2002-06-21 18:31:32 +00:00
|
|
|
perror("connect");
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_CONNECT;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-06-06 18:50:12 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
|
|
|
is_connected = true;
|
|
|
|
}
|
|
|
|
socket = fd;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-22 20:19:18 +00:00
|
|
|
void NET_XFER::close_socket() {
|
2002-08-02 18:31:20 +00:00
|
|
|
#ifdef _WIN32
|
2003-02-26 00:47:57 +00:00
|
|
|
NetClose();
|
2002-08-02 18:31:20 +00:00
|
|
|
if (socket) closesocket(socket);
|
|
|
|
#else
|
|
|
|
if (socket) close(socket);
|
|
|
|
#endif
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
void NET_XFER::init(char* host, int p, int b) {
|
2002-07-15 23:21:20 +00:00
|
|
|
socket = -1;
|
2002-04-30 22:22:54 +00:00
|
|
|
is_connected = false;
|
|
|
|
want_download = false;
|
|
|
|
want_upload = false;
|
|
|
|
do_file_io = false;
|
|
|
|
io_done = false;
|
2002-07-15 23:21:20 +00:00
|
|
|
file = NULL;
|
2002-04-30 22:22:54 +00:00
|
|
|
io_ready = false;
|
|
|
|
error = 0;
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(hostname, host);
|
2002-07-15 23:21:20 +00:00
|
|
|
port = p;
|
2003-04-02 19:30:15 +00:00
|
|
|
blocksize = (b > MAX_BLOCKSIZE ? MAX_BLOCKSIZE : b);
|
2003-02-24 23:59:46 +00:00
|
|
|
xfer_speed = 0;
|
2003-02-26 07:45:21 +00:00
|
|
|
recent_bytes = 0;
|
2003-02-24 23:59:46 +00:00
|
|
|
last_speed_update = 0;
|
2003-04-02 19:30:15 +00:00
|
|
|
file_read_buf_offset = 0;
|
|
|
|
file_read_buf_len = 0;
|
2003-05-16 20:32:36 +00:00
|
|
|
bytes_xferred = 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
NET_XFER_SET::NET_XFER_SET() {
|
|
|
|
max_bytes_sec_up = 0;
|
|
|
|
max_bytes_sec_down = 0;
|
|
|
|
bytes_left_up = 0;
|
|
|
|
bytes_left_down = 0;
|
|
|
|
bytes_up = 0;
|
|
|
|
bytes_down = 0;
|
|
|
|
up_active = false;
|
|
|
|
down_active = false;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Insert a NET_XFER object into the set
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int NET_XFER_SET::insert(NET_XFER* nxp) {
|
|
|
|
int retval = nxp->open_server();
|
|
|
|
if (retval) return retval;
|
|
|
|
net_xfers.push_back(nxp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Remove a NET_XFER object from the set
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int NET_XFER_SET::remove(NET_XFER* nxp) {
|
|
|
|
vector<NET_XFER*>::iterator iter;
|
|
|
|
|
2002-08-02 18:31:20 +00:00
|
|
|
nxp->close_socket();
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
iter = net_xfers.begin();
|
|
|
|
while (iter != net_xfers.end()) {
|
|
|
|
if (*iter == nxp) {
|
|
|
|
net_xfers.erase(iter);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
}
|
2003-07-29 23:26:32 +00:00
|
|
|
msg_printf(NULL, MSG_ERROR, "NET_XFER_SET::remove(): not found\n");
|
2002-04-30 22:22:54 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-05-15 18:05:24 +00:00
|
|
|
// Transfer data to/from active sockets.
|
|
|
|
// Keep doing I/O until would block, or we hit rate limits,
|
2003-02-26 00:47:57 +00:00
|
|
|
// or about .5 second goes by
|
2002-08-22 20:19:18 +00:00
|
|
|
//
|
2003-02-26 00:47:57 +00:00
|
|
|
bool NET_XFER_SET::poll() {
|
|
|
|
double bytes_xferred;
|
|
|
|
int retval;
|
|
|
|
time_t t = time(0);
|
|
|
|
bool action = false;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
while (1) {
|
2003-09-07 03:11:03 +00:00
|
|
|
retval = do_select(bytes_xferred, 0);
|
2003-02-26 00:47:57 +00:00
|
|
|
if (retval) break;
|
|
|
|
if (bytes_xferred == 0) break;
|
|
|
|
action = true;
|
|
|
|
if (time(0) != t) break;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2003-02-26 00:47:57 +00:00
|
|
|
return action;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2003-09-07 03:11:03 +00:00
|
|
|
static void double_to_timeval(double x, timeval& t) {
|
|
|
|
t.tv_sec = (int)x;
|
|
|
|
t.tv_usec = (int)(1000000*(x - (int)x));
|
|
|
|
}
|
|
|
|
|
2003-02-18 03:31:47 +00:00
|
|
|
// Wait at most x seconds for network I/O to become possible,
|
2003-05-15 18:05:24 +00:00
|
|
|
// then do up to about .5 seconds of I/O.
|
2003-02-18 03:31:47 +00:00
|
|
|
//
|
|
|
|
int NET_XFER_SET::net_sleep(double x) {
|
2003-02-26 00:47:57 +00:00
|
|
|
int retval;
|
|
|
|
double bytes_xferred;
|
2003-02-18 03:31:47 +00:00
|
|
|
|
2003-09-07 03:11:03 +00:00
|
|
|
retval = do_select(bytes_xferred, x);
|
2003-05-15 18:05:24 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
if (bytes_xferred) {
|
|
|
|
return poll();
|
|
|
|
}
|
|
|
|
return 0;
|
2003-02-18 03:31:47 +00:00
|
|
|
}
|
|
|
|
|
2003-05-15 18:05:24 +00:00
|
|
|
// do a select with the given timeout,
|
|
|
|
// then do I/O on as many sockets as possible, subject to rate limits
|
|
|
|
// Transfer at most one block per socket.
|
2003-06-24 00:33:59 +00:00
|
|
|
//
|
2003-09-07 03:11:03 +00:00
|
|
|
int NET_XFER_SET::do_select(double& bytes_transferred, double timeout) {
|
2003-08-31 22:28:16 +00:00
|
|
|
int n, fd, retval, nsocks_queried;
|
2002-04-30 22:22:54 +00:00
|
|
|
socklen_t i;
|
|
|
|
NET_XFER *nxp;
|
2003-09-07 03:11:03 +00:00
|
|
|
struct timeval tv;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-07-02 02:02:18 +00:00
|
|
|
ScopeMessages scope_messages(log_messages, ClientMessages::DEBUG_NET_XFER);
|
|
|
|
|
2003-05-15 21:25:34 +00:00
|
|
|
// if a second has gone by, do rate-limit accounting
|
|
|
|
//
|
2003-02-26 00:47:57 +00:00
|
|
|
time_t t = time(0);
|
|
|
|
if (t != last_time) {
|
|
|
|
last_time = t;
|
|
|
|
if (bytes_left_up < max_bytes_sec_up) {
|
|
|
|
bytes_left_up += max_bytes_sec_up;
|
|
|
|
}
|
|
|
|
if (bytes_left_down < max_bytes_sec_down) {
|
|
|
|
bytes_left_down += max_bytes_sec_down;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
bytes_transferred = 0;
|
|
|
|
|
|
|
|
fd_set read_fds, write_fds, error_fds;
|
2003-02-04 22:15:30 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
FD_ZERO(&read_fds);
|
|
|
|
FD_ZERO(&write_fds);
|
|
|
|
FD_ZERO(&error_fds);
|
|
|
|
|
2003-08-31 22:28:16 +00:00
|
|
|
// do a select on all active (non-throttled) sockets
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2003-08-31 22:28:16 +00:00
|
|
|
nsocks_queried = 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
for (i=0; i<net_xfers.size(); i++) {
|
2002-08-30 21:41:03 +00:00
|
|
|
nxp = net_xfers[i];
|
|
|
|
if (!nxp->is_connected) {
|
2003-05-15 18:24:48 +00:00
|
|
|
FD_SET(nxp->socket, &write_fds);
|
2003-08-31 22:28:16 +00:00
|
|
|
nsocks_queried++;
|
2003-05-15 18:24:48 +00:00
|
|
|
} else if (nxp->want_download) {
|
2003-02-26 00:47:57 +00:00
|
|
|
if (bytes_left_down > 0) {
|
2003-05-15 18:24:48 +00:00
|
|
|
FD_SET(nxp->socket, &read_fds);
|
2003-08-31 22:28:16 +00:00
|
|
|
nsocks_queried++;
|
2003-02-26 00:47:57 +00:00
|
|
|
} else {
|
2003-07-02 02:02:18 +00:00
|
|
|
scope_messages.printf("NET_XFER_SET::do_select(): Throttling download\n");
|
2003-02-26 00:47:57 +00:00
|
|
|
}
|
2003-05-15 18:24:48 +00:00
|
|
|
} else if (nxp->want_upload) {
|
2003-02-26 00:47:57 +00:00
|
|
|
if (bytes_left_up > 0) {
|
2003-05-15 18:24:48 +00:00
|
|
|
FD_SET(nxp->socket, &write_fds);
|
2003-08-31 22:28:16 +00:00
|
|
|
nsocks_queried++;
|
2003-02-26 00:47:57 +00:00
|
|
|
} else {
|
2003-07-02 02:02:18 +00:00
|
|
|
scope_messages.printf("NET_XFER_SET::do_select(): Throttling upload\n");
|
2003-02-26 00:47:57 +00:00
|
|
|
}
|
2002-08-30 21:41:03 +00:00
|
|
|
}
|
2003-05-15 18:24:48 +00:00
|
|
|
FD_SET(nxp->socket, &error_fds);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2003-09-07 03:11:03 +00:00
|
|
|
if (nsocks_queried==0) {
|
|
|
|
boinc_sleep(timeout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double_to_timeval(timeout, tv);
|
|
|
|
n = select(FD_SETSIZE, &read_fds, &write_fds, &error_fds, &tv);
|
2003-08-31 22:28:16 +00:00
|
|
|
scope_messages.printf(
|
|
|
|
"NET_XFER_SET::do_select(): queried %d, returned %d\n",
|
|
|
|
nsocks_queried, n
|
|
|
|
);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (n == 0) return 0;
|
|
|
|
if (n < 0) return ERR_SELECT;
|
|
|
|
|
|
|
|
// if got a descriptor, find the first one in round-robin order
|
|
|
|
// and do I/O on it
|
2003-02-26 00:47:57 +00:00
|
|
|
// TODO: use round-robin order
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
for (i=0; i<net_xfers.size(); i++) {
|
2002-08-30 21:41:03 +00:00
|
|
|
nxp = net_xfers[i];
|
2002-04-30 22:22:54 +00:00
|
|
|
fd = nxp->socket;
|
2002-08-30 21:41:03 +00:00
|
|
|
if (FD_ISSET(fd, &read_fds) || FD_ISSET(fd, &write_fds)) {
|
2003-09-04 20:11:47 +00:00
|
|
|
if (FD_ISSET(fd, &read_fds)) {
|
|
|
|
scope_messages.printf("NET_XFER_SET::do_select(): read enabled on socket %d\n", fd);
|
|
|
|
}
|
|
|
|
if (FD_ISSET(fd, &write_fds)) {
|
|
|
|
scope_messages.printf("NET_XFER_SET::do_select(): write enabled on socket %d\n", fd);
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!nxp->is_connected) {
|
2003-09-04 20:11:47 +00:00
|
|
|
n = get_socket_error(fd);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (n) {
|
2003-08-31 22:28:16 +00:00
|
|
|
scope_messages.printf(
|
|
|
|
"NET_XFER_SET::do_select(): socket %d connection to %s failed\n",
|
2003-09-01 17:11:14 +00:00
|
|
|
fd, nxp->hostname
|
2003-08-31 22:28:16 +00:00
|
|
|
);
|
2002-04-30 22:22:54 +00:00
|
|
|
nxp->error = ERR_CONNECT;
|
|
|
|
nxp->io_done = true;
|
|
|
|
} else {
|
2003-07-02 02:02:18 +00:00
|
|
|
scope_messages.printf("NET_XFER_SET::do_select(): socket %d is connected\n", fd);
|
2002-04-30 22:22:54 +00:00
|
|
|
nxp->is_connected = true;
|
|
|
|
bytes_transferred += 1;
|
|
|
|
}
|
|
|
|
} else if (nxp->do_file_io) {
|
2003-05-16 20:32:36 +00:00
|
|
|
n = 1;
|
|
|
|
time_t now = time(0);
|
|
|
|
do {
|
|
|
|
retval = nxp->do_xfer(n);
|
|
|
|
nxp->update_speed(n);
|
|
|
|
bytes_transferred += n;
|
|
|
|
if (nxp->want_download) {
|
|
|
|
down_active = true;
|
|
|
|
bytes_left_down -= n;
|
|
|
|
bytes_down += n;
|
|
|
|
} else {
|
|
|
|
up_active = true;
|
|
|
|
bytes_left_up -= n;
|
|
|
|
bytes_up += n;
|
|
|
|
}
|
|
|
|
// For uploads, keep trying to send until we fill
|
|
|
|
// the buffers or 1 second has passed
|
|
|
|
} while(nxp->want_upload && n > 0 && time(0) == now);
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
|
|
|
nxp->io_ready = true;
|
|
|
|
}
|
2002-08-30 21:41:03 +00:00
|
|
|
} else if (FD_ISSET(fd, &error_fds)) {
|
2003-07-02 02:02:18 +00:00
|
|
|
scope_messages.printf("NET_XFER_SET::do_select(): got error on socket %d\n", fd);
|
2002-04-30 22:22:54 +00:00
|
|
|
nxp = lookup_fd(fd);
|
2003-02-26 00:47:57 +00:00
|
|
|
if (nxp) {
|
|
|
|
nxp->got_error();
|
|
|
|
} else {
|
2003-06-03 22:47:15 +00:00
|
|
|
msg_printf(0, MSG_ERROR, "do_select(): nxp not found\n");
|
2003-02-26 00:47:57 +00:00
|
|
|
}
|
2002-08-30 21:41:03 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Return the NET_XFER object whose socket matches fd
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
NET_XFER* NET_XFER_SET::lookup_fd(int fd) {
|
|
|
|
for (unsigned int i=0; i<net_xfers.size(); i++) {
|
|
|
|
if (net_xfers[i]->socket == fd) {
|
|
|
|
return net_xfers[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// transfer up to a block of data; return #bytes transferred
|
|
|
|
//
|
|
|
|
int NET_XFER::do_xfer(int& nbytes_transferred) {
|
2003-05-16 20:32:36 +00:00
|
|
|
// Leave these as signed ints so recv/send can return errors
|
|
|
|
int n, m, nleft;
|
2003-04-02 19:30:15 +00:00
|
|
|
bool would_block;
|
2003-02-26 00:47:57 +00:00
|
|
|
char buf[MAX_BLOCKSIZE];
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
nbytes_transferred = 0;
|
|
|
|
|
2003-07-02 02:02:18 +00:00
|
|
|
ScopeMessages scope_messages(log_messages, ClientMessages::DEBUG_NET_XFER);
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
if (want_download) {
|
2002-06-06 18:50:12 +00:00
|
|
|
#ifdef _WIN32
|
2002-08-30 21:41:03 +00:00
|
|
|
n = recv(socket, buf, blocksize, 0);
|
2002-06-06 18:50:12 +00:00
|
|
|
#else
|
2002-08-30 21:41:03 +00:00
|
|
|
n = read(socket, buf, blocksize);
|
2002-06-06 18:50:12 +00:00
|
|
|
#endif
|
2003-07-02 02:02:18 +00:00
|
|
|
scope_messages.printf("NET_XFER::do_xfer(): read %d bytes from socket %d\n", n, socket);
|
2002-08-30 21:41:03 +00:00
|
|
|
if (n == 0) {
|
2002-08-12 21:54:19 +00:00
|
|
|
io_done = true;
|
2002-08-30 21:41:03 +00:00
|
|
|
want_download = false;
|
|
|
|
} else if (n < 0) {
|
|
|
|
io_done = true;
|
|
|
|
error = ERR_READ;
|
|
|
|
} else {
|
|
|
|
nbytes_transferred += n;
|
2003-02-27 09:21:22 +00:00
|
|
|
bytes_xferred += n;
|
2002-08-30 21:41:03 +00:00
|
|
|
m = fwrite(buf, 1, n, file);
|
|
|
|
if (n != m) {
|
|
|
|
fprintf(stdout, "Error: incomplete disk write\n");
|
|
|
|
io_done = true;
|
|
|
|
error = ERR_FWRITE;
|
|
|
|
}
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
} else if (want_upload) {
|
2003-04-02 19:30:15 +00:00
|
|
|
// If we've sent the current contents of
|
|
|
|
// the buffer, then read the next block
|
|
|
|
if (file_read_buf_len == file_read_buf_offset) {
|
|
|
|
m = fread(file_read_buf, 1, blocksize, file);
|
|
|
|
if (m == 0) {
|
|
|
|
want_upload = false;
|
|
|
|
io_done = true;
|
|
|
|
return 0;
|
|
|
|
} else if (m < 0) {
|
|
|
|
io_done = true;
|
|
|
|
error = ERR_FREAD;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
file_read_buf_len = m;
|
|
|
|
file_read_buf_offset = 0;
|
2002-08-30 21:41:03 +00:00
|
|
|
}
|
2003-04-02 19:30:15 +00:00
|
|
|
nleft = file_read_buf_len - file_read_buf_offset;
|
2002-08-30 21:41:03 +00:00
|
|
|
while (nleft) {
|
2002-06-06 18:50:12 +00:00
|
|
|
#ifdef _WIN32
|
2003-04-02 19:30:15 +00:00
|
|
|
n = send(socket, file_read_buf+file_read_buf_offset, nleft, 0);
|
|
|
|
would_block = (WSAGetLastError() == WSAEWOULDBLOCK);
|
2002-06-06 18:50:12 +00:00
|
|
|
#else
|
2003-04-02 19:30:15 +00:00
|
|
|
n = write(socket, file_read_buf+file_read_buf_offset, nleft);
|
|
|
|
would_block = (errno == EAGAIN);
|
2002-06-06 18:50:12 +00:00
|
|
|
#endif
|
2003-04-02 19:30:15 +00:00
|
|
|
if (would_block && n < 0) n = 0;
|
2003-07-02 02:02:18 +00:00
|
|
|
scope_messages.printf("NET_XFER::do_xfer(): wrote %d bytes to socket %d%s\n", n, socket,
|
|
|
|
(would_block?", would have blocked":""));
|
2003-04-02 00:25:00 +00:00
|
|
|
if (n < 0 && !would_block) {
|
2002-08-25 03:24:55 +00:00
|
|
|
error = ERR_WRITE;
|
|
|
|
io_done = true;
|
2003-02-26 00:47:57 +00:00
|
|
|
break;
|
2002-08-25 03:15:47 +00:00
|
|
|
}
|
2003-04-02 19:30:15 +00:00
|
|
|
|
|
|
|
file_read_buf_offset += n;
|
2002-04-30 22:22:54 +00:00
|
|
|
nbytes_transferred += n;
|
2003-02-27 09:21:22 +00:00
|
|
|
bytes_xferred += n;
|
2003-04-02 19:30:15 +00:00
|
|
|
|
2003-05-15 21:25:34 +00:00
|
|
|
if (n < nleft || would_block) {
|
2003-04-02 19:30:15 +00:00
|
|
|
break;
|
2003-05-15 21:25:34 +00:00
|
|
|
}
|
2003-04-02 19:30:15 +00:00
|
|
|
|
|
|
|
nleft -= n;
|
2002-08-30 21:41:03 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-24 23:59:46 +00:00
|
|
|
// Update the transfer speed for this NET_XFER
|
2003-02-26 07:45:21 +00:00
|
|
|
// Decay speed by 1/2 every 3 seconds
|
2003-02-24 23:59:46 +00:00
|
|
|
//
|
|
|
|
void NET_XFER::update_speed(int nbytes) {
|
2003-02-26 00:47:57 +00:00
|
|
|
time_t now, delta_t;
|
2003-02-24 23:59:46 +00:00
|
|
|
double x;
|
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
now = time(0);
|
2003-02-26 07:45:21 +00:00
|
|
|
recent_bytes += nbytes;
|
2003-02-24 23:59:46 +00:00
|
|
|
if (last_speed_update==0) last_speed_update = now;
|
|
|
|
delta_t = now-last_speed_update;
|
2003-02-26 07:45:21 +00:00
|
|
|
if (delta_t<=0) return;
|
2003-02-27 09:21:22 +00:00
|
|
|
x = exp(-delta_t*log(2.0)/3.0);
|
2003-02-26 07:45:21 +00:00
|
|
|
xfer_speed *= x;
|
|
|
|
xfer_speed += recent_bytes*(1-x);
|
2003-02-24 23:59:46 +00:00
|
|
|
last_speed_update = now;
|
2003-02-26 07:45:21 +00:00
|
|
|
recent_bytes = 0;
|
2003-02-24 23:59:46 +00:00
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
void NET_XFER::got_error() {
|
|
|
|
error = ERR_IO;
|
|
|
|
io_done = true;
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(ClientMessages::DEBUG_NET_XFER,
|
|
|
|
"IO error on socket %d\n", socket);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2003-02-26 00:47:57 +00:00
|
|
|
|
|
|
|
// return true if an upload is currently in progress
|
|
|
|
// or has been since the last call to this.
|
|
|
|
// Similar for download.
|
|
|
|
//
|
|
|
|
void NET_XFER_SET::check_active(bool& up, bool& down) {
|
|
|
|
unsigned int i;
|
|
|
|
NET_XFER* nxp;
|
|
|
|
|
|
|
|
up = up_active;
|
|
|
|
down = down_active;
|
|
|
|
for (i=0; i<net_xfers.size(); i++) {
|
|
|
|
nxp = net_xfers[i];
|
|
|
|
if (nxp->is_connected && nxp->do_file_io) {
|
|
|
|
nxp->want_download?down=true:up=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
up_active = false;
|
|
|
|
down_active = false;
|
|
|
|
}
|