Passed
Branch dev-v4 (e16a63)
by Sam
19:58
created

HIP::fromText()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

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