]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bss_sock.c
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / crypto / bio / bss_sock.c
1 /* crypto/bio/bss_sock.c */
2 /* Copyright (C) 1995-1997 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,"socket",
111 sock_write,
112 sock_read,
113 sock_puts,
114 NULL, /* sock_gets, */
115 sock_ctrl,
116 sock_new,
117 sock_free,
118 };
119
120 BIO_METHOD *BIO_s_socket()
121 {
122 return(&methods_sockp);
123 }
124 #else
125 static BIO_METHOD methods_fdp=
126 {
127 BIO_TYPE_FD,"file descriptor",
128 fd_write,
129 fd_read,
130 fd_puts,
131 NULL, /* fd_gets, */
132 fd_ctrl,
133 fd_new,
134 fd_free,
135 };
136
137 BIO_METHOD *BIO_s_fd()
138 {
139 return(&methods_fdp);
140 }
141 #endif
142
143 #ifndef BIO_FD
144 BIO *BIO_new_socket(fd,close_flag)
145 #else
146 BIO *BIO_new_fd(fd,close_flag)
147 #endif
148 int fd;
149 int close_flag;
150 {
151 BIO *ret;
152
153 #ifndef BIO_FD
154 ret=BIO_new(BIO_s_socket());
155 #else
156 ret=BIO_new(BIO_s_fd());
157 #endif
158 if (ret == NULL) return(NULL);
159 BIO_set_fd(ret,fd,close_flag);
160 return(ret);
161 }
162
163 #ifndef BIO_FD
164 static int sock_new(bi)
165 #else
166 static int fd_new(bi)
167 #endif
168 BIO *bi;
169 {
170 bi->init=0;
171 bi->num=0;
172 bi->ptr=NULL;
173 bi->flags=0;
174 return(1);
175 }
176
177 #ifndef BIO_FD
178 static int sock_free(a)
179 #else
180 static int fd_free(a)
181 #endif
182 BIO *a;
183 {
184 if (a == NULL) return(0);
185 if (a->shutdown)
186 {
187 if (a->init)
188 {
189 #ifndef BIO_FD
190 shutdown(a->num,2);
191 # ifdef WINDOWS
192 closesocket(a->num);
193 # else
194 close(a->num);
195 # endif
196 #else /* BIO_FD */
197 close(a->num);
198 #endif
199
200 }
201 a->init=0;
202 a->flags=0;
203 }
204 return(1);
205 }
206
207 #ifndef BIO_FD
208 static int sock_read(b,out,outl)
209 #else
210 static int fd_read(b,out,outl)
211 #endif
212 BIO *b;
213 char *out;
214 int outl;
215 {
216 int ret=0;
217
218 if (out != NULL)
219 {
220 errno=0;
221 #if defined(WINDOWS) && !defined(BIO_FD)
222 ret=recv(b->num,out,outl,0);
223 #else
224 ret=read(b->num,out,outl);
225 #endif
226 BIO_clear_retry_flags(b);
227 if (ret <= 0)
228 {
229 #ifndef BIO_FD
230 if (BIO_sock_should_retry(ret))
231 #else
232 if (BIO_fd_should_retry(ret))
233 #endif
234 BIO_set_retry_read(b);
235 }
236 }
237 return(ret);
238 }
239
240 #ifndef BIO_FD
241 static int sock_write(b,in,inl)
242 #else
243 static int fd_write(b,in,inl)
244 #endif
245 BIO *b;
246 char *in;
247 int inl;
248 {
249 int ret;
250
251 errno=0;
252 #if defined(WINDOWS) && !defined(BIO_FD)
253 ret=send(b->num,in,inl,0);
254 #else
255 ret=write(b->num,in,inl);
256 #endif
257 BIO_clear_retry_flags(b);
258 if (ret <= 0)
259 {
260 #ifndef BIO_FD
261 if (BIO_sock_should_retry(ret))
262 #else
263 if (BIO_fd_should_retry(ret))
264 #endif
265 BIO_set_retry_write(b);
266 }
267 return(ret);
268 }
269
270 #ifndef BIO_FD
271 static long sock_ctrl(b,cmd,num,ptr)
272 #else
273 static long fd_ctrl(b,cmd,num,ptr)
274 #endif
275 BIO *b;
276 int cmd;
277 long num;
278 char *ptr;
279 {
280 long ret=1;
281 int *ip;
282
283 switch (cmd)
284 {
285 case BIO_CTRL_RESET:
286 #ifdef BIO_FD
287 ret=(long)lseek(b->num,0,0);
288 #else
289 ret=0;
290 #endif
291 break;
292 case BIO_CTRL_INFO:
293 ret=0;
294 break;
295 case BIO_C_SET_FD:
296 #ifndef BIO_FD
297 sock_free(b);
298 #else
299 fd_free(b);
300 #endif
301 b->num= *((int *)ptr);
302 b->shutdown=(int)num;
303 b->init=1;
304 break;
305 case BIO_C_GET_FD:
306 if (b->init)
307 {
308 ip=(int *)ptr;
309 if (ip != NULL) *ip=b->num;
310 ret=b->num;
311 }
312 else
313 ret= -1;
314 break;
315 case BIO_CTRL_GET_CLOSE:
316 ret=b->shutdown;
317 break;
318 case BIO_CTRL_SET_CLOSE:
319 b->shutdown=(int)num;
320 break;
321 case BIO_CTRL_PENDING:
322 case BIO_CTRL_WPENDING:
323 ret=0;
324 break;
325 case BIO_CTRL_DUP:
326 case BIO_CTRL_FLUSH:
327 ret=1;
328 break;
329 break;
330 default:
331 ret=0;
332 break;
333 }
334 return(ret);
335 }
336
337 #ifdef undef
338 static int sock_gets(bp,buf,size)
339 BIO *bp;
340 char *buf;
341 int size;
342 {
343 return(-1);
344 }
345 #endif
346
347 #ifndef BIO_FD
348 static int sock_puts(bp,str)
349 #else
350 static int fd_puts(bp,str)
351 #endif
352 BIO *bp;
353 char *str;
354 {
355 int n,ret;
356
357 n=strlen(str);
358 #ifndef BIO_FD
359 ret=sock_write(bp,str,n);
360 #else
361 ret=fd_write(bp,str,n);
362 #endif
363 return(ret);
364 }
365
366 #ifndef BIO_FD
367 int BIO_sock_should_retry(i)
368 #else
369 int BIO_fd_should_retry(i)
370 #endif
371 int i;
372 {
373 if ((i == 0) || (i == -1))
374 {
375 #if !defined(BIO_FD) && defined(WINDOWS)
376 errno=WSAGetLastError();
377 #endif
378
379 #if defined(WINDOWS) /* more microsoft stupidity */
380 if ((i == -1) && (errno == 0))
381 return(1);
382 #endif
383 #ifndef BIO_FD
384 return(BIO_sock_non_fatal_error(errno));
385 #else
386 return(BIO_fd_non_fatal_error(errno));
387 #endif
388 }
389 return(0);
390 }
391
392 #ifndef BIO_FD
393 int BIO_sock_non_fatal_error(err)
394 #else
395 int BIO_fd_non_fatal_error(err)
396 #endif
397 int err;
398 {
399 switch (err)
400 {
401 #if !defined(BIO_FD) && defined(WINDOWS)
402 # if defined(WSAEWOULDBLOCK)
403 case WSAEWOULDBLOCK:
404 # endif
405
406 # if defined(WSAENOTCONN)
407 case WSAENOTCONN:
408 # endif
409 #endif
410
411 #ifdef EWOULDBLOCK
412 # ifdef WSAEWOULDBLOCK
413 # if WSAEWOULDBLOCK != EWOULDBLOCK
414 case EWOULDBLOCK:
415 # endif
416 # else
417 case EWOULDBLOCK:
418 # endif
419 #endif
420
421 #ifdef EINTR
422 case EINTR:
423 #endif
424
425 #ifdef EAGAIN
426 #if EWOULDBLOCK != EAGAIN
427 case EAGAIN:
428 # endif
429 #endif
430
431 #ifdef EPROTO
432 case EPROTO:
433 #endif
434
435 #ifdef EINPROGRESS
436 case EINPROGRESS:
437 #endif
438
439 #ifdef EALREADY
440 case EALREADY:
441 #endif
442 return(1);
443 break;
444 default:
445 break;
446 }
447 return(0);
448 }
449 #endif