File: CWDNSManager.m

package info (click to toggle)
pantomime 1.4.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,288 kB
  • sloc: objc: 22,039; makefile: 11; sh: 4
file content (780 lines) | stat: -rw-r--r-- 17,897 bytes parent folder | download
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/*
**  CWDNSManager.m
**
**  Copyright (c) 2004-2007 Ludovic Marcotte
**  Copyright (C) 2012-2022 Riccardo Mottola
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**          Riccardo Mottola <rm@gnu.org>
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**  
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**  Lesser General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#import <Pantomime/CWDNSManager.h>

#import <Pantomime/CWConstants.h>
#import <Pantomime/NSData+Extensions.h>

#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSScanner.h>

#ifdef __MINGW32__
#include <winsock2.h>

#elif defined (__NetBSD__)
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
//#include <res_update.h>
#include <arpa/inet.h>
#include <netdb.h>
#if __NetBSD_Version__ >= 300000000
#define HAS_RES_NINIT 1
#endif

#else
#include <sys/types.h>      // For u_char on Mac OS X
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <resolv.h>
#endif

#include <unistd.h>

#define MAX_PACKET_SIZE 512
#define MAX_TIMEOUT 2



static CWDNSManager *singleInstance = nil;

typedef struct _dns_packet_header
{
  unsigned short packet_id;
  unsigned short flags;
  unsigned short qdcount;
  unsigned short ancount;
  unsigned short nscount;
  unsigned short arcount;
} dns_packet_header;

typedef struct _dns_packet_question
{
  unsigned short qtype;
  unsigned short qclass;
} dns_packet_question;

typedef struct _dns_resource_record
{
  unsigned short type;
  unsigned short class;
  unsigned int ttl;
  unsigned short rdlength;
} dns_resource_record;

#ifdef MACOSX
void dns_socket_callback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void* data, void* info);

void dns_socket_callback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void* data, void* info)
{
  if (type&kCFSocketReadCallBack)
    {
      [(CWDNSManager *)info receivedEvent: (void*)CFSocketGetNative(s)
		       type: ET_RDESC
		       extra: 0
		       forMode: nil];
    }
}
#endif

//
//
//
@interface CWDNSRequest : NSObject
{
  @protected
    NSMutableArray *servers;
    NSData *name;

  @public
    unsigned short packet_id, count;
}

- (id) initWithName: (NSString *) theName;
- (NSMutableArray *) servers;
- (void) setServers: (NSMutableArray *)serverArray;
- (NSData *) name;

@end

@implementation CWDNSRequest

- (id) initWithName: (NSString *) theName
{
  self = [super init];
  if (self)
    {
      servers = [[NSMutableArray alloc] init];
      name = RETAIN([theName dataUsingEncoding: NSASCIIStringEncoding]);
      count = 0;
    }
  return self;
}

- (NSMutableArray *) servers
{
  return servers;
}

- (void) setServers: (NSMutableArray *)serverArray
{
  ASSIGN(self->servers, serverArray);
}

- (NSData *) name
{
  return name;
}

- (void) dealloc
{
  RELEASE(servers);
  RELEASE(name);
  [super dealloc];
}

@end

//
//
//
@interface CWDNSManager (Private)

- (void) _parseHostsFile;
- (void) _parseResolvFile;
- (void) _processResponse;
- (void) _sendRequest: (CWDNSRequest *) theRequest;
- (void) _tick: (id) sender;

@end

//
//
//
@implementation CWDNSManager

- (id) init
{
  self = [super init];
  if (self)
    {
      _cache = [[NSMutableDictionary alloc] init];
      _servers = [[NSMutableArray alloc] init];
      _queue = [[NSMutableArray alloc] init];
      _is_asynchronous = NO;

#ifdef MACOSX
  _runLoopSource = nil;
  _context = nil;
  _cf_socket = nil;
#endif

  [self _parseResolvFile];
  [self _parseHostsFile];

  if ([_servers count] && (_socket = socket(PF_INET, SOCK_DGRAM, 0)) >= 0)
    {
      _is_asynchronous = YES;
      _packet_id = 1;
      
#ifdef MACOSX
      _context = (CFSocketContext *)malloc(sizeof(CFSocketContext));
      memset(_context, 0, sizeof(CFSocketContext));
      _context->info = self;
      
      _cf_socket = CFSocketCreateWithNative(NULL, _socket, kCFSocketReadCallBack|kCFSocketWriteCallBack, dns_socket_callback, _context);
      CFSocketDisableCallBacks(_cf_socket, kCFSocketReadCallBack|kCFSocketWriteCallBack);
      
      if (!_cf_socket)
	{
	  _is_asynchronous = NO;
	  return self;
	}
      
      _runLoopSource = CFSocketCreateRunLoopSource(NULL, _cf_socket, 1);
      
      if (!_runLoopSource)
	{
	  CFSocketInvalidate(_cf_socket);
	  _is_asynchronous = NO;
	  return self;
	}
      
      CFRunLoopAddSource(CFRunLoopGetCurrent(), _runLoopSource, kCFRunLoopCommonModes);
#else
      [[NSRunLoop currentRunLoop] addEvent: (void *)_socket
#ifdef __MINGW32__
				  type: ET_HANDLE
#else
				  type: ET_RDESC
#endif
				  watcher: self
				  forMode: NSDefaultRunLoopMode];
#endif

      _timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
			target: self
			selector: @selector(_tick:)
			userInfo: nil
			repeats: YES];
      RETAIN(_timer);
      [_timer fire];
    }
    }
  return self;
}

//
//
//
- (void) dealloc
{
  [_timer invalidate];
  RELEASE(_timer);

#ifdef MACOSX
  if (CFRunLoopSourceIsValid(_runLoopSource))
    {
      CFRunLoopSourceInvalidate(_runLoopSource);
      CFRelease(_runLoopSource);
    }

  if (CFSocketIsValid(_cf_socket))
    {
      CFSocketInvalidate(_cf_socket);
    }

  CFRelease(_cf_socket);
  free(_context);
#endif

  RELEASE(_cache);
  RELEASE(_servers);
  RELEASE(_queue);
  [super dealloc];
}

//
//
//
- (NSArray *) addressesForName: (NSString *) theName  background: (BOOL) theBOOL
{
  id o;

  NSDebugLog(@"DNSManager - Looking up address: %@, background? %u", theName, theBOOL);
  o = [_cache objectForKey: theName];
  
  if (theBOOL)
    {
      if (o)
	{
	  POST_NOTIFICATION(PantomimeDNSResolutionCompleted, self, ([NSDictionary dictionaryWithObjectsAndKeys: theName, @"Name", [o objectAtIndex: 0], @"Address", nil]));
	}
      else
	{
	  CWDNSRequest *aRequest;
	  NSMutableArray *serversCopy;

	  serversCopy = [[NSMutableArray alloc] initWithArray: _servers];
	  aRequest = AUTORELEASE([[CWDNSRequest alloc] initWithName: theName]);
	  aRequest->packet_id = _packet_id++;
	  [aRequest setServers: serversCopy];
	  [serversCopy release];
	  aRequest->count = 0;

	  if ([_servers count])
	    {
	      [self _sendRequest: aRequest];
	    }
	}

      return nil;
    }

  if (!o)
    {
      struct hostent *host_info;

      //
      // We don't compare with _servers as it might have more
      // magic to obtain IP addresses from DNS names.
      //
      host_info = gethostbyname([theName cString]);
      
      if (host_info)
	{
	  unsigned i;

	  o = [NSMutableArray array];
          i = 0;
	  while (host_info->h_addr_list[i] != NULL)
	    {
              uint8_t c0, c1, c2, c3;
              char *buf;
              uint32_t r;
		  
              buf = host_info->h_addr_list[i];
              c0 = (uint8_t)*(buf);
              c1 = (uint8_t)*(buf+1);
              c2 = (uint8_t)*(buf+2);
              c3 = (uint8_t)*(buf+3);
		  
              r = ntohl((uint32_t)((c0<<24)|(c1<<16)|(c2<<8)|c3));

              [o addObject: [NSNumber numberWithUnsignedInt: r]];

              i++;
	    }
	  
	  // We only cache if we have at least one address for the DNS name.
	  if ([o count])
	    {
	      [_cache setObject: o  forKey: theName];
	    }
	}
      else
	{
	  o = nil;
	}
    }
  
  return o;
}

//
//
//
- (void) receivedEvent: (void *) theData
                  type: (RunLoopEventType) theType
                 extra: (void *) theExtra
               forMode: (NSString *) theMode
{
  switch (theType)
    {
#ifdef __MINGW32__
    case ET_HANDLE:
    case ET_TRIGGER:
#else
    case ET_RDESC:
#endif
      [self _processResponse];
      break;

    default:
      break;
    }
}

//
//
//
+ (id) singleInstance
{
  if (!singleInstance)
    {
      singleInstance = [[CWDNSManager alloc] init];
    }

  return singleInstance;
}

@end

@implementation CWDNSManager (Private)

//
// See man 5 hosts for all details
// on the format of the /etc/hosts file.
//
- (void) _parseHostsFile
{
  NSData *aData;

  aData = [NSData dataWithContentsOfFile: @"/etc/hosts"];
  
  if (aData)
    {
      NSArray *allLines;
      NSString *aString;
      BOOL b;
      NSUInteger i;
      
      allLines = [aData componentsSeparatedByCString: "\n"];
      
      for (i = 0; i < [allLines count]; i++)
	{
	  aData = [allLines objectAtIndex: i];

	  if ([aData hasCPrefix: "#"]) continue;
      
	  aString = [[NSString alloc] initWithData: aData  encoding: NSASCIIStringEncoding];
	  b = YES;

	  if (aString)
	    {
	      NSString *aWord;
	      NSString *theIP;
	      NSScanner *aScanner;

	      aScanner = [NSScanner scannerWithString: aString];
	      theIP = nil;
	      
	      [aScanner scanCharactersFromSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]  intoString: NULL];
	      
	      while ([aScanner scanUpToCharactersFromSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]  intoString: &aWord] == YES)
		{
		  if (b)
		    {
		      theIP = aWord;
		      b = NO;
		      continue;
		    }

		  [_cache setObject: [NSArray arrayWithObject: [NSNumber numberWithUnsignedInt: inet_addr([theIP UTF8String])]]  forKey: aWord];
		  [aScanner scanCharactersFromSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]  intoString: NULL];

		}

	      RELEASE(aString);
	    }
	}
    }
}

//
// See man 5 resolv.conf for all details
// on the format of the /etc/resolv.conf  file.
//
- (void) _parseResolvFile
{
  int i;
#ifdef HAS_RES_NINIT
  struct __res_state cw_res;
#else
#define cw_res _res
#endif

#ifdef HAS_RES_NINIT
  memset(&cw_res, 0, sizeof(cw_res));

  if (res_ninit(&cw_res) == -1)
      return;
#else
  if (res_init() == -1)
      return; 
#endif

  if ((cw_res.options & RES_INIT) == 0)
    return;

  for (i = 0 ; i < cw_res.nscount; i++ )
  {
    [_servers addObject: [NSNumber numberWithUnsignedInt: cw_res.nsaddr_list[i].sin_addr.s_addr]];
  }

#ifdef HAS_RES_NINIT
  res_ndestroy(&cw_res);
#endif
}


//
//
//
- (void) _processResponse
{
  CWDNSRequest *aRequest;
  NSString *aString;
  NSNumber *aNumber;

  dns_resource_record *resource_record;
  dns_packet_header *header;

  char *buf, qr, ra, rcode, *start;
  unsigned short flags, i, type;
  uint8_t c0, c1, c2, c3;
  uint32_t r;

  start = buf = (char *)malloc(MAX_PACKET_SIZE);

  if (recvfrom(_socket, buf, MAX_PACKET_SIZE, 0, NULL, NULL) == -1)
    {
      free(buf);
      return;
    }

  // We build our packet header. We should get (~118 bytes in total):
  //
  // - packet identifier
  // - flags (0x8180)
  // - qdcount
  // - ancount
  // - nscount
  // - arcount
  header = (dns_packet_header *)buf;  

  // We get the right DNSRequest object from the queue
  // based on our packet ID.
  aRequest = nil;

  for (i = 0; i < [_queue count]; i++)
    {
      aRequest = [_queue objectAtIndex: i];
      if (aRequest->packet_id == ntohs(header->packet_id)) break;
    }

  if (!aRequest)
    {
      free(buf);
      return;
    }

  flags = ntohs(header->flags);  
  
  qr = (char)((flags & 0x8000) >> 15);
  
  // We check if we got a response from the server. If not,
  // we try the next server, if any.
  if (!qr) return;

  ra = (char)((flags & 0x0080) >> 7);

  // We check if recursive queries are supported by
  // the server. If not, we try the next server, if any.
  if (!ra) return;

  rcode = (char)((flags & 0x000F) >> 0);

  // We check if we got any errors...
  // 2 - server failure
  // 3 - domain does not exist
  // 5 - the server refused to serve our query
  if (rcode)
    {
      return;
    }
  
  // We check if we got a response from the server. If not,
  // we try the next server, if any.
  if (!htons(header->ancount)) return;

  buf += sizeof(dns_packet_header);

  //
  // We skip over the Question section.
  //
  while (*buf)
    {
      buf += (int)(*buf)+1;
    }

  buf += sizeof(dns_packet_question)+1;
  

  //
  // We now read the Answer section of our packet
  //
  //                                  1  1  1  1  1  1
  //    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  //  |                                               |
  //  /                                               /
  //  /                      NAME                     /
  //  |                                               |
  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  //  |                      TYPE                     |
  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  //  |                     CLASS                     |
  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  //  |                      TTL                      |
  //  |                                               |
  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  //  |                   RDLENGTH                    |
  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
  //  /                     RDATA                     /
  //  /                                               /
  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  type = 0;

  while (type != 1)
    {
      if (!((*buf) & 0xC0))
	{      
	  while (*buf)  
	    {
	      buf+=(int)(*buf)+1;
	    }
	  
	  buf-=1;
	}
      
      resource_record = (dns_resource_record *)(buf+=2); 
      type = ntohs(resource_record->type);
      buf += (sizeof(dns_resource_record)-2)+ntohs(resource_record->rdlength);
    }
  
  buf -= ntohs(resource_record->rdlength);
  c0 = (uint8_t)*(buf);
  c1 = (uint8_t)*(buf+1);
  c2 = (uint8_t)*(buf+2);
  c3 = (uint8_t)*(buf+3);
  
  r = ntohl((uint32_t)((c0<<24)|(c1<<16)|(c2<<8)|c3));

  aString = AUTORELEASE([[NSString alloc] initWithData: [aRequest name]  encoding: NSASCIIStringEncoding]);
  aNumber = [NSNumber numberWithUnsignedInt: r];

  POST_NOTIFICATION(PantomimeDNSResolutionCompleted, self, ([NSDictionary dictionaryWithObjectsAndKeys: aString, @"Name", aNumber, @"Address", nil]));

  [_cache setObject: [NSArray arrayWithObject: aNumber]  forKey: aString];

  // We remove our request from the queue
  [_queue removeObject: aRequest];

  free(start);
}

//
//
//
- (void) _sendRequest: (CWDNSRequest *) theRequest
{
  NSArray *subdomains;

  struct sockaddr_in peer_address;
  dns_packet_question *question;
  dns_packet_header *header;
  unsigned i;
  size_t len;
  char *packet, *start;

  peer_address.sin_family = PF_INET;
  peer_address.sin_port = htons(53);

  peer_address.sin_addr.s_addr = [[[theRequest servers] objectAtIndex: 0] unsignedIntValue];
  start = packet = (char *)malloc(MAX_PACKET_SIZE);

  // We build our packet header. We have to set:
  //  
  // - packet identifier
  // - flags, we have something like this to fill:
  //   |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
  //    0      0000    0  0  1  0     000      0000
  // - qdcount: 1 entry in the question section.
  // - ancount: 0 resource record in the answer section
  // - nscount: 0 name server resource record in the authority records section
  // - arcount: 0 resource record in the additional records section
  //
  //NSLog(@"(0x0100:\t%d to %d\n1:\t\t%d to %d\n", 0x0100, htons(0x0100), 1, htons(1));
  header = (dns_packet_header *)packet;
  header->packet_id = htons(theRequest->packet_id);
  header->flags = htons(0x0100);    
  header->qdcount = htons(1);
  header->ancount = header->nscount = header->arcount = 0;

  // We build our packet question.
  packet += sizeof(dns_packet_header);

  // QNAME
  // a domain name represented as a sequence of labels, where
  // each label consists of a length octet followed by that
  // number of octets.  The domain name terminates with the
  // zero length octet for the null label of the root.  Note
  // that this field may be an odd number of octets; no
  // padding is used.
  //
  subdomains = [[theRequest name] componentsSeparatedByCString: "."];
  
  for (i = 0; i < [subdomains count]; i++)
    {
      *packet = len = [[subdomains objectAtIndex: i] length];
      memcpy(++packet, [[subdomains objectAtIndex: i] bytes], len);
      packet += len;
    }

  *(packet++) = '\0'; 
  question = (dns_packet_question *)packet;
  question->qtype = htons(1);  // Type A: 1 a host address
  question->qclass = htons(1); // IN: 1 the Internet
  packet += sizeof(dns_packet_question);
  
  len = packet-(char *)header;

  // We queue our DNS request
  if (![_queue containsObject: theRequest]) [_queue addObject: theRequest];
  
  // We send our packet. If we failed to send it, we don't care since
  // we'll try to send it to an other server shortly after.
  sendto(_socket, start, len, 0, (struct sockaddr *)&peer_address, sizeof(struct sockaddr));
  
  free(start);
}

//
//
//
- (void) _tick: (id) sender
{
  NSUInteger c;

  if ((c = [_queue count]))
    {
      CWDNSRequest *aRequest;

      while (c--)
	{
	  aRequest = [_queue objectAtIndex: c];
	  
	  if (aRequest->count == MAX_TIMEOUT)
	    {
	      if ([[aRequest servers] count] > 1)
		{
		  [[aRequest servers] removeObjectAtIndex: 0];
		  aRequest->count = 0;
		  
		  [self _sendRequest: aRequest];
		}
	      else
		{
		  NSDictionary *aDictionary;

		  aDictionary = [NSDictionary dictionaryWithObject: AUTORELEASE([[NSString alloc] initWithData: [aRequest name] encoding: NSASCIIStringEncoding])
					      forKey: @"Name"];
		  POST_NOTIFICATION(PantomimeDNSResolutionFailed, self, aDictionary);

		  [_queue removeObject: aRequest];
		}
	    }

	  aRequest->count++;
	}
    }
}

@end