summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Protocol/Nrpe/Packet.php
blob: 54c8526c00cbb722bf6036a85f91c077c35ba0b7 (plain)
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
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Protocol\Nrpe;

class Packet
{
    const QUERY    = 0x01;
    const RESPONSE = 0x02;

    protected $version = 0x02;
    protected $type;
    protected $body;
    protected static $randomBytes;

    public function __construct($type, $body)
    {
        $this->type = $type;
        $this->body = $body;
        $this->regenerateRandomBytes();
    }

    // TODO: renew "from time to time" to allow long-running daemons
    protected function regenerateRandomBytes()
    {
        self::$randomBytes = '';
        for ($i = 0; $i < 4096; $i++) {
            self::$randomBytes .= pack('N', mt_rand());
        }
    }

    public static function createQuery($body)
    {
        $packet = new Packet(self::QUERY, $body);
        return $packet;
    }

    protected function getFillString($length)
    {
        $max = strlen(self::$randomBytes) - $length;
        return substr(self::$randomBytes, rand(0, $max), $length);
    }

    // TODO: WTF is SR? And 2324?
    public function getBinary()
    {
        $version  = pack('n', $this->version);
        $type     = pack('n', $this->type);
        $dummycrc = "\x00\x00\x00\x00";
        $result   = "\x00\x00";
        $result   = pack('n', 2324);
        $body     = $this->body
                  . "\x00"
                  . $this->getFillString(1023 - strlen($this->body))
                  . 'SR';

        $crc = pack(
            'N',
            crc32($version . $type . $dummycrc . $result . $body)
        );
        $bytes = $version . $type . $crc . $result . $body;
        return $bytes;
    }

    public function __toString()
    {
        return $this->body;
    }
}