Class: Net::DNS::MDNS::Service

Inherits:
Object
  • Object
show all
Includes:
Net::DNS
Defined in:
lib/net/dns/mdns.rb

Overview

BackgroundQuery

Constant Summary

Constants included from Net::DNS

DecodeError, Net::DNS::Message, Name

Instance Method Summary collapse

Methods included from Net::DNS

rrname

Constructor Details

#initialize(name, type, port, txt = {}, target = nil, &proc) ⇒ Service

Returns a new instance of Service.



1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'lib/net/dns/mdns.rb', line 1054

def initialize(name, type, port, txt = {}, target = nil, &proc)
  # TODO - escape special characters
  @name = DNS::Name.create(name.to_str)
  @type = DNS::Name.create(type.to_str)
  @domain = DNS::Name.create('local')
  @port = port.to_int
  if target
    @target = DNS::Name.create(target)
    @hostrr = nil
  else
    @target = Responder.instance.hostname
    @hostrr = Responder.instance.hostrr
  end

  @txt = txt || {}
  @ttl = nil
  @priority = 0
  @weight = 0

  proc.call(self) if proc

  @srvttl = @ttl ||  240
  @ptrttl = @ttl || 7200

  @domain = Name.new(@domain.to_a, true)
  @type = @type + @domain
  @instance = @name + @type
  @enum = Name.create('_services._dns-sd._udp.') + @domain

  # build the RRs

  @rrenum = IN::PTR.new(@type)

  @rrptr = IN::PTR.new(@instance)

  @rrsrv = IN::SRV.new(@priority, @weight, @port, @target)

  strings = @txt.map { |k,v| k + '=' + v }

  @rrtxt = IN::TXT.new(*strings)

  # class << self
  #   undef_method 'ttl='
  # end
  #  -or-
  # undef :ttl=
  #
  # TODO - all the others

  start
end

Instance Method Details

#[]=(key, value) ⇒ Object

Set key/value pairs in a TXT record associated with SRV.



1042
1043
1044
# File 'lib/net/dns/mdns.rb', line 1042

def []=(key, value)
  @txt[key.to_str] = value.to_str
end

#answer_question(name, rtype, amsg) ⇒ Object

Questions we can answer: @instance:

name.type.domain -> SRV, TXT

@type:

type.domain -> PTR:name.type.domain

@enum:

_services._dns-sd._udp.<domain> -> PTR:type.domain


983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/net/dns/mdns.rb', line 983

def answer_question(name, rtype, amsg)
  case name
  when @instance
    # See [DNSSD:14.2]
    case rtype.object_id
    when IN::ANY.object_id
      amsg.add_question(name, rtype)
      amsg.add_answer(@instance, @srvttl, @rrsrv)
      amsg.add_answer(@instance, @srvttl, @rrtxt)
      amsg.add_additional(*@hostrr) if @hostrr
 
    when IN::SRV.object_id
      amsg.add_question(name, rtype)
      amsg.add_answer(@instance, @srvttl, @rrsrv)
      amsg.add_additional(*@hostrr) if @hostrr
 
    when IN::TXT.object_id
      amsg.add_question(name, rtype)
      amsg.add_answer(@instance, @srvttl, @rrtxt)
    end

  when @type
    # See [DNSSD:14.1]
    case rtype.object_id
    when IN::ANY.object_id, IN::PTR.object_id
      amsg.add_question(name, rtype)
      amsg.add_answer(@type,     @ptrttl, @rrptr)
      amsg.add_additional(@instance, @srvttl, @rrsrv)
      amsg.add_additional(@instance, @srvttl, @rrtxt)
      amsg.add_additional(*@hostrr) if @hostrr
    end

  when @enum
    case rtype.object_id
    when IN::ANY.object_id, IN::PTR.object_id
      amsg.add_question(name, rtype)
      amsg.add_answer(@type, @ptrttl, @rrenum)
    end

  end
end

#domain=(domain) ⇒ Object

Default - .local



1038
1039
1040
# File 'lib/net/dns/mdns.rb', line 1038

def domain=(domain)
  @domain = DNS::Name.create(domain.to_str)
end

#inspectObject



1050
1051
1052
# File 'lib/net/dns/mdns.rb', line 1050

def inspect
  "#<#{self.class}: #{@instance} is #{@target}:#{@port}>"
end

#priority=(secs) ⇒ Object

Default - 0



1030
1031
1032
# File 'lib/net/dns/mdns.rb', line 1030

def priority=(secs)
  @priority = secs.to_int
end

#startObject



1106
1107
1108
1109
1110
1111
1112
1113
1114
# File 'lib/net/dns/mdns.rb', line 1106

def start
  Responder.instance.service_start(self, [
       [@type, @ptrttl, @rrptr],
       [@instance, @srvttl, @rrsrv],
       [@instance, @srvttl, @rrtxt],
       @hostrr
    ].compact)
  self
end

#stopObject



1116
1117
1118
1119
# File 'lib/net/dns/mdns.rb', line 1116

def stop
  Responder.instance.service_stop(self)
  self
end

#to_sObject



1046
1047
1048
# File 'lib/net/dns/mdns.rb', line 1046

def to_s
  "MDNS::Service: #{@instance} is #{@target}:#{@port}>"
end

#ttl=(secs) ⇒ Object

Default - 7 days



1026
1027
1028
# File 'lib/net/dns/mdns.rb', line 1026

def ttl=(secs)
  @ttl = secs.to_int
end

#weight=(secs) ⇒ Object

Default - 0



1034
1035
1036
# File 'lib/net/dns/mdns.rb', line 1034

def weight=(secs)
  @weight = secs.to_int
end