155714Skris/* crypto/bio/bss_sock.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296341Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296341Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296341Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296341Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296341Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <errno.h>
6155714Skris#define USE_SOCKETS
6255714Skris#include "cryptlib.h"
63194206Ssimon
64194206Ssimon#ifndef OPENSSL_NO_SOCK
65194206Ssimon
66296341Sdelphij# include <openssl/bio.h>
6755714Skris
68296341Sdelphij# ifdef WATT32
69296341Sdelphij#  define sock_write SockWrite  /* Watt-32 uses same names */
70296341Sdelphij#  define sock_read  SockRead
71296341Sdelphij#  define sock_puts  SockPuts
72296341Sdelphij# endif
73109998Smarkm
7468651Skrisstatic int sock_write(BIO *h, const char *buf, int num);
7568651Skrisstatic int sock_read(BIO *h, char *buf, int size);
7668651Skrisstatic int sock_puts(BIO *h, const char *str);
7768651Skrisstatic long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
7855714Skrisstatic int sock_new(BIO *h);
7955714Skrisstatic int sock_free(BIO *data);
8055714Skrisint BIO_sock_should_retry(int s);
8155714Skris
82296341Sdelphijstatic BIO_METHOD methods_sockp = {
83296341Sdelphij    BIO_TYPE_SOCKET,
84296341Sdelphij    "socket",
85296341Sdelphij    sock_write,
86296341Sdelphij    sock_read,
87296341Sdelphij    sock_puts,
88296341Sdelphij    NULL,                       /* sock_gets, */
89296341Sdelphij    sock_ctrl,
90296341Sdelphij    sock_new,
91296341Sdelphij    sock_free,
92296341Sdelphij    NULL,
93296341Sdelphij};
9455714Skris
9555714SkrisBIO_METHOD *BIO_s_socket(void)
96296341Sdelphij{
97296341Sdelphij    return (&methods_sockp);
98296341Sdelphij}
9955714Skris
10055714SkrisBIO *BIO_new_socket(int fd, int close_flag)
101296341Sdelphij{
102296341Sdelphij    BIO *ret;
10355714Skris
104296341Sdelphij    ret = BIO_new(BIO_s_socket());
105296341Sdelphij    if (ret == NULL)
106296341Sdelphij        return (NULL);
107296341Sdelphij    BIO_set_fd(ret, fd, close_flag);
108296341Sdelphij    return (ret);
109296341Sdelphij}
11055714Skris
11155714Skrisstatic int sock_new(BIO *bi)
112296341Sdelphij{
113296341Sdelphij    bi->init = 0;
114296341Sdelphij    bi->num = 0;
115296341Sdelphij    bi->ptr = NULL;
116296341Sdelphij    bi->flags = 0;
117296341Sdelphij    return (1);
118296341Sdelphij}
11955714Skris
12055714Skrisstatic int sock_free(BIO *a)
121296341Sdelphij{
122296341Sdelphij    if (a == NULL)
123296341Sdelphij        return (0);
124296341Sdelphij    if (a->shutdown) {
125296341Sdelphij        if (a->init) {
126296341Sdelphij            SHUTDOWN2(a->num);
127296341Sdelphij        }
128296341Sdelphij        a->init = 0;
129296341Sdelphij        a->flags = 0;
130296341Sdelphij    }
131296341Sdelphij    return (1);
132296341Sdelphij}
133296341Sdelphij
13455714Skrisstatic int sock_read(BIO *b, char *out, int outl)
135296341Sdelphij{
136296341Sdelphij    int ret = 0;
13755714Skris
138296341Sdelphij    if (out != NULL) {
139296341Sdelphij        clear_socket_error();
140296341Sdelphij        ret = readsocket(b->num, out, outl);
141296341Sdelphij        BIO_clear_retry_flags(b);
142296341Sdelphij        if (ret <= 0) {
143296341Sdelphij            if (BIO_sock_should_retry(ret))
144296341Sdelphij                BIO_set_retry_read(b);
145296341Sdelphij        }
146296341Sdelphij    }
147296341Sdelphij    return (ret);
148296341Sdelphij}
14955714Skris
15068651Skrisstatic int sock_write(BIO *b, const char *in, int inl)
151296341Sdelphij{
152296341Sdelphij    int ret;
15355714Skris
154296341Sdelphij    clear_socket_error();
155296341Sdelphij    ret = writesocket(b->num, in, inl);
156296341Sdelphij    BIO_clear_retry_flags(b);
157296341Sdelphij    if (ret <= 0) {
158296341Sdelphij        if (BIO_sock_should_retry(ret))
159296341Sdelphij            BIO_set_retry_write(b);
160296341Sdelphij    }
161296341Sdelphij    return (ret);
162296341Sdelphij}
163296341Sdelphij
16468651Skrisstatic long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
165296341Sdelphij{
166296341Sdelphij    long ret = 1;
167296341Sdelphij    int *ip;
16855714Skris
169296341Sdelphij    switch (cmd) {
170296341Sdelphij    case BIO_C_SET_FD:
171296341Sdelphij        sock_free(b);
172296341Sdelphij        b->num = *((int *)ptr);
173296341Sdelphij        b->shutdown = (int)num;
174296341Sdelphij        b->init = 1;
175296341Sdelphij        break;
176296341Sdelphij    case BIO_C_GET_FD:
177296341Sdelphij        if (b->init) {
178296341Sdelphij            ip = (int *)ptr;
179296341Sdelphij            if (ip != NULL)
180296341Sdelphij                *ip = b->num;
181296341Sdelphij            ret = b->num;
182296341Sdelphij        } else
183296341Sdelphij            ret = -1;
184296341Sdelphij        break;
185296341Sdelphij    case BIO_CTRL_GET_CLOSE:
186296341Sdelphij        ret = b->shutdown;
187296341Sdelphij        break;
188296341Sdelphij    case BIO_CTRL_SET_CLOSE:
189296341Sdelphij        b->shutdown = (int)num;
190296341Sdelphij        break;
191296341Sdelphij    case BIO_CTRL_DUP:
192296341Sdelphij    case BIO_CTRL_FLUSH:
193296341Sdelphij        ret = 1;
194296341Sdelphij        break;
195296341Sdelphij    default:
196296341Sdelphij        ret = 0;
197296341Sdelphij        break;
198296341Sdelphij    }
199296341Sdelphij    return (ret);
200296341Sdelphij}
20155714Skris
20268651Skrisstatic int sock_puts(BIO *bp, const char *str)
203296341Sdelphij{
204296341Sdelphij    int n, ret;
20555714Skris
206296341Sdelphij    n = strlen(str);
207296341Sdelphij    ret = sock_write(bp, str, n);
208296341Sdelphij    return (ret);
209296341Sdelphij}
21055714Skris
21155714Skrisint BIO_sock_should_retry(int i)
212296341Sdelphij{
213296341Sdelphij    int err;
21455714Skris
215296341Sdelphij    if ((i == 0) || (i == -1)) {
216296341Sdelphij        err = get_last_socket_error();
21755714Skris
218296341Sdelphij# if defined(OPENSSL_SYS_WINDOWS) && 0/* more microsoft stupidity? perhaps
219296341Sdelphij                                       * not? Ben 4/1/99 */
220296341Sdelphij        if ((i == -1) && (err == 0))
221296341Sdelphij            return (1);
222296341Sdelphij# endif
22355714Skris
224296341Sdelphij        return (BIO_sock_non_fatal_error(err));
225296341Sdelphij    }
226296341Sdelphij    return (0);
227296341Sdelphij}
22855714Skris
22955714Skrisint BIO_sock_non_fatal_error(int err)
230296341Sdelphij{
231296341Sdelphij    switch (err) {
232296341Sdelphij# if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_NETWARE)
233296341Sdelphij#  if defined(WSAEWOULDBLOCK)
234296341Sdelphij    case WSAEWOULDBLOCK:
235296341Sdelphij#  endif
23655714Skris
237296341Sdelphij#  if 0                         /* This appears to always be an error */
238296341Sdelphij#   if defined(WSAENOTCONN)
239296341Sdelphij    case WSAENOTCONN:
240296341Sdelphij#   endif
24155714Skris#  endif
24255714Skris# endif
24355714Skris
244296341Sdelphij# ifdef EWOULDBLOCK
245296341Sdelphij#  ifdef WSAEWOULDBLOCK
246296341Sdelphij#   if WSAEWOULDBLOCK != EWOULDBLOCK
247296341Sdelphij    case EWOULDBLOCK:
248296341Sdelphij#   endif
249296341Sdelphij#  else
250296341Sdelphij    case EWOULDBLOCK:
25155714Skris#  endif
25255714Skris# endif
25355714Skris
254296341Sdelphij# if defined(ENOTCONN)
255296341Sdelphij    case ENOTCONN:
256296341Sdelphij# endif
25755714Skris
258296341Sdelphij# ifdef EINTR
259296341Sdelphij    case EINTR:
260296341Sdelphij# endif
26155714Skris
262296341Sdelphij# ifdef EAGAIN
263296341Sdelphij#  if EWOULDBLOCK != EAGAIN
264296341Sdelphij    case EAGAIN:
265296341Sdelphij#  endif
26655714Skris# endif
26755714Skris
268296341Sdelphij# ifdef EPROTO
269296341Sdelphij    case EPROTO:
270296341Sdelphij# endif
27155714Skris
272296341Sdelphij# ifdef EINPROGRESS
273296341Sdelphij    case EINPROGRESS:
274296341Sdelphij# endif
27555714Skris
276296341Sdelphij# ifdef EALREADY
277296341Sdelphij    case EALREADY:
278296341Sdelphij# endif
279296341Sdelphij        return (1);
280296341Sdelphij        /* break; */
281296341Sdelphij    default:
282296341Sdelphij        break;
283296341Sdelphij    }
284296341Sdelphij    return (0);
285296341Sdelphij}
286194206Ssimon
287296341Sdelphij#endif                          /* #ifndef OPENSSL_NO_SOCK */
288