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

Side by Side Diff: webrtc/base/natsocketfactory.cc

Issue 2620303003: Replace ASSERT by RTC_DCHECK in all non-test code. (Closed)
Patch Set: Address final nits. Created 3 years, 11 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
« no previous file with comments | « webrtc/base/natserver.cc ('k') | webrtc/base/network.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 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/base/natsocketfactory.h" 11 #include "webrtc/base/natsocketfactory.h"
12 12
13 #include "webrtc/base/arraysize.h" 13 #include "webrtc/base/arraysize.h"
14 #include "webrtc/base/checks.h"
14 #include "webrtc/base/logging.h" 15 #include "webrtc/base/logging.h"
15 #include "webrtc/base/natserver.h" 16 #include "webrtc/base/natserver.h"
16 #include "webrtc/base/virtualsocketserver.h" 17 #include "webrtc/base/virtualsocketserver.h"
17 18
18 namespace rtc { 19 namespace rtc {
19 20
20 // Packs the given socketaddress into the buffer in buf, in the quasi-STUN 21 // Packs the given socketaddress into the buffer in buf, in the quasi-STUN
21 // format that the natserver uses. 22 // format that the natserver uses.
22 // Returns 0 if an invalid address is passed. 23 // Returns 0 if an invalid address is passed.
23 size_t PackAddressForNAT(char* buf, size_t buf_size, 24 size_t PackAddressForNAT(char* buf, size_t buf_size,
24 const SocketAddress& remote_addr) { 25 const SocketAddress& remote_addr) {
25 const IPAddress& ip = remote_addr.ipaddr(); 26 const IPAddress& ip = remote_addr.ipaddr();
26 int family = ip.family(); 27 int family = ip.family();
27 buf[0] = 0; 28 buf[0] = 0;
28 buf[1] = family; 29 buf[1] = family;
29 // Writes the port. 30 // Writes the port.
30 *(reinterpret_cast<uint16_t*>(&buf[2])) = HostToNetwork16(remote_addr.port()); 31 *(reinterpret_cast<uint16_t*>(&buf[2])) = HostToNetwork16(remote_addr.port());
31 if (family == AF_INET) { 32 if (family == AF_INET) {
32 ASSERT(buf_size >= kNATEncodedIPv4AddressSize); 33 RTC_DCHECK(buf_size >= kNATEncodedIPv4AddressSize);
33 in_addr v4addr = ip.ipv4_address(); 34 in_addr v4addr = ip.ipv4_address();
34 memcpy(&buf[4], &v4addr, kNATEncodedIPv4AddressSize - 4); 35 memcpy(&buf[4], &v4addr, kNATEncodedIPv4AddressSize - 4);
35 return kNATEncodedIPv4AddressSize; 36 return kNATEncodedIPv4AddressSize;
36 } else if (family == AF_INET6) { 37 } else if (family == AF_INET6) {
37 ASSERT(buf_size >= kNATEncodedIPv6AddressSize); 38 RTC_DCHECK(buf_size >= kNATEncodedIPv6AddressSize);
38 in6_addr v6addr = ip.ipv6_address(); 39 in6_addr v6addr = ip.ipv6_address();
39 memcpy(&buf[4], &v6addr, kNATEncodedIPv6AddressSize - 4); 40 memcpy(&buf[4], &v6addr, kNATEncodedIPv6AddressSize - 4);
40 return kNATEncodedIPv6AddressSize; 41 return kNATEncodedIPv6AddressSize;
41 } 42 }
42 return 0U; 43 return 0U;
43 } 44 }
44 45
45 // Decodes the remote address from a packet that has been encoded with the nat's 46 // Decodes the remote address from a packet that has been encoded with the nat's
46 // quasi-STUN format. Returns the length of the address (i.e., the offset into 47 // quasi-STUN format. Returns the length of the address (i.e., the offset into
47 // data where the original packet starts). 48 // data where the original packet starts).
48 size_t UnpackAddressFromNAT(const char* buf, size_t buf_size, 49 size_t UnpackAddressFromNAT(const char* buf, size_t buf_size,
49 SocketAddress* remote_addr) { 50 SocketAddress* remote_addr) {
50 ASSERT(buf_size >= 8); 51 RTC_DCHECK(buf_size >= 8);
51 ASSERT(buf[0] == 0); 52 RTC_DCHECK(buf[0] == 0);
52 int family = buf[1]; 53 int family = buf[1];
53 uint16_t port = 54 uint16_t port =
54 NetworkToHost16(*(reinterpret_cast<const uint16_t*>(&buf[2]))); 55 NetworkToHost16(*(reinterpret_cast<const uint16_t*>(&buf[2])));
55 if (family == AF_INET) { 56 if (family == AF_INET) {
56 const in_addr* v4addr = reinterpret_cast<const in_addr*>(&buf[4]); 57 const in_addr* v4addr = reinterpret_cast<const in_addr*>(&buf[4]);
57 *remote_addr = SocketAddress(IPAddress(*v4addr), port); 58 *remote_addr = SocketAddress(IPAddress(*v4addr), port);
58 return kNATEncodedIPv4AddressSize; 59 return kNATEncodedIPv4AddressSize;
59 } else if (family == AF_INET6) { 60 } else if (family == AF_INET6) {
60 ASSERT(buf_size >= 20); 61 RTC_DCHECK(buf_size >= 20);
61 const in6_addr* v6addr = reinterpret_cast<const in6_addr*>(&buf[4]); 62 const in6_addr* v6addr = reinterpret_cast<const in6_addr*>(&buf[4]);
62 *remote_addr = SocketAddress(IPAddress(*v6addr), port); 63 *remote_addr = SocketAddress(IPAddress(*v6addr), port);
63 return kNATEncodedIPv6AddressSize; 64 return kNATEncodedIPv6AddressSize;
64 } 65 }
65 return 0U; 66 return 0U;
66 } 67 }
67 68
68 69
69 // NATSocket 70 // NATSocket
70 class NATSocket : public AsyncSocket, public sigslot::has_slots<> { 71 class NATSocket : public AsyncSocket, public sigslot::has_slots<> {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 } 123 }
123 124
124 if (result >= 0) { 125 if (result >= 0) {
125 remote_addr_ = addr; 126 remote_addr_ = addr;
126 } 127 }
127 128
128 return result; 129 return result;
129 } 130 }
130 131
131 int Send(const void* data, size_t size) override { 132 int Send(const void* data, size_t size) override {
132 ASSERT(connected_); 133 RTC_DCHECK(connected_);
133 return SendTo(data, size, remote_addr_); 134 return SendTo(data, size, remote_addr_);
134 } 135 }
135 136
136 int SendTo(const void* data, 137 int SendTo(const void* data,
137 size_t size, 138 size_t size,
138 const SocketAddress& addr) override { 139 const SocketAddress& addr) override {
139 ASSERT(!connected_ || addr == remote_addr_); 140 RTC_DCHECK(!connected_ || addr == remote_addr_);
140 if (server_addr_.IsNil() || type_ == SOCK_STREAM) { 141 if (server_addr_.IsNil() || type_ == SOCK_STREAM) {
141 return socket_->SendTo(data, size, addr); 142 return socket_->SendTo(data, size, addr);
142 } 143 }
143 // This array will be too large for IPv4 packets, but only by 12 bytes. 144 // This array will be too large for IPv4 packets, but only by 12 bytes.
144 std::unique_ptr<char[]> buf(new char[size + kNATEncodedIPv6AddressSize]); 145 std::unique_ptr<char[]> buf(new char[size + kNATEncodedIPv6AddressSize]);
145 size_t addrlength = PackAddressForNAT(buf.get(), 146 size_t addrlength = PackAddressForNAT(buf.get(),
146 size + kNATEncodedIPv6AddressSize, 147 size + kNATEncodedIPv6AddressSize,
147 addr); 148 addr);
148 size_t encoded_size = size + addrlength; 149 size_t encoded_size = size + addrlength;
149 memcpy(buf.get() + addrlength, data, size); 150 memcpy(buf.get() + addrlength, data, size);
150 int result = socket_->SendTo(buf.get(), encoded_size, server_addr_); 151 int result = socket_->SendTo(buf.get(), encoded_size, server_addr_);
151 if (result >= 0) { 152 if (result >= 0) {
152 ASSERT(result == static_cast<int>(encoded_size)); 153 RTC_DCHECK(result == static_cast<int>(encoded_size));
153 result = result - static_cast<int>(addrlength); 154 result = result - static_cast<int>(addrlength);
154 } 155 }
155 return result; 156 return result;
156 } 157 }
157 158
158 int Recv(void* data, size_t size, int64_t* timestamp) override { 159 int Recv(void* data, size_t size, int64_t* timestamp) override {
159 SocketAddress addr; 160 SocketAddress addr;
160 return RecvFrom(data, size, &addr, timestamp); 161 return RecvFrom(data, size, &addr, timestamp);
161 } 162 }
162 163
163 int RecvFrom(void* data, 164 int RecvFrom(void* data,
164 size_t size, 165 size_t size,
165 SocketAddress* out_addr, 166 SocketAddress* out_addr,
166 int64_t* timestamp) override { 167 int64_t* timestamp) override {
167 if (server_addr_.IsNil() || type_ == SOCK_STREAM) { 168 if (server_addr_.IsNil() || type_ == SOCK_STREAM) {
168 return socket_->RecvFrom(data, size, out_addr, timestamp); 169 return socket_->RecvFrom(data, size, out_addr, timestamp);
169 } 170 }
170 // Make sure we have enough room to read the requested amount plus the 171 // Make sure we have enough room to read the requested amount plus the
171 // largest possible header address. 172 // largest possible header address.
172 SocketAddress remote_addr; 173 SocketAddress remote_addr;
173 Grow(size + kNATEncodedIPv6AddressSize); 174 Grow(size + kNATEncodedIPv6AddressSize);
174 175
175 // Read the packet from the socket. 176 // Read the packet from the socket.
176 int result = socket_->RecvFrom(buf_, size_, &remote_addr, timestamp); 177 int result = socket_->RecvFrom(buf_, size_, &remote_addr, timestamp);
177 if (result >= 0) { 178 if (result >= 0) {
178 ASSERT(remote_addr == server_addr_); 179 RTC_DCHECK(remote_addr == server_addr_);
179 180
180 // TODO: we need better framing so we know how many bytes we can 181 // TODO: we need better framing so we know how many bytes we can
181 // return before we need to read the next address. For UDP, this will be 182 // return before we need to read the next address. For UDP, this will be
182 // fine as long as the reader always reads everything in the packet. 183 // fine as long as the reader always reads everything in the packet.
183 ASSERT((size_t)result < size_); 184 RTC_DCHECK((size_t)result < size_);
184 185
185 // Decode the wire packet into the actual results. 186 // Decode the wire packet into the actual results.
186 SocketAddress real_remote_addr; 187 SocketAddress real_remote_addr;
187 size_t addrlength = UnpackAddressFromNAT(buf_, result, &real_remote_addr); 188 size_t addrlength = UnpackAddressFromNAT(buf_, result, &real_remote_addr);
188 memcpy(data, buf_ + addrlength, result - addrlength); 189 memcpy(data, buf_ + addrlength, result - addrlength);
189 190
190 // Make sure this packet should be delivered before returning it. 191 // Make sure this packet should be delivered before returning it.
191 if (!connected_ || (real_remote_addr == remote_addr_)) { 192 if (!connected_ || (real_remote_addr == remote_addr_)) {
192 if (out_addr) 193 if (out_addr)
193 *out_addr = real_remote_addr; 194 *out_addr = real_remote_addr;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 int EstimateMTU(uint16_t* mtu) override { return socket_->EstimateMTU(mtu); } 229 int EstimateMTU(uint16_t* mtu) override { return socket_->EstimateMTU(mtu); }
229 int GetOption(Option opt, int* value) override { 230 int GetOption(Option opt, int* value) override {
230 return socket_->GetOption(opt, value); 231 return socket_->GetOption(opt, value);
231 } 232 }
232 int SetOption(Option opt, int value) override { 233 int SetOption(Option opt, int value) override {
233 return socket_->SetOption(opt, value); 234 return socket_->SetOption(opt, value);
234 } 235 }
235 236
236 void OnConnectEvent(AsyncSocket* socket) { 237 void OnConnectEvent(AsyncSocket* socket) {
237 // If we're NATed, we need to send a message with the real addr to use. 238 // If we're NATed, we need to send a message with the real addr to use.
238 ASSERT(socket == socket_); 239 RTC_DCHECK(socket == socket_);
239 if (server_addr_.IsNil()) { 240 if (server_addr_.IsNil()) {
240 connected_ = true; 241 connected_ = true;
241 SignalConnectEvent(this); 242 SignalConnectEvent(this);
242 } else { 243 } else {
243 SendConnectRequest(); 244 SendConnectRequest();
244 } 245 }
245 } 246 }
246 void OnReadEvent(AsyncSocket* socket) { 247 void OnReadEvent(AsyncSocket* socket) {
247 // If we're NATed, we need to process the connect reply. 248 // If we're NATed, we need to process the connect reply.
248 ASSERT(socket == socket_); 249 RTC_DCHECK(socket == socket_);
249 if (type_ == SOCK_STREAM && !server_addr_.IsNil() && !connected_) { 250 if (type_ == SOCK_STREAM && !server_addr_.IsNil() && !connected_) {
250 HandleConnectReply(); 251 HandleConnectReply();
251 } else { 252 } else {
252 SignalReadEvent(this); 253 SignalReadEvent(this);
253 } 254 }
254 } 255 }
255 void OnWriteEvent(AsyncSocket* socket) { 256 void OnWriteEvent(AsyncSocket* socket) {
256 ASSERT(socket == socket_); 257 RTC_DCHECK(socket == socket_);
257 SignalWriteEvent(this); 258 SignalWriteEvent(this);
258 } 259 }
259 void OnCloseEvent(AsyncSocket* socket, int error) { 260 void OnCloseEvent(AsyncSocket* socket, int error) {
260 ASSERT(socket == socket_); 261 RTC_DCHECK(socket == socket_);
261 SignalCloseEvent(this, error); 262 SignalCloseEvent(this, error);
262 } 263 }
263 264
264 private: 265 private:
265 // Makes sure the buffer is at least the given size. 266 // Makes sure the buffer is at least the given size.
266 void Grow(size_t new_size) { 267 void Grow(size_t new_size) {
267 if (size_ < new_size) { 268 if (size_ < new_size) {
268 delete[] buf_; 269 delete[] buf_;
269 size_ = new_size; 270 size_ = new_size;
270 buf_ = new char[size_]; 271 buf_ = new char[size_];
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 NATSocketServer::Translator* NATSocketServer::TranslatorMap::FindClient( 498 NATSocketServer::Translator* NATSocketServer::TranslatorMap::FindClient(
498 const SocketAddress& int_ip) { 499 const SocketAddress& int_ip) {
499 Translator* nat = NULL; 500 Translator* nat = NULL;
500 for (TranslatorMap::iterator it = begin(); it != end() && !nat; ++it) { 501 for (TranslatorMap::iterator it = begin(); it != end() && !nat; ++it) {
501 nat = it->second->FindClient(int_ip); 502 nat = it->second->FindClient(int_ip);
502 } 503 }
503 return nat; 504 return nat;
504 } 505 }
505 506
506 } // namespace rtc 507 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/natserver.cc ('k') | webrtc/base/network.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698