2 * Copyright (c) 2020 Apple Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * https://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __MDNS_MESSAGE_H__
18 #define __MDNS_MESSAGE_H__
20 #include "mdns_base.h"
22 #include <dispatch/dispatch.h>
25 #define MDNS_MESSAGE_SUBKIND_DECLARE(NAME) MDNS_DECL_SUBKIND(NAME ## _message, message)
28 MDNS_MESSAGE_SUBKIND_DECLARE(query
);
30 MDNS_ASSUME_NONNULL_BEGIN
32 #if OS_OBJECT_USE_OBJC
33 typedef mdns_message_t mdns_any_message_t
;
35 #if defined(__cplusplus)
36 typedef void * mdns_any_message_t
;
39 MDNS_UNION_MEMBER(message
);
40 MDNS_UNION_MEMBER(query_message
);
41 } mdns_any_message_t
__attribute__((__transparent_union__
));
46 * @typedef mdns_message_init_options_t
49 * Creates a DNS message object from a dispatch_data object.
51 * @constant mdns_message_init_option_null
52 * Represents no option.
54 * @constant mdns_message_init_option_disable_header_printing
55 * Disable message header printing in description.
57 OS_CLOSED_OPTIONS(mdns_message_init_options
, uint32_t,
58 mdns_message_init_option_null
= 0, // Null option.
59 mdns_message_init_option_disable_header_printing
= (1U << 0) // Disable message header printing in description.
66 * Creates a DNS message object from a dispatch_data object.
69 * A dispatch_data object containing a DNS message in wire format.
72 * Message initialization options.
75 * A new message object, or NULL if there was a lack of resources.
77 MDNS_RETURNS_RETAINED MDNS_WARN_RESULT
78 mdns_message_t _Nullable
79 mdns_message_create_with_dispatch_data(dispatch_data_t data
, mdns_message_init_options_t options
);
83 * Gets a message's data in wire format as a dispatch_data object.
89 mdns_message_get_dispatch_data(mdns_any_message_t message
);
93 * Returns a pointer to the first byte of a message's data in wire format.
98 const uint8_t * _Nullable
99 mdns_message_get_byte_ptr(mdns_any_message_t message
);
103 * Returns the length of a message's data in wire format.
109 mdns_message_get_length(mdns_any_message_t message
);
113 * Creates a DNS query message object.
116 * Message initialization options.
119 * A new query message object, or NULL if there was a lack of resources.
122 * A query message starts out in a mutable state. Functions such as mdns_query_message_set_qname(),
123 * mdns_query_message_set_qtype(), and mdns_query_message_set_qclass() can be used to modify the message.
124 * After all the desired modifications are made, the mdns_query_message_construct() function should be used
127 MDNS_RETURNS_RETAINED MDNS_WARN_RESULT
128 mdns_query_message_t _Nullable
129 mdns_query_message_create(mdns_message_init_options_t options
);
133 * Sets the QNAME of a query message's question.
139 * The QNAME in wire format.
142 * kNoErr if the QNAME was successfully set. Otherwise, an error code that indicates the error that was
146 * This function has no effect on a query message that has been constructed.
149 mdns_query_message_set_qname(mdns_query_message_t message
, const uint8_t *qname
);
153 * Sets the QTYPE of a query message's question.
162 * This function has no effect on a query message that has been constructed.
165 mdns_query_message_set_qtype(mdns_query_message_t message
, uint16_t qtype
);
169 * Sets the QCLASS of a query message's question.
178 * This function has no effect on a query message that has been constructed.
181 mdns_query_message_set_qclass(mdns_query_message_t message
, uint16_t qclass
);
185 * Sets a query message's ID.
194 * The default message ID is 0.
196 * This function has no effect on a query message that has been constructed.
199 mdns_query_message_set_message_id(mdns_query_message_t message
, uint16_t msg_id
);
203 * Sets or clears a query message's AD (authentic data) bit.
209 * If true, the AD bit is set. If false, the AD bit is cleared.
212 * The AD bit is clear by default.
214 * See <https://tools.ietf.org/html/rfc6840#section-5.7>.
216 * This function has no effect on a query message that has been constructed.
219 mdns_query_message_set_ad_bit(mdns_query_message_t message
, bool set
);
223 * Sets or clears a query message's CD (checking disabled) bit.
229 * If true, the CD bit is set. If false, the CD bit is cleared.
232 * The CD bit is clear by default.
234 * See <https://tools.ietf.org/html/rfc2535#section-6.1>.
236 * This function has no effect on a query message that has been constructed.
239 mdns_query_message_set_cd_bit(mdns_query_message_t message
, bool set
);
243 * Sets or clears a query message's DO (DNSSEC OK) bit in its EDNS0 header.
249 * If true, the DO bit is set. If false, the DO bit is cleared.
252 * The DO bit is clear by default.
254 * See <https://tools.ietf.org/html/rfc3225#section-3>.
256 * This function has no effect on a query message that has been constructed.
259 mdns_query_message_set_do_bit(mdns_query_message_t message
, bool set
);
263 * Specifies whether a query message should be constructed with EDNS0 padding.
269 * If true, EDNS0 padding will be used. If false, EDNS0 padding will not be used.
272 * By default, EDNS0 padding is not used.
274 * The padding strategy that will be utilized during construction is the Block-Length Padding strategy, as
275 * recommended by <https://tools.ietf.org/html/rfc8467#section-4.1>.
277 * This function has no effect on a query message that has been constructed.
280 mdns_query_message_use_edns0_padding(mdns_query_message_t message
, bool use
);
284 * Constructs a query message's data in wire format.
290 * kNoErr if the query message data was successfully constructed. Otherwise, an error code that indicates
291 * the error that was encountered during construction.
294 * After a successful call of this function, the return values of mdns_message_get_dispatch_data(),
295 * mdns_message_get_byte_ptr(), and mdns_message_get_length() will reflect the newly constructed message
298 * This function has no effect on a query message that has been constructed.
301 mdns_query_message_construct(mdns_query_message_t message
);
305 * Gets a query message's question's QNAME in wire format.
311 * The pointer returned by this function is guaranteed to be valid during the lifetime of the query message
315 mdns_query_message_get_qname(mdns_query_message_t message
);
319 * Gets a query message's question's QTYPE value.
325 mdns_query_message_get_qtype(mdns_query_message_t message
);
329 * Gets a query message's question's QCLASS value.
335 mdns_query_message_get_qclass(mdns_query_message_t message
);
339 * Gets a query message's ID.
345 mdns_query_message_get_message_id(mdns_query_message_t message
);
349 * Determines whether a query message's the DO bit is set.
355 mdns_query_message_do_bit_is_set(mdns_query_message_t message
);
359 MDNS_ASSUME_NONNULL_END
361 #endif // __MDNS_MESSAGE_H__