#include "log.h"
#include "ng_ipaddr.h"
#include "parse.h"
-#include "proc.h"
#include "resolve.h"
#include "tool.h"
} /* 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))
{
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 );
}
/* 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;
* IDENT user name.*/
CLIENT *c;
- int i;
+ CONN_ID i;
size_t len;
char *identptr;
#ifdef IDENTAUTH
#endif
LogDebug("Resolver: Got callback on fd %d, events %d", r_fd, events );
-
- /* Search associated connection ... */
- for( i = 0; i < Pool_Size; i++ ) {
- if(( My_Connections[i].sock != NONE )
- && (Proc_GetPipeFd(&My_Connections[i].proc_stat) == r_fd))
- break;
- }
- if( i >= Pool_Size ) {
+ i = Conn_GetFromProc(r_fd);
+ if (i == NONE) {
/* Ops, none found? Probably the connection has already
* been closed!? We'll ignore that ... */
io_close( r_fd );
}
/* 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;
#ifdef DEBUG
else Log( LOG_DEBUG, "Resolver: discarding result for already registered connection %d.", i );
#endif
- /* Reset penalty time */
- Conn_ResetPenalty( i );
} /* cb_Read_Resolver_Result */
return c ? c->client : NULL;
}
+/**
+ * Get PROC_STAT sub-process structure of a connection.
+ * @param Idx Connection index number
+ * @return PROC_STAT structure
+ */
+GLOBAL PROC_STAT *
+Conn_GetProcStat(CONN_ID Idx)
+{
+ CONNECTION *c;
+
+ assert(Idx >= 0);
+ c = array_get(&My_ConnArray, sizeof (CONNECTION), (size_t)Idx);
+ assert(c != NULL);
+ return &c->proc_stat;
+} /* Conn_GetProcStat */
+
+
+/**
+ * Get CONN_ID from file descriptor associated to a subprocess structure.
+ * @param fd File descriptor
+ * @return CONN_ID or NONE (-1)
+ */
+GLOBAL CONN_ID
+Conn_GetFromProc(int fd)
+{
+ int i;
+
+ assert(fd > 0);
+ for (i = 0; i < Pool_Size; i++) {
+ if ((My_Connections[i].sock != NONE)
+ && (Proc_GetPipeFd(&My_Connections[i].proc_stat) == fd))
+ return i;
+ }
+ return NONE;
+} /* Conn_GetFromProc */
+
#ifdef SSL_SUPPORT