2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
net-disconnect.c :
|
|
|
|
|
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "network.h"
|
|
|
|
|
|
|
|
/* when quitting, wait for max. 5 seconds before forcing to close the socket */
|
|
|
|
#define MAX_QUIT_CLOSE_WAIT 5
|
|
|
|
|
|
|
|
/* wait for max. 2 minutes for other side to close the socket */
|
|
|
|
#define MAX_CLOSE_WAIT (60*2)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
time_t created;
|
|
|
|
int handle;
|
|
|
|
int tag;
|
|
|
|
} NET_DISCONNECT_REC;
|
|
|
|
|
|
|
|
static GSList *disconnects;
|
|
|
|
|
|
|
|
static int timeout_tag;
|
|
|
|
|
|
|
|
static void net_disconnect_remove(NET_DISCONNECT_REC *rec)
|
|
|
|
{
|
|
|
|
disconnects = g_slist_remove(disconnects, rec);
|
|
|
|
|
|
|
|
g_source_remove(rec->tag);
|
|
|
|
g_free(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_disconnect(NET_DISCONNECT_REC *rec)
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* check if there's any data waiting in socket */
|
2000-07-16 16:18:05 -04:00
|
|
|
while ((ret = net_receive(rec->handle, buf, sizeof(buf))) > 0) ;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (ret == -1) {
|
|
|
|
/* socket was closed */
|
|
|
|
net_disconnect_remove(rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sig_timeout_disconnect(void)
|
|
|
|
{
|
|
|
|
NET_DISCONNECT_REC *rec;
|
|
|
|
GSList *tmp, *next;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
/* check if we've waited enough for sockets to close themselves */
|
|
|
|
now = time(NULL);
|
|
|
|
for (tmp = disconnects; tmp != NULL; tmp = next) {
|
|
|
|
rec = tmp->data;
|
|
|
|
next = tmp->next;
|
|
|
|
|
|
|
|
if (rec->created+MAX_CLOSE_WAIT <= now)
|
2000-07-16 16:18:05 -04:00
|
|
|
net_disconnect_remove(rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (disconnects == NULL) {
|
|
|
|
/* no more sockets in disconnect queue, stop calling this
|
|
|
|
function */
|
|
|
|
timeout_tag = -1;
|
|
|
|
}
|
2000-07-16 16:18:05 -04:00
|
|
|
return disconnects != NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to let the other side close the connection, if it still isn't
|
|
|
|
disconnected after certain amount of time, close it ourself */
|
|
|
|
void net_disconnect_later(int handle)
|
|
|
|
{
|
|
|
|
NET_DISCONNECT_REC *rec;
|
|
|
|
|
|
|
|
rec = g_new(NET_DISCONNECT_REC, 1);
|
|
|
|
rec->created = time(NULL);
|
|
|
|
rec->handle = handle;
|
2000-07-16 16:18:05 -04:00
|
|
|
rec->tag = g_input_add(handle, G_INPUT_READ,
|
|
|
|
(GInputFunction) sig_disconnect, rec);
|
|
|
|
|
|
|
|
if (timeout_tag == -1) {
|
|
|
|
timeout_tag = g_timeout_add(10000, (GSourceFunc)
|
|
|
|
sig_timeout_disconnect, NULL);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
disconnects = g_slist_append(disconnects, rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void net_disconnect_init(void)
|
|
|
|
{
|
|
|
|
disconnects = NULL;
|
|
|
|
timeout_tag = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_disconnect_deinit(void)
|
|
|
|
{
|
|
|
|
NET_DISCONNECT_REC *rec;
|
|
|
|
time_t now, max;
|
|
|
|
int first;
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set set;
|
|
|
|
|
|
|
|
/* give the sockets a chance to disconnect themselves.. */
|
|
|
|
max = time(NULL)+MAX_QUIT_CLOSE_WAIT;
|
|
|
|
first = 1;
|
|
|
|
while (disconnects != NULL) {
|
|
|
|
rec = disconnects->data;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
if (rec->created+MAX_QUIT_CLOSE_WAIT <= now || max <= now) {
|
|
|
|
/* this one has waited enough */
|
|
|
|
net_disconnect_remove(rec);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(rec->handle, &set);
|
|
|
|
tv.tv_sec = first ? 0 : max-now;
|
|
|
|
tv.tv_usec = first ? 100000 : 0;
|
2000-07-16 16:18:05 -04:00
|
|
|
if (select(rec->handle+1, &set, NULL, NULL, &tv) > 0 &&
|
|
|
|
FD_ISSET(rec->handle, &set)) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* data coming .. check if we can close the handle */
|
|
|
|
sig_disconnect(rec);
|
|
|
|
} else if (first) {
|
2000-07-16 16:18:05 -04:00
|
|
|
/* Display the text when we have already waited
|
|
|
|
for a while */
|
|
|
|
printf(_("Please wait, waiting for servers to close "
|
|
|
|
"connections..\n"));
|
2000-04-26 04:03:38 -04:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|