mdns 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eecfa9139011bd96bd24cfcd7c8ba37fc8ee947b
4
+ data.tar.gz: dbda624febbc8dd4c218e3f06653c984f52367d0
5
+ SHA512:
6
+ metadata.gz: b14273868c4ee104d216226a12b6c91831ee91f848d56caa613aac1727e07fc3655c471ba74b6511882390fde1f903126d7bfea70baa6d3996baba45828c10a0
7
+ data.tar.gz: 2f6143386390695317b2f1a163a18df969e100e81cf0d6b44736032bc7d6901edf5d95737d964d6ccc07b3452abe3a3050cb810f7772f90ca14217a557d3911b
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ notifications:
3
+ email: false
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.1
8
+ - jruby-19mode
9
+ - rbx
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mdns.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jack Chen (chendo)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # mDNS
2
+
3
+ Ever wanted to create your own custom `.local` hostnames on your network? Now you can!
4
+
5
+ This gem implements a super naive mDNS server that listens for queries matching entries you add, and responds to them.
6
+
7
+ This gem was created by observing existing mDNS queries and responses. I did not read the RFC, so it might not follow the spec properly.
8
+
9
+ Tested on OS X Mavericks.
10
+
11
+ Should work with any client that implements mDNS resolver.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'mdns'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install mdns
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require "mdns"
31
+ MDNS.add_record(Net::DNS::RR.new("leet.local. 60 A 10.0.13.37")) # Add a record for leet.local to resolve to 10.0.13.37 with a TTL of 60 seconds
32
+ MDNS.add_record("leet.local. 60 A 10.0.13.37")) # Can pass in a string too
33
+
34
+ MDNS.start # Start listening
35
+ ```
36
+
37
+ ## Caveats
38
+
39
+ * Does not "unpublish" a record when stopping. Couldn't seem to find an example response.
40
+ * Only supports A records
41
+
42
+ ## License
43
+
44
+ The MIT License (MIT)
45
+
46
+ Copyright (c) 2014 Jack "chendo" Chen
47
+
48
+ Permission is hereby granted, free of charge, to any person obtaining a copy
49
+ of this software and associated documentation files (the "Software"), to deal
50
+ in the Software without restriction, including without limitation the rights
51
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
52
+ copies of the Software, and to permit persons to whom the Software is
53
+ furnished to do so, subject to the following conditions:
54
+
55
+ The above copyright notice and this permission notice shall be included in
56
+ all copies or substantial portions of the Software.
57
+
58
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
64
+ THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,86 @@
1
+ require "mdns/version"
2
+ require "net/dns"
3
+
4
+ class MDNS
5
+ MULTICAST_IP = '224.0.0.251'
6
+ MDNS_PORT = 5353
7
+
8
+ class << self
9
+ def start
10
+ @socket = UDPSocket.new
11
+ @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
12
+ @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, true)
13
+ ip_mreq = IPAddr.new(MULTICAST_IP).hton + IPAddr.new('0.0.0.0').hton
14
+ @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, ip_mreq)
15
+ @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, 255)
16
+ @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_TTL, 255)
17
+ @socket.bind(Socket::INADDR_ANY, MDNS_PORT)
18
+ Thread.abort_on_exception = true
19
+ @thr = Thread.new do
20
+ loop do
21
+ data = @socket.recvfrom(1024)
22
+ packet = begin
23
+ Net::DNS::Packet::parse(data)
24
+ rescue => e
25
+ # Net::DNS::Packet doesn't handle a bunch of mDNS packets
26
+ end
27
+ next if packet.nil?
28
+ if packet.header.query? && packet.question.any? { |q| hosts.include?(q.qName.downcase) && q.qType.to_s == 'A' }
29
+ respond_to(packet)
30
+ end
31
+ end
32
+ end
33
+
34
+ records.values.each do |record|
35
+ respond_with(record)
36
+ end
37
+
38
+ at_exit do
39
+ stop
40
+ end
41
+ end
42
+
43
+ def respond_to(query)
44
+ puts "#{Time.now} Responding to query"
45
+ record = records[query.question.first.qName.downcase]
46
+ respond_with(record)
47
+ end
48
+
49
+ def respond_with(record)
50
+ # I have no idea what I'm doing
51
+ response = Net::DNS::Packet.new(record.name)
52
+ response.header.qr = true
53
+ response.header.aa = true
54
+ response.header.anCount = 1
55
+ response.header.arCount = 1
56
+ response.answer = record
57
+ @socket.send(response.data, 0, MULTICAST_IP, MDNS_PORT)
58
+ end
59
+
60
+ def add_record(record)
61
+ if record.is_a?(String)
62
+ record = Net::DNS::RR.new(record)
63
+ end
64
+ records[record.name.downcase] = record
65
+ respond_with(record) if @socket && !@socket.closed?
66
+ end
67
+
68
+ def records
69
+ @records ||= {}
70
+ end
71
+
72
+ def hosts
73
+ records.keys
74
+ end
75
+
76
+ def stop
77
+ if @thr
78
+ @thr.kill
79
+ @thr = nil
80
+ end
81
+ if @socket && !@socket.closed?
82
+ @socket.close
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ class MDNS
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mdns/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mdns"
8
+ spec.version = MDNS::VERSION
9
+ spec.authors = ["Jack Chen (chendo)"]
10
+ spec.email = ["github+mdns@chen.do"]
11
+ spec.summary = %q{Create your own .local address with mdns!}
12
+ spec.description = %q{mdns allows you to create your own .local hostnames and point them to whatever IP you want.}
13
+ spec.homepage = "https://github.com/chendo/mdns"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "net-dns", "~> 0.8"
22
+ spec.add_development_dependency "rspec", "~> 2.14"
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'resolv'
3
+
4
+ describe MDNS do
5
+ let(:domain) { "foo-bar-#{rand(1000)}.local." }
6
+ let(:ip) { "10.10.10.10" }
7
+ describe "integration test" do
8
+
9
+ context "MDNS not running" do
10
+ it "does not resolve" do
11
+ expect do
12
+ Socket.gethostbyname(domain)
13
+ end.to raise_error(SocketError)
14
+ end
15
+ end
16
+
17
+ context "MDNS is running" do
18
+ before do
19
+ MDNS.add_record(Net::DNS::RR.new("#{domain} 60 A #{ip}"))
20
+ MDNS.start
21
+ end
22
+
23
+ after do
24
+ MDNS.stop
25
+ end
26
+
27
+ it "does resolve" do
28
+ expect(Socket.gethostbyname(domain)).to be_a(Array)
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '.add_record' do
34
+ it "takes a string" do
35
+ MDNS.add_record("#{domain} 60 A #{ip}")
36
+ expect(MDNS.records.keys).to eq([domain])
37
+ expect(MDNS.records.values.first).to be_a(Net::DNS::RR)
38
+ end
39
+
40
+ it "takes a Net::DNS::RR" do
41
+ MDNS.add_record(Net::DNS::RR.new("#{domain} 60 A #{ip}"))
42
+ expect(MDNS.records.keys).to eq([domain])
43
+ expect(MDNS.records.values.first).to be_a(Net::DNS::RR)
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
2
+ require 'mdns'
3
+
4
+ RSpec.configure do |config|
5
+ config.formatter = 'documentation'
6
+ config.color = true
7
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mdns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jack Chen (chendo)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-dns
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: mdns allows you to create your own .local hostnames and point them to
70
+ whatever IP you want.
71
+ email:
72
+ - github+mdns@chen.do
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/mdns.rb
84
+ - lib/mdns/version.rb
85
+ - mdns.gemspec
86
+ - spec/mdns_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/chendo/mdns
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Create your own .local address with mdns!
112
+ test_files:
113
+ - spec/mdns_spec.rb
114
+ - spec/spec_helper.rb
115
+ has_rdoc: