-/*
- terminate a connection and notify the other
- end before closing the sockets
-*/
-void terminate_connection(conn_list_t *cl)
-{
-cp
- if(cl->status.remove)
- return;
-
- if(debug_lvl > 0)
- syslog(LOG_NOTICE, _("Closing connection with %s."), cl->hostname);
-
- if(cl->status.timeout)
- send_timeout(cl);
- else if(!cl->status.termreq)
- send_termreq(cl);
-
- close(cl->socket);
- if(cl->status.meta)
- close(cl->meta_socket);
-
- if(cl->status.outgoing)
- {
- signal(SIGALRM, sigalrm_handler);
- seconds_till_retry = 5;
- alarm(seconds_till_retry);
- syslog(LOG_NOTICE, _("Try to re-establish outgoing connection in 5 seconds."));
- }
-
- cl->status.active = 0;
- cl->status.remove = 1;
-cp
-}
-
-/*
- Check if the other end is active.
- If we have sent packets, but didn't receive any,
- then possibly the other end is dead. We send a
- PING request over the meta connection. If the other
- end does not reply in time, we consider them dead
- and close the connection.
-*/
-int check_dead_connections(void)
-{
- conn_list_t *p;
- time_t now;
-cp
- now = time(NULL);
- for(p = conn_list; p != NULL; p = p->next)
- {
- if(p->status.remove)
- continue;
- if(p->status.active && p->status.meta)
- {
- if(p->last_ping_time + timeout < now)
- {
- if(p->status.pinged && !p->status.got_pong)
- {
- syslog(LOG_INFO, _("%s (" IP_ADDR_S ") didn't respond to ping"),
- p->hostname, IP_ADDR_V(p->vpn_ip));
- p->status.timeout = 1;
- terminate_connection(p);
- }
- else if(p->want_ping)
- {
- send_ping(p);
- p->last_ping_time = now;
- p->status.pinged = 1;
- p->status.got_pong = 0;
- }
- }