aboutsummaryrefslogtreecommitdiff
path: root/p2p/base/mdns_message.cc
blob: 1aa996c4a89e228dafdaeb9f9a80bd75a23f8843 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 *  Copyright 2018 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "p2p/base/mdns_message.h"

#include "rtc_base/logging.h"
#include "rtc_base/net_helpers.h"
#include "rtc_base/string_encode.h"

namespace webrtc {

namespace {
// RFC 1035, Section 4.1.1.
//
// QR bit.
constexpr uint16_t kMdnsFlagMaskQueryOrResponse = 0x8000;
// AA bit.
constexpr uint16_t kMdnsFlagMaskAuthoritative = 0x0400;
// RFC 1035, Section 4.1.2, QCLASS and RFC 6762, Section 18.12, repurposing of
// top bit of QCLASS as the unicast response bit.
constexpr uint16_t kMdnsQClassMaskUnicastResponse = 0x8000;
constexpr size_t kMdnsHeaderSizeBytes = 12;

bool ReadDomainName(MessageBufferReader* buf, std::string* name) {
  size_t name_start_pos = buf->CurrentOffset();
  uint8_t label_length;
  if (!buf->ReadUInt8(&label_length)) {
    return false;
  }
  // RFC 1035, Section 4.1.4.
  //
  // If the first two bits of the length octet are ones, the name is compressed
  // and the rest six bits with the next octet denotes its position in the
  // message by the offset from the start of the message.
  auto is_pointer = [](uint8_t octet) {
    return (octet & 0x80) && (octet & 0x40);
  };
  while (label_length && !is_pointer(label_length)) {
    // RFC 1035, Section 2.3.1, labels are restricted to 63 octets or less.
    if (label_length > 63) {
      return false;
    }
    std::string label;
    if (!buf->ReadString(&label, label_length)) {
      return false;
    }
    (*name) += label + ".";
    if (!buf->ReadUInt8(&label_length)) {
      return false;
    }
  }
  if (is_pointer(label_length)) {
    uint8_t next_octet;
    if (!buf->ReadUInt8(&next_octet)) {
      return false;
    }
    size_t pos_jump_to = ((label_length & 0x3f) << 8) | next_octet;
    // A legitimate pointer only refers to a prior occurrence of the same name,
    // and we should only move strictly backward to a prior name field after the
    // header.
    if (pos_jump_to >= name_start_pos || pos_jump_to < kMdnsHeaderSizeBytes) {
      return false;
    }
    MessageBufferReader new_buf(buf->MessageData(), buf->MessageLength());
    if (!new_buf.Consume(pos_jump_to)) {
      return false;
    }
    return ReadDomainName(&new_buf, name);
  }
  return true;
}

void WriteDomainName(rtc::ByteBufferWriter* buf, const std::string& name) {
  std::vector<std::string> labels;
  rtc::tokenize(name, '.', &labels);
  for (const auto& label : labels) {
    buf->WriteUInt8(label.length());
    buf->WriteString(label);
  }
  buf->WriteUInt8(0);
}

}  // namespace

void MdnsHeader::SetQueryOrResponse(bool is_query) {
  if (is_query) {
    flags &= ~kMdnsFlagMaskQueryOrResponse;
  } else {
    flags |= kMdnsFlagMaskQueryOrResponse;
  }
}

void MdnsHeader::SetAuthoritative(bool is_authoritative) {
  if (is_authoritative) {
    flags |= kMdnsFlagMaskAuthoritative;
  } else {
    flags &= ~kMdnsFlagMaskAuthoritative;
  }
}

bool MdnsHeader::IsAuthoritative() const {
  return flags & kMdnsFlagMaskAuthoritative;
}

bool MdnsHeader::Read(MessageBufferReader* buf) {
  if (!buf->ReadUInt16(&id) || !buf->ReadUInt16(&flags) ||
      !buf->ReadUInt16(&qdcount) || !buf->ReadUInt16(&ancount) ||
      !buf->ReadUInt16(&nscount) || !buf->ReadUInt16(&arcount)) {
    RTC_LOG(LS_ERROR) << "Invalid mDNS header.";
    return false;
  }
  return true;
}

void MdnsHeader::Write(rtc::ByteBufferWriter* buf) const {
  buf->WriteUInt16(id);
  buf->WriteUInt16(flags);
  buf->WriteUInt16(qdcount);
  buf->WriteUInt16(ancount);
  buf->WriteUInt16(nscount);
  buf->WriteUInt16(arcount);
}

bool MdnsHeader::IsQuery() const {
  return !(flags & kMdnsFlagMaskQueryOrResponse);
}

MdnsSectionEntry::MdnsSectionEntry() = default;
MdnsSectionEntry::~MdnsSectionEntry() = default;
MdnsSectionEntry::MdnsSectionEntry(const MdnsSectionEntry& other) = default;

void MdnsSectionEntry::SetType(SectionEntryType type) {
  switch (type) {
    case SectionEntryType::kA:
      type_ = 1;
      return;
    case SectionEntryType::kAAAA:
      type_ = 28;
      return;
    default:
      RTC_NOTREACHED();
  }
}

SectionEntryType MdnsSectionEntry::GetType() const {
  switch (type_) {
    case 1:
      return SectionEntryType::kA;
    case 28:
      return SectionEntryType::kAAAA;
    default:
      return SectionEntryType::kUnsupported;
  }
}

void MdnsSectionEntry::SetClass(SectionEntryClass cls) {
  switch (cls) {
    case SectionEntryClass::kIN:
      class_ = 1;
      return;
    default:
      RTC_NOTREACHED();
  }
}

SectionEntryClass MdnsSectionEntry::GetClass() const {
  switch (class_) {
    case 1:
      return SectionEntryClass::kIN;
    default:
      return SectionEntryClass::kUnsupported;
  }
}

MdnsQuestion::MdnsQuestion() = default;
MdnsQuestion::MdnsQuestion(const MdnsQuestion& other) = default;
MdnsQuestion::~MdnsQuestion() = default;

bool MdnsQuestion::Read(MessageBufferReader* buf) {
  if (!ReadDomainName(buf, &name_)) {
    RTC_LOG(LS_ERROR) << "Invalid name.";
    return false;
  }
  if (!buf->ReadUInt16(&type_) || !buf->ReadUInt16(&class_)) {
    RTC_LOG(LS_ERROR) << "Invalid type and class.";
    return false;
  }
  return true;
}

bool MdnsQuestion::Write(rtc::ByteBufferWriter* buf) const {
  WriteDomainName(buf, name_);
  buf->WriteUInt16(type_);
  buf->WriteUInt16(class_);
  return true;
}

void MdnsQuestion::SetUnicastResponse(bool should_unicast) {
  if (should_unicast) {
    class_ |= kMdnsQClassMaskUnicastResponse;
  } else {
    class_ &= ~kMdnsQClassMaskUnicastResponse;
  }
}

bool MdnsQuestion::ShouldUnicastResponse() const {
  return class_ & kMdnsQClassMaskUnicastResponse;
}

MdnsResourceRecord::MdnsResourceRecord() = default;
MdnsResourceRecord::MdnsResourceRecord(const MdnsResourceRecord& other) =
    default;
MdnsResourceRecord::~MdnsResourceRecord() = default;

bool MdnsResourceRecord::Read(MessageBufferReader* buf) {
  if (!ReadDomainName(buf, &name_)) {
    return false;
  }
  if (!buf->ReadUInt16(&type_) || !buf->ReadUInt16(&class_) ||
      !buf->ReadUInt32(&ttl_seconds_) || !buf->ReadUInt16(&rdlength_)) {
    return false;
  }

  switch (GetType()) {
    case SectionEntryType::kA:
      return ReadARData(buf);
    case SectionEntryType::kAAAA:
      return ReadQuadARData(buf);
    case SectionEntryType::kUnsupported:
      return false;
    default:
      RTC_NOTREACHED();
  }
  return false;
}
bool MdnsResourceRecord::ReadARData(MessageBufferReader* buf) {
  // A RDATA contains a 32-bit IPv4 address.
  return buf->ReadString(&rdata_, 4);
}

bool MdnsResourceRecord::ReadQuadARData(MessageBufferReader* buf) {
  // AAAA RDATA contains a 128-bit IPv6 address.
  return buf->ReadString(&rdata_, 16);
}

bool MdnsResourceRecord::Write(rtc::ByteBufferWriter* buf) const {
  WriteDomainName(buf, name_);
  buf->WriteUInt16(type_);
  buf->WriteUInt16(class_);
  buf->WriteUInt32(ttl_seconds_);
  buf->WriteUInt16(rdlength_);
  switch (GetType()) {
    case SectionEntryType::kA:
      WriteARData(buf);
      return true;
    case SectionEntryType::kAAAA:
      WriteQuadARData(buf);
      return true;
    case SectionEntryType::kUnsupported:
      return false;
    default:
      RTC_NOTREACHED();
  }
  return true;
}

void MdnsResourceRecord::WriteARData(rtc::ByteBufferWriter* buf) const {
  buf->WriteString(rdata_);
}

void MdnsResourceRecord::WriteQuadARData(rtc::ByteBufferWriter* buf) const {
  buf->WriteString(rdata_);
}

bool MdnsResourceRecord::SetIPAddressInRecordData(
    const rtc::IPAddress& address) {
  int af = address.family();
  if (af != AF_INET && af != AF_INET6) {
    return false;
  }
  char out[16] = {0};
  if (!rtc::inet_pton(af, address.ToString().c_str(), out)) {
    return false;
  }
  rdlength_ = (af == AF_INET) ? 4 : 16;
  rdata_ = std::string(out, rdlength_);
  return true;
}

bool MdnsResourceRecord::GetIPAddressFromRecordData(
    rtc::IPAddress* address) const {
  if (GetType() != SectionEntryType::kA &&
      GetType() != SectionEntryType::kAAAA) {
    return false;
  }
  if (rdata_.size() != 4 && rdata_.size() != 16) {
    return false;
  }
  char out[INET6_ADDRSTRLEN] = {0};
  int af = (GetType() == SectionEntryType::kA) ? AF_INET : AF_INET6;
  if (!rtc::inet_ntop(af, rdata_.data(), out, sizeof(out))) {
    return false;
  }
  return rtc::IPFromString(std::string(out), address);
}

MdnsMessage::MdnsMessage() = default;
MdnsMessage::~MdnsMessage() = default;

bool MdnsMessage::Read(MessageBufferReader* buf) {
  RTC_DCHECK_EQ(0u, buf->CurrentOffset());
  if (!header_.Read(buf)) {
    return false;
  }

  auto read_question = [&buf](std::vector<MdnsQuestion>* section,
                              uint16_t count) {
    section->resize(count);
    for (auto& question : (*section)) {
      if (!question.Read(buf)) {
        return false;
      }
    }
    return true;
  };
  auto read_rr = [&buf](std::vector<MdnsResourceRecord>* section,
                        uint16_t count) {
    section->resize(count);
    for (auto& rr : (*section)) {
      if (!rr.Read(buf)) {
        return false;
      }
    }
    return true;
  };

  if (!read_question(&question_section_, header_.qdcount) ||
      !read_rr(&answer_section_, header_.ancount) ||
      !read_rr(&authority_section_, header_.nscount) ||
      !read_rr(&additional_section_, header_.arcount)) {
    return false;
  }
  return true;
}

bool MdnsMessage::Write(rtc::ByteBufferWriter* buf) const {
  header_.Write(buf);

  auto write_rr = [&buf](const std::vector<MdnsResourceRecord>& section) {
    for (const auto& rr : section) {
      if (!rr.Write(buf)) {
        return false;
      }
    }
    return true;
  };

  for (const auto& question : question_section_) {
    if (!question.Write(buf)) {
      return false;
    }
  }
  if (!write_rr(answer_section_) || !write_rr(authority_section_) ||
      !write_rr(additional_section_)) {
    return false;
  }

  return true;
}

bool MdnsMessage::ShouldUnicastResponse() const {
  bool should_unicast = false;
  for (const auto& question : question_section_) {
    should_unicast |= question.ShouldUnicastResponse();
  }
  return should_unicast;
}

void MdnsMessage::AddQuestion(const MdnsQuestion& question) {
  question_section_.push_back(question);
  header_.qdcount = question_section_.size();
}

void MdnsMessage::AddAnswerRecord(const MdnsResourceRecord& answer) {
  answer_section_.push_back(answer);
  header_.ancount = answer_section_.size();
}

}  // namespace webrtc