CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/net/dns/resolver/timeouts.rb
Views: 1904
1
# -*- coding: binary -*-
2
require 'timeout'
3
4
module SecondsHandle #:nodoc: all
5
def transform(secs)
6
case secs
7
when 0
8
to_s
9
when 1..59
10
"#{secs} seconds"
11
when 60..3559
12
"#{secs/60} minutes and #{secs%60} seconds"
13
else
14
hours = secs/3600
15
secs -= (hours*3600)
16
"#{hours} hours, #{secs/60} minutes and #{secs%60} seconds"
17
end
18
end
19
end
20
21
class DnsTimeout # :nodoc: all
22
23
include SecondsHandle
24
25
def initialize(seconds)
26
if seconds.is_a? Numeric and seconds >= 0
27
@timeout = seconds
28
else
29
raise DnsTimeoutArgumentError, "Invalid value for tcp timeout"
30
end
31
end
32
33
def to_i
34
@timeout
35
end
36
37
def to_s
38
if @timeout == 0
39
@output
40
else
41
@timeout.to_s
42
end
43
end
44
45
def pretty_to_s
46
transform(@timeout)
47
end
48
49
def timeout
50
unless block_given?
51
raise DnsTimeoutArgumentError, "Block required but missing"
52
end
53
if @timeout == 0
54
yield
55
else
56
return Timeout.timeout(@timeout) do
57
yield
58
end
59
end
60
end
61
end
62
63
class TcpTimeout < DnsTimeout # :nodoc: all
64
def initialize(seconds)
65
@output = "infinite"
66
super(seconds)
67
end
68
end
69
70
class UdpTimeout < DnsTimeout # :nodoc: all
71
def initialize(seconds)
72
@output = "not defined"
73
super(seconds)
74
end
75
end
76
77
class DnsTimeoutArgumentError < ArgumentError # :nodoc: all
78
end
79
80