dnsmessage 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +35 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +104 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dnsmessage.gemspec +31 -0
- data/examples/server.rb +53 -0
- data/lib/dnsmessage.rb +12 -0
- data/lib/dnsmessage/enums.rb +65 -0
- data/lib/dnsmessage/message.rb +141 -0
- data/lib/dnsmessage/name.rb +39 -0
- data/lib/dnsmessage/pointer.rb +54 -0
- data/lib/dnsmessage/question.rb +35 -0
- data/lib/dnsmessage/resource_record.rb +140 -0
- data/lib/dnsmessage/version.rb +3 -0
- metadata +72 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 3b0f9b39a569f631d9aee6c28f8820787ae68a3f62d24ef6d64e0b6275677dbc
|
|
4
|
+
data.tar.gz: 6d30ad38144d9117c2991439ea274bcfc3594e787583f816638ef87af417e6dc
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f5a5014c67f58edd7f0ce8da1659207fc3e53ebd4167997f0d89beadf6d7c72f203f939f41c8c18f1d87c28c0506aeb6843a892109c83b8006af465b30b76935
|
|
7
|
+
data.tar.gz: 6fc03487323152b8f51b028c6d009c3818f7a7bdd1e500d4ad4cb8fd430a8239d9c18608e4359ad5ef84b95822e6b160bdd19712735967324640547957acd414
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
|
2
|
+
# They are provided by a third-party and are governed by
|
|
3
|
+
# separate terms of service, privacy policy, and support
|
|
4
|
+
# documentation.
|
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
|
7
|
+
|
|
8
|
+
name: Ruby
|
|
9
|
+
|
|
10
|
+
on:
|
|
11
|
+
push:
|
|
12
|
+
branches: [ master ]
|
|
13
|
+
pull_request:
|
|
14
|
+
branches: [ master ]
|
|
15
|
+
|
|
16
|
+
jobs:
|
|
17
|
+
test:
|
|
18
|
+
|
|
19
|
+
runs-on: ubuntu-latest
|
|
20
|
+
strategy:
|
|
21
|
+
matrix:
|
|
22
|
+
ruby-version: ['2.6', '2.7', '3.0']
|
|
23
|
+
|
|
24
|
+
steps:
|
|
25
|
+
- uses: actions/checkout@v2
|
|
26
|
+
- name: Set up Ruby
|
|
27
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
|
28
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
|
29
|
+
uses: ruby/setup-ruby@v1
|
|
30
|
+
# uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
|
31
|
+
with:
|
|
32
|
+
ruby-version: ${{ matrix.ruby-version }}
|
|
33
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
|
34
|
+
- name: Run tests
|
|
35
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at cmol@zeuxion.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: https://contributor-covenant.org
|
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Claus Lensbøl
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# DNSMessage
|
|
2
|
+
|
|
3
|
+
DNSMessage is a Ruby library for building and parsing DNS messages.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
A full featured DNS parser. The library supports DNS name compression
|
|
9
|
+
and gives access to parse, build, and manipulate all aspects of the DNS
|
|
10
|
+
queries and replies.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Add this line to your application's Gemfile:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
gem 'dnsmessage'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
And then execute:
|
|
22
|
+
|
|
23
|
+
$ bundle install
|
|
24
|
+
|
|
25
|
+
Or install it yourself as:
|
|
26
|
+
|
|
27
|
+
$ gem install dnsmessage
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
require 'dnsmessage'
|
|
33
|
+
|
|
34
|
+
# Parse some byte string from a server or somewhere else
|
|
35
|
+
msg = DNSMessage::Message.parse(byte_string)
|
|
36
|
+
|
|
37
|
+
# Look at the questions and resource records in that byte string
|
|
38
|
+
puts msg.questions.inspect
|
|
39
|
+
puts msg.answers.inspect
|
|
40
|
+
puts msg.additionals.inspect
|
|
41
|
+
puts msg.authority.inspect
|
|
42
|
+
|
|
43
|
+
# Create a reply based on the message (even if it's a reply itself)
|
|
44
|
+
reply = DNSMessage::Message.reply_to(msg)
|
|
45
|
+
|
|
46
|
+
# Add an answer to the reply
|
|
47
|
+
answer = DNSMessage::RR.new(name: "some.domain.tld",
|
|
48
|
+
type: DNSMessage::Type::A,
|
|
49
|
+
klass: DNSMessage::Class::IN,
|
|
50
|
+
ttl: 7200,
|
|
51
|
+
rdata: IPAddr.new("4.3.2.1"))
|
|
52
|
+
reply.answers << answer
|
|
53
|
+
|
|
54
|
+
# (optional) Add EDNS with a 512 maximum size
|
|
55
|
+
reply.additionals << DNSMessage::RR.default_opt(512)
|
|
56
|
+
|
|
57
|
+
# Build reply into a byte string
|
|
58
|
+
reply_bytes = reply.build
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
An example of an IP discovery server can be found in the examples
|
|
63
|
+
directory.
|
|
64
|
+
|
|
65
|
+
## Supported Resource Record types
|
|
66
|
+
|
|
67
|
+
Currently implemented types are:
|
|
68
|
+
|
|
69
|
+
* A
|
|
70
|
+
* AAAA
|
|
71
|
+
* CNAME
|
|
72
|
+
* OPT
|
|
73
|
+
* TXT
|
|
74
|
+
|
|
75
|
+
Other records should be easy to add on a per-need basis as they should
|
|
76
|
+
be based on the building blocks of already existing. Look at
|
|
77
|
+
contributing for details on adding RR types.
|
|
78
|
+
|
|
79
|
+
## Development
|
|
80
|
+
|
|
81
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
82
|
+
|
|
83
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
84
|
+
|
|
85
|
+
## Contributing
|
|
86
|
+
|
|
87
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cmol/dnsmessage. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/cmol/dnsmessage/blob/master/CODE_OF_CONDUCT.md).
|
|
88
|
+
|
|
89
|
+
### Adding Resource Record Types
|
|
90
|
+
|
|
91
|
+
To add a Resource Record you need to find documentation for the
|
|
92
|
+
structure of that record, or create a dump using something like
|
|
93
|
+
wireshark. After that is done, you can use an existing builder or parser
|
|
94
|
+
from `lib/dnsmessage/resource_record.rb`. If you are creating a pull
|
|
95
|
+
request, please add tests for the given type.
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
101
|
+
|
|
102
|
+
## Code of Conduct
|
|
103
|
+
|
|
104
|
+
Everyone interacting in the dnsmessage project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dnsmessage/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "dnsmessage"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/dnsmessage.gemspec
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require_relative 'lib/dnsmessage/version'
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = "dnsmessage"
|
|
5
|
+
spec.version = DNSMessage::VERSION
|
|
6
|
+
spec.authors = ["Claus Lensbøl"]
|
|
7
|
+
spec.email = ["cmol@cmol.dk"]
|
|
8
|
+
|
|
9
|
+
spec.summary = %q{Ruby library to build and parse DNS messages}
|
|
10
|
+
spec.description = %q{A full featured DNS parser. The library supports DNS
|
|
11
|
+
name compression and gives access to parse, build, and manipulate all aspects
|
|
12
|
+
of the DNS queries and replies.}
|
|
13
|
+
spec.homepage = "https://github.com/cmol/dnsmessage"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
|
16
|
+
|
|
17
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
18
|
+
|
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/cmol/dnsmessage"
|
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/cmol/dnsmessage"
|
|
22
|
+
|
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
25
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
27
|
+
end
|
|
28
|
+
spec.bindir = "exe"
|
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
30
|
+
spec.require_paths = ["lib"]
|
|
31
|
+
end
|
data/examples/server.rb
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# This example implements an IP discover mechanism for IPv4 and IPv6.
|
|
2
|
+
# Run server with `ruby server.rb` and query with something like
|
|
3
|
+
# `dig my.ip @[address_of_server]`
|
|
4
|
+
# This server is not intended for production use unless you have very low
|
|
5
|
+
# volumes of traffic.
|
|
6
|
+
|
|
7
|
+
require "socket"
|
|
8
|
+
require "dnsmessage"
|
|
9
|
+
|
|
10
|
+
LISTEN_ADDR = "::"
|
|
11
|
+
LISTEN_PORT = 12345
|
|
12
|
+
MSG_LENGTH = 1400
|
|
13
|
+
FLAGS = 0
|
|
14
|
+
|
|
15
|
+
# Create socket and bind it to the listen on all addresses and the given port
|
|
16
|
+
server_socket = UDPSocket.new :INET6
|
|
17
|
+
server_socket.bind(LISTEN_ADDR, LISTEN_PORT)
|
|
18
|
+
|
|
19
|
+
loop do
|
|
20
|
+
# Listen for messages of up to specified length
|
|
21
|
+
message, client = server_socket.recvfrom(MSG_LENGTH)
|
|
22
|
+
msg = DNSMessage::Message.parse(message)
|
|
23
|
+
|
|
24
|
+
# Extract client information given as array and log connection
|
|
25
|
+
addr_info = Addrinfo.new(client)
|
|
26
|
+
puts "Client connected from #{addr_info.ip_address} using " +
|
|
27
|
+
"#{addr_info.ipv6_v4mapped? ? "IPv4" : "IPv6"}"
|
|
28
|
+
|
|
29
|
+
response = DNSMessage::Message::reply_to(msg)
|
|
30
|
+
opt = DNSMessage::RR.default_opt(512)
|
|
31
|
+
|
|
32
|
+
# Set IPv6 defaults
|
|
33
|
+
type = DNSMessage::Type::AAAA
|
|
34
|
+
ip = addr_info.ip_address
|
|
35
|
+
|
|
36
|
+
# See if we need to fall back to IPv4
|
|
37
|
+
if addr_info.ipv6_v4mapped?
|
|
38
|
+
type = DNSMessage::Type::A
|
|
39
|
+
ip = addr_info.ipv6_to_ipv4.ip_address
|
|
40
|
+
end
|
|
41
|
+
response.answers << DNSMessage::RR.new(
|
|
42
|
+
name: "your.ip",
|
|
43
|
+
type: type,
|
|
44
|
+
ttl: 10,
|
|
45
|
+
rdata: IPAddr.new(ip))
|
|
46
|
+
|
|
47
|
+
# Be nice and add an EDNS record
|
|
48
|
+
response.additionals << opt
|
|
49
|
+
|
|
50
|
+
# Write back to client with AddressFamily and reversed original message
|
|
51
|
+
server_socket.send(response.build,
|
|
52
|
+
FLAGS, addr_info.ip_address, addr_info.ip_port)
|
|
53
|
+
end
|
data/lib/dnsmessage.rb
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require "dnsmessage/version"
|
|
2
|
+
require "dnsmessage/enums"
|
|
3
|
+
require "dnsmessage/message"
|
|
4
|
+
require "dnsmessage/resource_record"
|
|
5
|
+
require "dnsmessage/question"
|
|
6
|
+
require "dnsmessage/name"
|
|
7
|
+
require "dnsmessage/pointer"
|
|
8
|
+
|
|
9
|
+
module DNSMessage
|
|
10
|
+
class Error < StandardError; end
|
|
11
|
+
# Your code goes here...
|
|
12
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
module DNSMessage
|
|
2
|
+
module Type
|
|
3
|
+
TYPE_STRINGS = {
|
|
4
|
+
1 => :A,
|
|
5
|
+
28 => :AAAA,
|
|
6
|
+
18 => :AFSDB,
|
|
7
|
+
42 => :APL,
|
|
8
|
+
257 => :CAA,
|
|
9
|
+
60 => :CDNSKEY,
|
|
10
|
+
59 => :CDS,
|
|
11
|
+
37 => :CERT,
|
|
12
|
+
5 => :CNAME,
|
|
13
|
+
62 => :CSYNC,
|
|
14
|
+
49 => :DHCID,
|
|
15
|
+
32769 => :DLV,
|
|
16
|
+
39 => :DNAME,
|
|
17
|
+
48 => :DNSKEY,
|
|
18
|
+
43 => :DS,
|
|
19
|
+
108 => :EUI48,
|
|
20
|
+
109 => :EUI64,
|
|
21
|
+
13 => :HINFO,
|
|
22
|
+
55 => :HIP,
|
|
23
|
+
45 => :IPSECKEY,
|
|
24
|
+
25 => :KEY,
|
|
25
|
+
36 => :KX,
|
|
26
|
+
29 => :LOC,
|
|
27
|
+
15 => :MX,
|
|
28
|
+
35 => :NAPTR,
|
|
29
|
+
2 => :NS,
|
|
30
|
+
47 => :NSE,
|
|
31
|
+
50 => :NSEC3,
|
|
32
|
+
51 => :NSEC3PARAM,
|
|
33
|
+
61 => :OPENPGPKEY,
|
|
34
|
+
12 => :PTR,
|
|
35
|
+
46 => :RRSIG,
|
|
36
|
+
17 => :RP,
|
|
37
|
+
24 => :SIG,
|
|
38
|
+
53 => :SMIMEA,
|
|
39
|
+
6 => :SOA,
|
|
40
|
+
33 => :SRV,
|
|
41
|
+
44 => :SSHFP,
|
|
42
|
+
32768 => :TA,
|
|
43
|
+
249 => :TKEY,
|
|
44
|
+
52 => :TLSA,
|
|
45
|
+
250 => :TSIG,
|
|
46
|
+
16 => :TXT,
|
|
47
|
+
256 => :URI,
|
|
48
|
+
63 => :ZONEMD,
|
|
49
|
+
64 => :SVCB,
|
|
50
|
+
65 => :HTTPS,
|
|
51
|
+
252 => :AXFR,
|
|
52
|
+
251 => :IXFR,
|
|
53
|
+
41 => :OPT
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
TYPE_STRINGS.each do |num,name|
|
|
57
|
+
const_set(name, num)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
module Class
|
|
63
|
+
IN = 1
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
module DNSMessage
|
|
2
|
+
class Message
|
|
3
|
+
|
|
4
|
+
NAME_POINTER = 0xc0
|
|
5
|
+
POINTER_MASK = 0x3fff
|
|
6
|
+
QUERY = 0
|
|
7
|
+
REPLY = 1
|
|
8
|
+
HEADER_SIZE = 12
|
|
9
|
+
|
|
10
|
+
attr_accessor :questions, :answers, :authority, :additionals,
|
|
11
|
+
:id, :qr, :opcode, :aa, :tc, :rd, :ra, :z, :rcode,
|
|
12
|
+
:qdcount, :ancount, :nscount, :arcount
|
|
13
|
+
|
|
14
|
+
def initialize()
|
|
15
|
+
@questions = []
|
|
16
|
+
@answers = []
|
|
17
|
+
@additionals = []
|
|
18
|
+
@authority = []
|
|
19
|
+
@id = @qr = @opcode = @aa = @tc = @rd = @ra = @z = $rcode = 0
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def parse(input)
|
|
23
|
+
ptr = Pointer.new()
|
|
24
|
+
parse_header(input)
|
|
25
|
+
idx = parse_questions(input, @qdcount, ptr)
|
|
26
|
+
@answers, idx = parse_records(input, @ancount, idx, ptr)
|
|
27
|
+
@authority, idx = parse_records(input, @nscount, idx, ptr)
|
|
28
|
+
@additionals, idx = parse_records(input, @arcount, idx, ptr)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.parse(input)
|
|
32
|
+
self.new().tap do | m |
|
|
33
|
+
m.parse(input)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.reply_to(q)
|
|
38
|
+
self.new().tap do | r |
|
|
39
|
+
r.id = q.id
|
|
40
|
+
r.qr = REPLY
|
|
41
|
+
r.questions = q.questions
|
|
42
|
+
r.qdcount = q.qdcount
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def parse_header(message)
|
|
48
|
+
return nil if message.nil? || message.empty?
|
|
49
|
+
@id, opts, @qdcount, @ancount, @nscount, @arcount =
|
|
50
|
+
message[0...12].unpack("n6")
|
|
51
|
+
@qr = (opts >> 15) & 0x1
|
|
52
|
+
@opcode = (opts >> 11) & 0xf
|
|
53
|
+
@aa = (opts >> 10) & 0x1
|
|
54
|
+
@tc = (opts >> 9) & 0x1
|
|
55
|
+
@rd = (opts >> 8) & 0x1
|
|
56
|
+
@ra = (opts >> 7) & 0x1
|
|
57
|
+
@z = (opts >> 4) & 0x7
|
|
58
|
+
@rcode = opts & 0xf
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def parse_questions(message, num_questions, ptr)
|
|
62
|
+
idx = HEADER_SIZE # Header takes up the first 12 bytes
|
|
63
|
+
@questions = (0...num_questions).map do
|
|
64
|
+
Question.parse(message, ptr, idx).tap do | q |
|
|
65
|
+
idx += q.size
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
idx
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def parse_records(message, num_records, idx, ptr)
|
|
72
|
+
[num_records.times.map do
|
|
73
|
+
ResourceRecord.parse(message[idx..-1],ptr).tap do | rr |
|
|
74
|
+
ptr.add_arr(rr.add_to_hash,idx)
|
|
75
|
+
idx += rr.size
|
|
76
|
+
end
|
|
77
|
+
end,
|
|
78
|
+
idx]
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def build
|
|
82
|
+
ptr = Pointer.new()
|
|
83
|
+
packet = build_header
|
|
84
|
+
packet << build_questions(ptr, packet.size)
|
|
85
|
+
packet << build_answers(ptr, packet.size)
|
|
86
|
+
packet << build_authority(ptr, packet.size)
|
|
87
|
+
packet << build_additionals(ptr,packet.size)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def build_header
|
|
91
|
+
opts = (@qr & 0x1) << 15 |
|
|
92
|
+
(@opcode & 0xf) << 11 |
|
|
93
|
+
(@aa & 0x1) << 10 |
|
|
94
|
+
(@tc & 0x1) << 9 |
|
|
95
|
+
(@rd & 0x1) << 8 |
|
|
96
|
+
(@ra & 0x1) << 7 |
|
|
97
|
+
(@z & 0x7) << 4 |
|
|
98
|
+
(@z & 0xf)
|
|
99
|
+
[@id, opts,
|
|
100
|
+
@questions.length,
|
|
101
|
+
@answers.length,
|
|
102
|
+
@authority.length,
|
|
103
|
+
@additionals.length].pack("n6")
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def build_questions(ptr, idx)
|
|
107
|
+
@questions.map do | q |
|
|
108
|
+
q.build(ptr,idx).tap do | bytes |
|
|
109
|
+
idx += bytes.length
|
|
110
|
+
end
|
|
111
|
+
end.join("")
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def build_answers(ptr, idx)
|
|
115
|
+
build_record(ptr, idx, @answers)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def build_authority(ptr, idx)
|
|
119
|
+
build_record(ptr, idx, @authority)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def build_additionals(ptr, idx)
|
|
123
|
+
build_record(ptr, idx, @additionals)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def build_record(ptr, idx, records)
|
|
127
|
+
records.map do | rr |
|
|
128
|
+
rr.build(ptr,idx).tap do | r |
|
|
129
|
+
idx += r.length
|
|
130
|
+
end
|
|
131
|
+
end.join("")
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def check_validity
|
|
135
|
+
raise(StandardError, "Bad qr type") if @qr != 0
|
|
136
|
+
raise(StandardError, "No questions in query") if @qdcount < 1
|
|
137
|
+
raise(StandardError, "Empty domain") if @domain_name.empty?
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
end
|
|
141
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module DNSMessage
|
|
2
|
+
module Name
|
|
3
|
+
|
|
4
|
+
NAME_POINTER = 0xc0
|
|
5
|
+
POINTER_MASK = 0x3fff
|
|
6
|
+
|
|
7
|
+
def self.parse(record, ptr)
|
|
8
|
+
# Read name loop
|
|
9
|
+
idx = 0
|
|
10
|
+
name = []
|
|
11
|
+
loop do
|
|
12
|
+
length = record[idx].unpack("c").first
|
|
13
|
+
idx += 1
|
|
14
|
+
if length & NAME_POINTER == NAME_POINTER
|
|
15
|
+
pointer = ((length << 8) | record[idx].unpack("c").first) & POINTER_MASK
|
|
16
|
+
return [ptr.find(pointer),idx+1,false]
|
|
17
|
+
elsif length == 0
|
|
18
|
+
break
|
|
19
|
+
else
|
|
20
|
+
name << record[idx...idx+length]
|
|
21
|
+
idx += length
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
[name.join("."), idx, true]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.build(name,ptr)
|
|
28
|
+
if ptr.find(name)
|
|
29
|
+
[[(ptr.find(name) | NAME_POINTER << 8)].pack("n"),false]
|
|
30
|
+
else
|
|
31
|
+
[name_bytes = name.split(".").map do | section |
|
|
32
|
+
section.length.chr + section
|
|
33
|
+
end.join("") + "\x0", # Terminate will nullptr
|
|
34
|
+
true]
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module DNSMessage
|
|
2
|
+
class Pointer
|
|
3
|
+
|
|
4
|
+
def initialize(hash = {})
|
|
5
|
+
@hash = hash
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def add_arr(arr, offset)
|
|
9
|
+
return unless arr
|
|
10
|
+
arr.each do |k,v|
|
|
11
|
+
k += offset if k.class == Integer
|
|
12
|
+
v += offset if v.class == Integer
|
|
13
|
+
add(k,v)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def add(key,value)
|
|
18
|
+
if key.class == Integer
|
|
19
|
+
add_name(key,value)
|
|
20
|
+
else
|
|
21
|
+
add_ptr(key,value)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def to_h
|
|
26
|
+
@hash
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def find(key)
|
|
30
|
+
@hash[key]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def add_name(key,value)
|
|
36
|
+
value = value.split(".")
|
|
37
|
+
loop do
|
|
38
|
+
return unless value.length > 1
|
|
39
|
+
@hash[key] = value.join(".")
|
|
40
|
+
key += value.shift.length + 1
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def add_ptr(key,value)
|
|
45
|
+
key = key.split(".")
|
|
46
|
+
loop do
|
|
47
|
+
return unless key.length > 1
|
|
48
|
+
@hash[key.join(".")] = value
|
|
49
|
+
value += key.shift.length + 1
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module DNSMessage
|
|
2
|
+
class Question
|
|
3
|
+
|
|
4
|
+
attr_accessor :name, :type, :klass
|
|
5
|
+
attr_reader :size, :add_to_hash
|
|
6
|
+
|
|
7
|
+
def initialize(name: nil, type: nil, klass: Class::IN)
|
|
8
|
+
@name = name
|
|
9
|
+
@type = type
|
|
10
|
+
@klass = klass
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.parse(question, ptr, idx)
|
|
14
|
+
self.new().tap do | q |
|
|
15
|
+
q.parse(question, ptr, idx)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def parse(question, ptr, idx)
|
|
20
|
+
@name, @size, add = Name.parse(question[idx..-1], ptr)
|
|
21
|
+
ptr.add(idx, @name) if add
|
|
22
|
+
|
|
23
|
+
# take last four bytes
|
|
24
|
+
@type, @klass = question[(idx+@size)..-1].unpack("n2")
|
|
25
|
+
@size += 4
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def build(ptr,idx)
|
|
29
|
+
name_bytes, add = Name.build(@name,ptr)
|
|
30
|
+
ptr.add(@name, idx) if add
|
|
31
|
+
name_bytes + [type,klass].pack("n2")
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
module DNSMessage
|
|
2
|
+
class ResourceRecord
|
|
3
|
+
|
|
4
|
+
PARSERS = {
|
|
5
|
+
Type::A => :parse_ip,
|
|
6
|
+
Type::AAAA => :parse_ip,
|
|
7
|
+
Type::CNAME => :parse_name,
|
|
8
|
+
Type::OPT => :parse_opt,
|
|
9
|
+
Type::TXT => :parse_text
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
BUILDERS = {
|
|
13
|
+
Type::A => :build_ip,
|
|
14
|
+
Type::AAAA => :build_ip,
|
|
15
|
+
Type::CNAME => :build_name,
|
|
16
|
+
Type::OPT => :build_opt,
|
|
17
|
+
Type::TXT => :build_text
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
attr_accessor :name, :type, :klass, :ttl, :rdata,
|
|
21
|
+
:opt_udp, :opt_rcode, :opt_edns0_version, :opt_z_dnssec
|
|
22
|
+
attr_reader :size, :add_to_hash
|
|
23
|
+
|
|
24
|
+
def initialize(name: nil, type: nil, klass: Class::IN, ttl: 0,
|
|
25
|
+
rdata: nil)
|
|
26
|
+
@name = name
|
|
27
|
+
@type = type
|
|
28
|
+
@klass = klass
|
|
29
|
+
@ttl = ttl
|
|
30
|
+
@rdata = rdata
|
|
31
|
+
@add_to_hash = []
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def add_to_hash
|
|
35
|
+
@add_to_hash
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.parse(record, ptr)
|
|
39
|
+
self.new().tap do |rr|
|
|
40
|
+
rr.parse(record, ptr)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def parse(record, ptr)
|
|
45
|
+
@name, idx, add = Name.parse(record,ptr)
|
|
46
|
+
@add_to_hash << [idx, @name] if add
|
|
47
|
+
@type, @klass, @ttl, rdata_length = record[idx...idx+10].unpack("nnNn")
|
|
48
|
+
@rdata = send(parser(@type), record, idx+10, rdata_length, ptr)
|
|
49
|
+
@size = idx + 10 + rdata_length
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def build(ptr,idx)
|
|
53
|
+
return "" unless BUILDERS[type]
|
|
54
|
+
|
|
55
|
+
name_bytes, add = Name.build(@name, ptr)
|
|
56
|
+
ptr.add(@name, idx) if add
|
|
57
|
+
data = send(builder(@type),ptr, idx + name_bytes.length)
|
|
58
|
+
@rdata_length = data.length
|
|
59
|
+
name_bytes + [@type, @klass, @ttl, @rdata_length].pack("nnNn") + data
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def self.default_opt(size)
|
|
63
|
+
self.new().tap do | opt |
|
|
64
|
+
opt.name = ""
|
|
65
|
+
opt.type = Type::OPT
|
|
66
|
+
opt.opt_udp = size
|
|
67
|
+
opt.opt_rcode = 0
|
|
68
|
+
opt.opt_edns0_version = 0
|
|
69
|
+
opt.opt_z_dnssec = 0
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
def parser(type)
|
|
76
|
+
PARSERS[type]
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def builder(type)
|
|
80
|
+
BUILDERS[type]
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
##
|
|
84
|
+
## Parsers
|
|
85
|
+
##
|
|
86
|
+
|
|
87
|
+
def parse_ip(rdata, start, length, ptr)
|
|
88
|
+
IPAddr.new_ntoh(rdata[start...start+length])
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def parse_opt(rdata, start, length, ptr)
|
|
92
|
+
@opt_udp = @klass
|
|
93
|
+
@opt_rcode, @opt_edns0_version, @opt_z_dnssec =
|
|
94
|
+
[@ttl].pack("N").unpack("CCn")
|
|
95
|
+
@opt_z_dnssec = @opt_z_dnssec >> 15
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def parse_text(rdata, start, length, ptr)
|
|
99
|
+
txt_length = rdata[start].ord
|
|
100
|
+
rdata[start+1..start+txt_length]
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def parse_name(rdata, start, length, ptr)
|
|
104
|
+
name, idx, add = Name.parse(rdata[0...length], ptr)
|
|
105
|
+
@add_to_hash << [start+idx, name] if add
|
|
106
|
+
name
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
##
|
|
110
|
+
## Builders
|
|
111
|
+
##
|
|
112
|
+
|
|
113
|
+
def build_ip(ptr, _)
|
|
114
|
+
@rdata.hton
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def build_opt(ptr, _)
|
|
118
|
+
@klass = @opt_udp
|
|
119
|
+
@ttl = [@opt_rcode,
|
|
120
|
+
@opt_edns0_version,
|
|
121
|
+
@opt_z_dnssec << 15].pack("CCn").unpack("N").first
|
|
122
|
+
"" # Set RDATA to nothing
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def build_text(ptr, _)
|
|
126
|
+
@rdata.length.chr + rdata
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def build_name(ptr, idx)
|
|
130
|
+
Name.build(@rdata, ptr).tap do | bytes, add |
|
|
131
|
+
ptr.add(@rdata,idx) if add
|
|
132
|
+
return bytes
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Add "alias"
|
|
139
|
+
RR = ResourceRecord
|
|
140
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: dnsmessage
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Claus Lensbøl
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-02-15 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: |-
|
|
14
|
+
A full featured DNS parser. The library supports DNS
|
|
15
|
+
name compression and gives access to parse, build, and manipulate all aspects
|
|
16
|
+
of the DNS queries and replies.
|
|
17
|
+
email:
|
|
18
|
+
- cmol@cmol.dk
|
|
19
|
+
executables: []
|
|
20
|
+
extensions: []
|
|
21
|
+
extra_rdoc_files: []
|
|
22
|
+
files:
|
|
23
|
+
- ".github/workflows/ruby.yml"
|
|
24
|
+
- ".gitignore"
|
|
25
|
+
- ".rspec"
|
|
26
|
+
- ".travis.yml"
|
|
27
|
+
- CODE_OF_CONDUCT.md
|
|
28
|
+
- Gemfile
|
|
29
|
+
- LICENSE.txt
|
|
30
|
+
- README.md
|
|
31
|
+
- Rakefile
|
|
32
|
+
- bin/console
|
|
33
|
+
- bin/setup
|
|
34
|
+
- dnsmessage.gemspec
|
|
35
|
+
- examples/server.rb
|
|
36
|
+
- lib/dnsmessage.rb
|
|
37
|
+
- lib/dnsmessage/enums.rb
|
|
38
|
+
- lib/dnsmessage/message.rb
|
|
39
|
+
- lib/dnsmessage/name.rb
|
|
40
|
+
- lib/dnsmessage/pointer.rb
|
|
41
|
+
- lib/dnsmessage/question.rb
|
|
42
|
+
- lib/dnsmessage/resource_record.rb
|
|
43
|
+
- lib/dnsmessage/version.rb
|
|
44
|
+
homepage: https://github.com/cmol/dnsmessage
|
|
45
|
+
licenses:
|
|
46
|
+
- MIT
|
|
47
|
+
metadata:
|
|
48
|
+
allowed_push_host: https://rubygems.org
|
|
49
|
+
homepage_uri: https://github.com/cmol/dnsmessage
|
|
50
|
+
source_code_uri: https://github.com/cmol/dnsmessage
|
|
51
|
+
changelog_uri: https://github.com/cmol/dnsmessage
|
|
52
|
+
post_install_message:
|
|
53
|
+
rdoc_options: []
|
|
54
|
+
require_paths:
|
|
55
|
+
- lib
|
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: 2.3.0
|
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
requirements: []
|
|
67
|
+
rubyforge_project:
|
|
68
|
+
rubygems_version: 2.7.6
|
|
69
|
+
signing_key:
|
|
70
|
+
specification_version: 4
|
|
71
|
+
summary: Ruby library to build and parse DNS messages
|
|
72
|
+
test_files: []
|