Debian Codegraph @

vendor/react/dns/src/Model/Message.php

raw
1 <?php
2
3 namespace React\Dns\Model;
4
5 use React\Dns\Query\Query;
6
7 /**
8 * This class represents an outgoing query message or an incoming response message
9 *
10 * @link https://tools.ietf.org/html/rfc1035#section-4.1.1
11 */
12 final class Message
13 {
14 const TYPE_A = 1;
15 const TYPE_NS = 2;
16 const TYPE_CNAME = 5;
17 const TYPE_SOA = 6;
18 const TYPE_PTR = 12;
19 const TYPE_MX = 15;
20 const TYPE_TXT = 16;
21 const TYPE_AAAA = 28;
22 const TYPE_SRV = 33;
23 const TYPE_SSHFP = 44;
24
25 /**
26 * pseudo-type for EDNS0
27 *
28 * These are included in the additional section and usually not in answer section.
29 * Defined in [RFC 6891](https://tools.ietf.org/html/rfc6891) (or older
30 * [RFC 2671](https://tools.ietf.org/html/rfc2671)).
31 *
32 * The OPT record uses the "class" field to store the maximum size.
33 *
34 * The OPT record uses the "ttl" field to store additional flags.
35 */
36 const TYPE_OPT = 41;
37
38 /**
39 * Sender Policy Framework (SPF) had a dedicated SPF type which has been
40 * deprecated in favor of reusing the existing TXT type.
41 *
42 * @deprecated https://datatracker.ietf.org/doc/html/rfc7208#section-3.1
43 * @see self::TYPE_TXT
44 */
45 const TYPE_SPF = 99;
46
47 const TYPE_ANY = 255;
48 const TYPE_CAA = 257;
49
50 const CLASS_IN = 1;
51
52 const OPCODE_QUERY = 0;
53 const OPCODE_IQUERY = 1; // inverse query
54 const OPCODE_STATUS = 2;
55
56 const RCODE_OK = 0;
57 const RCODE_FORMAT_ERROR = 1;
58 const RCODE_SERVER_FAILURE = 2;
59 const RCODE_NAME_ERROR = 3;
60 const RCODE_NOT_IMPLEMENTED = 4;
61 const RCODE_REFUSED = 5;
62
63 /**
64 * The edns-tcp-keepalive EDNS0 Option
65 *
66 * Option value contains a `?float` with timeout in seconds (in 0.1s steps)
67 * for DNS response or `null` for DNS query.
68 *
69 * @link https://tools.ietf.org/html/rfc7828
70 */
71 const OPT_TCP_KEEPALIVE = 11;
72
73 /**
74 * The EDNS(0) Padding Option
75 *
76 * Option value contains a `string` with binary data (usually variable
77 * number of null bytes)
78 *
79 * @link https://tools.ietf.org/html/rfc7830
80 */
81 const OPT_PADDING = 12;
82
83 /**
84 * Creates a new request message for the given query
85 *
86 * @param Query $query
87 * @return self
88 */
89 public static function createRequestForQuery(Query $query)
90 {
91 $request = new Message();
92 $request->id = self::generateId();
93 $request->rd = true;
94 $request->questions[] = $query;
95
96 return $request;
97 }
98
99 /**
100 * Creates a new response message for the given query with the given answer records
101 *
102 * @param Query $query
103 * @param Record[] $answers
104 * @return self
105 */
106 public static function createResponseWithAnswersForQuery(Query $query, array $answers)
107 {
108 $response = new Message();
109 $response->id = self::generateId();
110 $response->qr = true;
111 $response->rd = true;
112
113 $response->questions[] = $query;
114
115 foreach ($answers as $record) {
116 $response->answers[] = $record;
117 }
118
119 return $response;
120 }
121
122 /**
123 * generates a random 16 bit message ID
124 *
125 * This uses a CSPRNG so that an outside attacker that is sending spoofed
126 * DNS response messages can not guess the message ID to avoid possible
127 * cache poisoning attacks.
128 *
129 * The `random_int()` function is only available on PHP 7+ or when
130 * https://github.com/paragonie/random_compat is installed. As such, using
131 * the latest supported PHP version is highly recommended. This currently
132 * falls back to a less secure random number generator on older PHP versions
133 * in the hope that this system is properly protected against outside
134 * attackers, for example by using one of the common local DNS proxy stubs.
135 *
136 * @return int
137 * @see self::getId()
138 * @codeCoverageIgnore
139 */
140 private static function generateId()
141 {
142 if (function_exists('random_int')) {
143 return random_int(0, 0xffff);
144 }
145 return mt_rand(0, 0xffff);
146 }
147
148 /**
149 * The 16 bit message ID
150 *
151 * The response message ID has to match the request message ID. This allows
152 * the receiver to verify this is the correct response message. An outside
153 * attacker may try to inject fake responses by "guessing" the message ID,
154 * so this should use a proper CSPRNG to avoid possible cache poisoning.
155 *
156 * @var int 16 bit message ID
157 * @see self::generateId()
158 */
159 public $id = 0;
160
161 /**
162 * @var bool Query/Response flag, query=false or response=true
163 */
164 public $qr = false;
165
166 /**
167 * @var int specifies the kind of query (4 bit), see self::OPCODE_* constants
168 * @see self::OPCODE_QUERY
169 */
170 public $opcode = self::OPCODE_QUERY;
171
172 /**
173 *
174 * @var bool Authoritative Answer
175 */
176 public $aa = false;
177
178 /**
179 * @var bool TrunCation
180 */
181 public $tc = false;
182
183 /**
184 * @var bool Recursion Desired
185 */
186 public $rd = false;
187
188 /**
189 * @var bool Recursion Available
190 */
191 public $ra = false;
192
193 /**
194 * @var int response code (4 bit), see self::RCODE_* constants
195 * @see self::RCODE_OK
196 */
197 public $rcode = Message::RCODE_OK;
198
199 /**
200 * An array of Query objects
201 *
202 * ```php
203 * $questions = array(
204 * new Query(
205 * 'reactphp.org',
206 * Message::TYPE_A,
207 * Message::CLASS_IN
208 * )
209 * );
210 * ```
211 *
212 * @var Query[]
213 */
214 public $questions = array();
215
216 /**
217 * @var Record[]
218 */
219 public $answers = array();
220
221 /**
222 * @var Record[]
223 */
224 public $authority = array();
225
226 /**
227 * @var Record[]
228 */
229 public $additional = array();
230 }