Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: net/base/dns_util.cc

Issue 7063024: moved Buffer to dns_util as DnsResponseBuffer (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: fix copyright Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/dns_util.h ('k') | net/base/dnsrr_resolver.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/dns_util.h" 5 #include "net/base/dns_util.h"
6 6
7 #include <cstring> 7 #include <cstring>
8 8
9 namespace net { 9 namespace net {
10 10
11 // Based on DJB's public domain code. 11 // Based on DJB's public domain code.
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 } 89 }
90 90
91 std::string TrimEndingDot(const std::string& host) { 91 std::string TrimEndingDot(const std::string& host) {
92 std::string host_trimmed = host; 92 std::string host_trimmed = host;
93 size_t len = host_trimmed.length(); 93 size_t len = host_trimmed.length();
94 if (len > 1 && host_trimmed[len - 1] == '.') 94 if (len > 1 && host_trimmed[len - 1] == '.')
95 host_trimmed.erase(len - 1); 95 host_trimmed.erase(len - 1);
96 return host_trimmed; 96 return host_trimmed;
97 } 97 }
98 98
99 bool DnsResponseBuffer::U8(uint8* v) {
100 if (len_ < 1)
101 return false;
102 *v = *p_;
103 p_++;
104 len_--;
105 return true;
106 }
107
108 bool DnsResponseBuffer::U16(uint16* v) {
109 if (len_ < 2)
110 return false;
111 *v = static_cast<uint16>(p_[0]) << 8 |
112 static_cast<uint16>(p_[1]);
113 p_ += 2;
114 len_ -= 2;
115 return true;
116 }
117
118 bool DnsResponseBuffer::U32(uint32* v) {
119 if (len_ < 4)
120 return false;
121 *v = static_cast<uint32>(p_[0]) << 24 |
122 static_cast<uint32>(p_[1]) << 16 |
123 static_cast<uint32>(p_[2]) << 8 |
124 static_cast<uint32>(p_[3]);
125 p_ += 4;
126 len_ -= 4;
127 return true;
128 }
129
130 bool DnsResponseBuffer::Skip(unsigned n) {
131 if (len_ < n)
132 return false;
133 p_ += n;
134 len_ -= n;
135 return true;
136 }
137
138 bool DnsResponseBuffer::Block(base::StringPiece* out, unsigned len) {
139 if (len_ < len)
140 return false;
141 *out = base::StringPiece(reinterpret_cast<const char*>(p_), len);
142 p_ += len;
143 len_ -= len;
144 return true;
145 }
146
147 // DNSName parses a (possibly compressed) DNS name from the packet. If |name|
148 // is not NULL, then the name is written into it. See RFC 1035 section 4.1.4.
149 bool DnsResponseBuffer::DNSName(std::string* name) {
150 unsigned jumps = 0;
151 const uint8* p = p_;
152 unsigned len = len_;
153
154 if (name)
155 name->clear();
156
157 for (;;) {
158 if (len < 1)
159 return false;
160 uint8 d = *p;
161 p++;
162 len--;
163
164 // The two couple of bits of the length give the type of the length. It's
165 // either a direct length or a pointer to the remainder of the name.
166 if ((d & 0xc0) == 0xc0) {
167 // This limit matches the depth limit in djbdns.
168 if (jumps > 100)
169 return false;
170 if (len < 1)
171 return false;
172 uint16 offset = static_cast<uint16>(d) << 8 |
173 static_cast<uint16>(p[0]);
174 offset &= 0x3ff;
175 p++;
176 len--;
177
178 if (jumps == 0) {
179 p_ = p;
180 len_ = len;
181 }
182 jumps++;
183
184 if (offset >= packet_len_)
185 return false;
186 p = &packet_[offset];
187 len = packet_len_ - offset;
188 } else if ((d & 0xc0) == 0) {
189 uint8 label_len = d;
190 if (len < label_len)
191 return false;
192 if (name && label_len) {
193 if (!name->empty())
194 name->append(".");
195 name->append(reinterpret_cast<const char*>(p), label_len);
196 }
197 p += label_len;
198 len -= label_len;
199
200 if (jumps == 0) {
201 p_ = p;
202 len_ = len;
203 }
204
205 if (label_len == 0)
206 break;
207 } else {
208 return false;
209 }
210 }
211
212 return true;
213 }
214
99 } // namespace net 215 } // namespace net
OLDNEW
« no previous file with comments | « net/base/dns_util.h ('k') | net/base/dnsrr_resolver.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698