X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fngircd%2Fconn.c;h=78a20b055ea523a1692998a45e08c9d49bc1bd42;hb=c135d0dded909e2e5780697c4066ad44a3f488c8;hp=f4efff1655002f5687b44b8beb9f9cb38c90fbf2;hpb=bf8b64630443b2bd3fe2a298f860ea0d492df649;p=ngircd-alex.git diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c index f4efff16..78a20b05 100644 --- a/src/ngircd/conn.c +++ b/src/ngircd/conn.c @@ -419,6 +419,23 @@ Conn_Exit( void ) } /* 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)) { @@ -1074,10 +1091,6 @@ Conn_Close( CONN_ID Idx, const char *LogMsg, const char *FwdMsg, bool InformClie 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 ); @@ -1126,32 +1139,32 @@ Conn_CountAccepted(void) } /* 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; } } @@ -1494,7 +1507,7 @@ Read_Request( CONN_ID Idx ) * 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 @@ -1504,7 +1517,8 @@ Read_Request( CONN_ID Idx ) 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 */ @@ -1672,11 +1686,14 @@ Handle_Buffer(CONN_ID Idx) } /* 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]; @@ -1728,42 +1745,47 @@ Check_Connections(void) } /* 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 */ @@ -1952,7 +1974,7 @@ cb_Connect_to_Server(int fd, UNUSED short events) } /* 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; @@ -2005,7 +2027,7 @@ cb_Read_Resolver_Result( int r_fd, UNUSED short events ) } /* 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;