1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Clients\ICMP; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\CallbackWrapper; |
5
|
|
|
use PHPDaemon\Core\Daemon; |
6
|
|
|
use PHPDaemon\Network\ClientConnection; |
7
|
|
|
use PHPDaemon\Utils\Binary; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @package Applications |
11
|
|
|
* @subpackage ICMPClient |
12
|
|
|
* @author Vasily Zorin <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Connection extends ClientConnection |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var integer Packet sequence |
18
|
|
|
*/ |
19
|
|
|
protected $seq = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array @TODO DESCR |
23
|
|
|
*/ |
24
|
|
|
protected static $unreachableCodes = [ |
25
|
|
|
0x0 => 'netUnreachable', |
26
|
|
|
0x1 => 'hostUnreachable', |
27
|
|
|
0x2 => 'protocolUnreachable', |
28
|
|
|
0x3 => 'portUnreachable', |
29
|
|
|
0x4 => 'fragmentationNeeded', |
30
|
|
|
0x5 => 'sourceRouteFailed', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var boolean Enable bevConnect? |
35
|
|
|
*/ |
36
|
|
|
protected $bevConnectEnabled = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Send echo-request |
40
|
|
|
* @param callable $cb Callback |
41
|
|
|
* @param string $data Data |
42
|
|
|
* @callback $cb ( ) |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function sendEcho($cb, $data = 'phpdaemon') |
46
|
|
|
{ |
47
|
|
|
++$this->seq; |
48
|
|
|
if (mb_orig_strlen($data) % 2 !== 0) { |
49
|
|
|
$data .= "\x00"; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$packet = pack( |
53
|
|
|
'ccnnn', |
54
|
|
|
8, // type (c) |
55
|
|
|
0, // code (c) |
56
|
|
|
0, // checksum (n) |
57
|
|
|
Daemon::$process->getPid(), // pid (n) |
58
|
|
|
$this->seq // seq (n) |
59
|
|
|
) . $data; |
60
|
|
|
|
61
|
|
|
$packet = substr_replace($packet, self::checksum($packet), 2, 2); |
62
|
|
|
$this->write($packet); |
63
|
|
|
$this->onResponse->push([$cb, microtime(true)]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Build checksum |
68
|
|
|
* @param string $data Source |
69
|
|
|
* @return string Checksum |
70
|
|
|
*/ |
71
|
|
|
protected static function checksum($data) |
72
|
|
|
{ |
73
|
|
|
$bit = unpack('n*', $data); |
74
|
|
|
$sum = array_sum($bit); |
75
|
|
|
if (mb_orig_strlen($data) % 2) { |
76
|
|
|
$temp = unpack('C*', $data[mb_orig_strlen($data) - 1]); |
77
|
|
|
$sum += $temp[1]; |
78
|
|
|
} |
79
|
|
|
$sum = ($sum >> 16) + ($sum & 0xffff); |
80
|
|
|
$sum += ($sum >> 16); |
81
|
|
|
return pack('n*', ~$sum); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Called when new data received |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function onRead() |
89
|
|
|
{ |
90
|
|
|
$packet = $this->read(1024); |
91
|
|
|
$orig = $packet; |
92
|
|
|
$type = Binary::getByte($packet); |
93
|
|
|
$code = Binary::getByte($packet); |
94
|
|
|
$checksum = Binary::getStrWord($packet); |
95
|
|
|
$id = Binary::getWord($packet); |
|
|
|
|
96
|
|
|
$seq = Binary::getWord($packet); |
|
|
|
|
97
|
|
|
if ($checksum !== self::checksum(substr_replace($orig, "\x00\x00", 2, 2))) { |
98
|
|
|
$status = 'badChecksum'; |
99
|
|
|
} elseif ($type === 0x03) { |
100
|
|
|
$status = isset(static::$unreachableCodes[$code]) ? static::$unreachableCodes[$code] : 'unk' . $code . 'unreachable'; |
101
|
|
|
} else { |
102
|
|
|
$status = 'unknownType0x' . dechex($type); |
103
|
|
|
} |
104
|
|
|
while (!$this->onResponse->isEmpty()) { |
105
|
|
|
$el = $this->onResponse->shift(); |
106
|
|
|
if ($el instanceof CallbackWrapper) { |
107
|
|
|
$el = $el->unwrap(); |
108
|
|
|
} |
109
|
|
|
list($cb, $st) = $el; |
110
|
|
|
$cb(microtime(true) - $st, $status); |
111
|
|
|
} |
112
|
|
|
$this->finish(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.