Refactor the async wait fd logic
[openssl.git] / crypto / async / async_wait.c
1 /*
2  * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 2016 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    licensing@OpenSSL.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  */
52
53 /* This must be the first #include file */
54 #include "async_locl.h"
55
56 #include <openssl/err.h>
57
58 ASYNC_WAIT_CTX *ASYNC_WAIT_CTX_new(void)
59 {
60     return OPENSSL_zalloc(sizeof(ASYNC_WAIT_CTX));
61 }
62
63 void ASYNC_WAIT_CTX_free(ASYNC_WAIT_CTX *ctx)
64 {
65     struct fd_lookup_st *curr;
66
67     if (ctx == NULL)
68         return;
69
70     curr = ctx->fds;
71     while (curr != NULL) {
72         if (curr->del) {
73             /* This one has already been deleted so do nothing */
74             curr = curr->next;
75             continue;
76         }
77         if (curr->cleanup != NULL)
78             curr->cleanup(ctx, curr->key, curr->fd, curr->custom_data);
79         curr = curr->next;
80     }
81
82     OPENSSL_free(ctx);
83 }
84 int ASYNC_WAIT_CTX_set_wait_fd(ASYNC_WAIT_CTX *ctx, const void *key,
85                                OSSL_ASYNC_FD fd, void *custom_data,
86                                void (*cleanup)(ASYNC_WAIT_CTX *, const void *,
87                                                OSSL_ASYNC_FD, void *))
88 {
89     struct fd_lookup_st *fdlookup;
90
91     fdlookup = OPENSSL_zalloc(sizeof *fdlookup);
92     if (fdlookup == NULL)
93         return 0;
94
95     fdlookup->key = key;
96     fdlookup->fd = fd;
97     fdlookup->custom_data = custom_data;
98     fdlookup->cleanup = cleanup;
99     fdlookup->add = 1;
100     fdlookup->next = ctx->fds;
101     ctx->fds = fdlookup;
102     ctx->numadd++;
103     return 1;
104 }
105
106 int ASYNC_WAIT_CTX_get_fd(ASYNC_WAIT_CTX *ctx, const void *key,
107                           OSSL_ASYNC_FD *fd, void **custom_data)
108 {
109     struct fd_lookup_st *curr;
110
111     curr = ctx->fds;
112     while (curr != NULL) {
113         if (curr->del) {
114             /* This one has been marked deleted so do nothing */
115             curr = curr->next;
116             continue;
117         }
118         if (curr->key == key) {
119             *fd = curr->fd;
120             *custom_data = curr->custom_data;
121             return 1;
122         }
123         curr = curr->next;
124     }
125     return 0;
126 }
127
128 int ASYNC_WAIT_CTX_get_all_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *fd,
129                                size_t *numfds)
130 {
131     struct fd_lookup_st *curr;
132
133     curr = ctx->fds;
134     *numfds = 0;
135     while (curr != NULL) {
136         if (curr->del) {
137             /* This one has been marked deleted so do nothing */
138             curr = curr->next;
139             continue;
140         }
141         if (fd != NULL) {
142             *fd = curr->fd;
143             fd++;
144         }
145         (*numfds)++;
146         curr = curr->next;
147     }
148     return 1;
149 }
150
151 int ASYNC_WAIT_CTX_get_changed_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *addfd,
152                                    size_t *numaddfds, OSSL_ASYNC_FD *delfd,
153                                    size_t *numdelfds)
154 {
155     struct fd_lookup_st *curr;
156
157     *numaddfds = ctx->numadd;
158     *numdelfds = ctx->numdel;
159     if (addfd == NULL && delfd == NULL)
160         return 1;
161
162     curr = ctx->fds;
163
164     while (curr != NULL) {
165         /* We ignore fds that have been marked as both added and deleted */
166         if (curr->del && !curr->add && (delfd != NULL)) {
167             *delfd = curr->fd;
168             delfd++;
169         }
170         if (curr->add && !curr->del && (addfd != NULL)) {
171             *addfd = curr->fd;
172             addfd++;
173         }
174         curr = curr->next;
175     }
176
177     return 1;
178 }
179
180 int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key)
181 {
182     struct fd_lookup_st *curr;
183
184     curr = ctx->fds;
185     while (curr != NULL) {
186         if (curr->del) {
187             /* This one has been marked deleted already so do nothing */
188             curr = curr->next;
189             continue;
190         }
191         if (curr->key == key) {
192             /*
193              * Mark it as deleted. We don't call cleanup if explicitly asked
194              * to clear an fd. We assume the caller is going to do that
195              */
196             curr->del = 1;
197             ctx->numdel++;
198             return 1;
199         }
200         curr = curr->next;
201     }
202     return 0;
203 }
204
205 void async_wait_ctx_reset_counts(ASYNC_WAIT_CTX *ctx)
206 {
207     struct fd_lookup_st *curr, *prev = NULL;
208
209     ctx->numadd = 0;
210     ctx->numdel = 0;
211
212     curr = ctx->fds;
213
214     while (curr != NULL) {
215         if (curr->del) {
216             if (prev == NULL)
217                 ctx->fds = curr->next;
218             else
219                 prev->next = curr->next;
220             OPENSSL_free(curr);
221             if (prev == NULL)
222                 curr = ctx->fds;
223             else
224                 curr = prev->next;
225             continue;
226         }
227         if (curr->add) {
228             curr->add = 0;
229         }
230         prev = curr;
231         curr = curr->next;
232     }
233 }