Blame


1 46ec0f24 2002-05-27 alex /*
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)
4 46ec0f24 2002-05-27 alex *
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.
10 46ec0f24 2002-05-27 alex *
11 490f28ff 2002-12-12 alex * Asynchronous resolver
12 46ec0f24 2002-05-27 alex */
13 46ec0f24 2002-05-27 alex
14 46ec0f24 2002-05-27 alex
15 46ec0f24 2002-05-27 alex #include "portab.h"
16 46ec0f24 2002-05-27 alex
17 695631b2 2002-12-26 alex static char UNUSED id[] = "$Id: resolve.c,v 1.5 2002/12/26 17:04:54 alex Exp $";
18 490f28ff 2002-12-12 alex
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>
29 46ec0f24 2002-05-27 alex
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"
33 46ec0f24 2002-05-27 alex
34 46ec0f24 2002-05-27 alex #include "exp.h"
35 46ec0f24 2002-05-27 alex #include "resolve.h"
36 46ec0f24 2002-05-27 alex
37 46ec0f24 2002-05-27 alex
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 ));
40 46ec0f24 2002-05-27 alex
41 46ec0f24 2002-05-27 alex #ifdef h_errno
42 f7327524 2002-05-30 alex LOCAL CHAR *Get_Error PARAMS(( INT H_Error ));
43 46ec0f24 2002-05-27 alex #endif
44 46ec0f24 2002-05-27 alex
45 46ec0f24 2002-05-27 alex
46 46ec0f24 2002-05-27 alex GLOBAL VOID
47 46ec0f24 2002-05-27 alex Resolve_Init( VOID )
48 46ec0f24 2002-05-27 alex {
49 46ec0f24 2002-05-27 alex /* Modul initialisieren */
50 46ec0f24 2002-05-27 alex
51 46ec0f24 2002-05-27 alex FD_ZERO( &Resolver_FDs );
52 46ec0f24 2002-05-27 alex } /* Resolve_Init */
53 46ec0f24 2002-05-27 alex
54 46ec0f24 2002-05-27 alex
55 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
56 46ec0f24 2002-05-27 alex Resolve_Addr( struct sockaddr_in *Addr )
57 46ec0f24 2002-05-27 alex {
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. */
61 46ec0f24 2002-05-27 alex
62 46ec0f24 2002-05-27 alex RES_STAT *s;
63 46ec0f24 2002-05-27 alex INT pid;
64 46ec0f24 2002-05-27 alex
65 46ec0f24 2002-05-27 alex /* Speicher anfordern */
66 46ec0f24 2002-05-27 alex s = malloc( sizeof( RES_STAT ));
67 46ec0f24 2002-05-27 alex if( ! s )
68 46ec0f24 2002-05-27 alex {
69 63c36773 2002-06-09 alex Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
70 46ec0f24 2002-05-27 alex return NULL;
71 46ec0f24 2002-05-27 alex }
72 46ec0f24 2002-05-27 alex
73 46ec0f24 2002-05-27 alex /* Pipe fuer Antwort initialisieren */
74 46ec0f24 2002-05-27 alex if( pipe( s->pipe ) != 0 )
75 46ec0f24 2002-05-27 alex {
76 46ec0f24 2002-05-27 alex free( s );
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;
79 46ec0f24 2002-05-27 alex }
80 46ec0f24 2002-05-27 alex
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 )
84 46ec0f24 2002-05-27 alex {
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;
90 46ec0f24 2002-05-27 alex return s;
91 46ec0f24 2002-05-27 alex }
92 46ec0f24 2002-05-27 alex else if( pid == 0 )
93 46ec0f24 2002-05-27 alex {
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( );
98 46ec0f24 2002-05-27 alex exit( 0 );
99 46ec0f24 2002-05-27 alex }
100 46ec0f24 2002-05-27 alex else
101 46ec0f24 2002-05-27 alex {
102 46ec0f24 2002-05-27 alex /* Fehler */
103 46ec0f24 2002-05-27 alex free( s );
104 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
105 46ec0f24 2002-05-27 alex return NULL;
106 46ec0f24 2002-05-27 alex }
107 46ec0f24 2002-05-27 alex } /* Resolve_Addr */
108 46ec0f24 2002-05-27 alex
109 46ec0f24 2002-05-27 alex
110 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
111 46ec0f24 2002-05-27 alex Resolve_Name( CHAR *Host )
112 46ec0f24 2002-05-27 alex {
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. */
116 46ec0f24 2002-05-27 alex
117 46ec0f24 2002-05-27 alex RES_STAT *s;
118 46ec0f24 2002-05-27 alex INT pid;
119 46ec0f24 2002-05-27 alex
120 46ec0f24 2002-05-27 alex /* Speicher anfordern */
121 46ec0f24 2002-05-27 alex s = malloc( sizeof( RES_STAT ));
122 46ec0f24 2002-05-27 alex if( ! s )
123 46ec0f24 2002-05-27 alex {
124 63c36773 2002-06-09 alex Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Name]" );
125 46ec0f24 2002-05-27 alex return NULL;
126 46ec0f24 2002-05-27 alex }
127 46ec0f24 2002-05-27 alex
128 46ec0f24 2002-05-27 alex /* Pipe fuer Antwort initialisieren */
129 46ec0f24 2002-05-27 alex if( pipe( s->pipe ) != 0 )
130 46ec0f24 2002-05-27 alex {
131 46ec0f24 2002-05-27 alex free( s );
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;
134 46ec0f24 2002-05-27 alex }
135 46ec0f24 2002-05-27 alex
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 )
139 46ec0f24 2002-05-27 alex {
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;
145 46ec0f24 2002-05-27 alex return s;
146 46ec0f24 2002-05-27 alex }
147 46ec0f24 2002-05-27 alex else if( pid == 0 )
148 46ec0f24 2002-05-27 alex {
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( );
153 46ec0f24 2002-05-27 alex exit( 0 );
154 46ec0f24 2002-05-27 alex }
155 46ec0f24 2002-05-27 alex else
156 46ec0f24 2002-05-27 alex {
157 46ec0f24 2002-05-27 alex /* Fehler */
158 46ec0f24 2002-05-27 alex free( s );
159 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
160 46ec0f24 2002-05-27 alex return NULL;
161 46ec0f24 2002-05-27 alex }
162 46ec0f24 2002-05-27 alex } /* Resolve_Name */
163 46ec0f24 2002-05-27 alex
164 46ec0f24 2002-05-27 alex
165 46ec0f24 2002-05-27 alex LOCAL VOID
166 46ec0f24 2002-05-27 alex Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
167 46ec0f24 2002-05-27 alex {
168 46ec0f24 2002-05-27 alex /* Resolver Sub-Prozess: IP aufloesen und Ergebnis in Pipe schreiben. */
169 46ec0f24 2002-05-27 alex
170 46ec0f24 2002-05-27 alex CHAR hostname[HOST_LEN];
171 46ec0f24 2002-05-27 alex struct hostent *h;
172 46ec0f24 2002-05-27 alex
173 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
174 46ec0f24 2002-05-27 alex
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 ));
178 46ec0f24 2002-05-27 alex else
179 46ec0f24 2002-05-27 alex {
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 ));
182 46ec0f24 2002-05-27 alex #else
183 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
184 46ec0f24 2002-05-27 alex #endif
185 695631b2 2002-12-26 alex strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
186 46ec0f24 2002-05-27 alex }
187 46ec0f24 2002-05-27 alex
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 ))
190 46ec0f24 2002-05-27 alex {
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 );
193 46ec0f24 2002-05-27 alex return;
194 46ec0f24 2002-05-27 alex }
195 46ec0f24 2002-05-27 alex
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 */
198 46ec0f24 2002-05-27 alex
199 46ec0f24 2002-05-27 alex
200 46ec0f24 2002-05-27 alex LOCAL VOID
201 46ec0f24 2002-05-27 alex Do_ResolveName( CHAR *Host, INT w_fd )
202 46ec0f24 2002-05-27 alex {
203 46ec0f24 2002-05-27 alex /* Resolver Sub-Prozess: Name aufloesen und Ergebnis in Pipe schreiben. */
204 46ec0f24 2002-05-27 alex
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;
208 46ec0f24 2002-05-27 alex
209 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
210 46ec0f24 2002-05-27 alex
211 46ec0f24 2002-05-27 alex /* Namen aufloesen */
212 46ec0f24 2002-05-27 alex h = gethostbyname( Host );
213 46ec0f24 2002-05-27 alex if( h )
214 46ec0f24 2002-05-27 alex {
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 ));
217 46ec0f24 2002-05-27 alex }
218 46ec0f24 2002-05-27 alex else
219 46ec0f24 2002-05-27 alex {
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 ));
222 46ec0f24 2002-05-27 alex #else
223 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
224 46ec0f24 2002-05-27 alex #endif
225 46ec0f24 2002-05-27 alex strcpy( ip, "" );
226 46ec0f24 2002-05-27 alex }
227 46ec0f24 2002-05-27 alex
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 ))
230 46ec0f24 2002-05-27 alex {
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 );
233 46ec0f24 2002-05-27 alex return;
234 46ec0f24 2002-05-27 alex }
235 46ec0f24 2002-05-27 alex
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 */
238 46ec0f24 2002-05-27 alex
239 46ec0f24 2002-05-27 alex
240 46ec0f24 2002-05-27 alex #ifdef h_errno
241 46ec0f24 2002-05-27 alex
242 46ec0f24 2002-05-27 alex LOCAL CHAR *
243 46ec0f24 2002-05-27 alex Get_Error( INT H_Error )
244 46ec0f24 2002-05-27 alex {
245 46ec0f24 2002-05-27 alex /* Fehlerbeschreibung fuer H_Error liefern */
246 46ec0f24 2002-05-27 alex
247 46ec0f24 2002-05-27 alex switch( H_Error )
248 46ec0f24 2002-05-27 alex {
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";
257 46ec0f24 2002-05-27 alex default:
258 46ec0f24 2002-05-27 alex return "unknown error";
259 46ec0f24 2002-05-27 alex }
260 46ec0f24 2002-05-27 alex } /* Get_Error */
261 46ec0f24 2002-05-27 alex
262 46ec0f24 2002-05-27 alex #endif
263 46ec0f24 2002-05-27 alex
264 46ec0f24 2002-05-27 alex
265 46ec0f24 2002-05-27 alex /* -eof- */