Class: EventMachine::Udns::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/em-udns/resolver.rb,
ext/em-udns.c

Instance Method Summary collapse

Constructor Details

#initializeResolver

Returns a new instance of Resolver.

Raises:



4
5
6
7
8
# File 'lib/em-udns/resolver.rb', line 4

def initialize
  raise UdnsError, @alloc_error if @alloc_error
  @queries = {}
  dns_open
end

Instance Method Details

#activeObject



156
157
158
159
160
161
162
# File 'ext/em-udns.c', line 156

VALUE Resolver_active(VALUE self)
{
  struct dns_ctx *dns_context = NULL;

  Data_Get_Struct(self, struct dns_ctx, dns_context);
  return INT2FIX(dns_active(dns_context));
}

#cancel(query) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'ext/em-udns.c', line 141

VALUE Resolver_cancel(VALUE self, VALUE query)
{
  VALUE queries;
  VALUE value;
  
  queries = rb_ivar_get(self, id_queries);
  if (TYPE(rb_hash_aref(queries, query)) == T_TRUE) {
    rb_hash_aset(queries, query, Qfalse);
    return Qtrue;
  }
  else
    return Qfalse;
}

#fdObject



111
112
113
114
115
116
117
# File 'ext/em-udns.c', line 111

VALUE Resolver_fd(VALUE self)
{
  struct dns_ctx *dns_context = NULL;

  Data_Get_Struct(self, struct dns_ctx, dns_context);
  return INT2FIX(dns_sock(dns_context));
}

#ioeventObject



120
121
122
123
124
125
126
127
# File 'ext/em-udns.c', line 120

VALUE Resolver_ioevent(VALUE self)
{
  struct dns_ctx *dns_context = NULL;
    
  Data_Get_Struct(self, struct dns_ctx, dns_context);
  dns_ioevent(dns_context, 0);
  return Qfalse;
}

#submit_A(rb_domain) ⇒ Object



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
# File 'ext/em-udns.c', line 383

VALUE Resolver_submit_A(VALUE self, VALUE rb_domain)
{
  struct dns_ctx *dns_context;
  char *domain;
  VALUE query;
  VALUE error;
  struct resolver_query *data;

  
  Data_Get_Struct(self, struct dns_ctx, dns_context);
  domain = StringValueCStr(rb_domain);
  query = rb_obj_alloc(cQuery);

  data = ALLOC(struct resolver_query);
  data->resolver = self;
  data->query = query;
  
  if (!dns_submit_a4(dns_context, domain, 0, dns_result_A_cb, (void *)data)) {
    error = get_dns_error(dns_context);
    xfree(data);
    rb_funcall(query, method_do_error, 1, error);
  }
  else {
    rb_hash_aset(rb_ivar_get(self, id_queries), query, Qtrue);
  }
  
  return query;
}

#submit_AAAA(rb_domain) ⇒ Object



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
# File 'ext/em-udns.c', line 413

VALUE Resolver_submit_AAAA(VALUE self, VALUE rb_domain)
{
  struct dns_ctx *dns_context;
  char *domain;
  VALUE query;
  VALUE error;
  struct resolver_query *data;

  Data_Get_Struct(self, struct dns_ctx, dns_context);
  domain = StringValueCStr(rb_domain);
  query = rb_obj_alloc(cQuery);

  data = ALLOC(struct resolver_query);
  data->resolver = self;
  data->query = query;

  if (!dns_submit_a6(dns_context, domain, 0, dns_result_AAAA_cb, (void *)data)) {
    error = get_dns_error(dns_context);
    xfree(data);
    rb_funcall(query, method_do_error, 1, error);
  }
  else {
    rb_hash_aset(rb_ivar_get(self, id_queries), query, Qtrue);
  }
  
  return query;
}

#submit_MX(rb_domain) ⇒ Object



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
# File 'ext/em-udns.c', line 499

VALUE Resolver_submit_MX(VALUE self, VALUE rb_domain)
{
  struct dns_ctx *dns_context;
  char *domain;
  VALUE query;
  VALUE error;
  struct resolver_query *data;

  Data_Get_Struct(self, struct dns_ctx, dns_context);
  domain = StringValueCStr(rb_domain);
  query = rb_obj_alloc(cQuery);

  data = ALLOC(struct resolver_query);
  data->resolver = self;
  data->query = query;

  if (!dns_submit_mx(dns_context, domain, 0, dns_result_MX_cb, (void *)data)) {
    error = get_dns_error(dns_context);
    xfree(data);
    rb_funcall(query, method_do_error, 1, error);
  }
  else {
    rb_hash_aset(rb_ivar_get(self, id_queries), query, Qtrue);
  }
  
  return query;
}

#submit_NAPTR(rb_domain) ⇒ Object



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
# File 'ext/em-udns.c', line 599

VALUE Resolver_submit_NAPTR(VALUE self, VALUE rb_domain)
{
  struct dns_ctx *dns_context;
  char *domain;
  VALUE query;
  VALUE error;
  struct resolver_query *data;

  Data_Get_Struct(self, struct dns_ctx, dns_context);
  domain = StringValueCStr(rb_domain);
  query = rb_obj_alloc(cQuery);

  data = ALLOC(struct resolver_query);
  data->resolver = self;
  data->query = query;

  if (!dns_submit_naptr(dns_context, domain, 0, dns_result_NAPTR_cb, (void *)data)) {
    error = get_dns_error(dns_context);
    xfree(data);
    rb_funcall(query, method_do_error, 1, error);
  }
  else {
    rb_hash_aset(rb_ivar_get(self, id_queries), query, Qtrue);
  }
  
  return query;
}

#submit_PTR(rb_ip) ⇒ Object



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
# File 'ext/em-udns.c', line 442

VALUE Resolver_submit_PTR(VALUE self, VALUE rb_ip)
{
  struct dns_ctx *dns_context;
  char *ip;
  VALUE query;
  VALUE error;
  struct resolver_query *data;
  struct in_addr addr;
  struct in6_addr addr6;

  Data_Get_Struct(self, struct dns_ctx, dns_context);
  ip = StringValueCStr(rb_ip);
  query = rb_obj_alloc(cQuery);

  data = ALLOC(struct resolver_query);
  data->resolver = self;
  data->query = query;

  switch(dns_pton(AF_INET, ip, &addr)) {
    /* It's valid IPv4. */
    case 1:
      if (!dns_submit_a4ptr(dns_context, &addr, dns_result_PTR_cb, (void *)data)) {
        error = get_dns_error(dns_context);
        xfree(data);
        rb_funcall(query, method_do_error, 1, error);
      }
      else {
        rb_hash_aset(rb_ivar_get(self, id_queries), query, Qtrue);
      }
      break;
    /* Invalid IPv4, let's try with IPv6. */
    case 0:
      switch(dns_pton(AF_INET6, ip, &addr6)) {
        /* It's valid IPv6. */
        case 1:
          if (!dns_submit_a6ptr(dns_context, &addr6, dns_result_PTR_cb, (void *)data)) {
            error = get_dns_error(dns_context);
            xfree(data);
            rb_funcall(query, method_do_error, 1, error);
          }
          else {
            rb_hash_aset(rb_ivar_get(self, id_queries), query, Qtrue);
          }
          break;
        /* Also an invalid IPv6 so the IP is invalid. */
        case 0:
          xfree(data);
          rb_funcall(query, method_do_error, 1, symbol_dns_error_badquery);
          break;
      }
      break;
  }

  return query;
}

#submit_SRV(*args) ⇒ Object



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
# File 'ext/em-udns.c', line 557

VALUE Resolver_submit_SRV(int argc, VALUE *argv, VALUE self)
{
  struct dns_ctx *dns_context;
  char *domain;
  char *service = NULL;
  char *protocol = NULL;
  VALUE query;
  VALUE error;
  struct resolver_query *data;

  if (argc == 1 && TYPE(argv[0]) == T_STRING);
  else if (argc == 3 && TYPE(argv[0]) == T_STRING &&
           TYPE(argv[1]) == T_STRING && TYPE(argv[2]) == T_STRING) {
    service = StringValueCStr(argv[1]);
    protocol = StringValueCStr(argv[2]);
  }
  else if (argc == 3 && TYPE(argv[0]) == T_STRING &&
           TYPE(argv[1]) == T_NIL && TYPE(argv[2]) == T_NIL);
  else
    rb_raise(rb_eArgError, "arguments must be `domain' or `domain',`service',`protocol'");

  Data_Get_Struct(self, struct dns_ctx, dns_context);
  domain = StringValueCStr(argv[0]);
  query = rb_obj_alloc(cQuery);
  
  data = ALLOC(struct resolver_query);
  data->resolver = self;
  data->query = query;
  
  if (!dns_submit_srv(dns_context, domain, service, protocol, 0, dns_result_SRV_cb, (void *)data)) {
    error = get_dns_error(dns_context);
    xfree(data);
    rb_funcall(query, method_do_error, 1, error);
  }
  else {
    rb_hash_aset(rb_ivar_get(self, id_queries), query, Qtrue);
  }
  
  return query;
}

#submit_TXT(rb_domain) ⇒ Object



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
# File 'ext/em-udns.c', line 528

VALUE Resolver_submit_TXT(VALUE self, VALUE rb_domain)
{
  struct dns_ctx *dns_context;
  char *domain;
  VALUE query;
  VALUE error;
  struct resolver_query *data;

  Data_Get_Struct(self, struct dns_ctx, dns_context);
  domain = StringValueCStr(rb_domain);
  query = rb_obj_alloc(cQuery);

  data = ALLOC(struct resolver_query);
  data->resolver = self;
  data->query = query;

  if (!dns_submit_txt(dns_context, domain, DNS_C_IN, 0, dns_result_TXT_cb, (void *)data)) {
    error = get_dns_error(dns_context);
    xfree(data);
    rb_funcall(query, method_do_error, 1, error);
  }
  else {
    rb_hash_aset(rb_ivar_get(self, id_queries), query, Qtrue);
  }
  
  return query;
}