File: README.md

package info (click to toggle)
ruby-net-dns 0.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 452 kB
  • sloc: ruby: 3,944; makefile: 6
file content (172 lines) | stat: -rw-r--r-- 4,389 bytes parent folder | download | duplicates (2)
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
# Net::DNS

Net::DNS is a DNS library written in pure Ruby. It started as a port of Perl Net::DNS module, but it evolved in time into a full Ruby library.  


## Features

- Complete OO interface
- Clean and intuitive API
- Modular and flexible


## Requirements

Ruby >= 2.1


## Installation

The best way to install this library is via [RubyGems](https://rubygems.org/).

```
gem install net-dns
```


## API Documentation

Visit the page http://rdoc.info/gems/net-dns


## Trivial resolver

The simplest way to use the library is to invoke the Resolver() method:

```ruby
require 'rubygems' 
require 'net/dns'
p Resolver("www.google.com")
```

The output is compatible with BIND zone files and it's the same you would get with the dig utility.

```
;; Answer received from localhost:53 (212 bytes)
;;
;; HEADER SECTION
;; id = 64075
;; qr = 1       opCode: QUERY   aa = 0  tc = 0  rd = 1
;; ra = 1       ad = 0  cd = 0  rcode = NoError
;; qdCount = 1  anCount = 3     nsCount = 4     arCount = 4

;; QUESTION SECTION (1 record):
;; google.com.                  IN      A

;; ANSWER SECTION (3 records):
google.com.             212     IN      A       74.125.45.100
google.com.             212     IN      A       74.125.67.100
google.com.             212     IN      A       209.85.171.100

;; AUTHORITY SECTION (4 records):
google.com.             345512  IN      NS      ns1.google.com.
google.com.             345512  IN      NS      ns4.google.com.
google.com.             345512  IN      NS      ns2.google.com.
google.com.             345512  IN      NS      ns3.google.com.

;; ADDITIONAL SECTION (4 records):
ns1.google.com.         170275  IN      A       216.239.32.10
ns2.google.com.         170275  IN      A       216.239.34.10
ns3.google.com.         170275  IN      A       216.239.36.10
ns4.google.com.         170275  IN      A       216.239.38.10
```

An optional block can be passed yielding the Net::DNS::Packet object

```ruby
Resolver("www.google.com") { |packet| puts packet.size + " bytes" }
# => 484 bytes
```

Same for Net::DNS::Resolver.start():

```ruby
Net::DNS::Resolver.start("google.com").answer.size
# => 5
```

As optional parameters, +TYPE+ and +CLASS+ can be specified.

```ruby
p Net::DNS::Resolver.start("google.com", Net::DNS::MX)

;; Answer received from localhost:53 (316 bytes)
;;
;; HEADER SECTION
;; id = 59980
;; qr = 1       opCode: QUERY   aa = 0  tc = 0  rd = 1
;; ra = 1       ad = 0  cd = 0  rcode = NoError
;; qdCount = 1  anCount = 4     nsCount = 4     arCount = 8

;; QUESTION SECTION (1 record):
;; google.com.                  IN      MX

;; ANSWER SECTION (4 records):
google.com.             10800   IN      MX      10 smtp2.google.com.
google.com.             10800   IN      MX      10 smtp3.google.com.
google.com.             10800   IN      MX      10 smtp4.google.com.
google.com.             10800   IN      MX      10 smtp1.google.com.
```

## Handling the response packet

The method Net::DNS::Resolver.start is a wrapper around Resolver.new. It returns a new Net::DNS::Packet object.

A DNS packet is divided into 5 sections:

- header section # => a Net::DNS::Header object
- question section # => a Net::DNS::Question object
- answer section # => an Array of Net::DNS::RR objects
- authority section # => an Array of Net::DNS::RR objects
- additional section # => an Array of Net::DNS::RR objects

You can access each section by calling the attribute with the same name on a Packet object:

```ruby
packet = Net::DNS::Resolver.start("google.com")

header = packet.header
answer = packet.answer

puts "The packet is #{packet.data.size} bytes"
puts "It contains #{header.anCount} answer entries"

answer.any? {|ans| p ans}
```

The output is 

```
The packet is 378 bytes
It contains 3 answer entries
google.com.             244     IN      A       74.125.45.100
google.com.             244     IN      A       74.125.67.100
google.com.             244     IN      A       209.85.171.100
```

A better way to handle the answer section is to use the iterators directly on a Packet object:

```ruby
packet.each_address do |ip|
  puts "#{ip} is alive" if Ping.pingecho(ip.to_s, 10, 80)
end
```

Gives:

```
74.125.45.100 is alive
74.125.67.100 is alive
209.85.171.100 is alive
```


## License

Net::DNS is distributed under the same license Ruby is.


## Authors

- Marco Ceresa (@bluemonk)
- Simone Carletti (@weppos)