Class: TFTP::Handler::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tftp/tftp.rb

Overview

Base handler contains the common methods for real handlers.

Direct Known Subclasses

RWSimple

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Initialize the handler.

Options:

  • :logger => logger object (e.g. a Logger instance)
  • :timeout => used while waiting for next DATA/ACK packets (default: 5s)

All given options are saved in @opts.

Parameters:

  • opts (Hash) (defaults to: {})

    Options



109
110
111
112
113
# File 'lib/tftp/tftp.rb', line 109

def initialize(opts = {})
  @logger = opts[:logger]
  @timeout = opts[:timeout] || 5
  @opts = opts
end

Instance Method Details

#log(level, msg) ⇒ Object (private)



196
197
198
# File 'lib/tftp/tftp.rb', line 196

def log(level, msg)
  @logger.send(level, msg) if @logger
end

#recv(tag, sock, io) ⇒ Boolean

Receive data over an established connection.

Doesn't close neither sock nor io. Returns true if whole file has been received, false otherwise.

Parameters:

  • tag (String)

    Tag used for logging

  • sock (UDPSocket)

    Connected socket

  • io (IO)

    Object to write data to

Returns:

  • (Boolean)


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
190
191
192
193
# File 'lib/tftp/tftp.rb', line 163

def recv(tag, sock, io)
  sock.send(Packet::ACK.new(0).encode, 0)
  seq = 1
  begin
    loop do
      unless IO.select([sock], nil, nil, @timeout)
        log :warn, "#{tag} Timeout at block ##{seq}"
        return false
      end
      msg, _ = sock.recvfrom(516, 0)
      pkt = Packet.parse(msg)
      if pkt.class != Packet::DATA
        log :warn, "#{tag} Expected DATA but got: #{pkt.class}"
        return false
      end
      if pkt.seq != seq
        log :warn, "#{tag} Seq mismatch: #{seq} != #{pkt.seq}"
        return false
      end
      io.write(pkt.data)
      sock.send(Packet::ACK.new(seq).encode, 0)
      break if pkt.last?
      seq = (seq + 1) & 0xFFFF
    end
  rescue ParseError => e
    log :warn, "#{tag} Packet parse error: #{e.to_s}"
    return false
  end
  log :info, "#{tag} Received file"
  true
end

#send(tag, sock, io) ⇒ Object

Send data over an established connection.

Doesn't close neither sock nor io.

Parameters:

  • tag (String)

    Tag used for logging

  • sock (UDPSocket)

    Connected socket

  • io (IO)

    Object to send data from



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
# File 'lib/tftp/tftp.rb', line 122

def send(tag, sock, io)
  seq = 1
  begin
    while not io.eof?
      block = io.read(512)
      sock.send(Packet::DATA.new(seq, block).encode, 0)
      unless IO.select([sock], nil, nil, @timeout)
        log :warn, "#{tag} Timeout at block ##{seq}"
        return
      end
      msg, _ = sock.recvfrom(4, 0)
      pkt = Packet.parse(msg)
      if pkt.class != Packet::ACK
        log :warn, "#{tag} Expected ACK but got: #{pkt.class}"
        return
      end
      if pkt.seq != seq
        log :warn, "#{tag} Seq mismatch: #{seq} != #{pkt.seq}"
        return
      end
      # Increment with wrap around at 16 bit boundary,
      # because of tftp block number field size limit.
      seq = (seq + 1) & 0xFFFF
    end
    sock.send(Packet::DATA.new(seq, '').encode, 0) if io.size % 512 == 0
  rescue ParseError => e
    log :warn, "#{tag} Packet parse error: #{e.to_s}"
    return
  end
  log :info, "#{tag} Sent file"
end