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
|
|
|
|
|
|
|
#ifdef _WIN32
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifndef _CONSOLE
|
2004-12-06 23:02:47 +00:00
|
|
|
#include "stdafx.h"
|
2004-03-04 05:08:04 +00:00
|
|
|
#include "wingui_mainwindow.h"
|
2004-12-06 23:02:47 +00:00
|
|
|
#else
|
|
|
|
#include "boinc_win.h"
|
2004-03-04 05:08:04 +00:00
|
|
|
#endif
|
|
|
|
|
2002-12-18 20:17:35 +00:00
|
|
|
#include "Win_net.h"
|
2004-03-04 05:08:04 +00:00
|
|
|
#include "win_util.h"
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
#endif
|
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cmath>
|
2004-03-04 11:41:43 +00:00
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
#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>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
|
|
|
|
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"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "client_msgs.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-07-13 13:54:09 +00:00
|
|
|
#if defined(_WIN32)
|
2003-07-11 00:50:48 +00:00
|
|
|
typedef int socklen_t;
|
2004-12-05 23:52:17 +00:00
|
|
|
#elif defined( __APPLE__)
|
2003-12-08 23:04:51 +00:00
|
|
|
typedef int32_t socklen_t;
|
2004-12-05 23:52:17 +00:00
|
|
|
#elif !defined(GETSOCKOPT_SOCKLEN_T) && !defined(_SOCKLEN_T_DECLARED) && !defined(socklen_t)
|
2003-10-29 08:02:11 +00:00
|
|
|
typedef size_t socklen_t;
|
2002-05-25 15:03:53 +00:00
|
|
|
#endif
|
|
|
|
|
2004-06-30 18:17:21 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2004-06-16 23:16:08 +00:00
|
|
|
// if an active transfer doesn't get any activity
|
|
|
|
// in this many seconds, error out
|
|
|
|
#define NET_XFER_TIMEOUT 600
|
|
|
|
|
2004-08-17 10:46:37 +00:00
|
|
|
static void boinc_close_socket(int sock) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
closesocket(sock);
|
|
|
|
#else
|
|
|
|
close(sock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-09-04 20:11:47 +00:00
|
|
|
int get_socket_error(int fd) {
|
|
|
|
socklen_t intsize = sizeof(int);
|
|
|
|
int n;
|
2004-01-15 01:42:49 +00:00
|
|
|
#ifdef WIN32
|
2003-09-04 20:11:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-03-27 00:45:27 +00:00
|
|
|
int NET_XFER::get_ip_addr(int &ip_addr) {
|
2004-06-19 00:41:58 +00:00
|
|
|
return get_ip_addr(hostname, ip_addr);
|
2004-03-27 00:45:27 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2004-01-15 01:42:49 +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;
|
|
|
|
|
2004-11-10 21:09:54 +00:00
|
|
|
n = sprintf(msg, "Can't resolve hostname [%s] ", hostname);
|
2004-01-15 01:42:49 +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) {
|
2004-01-15 01:42:49 +00:00
|
|
|
#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
|
|
|
|
2004-01-15 01:42:49 +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) {
|
2004-01-15 01:42:49 +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
|
|
|
}
|
2004-01-17 21:34:54 +00:00
|
|
|
#ifndef _CONSOLE
|
2004-03-04 05:08:04 +00:00
|
|
|
if (WSAAsyncSelect( fd, g_myWnd->GetSafeHwnd(), RegisterWindowMessage(NET_ACTIVITY_MSG), 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() {
|
2004-01-15 01:42:49 +00:00
|
|
|
#ifdef WIN32
|
2003-02-26 00:47:57 +00:00
|
|
|
NetClose();
|
2002-08-02 18:31:20 +00:00
|
|
|
#endif
|
2004-08-16 09:41:02 +00:00
|
|
|
if (socket) {
|
|
|
|
boinc_close_socket(socket);
|
|
|
|
socket = 0;
|
|
|
|
}
|
2002-08-02 18:31:20 +00:00
|
|
|
}
|
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);
|
2004-04-20 02:47:51 +00:00
|
|
|
start_time = dtime();
|
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;
|
2004-09-22 23:34:52 +00:00
|
|
|
xfer_speed = 0;
|
2004-06-16 23:16:08 +00:00
|
|
|
reset_timeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NET_XFER::check_timeout(bool time_passed) {
|
|
|
|
if (seconds_until_timeout == 0) {
|
|
|
|
io_done = true;
|
|
|
|
error = ERR_TIMEOUT;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (time_passed) {
|
|
|
|
seconds_until_timeout--;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NET_XFER::reset_timeout() {
|
|
|
|
seconds_until_timeout = NET_XFER_TIMEOUT;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2004-02-05 22:36:55 +00:00
|
|
|
char* NET_XFER::get_hostname() {
|
|
|
|
return hostname;
|
|
|
|
}
|
|
|
|
|
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");
|
2004-01-30 22:19:19 +00:00
|
|
|
return ERR_NOT_FOUND;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2004-10-14 22:01:05 +00:00
|
|
|
// or .5 second goes by
|
2002-08-22 20:19:18 +00:00
|
|
|
//
|
2004-10-14 22:01:05 +00:00
|
|
|
bool NET_XFER_SET::poll(double now) {
|
2003-02-26 00:47:57 +00:00
|
|
|
double bytes_xferred;
|
|
|
|
int retval;
|
|
|
|
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;
|
2004-10-14 22:01:05 +00:00
|
|
|
if ((dtime() - now) > 0.5) 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) {
|
2004-10-14 22:01:05 +00:00
|
|
|
return poll(dtime());
|
2003-05-15 18:05:24 +00:00
|
|
|
}
|
|
|
|
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;
|
2004-01-15 01:42:49 +00:00
|
|
|
unsigned int i;
|
2002-04-30 22:22:54 +00:00
|
|
|
NET_XFER *nxp;
|
2003-09-07 03:11:03 +00:00
|
|
|
struct timeval tv;
|
2004-06-16 23:16:08 +00:00
|
|
|
bool time_passed = false;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-04-08 08:15:23 +00:00
|
|
|
SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_NET_XFER);
|
2003-07-02 02:02:18 +00:00
|
|
|
|
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) {
|
2004-06-16 23:16:08 +00:00
|
|
|
time_passed = true;
|
2003-02-26 00:47:57 +00:00
|
|
|
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) {
|
2004-06-16 23:16:08 +00:00
|
|
|
if (nxp->check_timeout(time_passed)) continue;
|
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) {
|
2004-06-16 23:16:08 +00:00
|
|
|
if (nxp->check_timeout(time_passed)) continue;
|
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) {
|
2004-06-16 23:16:08 +00:00
|
|
|
if (nxp->check_timeout(time_passed)) continue;
|
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",
|
2004-02-05 22:36:55 +00:00
|
|
|
fd, nxp->get_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;
|
2004-06-16 23:16:08 +00:00
|
|
|
nxp->reset_timeout();
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
} 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);
|
2004-09-22 21:08:26 +00:00
|
|
|
nxp->update_speed();
|
2004-06-16 23:16:08 +00:00
|
|
|
nxp->reset_timeout();
|
2003-05-16 20:32:36 +00:00
|
|
|
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;
|
|
|
|
|
2004-04-08 08:15:23 +00:00
|
|
|
SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_NET_XFER);
|
2003-07-02 02:02:18 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
if (want_download) {
|
2004-06-10 21:58:57 +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) {
|
2004-12-10 00:57:20 +00:00
|
|
|
// If we've sent the current contents of the buffer,
|
|
|
|
// read the next block
|
|
|
|
//
|
2003-04-02 19:30:15 +00:00
|
|
|
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) {
|
2004-01-15 01:42:49 +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;
|
2004-04-05 06:32:28 +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
|
2004-04-20 02:47:51 +00:00
|
|
|
// called on every I/O
|
2003-02-24 23:59:46 +00:00
|
|
|
//
|
2004-09-22 21:08:26 +00:00
|
|
|
void NET_XFER::update_speed() {
|
2004-04-20 02:47:51 +00:00
|
|
|
double delta_t = dtime() - start_time;
|
|
|
|
if (delta_t > 0) {
|
|
|
|
xfer_speed = bytes_xferred / delta_t;
|
|
|
|
} else if (xfer_speed == 0) {
|
|
|
|
xfer_speed = 999999999;
|
|
|
|
}
|
2003-02-24 23:59:46 +00:00
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
void NET_XFER::got_error() {
|
2004-12-10 00:57:20 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
error = ERR_IO;
|
|
|
|
io_done = true;
|
2004-04-05 06:32:28 +00:00
|
|
|
log_messages.printf(
|
2004-04-08 08:15:23 +00:00
|
|
|
CLIENT_MSG_LOG::DEBUG_NET_XFER, "IO error on socket %d\n", socket
|
2004-04-05 06:32:28 +00:00
|
|
|
);
|
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;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
static volatile const char __attribute__((unused)) *BOINCrcsid="$Id$";
|
|
|
|
#else
|
|
|
|
static volatile const char *BOINCrcsid="$Id$";
|
|
|
|
#endif
|