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.19 2005/09/03 11:17:16 fw Exp $";
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
46 static void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd ));
48 static void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int w_fd ));
51 static void Do_ResolveName PARAMS(( char *Host, int w_fd ));
54 static char *Get_Error PARAMS(( int H_Error ));
57 static 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 ));
128 Resolve_Name( char *Host )
130 /* Resolve hostname (asynchronous!). On errors, e.g. if the child
131 * process can't be forked, this functions returns NULL. */
137 if( ! s ) return NULL;
139 /* Fork sub-process */
145 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
146 if (!io_setnonblock( s->pipe[0] )) {
147 Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] );
150 if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) {
151 Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s",
152 s->pipe[0], strerror(errno) );
162 Log_Init_Resolver( );
163 Do_ResolveName( Host, s->pipe[1] );
164 Log_Exit_Resolver( );
168 Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
180 Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, int w_fd )
183 Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd )
186 /* Resolver sub-process: resolve IP address and write result into
189 char hostname[HOST_LEN];
190 char ipstr[HOST_LEN];
193 struct in_addr *addr;
200 array_init(&resolved_addr);
202 /* Resolve IP address */
204 Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
206 h = gethostbyaddr( (char *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
209 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
211 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
213 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
215 strlcpy( hostname, h->h_name, sizeof( hostname ));
217 h = gethostbyname( hostname );
219 if (memcmp(h->h_addr, &Addr->sin_addr, sizeof (struct in_addr))) {
220 addr = (struct in_addr*) h->h_addr;
221 strlcpy(ipstr, inet_ntoa(*addr), sizeof ipstr);
222 ntoaptr = inet_ntoa( Addr->sin_addr );
223 Log(LOG_WARNING,"Possible forgery: %s resolved to %s (which is at ip %s!)",
224 ntoaptr, hostname, ipstr);
225 strlcpy( hostname, ntoaptr, sizeof hostname);
228 ntoaptr = inet_ntoa( Addr->sin_addr );
229 Log(LOG_WARNING, "Possible forgery: %s resolved to %s (which has no ip address)",
231 strlcpy( hostname, ntoaptr, sizeof hostname);
234 Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
236 len = strlen( hostname );
237 hostname[len] = '\n'; len++;
238 if (!array_copyb(&resolved_addr, hostname, len )) {
239 Log_Resolver( LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror( errno ));
245 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
246 Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock );
247 res = ident_id( Sock, 10 );
248 Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" );
251 if (!array_cats(&resolved_addr, res))
252 Log_Resolver(LOG_WARNING, "Resolver: Cannot copy IDENT result: %s!", strerror(errno));
253 /* try to omit ident and return hostname only */
256 if (!array_catb(&resolved_addr, "\n", 1)) {
258 Log_Resolver(LOG_CRIT, "Resolver: Cannot copy result: %s!", strerror(errno));
259 array_free(&resolved_addr);
265 len = array_bytes(&resolved_addr);
266 if( (size_t)write( w_fd, array_start(&resolved_addr), len) != len )
267 Log_Resolver( LOG_CRIT, "Resolver: Can't write result to parent: %s!", strerror( errno ));
270 array_free(&resolved_addr);
271 } /* Do_ResolveAddr */
275 Do_ResolveName( char *Host, int w_fd )
277 /* Resolver sub-process: resolve name and write result into pipe
282 struct in_addr *addr;
285 Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
287 /* Resolve hostname */
288 h = gethostbyname( Host );
291 addr = (struct in_addr *)h->h_addr;
292 strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
297 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
299 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
303 if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
305 /* Write result into pipe to parent */
307 ip[len] = '\n'; len++;
308 if( (size_t)write( w_fd, ip, len ) != (size_t)len )
310 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
313 } /* Do_ResolveName */
319 Get_Error( int H_Error )
321 /* Get error message for H_Error */
326 return "host not found";
328 return "name valid but no IP address defined";
330 return "name server error";
332 return "name server temporary not available";
334 return "unknown error";
346 /* Allocate memory */
347 s = (RES_STAT *)malloc( sizeof( RES_STAT ));
350 Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
354 /* Initialize pipe for result */
355 if( pipe( s->pipe ) != 0 )
358 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
363 array_init(&s->buffer);