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

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/mount_node_udp.cc

Issue 63573006: [NaCl SDK] nacl_io: Auto-bind UDP socket on first sendto(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: getsockname stuff Created 7 years, 1 month 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 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 5
6 #include "nacl_io/mount_node_udp.h" 6 #include "nacl_io/mount_node_udp.h"
7 7
8 #include <errno.h> 8 #include <errno.h>
9 #include <string.h> 9 #include <string.h>
10 10
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 if (IsBound()) 211 if (IsBound())
212 return EINVAL; 212 return EINVAL;
213 213
214 PP_Resource out_addr = SockAddrToResource(addr, len); 214 PP_Resource out_addr = SockAddrToResource(addr, len);
215 if (0 == out_addr) 215 if (0 == out_addr)
216 return EINVAL; 216 return EINVAL;
217 217
218 int err = UDPInterface()->Bind(socket_resource_, 218 int err = UDPInterface()->Bind(socket_resource_,
219 out_addr, 219 out_addr,
220 PP_BlockUntilComplete()); 220 PP_BlockUntilComplete());
221 if (err != 0) { 221 mount_->ppapi()->ReleaseResource(out_addr);
222 mount_->ppapi()->ReleaseResource(out_addr); 222 if (err != 0)
223 return PPErrorToErrno(err); 223 return PPErrorToErrno(err);
224 } 224
225 // Get the address that was actually bound (in case addr was 0.0.0.0:0).
226 out_addr = UDPInterface()->GetBoundAddress(socket_resource_);
227 if (out_addr == 0)
228 return EINVAL;
225 229
226 // Now that we are bound, we can start sending and receiving. 230 // Now that we are bound, we can start sending and receiving.
227 SetStreamFlags(SSF_CAN_SEND | SSF_CAN_RECV); 231 SetStreamFlags(SSF_CAN_SEND | SSF_CAN_RECV);
228 QueueInput(); 232 QueueInput();
229 233
230 local_addr_ = out_addr; 234 local_addr_ = out_addr;
231 return 0; 235 return 0;
232 } 236 }
233 237
234 Error MountNodeUDP::Connect(const HandleAttr& attr, 238 Error MountNodeUDP::Connect(const HandleAttr& attr,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 279
276 // Should never happen, Recv_Locked should not be called 280 // Should never happen, Recv_Locked should not be called
277 // unless already in a POLLIN state. 281 // unless already in a POLLIN state.
278 return EBADF; 282 return EBADF;
279 } 283 }
280 284
281 Error MountNodeUDP::Send_Locked(const void* buf, 285 Error MountNodeUDP::Send_Locked(const void* buf,
282 size_t len, 286 size_t len,
283 PP_Resource addr, 287 PP_Resource addr,
284 int* out_len) { 288 int* out_len) {
289 if (!IsBound()) {
290 // Pepper requires a socket to be bound before it can send.
291 sockaddr_in addr;
292 addr.sin_family = AF_INET;
293 addr.sin_port = 0;
294 memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
295 Error err =
296 Bind(reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr));
297 if (err != 0)
298 return err;
299 }
300
285 *out_len = 0; 301 *out_len = 0;
286 int capped_len = 302 int capped_len =
287 static_cast<int32_t>(std::min<int>(len, kMaxPacketSize)); 303 static_cast<int32_t>(std::min<int>(len, kMaxPacketSize));
288 Packet* packet = new Packet(mount_->ppapi()); 304 Packet* packet = new Packet(mount_->ppapi());
289 packet->Copy(buf, capped_len, addr); 305 packet->Copy(buf, capped_len, addr);
290 306
291 emitter_->WriteTXPacket_Locked(packet); 307 emitter_->WriteTXPacket_Locked(packet);
292 *out_len = capped_len; 308 *out_len = capped_len;
293 return 0; 309 return 0;
294 } 310 }
295 311
296 } // namespace nacl_io 312 } // namespace nacl_io
297 313
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698