stream-tcp, stream-ssl: Remove unneeded getsockname() calls.
[cascardo/ovs.git] / lib / stream-fd.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "stream-fd.h"
19 #include <errno.h>
20 #include <poll.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include "fatal-signal.h"
27 #include "poll-loop.h"
28 #include "socket-util.h"
29 #include "util.h"
30 #include "stream-provider.h"
31 #include "stream.h"
32 #include "vlog.h"
33
34 VLOG_DEFINE_THIS_MODULE(stream_fd);
35
36 /* Active file descriptor stream. */
37
38 struct stream_fd
39 {
40     struct stream stream;
41     int fd;
42 };
43
44 static const struct stream_class stream_fd_class;
45
46 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
47
48 static void maybe_unlink_and_free(char *path);
49
50 /* Creates a new stream named 'name' that will send and receive data on 'fd'
51  * and stores a pointer to the stream in '*streamp'.  Initial connection status
52  * 'connect_status' is interpreted as described for stream_init().
53  *
54  * Returns 0 if successful, otherwise a positive errno value.  (The current
55  * implementation never fails.) */
56 int
57 new_fd_stream(const char *name, int fd, int connect_status,
58               struct stream **streamp)
59 {
60     struct stream_fd *s;
61
62     s = xmalloc(sizeof *s);
63     stream_init(&s->stream, &stream_fd_class, connect_status, name);
64     s->fd = fd;
65     *streamp = &s->stream;
66     return 0;
67 }
68
69 static struct stream_fd *
70 stream_fd_cast(struct stream *stream)
71 {
72     stream_assert_class(stream, &stream_fd_class);
73     return CONTAINER_OF(stream, struct stream_fd, stream);
74 }
75
76 static void
77 fd_close(struct stream *stream)
78 {
79     struct stream_fd *s = stream_fd_cast(stream);
80     closesocket(s->fd);
81     free(s);
82 }
83
84 static int
85 fd_connect(struct stream *stream)
86 {
87     struct stream_fd *s = stream_fd_cast(stream);
88     return check_connection_completion(s->fd);
89 }
90
91 static ssize_t
92 fd_recv(struct stream *stream, void *buffer, size_t n)
93 {
94     struct stream_fd *s = stream_fd_cast(stream);
95     ssize_t retval;
96     int error;
97
98     retval = recv(s->fd, buffer, n, 0);
99     if (retval < 0) {
100         error = sock_errno();
101 #ifdef _WIN32
102         if (error == WSAEWOULDBLOCK) {
103            error = EAGAIN;
104         }
105 #endif
106         if (error != EAGAIN) {
107             VLOG_DBG_RL(&rl, "recv: %s", sock_strerror(error));
108         }
109         return -error;
110     }
111     return retval;
112 }
113
114 static ssize_t
115 fd_send(struct stream *stream, const void *buffer, size_t n)
116 {
117     struct stream_fd *s = stream_fd_cast(stream);
118     ssize_t retval;
119     int error;
120
121     retval = send(s->fd, buffer, n, 0);
122     if (retval < 0) {
123         error = sock_errno();
124 #ifdef _WIN32
125         if (error == WSAEWOULDBLOCK) {
126            error = EAGAIN;
127         }
128 #endif
129         if (error != EAGAIN) {
130             VLOG_DBG_RL(&rl, "recv: %s", sock_strerror(error));
131         }
132         return -error;
133     }
134     return (retval > 0 ? retval : -EAGAIN);
135 }
136
137 static void
138 fd_wait(struct stream *stream, enum stream_wait_type wait)
139 {
140     struct stream_fd *s = stream_fd_cast(stream);
141     switch (wait) {
142     case STREAM_CONNECT:
143     case STREAM_SEND:
144         poll_fd_wait(s->fd, POLLOUT);
145         break;
146
147     case STREAM_RECV:
148         poll_fd_wait(s->fd, POLLIN);
149         break;
150
151     default:
152         OVS_NOT_REACHED();
153     }
154 }
155
156 static const struct stream_class stream_fd_class = {
157     "fd",                       /* name */
158     false,                      /* needs_probes */
159     NULL,                       /* open */
160     fd_close,                   /* close */
161     fd_connect,                 /* connect */
162     fd_recv,                    /* recv */
163     fd_send,                    /* send */
164     NULL,                       /* run */
165     NULL,                       /* run_wait */
166     fd_wait,                    /* wait */
167 };
168 \f
169 /* Passive file descriptor stream. */
170
171 struct fd_pstream
172 {
173     struct pstream pstream;
174     int fd;
175     int (*accept_cb)(int fd, const struct sockaddr_storage *, size_t ss_len,
176                      struct stream **);
177     int (*set_dscp_cb)(int fd, uint8_t dscp);
178     char *unlink_path;
179 };
180
181 static const struct pstream_class fd_pstream_class;
182
183 static struct fd_pstream *
184 fd_pstream_cast(struct pstream *pstream)
185 {
186     pstream_assert_class(pstream, &fd_pstream_class);
187     return CONTAINER_OF(pstream, struct fd_pstream, pstream);
188 }
189
190 /* Creates a new pstream named 'name' that will accept new socket connections
191  * on 'fd' and stores a pointer to the stream in '*pstreamp'.
192  *
193  * When a connection has been accepted, 'accept_cb' will be called with the new
194  * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
195  * accept_cb must return 0 if the connection is successful, in which case it
196  * must initialize '*streamp' to the new stream, or a positive errno value on
197  * error.  In either case accept_cb takes ownership of the 'fd' passed in.
198  *
199  * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
200  * to fatal_signal_unlink_file_now() and freed with free().
201  *
202  * Returns 0 if successful, otherwise a positive errno value.  (The current
203  * implementation never fails.) */
204 int
205 new_fd_pstream(const char *name, int fd,
206                int (*accept_cb)(int fd, const struct sockaddr_storage *ss,
207                                 size_t ss_len, struct stream **streamp),
208                int (*set_dscp_cb)(int fd, uint8_t dscp),
209                char *unlink_path, struct pstream **pstreamp)
210 {
211     struct fd_pstream *ps = xmalloc(sizeof *ps);
212     pstream_init(&ps->pstream, &fd_pstream_class, name);
213     ps->fd = fd;
214     ps->accept_cb = accept_cb;
215     ps->set_dscp_cb = set_dscp_cb;
216     ps->unlink_path = unlink_path;
217     *pstreamp = &ps->pstream;
218     return 0;
219 }
220
221 static void
222 pfd_close(struct pstream *pstream)
223 {
224     struct fd_pstream *ps = fd_pstream_cast(pstream);
225     closesocket(ps->fd);
226     maybe_unlink_and_free(ps->unlink_path);
227     free(ps);
228 }
229
230 static int
231 pfd_accept(struct pstream *pstream, struct stream **new_streamp)
232 {
233     struct fd_pstream *ps = fd_pstream_cast(pstream);
234     struct sockaddr_storage ss;
235     socklen_t ss_len = sizeof ss;
236     int new_fd;
237     int retval;
238
239     new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
240     if (new_fd < 0) {
241         retval = sock_errno();
242 #ifdef _WIN32
243         if (retval == WSAEWOULDBLOCK) {
244             retval = EAGAIN;
245         }
246 #endif
247         if (retval != EAGAIN) {
248             VLOG_DBG_RL(&rl, "accept: %s", sock_strerror(retval));
249         }
250         return retval;
251     }
252
253     retval = set_nonblocking(new_fd);
254     if (retval) {
255         closesocket(new_fd);
256         return retval;
257     }
258
259     return ps->accept_cb(new_fd, &ss, ss_len, new_streamp);
260 }
261
262 static void
263 pfd_wait(struct pstream *pstream)
264 {
265     struct fd_pstream *ps = fd_pstream_cast(pstream);
266     poll_fd_wait(ps->fd, POLLIN);
267 }
268
269 static int
270 pfd_set_dscp(struct pstream *pstream, uint8_t dscp)
271 {
272     struct fd_pstream *ps = fd_pstream_cast(pstream);
273     if (ps->set_dscp_cb) {
274         return ps->set_dscp_cb(ps->fd, dscp);
275     }
276     return 0;
277 }
278
279 static const struct pstream_class fd_pstream_class = {
280     "pstream",
281     false,
282     NULL,
283     pfd_close,
284     pfd_accept,
285     pfd_wait,
286     pfd_set_dscp,
287 };
288 \f
289 /* Helper functions. */
290 static void
291 maybe_unlink_and_free(char *path)
292 {
293     if (path) {
294         fatal_signal_unlink_file_now(path);
295         free(path);
296     }
297 }