On Wed, 22 Apr 1998, Eduardo Marcel Macan wrote:
>       I need to implement a server/client that can handle multiple
> non-blocking connections, and I keep seeing things about the BIO ,
> it really looks like a damn good thing, is there any documentation
> available concerning the BIO interface?

A quick BIO summery for 0.9.0

        BIO *ssl_bio,*conn_bio,*bio;

        conn_bio=BIO_new_connect("remote.host.name.org:443");
        ssl_bio=BIO_new_ssl_connect(SSL_CTX *ctx);

        /* We have 2 options, you can do a BIO_do_handshake(conn_bio)
         * until you get 1 or 0 to establish a connection, then push it
         * onto the SSL BIO, and then do a BIO_do_handshake(ssl_bio)
         * until 0 or 1 which means the SSL handshake has finished (worked
         * or failed. or we can simply do the following */

        bio=BIO_push(ssl_bio,conn_bio);

        /* Into program logic, we can either do BIO_do_handshake() until
         * 0 or 1, or we can just start reading/writing as follows, in all
         * cases the non-blocking logic is outlined */
        
        i=BIO_read(bio,buf,num); /* or */
        i=BIO_write(bio,buf,num); /* or */
        i=BIO_do_handshake(bio);
        /* one of the above three can be done, then follow it with this logic
         */

        if (i > 0)
                {
                read/written data or the 'initialisation' has finished.
                For connect, this means a non-blocking connect has finished
                and for read/write it means data was read or written.
                }
        else if (i == 0)
                {
                protocol failed or connection closed.  Anyway, it is finished.
                }
        else if (i < 0)
                {
                if (BIO_should_retry(bio))
                        { /* Was a non-blocking IO condition */
                        if (BIO_should_read(bio))
                                { /* set select(2) flags accordingly */
                                }
                        else if (BIO_should_write(bio))
                                { /* set select(2) flags accordingly */
                                }
                        else if (BIO_io_special(bio))
                                {
                                j=BIO_get_retry_reason(bio);
                                if (j == BIO_RR_CONNECT)
                                        {
                                        /* non-blocking connect, this
                                         * is currently the only 'special'
                                         * retry reason */
                                        }
                                }
                        }
                else
                        {
                        /* A system error of some type */
                        }
                }

Now the reason for this stuff is because in the SSL protocol, during an
initial handshake or a session-renegotiation (which can occur at any time),
a program read can fail because the OS wanted to write and the other way
round.  The program can get a little tricky when you then have to know that
when you are able to 'read' you actually want to do a BIO_write() call :-).

AS for the BIO interface, there is BIO_read(), BIO_write() which are basicaly
the same as read(2) and write(2); the 'should retry' stuff, and BIO_ctrl()
which is used to get and set values.  There are lots and lots of macros that
map into BIO_ctrl().
BIOs can be pushed and poped.  For the above SSL example, if we want some
buffering, so each BIO_write() does not generate a packet on the wire,

bbio=BIO_new(BIO_f_buffer());
bio=BIO_push(bbio,bio);

BIO_set_read_buffer_size(bbio,0);
BIO_set_write_buffer_size(bbio,1024*10);

etc...


eric

+-------------------------------------------------------------------------+
| Administrative requests should be sent to [EMAIL PROTECTED] |
| List service provided by Open Software Associates, http://www.osa.com/  |
+-------------------------------------------------------------------------+

Reply via email to