File: connectivity.rb

package info (click to toggle)
zonecheck 2.0.4-13lenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,424 kB
  • ctags: 1,177
  • sloc: ruby: 9,582; xml: 731; sh: 574; makefile: 66
file content (145 lines) | stat: -rw-r--r-- 4,350 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
59
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
91
92
93
94
95
96
97
98
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
# ZCTEST 1.0
# $Id: connectivity.rb,v 1.22 2003/11/24 10:07:21 sdalu Exp $

# 
# CONTACT     : zonecheck@nic.fr
# AUTHOR      : Stephane D'Alu <sdalu@nic.fr>
#
# CREATED     : 2002/08/02 13:58:17
# REVISION    : $Revision: 1.22 $ 
# DATE        : $Date: 2003/11/24 10:07:21 $
#
# CONTRIBUTORS: (see also CREDITS file)
#
#
# LICENSE     : GPL v2 (or MIT/X11-like after agreement)
# COPYRIGHT   : AFNIC (c) 2003
#
# This file is part of ZoneCheck.
#
# ZoneCheck is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# ZoneCheck is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZoneCheck; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

#####
#
# WARN:
#   - 'chk_icmp' contains system dependant 'ping' command
#      although -q and -c option seems to be fairly standard,
#      unfortunately 'ping' is not part of POSIX
# 
# OPEN:
#   - 'chk_udp' perhaps used a SOA request instead of a 'server status'
#     in case the tested DNS uses view
#
# BUGFIX:
#   - chk_udp: recvfrom reject datagram bigger than the requested size
#              instead of copying the beginning
#     using the NResolv::DNS::UDPSize instead of 1 byte
#

require 'framework'
require 'timeout'


module CheckNetworkAddress
    ##
    ## Check nameserver connectivity (ICMP, UDP, TCP)
    ## 
    ## - as request are directly made to the DNS (no CacheManager)
    ##   the tests won't be affected by the transport flags
    ## 
    class Connectivity < Test
	with_msgcat 'test/connectivity.%s'

	def initialize(*args)
	    super(*args)
	    @ping4_cmd = const('ping4')
	    @ping6_cmd = const('ping6')
	end

	#-- Checks --------------------------------------------------
	# DESC: Test TCP connectivity with DNS server
	def chk_tcp(ns, ip)
	    # The idea is to open a TCP connection
	    #  if no one is listening          => Errno::ECONNREFUSED
	    #  if there is a firewal (timeout) => Errno::EINVAL
	    #                                     Errno::ECONNRESET
	    # The total timeout has been forced to 8 seconds
	    #  as with the chk_udp test
	    msg		= NResolv::DNS::Message::Query::new
	    msg.question.add(@domain.name, NResolv::DNS::Resource::IN::ANY)
	    rawmsg	= msg.to_wire
	    sock	= nil
	    begin
		timeout(8) {
		    sock	= TCPSocket::new(ip.to_s, NResolv::DNS::Port)
		    sock.write([rawmsg.length].pack('n'))
		    sock.write(rawmsg)
		    lenhdr	= sock.read(2)
		    return false if lenhdr.nil? || lenhdr.size != 2
		    len		= lenhdr.unpack('n')[0]
		    reply	= sock.read(len)
		}
		true
	    rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EINVAL,
		    TimeoutError
		false
	    ensure
		sock.close unless sock.nil?
	    end
	end

	# DESC: Test UDP connectivity with DNS server
	def chk_udp(ns, ip)
	    # The idea is to send 25 'query' concerning the domain in 5 seconds
	    #  if we received one answer within 8 seconds (dont care about 
	    #  its content, we consider it sucessful)
	    msg		= NResolv::DNS::Message::Query::new
	    msg.question.add(@domain.name, NResolv::DNS::Resource::IN::ANY)
	    rawmsg	= msg.to_wire
	    sock	= nil
	    thr		= nil
	    begin
		sock = UDPSocket::new(ip.protocol)
		sock.connect(ip.to_s, NResolv::DNS::Port)
		thr = Thread::new {
		    (1..25).each { sock.write(rawmsg) ; sleep(0.2) }
		}
		timeout(8) { sock.recv(NResolv::DNS::UDPSize) }
		true
	    rescue Errno::ECONNREFUSED, TimeoutError
		false
	    ensure
		thr.kill   unless thr.nil?
		sock.close unless sock.nil?
	    end
	end

	# DESC: Test if host is alive (watch for firewall)
	def chk_icmp(ns, ip)
	    # Build ping command
	    ping_tmpl = case ip
			when Address::IPv4 then @ping4_cmd
			when Address::IPv6 then @ping6_cmd
			else raise 'INTERNAL: Unknown address format'
			end
	    ping_cmd = ping_tmpl % [ ip.to_s ]

	    # Do ping
	    dbgmsg(ns, ip) { "Executing: #{ping_cmd}" }
	    system(ping_cmd)
	end
    end
end