X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fngircd%2Fresolve.c;h=2f0534a58de25bd0bfd4f285d9fc308e5ee78350;hb=a17745d6d7c37bebbd5e0cb651579a727389a87f;hp=a5ba32e4cd5cc32ca1bf83725702e98b013fd58c;hpb=77f54693ef258b1fe65ee105fc026dfb2c6257dc;p=ngircd-alex.git diff --git a/src/ngircd/resolve.c b/src/ngircd/resolve.c index a5ba32e4..2f0534a5 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.17 2005/07/31 20:13:08 alex Exp $"; +static char UNUSED id[] = "$Id: resolve.c,v 1.22 2006/02/08 15:20:21 fw Exp $"; #include "imp.h" #include @@ -42,144 +42,106 @@ static char UNUSED id[] = "$Id: resolve.c,v 1.17 2005/07/31 20:13:08 alex Exp $" #include "io.h" -#ifdef IDENTAUTH static void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd )); -#else -static void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int w_fd )); -#endif - -static void Do_ResolveName PARAMS(( char *Host, int w_fd )); +static void Do_ResolveName PARAMS(( const char *Host, int w_fd )); +static bool register_callback PARAMS((RES_STAT *s, void (*cbfunc)(int, short))); #ifdef h_errno static char *Get_Error PARAMS(( int H_Error )); #endif -static RES_STAT *New_Res_Stat PARAMS(( void )); - +static int +Resolver_fork(int *pipefds) +{ + int pid; + if (pipe(pipefds) != 0) { + Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno )); + return -1; + } -static void -cb_resolver(int fd, short unused) { - (void) unused; /* shut up compiler warning */ - Read_Resolver_Result(fd); + pid = fork(); + switch(pid) { + case -1: + Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno )); + close(pipefds[0]); + close(pipefds[1]); + return -1; + case 0: /* child */ + close(pipefds[0]); + Log_Init_Resolver( ); + return 0; + } + /* parent */ + close(pipefds[1]); + return pid; } -#ifdef IDENTAUTH -GLOBAL RES_STAT * -Resolve_Addr( struct sockaddr_in *Addr, int Sock ) -#else -GLOBAL RES_STAT * -Resolve_Addr( struct sockaddr_in *Addr ) -#endif +GLOBAL bool +Resolve_Addr( RES_STAT *s, struct sockaddr_in *Addr, int identsock, void (*cbfunc)(int, short)) { - /* Resolve IP (asynchronous!). On errors, e.g. if the child process - * can't be forked, this functions returns NULL. */ - - RES_STAT *s; - int pid; - - s = New_Res_Stat( ); - if( ! s ) return NULL; + /* Resolve IP (asynchronous!). */ + int pid, pipefd[2]; + assert(s != NULL); + s->success = false; - /* For sub-process */ - pid = fork( ); - if( pid > 0 ) - { - close( s->pipe[1] ); - /* Main process */ + pid = Resolver_fork(pipefd); + if (pid > 0) { +#ifdef DEBUG Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid ); - if (!io_setnonblock( s->pipe[0] )) { - Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] ); - goto out; - } - if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) { - Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s", - s->pipe[0], strerror(errno) ); - goto out; - } +#endif s->pid = pid; - return s; - } - else if( pid == 0 ) - { - close( s->pipe[0] ); + s->resolver_fd = pipefd[0]; + return register_callback(s, cbfunc); + } else if( pid == 0 ) { /* Sub process */ - Log_Init_Resolver( ); -#ifdef IDENTAUTH - Do_ResolveAddr( Addr, Sock, s->pipe[1] ); -#else - Do_ResolveAddr( Addr, s->pipe[1] ); -#endif + Do_ResolveAddr( Addr, identsock, pipefd[1]); Log_Exit_Resolver( ); - exit( 0 ); + exit(0); } - - Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno )); - -out: /* Error! */ - close( s->pipe[0] ); - free( s ); -return NULL; + return false; } /* Resolve_Addr */ -GLOBAL RES_STAT * -Resolve_Name( char *Host ) +GLOBAL bool +Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short)) { - /* Resolve hostname (asynchronous!). On errors, e.g. if the child - * process can't be forked, this functions returns NULL. */ + /* Resolve hostname (asynchronous!). */ + int pid, pipefd[2]; + assert(s != NULL); + s->success = false; - RES_STAT *s; - int pid; - - s = New_Res_Stat( ); - if( ! s ) return NULL; - - /* Fork sub-process */ - pid = fork( ); - if( pid > 0 ) - { - close( s->pipe[1] ); + pid = Resolver_fork(pipefd); + if (pid > 0) { /* Main process */ +#ifdef DEBUG Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid ); - if (!io_setnonblock( s->pipe[0] )) { - Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] ); - goto out; - } - if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) { - Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s", - s->pipe[0], strerror(errno) ); - goto out; - } +#endif s->pid = pid; - return s; - } - else if( pid == 0 ) - { - close( s->pipe[0] ); + s->resolver_fd = pipefd[0]; + return register_callback(s, cbfunc); + } else if( pid == 0 ) { /* Sub process */ - Log_Init_Resolver( ); - Do_ResolveName( Host, s->pipe[1] ); + Do_ResolveName(Host, pipefd[1]); Log_Exit_Resolver( ); - exit( 0 ); + exit(0); } + return false; +} /* Resolve_Name */ - Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno )); -out: /* Error! */ - close( s->pipe[0] ); - free( s ); - return NULL; -} /* Resolve_Name */ +GLOBAL void +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 */ +} -#ifdef IDENTAUTH static void -Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, int w_fd ) -#else -static void -Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd ) -#endif +Do_ResolveAddr( struct sockaddr_in *Addr, int identsock, int w_fd ) { /* Resolver sub-process: resolve IP address and write result into * pipe to parent. */ @@ -190,10 +152,11 @@ Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd ) size_t len; struct in_addr *addr; char *ntoaptr; + array resolved_addr; #ifdef IDENTAUTH char *res; #endif - + array_init(&resolved_addr); /* Resolve IP address */ #ifdef DEBUG Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr )); @@ -228,41 +191,47 @@ Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd ) } Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname ); - /* Write resolver result into pipe to parent */ len = strlen( hostname ); hostname[len] = '\n'; len++; - if( (size_t)write( w_fd, hostname, len ) != (size_t)len ) - { - Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno )); + if (!array_copyb(&resolved_addr, hostname, len )) { + Log_Resolver( LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror( errno )); close( w_fd ); return; } #ifdef IDENTAUTH - /* Do "IDENT" (aka "AUTH") lookup and write result to parent */ - Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock ); - res = ident_id( Sock, 10 ); - Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" ); - - /* Write IDENT result into pipe to parent */ - if (res) { - len = strlen(res); - res[len] = '\n'; - len++; - } else len = 1; - - if( (size_t)write( w_fd, res ? res : "\n", len ) != (size_t)len ) - { - Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent (IDENT): %s!", strerror( errno )); - close( w_fd ); + assert(identsock >= 0); + if (identsock >= 0) { + /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */ +#ifdef DEBUG + Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", identsock ); +#endif + res = ident_id( identsock, 10 ); +#ifdef DEBUG + Log_Resolver(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", + identsock, res ? res : "(NULL)" ); +#endif + if (res && !array_cats(&resolved_addr, res)) { + Log_Resolver(LOG_WARNING, "Resolver: Cannot copy IDENT result: %s!", strerror(errno)); + /* omit ident and return hostname only */ + } + + if (res) free(res); } - free( res ); +#else + (void)identsock; #endif + len = array_bytes(&resolved_addr); + if( (size_t)write( w_fd, array_start(&resolved_addr), len) != len ) + Log_Resolver( LOG_CRIT, "Resolver: Can't write result to parent: %s!", strerror( errno )); + + close(w_fd); + array_free(&resolved_addr); } /* Do_ResolveAddr */ static void -Do_ResolveName( char *Host, int w_fd ) +Do_ResolveName( const char *Host, int w_fd ) { /* Resolver sub-process: resolve name and write result into pipe * to parent. */ @@ -276,27 +245,24 @@ Do_ResolveName( char *Host, int w_fd ) /* Resolve hostname */ h = gethostbyname( Host ); - if( h ) - { + if( h ) { addr = (struct in_addr *)h->h_addr; strlcpy( ip, inet_ntoa( *addr ), sizeof( ip )); - } - else - { + } else { #ifdef h_errno Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno )); #else Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host ); #endif - ip[0] = '\0'; + close(w_fd); + return; } - if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip ); - +#ifdef DEBUG + Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip ); +#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( write( w_fd, ip, len ) != len) { Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno )); close( w_fd ); } @@ -328,33 +294,70 @@ Get_Error( int H_Error ) #endif -static RES_STAT * -New_Res_Stat( void ) +static bool +register_callback( RES_STAT *s, void (*cbfunc)(int, short)) { - RES_STAT *s; + assert(cbfunc); + assert(s != NULL); + assert(s->resolver_fd >= 0); - /* Allocate memory */ - s = (RES_STAT *)malloc( sizeof( RES_STAT )); - if( ! s ) - { - Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" ); - return NULL; - } + if (io_setnonblock(s->resolver_fd) && + io_event_create(s->resolver_fd, IO_WANTREAD, cbfunc)) + return true; + + Log( LOG_CRIT, "Resolver: Could not register callback function: %s!", strerror(errno)); + Resolve_Shutdown(s); + return false; +} - /* Initialize pipe for result */ - if( pipe( s->pipe ) != 0 ) - { - free( s ); - Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno )); - return NULL; - } - s->stage = 0; - array_init(&s->buffer); - s->pid = -1; +GLOBAL bool +Resolve_Shutdown( RES_STAT *s) +{ + bool ret = false; - return s; -} /* New_Res_Stat */ + assert(s != NULL); + assert(s->resolver_fd >= 0); + if (s->resolver_fd >= 0) + ret = io_close(s->resolver_fd); + + Resolve_Init(s); + return ret; +} + +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; + 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; + return 0; + } + return 0; + } + + Resolve_Shutdown(s); + if (bytes_read == 0) { /* EOF: lookup failed */ +#ifdef DEBUG + Log( LOG_DEBUG, "Resolver: Can't read result: EOF"); +#endif + return 0; + } + + s->success = true; + return bytes_read; +} /* -eof- */ +