Fix const declarations.
[openssl.git] / crypto / bio / bss_acpt.c
index 2514741..ecdd170 100644 (file)
@@ -1,5 +1,5 @@
 /* crypto/bio/bss_acpt.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
@@ -82,6 +82,10 @@ typedef struct bio_accept_st
 
        char *addr;
        int nbio;
+       /* If 0, it means normal, if 1, do a connect on bind failure,
+        * and if there is no-one listening, bind with SO_REUSEADDR.
+        * If 2, always use SO_REUSEADDR. */
+       int bind_mode;
        BIO *bio_chain;
        } BIO_ACCEPT;
 
@@ -121,7 +125,8 @@ void BIO_ACCEPT_free();
 
 static BIO_METHOD methods_acceptp=
        {
-       BIO_TYPE_ACCEPT,"socket accept",
+       BIO_TYPE_ACCEPT,
+       "socket accept",
        acpt_write,
        acpt_read,
        acpt_puts,
@@ -161,12 +166,16 @@ BIO_ACCEPT *BIO_ACCEPT_new()
 
        memset(ret,0,sizeof(BIO_ACCEPT));
        ret->accept_sock=INVALID_SOCKET;
+       ret->bind_mode=BIO_BIND_NORMAL;
        return(ret);
        }
 
 void BIO_ACCEPT_free(a)
 BIO_ACCEPT *a;
        {
+       if(a == NULL)
+           return;
+
        if (a->param_addr != NULL) Free(a->param_addr);
        if (a->addr != NULL) Free(a->addr);
        if (a->bio_chain != NULL) BIO_free(a->bio_chain);
@@ -182,11 +191,7 @@ BIO *bio;
        if (c->accept_sock != INVALID_SOCKET)
                {
                shutdown(c->accept_sock,2);
-# ifdef WINDOWS
                closesocket(c->accept_sock);
-# else
-               close(c->accept_sock);
-# endif
                c->accept_sock=INVALID_SOCKET;
                bio->num=INVALID_SOCKET;
                }
@@ -216,7 +221,6 @@ BIO *b;
 BIO_ACCEPT *c;
        {
        BIO *bio=NULL,*dbio;
-       unsigned long l=1;
        int s= -1;
        int i;
 
@@ -229,31 +233,24 @@ again:
                        BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED);
                        return(-1);
                        }
-               s=BIO_get_accept_socket(c->param_addr);
+               s=BIO_get_accept_socket(c->param_addr,c->bind_mode);
                if (s == INVALID_SOCKET)
                        return(-1);
 
-#ifdef FIONBIO
                if (c->accept_nbio)
                        {
-                       i=BIO_socket_ioctl(b->num,FIONBIO,&l);
-                       if (i < 0)
+                       if (!BIO_socket_nbio(s,1))
                                {
-#ifdef WINDOWS
                                closesocket(s);
-#else
-                               close(s);
-# endif
                                BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
                                return(-1);
                                }
                        }
-#endif
                c->accept_sock=s;
                b->num=s;
                c->state=ACPT_S_GET_ACCEPT_SOCKET;
                return(1);
-               break;
+               /* break; */
        case ACPT_S_GET_ACCEPT_SOCKET:
                if (b->next_bio != NULL)
                        {
@@ -268,17 +265,14 @@ again:
                BIO_set_callback(bio,BIO_get_callback(b));
                BIO_set_callback_arg(bio,BIO_get_callback_arg(b));
 
-#ifdef FIONBIO
                if (c->nbio)
                        {
-                       i=BIO_socket_ioctl(i,FIONBIO,&l);
-                       if (i < 0)
+                       if (!BIO_socket_nbio(i,1))
                                {
                                BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
                                goto err;
                                }
                        }
-#endif
 
                /* If the accept BIO has an bio_chain, we dup it and
                 * put the new socket at the end. */
@@ -297,15 +291,9 @@ err:
                if (bio != NULL)
                        BIO_free(bio);
                else if (s >= 0)
-                       {
-#ifdef WINDOWS
                        closesocket(s);
-#else
-                       close(s);
-# endif
-                       }
                return(0);
-               break;
+               /* break; */
        case ACPT_S_OK:
                if (b->next_bio == NULL)
                        {
@@ -313,10 +301,10 @@ err:
                        goto again;
                        }
                return(1);
-               break;
+               /* break; */
        default:        
                return(0);
-               break;
+               /* break; */
                }
 
        }
@@ -375,6 +363,7 @@ char *ptr;
        int *ip;
        long ret=1;
        BIO_ACCEPT *data;
+       char **pp;
 
        data=(BIO_ACCEPT *)b->ptr;
 
@@ -415,13 +404,35 @@ char *ptr;
        case BIO_C_SET_NBIO:
                data->nbio=(int)num;
                break;
+       case BIO_C_SET_FD:
+               b->init=1;
+               b->num= *((int *)ptr);
+               data->accept_sock=b->num;
+               data->state=ACPT_S_GET_ACCEPT_SOCKET;
+               b->shutdown=(int)num;
+               b->init=1;
+               break;
        case BIO_C_GET_FD:
                if (b->init)
                        {
                        ip=(int *)ptr;
                        if (ip != NULL)
                                *ip=data->accept_sock;
-                       ret=b->num;
+                       ret=data->accept_sock;
+                       }
+               else
+                       ret= -1;
+               break;
+       case BIO_C_GET_ACCEPT:
+               if (b->init)
+                       {
+                       if (ptr != NULL)
+                               {
+                               pp=(char **)ptr;
+                               *pp=data->param_addr;
+                               }
+                       else
+                               ret= -1;
                        }
                else
                        ret= -1;
@@ -438,6 +449,12 @@ char *ptr;
                break;
        case BIO_CTRL_FLUSH:
                break;
+       case BIO_C_SET_BIND_MODE:
+               data->bind_mode=(int)num;
+               break;
+       case BIO_C_GET_BIND_MODE:
+               ret=(long)data->bind_mode;
+               break;
        case BIO_CTRL_DUP:
                dbio=(BIO *)ptr;
 /*             if (data->param_port) EAY EAY