]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/resolve.c
Fixed up some castings.
[ngircd-alex.git] / src / ngircd / resolve.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4  *
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.
10  *
11  * Asynchronous resolver
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: resolve.c,v 1.6 2003/04/21 10:52:51 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <netdb.h>
29
30 #include "conn.h"
31 #include "defines.h"
32 #include "log.h"
33
34 #include "exp.h"
35 #include "resolve.h"
36
37
38 LOCAL VOID Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, INT w_fd ));
39 LOCAL VOID Do_ResolveName PARAMS(( CHAR *Host, INT w_fd ));
40
41 #ifdef h_errno
42 LOCAL CHAR *Get_Error PARAMS(( INT H_Error ));
43 #endif
44
45
46 GLOBAL VOID
47 Resolve_Init( VOID )
48 {
49         /* Modul initialisieren */
50
51         FD_ZERO( &Resolver_FDs );
52 } /* Resolve_Init */
53
54
55 GLOBAL RES_STAT *
56 Resolve_Addr( struct sockaddr_in *Addr )
57 {
58         /* IP (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
59          * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
60          * Der Host kann dann nicht aufgeloest werden. */
61
62         RES_STAT *s;
63         INT pid;
64
65         /* Speicher anfordern */
66         s = malloc( sizeof( RES_STAT ));
67         if( ! s )
68         {
69                 Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
70                 return NULL;
71         }
72
73         /* Pipe fuer Antwort initialisieren */
74         if( pipe( s->pipe ) != 0 )
75         {
76                 free( s );
77                 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
78                 return NULL;
79         }
80
81         /* Sub-Prozess erzeugen */
82         pid = fork( );
83         if( pid > 0 )
84         {
85                 /* Haupt-Prozess */
86                 Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
87                 FD_SET( s->pipe[0], &Resolver_FDs );
88                 if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
89                 s->pid = pid;
90                 return s;
91         }
92         else if( pid == 0 )
93         {
94                 /* Sub-Prozess */
95                 Log_Init_Resolver( );
96                 Do_ResolveAddr( Addr, s->pipe[1] );
97                 Log_Exit_Resolver( );
98                 exit( 0 );
99         }
100         else
101         {
102                 /* Fehler */
103                 free( s );
104                 Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
105                 return NULL;
106         }
107 } /* Resolve_Addr */
108
109
110 GLOBAL RES_STAT *
111 Resolve_Name( CHAR *Host )
112 {
113         /* Hostnamen (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
114          * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
115          * Der Host kann dann nicht aufgeloest werden. */
116
117         RES_STAT *s;
118         INT pid;
119
120         /* Speicher anfordern */
121         s = malloc( sizeof( RES_STAT ));
122         if( ! s )
123         {
124                 Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Name]" );
125                 return NULL;
126         }
127
128         /* Pipe fuer Antwort initialisieren */
129         if( pipe( s->pipe ) != 0 )
130         {
131                 free( s );
132                 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
133                 return NULL;
134         }
135
136         /* Sub-Prozess erzeugen */
137         pid = fork( );
138         if( pid > 0 )
139         {
140                 /* Haupt-Prozess */
141                 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
142                 FD_SET( s->pipe[0], &Resolver_FDs );
143                 if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
144                 s->pid = pid;
145                 return s;
146         }
147         else if( pid == 0 )
148         {
149                 /* Sub-Prozess */
150                 Log_Init_Resolver( );
151                 Do_ResolveName( Host, s->pipe[1] );
152                 Log_Exit_Resolver( );
153                 exit( 0 );
154         }
155         else
156         {
157                 /* Fehler */
158                 free( s );
159                 Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
160                 return NULL;
161         }
162 } /* Resolve_Name */
163
164
165 LOCAL VOID
166 Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
167 {
168         /* Resolver Sub-Prozess: IP aufloesen und Ergebnis in Pipe schreiben. */
169
170         CHAR hostname[HOST_LEN];
171         struct hostent *h;
172
173         Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
174
175         /* Namen aufloesen */
176         h = gethostbyaddr( (CHAR *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
177         if( h ) strlcpy( hostname, h->h_name, sizeof( hostname ));
178         else
179         {
180 #ifdef h_errno
181                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
182 #else
183                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
184 #endif  
185                 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
186         }
187
188         /* Antwort an Parent schreiben */
189         if( (size_t)write( w_fd, hostname, strlen( hostname ) + 1 ) != (size_t)( strlen( hostname ) + 1 ))
190         {
191                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
192                 close( w_fd );
193                 return;
194         }
195
196         Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
197 } /* Do_ResolveAddr */
198
199
200 LOCAL VOID
201 Do_ResolveName( CHAR *Host, INT w_fd )
202 {
203         /* Resolver Sub-Prozess: Name aufloesen und Ergebnis in Pipe schreiben. */
204
205         CHAR ip[16];
206         struct hostent *h;
207         struct in_addr *addr;
208
209         Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
210
211         /* Namen aufloesen */
212         h = gethostbyname( Host );
213         if( h )
214         {
215                 addr = (struct in_addr *)h->h_addr;
216                 strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
217         }
218         else
219         {
220 #ifdef h_errno
221                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
222 #else
223                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
224 #endif
225                 strcpy( ip, "" );
226         }
227
228         /* Antwort an Parent schreiben */
229         if( (size_t)write( w_fd, ip, strlen( ip ) + 1 ) != (size_t)( strlen( ip ) + 1 ))
230         {
231                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
232                 close( w_fd );
233                 return;
234         }
235
236         if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
237 } /* Do_ResolveName */
238
239
240 #ifdef h_errno
241
242 LOCAL CHAR *
243 Get_Error( INT H_Error )
244 {
245         /* Fehlerbeschreibung fuer H_Error liefern */
246
247         switch( H_Error )
248         {
249                 case HOST_NOT_FOUND:
250                         return "host not found";
251                 case NO_DATA:
252                         return "name valid but no IP address defined";
253                 case NO_RECOVERY:
254                         return "name server error";
255                 case TRY_AGAIN:
256                         return "name server temporary not available";
257                 default:
258                         return "unknown error";
259         }
260 } /* Get_Error */
261
262 #endif
263
264
265 /* -eof- */