Message
public struct Message
extension Message: CustomDebugStringConvertible
All communications inside of the domain protocol are carried in a single format called a message. The top level format of message is divided into 5 sections (some of which are empty in certain cases) shown below:
+---------------------+
| Header |
+---------------------+
| Question | the question for the name server
+---------------------+
| Answer | RRs answering the question
+---------------------+
| Authority | RRs pointing toward an authority
+---------------------+
| Additional | RRs holding additional information
+---------------------+
The header section is always present. The header includes fields that specify which of the remaining sections are present, and also specify whether the message is a query or a response, a standard query or some other opcode, etc.
The names of the sections after the header are derived from their use in standard queries. The question section contains fields that describe a question to a name server. These fields are a query type (QTYPE), a query class (QCLASS), and a query domain name (QNAME). The last three sections have the same format: a possibly empty list of concatenated resource records (RRs). The answer section contains RRs that answer the question; the authority section contains RRs that point toward an authoritative name server; the additional records section contains RRs which relate to the query, but are not strictly answers for the question.
-
A 16 bit identifier assigned by the program that generates any kind of query. This identifier is copied the corresponding reply and can be used by the requester to match up replies to outstanding queries.
Declaration
Swift
public var id: UInt16
-
A one bit field that specifies whether this message is a query (0), or a response (1).
Declaration
Swift
public var type: MessageType
-
A four bit field that specifies kind of query in this message. This value is set by the originator of a query and copied into the response. The values are:
Declaration
Swift
public var operationCode: OperationCode
-
Authoritative Answer - this bit is valid in responses, and specifies that the responding name server is an authority for the domain name in question section.
Note that the contents of the answer section may have multiple owner names because of aliases. The AA bit corresponds to the name which matches the query name, or the first owner name in the answer section.
Declaration
Swift
public var authoritativeAnswer: Bool
-
TrunCation - specifies that this message was truncated due to length greater than that permitted on the transmission channel.
Declaration
Swift
public var truncation: Bool
-
Recursion Desired - this bit may be set in a query and is copied into the response. If RD is set, it directs the name server to pursue the query recursively. Recursive query support is optional.
Declaration
Swift
public var recursionDesired: Bool
-
Recursion Available - this be is set or cleared in a response, and denotes whether recursive query support is available in the name server.
Declaration
Swift
public var recursionAvailable: Bool
-
Response code - this 4 bit field is set as part of responses.
Declaration
Swift
public var returnCode: ReturnCode
-
Question section.
Declaration
Swift
public var questions: [Question]
-
Answer section.
Declaration
Swift
public var answers: [ResourceRecord]
-
Authority section.
Declaration
Swift
public var authorities: [ResourceRecord]
-
Additional records section.
Declaration
Swift
public var additional: [ResourceRecord]
-
init(id:
type: operationCode: authoritativeAnswer: truncation: recursionDesired: recursionAvailable: returnCode: questions: answers: authorities: additional: ) Create a
Message
instance.To create a query:
let query = Message( id: UInt16(extendingOrTruncating: arc4random()), type: .query, questions: [ Question(name: "apple.com", type: .host) ])
To create a response for this query:
let response = Message( id: query.id, type: .response, returnCode: .noError, questions: query.questions, answers: [ HostRecord<IPv4>(name: "apple.com", ttl: 3600, ip: IPv4("17.172.224.47")!) ])
Declaration
Swift
public init( id: UInt16 = 0, type: MessageType, operationCode: OperationCode = .query, authoritativeAnswer: Bool = true, truncation: Bool = false, recursionDesired: Bool = false, recursionAvailable: Bool = false, returnCode: ReturnCode = .noError, questions: [Question] = [], answers: [ResourceRecord] = [], authorities: [ResourceRecord] = [], additional: [ResourceRecord] = [] )
Parameters
id
type
operationCode
authoritativeAnswer
truncation
recursionDesired
recursionAvailable
returnCode
questions
answers
authorities
additional
-
Serialize a
Message
for sending over TCP.The DNS TCP format prepends the message size before the actual message.
Throws
Declaration
Swift
public func serializeTCP() throws -> Data
Return Value
Data
to be send over TCP. -
Serialize a
Message
for sending over UDP.Throws
Declaration
Swift
public func serialize() throws -> Data
Return Value
Data
to be send over UDP. -
Deserializes a
Message
from a TCP stream.The DNS TCP format prepends the message size before the actual message.
Throws
Declaration
Swift
public init(deserializeTCP bytes: Data) throws
Parameters
bytes
the received bytes.
-
Deserializes a
Message
from a UDP stream.Throws
Declaration
Swift
public init(deserialize bytes: Data) throws
Parameters
bytes
the bytes to deserialize.
-
A textual representation of this instance, suitable for debugging.
Declaration
Swift
public var debugDescription: String { get }