diff options
Diffstat (limited to 'src/lib/libcrypto/bio/bss_sock.c')
-rw-r--r-- | src/lib/libcrypto/bio/bss_sock.c | 461 |
1 files changed, 461 insertions, 0 deletions
diff --git a/src/lib/libcrypto/bio/bss_sock.c b/src/lib/libcrypto/bio/bss_sock.c new file mode 100644 index 0000000000..d907a2867b --- /dev/null +++ b/src/lib/libcrypto/bio/bss_sock.c | |||
@@ -0,0 +1,461 @@ | |||
1 | /* crypto/bio/bss_sock.c */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
18 | * If this package is used in a product, Eric Young should be given attribution | ||
19 | * as the author of the parts of the library used. | ||
20 | * This can be in the form of a textual message at program startup or | ||
21 | * in documentation (online or textual) provided with the package. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
53 | * The licence and distribution terms for any publically available version or | ||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | |||
59 | #if !defined(NO_SOCK) || defined(BIO_FD) | ||
60 | |||
61 | #include <stdio.h> | ||
62 | #include <errno.h> | ||
63 | #define USE_SOCKETS | ||
64 | #include "cryptlib.h" | ||
65 | #include "bio.h" | ||
66 | |||
67 | #ifndef BIO_FD | ||
68 | #ifndef NOPROTO | ||
69 | static int sock_write(BIO *h,char *buf,int num); | ||
70 | static int sock_read(BIO *h,char *buf,int size); | ||
71 | static int sock_puts(BIO *h,char *str); | ||
72 | static long sock_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
73 | static int sock_new(BIO *h); | ||
74 | static int sock_free(BIO *data); | ||
75 | int BIO_sock_should_retry(int s); | ||
76 | #else | ||
77 | static int sock_write(); | ||
78 | static int sock_read(); | ||
79 | static int sock_puts(); | ||
80 | static long sock_ctrl(); | ||
81 | static int sock_new(); | ||
82 | static int sock_free(); | ||
83 | int BIO_sock_should_retry(); | ||
84 | #endif | ||
85 | |||
86 | #else | ||
87 | |||
88 | #ifndef NOPROTO | ||
89 | static int fd_write(BIO *h,char *buf,int num); | ||
90 | static int fd_read(BIO *h,char *buf,int size); | ||
91 | static int fd_puts(BIO *h,char *str); | ||
92 | static long fd_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
93 | static int fd_new(BIO *h); | ||
94 | static int fd_free(BIO *data); | ||
95 | int BIO_fd_should_retry(int s); | ||
96 | #else | ||
97 | static int fd_write(); | ||
98 | static int fd_read(); | ||
99 | static int fd_puts(); | ||
100 | static long fd_ctrl(); | ||
101 | static int fd_new(); | ||
102 | static int fd_free(); | ||
103 | int BIO_fd_should_retry(); | ||
104 | #endif | ||
105 | #endif | ||
106 | |||
107 | #ifndef BIO_FD | ||
108 | static BIO_METHOD methods_sockp= | ||
109 | { | ||
110 | BIO_TYPE_SOCKET, | ||
111 | "socket", | ||
112 | sock_write, | ||
113 | sock_read, | ||
114 | sock_puts, | ||
115 | NULL, /* sock_gets, */ | ||
116 | sock_ctrl, | ||
117 | sock_new, | ||
118 | sock_free, | ||
119 | }; | ||
120 | |||
121 | BIO_METHOD *BIO_s_socket() | ||
122 | { | ||
123 | return(&methods_sockp); | ||
124 | } | ||
125 | #else | ||
126 | static BIO_METHOD methods_fdp= | ||
127 | { | ||
128 | BIO_TYPE_FD,"file descriptor", | ||
129 | fd_write, | ||
130 | fd_read, | ||
131 | fd_puts, | ||
132 | NULL, /* fd_gets, */ | ||
133 | fd_ctrl, | ||
134 | fd_new, | ||
135 | fd_free, | ||
136 | }; | ||
137 | |||
138 | BIO_METHOD *BIO_s_fd() | ||
139 | { | ||
140 | return(&methods_fdp); | ||
141 | } | ||
142 | #endif | ||
143 | |||
144 | #ifndef BIO_FD | ||
145 | BIO *BIO_new_socket(fd,close_flag) | ||
146 | #else | ||
147 | BIO *BIO_new_fd(fd,close_flag) | ||
148 | #endif | ||
149 | int fd; | ||
150 | int close_flag; | ||
151 | { | ||
152 | BIO *ret; | ||
153 | |||
154 | #ifndef BIO_FD | ||
155 | ret=BIO_new(BIO_s_socket()); | ||
156 | #else | ||
157 | ret=BIO_new(BIO_s_fd()); | ||
158 | #endif | ||
159 | if (ret == NULL) return(NULL); | ||
160 | BIO_set_fd(ret,fd,close_flag); | ||
161 | return(ret); | ||
162 | } | ||
163 | |||
164 | #ifndef BIO_FD | ||
165 | static int sock_new(bi) | ||
166 | #else | ||
167 | static int fd_new(bi) | ||
168 | #endif | ||
169 | BIO *bi; | ||
170 | { | ||
171 | bi->init=0; | ||
172 | bi->num=0; | ||
173 | bi->ptr=NULL; | ||
174 | bi->flags=0; | ||
175 | return(1); | ||
176 | } | ||
177 | |||
178 | #ifndef BIO_FD | ||
179 | static int sock_free(a) | ||
180 | #else | ||
181 | static int fd_free(a) | ||
182 | #endif | ||
183 | BIO *a; | ||
184 | { | ||
185 | if (a == NULL) return(0); | ||
186 | if (a->shutdown) | ||
187 | { | ||
188 | if (a->init) | ||
189 | { | ||
190 | #ifndef BIO_FD | ||
191 | shutdown(a->num,2); | ||
192 | # ifdef WINDOWS | ||
193 | closesocket(a->num); | ||
194 | # else | ||
195 | close(a->num); | ||
196 | # endif | ||
197 | #else /* BIO_FD */ | ||
198 | close(a->num); | ||
199 | #endif | ||
200 | |||
201 | } | ||
202 | a->init=0; | ||
203 | a->flags=0; | ||
204 | } | ||
205 | return(1); | ||
206 | } | ||
207 | |||
208 | #ifndef BIO_FD | ||
209 | static int sock_read(b,out,outl) | ||
210 | #else | ||
211 | static int fd_read(b,out,outl) | ||
212 | #endif | ||
213 | BIO *b; | ||
214 | char *out; | ||
215 | int outl; | ||
216 | { | ||
217 | int ret=0; | ||
218 | |||
219 | if (out != NULL) | ||
220 | { | ||
221 | #if defined(WINDOWS) && !defined(BIO_FD) | ||
222 | clear_socket_error(); | ||
223 | ret=recv(b->num,out,outl,0); | ||
224 | #else | ||
225 | clear_sys_error(); | ||
226 | ret=read(b->num,out,outl); | ||
227 | #endif | ||
228 | BIO_clear_retry_flags(b); | ||
229 | if (ret <= 0) | ||
230 | { | ||
231 | #ifndef BIO_FD | ||
232 | if (BIO_sock_should_retry(ret)) | ||
233 | #else | ||
234 | if (BIO_fd_should_retry(ret)) | ||
235 | #endif | ||
236 | BIO_set_retry_read(b); | ||
237 | } | ||
238 | } | ||
239 | return(ret); | ||
240 | } | ||
241 | |||
242 | #ifndef BIO_FD | ||
243 | static int sock_write(b,in,inl) | ||
244 | #else | ||
245 | static int fd_write(b,in,inl) | ||
246 | #endif | ||
247 | BIO *b; | ||
248 | char *in; | ||
249 | int inl; | ||
250 | { | ||
251 | int ret; | ||
252 | |||
253 | #if defined(WINDOWS) && !defined(BIO_FD) | ||
254 | clear_socket_error(); | ||
255 | ret=send(b->num,in,inl,0); | ||
256 | #else | ||
257 | clear_sys_error(); | ||
258 | ret=write(b->num,in,inl); | ||
259 | #endif | ||
260 | BIO_clear_retry_flags(b); | ||
261 | if (ret <= 0) | ||
262 | { | ||
263 | #ifndef BIO_FD | ||
264 | if (BIO_sock_should_retry(ret)) | ||
265 | #else | ||
266 | if (BIO_fd_should_retry(ret)) | ||
267 | #endif | ||
268 | BIO_set_retry_write(b); | ||
269 | } | ||
270 | return(ret); | ||
271 | } | ||
272 | |||
273 | #ifndef BIO_FD | ||
274 | static long sock_ctrl(b,cmd,num,ptr) | ||
275 | #else | ||
276 | static long fd_ctrl(b,cmd,num,ptr) | ||
277 | #endif | ||
278 | BIO *b; | ||
279 | int cmd; | ||
280 | long num; | ||
281 | char *ptr; | ||
282 | { | ||
283 | long ret=1; | ||
284 | int *ip; | ||
285 | |||
286 | switch (cmd) | ||
287 | { | ||
288 | case BIO_CTRL_RESET: | ||
289 | #ifdef BIO_FD | ||
290 | ret=(long)lseek(b->num,0,0); | ||
291 | #else | ||
292 | ret=0; | ||
293 | #endif | ||
294 | break; | ||
295 | case BIO_CTRL_INFO: | ||
296 | ret=0; | ||
297 | break; | ||
298 | case BIO_C_SET_FD: | ||
299 | #ifndef BIO_FD | ||
300 | sock_free(b); | ||
301 | #else | ||
302 | fd_free(b); | ||
303 | #endif | ||
304 | b->num= *((int *)ptr); | ||
305 | b->shutdown=(int)num; | ||
306 | b->init=1; | ||
307 | break; | ||
308 | case BIO_C_GET_FD: | ||
309 | if (b->init) | ||
310 | { | ||
311 | ip=(int *)ptr; | ||
312 | if (ip != NULL) *ip=b->num; | ||
313 | ret=b->num; | ||
314 | } | ||
315 | else | ||
316 | ret= -1; | ||
317 | break; | ||
318 | case BIO_CTRL_GET_CLOSE: | ||
319 | ret=b->shutdown; | ||
320 | break; | ||
321 | case BIO_CTRL_SET_CLOSE: | ||
322 | b->shutdown=(int)num; | ||
323 | break; | ||
324 | case BIO_CTRL_PENDING: | ||
325 | case BIO_CTRL_WPENDING: | ||
326 | ret=0; | ||
327 | break; | ||
328 | case BIO_CTRL_DUP: | ||
329 | case BIO_CTRL_FLUSH: | ||
330 | ret=1; | ||
331 | break; | ||
332 | break; | ||
333 | default: | ||
334 | ret=0; | ||
335 | break; | ||
336 | } | ||
337 | return(ret); | ||
338 | } | ||
339 | |||
340 | #ifdef undef | ||
341 | static int sock_gets(bp,buf,size) | ||
342 | BIO *bp; | ||
343 | char *buf; | ||
344 | int size; | ||
345 | { | ||
346 | return(-1); | ||
347 | } | ||
348 | #endif | ||
349 | |||
350 | #ifndef BIO_FD | ||
351 | static int sock_puts(bp,str) | ||
352 | #else | ||
353 | static int fd_puts(bp,str) | ||
354 | #endif | ||
355 | BIO *bp; | ||
356 | char *str; | ||
357 | { | ||
358 | int n,ret; | ||
359 | |||
360 | n=strlen(str); | ||
361 | #ifndef BIO_FD | ||
362 | ret=sock_write(bp,str,n); | ||
363 | #else | ||
364 | ret=fd_write(bp,str,n); | ||
365 | #endif | ||
366 | return(ret); | ||
367 | } | ||
368 | |||
369 | #ifndef BIO_FD | ||
370 | int BIO_sock_should_retry(i) | ||
371 | #else | ||
372 | int BIO_fd_should_retry(i) | ||
373 | #endif | ||
374 | int i; | ||
375 | { | ||
376 | int err; | ||
377 | |||
378 | if ((i == 0) || (i == -1)) | ||
379 | { | ||
380 | #if !defined(BIO_FD) && defined(WINDOWS) | ||
381 | err=get_last_socket_error(); | ||
382 | #else | ||
383 | err=get_last_sys_error(); | ||
384 | #endif | ||
385 | |||
386 | #if defined(WINDOWS) /* more microsoft stupidity */ | ||
387 | if ((i == -1) && (err == 0)) | ||
388 | return(1); | ||
389 | #endif | ||
390 | |||
391 | #ifndef BIO_FD | ||
392 | return(BIO_sock_non_fatal_error(err)); | ||
393 | #else | ||
394 | return(BIO_fd_non_fatal_error(err)); | ||
395 | #endif | ||
396 | } | ||
397 | return(0); | ||
398 | } | ||
399 | |||
400 | #ifndef BIO_FD | ||
401 | int BIO_sock_non_fatal_error(err) | ||
402 | #else | ||
403 | int BIO_fd_non_fatal_error(err) | ||
404 | #endif | ||
405 | int err; | ||
406 | { | ||
407 | switch (err) | ||
408 | { | ||
409 | #if !defined(BIO_FD) && defined(WINDOWS) | ||
410 | # if defined(WSAEWOULDBLOCK) | ||
411 | case WSAEWOULDBLOCK: | ||
412 | # endif | ||
413 | |||
414 | # if defined(WSAENOTCONN) | ||
415 | case WSAENOTCONN: | ||
416 | # endif | ||
417 | #endif | ||
418 | |||
419 | #ifdef EWOULDBLOCK | ||
420 | # ifdef WSAEWOULDBLOCK | ||
421 | # if WSAEWOULDBLOCK != EWOULDBLOCK | ||
422 | case EWOULDBLOCK: | ||
423 | # endif | ||
424 | # else | ||
425 | case EWOULDBLOCK: | ||
426 | # endif | ||
427 | #endif | ||
428 | |||
429 | #if defined(ENOTCONN) | ||
430 | case ENOTCONN: | ||
431 | #endif | ||
432 | |||
433 | #ifdef EINTR | ||
434 | case EINTR: | ||
435 | #endif | ||
436 | |||
437 | #ifdef EAGAIN | ||
438 | #if EWOULDBLOCK != EAGAIN | ||
439 | case EAGAIN: | ||
440 | # endif | ||
441 | #endif | ||
442 | |||
443 | #ifdef EPROTO | ||
444 | case EPROTO: | ||
445 | #endif | ||
446 | |||
447 | #ifdef EINPROGRESS | ||
448 | case EINPROGRESS: | ||
449 | #endif | ||
450 | |||
451 | #ifdef EALREADY | ||
452 | case EALREADY: | ||
453 | #endif | ||
454 | return(1); | ||
455 | break; | ||
456 | default: | ||
457 | break; | ||
458 | } | ||
459 | return(0); | ||
460 | } | ||
461 | #endif | ||