2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
11 * Asynchronous resolver
17 static char UNUSED id[] = "$Id: resolve.c,v 1.14 2005/07/24 21:42:00 fw Exp $";
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
46 LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd ));
48 LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int w_fd ));
51 LOCAL void Do_ResolveName PARAMS(( char *Host, int w_fd ));
54 LOCAL char *Get_Error PARAMS(( int H_Error ));
57 LOCAL RES_STAT *New_Res_Stat PARAMS(( void ));
61 cb_resolver(int fd, short unused) {
62 (void) unused; /* shut up compiler warning */
63 Read_Resolver_Result(fd);
69 Resolve_Addr( struct sockaddr_in *Addr, int Sock )
72 Resolve_Addr( struct sockaddr_in *Addr )
75 /* Resolve IP (asynchronous!). On errors, e.g. if the child process
76 * can't be forked, this functions returns NULL. */
82 if( ! s ) return NULL;
90 Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
91 if (!io_setnonblock( s->pipe[0] )) {
92 Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] );
95 if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) {
96 Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s",
97 s->pipe[0], strerror(errno) );
107 Log_Init_Resolver( );
109 Do_ResolveAddr( Addr, Sock, s->pipe[1] );
111 Do_ResolveAddr( Addr, s->pipe[1] );
113 Log_Exit_Resolver( );
117 Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
127 Resolve_Name( char *Host )
129 /* Resolve hostname (asynchronous!). On errors, e.g. if the child
130 * process can't be forked, this functions returns NULL. */
136 if( ! s ) return NULL;
138 /* Fork sub-process */
144 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
145 if (!io_setnonblock( s->pipe[0] )) {
146 Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] );
149 if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) {
150 Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s",
151 s->pipe[0], strerror(errno) );
161 Log_Init_Resolver( );
162 Do_ResolveName( Host, s->pipe[1] );
163 Log_Exit_Resolver( );
167 Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
178 Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, int w_fd )
181 Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd )
184 /* Resolver sub-process: resolve IP address and write result into
187 char hostname[HOST_LEN];
188 char ipstr[HOST_LEN];
191 struct in_addr *addr;
196 /* Resolve IP address */
197 Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
198 h = gethostbyaddr( (char *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
201 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
203 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
205 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
207 strlcpy( hostname, h->h_name, sizeof( hostname ));
209 h = gethostbyname( hostname );
211 if (memcmp(h->h_addr, &Addr->sin_addr, sizeof (struct in_addr))) {
212 addr = (struct in_addr*) h->h_addr;
213 strlcpy(ipstr, inet_ntoa(*addr), sizeof ipstr);
214 Log(LOG_WARNING,"Possible forgery: %s resolved to %s (which is at ip %s!)",
215 inet_ntoa( Addr->sin_addr), hostname, ipstr);
216 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
219 Log(LOG_WARNING, "Possible forgery: %s resolved to %s (which has no ip address)",
220 inet_ntoa( Addr->sin_addr ), hostname);
221 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
224 Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
226 /* Write resolver result into pipe to parent */
227 len = strlen( hostname );
228 hostname[len] = '\n'; len++;
229 if( (size_t)write( w_fd, hostname, len ) != (size_t)len )
231 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
237 /* Do "IDENT" (aka "AUTH") lookup and write result to parent */
238 Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock );
239 res = ident_id( Sock, 10 );
240 Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" );
242 /* Write IDENT result into pipe to parent */
249 if( (size_t)write( w_fd, res ? res : "\n", len ) != (size_t)len )
251 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent (IDENT): %s!", strerror( errno ));
256 } /* Do_ResolveAddr */
260 Do_ResolveName( char *Host, int w_fd )
262 /* Resolver sub-process: resolve name and write result into pipe
267 struct in_addr *addr;
270 Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
272 /* Resolve hostname */
273 h = gethostbyname( Host );
276 addr = (struct in_addr *)h->h_addr;
277 strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
282 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
284 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
288 if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
290 /* Write result into pipe to parent */
292 ip[len] = '\n'; len++;
293 if( (size_t)write( w_fd, ip, len ) != (size_t)len )
295 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
298 } /* Do_ResolveName */
304 Get_Error( int H_Error )
306 /* Get error message for H_Error */
311 return "host not found";
313 return "name valid but no IP address defined";
315 return "name server error";
317 return "name server temporary not available";
319 return "unknown error";
331 /* Allocate memory */
332 s = (RES_STAT *)malloc( sizeof( RES_STAT ));
335 Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
339 /* Initialize pipe for result */
340 if( pipe( s->pipe ) != 0 )
343 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));