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.
8296465Sdelphij *
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).
15296465Sdelphij *
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.
22296465Sdelphij *
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 :-).
37296465Sdelphij * 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)"
40296465Sdelphij *
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.
52296465Sdelphij *
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
66296465Sdelphij# include <openssl/bio.h>
6755714Skris
68296465Sdelphij# ifdef WATT32
69296465Sdelphij#  define sock_write SockWrite  /* Watt-32 uses same names */
70296465Sdelphij#  define sock_read  SockRead
71296465Sdelphij#  define sock_puts  SockPuts
72296465Sdelphij# 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
82296465Sdelphijstatic BIO_METHOD methods_sockp = {
83296465Sdelphij    BIO_TYPE_SOCKET,
84296465Sdelphij    "socket",
85296465Sdelphij    sock_write,
86296465Sdelphij    sock_read,
87296465Sdelphij    sock_puts,
88296465Sdelphij    NULL,                       /* sock_gets, */
89296465Sdelphij    sock_ctrl,
90296465Sdelphij    sock_new,
91296465Sdelphij    sock_free,
92296465Sdelphij    NULL,
93296465Sdelphij};
9455714Skris
9555714SkrisBIO_METHOD *BIO_s_socket(void)
96296465Sdelphij{
97296465Sdelphij    return (&methods_sockp);
98296465Sdelphij}
9955714Skris
10055714SkrisBIO *BIO_new_socket(int fd, int close_flag)
101296465Sdelphij{
102296465Sdelphij    BIO *ret;
10355714Skris
104296465Sdelphij    ret = BIO_new(BIO_s_socket());
105296465Sdelphij    if (ret == NULL)
106296465Sdelphij        return (NULL);
107296465Sdelphij    BIO_set_fd(ret, fd, close_flag);
108296465Sdelphij    return (ret);
109296465Sdelphij}
11055714Skris
11155714Skrisstatic int sock_new(BIO *bi)
112296465Sdelphij{
113296465Sdelphij    bi->init = 0;
114296465Sdelphij    bi->num = 0;
115296465Sdelphij    bi->ptr = NULL;
116296465Sdelphij    bi->flags = 0;
117296465Sdelphij    return (1);
118296465Sdelphij}
11955714Skris
12055714Skrisstatic int sock_free(BIO *a)
121296465Sdelphij{
122296465Sdelphij    if (a == NULL)
123296465Sdelphij        return (0);
124296465Sdelphij    if (a->shutdown) {
125296465Sdelphij        if (a->init) {
126296465Sdelphij            SHUTDOWN2(a->num);
127296465Sdelphij        }
128296465Sdelphij        a->init = 0;
129296465Sdelphij        a->flags = 0;
130296465Sdelphij    }
131296465Sdelphij    return (1);
132296465Sdelphij}
133296465Sdelphij
13455714Skrisstatic int sock_read(BIO *b, char *out, int outl)
135296465Sdelphij{
136296465Sdelphij    int ret = 0;
13755714Skris
138296465Sdelphij    if (out != NULL) {
139296465Sdelphij        clear_socket_error();
140296465Sdelphij        ret = readsocket(b->num, out, outl);
141296465Sdelphij        BIO_clear_retry_flags(b);
142296465Sdelphij        if (ret <= 0) {
143296465Sdelphij            if (BIO_sock_should_retry(ret))
144296465Sdelphij                BIO_set_retry_read(b);
145296465Sdelphij        }
146296465Sdelphij    }
147296465Sdelphij    return (ret);
148296465Sdelphij}
14955714Skris
15068651Skrisstatic int sock_write(BIO *b, const char *in, int inl)
151296465Sdelphij{
152296465Sdelphij    int ret;
15355714Skris
154296465Sdelphij    clear_socket_error();
155296465Sdelphij    ret = writesocket(b->num, in, inl);
156296465Sdelphij    BIO_clear_retry_flags(b);
157296465Sdelphij    if (ret <= 0) {
158296465Sdelphij        if (BIO_sock_should_retry(ret))
159296465Sdelphij            BIO_set_retry_write(b);
160296465Sdelphij    }
161296465Sdelphij    return (ret);
162296465Sdelphij}
163296465Sdelphij
16468651Skrisstatic long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
165296465Sdelphij{
166296465Sdelphij    long ret = 1;
167296465Sdelphij    int *ip;
16855714Skris
169296465Sdelphij    switch (cmd) {
170296465Sdelphij    case BIO_C_SET_FD:
171296465Sdelphij        sock_free(b);
172296465Sdelphij        b->num = *((int *)ptr);
173296465Sdelphij        b->shutdown = (int)num;
174296465Sdelphij        b->init = 1;
175296465Sdelphij        break;
176296465Sdelphij    case BIO_C_GET_FD:
177296465Sdelphij        if (b->init) {
178296465Sdelphij            ip = (int *)ptr;
179296465Sdelphij            if (ip != NULL)
180296465Sdelphij                *ip = b->num;
181296465Sdelphij            ret = b->num;
182296465Sdelphij        } else
183296465Sdelphij            ret = -1;
184296465Sdelphij        break;
185296465Sdelphij    case BIO_CTRL_GET_CLOSE:
186296465Sdelphij        ret = b->shutdown;
187296465Sdelphij        break;
188296465Sdelphij    case BIO_CTRL_SET_CLOSE:
189296465Sdelphij        b->shutdown = (int)num;
190296465Sdelphij        break;
191296465Sdelphij    case BIO_CTRL_DUP:
192296465Sdelphij    case BIO_CTRL_FLUSH:
193296465Sdelphij        ret = 1;
194296465Sdelphij        break;
195296465Sdelphij    default:
196296465Sdelphij        ret = 0;
197296465Sdelphij        break;
198296465Sdelphij    }
199296465Sdelphij    return (ret);
200296465Sdelphij}
20155714Skris
20268651Skrisstatic int sock_puts(BIO *bp, const char *str)
203296465Sdelphij{
204296465Sdelphij    int n, ret;
20555714Skris
206296465Sdelphij    n = strlen(str);
207296465Sdelphij    ret = sock_write(bp, str, n);
208296465Sdelphij    return (ret);
209296465Sdelphij}
21055714Skris
21155714Skrisint BIO_sock_should_retry(int i)
212296465Sdelphij{
213296465Sdelphij    int err;
21455714Skris
215296465Sdelphij    if ((i == 0) || (i == -1)) {
216296465Sdelphij        err = get_last_socket_error();
21755714Skris
218296465Sdelphij# if defined(OPENSSL_SYS_WINDOWS) && 0/* more microsoft stupidity? perhaps
219296465Sdelphij                                       * not? Ben 4/1/99 */
220296465Sdelphij        if ((i == -1) && (err == 0))
221296465Sdelphij            return (1);
222296465Sdelphij# endif
22355714Skris
224296465Sdelphij        return (BIO_sock_non_fatal_error(err));
225296465Sdelphij    }
226296465Sdelphij    return (0);
227296465Sdelphij}
22855714Skris
22955714Skrisint BIO_sock_non_fatal_error(int err)
230296465Sdelphij{
231296465Sdelphij    switch (err) {
232296465Sdelphij# if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_NETWARE)
233296465Sdelphij#  if defined(WSAEWOULDBLOCK)
234296465Sdelphij    case WSAEWOULDBLOCK:
235296465Sdelphij#  endif
23655714Skris
237296465Sdelphij#  if 0                         /* This appears to always be an error */
238296465Sdelphij#   if defined(WSAENOTCONN)
239296465Sdelphij    case WSAENOTCONN:
240296465Sdelphij#   endif
24155714Skris#  endif
24255714Skris# endif
24355714Skris
244296465Sdelphij# ifdef EWOULDBLOCK
245296465Sdelphij#  ifdef WSAEWOULDBLOCK
246296465Sdelphij#   if WSAEWOULDBLOCK != EWOULDBLOCK
247296465Sdelphij    case EWOULDBLOCK:
248296465Sdelphij#   endif
249296465Sdelphij#  else
250296465Sdelphij    case EWOULDBLOCK:
25155714Skris#  endif
25255714Skris# endif
25355714Skris
254296465Sdelphij# if defined(ENOTCONN)
255296465Sdelphij    case ENOTCONN:
256296465Sdelphij# endif
25755714Skris
258296465Sdelphij# ifdef EINTR
259296465Sdelphij    case EINTR:
260296465Sdelphij# endif
26155714Skris
262296465Sdelphij# ifdef EAGAIN
263296465Sdelphij#  if EWOULDBLOCK != EAGAIN
264296465Sdelphij    case EAGAIN:
265296465Sdelphij#  endif
26655714Skris# endif
26755714Skris
268296465Sdelphij# ifdef EPROTO
269296465Sdelphij    case EPROTO:
270296465Sdelphij# endif
27155714Skris
272296465Sdelphij# ifdef EINPROGRESS
273296465Sdelphij    case EINPROGRESS:
274296465Sdelphij# endif
27555714Skris
276296465Sdelphij# ifdef EALREADY
277296465Sdelphij    case EALREADY:
278296465Sdelphij# endif
279296465Sdelphij        return (1);
280296465Sdelphij        /* break; */
281296465Sdelphij    default:
282296465Sdelphij        break;
283296465Sdelphij    }
284296465Sdelphij    return (0);
285296465Sdelphij}
286194206Ssimon
287296465Sdelphij#endif                          /* #ifndef OPENSSL_NO_SOCK */
288