Skip to content

Commit 9d62568

Browse files
committed
Bugfix: class.Scan.php. Check for correct IPv4/IPv6 ping path. #60
Refactor: Sync functions/classes/* with phpipam master.
1 parent 5a776dc commit 9d62568

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

functions/classes/class.PDO.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function connect() {
153153
$this->pdo = new \PDO($dsn, $this->username, $this->password);
154154
}
155155

156-
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
156+
$this->setErrMode(\PDO::ERRMODE_EXCEPTION);
157157

158158
} catch (\PDOException $e) {
159159
throw new Exception ("Could not connect to database! ".$e->getMessage());
@@ -162,6 +162,14 @@ public function connect() {
162162
@$this->pdo->query('SET NAMES \'' . $this->charset . '\';');
163163
}
164164

165+
/**
166+
* Set PDO error mode
167+
* @param mixed $mode
168+
*/
169+
public function setErrMode($mode = \PDO::ERRMODE_EXCEPTION) {
170+
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, $mode);
171+
}
172+
165173
/**
166174
* makeDsn function.
167175
*

functions/classes/class.Scan.php

+29-18
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,23 @@ public function __construct (Database_PDO $database, $settings = null) {
114114
$this->Database = $database;
115115
# initialize Result
116116
$this->Result = new Result ();
117+
117118
# fetch settings
118-
$this->settings = is_null($this->settings) ? $this->get_settings() : (object) $this->settings;
119+
$settings = is_null($this->settings) ? $this->get_settings() : (object) $this->settings;
120+
$config = Config::ValueOf('config');
121+
122+
$this->ping_type = $config['method'];
123+
$this->ping_path = ($this->ping_type == "ping") ? $config['pingpath'] : '';
124+
$this->fping_path = ($this->ping_type == "fping") ? $config['pingpath'] : '';
125+
119126
# set type
120-
$this->reset_scan_method ($this->settings->scanPingType);
127+
$this->reset_scan_method ($this->ping_type);
121128
# set OS type
122129
$this->set_os_type ();
123130
# set php exec
124131
$this->set_php_exec ();
125132
# Log object
126-
$this->Log = new Logging ($this->Database, $this->settings);
133+
$this->Log = new Logging ($this->Database, $settings);
127134

128135
if ($errmsg = php_feature_missing(null, ['exec']))
129136
$this->Result->show("danger", $errmsg, true);
@@ -268,17 +275,17 @@ public function ping_address ($address, $count=1, $timeout = 1) {
268275
* @return void
269276
*/
270277
protected function ping_address_method_ping ($address) {
271-
# verify ping path
272-
$this->ping_verify_path ($this->settings->scanPingPath);
273-
274278
# if ipv6 append 6
275-
if ($this->identify_address ($address)=="IPv6") { $this->settings->scanPingPath = $this->settings->scanPingPath."6"; }
279+
$ping_path = ($this->identify_address ($address)=="IPv6") ? $this->ping_path."6" : $this->ping_path;
280+
281+
# verify ping path
282+
$this->ping_verify_path ($ping_path);
276283

277284
# set ping command based on OS type
278-
if ($this->os_type == "FreeBSD") { $cmd = $this->settings->scanPingPath." -c $this->icmp_count -W ".($this->icmp_timeout*1000)." $address 1>/dev/null 2>&1"; }
279-
elseif($this->os_type == "Linux") { $cmd = $this->settings->scanPingPath." -c $this->icmp_count -W $this->icmp_timeout $address 1>/dev/null 2>&1"; }
280-
elseif($this->os_type == "Windows") { $cmd = $this->settings->scanPingPath." -n $this->icmp_count -w ".($this->icmp_timeout*1000)." $address"; }
281-
else { $cmd = $this->settings->scanPingPath." -c $this->icmp_count -n $address 1>/dev/null 2>&1"; }
285+
if ($this->os_type == "FreeBSD") { $cmd = $ping_path." -c $this->icmp_count -W ".($this->icmp_timeout*1000)." $address 1>/dev/null 2>&1"; }
286+
elseif($this->os_type == "Linux") { $cmd = $ping_path." -c $this->icmp_count -W $this->icmp_timeout $address 1>/dev/null 2>&1"; }
287+
elseif($this->os_type == "Windows") { $cmd = $ping_path." -n $this->icmp_count -w ".($this->icmp_timeout*1000)." $address"; }
288+
else { $cmd = $ping_path." -c $this->icmp_count -n $address 1>/dev/null 2>&1"; }
282289

283290
# for IPv6 remove wait
284291
if ($this->identify_address ($address)=="IPv6") {
@@ -374,14 +381,14 @@ protected function ping_address_method_pear ($address) {
374381
* @return void
375382
*/
376383
public function ping_address_method_fping ($address) {
377-
# verify ping path
378-
$this->ping_verify_path ($this->settings->scanFPingPath);
379-
380384
# if ipv6 append 6
381-
if ($this->identify_address ($address)=="IPv6") { $this->settings->scanFPingPath = $this->settings->scanFPingPath."6"; }
385+
$fping_path = ($this->identify_address ($address)=="IPv6") ? $this->fping_path."6" : $this->fping_path;
386+
387+
# verify ping path
388+
$this->ping_verify_path ($fping_path);
382389

383390
# set command
384-
$cmd = $this->settings->scanFPingPath." -c $this->icmp_count -t ".($this->icmp_timeout*1000)." $address";
391+
$cmd = $fping_path." -c $this->icmp_count -t ".($this->icmp_timeout*1000)." $address";
385392
# execute command, return $retval
386393
exec($cmd, $output, $retval);
387394

@@ -427,10 +434,14 @@ private function save_fping_rtt ($line) {
427434
* @return void
428435
*/
429436
public function ping_address_method_fping_subnet ($subnet_cidr, $return_result = false) {
437+
# if ipv6 append 6
438+
$fping_path = ($this->identify_address ($address)=="IPv6") ? $this->fping_path."6" : $this->fping_path;
439+
430440
# verify ping path
431-
$this->ping_verify_path ($this->settings->scanFPingPath);
441+
$this->ping_verify_path ($fping_path);
442+
432443
# set command
433-
$cmd = $this->settings->scanFPingPath." -c $this->icmp_count -t ".($this->icmp_timeout*1000)." -Ag $subnet_cidr";
444+
$cmd = $fping_path." -c $this->icmp_count -t ".($this->icmp_timeout*1000)." -Ag $subnet_cidr";
434445
# execute command, return $retval
435446
exec($cmd, $output, $retval);
436447

functions/classes/class.Thread.php

100755100644
File mode changed.

0 commit comments

Comments
 (0)