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

Side by Side Diff: net/udp/udp_socket_libevent.cc

Issue 690903002: Remove timing limitation of SetOption invocation for PPAPI sockets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/udp/udp_socket_libevent.h" 5 #include "net/udp/udp_socket_libevent.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <netdb.h> 9 #include <netdb.h>
10 #include <net/if.h> 10 #include <net/if.h>
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 if (rv < 0) { 310 if (rv < 0) {
311 Close(); 311 Close();
312 return rv; 312 return rv;
313 } 313 }
314 local_address_.reset(); 314 local_address_.reset();
315 return rv; 315 return rv;
316 } 316 }
317 317
318 int UDPSocketLibevent::SetReceiveBufferSize(int32 size) { 318 int UDPSocketLibevent::SetReceiveBufferSize(int32 size) {
319 DCHECK(CalledOnValidThread()); 319 DCHECK(CalledOnValidThread());
320 int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF, 320 if (is_connected()) {
321 reinterpret_cast<const char*>(&size), sizeof(size)); 321 int result = SetReceiveBufferSizeInternal(size);
322 int last_error = errno; 322 if (result != OK)
323 DCHECK(!rv) << "Could not set socket receive buffer size: " << last_error; 323 return result;
324 return rv == 0 ? OK : MapSystemError(last_error); 324 }
325 socket_options_ |= SOCKET_OPTION_RCVBUF_SIZE;
326 rcvbuf_size_ = size;
327 return OK;
325 } 328 }
326 329
327 int UDPSocketLibevent::SetSendBufferSize(int32 size) { 330 int UDPSocketLibevent::SetSendBufferSize(int32 size) {
328 DCHECK(CalledOnValidThread()); 331 DCHECK(CalledOnValidThread());
329 int rv = setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, 332 if (is_connected()) {
330 reinterpret_cast<const char*>(&size), sizeof(size)); 333 int result = SetSendBufferSizeInternal(size);
331 int last_error = errno; 334 if (result != OK)
332 DCHECK(!rv) << "Could not set socket send buffer size: " << last_error; 335 return result;
333 return rv == 0 ? OK : MapSystemError(last_error); 336 }
337 socket_options_ |= SOCKET_OPTION_SNDBUF_SIZE;
338 sndbuf_size_ = size;
339 return OK;
334 } 340 }
335 341
336 void UDPSocketLibevent::AllowAddressReuse() { 342 int UDPSocketLibevent::AllowAddressReuse() {
337 DCHECK(CalledOnValidThread()); 343 DCHECK(CalledOnValidThread());
338 DCHECK(!is_connected()); 344 if (is_connected()) {
jar (doing other things) 2014/11/04 22:51:09 What are the semantics for setting AllowAddressRus
hidehiko 2014/11/05 12:48:30 It affects the behavior of other socket's for the
339 345 int result = SetAddressReuseInternal(true);
346 if (result != OK)
347 return result;
348 }
340 socket_options_ |= SOCKET_OPTION_REUSE_ADDRESS; 349 socket_options_ |= SOCKET_OPTION_REUSE_ADDRESS;
350 return OK;
341 } 351 }
342 352
343 void UDPSocketLibevent::AllowBroadcast() { 353 int UDPSocketLibevent::DisallowAddressReuse() {
344 DCHECK(CalledOnValidThread()); 354 DCHECK(CalledOnValidThread());
345 DCHECK(!is_connected()); 355 if (is_connected()) {
356 int result = SetAddressReuseInternal(false);
357 if (result != OK)
358 return result;
359 }
360 socket_options_ &= ~SOCKET_OPTION_REUSE_ADDRESS;
361 return OK;
362 }
346 363
364 int UDPSocketLibevent::AllowBroadcast() {
jar (doing other things) 2014/11/04 22:51:09 Why don't you use a single setter method, taking a
hidehiko 2014/11/05 12:48:30 I just kept the current interface, as UDPSocket{Li
365 DCHECK(CalledOnValidThread());
366 if (is_connected()) {
367 int result = SetBroadcastInternal(true);
368 if (result != OK)
369 return result;
370 }
347 socket_options_ |= SOCKET_OPTION_BROADCAST; 371 socket_options_ |= SOCKET_OPTION_BROADCAST;
372 return OK;
373 }
374
375 int UDPSocketLibevent::DisallowBroadcast() {
376 DCHECK(CalledOnValidThread());
377 if (is_connected()) {
378 int result = SetBroadcastInternal(false);
379 if (result != OK)
380 return result;
381 }
382 socket_options_ &= ~SOCKET_OPTION_BROADCAST;
383 return OK;
348 } 384 }
349 385
350 void UDPSocketLibevent::ReadWatcher::OnFileCanReadWithoutBlocking(int) { 386 void UDPSocketLibevent::ReadWatcher::OnFileCanReadWithoutBlocking(int) {
351 if (!socket_->read_callback_.is_null()) 387 if (!socket_->read_callback_.is_null())
352 socket_->DidCompleteRead(); 388 socket_->DidCompleteRead();
353 } 389 }
354 390
355 void UDPSocketLibevent::WriteWatcher::OnFileCanWriteWithoutBlocking(int) { 391 void UDPSocketLibevent::WriteWatcher::OnFileCanWriteWithoutBlocking(int) {
356 if (!socket_->write_callback_.is_null()) 392 if (!socket_->write_callback_.is_null())
357 socket_->DidCompleteWrite(); 393 socket_->DidCompleteWrite();
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 addr, 545 addr,
510 storage.addr_len)); 546 storage.addr_len));
511 if (result < 0) 547 if (result < 0)
512 result = MapSystemError(errno); 548 result = MapSystemError(errno);
513 if (result != ERR_IO_PENDING) 549 if (result != ERR_IO_PENDING)
514 LogWrite(result, buf->data(), address); 550 LogWrite(result, buf->data(), address);
515 return result; 551 return result;
516 } 552 }
517 553
518 int UDPSocketLibevent::SetSocketOptions() { 554 int UDPSocketLibevent::SetSocketOptions() {
519 int true_value = 1; 555 DCHECK(CalledOnValidThread());
520 if (socket_options_ & SOCKET_OPTION_REUSE_ADDRESS) { 556 if (socket_options_ & SOCKET_OPTION_REUSE_ADDRESS) {
521 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value, 557 int rv = SetAddressReuseInternal(true);
522 sizeof(true_value)); 558 if (rv != OK)
523 if (rv < 0) 559 return rv;
524 return MapSystemError(errno);
525 } 560 }
526 if (socket_options_ & SOCKET_OPTION_BROADCAST) { 561 if (socket_options_ & SOCKET_OPTION_BROADCAST) {
527 int rv; 562 int rv = SetBroadcastInternal(true);
528 #if defined(OS_MACOSX) 563 if (rv != OK)
529 // SO_REUSEPORT on OSX permits multiple processes to each receive 564 return rv;
530 // UDP multicast or broadcast datagrams destined for the bound
531 // port.
532 rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEPORT, &true_value,
533 sizeof(true_value));
534 #else
535 rv = setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, &true_value,
536 sizeof(true_value));
537 #endif // defined(OS_MACOSX)
538 if (rv < 0)
539 return MapSystemError(errno);
540 } 565 }
541
542 if (!(socket_options_ & SOCKET_OPTION_MULTICAST_LOOP)) { 566 if (!(socket_options_ & SOCKET_OPTION_MULTICAST_LOOP)) {
543 int rv; 567 int rv;
544 if (addr_family_ == AF_INET) { 568 if (addr_family_ == AF_INET) {
545 u_char loop = 0; 569 u_char loop = 0;
546 rv = setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_LOOP, 570 rv = setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_LOOP,
547 &loop, sizeof(loop)); 571 &loop, sizeof(loop));
548 } else { 572 } else {
549 u_int loop = 0; 573 u_int loop = 0;
550 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, 574 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
551 &loop, sizeof(loop)); 575 &loop, sizeof(loop));
552 } 576 }
553 if (rv < 0) 577 if (rv < 0)
554 return MapSystemError(errno); 578 return MapSystemError(errno);
555 } 579 }
580 if (socket_options_ & SOCKET_OPTION_RCVBUF_SIZE) {
581 int rv = SetReceiveBufferSizeInternal(rcvbuf_size_);
582 if (rv != OK)
583 return rv;
584 }
585 if (socket_options_ & SOCKET_OPTION_SNDBUF_SIZE) {
586 int rv = SetSendBufferSizeInternal(sndbuf_size_);
587 if (rv != OK)
588 return rv;
589 }
590
556 if (multicast_time_to_live_ != IP_DEFAULT_MULTICAST_TTL) { 591 if (multicast_time_to_live_ != IP_DEFAULT_MULTICAST_TTL) {
557 int rv; 592 int rv;
558 if (addr_family_ == AF_INET) { 593 if (addr_family_ == AF_INET) {
559 u_char ttl = multicast_time_to_live_; 594 u_char ttl = multicast_time_to_live_;
560 rv = setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_TTL, 595 rv = setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_TTL,
561 &ttl, sizeof(ttl)); 596 &ttl, sizeof(ttl));
562 } else { 597 } else {
563 // Signed integer. -1 to use route default. 598 // Signed integer. -1 to use route default.
564 int ttl = multicast_time_to_live_; 599 int ttl = multicast_time_to_live_;
565 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 600 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 break; 633 break;
599 } 634 }
600 default: 635 default:
601 NOTREACHED() << "Invalid address family"; 636 NOTREACHED() << "Invalid address family";
602 return ERR_ADDRESS_INVALID; 637 return ERR_ADDRESS_INVALID;
603 } 638 }
604 } 639 }
605 return OK; 640 return OK;
606 } 641 }
607 642
643 int UDPSocketLibevent::SetAddressReuseInternal(bool value) {
644 DCHECK(CalledOnValidThread());
645 DCHECK(is_connected());
646 int opt_value = value ? 1 : 0;
647 int rv = setsockopt(
648 socket_, SOL_SOCKET, SO_REUSEADDR, &opt_value, sizeof(opt_value));
649 return rv == 0 ? OK : MapSystemError(errno);
650 }
651
652 int UDPSocketLibevent::SetBroadcastInternal(bool value) {
653 DCHECK(CalledOnValidThread());
654 DCHECK(is_connected());
655 int opt_value = value ? 1 : 0;
656 #if defined(OS_MACOSX)
657 // SO_REUSEPORT on OSX permits multiple processes to each receive
658 // UDP multicast or broadcast datagrams destined for the bound
659 // port.
660 const int kOptName = SO_REUSEPORT;
661 #else
662 const int kOptName = SO_BROADCAST;
663 #endif
664 int rv = setsockopt(
665 socket_, SOL_SOCKET, kOptName, &opt_value, sizeof(opt_value));
666 return rv == 0 ? OK : MapSystemError(errno);
667 }
668
669 int UDPSocketLibevent::SetReceiveBufferSizeInternal(int32 size) {
670 DCHECK(CalledOnValidThread());
671 DCHECK(is_connected());
672 int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF,
673 reinterpret_cast<const char*>(&size), sizeof(size));
674 int last_error = errno;
675 DCHECK(!rv) << "Could not set socket receive buffer size: " << last_error;
676 return rv == 0 ? OK : MapSystemError(last_error);
677 }
678
679 int UDPSocketLibevent::SetSendBufferSizeInternal(int32 size) {
680 DCHECK(CalledOnValidThread());
681 DCHECK(is_connected());
682 int rv = setsockopt(socket_, SOL_SOCKET, SO_SNDBUF,
683 reinterpret_cast<const char*>(&size), sizeof(size));
684 int last_error = errno;
685 DCHECK(!rv) << "Could not set socket send buffer size: " << last_error;
686 return rv == 0 ? OK : MapSystemError(last_error);
687 }
688
608 int UDPSocketLibevent::DoBind(const IPEndPoint& address) { 689 int UDPSocketLibevent::DoBind(const IPEndPoint& address) {
609 SockaddrStorage storage; 690 SockaddrStorage storage;
610 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) 691 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
611 return ERR_ADDRESS_INVALID; 692 return ERR_ADDRESS_INVALID;
612 int rv = bind(socket_, storage.addr, storage.addr_len); 693 int rv = bind(socket_, storage.addr, storage.addr_len);
613 if (rv == 0) 694 if (rv == 0)
614 return OK; 695 return OK;
615 int last_error = errno; 696 int last_error = errno;
616 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.UdpSocketBindErrorFromPosix", last_error); 697 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.UdpSocketBindErrorFromPosix", last_error);
617 #if defined(OS_CHROMEOS) 698 #if defined(OS_CHROMEOS)
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
767 return MapSystemError(errno); 848 return MapSystemError(errno);
768 849
769 return OK; 850 return OK;
770 } 851 }
771 852
772 void UDPSocketLibevent::DetachFromThread() { 853 void UDPSocketLibevent::DetachFromThread() {
773 base::NonThreadSafe::DetachFromThread(); 854 base::NonThreadSafe::DetachFromThread();
774 } 855 }
775 856
776 } // namespace net 857 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698