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