]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bss_sock.c
Remove NOPROTO definitions and error code comments.
[thirdparty/openssl.git] / crypto / bio / bss_sock.c
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 <openssl/bio.h>
66
67 #ifndef BIO_FD
68 static int sock_write(BIO *h,char *buf,int num);
69 static int sock_read(BIO *h,char *buf,int size);
70 static int sock_puts(BIO *h,char *str);
71 static long sock_ctrl(BIO *h,int cmd,long arg1,char *arg2);
72 static int sock_new(BIO *h);
73 static int sock_free(BIO *data);
74 int BIO_sock_should_retry(int s);
75 #else
76
77 static int fd_write(BIO *h,char *buf,int num);
78 static int fd_read(BIO *h,char *buf,int size);
79 static int fd_puts(BIO *h,char *str);
80 static long fd_ctrl(BIO *h,int cmd,long arg1,char *arg2);
81 static int fd_new(BIO *h);
82 static int fd_free(BIO *data);
83 int BIO_fd_should_retry(int s);
84 #endif
85
86 #ifndef BIO_FD
87 static BIO_METHOD methods_sockp=
88 {
89 BIO_TYPE_SOCKET,
90 "socket",
91 sock_write,
92 sock_read,
93 sock_puts,
94 NULL, /* sock_gets, */
95 sock_ctrl,
96 sock_new,
97 sock_free,
98 };
99
100 BIO_METHOD *BIO_s_socket(void)
101 {
102 return(&methods_sockp);
103 }
104 #else
105 static BIO_METHOD methods_fdp=
106 {
107 BIO_TYPE_FD,"file descriptor",
108 fd_write,
109 fd_read,
110 fd_puts,
111 NULL, /* fd_gets, */
112 fd_ctrl,
113 fd_new,
114 fd_free,
115 };
116
117 BIO_METHOD *BIO_s_fd(void)
118 {
119 return(&methods_fdp);
120 }
121 #endif
122
123 #ifndef BIO_FD
124 BIO *BIO_new_socket(int fd, int close_flag)
125 #else
126 BIO *BIO_new_fd(int fd,int close_flag)
127 #endif
128 {
129 BIO *ret;
130
131 #ifndef BIO_FD
132 ret=BIO_new(BIO_s_socket());
133 #else
134 ret=BIO_new(BIO_s_fd());
135 #endif
136 if (ret == NULL) return(NULL);
137 BIO_set_fd(ret,fd,close_flag);
138 return(ret);
139 }
140
141 #ifndef BIO_FD
142 static int sock_new(BIO *bi)
143 #else
144 static int fd_new(BIO *bi)
145 #endif
146 {
147 bi->init=0;
148 bi->num=0;
149 bi->ptr=NULL;
150 bi->flags=0;
151 return(1);
152 }
153
154 #ifndef BIO_FD
155 static int sock_free(BIO *a)
156 #else
157 static int fd_free(BIO *a)
158 #endif
159 {
160 if (a == NULL) return(0);
161 if (a->shutdown)
162 {
163 if (a->init)
164 {
165 #ifndef BIO_FD
166 shutdown(a->num,2);
167 closesocket(a->num);
168 #else /* BIO_FD */
169 close(a->num);
170 #endif
171
172 }
173 a->init=0;
174 a->flags=0;
175 }
176 return(1);
177 }
178
179 #ifndef BIO_FD
180 static int sock_read(BIO *b, char *out, int outl)
181 #else
182 static int fd_read(BIO *b, char *out,int outl)
183 #endif
184 {
185 int ret=0;
186
187 if (out != NULL)
188 {
189 #ifndef BIO_FD
190 clear_socket_error();
191 ret=readsocket(b->num,out,outl);
192 #else
193 clear_sys_error();
194 ret=read(b->num,out,outl);
195 #endif
196 BIO_clear_retry_flags(b);
197 if (ret <= 0)
198 {
199 #ifndef BIO_FD
200 if (BIO_sock_should_retry(ret))
201 #else
202 if (BIO_fd_should_retry(ret))
203 #endif
204 BIO_set_retry_read(b);
205 }
206 }
207 return(ret);
208 }
209
210 #ifndef BIO_FD
211 static int sock_write(BIO *b, char *in, int inl)
212 #else
213 static int fd_write(BIO *b, char *in, int inl)
214 #endif
215 {
216 int ret;
217
218 #ifndef BIO_FD
219 clear_socket_error();
220 ret=writesocket(b->num,in,inl);
221 #else
222 clear_sys_error();
223 ret=write(b->num,in,inl);
224 #endif
225 BIO_clear_retry_flags(b);
226 if (ret <= 0)
227 {
228 #ifndef BIO_FD
229 if (BIO_sock_should_retry(ret))
230 #else
231 if (BIO_fd_should_retry(ret))
232 #endif
233 BIO_set_retry_write(b);
234 }
235 return(ret);
236 }
237
238 #ifndef BIO_FD
239 static long sock_ctrl(BIO *b, int cmd, long num, char *ptr)
240 #else
241 static long fd_ctrl(BIO *b, int cmd, long num, char *ptr)
242 #endif
243 {
244 long ret=1;
245 int *ip;
246
247 switch (cmd)
248 {
249 case BIO_CTRL_RESET:
250 num=0;
251 case BIO_C_FILE_SEEK:
252 #ifdef BIO_FD
253 ret=(long)lseek(b->num,num,0);
254 #else
255 ret=0;
256 #endif
257 break;
258 case BIO_C_FILE_TELL:
259 case BIO_CTRL_INFO:
260 #ifdef BIO_FD
261 ret=(long)lseek(b->num,0,1);
262 #else
263 ret=0;
264 #endif
265 break;
266 case BIO_C_SET_FD:
267 #ifndef BIO_FD
268 sock_free(b);
269 #else
270 fd_free(b);
271 #endif
272 b->num= *((int *)ptr);
273 b->shutdown=(int)num;
274 b->init=1;
275 break;
276 case BIO_C_GET_FD:
277 if (b->init)
278 {
279 ip=(int *)ptr;
280 if (ip != NULL) *ip=b->num;
281 ret=b->num;
282 }
283 else
284 ret= -1;
285 break;
286 case BIO_CTRL_GET_CLOSE:
287 ret=b->shutdown;
288 break;
289 case BIO_CTRL_SET_CLOSE:
290 b->shutdown=(int)num;
291 break;
292 case BIO_CTRL_PENDING:
293 case BIO_CTRL_WPENDING:
294 ret=0;
295 break;
296 case BIO_CTRL_DUP:
297 case BIO_CTRL_FLUSH:
298 ret=1;
299 break;
300 default:
301 ret=0;
302 break;
303 }
304 return(ret);
305 }
306
307 #ifdef undef
308 static int sock_gets(BIO *bp, char *buf,int size)
309 {
310 return(-1);
311 }
312 #endif
313
314 #ifndef BIO_FD
315 static int sock_puts(BIO *bp, char *str)
316 #else
317 static int fd_puts(BIO *bp, char *str)
318 #endif
319 {
320 int n,ret;
321
322 n=strlen(str);
323 #ifndef BIO_FD
324 ret=sock_write(bp,str,n);
325 #else
326 ret=fd_write(bp,str,n);
327 #endif
328 return(ret);
329 }
330
331 #ifndef BIO_FD
332 int BIO_sock_should_retry(int i)
333 #else
334 int BIO_fd_should_retry(int i)
335 #endif
336 {
337 int err;
338
339 if ((i == 0) || (i == -1))
340 {
341 #ifndef BIO_FD
342 err=get_last_socket_error();
343 #else
344 err=get_last_sys_error();
345 #endif
346
347 #if defined(WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
348 if ((i == -1) && (err == 0))
349 return(1);
350 #endif
351
352 #ifndef BIO_FD
353 return(BIO_sock_non_fatal_error(err));
354 #else
355 return(BIO_fd_non_fatal_error(err));
356 #endif
357 }
358 return(0);
359 }
360
361 #ifndef BIO_FD
362 int BIO_sock_non_fatal_error(int err)
363 #else
364 int BIO_fd_non_fatal_error(int err)
365 #endif
366 {
367 switch (err)
368 {
369 #if !defined(BIO_FD) && defined(WINDOWS)
370 # if defined(WSAEWOULDBLOCK)
371 case WSAEWOULDBLOCK:
372 # endif
373
374 # if 0 /* This appears to always be an error */
375 # if defined(WSAENOTCONN)
376 case WSAENOTCONN:
377 # endif
378 # endif
379 #endif
380
381 #ifdef EWOULDBLOCK
382 # ifdef WSAEWOULDBLOCK
383 # if WSAEWOULDBLOCK != EWOULDBLOCK
384 case EWOULDBLOCK:
385 # endif
386 # else
387 case EWOULDBLOCK:
388 # endif
389 #endif
390
391 #if defined(ENOTCONN)
392 case ENOTCONN:
393 #endif
394
395 #ifdef EINTR
396 case EINTR:
397 #endif
398
399 #ifdef EAGAIN
400 #if EWOULDBLOCK != EAGAIN
401 case EAGAIN:
402 # endif
403 #endif
404
405 #ifdef EPROTO
406 case EPROTO:
407 #endif
408
409 #ifdef EINPROGRESS
410 case EINPROGRESS:
411 #endif
412
413 #ifdef EALREADY
414 case EALREADY:
415 #endif
416 return(1);
417 /* break; */
418 default:
419 break;
420 }
421 return(0);
422 }
423 #endif