2 46ec0f24 2002-05-27 alex * ngIRCd -- The Next Generation IRC Daemon
3 46ec0f24 2002-05-27 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
11 490f28ff 2002-12-12 alex * Asynchronous resolver
15 46ec0f24 2002-05-27 alex #include "portab.h"
17 695631b2 2002-12-26 alex static char UNUSED id[] = "$Id: resolve.c,v 1.5 2002/12/26 17:04:54 alex Exp $";
19 46ec0f24 2002-05-27 alex #include "imp.h"
20 46ec0f24 2002-05-27 alex #include <assert.h>
21 46ec0f24 2002-05-27 alex #include <errno.h>
22 46ec0f24 2002-05-27 alex #include <stdlib.h>
23 46ec0f24 2002-05-27 alex #include <string.h>
24 46ec0f24 2002-05-27 alex #include <unistd.h>
25 46ec0f24 2002-05-27 alex #include <sys/socket.h>
26 46ec0f24 2002-05-27 alex #include <netinet/in.h>
27 46ec0f24 2002-05-27 alex #include <arpa/inet.h>
28 46ec0f24 2002-05-27 alex #include <netdb.h>
30 46ec0f24 2002-05-27 alex #include "conn.h"
31 46ec0f24 2002-05-27 alex #include "defines.h"
32 46ec0f24 2002-05-27 alex #include "log.h"
34 46ec0f24 2002-05-27 alex #include "exp.h"
35 46ec0f24 2002-05-27 alex #include "resolve.h"
38 46ec0f24 2002-05-27 alex LOCAL VOID Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, INT w_fd ));
39 46ec0f24 2002-05-27 alex LOCAL VOID Do_ResolveName PARAMS(( CHAR *Host, INT w_fd ));
41 46ec0f24 2002-05-27 alex #ifdef h_errno
42 f7327524 2002-05-30 alex LOCAL CHAR *Get_Error PARAMS(( INT H_Error ));
47 46ec0f24 2002-05-27 alex Resolve_Init( VOID )
49 46ec0f24 2002-05-27 alex /* Modul initialisieren */
51 46ec0f24 2002-05-27 alex FD_ZERO( &Resolver_FDs );
52 46ec0f24 2002-05-27 alex } /* Resolve_Init */
55 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
56 46ec0f24 2002-05-27 alex Resolve_Addr( struct sockaddr_in *Addr )
58 46ec0f24 2002-05-27 alex /* IP (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
59 46ec0f24 2002-05-27 alex * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
60 46ec0f24 2002-05-27 alex * Der Host kann dann nicht aufgeloest werden. */
62 46ec0f24 2002-05-27 alex RES_STAT *s;
65 46ec0f24 2002-05-27 alex /* Speicher anfordern */
66 46ec0f24 2002-05-27 alex s = malloc( sizeof( RES_STAT ));
69 63c36773 2002-06-09 alex Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
70 46ec0f24 2002-05-27 alex return NULL;
73 46ec0f24 2002-05-27 alex /* Pipe fuer Antwort initialisieren */
74 46ec0f24 2002-05-27 alex if( pipe( s->pipe ) != 0 )
77 46ec0f24 2002-05-27 alex Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
78 46ec0f24 2002-05-27 alex return NULL;
81 46ec0f24 2002-05-27 alex /* Sub-Prozess erzeugen */
82 46ec0f24 2002-05-27 alex pid = fork( );
83 46ec0f24 2002-05-27 alex if( pid > 0 )
85 46ec0f24 2002-05-27 alex /* Haupt-Prozess */
86 46ec0f24 2002-05-27 alex Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
87 46ec0f24 2002-05-27 alex FD_SET( s->pipe[0], &Resolver_FDs );
88 46ec0f24 2002-05-27 alex if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
89 46ec0f24 2002-05-27 alex s->pid = pid;
92 46ec0f24 2002-05-27 alex else if( pid == 0 )
94 46ec0f24 2002-05-27 alex /* Sub-Prozess */
95 46ec0f24 2002-05-27 alex Log_Init_Resolver( );
96 46ec0f24 2002-05-27 alex Do_ResolveAddr( Addr, s->pipe[1] );
97 46ec0f24 2002-05-27 alex Log_Exit_Resolver( );
102 46ec0f24 2002-05-27 alex /* Fehler */
104 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
105 46ec0f24 2002-05-27 alex return NULL;
107 46ec0f24 2002-05-27 alex } /* Resolve_Addr */
110 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
111 46ec0f24 2002-05-27 alex Resolve_Name( CHAR *Host )
113 46ec0f24 2002-05-27 alex /* Hostnamen (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
114 46ec0f24 2002-05-27 alex * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
115 46ec0f24 2002-05-27 alex * Der Host kann dann nicht aufgeloest werden. */
117 46ec0f24 2002-05-27 alex RES_STAT *s;
120 46ec0f24 2002-05-27 alex /* Speicher anfordern */
121 46ec0f24 2002-05-27 alex s = malloc( sizeof( RES_STAT ));
124 63c36773 2002-06-09 alex Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Name]" );
125 46ec0f24 2002-05-27 alex return NULL;
128 46ec0f24 2002-05-27 alex /* Pipe fuer Antwort initialisieren */
129 46ec0f24 2002-05-27 alex if( pipe( s->pipe ) != 0 )
132 46ec0f24 2002-05-27 alex Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
133 46ec0f24 2002-05-27 alex return NULL;
136 46ec0f24 2002-05-27 alex /* Sub-Prozess erzeugen */
137 46ec0f24 2002-05-27 alex pid = fork( );
138 46ec0f24 2002-05-27 alex if( pid > 0 )
140 46ec0f24 2002-05-27 alex /* Haupt-Prozess */
141 46ec0f24 2002-05-27 alex Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
142 46ec0f24 2002-05-27 alex FD_SET( s->pipe[0], &Resolver_FDs );
143 46ec0f24 2002-05-27 alex if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
144 46ec0f24 2002-05-27 alex s->pid = pid;
147 46ec0f24 2002-05-27 alex else if( pid == 0 )
149 46ec0f24 2002-05-27 alex /* Sub-Prozess */
150 46ec0f24 2002-05-27 alex Log_Init_Resolver( );
151 46ec0f24 2002-05-27 alex Do_ResolveName( Host, s->pipe[1] );
152 46ec0f24 2002-05-27 alex Log_Exit_Resolver( );
157 46ec0f24 2002-05-27 alex /* Fehler */
159 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
160 46ec0f24 2002-05-27 alex return NULL;
162 46ec0f24 2002-05-27 alex } /* Resolve_Name */
166 46ec0f24 2002-05-27 alex Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
168 46ec0f24 2002-05-27 alex /* Resolver Sub-Prozess: IP aufloesen und Ergebnis in Pipe schreiben. */
170 46ec0f24 2002-05-27 alex CHAR hostname[HOST_LEN];
171 46ec0f24 2002-05-27 alex struct hostent *h;
173 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
175 46ec0f24 2002-05-27 alex /* Namen aufloesen */
176 46ec0f24 2002-05-27 alex h = gethostbyaddr( (CHAR *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
177 695631b2 2002-12-26 alex if( h ) strlcpy( hostname, h->h_name, sizeof( hostname ));
180 46ec0f24 2002-05-27 alex #ifdef h_errno
181 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
183 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
185 695631b2 2002-12-26 alex strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
188 46ec0f24 2002-05-27 alex /* Antwort an Parent schreiben */
189 46ec0f24 2002-05-27 alex if( write( w_fd, hostname, strlen( hostname ) + 1 ) != ( strlen( hostname ) + 1 ))
191 46ec0f24 2002-05-27 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
192 46ec0f24 2002-05-27 alex close( w_fd );
196 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
197 46ec0f24 2002-05-27 alex } /* Do_ResolveAddr */
201 46ec0f24 2002-05-27 alex Do_ResolveName( CHAR *Host, INT w_fd )
203 46ec0f24 2002-05-27 alex /* Resolver Sub-Prozess: Name aufloesen und Ergebnis in Pipe schreiben. */
205 46ec0f24 2002-05-27 alex CHAR ip[16];
206 46ec0f24 2002-05-27 alex struct hostent *h;
207 46ec0f24 2002-05-27 alex struct in_addr *addr;
209 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
211 46ec0f24 2002-05-27 alex /* Namen aufloesen */
212 46ec0f24 2002-05-27 alex h = gethostbyname( Host );
215 46ec0f24 2002-05-27 alex addr = (struct in_addr *)h->h_addr;
216 695631b2 2002-12-26 alex strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
220 46ec0f24 2002-05-27 alex #ifdef h_errno
221 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
223 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
225 46ec0f24 2002-05-27 alex strcpy( ip, "" );
228 46ec0f24 2002-05-27 alex /* Antwort an Parent schreiben */
229 46ec0f24 2002-05-27 alex if( write( w_fd, ip, strlen( ip ) + 1 ) != ( strlen( ip ) + 1 ))
231 46ec0f24 2002-05-27 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
232 46ec0f24 2002-05-27 alex close( w_fd );
236 46ec0f24 2002-05-27 alex if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
237 46ec0f24 2002-05-27 alex } /* Do_ResolveName */
240 46ec0f24 2002-05-27 alex #ifdef h_errno
242 46ec0f24 2002-05-27 alex LOCAL CHAR *
243 46ec0f24 2002-05-27 alex Get_Error( INT H_Error )
245 46ec0f24 2002-05-27 alex /* Fehlerbeschreibung fuer H_Error liefern */
247 46ec0f24 2002-05-27 alex switch( H_Error )
249 46ec0f24 2002-05-27 alex case HOST_NOT_FOUND:
250 46ec0f24 2002-05-27 alex return "host not found";
251 46ec0f24 2002-05-27 alex case NO_DATA:
252 46ec0f24 2002-05-27 alex return "name valid but no IP address defined";
253 46ec0f24 2002-05-27 alex case NO_RECOVERY:
254 46ec0f24 2002-05-27 alex return "name server error";
255 46ec0f24 2002-05-27 alex case TRY_AGAIN:
256 46ec0f24 2002-05-27 alex return "name server temporary not available";
258 46ec0f24 2002-05-27 alex return "unknown error";
260 46ec0f24 2002-05-27 alex } /* Get_Error */
265 46ec0f24 2002-05-27 alex /* -eof- */