2 * Copyright (c) 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "stream-fd.h"
23 #include <sys/types.h>
26 #include "fatal-signal.h"
27 #include "poll-loop.h"
28 #include "socket-util.h"
30 #include "stream-provider.h"
34 VLOG_DEFINE_THIS_MODULE(stream_fd_windows);
36 /* Active file descriptor stream. */
45 static const struct stream_class stream_fd_class;
47 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
49 /* Creates a new stream named 'name' that will send and receive data on 'fd'
50 * and stores a pointer to the stream in '*streamp'. Initial connection status
51 * 'connect_status' is interpreted as described for stream_init().
53 * Returns 0 if successful, otherwise a positive errno value. (The current
54 * implementation never fails.) */
56 new_fd_stream(const char *name, int fd, int connect_status,
57 struct stream **streamp)
61 s = xmalloc(sizeof *s);
62 stream_init(&s->stream, &stream_fd_class, connect_status, name);
64 s->wevent = CreateEvent(NULL, FALSE, FALSE, NULL);
65 *streamp = &s->stream;
69 static struct stream_fd *
70 stream_fd_cast(struct stream *stream)
72 stream_assert_class(stream, &stream_fd_class);
73 return CONTAINER_OF(stream, struct stream_fd, stream);
77 fd_close(struct stream *stream)
79 struct stream_fd *s = stream_fd_cast(stream);
80 WSAEventSelect(s->fd, NULL, 0);
81 CloseHandle(s->wevent);
87 fd_connect(struct stream *stream)
89 struct stream_fd *s = stream_fd_cast(stream);
90 return check_connection_completion(s->fd);
94 fd_recv(struct stream *stream, void *buffer, size_t n)
96 struct stream_fd *s = stream_fd_cast(stream);
99 retval = recv(s->fd, buffer, n, 0);
101 retval = -sock_errno();
103 if (retval == -WSAEWOULDBLOCK) {
110 fd_send(struct stream *stream, const void *buffer, size_t n)
112 struct stream_fd *s = stream_fd_cast(stream);
115 retval = send(s->fd, buffer, n, 0);
117 retval = -sock_errno();
119 if (retval == -WSAEWOULDBLOCK) {
127 fd_wait(struct stream *stream, enum stream_wait_type wait)
129 struct stream_fd *s = stream_fd_cast(stream);
133 poll_fd_wait_event(s->fd, s->wevent, POLLOUT);
137 poll_fd_wait_event(s->fd, s->wevent, POLLIN);
145 static const struct stream_class stream_fd_class = {
147 false, /* needs_probes */
149 fd_close, /* close */
150 fd_connect, /* connect */
158 /* Passive file descriptor stream. */
162 struct pstream pstream;
165 int (*accept_cb)(int fd, const struct sockaddr_storage *, size_t ss_len,
167 int (*set_dscp_cb)(int fd, uint8_t dscp);
171 static const struct pstream_class fd_pstream_class;
173 static struct fd_pstream *
174 fd_pstream_cast(struct pstream *pstream)
176 pstream_assert_class(pstream, &fd_pstream_class);
177 return CONTAINER_OF(pstream, struct fd_pstream, pstream);
180 /* Creates a new pstream named 'name' that will accept new socket connections
181 * on 'fd' and stores a pointer to the stream in '*pstreamp'.
183 * When a connection has been accepted, 'accept_cb' will be called with the new
184 * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
185 * accept_cb must return 0 if the connection is successful, in which case it
186 * must initialize '*streamp' to the new stream, or a positive errno value on
187 * error. In either case accept_cb takes ownership of the 'fd' passed in.
189 * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
190 * to fatal_signal_unlink_file_now() and freed with free().
192 * Returns 0 if successful, otherwise a positive errno value. (The current
193 * implementation never fails.) */
195 new_fd_pstream(const char *name, int fd,
196 int (*accept_cb)(int fd, const struct sockaddr_storage *ss,
197 size_t ss_len, struct stream **streamp),
198 int (*set_dscp_cb)(int fd, uint8_t dscp),
199 char *unlink_path, struct pstream **pstreamp)
201 struct fd_pstream *ps = xmalloc(sizeof *ps);
202 pstream_init(&ps->pstream, &fd_pstream_class, name);
204 ps->wevent = CreateEvent(NULL, FALSE, FALSE, NULL);
205 ps->accept_cb = accept_cb;
206 ps->set_dscp_cb = set_dscp_cb;
207 ps->unlink_path = unlink_path;
208 *pstreamp = &ps->pstream;
213 pfd_close(struct pstream *pstream)
215 struct fd_pstream *ps = fd_pstream_cast(pstream);
216 WSAEventSelect(ps->fd, NULL, 0);
217 CloseHandle(ps->wevent);
223 pfd_accept(struct pstream *pstream, struct stream **new_streamp)
225 struct fd_pstream *ps = fd_pstream_cast(pstream);
226 struct sockaddr_storage ss;
227 socklen_t ss_len = sizeof ss;
231 new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
233 retval = sock_errno();
234 if (retval == WSAEWOULDBLOCK) {
240 retval = set_nonblocking(new_fd);
246 return ps->accept_cb(new_fd, &ss, ss_len, new_streamp);
250 pfd_wait(struct pstream *pstream)
252 struct fd_pstream *ps = fd_pstream_cast(pstream);
253 poll_fd_wait_event(ps->fd, ps->wevent, POLLIN);
257 pfd_set_dscp(struct pstream *pstream, uint8_t dscp)
259 struct fd_pstream *ps = fd_pstream_cast(pstream);
260 if (ps->set_dscp_cb) {
261 return ps->set_dscp_cb(ps->fd, dscp);
266 static const struct pstream_class fd_pstream_class = {