Connection   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendEcho() 0 20 2
A checksum() 0 12 2
B onRead() 0 26 6
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);
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
96
        $seq = Binary::getWord($packet);
0 ignored issues
show
Unused Code introduced by
$seq is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
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