| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef NET_UDP_UDP_SOCKET_WIN_H_ | |
| 6 #define NET_UDP_UDP_SOCKET_WIN_H_ | |
| 7 | |
| 8 #include <qos2.h> | |
| 9 #include <winsock2.h> | |
| 10 | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/threading/non_thread_safe.h" | |
| 14 #include "base/win/object_watcher.h" | |
| 15 #include "base/win/scoped_handle.h" | |
| 16 #include "net/base/address_family.h" | |
| 17 #include "net/base/completion_callback.h" | |
| 18 #include "net/base/net_export.h" | |
| 19 #include "net/base/rand_callback.h" | |
| 20 #include "net/base/ip_endpoint.h" | |
| 21 #include "net/base/io_buffer.h" | |
| 22 #include "net/base/net_log.h" | |
| 23 #include "net/udp/datagram_socket.h" | |
| 24 | |
| 25 namespace net { | |
| 26 | |
| 27 class NET_EXPORT UDPSocketWin | |
| 28 : NON_EXPORTED_BASE(public base::NonThreadSafe), | |
| 29 NON_EXPORTED_BASE(public base::win::ObjectWatcher::Delegate) { | |
| 30 public: | |
| 31 UDPSocketWin(DatagramSocket::BindType bind_type, | |
| 32 const RandIntCallback& rand_int_cb, | |
| 33 net::NetLog* net_log, | |
| 34 const net::NetLog::Source& source); | |
| 35 virtual ~UDPSocketWin(); | |
| 36 | |
| 37 // Opens the socket. | |
| 38 // Returns a net error code. | |
| 39 int Open(AddressFamily address_family); | |
| 40 | |
| 41 // Connects the socket to connect with a certain |address|. | |
| 42 // Should be called after Open(). | |
| 43 // Returns a net error code. | |
| 44 int Connect(const IPEndPoint& address); | |
| 45 | |
| 46 // Binds the address/port for this socket to |address|. This is generally | |
| 47 // only used on a server. Should be called after Open(). | |
| 48 // Returns a net error code. | |
| 49 int Bind(const IPEndPoint& address); | |
| 50 | |
| 51 // Closes the socket. | |
| 52 // TODO(rvargas, hidehiko): Disallow re-Open() after Close(). | |
| 53 void Close(); | |
| 54 | |
| 55 // Copies the remote udp address into |address| and returns a net error code. | |
| 56 int GetPeerAddress(IPEndPoint* address) const; | |
| 57 | |
| 58 // Copies the local udp address into |address| and returns a net error code. | |
| 59 // (similar to getsockname) | |
| 60 int GetLocalAddress(IPEndPoint* address) const; | |
| 61 | |
| 62 // IO: | |
| 63 // Multiple outstanding read requests are not supported. | |
| 64 // Full duplex mode (reading and writing at the same time) is supported | |
| 65 | |
| 66 // Reads from the socket. | |
| 67 // Only usable from the client-side of a UDP socket, after the socket | |
| 68 // has been connected. | |
| 69 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
| 70 | |
| 71 // Writes to the socket. | |
| 72 // Only usable from the client-side of a UDP socket, after the socket | |
| 73 // has been connected. | |
| 74 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | |
| 75 | |
| 76 // Reads from a socket and receive sender address information. | |
| 77 // |buf| is the buffer to read data into. | |
| 78 // |buf_len| is the maximum amount of data to read. | |
| 79 // |address| is a buffer provided by the caller for receiving the sender | |
| 80 // address information about the received data. This buffer must be kept | |
| 81 // alive by the caller until the callback is placed. | |
| 82 // |address_length| is a ptr to the length of the |address| buffer. This | |
| 83 // is an input parameter containing the maximum size |address| can hold | |
| 84 // and also an output parameter for the size of |address| upon completion. | |
| 85 // |callback| is the callback on completion of the RecvFrom. | |
| 86 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. | |
| 87 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, | |
| 88 // and |address_length| alive until the callback is called. | |
| 89 int RecvFrom(IOBuffer* buf, | |
| 90 int buf_len, | |
| 91 IPEndPoint* address, | |
| 92 const CompletionCallback& callback); | |
| 93 | |
| 94 // Sends to a socket with a particular destination. | |
| 95 // |buf| is the buffer to send | |
| 96 // |buf_len| is the number of bytes to send | |
| 97 // |address| is the recipient address. | |
| 98 // |address_length| is the size of the recipient address | |
| 99 // |callback| is the user callback function to call on complete. | |
| 100 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. | |
| 101 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| | |
| 102 // alive until the callback is called. | |
| 103 int SendTo(IOBuffer* buf, | |
| 104 int buf_len, | |
| 105 const IPEndPoint& address, | |
| 106 const CompletionCallback& callback); | |
| 107 | |
| 108 // Sets the receive buffer size (in bytes) for the socket. | |
| 109 // Returns a net error code. | |
| 110 int SetReceiveBufferSize(int32 size); | |
| 111 | |
| 112 // Sets the send buffer size (in bytes) for the socket. | |
| 113 // Returns a net error code. | |
| 114 int SetSendBufferSize(int32 size); | |
| 115 | |
| 116 // Returns true if the socket is already connected or bound. | |
| 117 bool is_connected() const { return is_connected_; } | |
| 118 | |
| 119 const BoundNetLog& NetLog() const { return net_log_; } | |
| 120 | |
| 121 // Sets corresponding flags in |socket_options_| to allow the socket | |
| 122 // to share the local address to which the socket will be bound with | |
| 123 // other processes. Should be called between Open() and Bind(). | |
| 124 // Returns a net error code. | |
| 125 int AllowAddressReuse(); | |
| 126 | |
| 127 // Sets corresponding flags in |socket_options_| to allow sending | |
| 128 // and receiving packets to and from broadcast addresses. | |
| 129 int SetBroadcast(bool broadcast); | |
| 130 | |
| 131 // Joins the multicast group. | |
| 132 // |group_address| is the group address to join, could be either | |
| 133 // an IPv4 or IPv6 address. | |
| 134 // Returns a net error code. | |
| 135 int JoinGroup(const IPAddressNumber& group_address) const; | |
| 136 | |
| 137 // Leaves the multicast group. | |
| 138 // |group_address| is the group address to leave, could be either | |
| 139 // an IPv4 or IPv6 address. If the socket hasn't joined the group, | |
| 140 // it will be ignored. | |
| 141 // It's optional to leave the multicast group before destroying | |
| 142 // the socket. It will be done by the OS. | |
| 143 // Return a net error code. | |
| 144 int LeaveGroup(const IPAddressNumber& group_address) const; | |
| 145 | |
| 146 // Sets interface to use for multicast. If |interface_index| set to 0, | |
| 147 // default interface is used. | |
| 148 // Should be called before Bind(). | |
| 149 // Returns a net error code. | |
| 150 int SetMulticastInterface(uint32 interface_index); | |
| 151 | |
| 152 // Sets the time-to-live option for UDP packets sent to the multicast | |
| 153 // group address. The default value of this option is 1. | |
| 154 // Cannot be negative or more than 255. | |
| 155 // Should be called before Bind(). | |
| 156 int SetMulticastTimeToLive(int time_to_live); | |
| 157 | |
| 158 // Sets the loopback flag for UDP socket. If this flag is true, the host | |
| 159 // will receive packets sent to the joined group from itself. | |
| 160 // The default value of this option is true. | |
| 161 // Should be called before Bind(). | |
| 162 // | |
| 163 // Note: the behavior of |SetMulticastLoopbackMode| is slightly | |
| 164 // different between Windows and Unix-like systems. The inconsistency only | |
| 165 // happens when there are more than one applications on the same host | |
| 166 // joined to the same multicast group while having different settings on | |
| 167 // multicast loopback mode. On Windows, the applications with loopback off | |
| 168 // will not RECEIVE the loopback packets; while on Unix-like systems, the | |
| 169 // applications with loopback off will not SEND the loopback packets to | |
| 170 // other applications on the same host. See MSDN: http://goo.gl/6vqbj | |
| 171 int SetMulticastLoopbackMode(bool loopback); | |
| 172 | |
| 173 // Sets the differentiated services flags on outgoing packets. May not | |
| 174 // do anything on some platforms. | |
| 175 int SetDiffServCodePoint(DiffServCodePoint dscp); | |
| 176 | |
| 177 // Resets the thread to be used for thread-safety checks. | |
| 178 void DetachFromThread(); | |
| 179 | |
| 180 // This class by default uses overlapped IO. Call this method before Open() | |
| 181 // to switch to non-blocking IO. | |
| 182 void UseNonBlockingIO(); | |
| 183 | |
| 184 private: | |
| 185 enum SocketOptions { | |
| 186 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 | |
| 187 }; | |
| 188 | |
| 189 class Core; | |
| 190 | |
| 191 void DoReadCallback(int rv); | |
| 192 void DoWriteCallback(int rv); | |
| 193 | |
| 194 void DidCompleteRead(); | |
| 195 void DidCompleteWrite(); | |
| 196 | |
| 197 // base::ObjectWatcher::Delegate implementation. | |
| 198 virtual void OnObjectSignaled(HANDLE object); | |
| 199 void OnReadSignaled(); | |
| 200 void OnWriteSignaled(); | |
| 201 | |
| 202 void WatchForReadWrite(); | |
| 203 | |
| 204 // Handles stats and logging. |result| is the number of bytes transferred, on | |
| 205 // success, or the net error code on failure. | |
| 206 void LogRead(int result, const char* bytes, const IPEndPoint* address) const; | |
| 207 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; | |
| 208 | |
| 209 // Same as SendTo(), except that address is passed by pointer | |
| 210 // instead of by reference. It is called from Write() with |address| | |
| 211 // set to NULL. | |
| 212 int SendToOrWrite(IOBuffer* buf, | |
| 213 int buf_len, | |
| 214 const IPEndPoint* address, | |
| 215 const CompletionCallback& callback); | |
| 216 | |
| 217 int InternalConnect(const IPEndPoint& address); | |
| 218 | |
| 219 // Version for using overlapped IO. | |
| 220 int InternalRecvFromOverlapped(IOBuffer* buf, | |
| 221 int buf_len, | |
| 222 IPEndPoint* address); | |
| 223 int InternalSendToOverlapped(IOBuffer* buf, | |
| 224 int buf_len, | |
| 225 const IPEndPoint* address); | |
| 226 | |
| 227 // Version for using non-blocking IO. | |
| 228 int InternalRecvFromNonBlocking(IOBuffer* buf, | |
| 229 int buf_len, | |
| 230 IPEndPoint* address); | |
| 231 int InternalSendToNonBlocking(IOBuffer* buf, | |
| 232 int buf_len, | |
| 233 const IPEndPoint* address); | |
| 234 | |
| 235 // Applies |socket_options_| to |socket_|. Should be called before | |
| 236 // Bind(). | |
| 237 int SetMulticastOptions(); | |
| 238 int DoBind(const IPEndPoint& address); | |
| 239 // Binds to a random port on |address|. | |
| 240 int RandomBind(const IPAddressNumber& address); | |
| 241 | |
| 242 SOCKET socket_; | |
| 243 int addr_family_; | |
| 244 bool is_connected_; | |
| 245 | |
| 246 // Bitwise-or'd combination of SocketOptions. Specifies the set of | |
| 247 // options that should be applied to |socket_| before Bind(). | |
| 248 int socket_options_; | |
| 249 | |
| 250 // Multicast interface. | |
| 251 uint32 multicast_interface_; | |
| 252 | |
| 253 // Multicast socket options cached for SetMulticastOption. | |
| 254 // Cannot be used after Bind(). | |
| 255 int multicast_time_to_live_; | |
| 256 | |
| 257 // How to do source port binding, used only when UDPSocket is part of | |
| 258 // UDPClientSocket, since UDPServerSocket provides Bind. | |
| 259 DatagramSocket::BindType bind_type_; | |
| 260 | |
| 261 // PRNG function for generating port numbers. | |
| 262 RandIntCallback rand_int_cb_; | |
| 263 | |
| 264 // These are mutable since they're just cached copies to make | |
| 265 // GetPeerAddress/GetLocalAddress smarter. | |
| 266 mutable scoped_ptr<IPEndPoint> local_address_; | |
| 267 mutable scoped_ptr<IPEndPoint> remote_address_; | |
| 268 | |
| 269 // The core of the socket that can live longer than the socket itself. We pass | |
| 270 // resources to the Windows async IO functions and we have to make sure that | |
| 271 // they are not destroyed while the OS still references them. | |
| 272 scoped_refptr<Core> core_; | |
| 273 | |
| 274 // True if non-blocking IO is used. | |
| 275 bool use_non_blocking_io_; | |
| 276 | |
| 277 // Watches |read_write_event_|. | |
| 278 base::win::ObjectWatcher read_write_watcher_; | |
| 279 | |
| 280 // Events for read and write. | |
| 281 base::win::ScopedHandle read_write_event_; | |
| 282 | |
| 283 // The buffers used in Read() and Write(). | |
| 284 scoped_refptr<IOBuffer> read_iobuffer_; | |
| 285 scoped_refptr<IOBuffer> write_iobuffer_; | |
| 286 | |
| 287 int read_iobuffer_len_; | |
| 288 int write_iobuffer_len_; | |
| 289 | |
| 290 IPEndPoint* recv_from_address_; | |
| 291 | |
| 292 // Cached copy of the current address we're sending to, if any. Used for | |
| 293 // logging. | |
| 294 scoped_ptr<IPEndPoint> send_to_address_; | |
| 295 | |
| 296 // External callback; called when read is complete. | |
| 297 CompletionCallback read_callback_; | |
| 298 | |
| 299 // External callback; called when write is complete. | |
| 300 CompletionCallback write_callback_; | |
| 301 | |
| 302 BoundNetLog net_log_; | |
| 303 | |
| 304 // QWAVE data. Used to set DSCP bits on outgoing packets. | |
| 305 HANDLE qos_handle_; | |
| 306 QOS_FLOWID qos_flow_id_; | |
| 307 | |
| 308 DISALLOW_COPY_AND_ASSIGN(UDPSocketWin); | |
| 309 }; | |
| 310 | |
| 311 //----------------------------------------------------------------------------- | |
| 312 | |
| 313 // QWAVE (Quality Windows Audio/Video Experience) is the latest windows | |
| 314 // library for setting packet priorities (and other things). Unfortunately, | |
| 315 // Microsoft has decided that setting the DSCP bits with setsockopt() no | |
| 316 // longer works, so we have to use this API instead. | |
| 317 // This class is meant to be used as a singleton. It exposes a few dynamically | |
| 318 // loaded functions and a bool called "qwave_supported". | |
| 319 class NET_EXPORT QwaveAPI { | |
| 320 typedef BOOL (WINAPI *CreateHandleFn)(PQOS_VERSION, PHANDLE); | |
| 321 typedef BOOL (WINAPI *CloseHandleFn)(HANDLE); | |
| 322 typedef BOOL (WINAPI *AddSocketToFlowFn)( | |
| 323 HANDLE, SOCKET, PSOCKADDR, QOS_TRAFFIC_TYPE, DWORD, PQOS_FLOWID); | |
| 324 typedef BOOL (WINAPI *RemoveSocketFromFlowFn)( | |
| 325 HANDLE, SOCKET, QOS_FLOWID, DWORD); | |
| 326 typedef BOOL (WINAPI *SetFlowFn)( | |
| 327 HANDLE, QOS_FLOWID, QOS_SET_FLOW, ULONG, PVOID, DWORD, LPOVERLAPPED); | |
| 328 | |
| 329 public: | |
| 330 QwaveAPI(); | |
| 331 | |
| 332 static QwaveAPI& Get(); | |
| 333 | |
| 334 bool qwave_supported() const; | |
| 335 BOOL CreateHandle(PQOS_VERSION version, PHANDLE handle); | |
| 336 BOOL CloseHandle(HANDLE handle); | |
| 337 BOOL AddSocketToFlow(HANDLE handle, | |
| 338 SOCKET socket, | |
| 339 PSOCKADDR addr, | |
| 340 QOS_TRAFFIC_TYPE traffic_type, | |
| 341 DWORD flags, | |
| 342 PQOS_FLOWID flow_id); | |
| 343 BOOL RemoveSocketFromFlow(HANDLE handle, | |
| 344 SOCKET socket, | |
| 345 QOS_FLOWID flow_id, | |
| 346 DWORD reserved); | |
| 347 BOOL SetFlow(HANDLE handle, | |
| 348 QOS_FLOWID flow_id, | |
| 349 QOS_SET_FLOW op, | |
| 350 ULONG size, | |
| 351 PVOID data, | |
| 352 DWORD reserved, | |
| 353 LPOVERLAPPED overlapped); | |
| 354 | |
| 355 private: | |
| 356 bool qwave_supported_; | |
| 357 CreateHandleFn create_handle_func_; | |
| 358 CloseHandleFn close_handle_func_; | |
| 359 AddSocketToFlowFn add_socket_to_flow_func_; | |
| 360 RemoveSocketFromFlowFn remove_socket_from_flow_func_; | |
| 361 SetFlowFn set_flow_func_; | |
| 362 | |
| 363 FRIEND_TEST_ALL_PREFIXES(UDPSocketTest, SetDSCPFake); | |
| 364 DISALLOW_COPY_AND_ASSIGN(QwaveAPI); | |
| 365 }; | |
| 366 | |
| 367 | |
| 368 } // namespace net | |
| 369 | |
| 370 #endif // NET_UDP_UDP_SOCKET_WIN_H_ | |
| OLD | NEW |