class PassiveDNS::PDNSToolStateDB
creates persistence to the tool state by leveraging SQLite3
Attributes
Public Class Methods
Source
# File lib/passivedns/client/state.rb, line 202 def initialize(sqlitedb = nil) super() @debug = false puts "PDNSToolState initialize #{sqlitedb}" if @debug @level = 0 @sqlitedb = sqlitedb raise 'Cannot use this class without a database file' unless @sqlitedb newdb = true unless File.exist? @sqlitedb @sqlitedbh = SQLite3::Database.new @sqlitedb create_tables if newdb res = @sqlitedbh.execute "select min(level) from queue where state = 'pending'" return unless res res.each do |row| @level = row[0].to_i puts "changed @level = #{@level}" if @debug end end
creates an SQLite3-based Passive DNS Client state only argument is the filename of the sqlite3 database
Calls superclass method
PassiveDNS::PDNSToolState::new
Public Instance Methods
Source
# File lib/passivedns/client/state.rb, line 256 def add_query(query, state, level = @level + 1) return if get_state query curtime = Time.now.to_f puts "add_query(#{query},#{state},level=#{level})" if @debug @sqlitedbh.execute 'insert into queue values (?, ?, ?, ?)', [query.to_s, state, level, curtime] rescue SQLite3::ConstraintException # the query is already queued end
adding a query to the queue of things to be queried, but only if the query isn’t already queued or answered
Source
# File lib/passivedns/client/state.rb, line 243 def add_result(res) puts "adding result: #{res}" if @debug curtime = Time.now.to_f @sqlitedbh.execute('insert into results values (?, ?, ?, ?, ?, ?, ?)', [res.query, res.answer, res.rrtype, res.ttl, res.firstseen, res.lastseen, curtime].map do |v| v.nil? || v.is_a?(Float) ? v : v.to_s end) add_query res.answer, 'pending' add_query res.query, 'pending' end
adds the record to the list of records received and tries to add the answer and query back to the queue for future query
Source
# File lib/passivedns/client/state.rb, line 223 def create_tables puts 'creating tables' if @debug @sqlitedbh.execute 'create table results (query, answer, rrtype, ttl, firstseen, lastseen, ts REAL)' @sqlitedbh.execute 'create table queue (query, state, level INTEGER, ts REAL)' @sqlitedbh.execute 'create index residx on results (ts)' @sqlitedbh.execute 'create unique index queue_unique on queue (query)' @sqlitedbh.execute 'create index queue_level_idx on queue (level)' @sqlitedbh.execute 'create index queue_state_idx on queue (state)' end
creates the sqlite3 tables needed to track the state of this tool as itqueries and recurses
Source
# File lib/passivedns/client/state.rb, line 278 def each_query(max_level = 20) puts "each_query max_level=#{max_level} curlevel=#{@level}" if @debug row = @sqlitedbh.get_first_row "select query, state, level from queue where state = 'failed' or state = 'pending' " \ 'order by level limit 1' return unless row query, state, level = row puts " #{query},#{state},#{level}" if @debug return unless level < max_level update_query query, 'queried' yield query end
returns each query waiting on the queue
Source
# File lib/passivedns/client/state.rb, line 272 def get_state(query) row = @sqlitedbh.get_first_row 'select state from queue where query = ?', [query.to_s] row ? row[0] : false end
returns each query waiting on the queue
Source
# File lib/passivedns/client/state.rb, line 234 def next_result rows = @sqlitedbh.execute 'select query, answer, rrtype, ttl, firstseen, lastseen from results order by ts' rows.each do |row| # the database does not keep the source or the response time yield PDNSResult.new(nil, nil, *row) end end
returns the next record
Source
# File lib/passivedns/client/state.rb, line 267 def update_query(query, state) @sqlitedbh.execute 'update queue set state = ? where query = ?', [state, query.to_s] end
sets the state of a given query