2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2003-08-05 22:49:20 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2003-08-05 22:49:20 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software 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 Lesser General Public License for more details.
|
2002-06-06 18:37:05 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-06-06 18:37:05 +00:00
|
|
|
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2002-06-06 18:37:05 +00:00
|
|
|
|
2002-08-09 21:43:19 +00:00
|
|
|
#include "win_net.h"
|
2002-12-18 20:20:04 +00:00
|
|
|
#include "client_state.h"
|
2005-01-28 21:46:41 +00:00
|
|
|
#include "hostinfo_network.h"
|
2002-06-06 18:37:05 +00:00
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#define DIAL_WAIT 60 // seconds after dial to wait (in case of cancel)
|
|
|
|
#define CONFIRM_WAIT 60 // seconds after user says not to connect to ask again
|
|
|
|
#define CLOSE_WAIT 5 // seconds after last call to close that the connection should be terminated
|
2002-12-18 20:20:04 +00:00
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
int net_ref_count = -1; // -1 closed, 0 open but not used, >0 number of users
|
|
|
|
double net_last_req_time = 0; // last time user was requested to connect in seconds
|
|
|
|
double net_last_dial_time = 0; // last time modem was dialed
|
|
|
|
double net_close_time = 0; // 0 don't close, >0 time when network connection should be terminated in seconds
|
2002-12-18 20:20:04 +00:00
|
|
|
bool dialed = false;
|
2002-06-06 18:37:05 +00:00
|
|
|
|
2004-06-09 18:17:25 +00:00
|
|
|
int WinsockInitialize()
|
|
|
|
{
|
|
|
|
WSADATA wsdata;
|
|
|
|
return WSAStartup( MAKEWORD( 1, 1 ), &wsdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WinsockCleanup()
|
|
|
|
{
|
|
|
|
return WSACleanup();
|
|
|
|
}
|
|
|
|
|
2005-01-28 21:46:41 +00:00
|
|
|
typedef BOOL (WINAPI *GetStateProc)( OUT LPDWORD lpdwFlags, IN DWORD dwReserved);
|
|
|
|
|
|
|
|
int get_connected_state( ) {
|
2004-11-24 07:02:01 +00:00
|
|
|
int online = 0;
|
2005-01-28 21:46:41 +00:00
|
|
|
static bool first=true;
|
|
|
|
static HMODULE libmodule;
|
|
|
|
static GetStateProc GetState;
|
|
|
|
DWORD connectionFlags;
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
libmodule = LoadLibrary("wininet.dll");
|
|
|
|
if (libmodule) {
|
|
|
|
GetState = (GetStateProc) GetProcAddress(libmodule, "InternetGetConnectedState");
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
if (libmodule && GetState) {
|
|
|
|
online = (*GetState)(&connectionFlags, 0);
|
|
|
|
if (online) {
|
|
|
|
return CONNECTED_STATE_CONNECTED;
|
|
|
|
} else {
|
|
|
|
return CONNECTED_STATE_NOT_CONNECTED;
|
|
|
|
}
|
2004-11-24 07:02:01 +00:00
|
|
|
}
|
2005-01-28 21:46:41 +00:00
|
|
|
return CONNECTED_STATE_UNKNOWN;
|
2004-11-24 07:02:01 +00:00
|
|
|
}
|
|
|
|
|
2002-08-09 21:43:19 +00:00
|
|
|
int NetOpen( void )
|
|
|
|
{
|
2004-06-09 18:17:25 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
typedef BOOL (WINAPI *GetStateProc)( OUT LPDWORD lpdwFlags, IN DWORD dwReserved);
|
|
|
|
typedef BOOL (WINAPI *AutoDialProc)( IN DWORD dwFlags, IN DWORD dwReserved);
|
|
|
|
|
2003-08-05 22:49:20 +00:00
|
|
|
if(net_ref_count >= 0) {
|
|
|
|
net_ref_count ++;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-18 20:20:04 +00:00
|
|
|
|
2003-08-05 22:49:20 +00:00
|
|
|
GetStateProc GetState = NULL;
|
|
|
|
AutoDialProc AutoDial = NULL;
|
|
|
|
DWORD connectionFlags;
|
|
|
|
HMODULE libmodule = NULL;
|
|
|
|
|
|
|
|
libmodule = LoadLibrary("wininet.dll");
|
|
|
|
if (libmodule) {
|
|
|
|
GetState = (GetStateProc)GetProcAddress(libmodule, "InternetGetConnectedState");
|
|
|
|
AutoDial = (AutoDialProc)GetProcAddress(libmodule, "InternetAutodial");
|
|
|
|
|
|
|
|
if (GetState && AutoDial) {
|
|
|
|
rc = (*GetState)(&connectionFlags, 0);
|
|
|
|
|
|
|
|
// Don't Autodial if already connected to Internet by Modem or LAN
|
|
|
|
if (!rc) {
|
|
|
|
if((double)time(NULL) < net_last_dial_time + CONFIRM_WAIT) {
|
|
|
|
return -1;
|
|
|
|
}
|
2004-01-19 20:53:40 +00:00
|
|
|
#if !defined(_WIN32) && !defined(_CONSOLE)
|
2003-08-05 22:49:20 +00:00
|
|
|
if(gstate.global_prefs.confirm_before_connecting) {
|
|
|
|
net_last_req_time = (double)time(NULL);
|
|
|
|
if(!RequestNetConnect()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2003-11-05 00:13:16 +00:00
|
|
|
#endif
|
2003-08-05 22:49:20 +00:00
|
|
|
net_last_dial_time = (double)time(NULL);
|
|
|
|
rc = (*AutoDial)(INTERNET_AUTODIAL_FORCE_UNATTENDED, 0);
|
|
|
|
if (rc) {
|
|
|
|
dialed = true;
|
|
|
|
} else {
|
|
|
|
// InternetAutodial() returns error 86 for some users
|
|
|
|
// and 668 for some other users, but a subsequent call
|
|
|
|
// to gethostbyname() or connect() autodials successfully.
|
|
|
|
// So (with one exception) we ignore failure returns
|
|
|
|
// from InternetAutodial() to work around this problem.
|
|
|
|
// Error 86 is "The specified Network Password is not correct."
|
|
|
|
// Error 668 is RAS Error "The connection dropped."
|
|
|
|
rc = GetLastError();
|
|
|
|
// Don't continue if busy signal, no answer or user cancelled
|
|
|
|
if (rc == ERROR_USER_DISCONNECTION) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
net_ref_count = 1;
|
|
|
|
return 0;
|
2002-08-09 21:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetClose( void )
|
|
|
|
{
|
2003-08-05 22:49:20 +00:00
|
|
|
if(net_ref_count > 0) net_ref_count --;
|
|
|
|
if(net_ref_count == 0) {
|
|
|
|
net_close_time = (double)time(NULL) + CLOSE_WAIT;
|
|
|
|
}
|
2002-08-09 21:43:19 +00:00
|
|
|
}
|
|
|
|
|
2002-12-18 20:20:04 +00:00
|
|
|
void NetCheck( void ) {
|
2003-08-05 22:49:20 +00:00
|
|
|
if(net_ref_count == 0 && net_close_time > 0 && net_close_time < (double)time(NULL)) {
|
|
|
|
|
|
|
|
typedef BOOL (WINAPI *HangupProc)(IN DWORD dwReserved);
|
|
|
|
HangupProc HangUp = NULL;
|
|
|
|
HMODULE libmodule = NULL;
|
|
|
|
|
|
|
|
// Hang up the modem if we dialed it
|
|
|
|
if (dialed && gstate.global_prefs.hangup_if_dialed) {
|
|
|
|
libmodule = LoadLibrary("wininet.dll");
|
|
|
|
if (libmodule) {
|
|
|
|
HangUp = (HangupProc)GetProcAddress(libmodule, "InternetAutodialHangup");
|
|
|
|
if (HangUp) int rc = (* HangUp)(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dialed = false;
|
|
|
|
net_ref_count = -1;
|
|
|
|
net_close_time = 0;
|
2004-06-09 18:17:25 +00:00
|
|
|
|
2003-08-05 22:49:20 +00:00
|
|
|
}
|
2002-12-18 20:20:04 +00:00
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_4971b5333e = "$Id$";
|