Break out DllMain from crypto/cryptlib.c and use it in shared libs only
[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     struct fd_lookup_st *next;
67
68     if (ctx == NULL)
69         return;
70
71     curr = ctx->fds;
72     while (curr != NULL) {
73         if (!curr->del) {
74             /* Only try and cleanup if it hasn't been marked deleted */
75             if (curr->cleanup != NULL)
76                 curr->cleanup(ctx, curr->key, curr->fd, curr->custom_data);
77         }
78         /* Always free the fd_lookup_st */
79         next = curr->next;
80         OPENSSL_free(curr);
81         curr = next;
82     }
83
84     OPENSSL_free(ctx);
85 }
86 int ASYNC_WAIT_CTX_set_wait_fd(ASYNC_WAIT_CTX *ctx, const void *key,
87                                OSSL_ASYNC_FD fd, void *custom_data,
88                                void (*cleanup)(ASYNC_WAIT_CTX *, const void *,
89                                                OSSL_ASYNC_FD, void *))
90 {
91     struct fd_lookup_st *fdlookup;
92
93     fdlookup = OPENSSL_zalloc(sizeof *fdlookup);
94     if (fdlookup == NULL)
95         return 0;
96
97     fdlookup->key = key;
98     fdlookup->fd = fd;
99     fdlookup->custom_data = custom_data;
100     fdlookup->cleanup = cleanup;
101     fdlookup->add = 1;
102     fdlookup->next = ctx->fds;
103     ctx->fds = fdlookup;
104     ctx->numadd++;
105     return 1;
106 }
107
108 int ASYNC_WAIT_CTX_get_fd(ASYNC_WAIT_CTX *ctx, const void *key,
109                           OSSL_ASYNC_FD *fd, void **custom_data)
110 {
111     struct fd_lookup_st *curr;
112
113     curr = ctx->fds;
114     while (curr != NULL) {
115         if (curr->del) {
116             /* This one has been marked deleted so do nothing */
117             curr = curr->next;
118             continue;
119         }
120         if (curr->key == key) {
121             *fd = curr->fd;
122             *custom_data = curr->custom_data;
123             return 1;
124         }
125         curr = curr->next;
126     }
127     return 0;
128 }
129
130 int ASYNC_WAIT_CTX_get_all_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *fd,
131                                size_t *numfds)
132 {
133     struct fd_lookup_st *curr;
134
135     curr = ctx->fds;
136     *numfds = 0;
137     while (curr != NULL) {
138         if (curr->del) {
139             /* This one has been marked deleted so do nothing */
140             curr = curr->next;
141             continue;
142         }
143         if (fd != NULL) {
144             *fd = curr->fd;
145             fd++;
146         }
147         (*numfds)++;
148         curr = curr->next;
149     }
150     return 1;
151 }
152
153 int ASYNC_WAIT_CTX_get_changed_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *addfd,
154                                    size_t *numaddfds, OSSL_ASYNC_FD *delfd,
155                                    size_t *numdelfds)
156 {
157     struct fd_lookup_st *curr;
158
159     *numaddfds = ctx->numadd;
160     *numdelfds = ctx->numdel;
161     if (addfd == NULL && delfd == NULL)
162         return 1;
163
164     curr = ctx->fds;
165
166     while (curr != NULL) {
167         /* We ignore fds that have been marked as both added and deleted */
168         if (curr->del && !curr->add && (delfd != NULL)) {
169             *delfd = curr->fd;
170             delfd++;
171         }
172         if (curr->add && !curr->del && (addfd != NULL)) {
173             *addfd = curr->fd;
174             addfd++;
175         }
176         curr = curr->next;
177     }
178
179     return 1;
180 }
181
182 int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key)
183 {
184     struct fd_lookup_st *curr;
185
186     curr = ctx->fds;
187     while (curr != NULL) {
188         if (curr->del) {
189             /* This one has been marked deleted already so do nothing */
190             curr = curr->next;
191             continue;
192         }
193         if (curr->key == key) {
194             /*
195              * Mark it as deleted. We don't call cleanup if explicitly asked
196              * to clear an fd. We assume the caller is going to do that (if
197              * appropriate).
198              */
199             curr->del = 1;
200             ctx->numdel++;
201             return 1;
202         }
203         curr = curr->next;
204     }
205     return 0;
206 }
207
208 void async_wait_ctx_reset_counts(ASYNC_WAIT_CTX *ctx)
209 {
210     struct fd_lookup_st *curr, *prev = NULL;
211
212     ctx->numadd = 0;
213     ctx->numdel = 0;
214
215     curr = ctx->fds;
216
217     while (curr != NULL) {
218         if (curr->del) {
219             if (prev == NULL)
220                 ctx->fds = curr->next;
221             else
222                 prev->next = curr->next;
223             OPENSSL_free(curr);
224             if (prev == NULL)
225                 curr = ctx->fds;
226             else
227                 curr = prev->next;
228             continue;
229         }
230         if (curr->add) {
231             curr->add = 0;
232         }
233         prev = curr;
234         curr = curr->next;
235     }
236 }