X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Fresolve.c;h=dbf28736ab8c2329705be524c5d893be5266e230;hp=e25849497f06869065b02082bc294689b2a808c0;hb=20ce56cc5bdbee652f73499e3b03714ecbb45085;hpb=5b2364b236ce3027ff5fcf93cad8cb75034d3838 diff --git a/src/ngircd/resolve.c b/src/ngircd/resolve.c index e2584949..dbf28736 100644 --- a/src/ngircd/resolve.c +++ b/src/ngircd/resolve.c @@ -14,7 +14,7 @@ #include "portab.h" -static char UNUSED id[] = "$Id: resolve.c,v 1.21 2005/09/12 19:10:20 fw Exp $"; +static char UNUSED id[] = "$Id: resolve.c,v 1.27 2007/11/25 18:42:37 fw Exp $"; #include "imp.h" #include @@ -24,7 +24,6 @@ static char UNUSED id[] = "$Id: resolve.c,v 1.21 2005/09/12 19:10:20 fw Exp $"; #include #include #include -#include #include #ifdef IDENTAUTH @@ -36,6 +35,7 @@ static char UNUSED id[] = "$Id: resolve.c,v 1.21 2005/09/12 19:10:20 fw Exp $"; #include "conn.h" #include "defines.h" #include "log.h" +#include "tool.h" #include "exp.h" #include "resolve.h" @@ -50,10 +50,11 @@ static bool register_callback PARAMS((RES_STAT *s, void (*cbfunc)(int, short))); static char *Get_Error PARAMS(( int H_Error )); #endif -static int +static pid_t Resolver_fork(int *pipefds) { - int pid; + pid_t pid; + if (pipe(pipefds) != 0) { Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno )); return -1; @@ -77,13 +78,17 @@ Resolver_fork(int *pipefds) } +/** + * Resolve IP (asynchronous!). + */ GLOBAL bool -Resolve_Addr( RES_STAT *s, struct sockaddr_in *Addr, int identsock, void (*cbfunc)(int, short)) +Resolve_Addr(RES_STAT * s, struct sockaddr_in *Addr, int identsock, + void (*cbfunc) (int, short)) { - /* Resolve IP (asynchronous!). */ - int pid, pipefd[2]; + int pipefd[2]; + pid_t pid; + assert(s != NULL); - s->success = false; pid = Resolver_fork(pipefd); if (pid > 0) { @@ -103,13 +108,16 @@ Resolve_Addr( RES_STAT *s, struct sockaddr_in *Addr, int identsock, void (*cbfun } /* Resolve_Addr */ +/** + * Resolve hostname (asynchronous!). + */ GLOBAL bool Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short)) { - /* Resolve hostname (asynchronous!). */ - int pid, pipefd[2]; + int pipefd[2]; + pid_t pid; + assert(s != NULL); - s->success = false; pid = Resolver_fork(pipefd); if (pid > 0) { @@ -136,7 +144,6 @@ Resolve_Init(RES_STAT *s) assert(s != NULL); s->resolver_fd = -1; s->pid = 0; - /* s->success must not be changed -- it will be set by other Resolve_*() functions */ } @@ -239,7 +246,7 @@ Do_ResolveName( const char *Host, int w_fd ) char ip[16]; struct hostent *h; struct in_addr *addr; - int len; + size_t len; Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host ); @@ -262,8 +269,7 @@ Do_ResolveName( const char *Host, int w_fd ) #endif /* Write result into pipe to parent */ len = strlen( ip ); - ip[len] = '\n'; len++; - if( (size_t)write( w_fd, ip, len ) != (size_t)len ) { + if ((size_t)write( w_fd, ip, len ) != len) { Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno )); close( w_fd ); } @@ -298,7 +304,7 @@ Get_Error( int H_Error ) static bool register_callback( RES_STAT *s, void (*cbfunc)(int, short)) { - assert(cbfunc); + assert(cbfunc != NULL); assert(s != NULL); assert(s->resolver_fd >= 0); @@ -307,7 +313,8 @@ register_callback( RES_STAT *s, void (*cbfunc)(int, short)) return true; Log( LOG_CRIT, "Resolver: Could not register callback function: %s!", strerror(errno)); - Resolve_Shutdown(s); + close(s->resolver_fd); + Resolve_Init(s); return false; } @@ -327,38 +334,32 @@ Resolve_Shutdown( RES_STAT *s) return ret; } - + +/** + * Read result of resolver sub-process from pipe + */ GLOBAL size_t Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen) { - /* Read result of resolver sub-process from pipe */ - int err, bytes_read; + ssize_t bytes_read; + assert(buflen > 0); /* Read result from pipe */ - errno = 0; bytes_read = read(s->resolver_fd, readbuf, buflen); if (bytes_read < 0) { - if (errno != EAGAIN) { - err = errno; - Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(err)); - Resolve_Shutdown(s); - errno = err; + if (errno == EAGAIN) return 0; - } - return 0; - } - Resolve_Shutdown(s); - if (bytes_read == 0) { /* EOF: lookup failed */ + Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(errno)); + bytes_read = 0; + } #ifdef DEBUG + else if (bytes_read == 0) Log( LOG_DEBUG, "Resolver: Can't read result: EOF"); #endif - return 0; - } - - s->success = true; - return bytes_read; + Resolve_Shutdown(s); + return (size_t)bytes_read; } /* -eof- */