sl@0: /* crypto/bio/bss_acpt.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #define USE_SOCKETS sl@0: #include "cryptlib.h" sl@0: #include sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: sl@0: #ifndef OPENSSL_NO_SOCK sl@0: sl@0: #ifdef OPENSSL_SYS_WIN16 sl@0: #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ sl@0: #else sl@0: #define SOCKET_PROTOCOL IPPROTO_TCP sl@0: #endif sl@0: sl@0: #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000) sl@0: /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ sl@0: #undef FIONBIO sl@0: #endif sl@0: sl@0: typedef struct bio_accept_st sl@0: { sl@0: int state; sl@0: char *param_addr; sl@0: sl@0: int accept_sock; sl@0: int accept_nbio; sl@0: sl@0: char *addr; sl@0: int nbio; sl@0: /* If 0, it means normal, if 1, do a connect on bind failure, sl@0: * and if there is no-one listening, bind with SO_REUSEADDR. sl@0: * If 2, always use SO_REUSEADDR. */ sl@0: int bind_mode; sl@0: BIO *bio_chain; sl@0: } BIO_ACCEPT; sl@0: sl@0: static int acpt_write(BIO *h, const char *buf, int num); sl@0: static int acpt_read(BIO *h, char *buf, int size); sl@0: static int acpt_puts(BIO *h, const char *str); sl@0: static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2); sl@0: static int acpt_new(BIO *h); sl@0: static int acpt_free(BIO *data); sl@0: static int acpt_state(BIO *b, BIO_ACCEPT *c); sl@0: static void acpt_close_socket(BIO *data); sl@0: BIO_ACCEPT *BIO_ACCEPT_new(void ); sl@0: void BIO_ACCEPT_free(BIO_ACCEPT *a); sl@0: sl@0: #define ACPT_S_BEFORE 1 sl@0: #define ACPT_S_GET_ACCEPT_SOCKET 2 sl@0: #define ACPT_S_OK 3 sl@0: sl@0: #ifndef EMULATOR sl@0: static BIO_METHOD methods_acceptp= sl@0: { sl@0: BIO_TYPE_ACCEPT, sl@0: "socket accept", sl@0: acpt_write, sl@0: acpt_read, sl@0: acpt_puts, sl@0: NULL, /* connect_gets, */ sl@0: acpt_ctrl, sl@0: acpt_new, sl@0: acpt_free, sl@0: NULL, sl@0: }; sl@0: #else sl@0: sl@0: GET_STATIC_VAR_FROM_TLS(methods_acceptp,bss_acpt,BIO_METHOD) sl@0: #define methods_acceptp (*GET_WSD_VAR_NAME(methods_acceptp,bss_acpt,s)()) sl@0: const BIO_METHOD temp_s_methods_acceptp= sl@0: { sl@0: BIO_TYPE_ACCEPT, sl@0: "socket accept", sl@0: acpt_write, sl@0: acpt_read, sl@0: acpt_puts, sl@0: NULL, /* connect_gets, */ sl@0: acpt_ctrl, sl@0: acpt_new, sl@0: acpt_free, sl@0: NULL, sl@0: }; sl@0: sl@0: #endif sl@0: sl@0: sl@0: EXPORT_C BIO_METHOD *BIO_s_accept(void) sl@0: { sl@0: return(&methods_acceptp); sl@0: } sl@0: sl@0: static int acpt_new(BIO *bi) sl@0: { sl@0: BIO_ACCEPT *ba; sl@0: sl@0: bi->init=0; sl@0: bi->num=INVALID_SOCKET; sl@0: bi->flags=0; sl@0: if ((ba=BIO_ACCEPT_new()) == NULL) sl@0: return(0); sl@0: bi->ptr=(char *)ba; sl@0: ba->state=ACPT_S_BEFORE; sl@0: bi->shutdown=1; sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C BIO_ACCEPT *BIO_ACCEPT_new(void) sl@0: { sl@0: BIO_ACCEPT *ret; sl@0: sl@0: if ((ret=(BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL) sl@0: return(NULL); sl@0: sl@0: memset(ret,0,sizeof(BIO_ACCEPT)); sl@0: ret->accept_sock=INVALID_SOCKET; sl@0: ret->bind_mode=BIO_BIND_NORMAL; sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C void BIO_ACCEPT_free(BIO_ACCEPT *a) sl@0: { sl@0: if(a == NULL) sl@0: return; sl@0: sl@0: if (a->param_addr != NULL) OPENSSL_free(a->param_addr); sl@0: if (a->addr != NULL) OPENSSL_free(a->addr); sl@0: if (a->bio_chain != NULL) BIO_free(a->bio_chain); sl@0: OPENSSL_free(a); sl@0: } sl@0: sl@0: static void acpt_close_socket(BIO *bio) sl@0: { sl@0: BIO_ACCEPT *c; sl@0: sl@0: c=(BIO_ACCEPT *)bio->ptr; sl@0: if (c->accept_sock != INVALID_SOCKET) sl@0: { sl@0: shutdown(c->accept_sock,2); sl@0: closesocket(c->accept_sock); sl@0: c->accept_sock=INVALID_SOCKET; sl@0: bio->num=INVALID_SOCKET; sl@0: } sl@0: } sl@0: sl@0: static int acpt_free(BIO *a) sl@0: { sl@0: BIO_ACCEPT *data; sl@0: sl@0: if (a == NULL) return(0); sl@0: data=(BIO_ACCEPT *)a->ptr; sl@0: sl@0: if (a->shutdown) sl@0: { sl@0: acpt_close_socket(a); sl@0: BIO_ACCEPT_free(data); sl@0: a->ptr=NULL; sl@0: a->flags=0; sl@0: a->init=0; sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: static int acpt_state(BIO *b, BIO_ACCEPT *c) sl@0: { sl@0: BIO *bio=NULL,*dbio; sl@0: int s= -1; sl@0: int i; sl@0: sl@0: again: sl@0: switch (c->state) sl@0: { sl@0: case ACPT_S_BEFORE: sl@0: if (c->param_addr == NULL) sl@0: { sl@0: BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED); sl@0: return(-1); sl@0: } sl@0: s=BIO_get_accept_socket(c->param_addr,c->bind_mode); sl@0: if (s == INVALID_SOCKET) sl@0: return(-1); sl@0: sl@0: if (c->accept_nbio) sl@0: { sl@0: if (!BIO_socket_nbio(s,1)) sl@0: { sl@0: closesocket(s); sl@0: BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET); sl@0: return(-1); sl@0: } sl@0: } sl@0: c->accept_sock=s; sl@0: b->num=s; sl@0: c->state=ACPT_S_GET_ACCEPT_SOCKET; sl@0: return(1); sl@0: /* break; */ sl@0: case ACPT_S_GET_ACCEPT_SOCKET: sl@0: if (b->next_bio != NULL) sl@0: { sl@0: c->state=ACPT_S_OK; sl@0: goto again; sl@0: } sl@0: BIO_clear_retry_flags(b); sl@0: b->retry_reason=0; sl@0: i=BIO_accept(c->accept_sock,&(c->addr)); sl@0: sl@0: /* -2 return means we should retry */ sl@0: if(i == -2) sl@0: { sl@0: BIO_set_retry_special(b); sl@0: b->retry_reason=BIO_RR_ACCEPT; sl@0: return -1; sl@0: } sl@0: sl@0: if (i < 0) return(i); sl@0: sl@0: bio=BIO_new_socket(i,BIO_CLOSE); sl@0: if (bio == NULL) goto err; sl@0: sl@0: BIO_set_callback(bio,BIO_get_callback(b)); sl@0: BIO_set_callback_arg(bio,BIO_get_callback_arg(b)); sl@0: sl@0: if (c->nbio) sl@0: { sl@0: if (!BIO_socket_nbio(i,1)) sl@0: { sl@0: BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: /* If the accept BIO has an bio_chain, we dup it and sl@0: * put the new socket at the end. */ sl@0: if (c->bio_chain != NULL) sl@0: { sl@0: if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL) sl@0: goto err; sl@0: if (!BIO_push(dbio,bio)) goto err; sl@0: bio=dbio; sl@0: } sl@0: if (BIO_push(b,bio) == NULL) goto err; sl@0: sl@0: c->state=ACPT_S_OK; sl@0: return(1); sl@0: err: sl@0: if (bio != NULL) sl@0: BIO_free(bio); sl@0: else if (s >= 0) sl@0: closesocket(s); sl@0: return(0); sl@0: /* break; */ sl@0: case ACPT_S_OK: sl@0: if (b->next_bio == NULL) sl@0: { sl@0: c->state=ACPT_S_GET_ACCEPT_SOCKET; sl@0: goto again; sl@0: } sl@0: return(1); sl@0: /* break; */ sl@0: default: sl@0: return(0); sl@0: /* break; */ sl@0: } sl@0: sl@0: } sl@0: sl@0: static int acpt_read(BIO *b, char *out, int outl) sl@0: { sl@0: int ret=0; sl@0: BIO_ACCEPT *data; sl@0: sl@0: BIO_clear_retry_flags(b); sl@0: data=(BIO_ACCEPT *)b->ptr; sl@0: sl@0: while (b->next_bio == NULL) sl@0: { sl@0: ret=acpt_state(b,data); sl@0: if (ret <= 0) return(ret); sl@0: } sl@0: sl@0: ret=BIO_read(b->next_bio,out,outl); sl@0: BIO_copy_next_retry(b); sl@0: return(ret); sl@0: } sl@0: sl@0: static int acpt_write(BIO *b, const char *in, int inl) sl@0: { sl@0: int ret; sl@0: BIO_ACCEPT *data; sl@0: sl@0: BIO_clear_retry_flags(b); sl@0: data=(BIO_ACCEPT *)b->ptr; sl@0: sl@0: while (b->next_bio == NULL) sl@0: { sl@0: ret=acpt_state(b,data); sl@0: if (ret <= 0) return(ret); sl@0: } sl@0: sl@0: ret=BIO_write(b->next_bio,in,inl); sl@0: BIO_copy_next_retry(b); sl@0: return(ret); sl@0: } sl@0: sl@0: static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr) sl@0: { sl@0: BIO *dbio; sl@0: int *ip; sl@0: long ret=1; sl@0: BIO_ACCEPT *data; sl@0: char **pp; sl@0: sl@0: data=(BIO_ACCEPT *)b->ptr; sl@0: sl@0: switch (cmd) sl@0: { sl@0: case BIO_CTRL_RESET: sl@0: ret=0; sl@0: data->state=ACPT_S_BEFORE; sl@0: acpt_close_socket(b); sl@0: b->flags=0; sl@0: break; sl@0: case BIO_C_DO_STATE_MACHINE: sl@0: /* use this one to start the connection */ sl@0: ret=(long)acpt_state(b,data); sl@0: break; sl@0: case BIO_C_SET_ACCEPT: sl@0: if (ptr != NULL) sl@0: { sl@0: if (num == 0) sl@0: { sl@0: b->init=1; sl@0: if (data->param_addr != NULL) sl@0: OPENSSL_free(data->param_addr); sl@0: data->param_addr=BUF_strdup(ptr); sl@0: } sl@0: else if (num == 1) sl@0: { sl@0: data->accept_nbio=(ptr != NULL); sl@0: } sl@0: else if (num == 2) sl@0: { sl@0: if (data->bio_chain != NULL) sl@0: BIO_free(data->bio_chain); sl@0: data->bio_chain=(BIO *)ptr; sl@0: } sl@0: } sl@0: break; sl@0: case BIO_C_SET_NBIO: sl@0: data->nbio=(int)num; sl@0: break; sl@0: case BIO_C_SET_FD: sl@0: b->init=1; sl@0: b->num= *((int *)ptr); sl@0: data->accept_sock=b->num; sl@0: data->state=ACPT_S_GET_ACCEPT_SOCKET; sl@0: b->shutdown=(int)num; sl@0: b->init=1; sl@0: break; sl@0: case BIO_C_GET_FD: sl@0: if (b->init) sl@0: { sl@0: ip=(int *)ptr; sl@0: if (ip != NULL) sl@0: *ip=data->accept_sock; sl@0: ret=data->accept_sock; sl@0: } sl@0: else sl@0: ret= -1; sl@0: break; sl@0: case BIO_C_GET_ACCEPT: sl@0: if (b->init) sl@0: { sl@0: if (ptr != NULL) sl@0: { sl@0: pp=(char **)ptr; sl@0: *pp=data->param_addr; sl@0: } sl@0: else sl@0: ret= -1; sl@0: } sl@0: else sl@0: ret= -1; sl@0: break; sl@0: case BIO_CTRL_GET_CLOSE: sl@0: ret=b->shutdown; sl@0: break; sl@0: case BIO_CTRL_SET_CLOSE: sl@0: b->shutdown=(int)num; sl@0: break; sl@0: case BIO_CTRL_PENDING: sl@0: case BIO_CTRL_WPENDING: sl@0: ret=0; sl@0: break; sl@0: case BIO_CTRL_FLUSH: sl@0: break; sl@0: case BIO_C_SET_BIND_MODE: sl@0: data->bind_mode=(int)num; sl@0: break; sl@0: case BIO_C_GET_BIND_MODE: sl@0: ret=(long)data->bind_mode; sl@0: break; sl@0: case BIO_CTRL_DUP: sl@0: dbio=(BIO *)ptr; sl@0: /* if (data->param_port) EAY EAY sl@0: BIO_set_port(dbio,data->param_port); sl@0: if (data->param_hostname) sl@0: BIO_set_hostname(dbio,data->param_hostname); sl@0: BIO_set_nbio(dbio,data->nbio); */ sl@0: break; sl@0: sl@0: default: sl@0: ret=0; sl@0: break; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: static int acpt_puts(BIO *bp, const char *str) sl@0: { sl@0: int n,ret; sl@0: sl@0: n=strlen(str); sl@0: ret=acpt_write(bp,str,n); sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C BIO *BIO_new_accept(char *str) sl@0: { sl@0: BIO *ret; sl@0: sl@0: ret=BIO_new(BIO_s_accept()); sl@0: if (ret == NULL) return(NULL); sl@0: if (BIO_set_accept_port(ret,str)) sl@0: return(ret); sl@0: else sl@0: { sl@0: BIO_free(ret); sl@0: return(NULL); sl@0: } sl@0: } sl@0: sl@0: #endif