Passed
Branch master (8940db)
by Sam
02:38
created

HIP::toText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
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
     * @return HIP
163
     *
164
     * @throws ParseException
165
     */
166 1
    public static function fromText(string $text): RdataInterface
167
    {
168 1
        $rdata = explode(Tokens::SPACE, $text);
169 1
        $hip = new self();
170 1
        $hip->setPublicKeyAlgorithm((int) array_shift($rdata));
171
172 1
        if (false === $hostIdentityTag = @hex2bin((string) array_shift($rdata))) {
173
            throw new ParseException(sprintf('Unable to parse host identity tag of rdata string "%s".', $text));
174
        }
175 1
        $hip->setHostIdentityTag($hostIdentityTag);
176
177 1
        if (false === $publicKey = base64_decode((string) array_shift($rdata))) {
178
            throw new ParseException(sprintf('Unable to parse public key of rdata string "%s".', $text));
179
        }
180 1
        $hip->setPublicKey($publicKey);
181 1
        array_map([$hip, 'addRendezvousServer'], $rdata);
182
183 1
        return $hip;
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     *
189
     * @return HIP
190
     */
191 1
    public static function fromWire(string $rdata): RdataInterface
192
    {
193 1
        $hip = new self();
194 1
        $offset = 0;
195 1
        $rdLen = strlen($rdata);
196 1
        $integers = unpack('C<hitLen>/C<algorithm>/n<pkLen>', $rdata, $offset);
197 1
        $offset += 4;
198 1
        $hitLen = (int) $integers['<hitLen>'];
199 1
        $pkLen = (int) $integers['<pkLen>'];
200
201 1
        $hip->setPublicKeyAlgorithm((int) $integers['<algorithm>']);
202
203 1
        $hip->setHostIdentityTag(substr($rdata, $offset, $hitLen));
204 1
        $offset += $hitLen;
205
206 1
        $hip->setPublicKey(substr($rdata, $offset, $pkLen));
207 1
        $offset += $pkLen;
208
209 1
        while ($offset < $rdLen) {
210 1
            $hip->addRendezvousServer(self::decodeName($rdata, $offset));
211
        }
212
213 1
        return $hip;
214
    }
215
}
216