1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/dns/record_parsed.h" 6 7 #include "base/logging.h" 8 #include "net/dns/dns_response.h" 9 #include "net/dns/record_rdata.h" 10 11 namespace net { 12 13 RecordParsed::RecordParsed(const std::string& name, uint16 type, uint16 klass, 14 uint32 ttl, scoped_ptr<const RecordRdata> rdata, 15 base::Time time_created) 16 : name_(name), type_(type), klass_(klass), ttl_(ttl), rdata_(rdata.Pass()), 17 time_created_(time_created) { 18 } 19 20 RecordParsed::~RecordParsed() { 21 } 22 23 // static 24 scoped_ptr<const RecordParsed> RecordParsed::CreateFrom( 25 DnsRecordParser* parser, 26 base::Time time_created) { 27 DnsResourceRecord record; 28 scoped_ptr<const RecordRdata> rdata; 29 30 if (!parser->ReadRecord(&record)) 31 return scoped_ptr<const RecordParsed>(); 32 33 switch (record.type) { 34 case ARecordRdata::kType: 35 rdata = ARecordRdata::Create(record.rdata, *parser); 36 break; 37 case AAAARecordRdata::kType: 38 rdata = AAAARecordRdata::Create(record.rdata, *parser); 39 break; 40 case CnameRecordRdata::kType: 41 rdata = CnameRecordRdata::Create(record.rdata, *parser); 42 break; 43 case PtrRecordRdata::kType: 44 rdata = PtrRecordRdata::Create(record.rdata, *parser); 45 break; 46 case SrvRecordRdata::kType: 47 rdata = SrvRecordRdata::Create(record.rdata, *parser); 48 break; 49 case TxtRecordRdata::kType: 50 rdata = TxtRecordRdata::Create(record.rdata, *parser); 51 break; 52 case NsecRecordRdata::kType: 53 rdata = NsecRecordRdata::Create(record.rdata, *parser); 54 break; 55 default: 56 LOG(WARNING) << "Unknown RData type for recieved record: " << record.type; 57 return scoped_ptr<const RecordParsed>(); 58 } 59 60 if (!rdata.get()) 61 return scoped_ptr<const RecordParsed>(); 62 63 return scoped_ptr<const RecordParsed>(new RecordParsed(record.name, 64 record.type, 65 record.klass, 66 record.ttl, 67 rdata.Pass(), 68 time_created)); 69 } 70 71 bool RecordParsed::IsEqual(const RecordParsed* other, bool is_mdns) const { 72 DCHECK(other); 73 uint16 klass = klass_; 74 uint16 other_klass = other->klass_; 75 76 if (is_mdns) { 77 klass &= dns_protocol::kMDnsClassMask; 78 other_klass &= dns_protocol::kMDnsClassMask; 79 } 80 81 return name_ == other->name_ && 82 klass == other_klass && 83 type_ == other->type_ && 84 rdata_->IsEqual(other->rdata_.get()); 85 } 86 } 87