-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.c
347 lines (287 loc) · 6.98 KB
/
dns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**************************************************************************
=======================
The Voyager Web Browser
=======================
Copyright (C) 1995-2003 by
Oliver Wagner <owagner@vapor.com>
All Rights Reserved
Parts Copyright (C) by
David Gerber <zapek@vapor.com>
Jon Bright <jon@siliconcircus.com>
Matt Sealey <neko@vapor.com>
**************************************************************************/
/*
** DNS
** ---
**
** V uses a fixed set of 4 concurrent DNS processes
**
** $Id: dns.c,v 1.33 2003/11/21 07:48:49 zapek Exp $
**
*/
#include "voyager.h"
#if USE_NET
/* public */
#if defined( AMIGAOS ) || defined( __MORPHOS__ )
#include <proto/exec.h>
#endif
/* private */
#include "voyager_cat.h"
#include "network.h"
#include "dns.h"
struct MsgPort *dnsport[ DNSTASKS ];
struct Process *dnsproc[ DNSTASKS ];
struct MsgPort *dnsreply;
struct dnscachenode *dnscachelist;
APTR dnscachepool;
char dnsbusy[ DNSTASKS * 64 ];
UWORD dnshandler_die;
struct SignalSemaphore dnscachesem;
int init_dnscache( void )
{
D( db_init, bug( "initializing..\n" ) );
dnscachepool = CreatePool( 0, 2048, 1024 );
InitSemaphore( &dnscachesem );
if( dnscachepool )
{
struct dnscachenode *dns;
dns = AllocPooled( dnscachepool, sizeof( *dns ) + 23 + 8 );
if( dns )
{
memset( dns, 0, sizeof( *dns ) );
dns->namelen = 9;
memcpy( dns->name, "localhost\0localhost", 20 );
dns->ip = (APTR)( dns->name + 23 );
dns->ip[ 0 ] = MAKE_ID( 127, 0, 0, 1 );
dns->ip[ 1 ] = 0;
dnscachelist = dns;
}
}
return( ( int )dnscachepool );
}
void cleanup_dnscache( void )
{
D( db_init, bug( "cleaning up..\n" ) );
if( dnscachepool )
{
DeletePool( dnscachepool );
}
}
struct dnscachenode *dnscache_find( char *name )
{
struct dnscachenode *dcn;
UWORD len = strlen( name );
ObtainSemaphoreShared( &dnscachesem );
for( dcn = dnscachelist; dcn; dcn = dcn->next )
{
if( dcn->namelen == len && !stricmp( name, dcn->name ) )
break;
}
ReleaseSemaphore( &dnscachesem );
return( dcn );
}
static struct dnscachenode *dnscache_store( char *name, struct hostent *hent )
{
struct dnscachenode *dcn;
int namelen = strlen( name );
int ipnum;
char **hipp;
// count number of resolver replies
hipp = (char**)hent->h_addr_list;
for( ipnum = 0; *hipp; hipp++, ipnum++ );
ObtainSemaphore( &dnscachesem );
dcn = AllocPooled( dnscachepool, ipnum * 4 + 4 + sizeof( *dcn ) + namelen + strlen( hent->h_name ) + 2 );
if( dcn )
{
ULONG *ipptr = (APTR)dcn;
dcn = (APTR)(( (ULONG*)dcn ) + ( ipnum + 1 ));
strcpy( dcn->name, name );
dcn->namelen = namelen;
strcpy( dcn->name + namelen + 1, hent->h_name );
dcn->ip = ipptr;
dcn->http11failed = 0;
dcn->next = dnscachelist;
dnscachelist = dcn;
hipp = (char**)hent->h_addr_list;
while( ipnum-- )
{
*ipptr++ = **((ULONG**)hipp);
hipp++;
}
*ipptr = 0;
}
ReleaseSemaphore( &dnscachesem );
return( dcn );
}
//#define IPLOG
#ifdef IPLOG
#include "tagit.h"
ULONG ip = MAKE_ID( 194, 77, 181, 15 ) + 0xc71943;
static void iplog( void )
{
struct Library *SocketBase;
static __chip ULONG tagspace[] = { TAG1, 0, 0, 0, 0, 0, 0, 0, 0, TAG2 };
SocketBase = OpenLibrary( "bsdsocket.library", 3 );
if( SocketBase )
{
int msg[ 5 + 8 ];
struct sockaddr_in sockadr;
int sock;
extern ULONG serialnumber;
msg[ 0 ] = SysBase->AttnFlags;
msg[ 1 ] = serialnumber;
msg[ 2 ] = 2; // V
msg[ 3 ] = VERHEXID;
msg[ 5 ] = COMPILEREV;
memcpy( &msg[ 5 ], &tagspace[ 1 ], 8 * 4 );
memset( &sockadr, 0, sizeof( sockadr ) );
sockadr.sin_len = sizeof( sockadr );
ip -= 0xc71943;
memcpy( &sockadr.sin_addr, &ip, 4 );
sockadr.sin_family = AF_INET;
sockadr.sin_port = 53;
sock = Socket( AF_INET, SOCK_DGRAM, 0 );
SendTo( sock, (APTR)msg, sizeof( msg ), 0, (APTR)&sockadr, sizeof( sockadr ) );
CloseSocket( sock );
}
CloseLibrary( SocketBase );
}
#endif
void SAVEDS dnshandler( void )
{
struct dnsmsg *dnsmsg;
ULONG psig;
#ifdef MBX
NetData_p NetBase = NULL;
#else
struct Library *SocketBase = NULL;
#endif
int netopen = FALSE;
struct hostent *host, hent;
int mytasknum;
char *p;
ULONG *ip[ 2 ], ipn;
#if USE_EXECUTIVE
APTR executivemsg;
executivemsg = InitExecutive();
if( executivemsg )
{
SetNice( executivemsg, -20 ); /* TOFIX! should be configurable one day */
ExitExecutive( executivemsg );
}
#endif /* USE_EXECUTIVE */
ip[ 0 ] = &ipn;
ip[ 1 ] = 0;
hent.h_addr_list = (APTR)&ip[ 0 ];
p = strchr( FindTask( 0 )->tc_Node.ln_Name, 0 ) - 1;
mytasknum = *p - '1';
#ifdef IPLOG
if( !mytasknum )
iplog();
#endif
D( db_dns, bug( "starting up DNS handler %ld\r\n", mytasknum ));
#ifndef __MORPHOS__
dnsport[ mytasknum ] = CreateMsgPort();
if( !dnsport[ mytasknum ] )
{
dnsproc[ mytasknum ] = NULL;
return;
}
#endif
psig = 1L<<dnsport[ mytasknum ]->mp_SigBit;
while( !dnshandler_die )
{
if( Wait( SIGBREAKF_CTRL_C | psig ) & SIGBREAKF_CTRL_C )
{
#ifndef CONNECT
CloseLibrary( SocketBase );
SocketBase = NULL;
#endif /* CONNECT */
netopen = FALSE;
}
while( dnsmsg = (struct dnsmsg*)GetMsg( dnsport[ mytasknum ] ) )
{
dnsmsg->dcn = dnscache_find( dnsmsg->name );
if( !dnsmsg->dcn )
{
if( !netopen )
{
#ifndef CONNECT
SocketBase = OpenLibrary( "bsdsocket.library", 3 );
#ifdef NETCONNECT
if( !SocketBase )
{
SocketBase = ( struct Library * )NCL_OpenSocket();
}
#endif /* !NETCONNECT */
if( SocketBase )
{
netopen = TRUE;
}
#endif /* CONNECT */
}
// currently queued..
stccpy( &dnsbusy[ mytasknum * 64 ], dnsmsg->name, 64 );
D( db_dns, bug( "%lx: querying for %s (%lx/%lx)\r\n", FindTask( 0 ), dnsmsg->name, dnsmsg, dnsmsg->name ));
host = NULL;
if( SocketBase )
{
ipn = inet_addr( dnsmsg->name );
if( !ipn || ipn == INADDR_NONE )
{
D( db_dns, bug( "%lx: gethostbyname() for %s\r\n", FindTask( 0 ), dnsmsg->name ));
host = gethostbyname( dnsmsg->name );
}
else
{
D( db_dns, bug( "%lx: inet_addr() succeeded for %s (%d.%d.%d.%d)\r\n", FindTask( 0 ), dnsmsg->name, ipn>>24, (ipn>>16)&0xFF, (ipn>>8)&0xFF, ipn&0xFF ));
host = &hent;
hent.h_name = dnsmsg->name;
}
}
if( host )
{
dnsmsg->dcn = dnscache_store( dnsmsg->name, host );
}
}
ReplyMsg( (struct Message *)dnsmsg );
}
}
if( netopen )
{
CloseLibrary( SocketBase );
}
Forbid();
// Flush pending messages
while( dnsmsg = (struct dnsmsg*)GetMsg( dnsport[ mytasknum ] ) )
ReplyMsg( (struct Message *)dnsmsg );
#ifndef __MORPHOS__
DeleteMsgPort( dnsport[ mytasknum ] );
dnsport[ mytasknum ] = NULL;
#endif
dnsproc[ mytasknum ] = NULL;
}
void dnsmsg_queue( struct dnsmsg *dm )
{
int c;
static int dnstaskcnt;
for( c = 0; c < DNSTASKS; c++ )
{
if( !stricmp( &dnsbusy[ c * 64 ], dm->name ) )
break;
}
if( c == DNSTASKS )
c = ( dnstaskcnt++ ) % DNSTASKS;
Forbid();
if( dnsport[ c ] )
{
PutMsg( dnsport[ c ], (struct Message *)dm );
}
else
{
// Already shut down
ReplyMsg( (struct Message *)dm );
}
Permit();
}
#endif /* USE_NET */