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. */
44 static const struct stream_class stream_fd_class
;
46 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 25);
48 /* Creates a new stream named 'name' that will send and receive data on 'fd'
49 * and stores a pointer to the stream in '*streamp'. Initial connection status
50 * 'connect_status' is interpreted as described for stream_init().
52 * Returns 0 if successful, otherwise a positive errno value. (The current
53 * implementation never fails.) */
55 new_fd_stream(const char *name
, int fd
, int connect_status
,
56 struct stream
**streamp
)
60 s
= xmalloc(sizeof *s
);
61 stream_init(&s
->stream
, &stream_fd_class
, connect_status
, name
);
63 *streamp
= &s
->stream
;
67 static struct stream_fd
*
68 stream_fd_cast(struct stream
*stream
)
70 stream_assert_class(stream
, &stream_fd_class
);
71 return CONTAINER_OF(stream
, struct stream_fd
, stream
);
75 fd_close(struct stream
*stream
)
77 struct stream_fd
*s
= stream_fd_cast(stream
);
83 fd_connect(struct stream
*stream
)
85 struct stream_fd
*s
= stream_fd_cast(stream
);
86 return check_connection_completion(s
->fd
);
90 fd_recv(struct stream
*stream
, void *buffer
, size_t n
)
92 struct stream_fd
*s
= stream_fd_cast(stream
);
95 retval
= recv(s
->fd
, buffer
, n
, 0);
97 retval
= -sock_errno();
99 if (retval
== -WSAEWOULDBLOCK
) {
106 fd_send(struct stream
*stream
, const void *buffer
, size_t n
)
108 struct stream_fd
*s
= stream_fd_cast(stream
);
111 retval
= send(s
->fd
, buffer
, n
, 0);
113 retval
= -sock_errno();
115 if (retval
== -WSAEWOULDBLOCK
) {
123 fd_wait(struct stream
*stream
, enum stream_wait_type wait
)
125 struct stream_fd
*s
= stream_fd_cast(stream
);
129 poll_fd_wait(s
->fd
, POLLOUT
);
133 poll_fd_wait(s
->fd
, POLLIN
);
141 static const struct stream_class stream_fd_class
= {
143 false, /* needs_probes */
145 fd_close
, /* close */
146 fd_connect
, /* connect */
154 /* Passive file descriptor stream. */
158 struct pstream pstream
;
160 int (*accept_cb
)(int fd
, const struct sockaddr_storage
*, size_t ss_len
,
162 int (*set_dscp_cb
)(int fd
, uint8_t dscp
);
166 static const struct pstream_class fd_pstream_class
;
168 static struct fd_pstream
*
169 fd_pstream_cast(struct pstream
*pstream
)
171 pstream_assert_class(pstream
, &fd_pstream_class
);
172 return CONTAINER_OF(pstream
, struct fd_pstream
, pstream
);
175 /* Creates a new pstream named 'name' that will accept new socket connections
176 * on 'fd' and stores a pointer to the stream in '*pstreamp'.
178 * When a connection has been accepted, 'accept_cb' will be called with the new
179 * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
180 * accept_cb must return 0 if the connection is successful, in which case it
181 * must initialize '*streamp' to the new stream, or a positive errno value on
182 * error. In either case accept_cb takes ownership of the 'fd' passed in.
184 * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
185 * to fatal_signal_unlink_file_now() and freed with free().
187 * Returns 0 if successful, otherwise a positive errno value. (The current
188 * implementation never fails.) */
190 new_fd_pstream(const char *name
, int fd
,
191 int (*accept_cb
)(int fd
, const struct sockaddr_storage
*ss
,
192 size_t ss_len
, struct stream
**streamp
),
193 int (*set_dscp_cb
)(int fd
, uint8_t dscp
),
194 char *unlink_path
, struct pstream
**pstreamp
)
196 struct fd_pstream
*ps
= xmalloc(sizeof *ps
);
197 pstream_init(&ps
->pstream
, &fd_pstream_class
, name
);
199 ps
->accept_cb
= accept_cb
;
200 ps
->set_dscp_cb
= set_dscp_cb
;
201 ps
->unlink_path
= unlink_path
;
202 *pstreamp
= &ps
->pstream
;
207 pfd_close(struct pstream
*pstream
)
209 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
211 if (ps
->unlink_path
) {
212 fatal_signal_unlink_file_now(ps
->unlink_path
);
213 free(ps
->unlink_path
);
219 pfd_accept(struct pstream
*pstream
, struct stream
**new_streamp
)
221 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
222 struct sockaddr_storage ss
;
223 socklen_t ss_len
= sizeof ss
;
227 new_fd
= accept(ps
->fd
, (struct sockaddr
*) &ss
, &ss_len
);
229 retval
= sock_errno();
230 if (retval
== WSAEWOULDBLOCK
) {
236 retval
= set_nonblocking(new_fd
);
242 return ps
->accept_cb(new_fd
, &ss
, ss_len
, new_streamp
);
246 pfd_wait(struct pstream
*pstream
)
248 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
249 poll_fd_wait(ps
->fd
, POLLIN
);
253 pfd_set_dscp(struct pstream
*pstream
, uint8_t dscp
)
255 struct fd_pstream
*ps
= fd_pstream_cast(pstream
);
256 if (ps
->set_dscp_cb
) {
257 return ps
->set_dscp_cb(ps
->fd
, dscp
);
262 static const struct pstream_class fd_pstream_class
= {