Class: PassiveDNS::PDNSToolStateDB

Inherits:
PDNSToolState show all
Defined in:
lib/passivedns/client/state.rb

Instance Attribute Summary collapse

Attributes inherited from PDNSToolState

#debug

Instance Method Summary collapse

Methods inherited from PDNSToolState

#to_gdf, #to_graphml, #to_graphviz, #to_json, #to_s, #to_xml, #to_yaml

Constructor Details

#initialize(sqlitedb = nil) ⇒ PDNSToolStateDB

Returns a new instance of PDNSToolStateDB.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/passivedns/client/state.rb', line 186

def initialize(sqlitedb=nil)
	puts "PDNSToolState  initialize  #{sqlitedb}" if @debug
	@level = 0
	@sqlitedb = sqlitedb
	raise "Cannot use this class without a database file" unless @sqlitedb
	unless File.exists?(@sqlitedb)
		newdb = true
	end
	@sqlitedbh = SQLite3::Database.new(@sqlitedb)
	if newdb
		create_tables
	end
	res = @sqlitedbh.execute("select min(level) from queue where state = 'pending'")
	if res
		res.each do |row|
			@level = row[0].to_i
			puts "changed @level = #{@level}" if @debug
		end
	end
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



185
186
187
# File 'lib/passivedns/client/state.rb', line 185

def level
  @level
end

Instance Method Details

#add_query(query, state, level = @level+1) ⇒ Object



233
234
235
236
237
238
239
240
241
# File 'lib/passivedns/client/state.rb', line 233

def add_query(query,state,level=@level+1)
	return if get_state(query)
	curtime = Time.now().to_f
	begin
		puts "add_query(#{query},#{state},level=#{level})" if @debug
		@sqlitedbh.execute("insert into queue values ('#{query}','#{state}',#{level},#{curtime})")
	rescue
	end
end

#add_result(res) ⇒ Object



224
225
226
227
228
229
230
231
# File 'lib/passivedns/client/state.rb', line 224

def add_result(res)
	puts "adding result: #{res.to_s}" 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})")

	add_query(res.answer,'pending')
	add_query(res.query,'pending')
end

#create_tablesObject



207
208
209
210
211
212
213
214
215
# File 'lib/passivedns/client/state.rb', line 207

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

#each_query(max_level = 20) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/passivedns/client/state.rb', line 257

def each_query(max_level=20)
	puts "each_query max_level=#{max_level} curlevel=#{@level}" if @debug
	rows = @sqlitedbh.execute("select query, state, level from queue where state = 'failed' or state = 'pending' order by level limit 1")
	if rows
		rows.each do |row|
			query,state,level = row
			puts "  #{query},#{state},#{level}" if @debug
			if level < max_level
				update_query(query,'queried')
				yield query
			end
		end
	end
end

#get_state(query) ⇒ Object



247
248
249
250
251
252
253
254
255
# File 'lib/passivedns/client/state.rb', line 247

def get_state(query)
	rows = @sqlitedbh.execute("select state from queue where query = '#{query}'")
	if rows
		rows.each do |row|
			return row[0]
		end
	end
	false
end

#next_resultObject



217
218
219
220
221
222
# File 'lib/passivedns/client/state.rb', line 217

def next_result
	rows = @sqlitedbh.execute("select query, answer, rrtype, ttl, firstseen, lastseen from results order by ts")
	rows.each do |row|
		yield PDNSResult.new(*row)
	end
end

#update_query(query, state) ⇒ Object



243
244
245
# File 'lib/passivedns/client/state.rb', line 243

def update_query(query,state)
	@sqlitedbh.execute("update queue set state = '#{state}' where query = '#{query}'")
end