HIP::setHostIdentityTag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Rdata;
15
16
use Badcow\DNS\Message;
17
use Badcow\DNS\Parser\ParseException;
18
use Badcow\DNS\Parser\Tokens;
19
use Badcow\DNS\Validator;
20
21
/**
22
 * {@link https://tools.ietf.org/html/rfc8005}.
23
 */
24
class HIP implements RdataInterface
25
{
26 1
    use RdataTrait;
27
28
    const TYPE = 'HIP';
29
    const TYPE_CODE = 55;
30
31
    /**
32
     * @var int
33
     */
34
    private $publicKeyAlgorithm;
35
36
    /**
37
     * @var string
38
     */
39
    private $hostIdentityTag;
40
41
    /**
42
     * @var string
43
     */
44
    private $publicKey;
45
46
    /**
47
     * @var string[]
48
     */
49
    private $rendezvousServers = [];
50
51 1
    public function getPublicKeyAlgorithm(): int
52
    {
53 1
        return $this->publicKeyAlgorithm;
54
    }
55
56 4
    public function setPublicKeyAlgorithm(int $publicKeyAlgorithm): void
57
    {
58 4
        $this->publicKeyAlgorithm = $publicKeyAlgorithm;
59 4
    }
60
61 1
    public function getHostIdentityTag(): string
62
    {
63 1
        return $this->hostIdentityTag;
64
    }
65
66 4
    public function setHostIdentityTag(string $hostIdentityTag): void
67
    {
68 4
        $this->hostIdentityTag = $hostIdentityTag;
69 4
    }
70
71 1
    public function getPublicKey(): string
72
    {
73 1
        return $this->publicKey;
74
    }
75
76 4
    public function setPublicKey(string $publicKey): void
77
    {
78 4
        $this->publicKey = $publicKey;
79 4
    }
80
81 4
    public function addRendezvousServer(string $server): void
82
    {
83 4
        if (!Validator::fullyQualifiedDomainName($server)) {
84
            throw new \InvalidArgumentException('Rendezvous Server must be a fully-qualified domain name.');
85
        }
86
87 4
        $this->rendezvousServers[] = $server;
88 4
    }
89
90
    /**
91
     * @return string[]
92
     */
93 1
    public function getRendezvousServers(): array
94
    {
95 1
        return $this->rendezvousServers;
96
    }
97
98
    /**
99
     * Clear all rendezvous servers from the record.
100
     */
101
    public function clearRendezvousServer(): void
102
    {
103
        $this->rendezvousServers = [];
104
    }
105
106 1
    public function toText(): string
107
    {
108 1
        return sprintf('%d %s %s %s',
109 1
            $this->publicKeyAlgorithm,
110 1
            bin2hex($this->hostIdentityTag),
111 1
            base64_encode($this->publicKey),
112 1
            implode(Tokens::SPACE, $this->rendezvousServers)
113
        );
114
    }
115
116 1
    public function toWire(): string
117
    {
118 1
        $rdata = pack('CCn',
119 1
            strlen($this->hostIdentityTag),
120 1
            $this->publicKeyAlgorithm,
121 1
            strlen($this->publicKey)
122
        );
123
124 1
        $rdata .= $this->hostIdentityTag;
125 1
        $rdata .= $this->publicKey;
126 1
        foreach ($this->rendezvousServers as $server) {
127 1
            $rdata .= Message::encodeName($server);
128
        }
129
130 1
        return $rdata;
131
    }
132
133
    /**
134
     * @throws ParseException
135
     */
136 1
    public function fromText(string $text): void
137
    {
138 1
        $rdata = explode(Tokens::SPACE, $text);
139 1
        $this->setPublicKeyAlgorithm((int) array_shift($rdata));
140
141 1
        if (false === $hostIdentityTag = @hex2bin((string) array_shift($rdata))) {
142
            throw new ParseException(sprintf('Unable to parse host identity tag of rdata string "%s".', $text));
143
        }
144 1
        $this->setHostIdentityTag($hostIdentityTag);
145
146 1
        if (false === $publicKey = base64_decode((string) array_shift($rdata), true)) {
147
            throw new ParseException(sprintf('Unable to parse public key of rdata string "%s".', $text));
148
        }
149 1
        $this->setPublicKey($publicKey);
150 1
        array_map([$this, 'addRendezvousServer'], $rdata);
151 1
    }
152
153 1
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
154
    {
155 1
        $end = $offset + ($rdLength ?? strlen($rdata));
156 1
        if (false === $integers = unpack('C<hitLen>/C<algorithm>/n<pkLen>', $rdata, $offset)) {
157 1
            throw new DecodeException(static::TYPE, $rdata);
158 1
        }
159 1
        $offset += 4;
160
        $hitLen = (int) $integers['<hitLen>'];
161 1
        $pkLen = (int) $integers['<pkLen>'];
162
163 1
        $this->setPublicKeyAlgorithm((int) $integers['<algorithm>']);
164 1
165
        $this->setHostIdentityTag(substr($rdata, $offset, $hitLen));
166 1
        $offset += $hitLen;
167 1
168
        $this->setPublicKey(substr($rdata, $offset, $pkLen));
169 1
        $offset += $pkLen;
170 1
171
        while ($offset < $end) {
172 1
            $this->addRendezvousServer(Message::decodeName($rdata, $offset));
173
        }
174
    }
175
}
176