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 #ifndef NET_UDP_UDP_SOCKET_WIN_H_ | 5 #ifndef NET_UDP_UDP_SOCKET_WIN_H_ |
6 #define NET_UDP_UDP_SOCKET_WIN_H_ | 6 #define NET_UDP_UDP_SOCKET_WIN_H_ |
7 | 7 |
8 #include <qos2.h> | 8 #include <qos2.h> |
9 #include <winsock2.h> | 9 #include <winsock2.h> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
14 #include "base/win/object_watcher.h" | 14 #include "base/win/object_watcher.h" |
| 15 #include "net/base/address_family.h" |
15 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
16 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
17 #include "net/base/rand_callback.h" | 18 #include "net/base/rand_callback.h" |
18 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
19 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
20 #include "net/base/net_log.h" | 21 #include "net/base/net_log.h" |
21 #include "net/udp/datagram_socket.h" | 22 #include "net/udp/datagram_socket.h" |
22 | 23 |
23 namespace net { | 24 namespace net { |
24 | 25 |
25 class NET_EXPORT UDPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 26 class NET_EXPORT UDPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
26 public: | 27 public: |
27 UDPSocketWin(DatagramSocket::BindType bind_type, | 28 UDPSocketWin(DatagramSocket::BindType bind_type, |
28 const RandIntCallback& rand_int_cb, | 29 const RandIntCallback& rand_int_cb, |
29 net::NetLog* net_log, | 30 net::NetLog* net_log, |
30 const net::NetLog::Source& source); | 31 const net::NetLog::Source& source); |
31 virtual ~UDPSocketWin(); | 32 virtual ~UDPSocketWin(); |
32 | 33 |
| 34 // Opens the socket. |
| 35 // Returns a net error code. |
| 36 int Open(AddressFamily address_family); |
| 37 |
33 // Connect the socket to connect with a certain |address|. | 38 // Connect the socket to connect with a certain |address|. |
| 39 // Should be called after Open(). |
34 // Returns a net error code. | 40 // Returns a net error code. |
35 int Connect(const IPEndPoint& address); | 41 int Connect(const IPEndPoint& address); |
36 | 42 |
37 // Bind the address/port for this socket to |address|. This is generally | 43 // Bind the address/port for this socket to |address|. This is generally |
38 // only used on a server. | 44 // only used on a server. Should be called after Open(). |
39 // Returns a net error code. | 45 // Returns a net error code. |
40 int Bind(const IPEndPoint& address); | 46 int Bind(const IPEndPoint& address); |
41 | 47 |
42 // Close the socket. | 48 // Close the socket. |
43 void Close(); | 49 void Close(); |
44 | 50 |
45 // Copy the remote udp address into |address| and return a network error code. | 51 // Copy the remote udp address into |address| and return a net error code. |
46 int GetPeerAddress(IPEndPoint* address) const; | 52 int GetPeerAddress(IPEndPoint* address) const; |
47 | 53 |
48 // Copy the local udp address into |address| and return a network error code. | 54 // Copy the local udp address into |address| and return a net error code. |
49 // (similar to getsockname) | 55 // (similar to getsockname) |
50 int GetLocalAddress(IPEndPoint* address) const; | 56 int GetLocalAddress(IPEndPoint* address) const; |
51 | 57 |
52 // IO: | 58 // IO: |
53 // Multiple outstanding read requests are not supported. | 59 // Multiple outstanding read requests are not supported. |
54 // Full duplex mode (reading and writing at the same time) is supported | 60 // Full duplex mode (reading and writing at the same time) is supported |
55 | 61 |
56 // Read from the socket. | 62 // Read from the socket. |
57 // Only usable from the client-side of a UDP socket, after the socket | 63 // Only usable from the client-side of a UDP socket, after the socket |
58 // has been connected. | 64 // has been connected. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 103 |
98 // Set the receive buffer size (in bytes) for the socket. | 104 // Set the receive buffer size (in bytes) for the socket. |
99 // Returns a net error code. | 105 // Returns a net error code. |
100 int SetReceiveBufferSize(int32 size); | 106 int SetReceiveBufferSize(int32 size); |
101 | 107 |
102 // Set the send buffer size (in bytes) for the socket. | 108 // Set the send buffer size (in bytes) for the socket. |
103 // Returns a net error code. | 109 // Returns a net error code. |
104 int SetSendBufferSize(int32 size); | 110 int SetSendBufferSize(int32 size); |
105 | 111 |
106 // Returns true if the socket is already connected or bound. | 112 // Returns true if the socket is already connected or bound. |
107 bool is_connected() const { return socket_ != INVALID_SOCKET; } | 113 bool is_connected() const { return is_connected_; } |
108 | 114 |
109 const BoundNetLog& NetLog() const { return net_log_; } | 115 const BoundNetLog& NetLog() const { return net_log_; } |
110 | 116 |
111 // Sets corresponding flags in |socket_options_| to allow the socket | 117 // Sets corresponding flags in |socket_options_| to allow the socket |
112 // to share the local address to which the socket will be bound with | 118 // to share the local address to which the socket will be bound with |
113 // other processes. Should be called before Bind(). | 119 // other processes. Should be called before Bind(). |
114 void AllowAddressReuse(); | 120 int AllowAddressReuse(); |
115 | 121 |
116 // Sets corresponding flags in |socket_options_| to allow sending | 122 // Sets corresponding flags in |socket_options_| to allow sending |
117 // and receiving packets to and from broadcast addresses. Should be | 123 // and receiving packets to and from broadcast addresses. |
118 // called before Bind(). | 124 int SetBroadcast(bool broadcast); |
119 void AllowBroadcast(); | |
120 | 125 |
121 // Join the multicast group. | 126 // Join the multicast group. |
122 // |group_address| is the group address to join, could be either | 127 // |group_address| is the group address to join, could be either |
123 // an IPv4 or IPv6 address. | 128 // an IPv4 or IPv6 address. |
124 // Return a network error code. | 129 // Return a net error code. |
125 int JoinGroup(const IPAddressNumber& group_address) const; | 130 int JoinGroup(const IPAddressNumber& group_address) const; |
126 | 131 |
127 // Leave the multicast group. | 132 // Leave the multicast group. |
128 // |group_address| is the group address to leave, could be either | 133 // |group_address| is the group address to leave, could be either |
129 // an IPv4 or IPv6 address. If the socket hasn't joined the group, | 134 // an IPv4 or IPv6 address. If the socket hasn't joined the group, |
130 // it will be ignored. | 135 // it will be ignored. |
131 // It's optional to leave the multicast group before destroying | 136 // It's optional to leave the multicast group before destroying |
132 // the socket. It will be done by the OS. | 137 // the socket. It will be done by the OS. |
133 // Return a network error code. | 138 // Return a net error code. |
134 int LeaveGroup(const IPAddressNumber& group_address) const; | 139 int LeaveGroup(const IPAddressNumber& group_address) const; |
135 | 140 |
136 // Set interface to use for multicast. If |interface_index| set to 0, default | 141 // Set interface to use for multicast. If |interface_index| set to 0, default |
137 // interface is used. | 142 // interface is used. |
138 // Should be called before Bind(). | 143 // Should be called before Bind(). |
139 // Returns a network error code. | 144 // Returns a net error code. |
140 int SetMulticastInterface(uint32 interface_index); | 145 int SetMulticastInterface(uint32 interface_index); |
141 | 146 |
142 // Set the time-to-live option for UDP packets sent to the multicast | 147 // Set the time-to-live option for UDP packets sent to the multicast |
143 // group address. The default value of this option is 1. | 148 // group address. The default value of this option is 1. |
144 // Cannot be negative or more than 255. | 149 // Cannot be negative or more than 255. |
145 // Should be called before Bind(). | 150 // Should be called before Bind(). |
146 int SetMulticastTimeToLive(int time_to_live); | 151 int SetMulticastTimeToLive(int time_to_live); |
147 | 152 |
148 // Set the loopback flag for UDP socket. If this flag is true, the host | 153 // Set the loopback flag for UDP socket. If this flag is true, the host |
149 // will receive packets sent to the joined group from itself. | 154 // will receive packets sent to the joined group from itself. |
(...skipping 12 matching lines...) Expand all Loading... |
162 | 167 |
163 // Set the differentiated services flags on outgoing packets. May not | 168 // Set the differentiated services flags on outgoing packets. May not |
164 // do anything on some platforms. | 169 // do anything on some platforms. |
165 int SetDiffServCodePoint(DiffServCodePoint dscp); | 170 int SetDiffServCodePoint(DiffServCodePoint dscp); |
166 | 171 |
167 // Resets the thread to be used for thread-safety checks. | 172 // Resets the thread to be used for thread-safety checks. |
168 void DetachFromThread(); | 173 void DetachFromThread(); |
169 | 174 |
170 private: | 175 private: |
171 enum SocketOptions { | 176 enum SocketOptions { |
172 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, | 177 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 |
173 SOCKET_OPTION_BROADCAST = 1 << 1, | |
174 SOCKET_OPTION_MULTICAST_LOOP = 1 << 2 | |
175 }; | 178 }; |
176 | 179 |
177 class Core; | 180 class Core; |
178 | 181 |
179 void DoReadCallback(int rv); | 182 void DoReadCallback(int rv); |
180 void DoWriteCallback(int rv); | 183 void DoWriteCallback(int rv); |
181 void DidCompleteRead(); | 184 void DidCompleteRead(); |
182 void DidCompleteWrite(); | 185 void DidCompleteWrite(); |
183 | 186 |
184 // Handles stats and logging. |result| is the number of bytes transferred, on | 187 // Handles stats and logging. |result| is the number of bytes transferred, on |
185 // success, or the net error code on failure. LogRead retrieves the address | 188 // success, or the net error code on failure. LogRead retrieves the address |
186 // from |recv_addr_storage_|, while LogWrite takes it as an optional argument. | 189 // from |recv_addr_storage_|, while LogWrite takes it as an optional argument. |
187 void LogRead(int result, const char* bytes) const; | 190 void LogRead(int result, const char* bytes) const; |
188 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; | 191 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; |
189 | 192 |
190 // Returns the OS error code (or 0 on success). | |
191 int CreateSocket(int addr_family); | |
192 | |
193 // Same as SendTo(), except that address is passed by pointer | 193 // Same as SendTo(), except that address is passed by pointer |
194 // instead of by reference. It is called from Write() with |address| | 194 // instead of by reference. It is called from Write() with |address| |
195 // set to NULL. | 195 // set to NULL. |
196 int SendToOrWrite(IOBuffer* buf, | 196 int SendToOrWrite(IOBuffer* buf, |
197 int buf_len, | 197 int buf_len, |
198 const IPEndPoint* address, | 198 const IPEndPoint* address, |
199 const CompletionCallback& callback); | 199 const CompletionCallback& callback); |
200 | 200 |
201 int InternalConnect(const IPEndPoint& address); | 201 int InternalConnect(const IPEndPoint& address); |
202 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); | 202 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
203 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); | 203 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
204 | 204 |
205 // Applies |socket_options_| to |socket_|. Should be called before | 205 // Applies |socket_options_| to |socket_|. Should be called before |
206 // Bind(). | 206 // Bind(). |
207 int SetSocketOptions(); | 207 int SetMulticastOptions(); |
208 int DoBind(const IPEndPoint& address); | 208 int DoBind(const IPEndPoint& address); |
209 // Binds to a random port on |address|. | 209 // Binds to a random port on |address|. |
210 int RandomBind(const IPAddressNumber& address); | 210 int RandomBind(const IPAddressNumber& address); |
211 | 211 |
212 // Attempts to convert the data in |recv_addr_storage_| and |recv_addr_len_| | 212 // Attempts to convert the data in |recv_addr_storage_| and |recv_addr_len_| |
213 // to an IPEndPoint and writes it to |address|. Returns true on success. | 213 // to an IPEndPoint and writes it to |address|. Returns true on success. |
214 bool ReceiveAddressToIPEndpoint(IPEndPoint* address) const; | 214 bool ReceiveAddressToIPEndpoint(IPEndPoint* address) const; |
215 | 215 |
216 SOCKET socket_; | 216 SOCKET socket_; |
217 int addr_family_; | 217 int addr_family_; |
| 218 bool is_connected_; |
218 | 219 |
219 // Bitwise-or'd combination of SocketOptions. Specifies the set of | 220 // Bitwise-or'd combination of SocketOptions. Specifies the set of |
220 // options that should be applied to |socket_| before Bind(). | 221 // options that should be applied to |socket_| before Bind(). |
221 int socket_options_; | 222 int socket_options_; |
222 | 223 |
223 // Multicast interface. | 224 // Multicast interface. |
224 uint32 multicast_interface_; | 225 uint32 multicast_interface_; |
225 | 226 |
226 // Multicast socket options cached for SetSocketOption. | 227 // Multicast socket options cached for SetMulticastOption. |
227 // Cannot be used after Bind(). | 228 // Cannot be used after Bind(). |
228 int multicast_time_to_live_; | 229 int multicast_time_to_live_; |
229 | 230 |
230 // How to do source port binding, used only when UDPSocket is part of | 231 // How to do source port binding, used only when UDPSocket is part of |
231 // UDPClientSocket, since UDPServerSocket provides Bind. | 232 // UDPClientSocket, since UDPServerSocket provides Bind. |
232 DatagramSocket::BindType bind_type_; | 233 DatagramSocket::BindType bind_type_; |
233 | 234 |
234 // PRNG function for generating port numbers. | 235 // PRNG function for generating port numbers. |
235 RandIntCallback rand_int_cb_; | 236 RandIntCallback rand_int_cb_; |
236 | 237 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 SetFlowFn set_flow_func_; | 319 SetFlowFn set_flow_func_; |
319 | 320 |
320 FRIEND_TEST_ALL_PREFIXES(UDPSocketTest, SetDSCPFake); | 321 FRIEND_TEST_ALL_PREFIXES(UDPSocketTest, SetDSCPFake); |
321 DISALLOW_COPY_AND_ASSIGN(QwaveAPI); | 322 DISALLOW_COPY_AND_ASSIGN(QwaveAPI); |
322 }; | 323 }; |
323 | 324 |
324 | 325 |
325 } // namespace net | 326 } // namespace net |
326 | 327 |
327 #endif // NET_UDP_UDP_SOCKET_WIN_H_ | 328 #endif // NET_UDP_UDP_SOCKET_WIN_H_ |
OLD | NEW |