OLD | NEW |
---|---|
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_win.h" | 5 #include "net/udp/udp_socket_win.h" |
6 | 6 |
7 #include <mstcpip.h> | 7 #include <mstcpip.h> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 core_->Release(); | 156 core_->Release(); |
157 } | 157 } |
158 | 158 |
159 //----------------------------------------------------------------------------- | 159 //----------------------------------------------------------------------------- |
160 | 160 |
161 UDPSocketWin::UDPSocketWin(DatagramSocket::BindType bind_type, | 161 UDPSocketWin::UDPSocketWin(DatagramSocket::BindType bind_type, |
162 const RandIntCallback& rand_int_cb, | 162 const RandIntCallback& rand_int_cb, |
163 net::NetLog* net_log, | 163 net::NetLog* net_log, |
164 const net::NetLog::Source& source) | 164 const net::NetLog::Source& source) |
165 : socket_(INVALID_SOCKET), | 165 : socket_(INVALID_SOCKET), |
166 sock_addr_family_(0), | |
166 socket_options_(0), | 167 socket_options_(0), |
168 multicast_time_to_live_(1), | |
169 multicast_loopback_mode_(true), | |
167 bind_type_(bind_type), | 170 bind_type_(bind_type), |
168 rand_int_cb_(rand_int_cb), | 171 rand_int_cb_(rand_int_cb), |
169 recv_from_address_(NULL), | 172 recv_from_address_(NULL), |
170 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { | 173 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { |
171 EnsureWinsockInit(); | 174 EnsureWinsockInit(); |
172 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 175 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
173 source.ToEventParametersCallback()); | 176 source.ToEventParametersCallback()); |
174 if (bind_type == DatagramSocket::RANDOM_BIND) | 177 if (bind_type == DatagramSocket::RANDOM_BIND) |
175 DCHECK(!rand_int_cb.is_null()); | 178 DCHECK(!rand_int_cb.is_null()); |
176 } | 179 } |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 if (rv < 0) | 351 if (rv < 0) |
349 return rv; | 352 return rv; |
350 rv = DoBind(address); | 353 rv = DoBind(address); |
351 if (rv < 0) | 354 if (rv < 0) |
352 return rv; | 355 return rv; |
353 local_address_.reset(); | 356 local_address_.reset(); |
354 return rv; | 357 return rv; |
355 } | 358 } |
356 | 359 |
357 int UDPSocketWin::CreateSocket(const IPEndPoint& address) { | 360 int UDPSocketWin::CreateSocket(const IPEndPoint& address) { |
358 socket_ = WSASocket(address.GetSockAddrFamily(), SOCK_DGRAM, IPPROTO_UDP, | 361 sock_addr_family_ = address.GetSockAddrFamily(); |
362 socket_ = WSASocket(sock_addr_family_, SOCK_DGRAM, IPPROTO_UDP, | |
359 NULL, 0, WSA_FLAG_OVERLAPPED); | 363 NULL, 0, WSA_FLAG_OVERLAPPED); |
360 if (socket_ == INVALID_SOCKET) | 364 if (socket_ == INVALID_SOCKET) |
361 return MapSystemError(WSAGetLastError()); | 365 return MapSystemError(WSAGetLastError()); |
362 core_ = new Core(this); | 366 core_ = new Core(this); |
363 return OK; | 367 return OK; |
364 } | 368 } |
365 | 369 |
366 bool UDPSocketWin::SetReceiveBufferSize(int32 size) { | 370 bool UDPSocketWin::SetReceiveBufferSize(int32 size) { |
367 DCHECK(CalledOnValidThread()); | 371 DCHECK(CalledOnValidThread()); |
368 int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF, | 372 int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF, |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
577 if (rv < 0) | 581 if (rv < 0) |
578 return MapSystemError(errno); | 582 return MapSystemError(errno); |
579 } | 583 } |
580 if (socket_options_ & SOCKET_OPTION_BROADCAST) { | 584 if (socket_options_ & SOCKET_OPTION_BROADCAST) { |
581 int rv = setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, | 585 int rv = setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, |
582 reinterpret_cast<const char*>(&true_value), | 586 reinterpret_cast<const char*>(&true_value), |
583 sizeof(true_value)); | 587 sizeof(true_value)); |
584 if (rv < 0) | 588 if (rv < 0) |
585 return MapSystemError(errno); | 589 return MapSystemError(errno); |
586 } | 590 } |
591 if (multicast_time_to_live_ != 1) { | |
wtc
2013/04/16 19:59:42
Can you cite a MSDN page or some Microsoft documen
Bei Zhang
2013/04/17 17:53:03
http://ibiblio.org/pub/linux/docs/HOWTO/other-form
| |
592 int rv = SetMulticastTimeToLive(multicast_time_to_live_); | |
593 if (rv != OK) | |
594 return rv; | |
595 } | |
596 if (!multicast_loopback_mode_) { | |
597 int rv = SetMulticastLoopbackMode(multicast_loopback_mode_); | |
598 if (rv != OK) | |
599 return rv; | |
600 } | |
587 return OK; | 601 return OK; |
588 } | 602 } |
589 | 603 |
590 int UDPSocketWin::DoBind(const IPEndPoint& address) { | 604 int UDPSocketWin::DoBind(const IPEndPoint& address) { |
591 SockaddrStorage storage; | 605 SockaddrStorage storage; |
592 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 606 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
593 return ERR_UNEXPECTED; | 607 return ERR_UNEXPECTED; |
594 int rv = bind(socket_, storage.addr, storage.addr_len); | 608 int rv = bind(socket_, storage.addr, storage.addr_len); |
595 return rv < 0 ? MapSystemError(WSAGetLastError()) : rv; | 609 return rv < 0 ? MapSystemError(WSAGetLastError()) : rv; |
596 } | 610 } |
(...skipping 10 matching lines...) Expand all Loading... | |
607 return rv; | 621 return rv; |
608 } | 622 } |
609 return DoBind(IPEndPoint(ip, 0)); | 623 return DoBind(IPEndPoint(ip, 0)); |
610 } | 624 } |
611 | 625 |
612 bool UDPSocketWin::ReceiveAddressToIPEndpoint(IPEndPoint* address) const { | 626 bool UDPSocketWin::ReceiveAddressToIPEndpoint(IPEndPoint* address) const { |
613 SockaddrStorage& storage = core_->recv_addr_storage_; | 627 SockaddrStorage& storage = core_->recv_addr_storage_; |
614 return address->FromSockAddr(storage.addr, storage.addr_len); | 628 return address->FromSockAddr(storage.addr, storage.addr_len); |
615 } | 629 } |
616 | 630 |
631 | |
632 int UDPSocketWin::JoinGroup( | |
633 const net::IPAddressNumber& group_address) const { | |
634 DCHECK(CalledOnValidThread()); | |
635 DCHECK(is_connected()); | |
636 int rv = -1; | |
wtc
2013/04/16 19:59:42
Nit: initialize |rv| to SOCKET_ERROR (which has th
Bei Zhang
2013/04/17 17:53:03
Done.
| |
637 switch (group_address.size()) { | |
638 case kIPv4AddressSize: { | |
639 if (sock_addr_family_ != AF_INET) | |
640 return ERR_ADDRESS_INVALID; | |
641 ip_mreq mreq; | |
642 memset(&mreq, 0, sizeof(mreq)); | |
643 memcpy(&mreq.imr_multiaddr, &group_address[0], kIPv4AddressSize); | |
644 rv = setsockopt(socket_, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
645 reinterpret_cast<const char*>(&mreq), | |
646 sizeof(mreq)); | |
647 break; | |
648 } | |
649 case kIPv6AddressSize: { | |
650 if (sock_addr_family_ != AF_INET6) | |
651 return ERR_ADDRESS_INVALID; | |
652 ipv6_mreq mreq; | |
653 memset(&mreq, 0, sizeof(mreq)); | |
654 memcpy(&mreq.ipv6mr_multiaddr, &group_address[0], kIPv6AddressSize); | |
655 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, | |
656 reinterpret_cast<const char*>(&mreq), | |
657 sizeof(mreq)); | |
658 break; | |
wtc
2013/04/16 19:59:42
NOTE: all of my comments on JoinGroup also apply t
Bei Zhang
2013/04/17 17:53:03
Done.
| |
659 } | |
660 } | |
661 return MapSystemError(rv); | |
wtc
2013/04/16 19:59:42
BUG: this should be:
if (rv != 0)
return Map
Bei Zhang
2013/04/17 17:53:03
Done.
| |
662 } | |
663 | |
664 int UDPSocketWin::LeaveGroup( | |
665 const net::IPAddressNumber& group_address) const { | |
666 DCHECK(CalledOnValidThread()); | |
667 DCHECK(is_connected()); | |
668 int rv = -1; | |
669 switch (group_address.size()) { | |
670 case kIPv4AddressSize: { | |
671 if (sock_addr_family_ != AF_INET) | |
672 return ERR_ADDRESS_INVALID; | |
673 ip_mreq mreq; | |
674 memset(&mreq, 0, sizeof(mreq)); | |
675 memcpy(&mreq.imr_multiaddr, &group_address[0], kIPv4AddressSize); | |
676 rv = setsockopt(socket_, IPPROTO_IP, IP_DROP_MEMBERSHIP, | |
677 reinterpret_cast<const char*>(&mreq), | |
678 sizeof(mreq)); | |
679 break; | |
680 } | |
681 case kIPv6AddressSize: { | |
682 if (sock_addr_family_ != AF_INET6) | |
683 return ERR_ADDRESS_INVALID; | |
684 ipv6_mreq mreq; | |
685 memset(&mreq, 0, sizeof(mreq)); | |
686 memcpy(&mreq.ipv6mr_multiaddr, &group_address[0], kIPv6AddressSize); | |
687 rv = setsockopt(socket_, IPPROTO_IPV6, IP_DROP_MEMBERSHIP, | |
688 reinterpret_cast<const char*>(&mreq), | |
689 sizeof(mreq)); | |
690 break; | |
691 } | |
692 } | |
693 return MapSystemError(rv); | |
694 } | |
695 | |
696 int UDPSocketWin::SetMulticastTimeToLive(int time_to_live) { | |
697 DCHECK(CalledOnValidThread()); | |
698 if (is_connected()) { | |
699 if (time_to_live < 0 || time_to_live > 255) | |
700 return ERR_INVALID_ARGUMENT; | |
wtc
2013/04/16 19:59:42
This check should be moved to line 709, before we
Bei Zhang
2013/04/17 17:53:03
I'm changing the behavior to make it only works be
| |
701 uint8 ttl = time_to_live; | |
702 int ip_family = sock_addr_family_ == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; | |
703 return MapSystemError(setsockopt(socket_, | |
704 ip_family, | |
705 IP_MULTICAST_TTL, | |
706 reinterpret_cast<const char*>(&ttl), | |
707 sizeof(ttl))); | |
708 } else { | |
709 multicast_time_to_live_ = time_to_live; | |
710 return OK; | |
711 } | |
712 } | |
713 | |
714 int UDPSocketWin::SetMulticastLoopbackMode(bool loopback) { | |
715 DCHECK(CalledOnValidThread()); | |
716 if (is_connected()) { | |
717 uint8 loop = loopback; | |
wtc
2013/04/16 19:59:42
NOTE: all of my comments on SetMulticastLoopbackMo
Bei Zhang
2013/04/17 17:53:03
Thank you for pointing that out. Adding the notes
wtc
2013/04/17 22:06:50
I think you should also add the notes to the
udp_s
| |
718 int ip_family = sock_addr_family_ == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; | |
719 return MapSystemError(setsockopt(socket_, | |
wtc
2013/04/16 19:59:42
BUG: MapSystemError() takes a socket error code
(W
Bei Zhang
2013/04/17 17:53:03
Done.
| |
720 ip_family, | |
721 IP_MULTICAST_LOOP, | |
722 reinterpret_cast<const char*>(&loop), | |
723 sizeof(loop))); | |
724 } else { | |
725 multicast_loopback_mode_ = loopback; | |
wtc
2013/04/16 19:59:42
I think this function should always store |loopbac
Bei Zhang
2013/04/17 17:53:03
Done.
| |
726 return OK; | |
727 } | |
728 } | |
729 | |
617 } // namespace net | 730 } // namespace net |
OLD | NEW |