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 46ec0f24 2002-05-27 alex * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 46ec0f24 2002-05-27 alex * der GNU General Public License (GPL), wie von der Free Software Foundation
7 46ec0f24 2002-05-27 alex * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 46ec0f24 2002-05-27 alex * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 46ec0f24 2002-05-27 alex * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 46ec0f24 2002-05-27 alex * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 63c36773 2002-06-09 alex * $Id: resolve.c,v 1.3 2002/06/09 13:19:08 alex Exp $
14 46ec0f24 2002-05-27 alex * resolve.c: asyncroner Resolver
18 46ec0f24 2002-05-27 alex #include "portab.h"
20 46ec0f24 2002-05-27 alex #include "imp.h"
21 46ec0f24 2002-05-27 alex #include <assert.h>
22 46ec0f24 2002-05-27 alex #include <errno.h>
23 46ec0f24 2002-05-27 alex #include <stdlib.h>
24 46ec0f24 2002-05-27 alex #include <string.h>
25 46ec0f24 2002-05-27 alex #include <unistd.h>
26 46ec0f24 2002-05-27 alex #include <sys/socket.h>
27 46ec0f24 2002-05-27 alex #include <netinet/in.h>
28 46ec0f24 2002-05-27 alex #include <arpa/inet.h>
29 46ec0f24 2002-05-27 alex #include <netdb.h>
31 46ec0f24 2002-05-27 alex #include "conn.h"
32 46ec0f24 2002-05-27 alex #include "defines.h"
33 46ec0f24 2002-05-27 alex #include "log.h"
35 46ec0f24 2002-05-27 alex #include "exp.h"
36 46ec0f24 2002-05-27 alex #include "resolve.h"
39 46ec0f24 2002-05-27 alex LOCAL VOID Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, INT w_fd ));
40 46ec0f24 2002-05-27 alex LOCAL VOID Do_ResolveName PARAMS(( CHAR *Host, INT w_fd ));
42 46ec0f24 2002-05-27 alex #ifdef h_errno
43 f7327524 2002-05-30 alex LOCAL CHAR *Get_Error PARAMS(( INT H_Error ));
48 46ec0f24 2002-05-27 alex Resolve_Init( VOID )
50 46ec0f24 2002-05-27 alex /* Modul initialisieren */
52 46ec0f24 2002-05-27 alex FD_ZERO( &Resolver_FDs );
53 46ec0f24 2002-05-27 alex } /* Resolve_Init */
56 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
57 46ec0f24 2002-05-27 alex Resolve_Addr( struct sockaddr_in *Addr )
59 46ec0f24 2002-05-27 alex /* IP (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
60 46ec0f24 2002-05-27 alex * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
61 46ec0f24 2002-05-27 alex * Der Host kann dann nicht aufgeloest werden. */
63 46ec0f24 2002-05-27 alex RES_STAT *s;
66 46ec0f24 2002-05-27 alex /* Speicher anfordern */
67 46ec0f24 2002-05-27 alex s = malloc( sizeof( RES_STAT ));
70 63c36773 2002-06-09 alex Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
71 46ec0f24 2002-05-27 alex return NULL;
74 46ec0f24 2002-05-27 alex /* Pipe fuer Antwort initialisieren */
75 46ec0f24 2002-05-27 alex if( pipe( s->pipe ) != 0 )
78 46ec0f24 2002-05-27 alex Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
79 46ec0f24 2002-05-27 alex return NULL;
82 46ec0f24 2002-05-27 alex /* Sub-Prozess erzeugen */
83 46ec0f24 2002-05-27 alex pid = fork( );
84 46ec0f24 2002-05-27 alex if( pid > 0 )
86 46ec0f24 2002-05-27 alex /* Haupt-Prozess */
87 46ec0f24 2002-05-27 alex Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
88 46ec0f24 2002-05-27 alex FD_SET( s->pipe[0], &Resolver_FDs );
89 46ec0f24 2002-05-27 alex if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
90 46ec0f24 2002-05-27 alex s->pid = pid;
93 46ec0f24 2002-05-27 alex else if( pid == 0 )
95 46ec0f24 2002-05-27 alex /* Sub-Prozess */
96 46ec0f24 2002-05-27 alex Log_Init_Resolver( );
97 46ec0f24 2002-05-27 alex Do_ResolveAddr( Addr, s->pipe[1] );
98 46ec0f24 2002-05-27 alex Log_Exit_Resolver( );
103 46ec0f24 2002-05-27 alex /* Fehler */
105 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
106 46ec0f24 2002-05-27 alex return NULL;
108 46ec0f24 2002-05-27 alex } /* Resolve_Addr */
111 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
112 46ec0f24 2002-05-27 alex Resolve_Name( CHAR *Host )
114 46ec0f24 2002-05-27 alex /* Hostnamen (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
115 46ec0f24 2002-05-27 alex * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
116 46ec0f24 2002-05-27 alex * Der Host kann dann nicht aufgeloest werden. */
118 46ec0f24 2002-05-27 alex RES_STAT *s;
121 46ec0f24 2002-05-27 alex /* Speicher anfordern */
122 46ec0f24 2002-05-27 alex s = malloc( sizeof( RES_STAT ));
125 63c36773 2002-06-09 alex Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Name]" );
126 46ec0f24 2002-05-27 alex return NULL;
129 46ec0f24 2002-05-27 alex /* Pipe fuer Antwort initialisieren */
130 46ec0f24 2002-05-27 alex if( pipe( s->pipe ) != 0 )
133 46ec0f24 2002-05-27 alex Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
134 46ec0f24 2002-05-27 alex return NULL;
137 46ec0f24 2002-05-27 alex /* Sub-Prozess erzeugen */
138 46ec0f24 2002-05-27 alex pid = fork( );
139 46ec0f24 2002-05-27 alex if( pid > 0 )
141 46ec0f24 2002-05-27 alex /* Haupt-Prozess */
142 46ec0f24 2002-05-27 alex Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
143 46ec0f24 2002-05-27 alex FD_SET( s->pipe[0], &Resolver_FDs );
144 46ec0f24 2002-05-27 alex if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
145 46ec0f24 2002-05-27 alex s->pid = pid;
148 46ec0f24 2002-05-27 alex else if( pid == 0 )
150 46ec0f24 2002-05-27 alex /* Sub-Prozess */
151 46ec0f24 2002-05-27 alex Log_Init_Resolver( );
152 46ec0f24 2002-05-27 alex Do_ResolveName( Host, s->pipe[1] );
153 46ec0f24 2002-05-27 alex Log_Exit_Resolver( );
158 46ec0f24 2002-05-27 alex /* Fehler */
160 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
161 46ec0f24 2002-05-27 alex return NULL;
163 46ec0f24 2002-05-27 alex } /* Resolve_Name */
167 46ec0f24 2002-05-27 alex Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
169 46ec0f24 2002-05-27 alex /* Resolver Sub-Prozess: IP aufloesen und Ergebnis in Pipe schreiben. */
171 46ec0f24 2002-05-27 alex CHAR hostname[HOST_LEN];
172 46ec0f24 2002-05-27 alex struct hostent *h;
174 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
176 46ec0f24 2002-05-27 alex /* Namen aufloesen */
177 46ec0f24 2002-05-27 alex h = gethostbyaddr( (CHAR *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
178 46ec0f24 2002-05-27 alex if( h ) strcpy( hostname, h->h_name );
181 46ec0f24 2002-05-27 alex #ifdef h_errno
182 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
184 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
186 46ec0f24 2002-05-27 alex strcpy( hostname, inet_ntoa( Addr->sin_addr ));
189 46ec0f24 2002-05-27 alex /* Antwort an Parent schreiben */
190 46ec0f24 2002-05-27 alex if( write( w_fd, hostname, strlen( hostname ) + 1 ) != ( strlen( hostname ) + 1 ))
192 46ec0f24 2002-05-27 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
193 46ec0f24 2002-05-27 alex close( w_fd );
197 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
198 46ec0f24 2002-05-27 alex } /* Do_ResolveAddr */
202 46ec0f24 2002-05-27 alex Do_ResolveName( CHAR *Host, INT w_fd )
204 46ec0f24 2002-05-27 alex /* Resolver Sub-Prozess: Name aufloesen und Ergebnis in Pipe schreiben. */
206 46ec0f24 2002-05-27 alex CHAR ip[16];
207 46ec0f24 2002-05-27 alex struct hostent *h;
208 46ec0f24 2002-05-27 alex struct in_addr *addr;
210 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
212 46ec0f24 2002-05-27 alex /* Namen aufloesen */
213 46ec0f24 2002-05-27 alex h = gethostbyname( Host );
216 46ec0f24 2002-05-27 alex addr = (struct in_addr *)h->h_addr;
217 46ec0f24 2002-05-27 alex strcpy( ip, inet_ntoa( *addr ));
221 46ec0f24 2002-05-27 alex #ifdef h_errno
222 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
224 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
226 46ec0f24 2002-05-27 alex strcpy( ip, "" );
229 46ec0f24 2002-05-27 alex /* Antwort an Parent schreiben */
230 46ec0f24 2002-05-27 alex if( write( w_fd, ip, strlen( ip ) + 1 ) != ( strlen( ip ) + 1 ))
232 46ec0f24 2002-05-27 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
233 46ec0f24 2002-05-27 alex close( w_fd );
237 46ec0f24 2002-05-27 alex if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
238 46ec0f24 2002-05-27 alex } /* Do_ResolveName */
241 46ec0f24 2002-05-27 alex #ifdef h_errno
243 46ec0f24 2002-05-27 alex LOCAL CHAR *
244 46ec0f24 2002-05-27 alex Get_Error( INT H_Error )
246 46ec0f24 2002-05-27 alex /* Fehlerbeschreibung fuer H_Error liefern */
248 46ec0f24 2002-05-27 alex switch( H_Error )
250 46ec0f24 2002-05-27 alex case HOST_NOT_FOUND:
251 46ec0f24 2002-05-27 alex return "host not found";
252 46ec0f24 2002-05-27 alex case NO_DATA:
253 46ec0f24 2002-05-27 alex return "name valid but no IP address defined";
254 46ec0f24 2002-05-27 alex case NO_RECOVERY:
255 46ec0f24 2002-05-27 alex return "name server error";
256 46ec0f24 2002-05-27 alex case TRY_AGAIN:
257 46ec0f24 2002-05-27 alex return "name server temporary not available";
259 46ec0f24 2002-05-27 alex return "unknown error";
261 46ec0f24 2002-05-27 alex } /* Get_Error */
266 46ec0f24 2002-05-27 alex /* -eof- */