雑記

2000|01|
2003|05|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|
2007|01|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|09|11|
2009|02|03|05|06|07|08|10|11|12|
2010|01|03|04|05|06|07|08|09|10|
2011|05|06|09|10|
2012|03|07|09|12|
2013|01|02|04|05|07|08|10|11|
2014|04|05|08|10|12|
2015|01|05|
2016|09|

2007-07-12

[Ruby] メモ:DNSのSRVレコードを引く

Resolvクラスを拡張してDNSのSRVレコードを引けるようにするためのコード

require 'resolv'
 
class Resolv
   class Resource
      class SRV < Resource
         TypeValue = 33
 
         def initialize(priority, weight, port, target)
            @priority = priority
            @weight = weight
            @port = port
            @target = target
         end
         attr_reader :priority, :weight, :port, :target
 
         def encode_rdata(msg)
            msg.put_pack('n', @priority)
            msg.put_pack('n', @weight)
            msg.put_pack('n', @port)
            msg.put_name(@target)
         end
 
         def self.decode_rdata(msg)
            priority, = msg.get_unpack('n')
            weight, = msg.get_unpack('n')
            port, = msg.get_unpack('n')
            target = msg.get_name
            return self.new(priority, weight, port, target)
         end
      end
   end
end
 
Resolv::DNS.new.getresources("_http._tcp.example.com", Resolv::DNS::Resource::IN::SRV).collect
TypeValue に指定する数値が分かれば他のレコードへの応用も簡単。以下、FreeBSDの/usr/src/include/arpa/nameser.hより抜粋。
/*
 * Currently defined type values for resources and queries.
 */
typedef enum __ns_type {
   ns_t_a = 1,    /* Host address. */
   ns_t_ns = 2,      /* Authoritative server. */
   ns_t_md = 3,      /* Mail destination. */
   ns_t_mf = 4,      /* Mail forwarder. */
   ns_t_cname = 5,      /* Canonical name. */
   ns_t_soa = 6,     /* Start of authority zone. */
   ns_t_mb = 7,      /* Mailbox domain name. */
   ns_t_mg = 8,      /* Mail group member. */
   ns_t_mr = 9,      /* Mail rename name. */
   ns_t_null = 10,      /* Null resource record. */
   ns_t_wks = 11,    /* Well known service. */
   ns_t_ptr = 12,    /* Domain name pointer. */
   ns_t_hinfo = 13,  /* Host information. */
   ns_t_minfo = 14,  /* Mailbox information. */
   ns_t_mx = 15,     /* Mail routing information. */
   ns_t_txt = 16,    /* Text strings. */
   ns_t_rp = 17,     /* Responsible person. */
   ns_t_afsdb = 18,  /* AFS cell database. */
   ns_t_x25 = 19,    /* X_25 calling address. */
   ns_t_isdn = 20,      /* ISDN calling address. */
   ns_t_rt = 21,     /* Router. */
   ns_t_nsap = 22,      /* NSAP address. */
   ns_t_nsap_ptr = 23,  /* Reverse NSAP lookup (deprecated). */
   ns_t_sig = 24,    /* Security signature. */
   ns_t_key = 25,    /* Security key. */
   ns_t_px = 26,     /* X.400 mail mapping. */
   ns_t_gpos = 27,      /* Geographical position (withdrawn). */
   ns_t_aaaa = 28,      /* Ip6 Address. */
   ns_t_loc = 29,    /* Location Information. */
   ns_t_nxt = 30,    /* Next domain (security). */
   ns_t_eid = 31,    /* Endpoint identifier. */
   ns_t_nimloc = 32, /* Nimrod Locator. */
   ns_t_srv = 33,    /* Server Selection. */
   ns_t_atma = 34,      /* ATM Address */
   ns_t_naptr = 35,  /* Naming Authority PoinTeR */
   ns_t_opt = 41,    /* OPT pseudo-RR, RFC2761 */
   /* Query type values which do not appear in resource records. */
   ns_t_ixfr = 251,  /* Incremental zone transfer. */
   ns_t_axfr = 252,  /* Transfer zone of authority. */
   ns_t_mailb = 253, /* Transfer mailbox records. */
   ns_t_maila = 254, /* Transfer mail agent records. */
   ns_t_any = 255,      /* Wildcard match. */
   ns_t_max = 65536
} ns_type;
gposなんてのもあるんですね。locってのもあるけど、何が違うんだろう?