} /* Conn_Exit */
+/**
+ * Close all sockets (file descriptors) of open connections.
+ * This is useful in forked child processes, for example, to make sure that
+ * they don't hold connections open that the main process wants to close.
+ */
+GLOBAL void
+Conn_CloseAllSockets(void)
+{
+ CONN_ID idx;
+
+ for(idx = 0; idx < Pool_Size; idx++) {
+ if(My_Connections[idx].sock > NONE)
+ close(My_Connections[idx].sock);
+ }
+}
+
+
static unsigned int
ports_initlisteners(array *a, const char *listen_addr, void (*func)(int,short))
{
unsigned int created = 0;
char *copy, *listen_addr;
- if (!io_library_init(CONNECTION_POOL)) {
- Log(LOG_EMERG, "Cannot initialize IO routines: %s", strerror(errno));
- return -1;
- }
-
assert(Conf_ListenAddress);
/* can't use Conf_ListenAddress directly, see below */
Rendezvous_Handler();
#endif
- /* Should the configuration be reloaded? */
- if (NGIRCd_SignalRehash)
- NGIRCd_Rehash();
-
/* Check configured servers and established links */
Check_Servers();
Check_Connections();
in_k, out_k);
}
- /* Kill possibly running subprocess */
- if (Proc_InProgress(&My_Connections[Idx].proc_stat))
- Proc_Kill(&My_Connections[Idx].proc_stat);
-
/* Servers: Modify time of next connect attempt? */
Conf_UnsetServer( Idx );
} /* Conn_CountAccepted */
+/**
+ * Synchronize established connections and configured server structures
+ * after a configuration update and store the correct connection IDs, if any.
+ */
GLOBAL void
-Conn_SyncServerStruct( void )
+Conn_SyncServerStruct(void)
{
- /* Synchronize server structures (connection IDs):
- * connections <-> configuration */
-
CLIENT *client;
CONN_ID i;
int c;
- for( i = 0; i < Pool_Size; i++ ) {
- /* Established connection? */
- if (My_Connections[i].sock < 0)
+ for (i = 0; i < Pool_Size; i++) {
+ if (My_Connections[i].sock == NONE)
continue;
- /* Server connection? */
- client = Conn_GetClient( i );
- if(( ! client ) || ( Client_Type( client ) != CLIENT_SERVER )) continue;
+ /* Server link? */
+ client = Conn_GetClient(i);
+ if (!client || Client_Type(client) != CLIENT_SERVER)
+ continue;
- for( c = 0; c < MAX_SERVERS; c++ )
- {
+ for (c = 0; c < MAX_SERVERS; c++) {
/* Configured server? */
- if( ! Conf_Server[c].host[0] ) continue;
+ if (!Conf_Server[c].host[0])
+ continue;
- /* Duplicate? */
- if( strcmp( Conf_Server[c].name, Client_ID( client )) == 0 )
+ if (strcasecmp(Conf_Server[c].name, Client_ID(client)) == 0)
Conf_Server[c].conn_id = i;
}
}
* registered as a user, server or service connection. Don't update
* otherwise, so users have at least Conf_PongTimeout seconds time to
* register with the IRC server -- see Check_Connections().
- * Set "lastping", too, so we can handle time shifts backwards ... */
+ * Update "lastping", too, if time shifted backwards ... */
c = Conn_GetClient(Idx);
if (c && (Client_Type(c) == CLIENT_USER
|| Client_Type(c) == CLIENT_SERVER
My_Connections[Idx].bps = 0;
My_Connections[Idx].lastdata = t;
- My_Connections[Idx].lastping = My_Connections[Idx].lastdata;
+ if (My_Connections[Idx].lastping > t)
+ My_Connections[Idx].lastping = t;
}
/* Look at the data in the (read-) buffer of this connection */
} /* Handle_Buffer */
+/**
+ * Check whether established connections are still alive or not.
+ * If not, play PING-PONG first; and if that doesn't help either,
+ * disconnect the respective peer.
+ */
static void
Check_Connections(void)
{
- /* check if connections are alive. if not, play PING-PONG first.
- * if this doesn't help either, disconnect client. */
CLIENT *c;
CONN_ID i;
char msg[64];
} /* Check_Connections */
+/**
+ * Check if further server links should be established.
+ */
static void
-Check_Servers( void )
+Check_Servers(void)
{
- /* Check if we can establish further server links */
-
int i, n;
time_t time_now;
+ time_now = time(NULL);
+
/* Check all configured servers */
- for( i = 0; i < MAX_SERVERS; i++ ) {
- /* Valid outgoing server which isn't already connected or disabled? */
- if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) ||
- ( Conf_Server[i].conn_id > NONE ) || ( Conf_Server[i].flags & CONF_SFLAG_DISABLED ))
- continue;
+ for (i = 0; i < MAX_SERVERS; i++) {
+ if (Conf_Server[i].conn_id != NONE)
+ continue; /* Already establishing or connected */
+ if (!Conf_Server[i].host[0] || !Conf_Server[i].port > 0)
+ continue; /* No host and/or port configured */
+ if (Conf_Server[i].flags & CONF_SFLAG_DISABLED)
+ continue; /* Disabled configuration entry */
+ if (Conf_Server[i].lasttry > (time_now - Conf_ConnectRetry))
+ continue; /* We have to wait a little bit ... */
/* Is there already a connection in this group? */
- if( Conf_Server[i].group > NONE ) {
+ if (Conf_Server[i].group > NONE) {
for (n = 0; n < MAX_SERVERS; n++) {
- if (n == i) continue;
+ if (n == i)
+ continue;
if ((Conf_Server[n].conn_id != NONE) &&
- (Conf_Server[n].group == Conf_Server[i].group))
- break;
+ (Conf_Server[n].group == Conf_Server[i].group))
+ break;
}
- if (n < MAX_SERVERS) continue;
+ if (n < MAX_SERVERS)
+ continue;
}
- /* Check last connect attempt? */
- time_now = time(NULL);
- if( Conf_Server[i].lasttry > (time_now - Conf_ConnectRetry))
- continue;
-
/* Okay, try to connect now */
Conf_Server[i].lasttry = time_now;
Conf_Server[i].conn_id = SERVER_WAIT;
assert(Proc_GetPipeFd(&Conf_Server[i].res_stat) < 0);
- Resolve_Name(&Conf_Server[i].res_stat, Conf_Server[i].host, cb_Connect_to_Server);
+ Resolve_Name(&Conf_Server[i].res_stat, Conf_Server[i].host,
+ cb_Connect_to_Server);
}
} /* Check_Servers */
}
/* Read result from pipe */
- len = Resolve_Read(&Conf_Server[i].res_stat, dest_addrs, sizeof(dest_addrs));
+ len = Proc_Read(&Conf_Server[i].res_stat, dest_addrs, sizeof(dest_addrs));
if (len == 0)
return;
}
/* Read result from pipe */
- len = Resolve_Read(&My_Connections[i].proc_stat, readbuf, sizeof readbuf -1);
+ len = Proc_Read(&My_Connections[i].proc_stat, readbuf, sizeof readbuf -1);
if (len == 0)
return;
#endif
+#ifdef DEBUG
+
+GLOBAL void
+Conn_DebugDump(void)
+{
+ int i;
+
+ Log(LOG_DEBUG, "Connection status:");
+ for (i = 0; i < Pool_Size; i++) {
+ if (My_Connections[i].sock == NONE)
+ continue;
+ Log(LOG_DEBUG,
+ " - %d: host=%s, lastdata=%ld, lastping=%ld, delaytime=%ld, flag=%d, options=%d, bps=%d, client=%s",
+ My_Connections[i].sock, My_Connections[i].host,
+ My_Connections[i].lastdata, My_Connections[i].lastping,
+ My_Connections[i].delaytime, My_Connections[i].flag,
+ My_Connections[i].options, My_Connections[i].bps,
+ My_Connections[i].client ? Client_ID(My_Connections[i].client) : "-");
+ }
+} /* Conn_DumpClients */
+
+#endif
+
+
/* -eof- */