Class: ZeroConf::Service

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/zeroconf/service.rb

Constant Summary

Constants included from Utils

Utils::BROADCAST_V4, Utils::BROADCAST_V6, Utils::DISCOVERY_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#broadcast_v4, #broadcast_v6, #multicast_send, #open_ipv4, #open_ipv6, #unicast_send

Constructor Details

#initialize(service, service_port, hostname = Socket.gethostname, service_interfaces: ZeroConf.service_interfaces, text: [""], abort_on_malformed_requests: false, started_callback: nil) ⇒ Service

Returns a new instance of Service.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/zeroconf/service.rb', line 12

def initialize service, service_port, hostname = Socket.gethostname, service_interfaces: ZeroConf.service_interfaces, text: [""], abort_on_malformed_requests: false, started_callback: nil
  @service = service
  @service_port = service_port
  @hostname = hostname
  @service_interfaces = service_interfaces
  @abort_on_malformed_requests = abort_on_malformed_requests
  @service_name = "#{hostname}.#{service}"
  @qualified_host = "#{hostname}.local."
  @started_callback = started_callback
  @text = text
  @started = false
  @rd, @wr = IO.pipe
end

Instance Attribute Details

#abort_on_malformed_requestsObject (readonly)

Returns the value of attribute abort_on_malformed_requests.



9
10
11
# File 'lib/zeroconf/service.rb', line 9

def abort_on_malformed_requests
  @abort_on_malformed_requests
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



9
10
11
# File 'lib/zeroconf/service.rb', line 9

def hostname
  @hostname
end

#qualified_hostObject (readonly)

Returns the value of attribute qualified_host.



9
10
11
# File 'lib/zeroconf/service.rb', line 9

def qualified_host
  @qualified_host
end

#serviceObject (readonly)

Returns the value of attribute service.



9
10
11
# File 'lib/zeroconf/service.rb', line 9

def service
  @service
end

#service_interfacesObject (readonly)

Returns the value of attribute service_interfaces.



9
10
11
# File 'lib/zeroconf/service.rb', line 9

def service_interfaces
  @service_interfaces
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



9
10
11
# File 'lib/zeroconf/service.rb', line 9

def service_name
  @service_name
end

#service_portObject (readonly)

Returns the value of attribute service_port.



9
10
11
# File 'lib/zeroconf/service.rb', line 9

def service_port
  @service_port
end

#textObject (readonly)

Returns the value of attribute text.



9
10
11
# File 'lib/zeroconf/service.rb', line 9

def text
  @text
end

Instance Method Details

#announcementObject



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
# File 'lib/zeroconf/service.rb', line 28

def announcement
  msg = Resolv::DNS::Message.new(0)
  msg.qr = 1
  msg.aa = 1

  msg.add_additional service_name, 60, MDNS::Announce::IN::SRV.new(0, 0, service_port, qualified_host)

  service_interfaces.each do |iface|
    if iface.addr.ipv4?
      msg.add_additional qualified_host,
        60,
        MDNS::Announce::IN::A.new(iface.addr.ip_address)
    else
      msg.add_additional qualified_host,
        60,
        MDNS::Announce::IN::AAAA.new(iface.addr.ip_address)
    end
  end

  if @text
    msg.add_additional service_name,
      60,
      MDNS::Announce::IN::TXT.new(*@text)
  end

  msg.add_answer service,
    60,
    Resolv::DNS::Resource::IN::PTR.new(Resolv::DNS::Name.create(service_name))

  msg
end

#disconnect_msgObject



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
# File 'lib/zeroconf/service.rb', line 60

def disconnect_msg
  msg = Resolv::DNS::Message.new(0)
  msg.qr = 1
  msg.aa = 1

  msg.add_additional service_name, 0, Resolv::DNS::Resource::IN::SRV.new(0, 0, service_port, qualified_host)

  service_interfaces.each do |iface|
    if iface.addr.ipv4?
      msg.add_additional qualified_host,
        0,
        Resolv::DNS::Resource::IN::A.new(iface.addr.ip_address)
    else
      msg.add_additional qualified_host,
        0,
        Resolv::DNS::Resource::IN::AAAA.new(iface.addr.ip_address)
    end
  end

  if @text
    msg.add_additional service_name,
      0,
      Resolv::DNS::Resource::IN::TXT.new(*@text)
  end

  msg.add_answer service,
    0,
    Resolv::DNS::Resource::IN::PTR.new(Resolv::DNS::Name.create(service_name))

  msg
end

#startObject



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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/zeroconf/service.rb', line 99

def start
  sock = open_ipv4 Addrinfo.new(Socket.sockaddr_in(Resolv::MDNS::Port, Socket::INADDR_ANY)), Resolv::MDNS::Port

  sockets = [sock, @rd]

  msg = announcement

  # announce
  multicast_send(sock, msg.encode)

  @started = true
  @started_callback&.call

  loop do
    readers, = IO.select(sockets, [], [])
    next unless readers

    readers.each do |reader|
      if reader == @rd
        @rd.close
        return
      end

      buf, from = reader.recvfrom 2048
      msg = begin
        Resolv::DNS::Message.decode(buf)
      rescue Resolv::DNS::DecodeError
        next unless abort_on_malformed_requests
        @rd.close
        stop
        raise
      end

      has_flags = (buf.getbyte(3) << 8 | buf.getbyte(2)) != 0

      msg.question.each do |name, type|
        class_type = type::ClassValue & ~MDNS_CACHE_FLUSH

        break unless class_type == 1 || class_type == 255

        unicast = type::ClassValue & PTR::MDNS_UNICAST_RESPONSE > 0

        qn = name.to_s + "."

        res = case qn
        when DISCOVERY_NAME
          break if has_flags

          if unicast
            dnssd_unicast_answer
          else
            dnssd_multicast_answer
          end
        when service
          if unicast
            service_unicast_answer
          else
            service_multicast_answer
          end
        when service_name
          if unicast
            service_instance_unicast_answer
          else
            service_instance_multicast_answer
          end
        when qualified_host
          if unicast
            name_answer_unicast
          else
            name_answer_multicast
          end
        else
          #p [:QUERY2, type, type::ClassValue, name]
        end

        next unless res

        if unicast
          unicast_send reader, res.encode, from
        else
          multicast_send reader, res.encode
        end
      end

      # only yield replies to this question
    end
  end
ensure
  multicast_send(sock, disconnect_msg.encode) if sock
  sockets.map(&:close) if sockets
end

#started?Boolean

Returns:

  • (Boolean)


26
# File 'lib/zeroconf/service.rb', line 26

def started?; @started; end

#stopObject



92
93
94
95
96
97
# File 'lib/zeroconf/service.rb', line 92

def stop
  return unless @started
  @wr.write "x"
  @wr.close
  @started = false
end