Skip to content

Commit

Permalink
Fixing some bugs pointed out by Matt Wright.
Browse files Browse the repository at this point in the history
Thanks Matt!
  • Loading branch information
Roguelazer committed Apr 21, 2010
1 parent f73a515 commit 8196960
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 51 deletions.
41 changes: 29 additions & 12 deletions src/avahi_model.rb
Expand Up @@ -120,12 +120,7 @@ def add_type_listener(service_type)
host = msg.params[5]
address = msg.params[7]
port = msg.params[8]
val = ""
msg.params[9].each { |c|
val += c.pack("c*")
val += " "
}
txt = val
txt = Wamupd::AvahiModel.pack_txt_param(msg.params[9])
add_service_record(name, type, host, port, txt)
end
end
Expand All @@ -138,19 +133,41 @@ def add_type_listener(service_type)
host = msg.params[5]
address = msg.params[7]
port = msg.params[8]
val = ""
msg.params[9].each { |c|
val += c.pack("c*")
val += " "
}
txt = val
txt = Wamupd::AvahiModel.pack_txt_param(msg.params[9])
remove_service_record(name, type, host, port, txt)
end
end
end
}
end

# Pack an array of TXT parameters into a single TXT record
# appropriately
#
# Note: The actual TXT record should consist of name-value pairs,
# with each pair delimited by its length.
#
# As of 2010-04-21, Dnsruby internally converts double-quoted,
# space-separted strings into the appropriate format. Most
# annoyingly, if you provide it input already in the right format,
# it sticks a completely unnecessary byte on the front and screws it
# up.
#
# Aren't you happy you know that now?
def self.pack_txt_param(strs)
val = ""
strs.each { |c|
# Dnsruby uses multi-byte lengths if any of your records are
# over 255 characters, though. This makes mDNSResponder get
# amusingly confused.
if (c.length > 255)
next
end
val += "\"#{c.pack("c*")}\" "
}
return val.chop
end

# Construct an AvahiService from the given parameters
def add_service_record(name, type, host, port, txt)
# Replace the .local that Avahi sticks at the end of the host (why
Expand Down
2 changes: 1 addition & 1 deletion src/avahi_service.rb
Expand Up @@ -79,7 +79,7 @@ def target

# TXT record
def txt
return (@txt.nil? || @txt == "") ? "\0" : @txt
return (@txt.nil? || @txt == "") ? false : @txt
end

# Initialize
Expand Down
9 changes: 6 additions & 3 deletions src/dns_avahi_controller.rb
Expand Up @@ -148,9 +148,12 @@ def publish(service, ttl=@sa.ttl, lease_time=@sa.lease_time)
to_update << {:target=>service.type_in_zone_with_name,
:type=>Dnsruby::Types.SRV, :ttl=>ttl,
:value=> "#{@sa.priority} #{@sa.weight} #{service.port} #{service.target}"}
to_update << {:target => service.type_in_zone_with_name,
:type=>Dnsruby::Types.TXT, :ttl=>ttl,
:value=>service.txt}
# why doesn't Ruby have !==
unless (service.txt === false)
to_update << {:target => service.type_in_zone_with_name,
:type=>Dnsruby::Types.TXT, :ttl=>ttl,
:value=>service.txt}
end
update_time = Time.now() + lease_time
@lease_queue.push(Wamupd::LeaseUpdate.new(update_time, service), update_time)
return DNSUpdate.publish_all(to_update)
Expand Down
2 changes: 0 additions & 2 deletions src/main.rb
Expand Up @@ -39,8 +39,6 @@
# Show this help
# -v, --verbose
# Be verbose
#
# :title: wamupd

# Update the include path
$:.push(File.dirname(__FILE__))
Expand Down
4 changes: 3 additions & 1 deletion src/main_settings.rb
Expand Up @@ -86,6 +86,7 @@ def initialize
@ipv6 = nil
@sleep_time = 60
@max_dns_response_time=10
@zone = ""
end

# Are we using DNSSEC?
Expand Down Expand Up @@ -151,7 +152,8 @@ def make_resolver
:nameserver => self.dns_server,
:port => self.dns_port,
:tsig => ts,
:dnssec => false
:dnssec => false,
:use_tcp => true
})

end
Expand Down
29 changes: 29 additions & 0 deletions test/test_avahi_model.rb
@@ -0,0 +1,29 @@
#!/usr/bin/ruby
# Copyright (C) 2009-2010 James Brown <roguelazer@roguelazer.com>.
#
# This file is part of wamupd.
#
# wamupd is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wamupd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with wamupd. If not, see <http://www.gnu.org/licenses/>.

require 'test/unit'
require 'main_settings'
require 'avahi_model'

class Test::AvahiModel < Test::Unit::TestCase
def test_pack
array = [[102,97,99,101],[99,97,98]]
#assert_equal("\004face\003cab", Wamupd::AvahiModel.pack_txt_param(array))
assert_equal("\"face\" \"cab\"", Wamupd::AvahiModel.pack_txt_param(array))
end
end
2 changes: 1 addition & 1 deletion test/test_avahi_service.rb
Expand Up @@ -45,6 +45,6 @@ def test_hash_construct
assert_nil(a.hostname)
assert_nil(a.domainname)
assert_nil(a.port)
assert_equal("\0", a.txt)
assert_equal(false, a.txt)
end
end
2 changes: 1 addition & 1 deletion test/test_avahi_service_file.rb
Expand Up @@ -38,7 +38,7 @@ def test_ssh
assert_equal(22, s.port)
assert_nil(s.subtype)
assert_nil(s.hostname)
assert_equal("\0", s.txt)
assert_equal(false, s.txt)
}
end

Expand Down
33 changes: 3 additions & 30 deletions test/test_dns_avahi_controller.rb
Expand Up @@ -48,42 +48,15 @@ def test_parallel
d.run
}
ct = Thread.new {
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::ADD, ""))
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::ADD, Wamupd::AvahiService.new("", {:type=>"test"})))
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::ADD, Wamupd::AvahiService.new("", {:type=>"test2"})))
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::QUIT))
}
dt.join
ct.join
assert_equal(1, i)
assert_equal(2, i)
end

def test_parallel_2
d = Wamupd::DNSAvahiController.new()
i = 0
dt = Thread.new {
d.on(:added) {
i += 1
}
d.on(:quit) {
Thread.exit
}
d.run
}
ct = Thread.new {
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::ADD, ""))
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::ADD, ""))
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::ADD, ""))
}
c2t = Thread.new {
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::ADD, ""))
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::ADD, ""))
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::ADD, ""))
d.queue.push(Wamupd::Action.new(Wamupd::ActionType::QUIT, ""))
}
dt.join
ct.join
c2t.join
assert_equal(6, i)
end

def test_raise
d = Wamupd::DNSAvahiController.new
Expand Down

0 comments on commit 8196960

Please sign in to comment.