4

I'm attempting to configure my SSL sockets to manage multiple connections using 'select()', however I've been unable to get it working. Currently the connections are being accepted, however they are blocking, therefore the server can only handle reading each request at a time.

Code:

int main(int argc, char **argv)
{
    int sock;
    SSL_CTX *ctx;

    init_openssl(); //Load dependencies
    ctx = create_context(); //Set Protocol

    configure_context(ctx); //Set key/cert

    sock = create_socket(3000); //Configure and bind listener

    fd_set active_fd_set, read_fd_set;
    timeval t;

    FD_ZERO(&active_fd_set); //initialise fd active
    FD_SET(sock,&active_fd_set); //includes sock in the fd

    while(1)
    {
        int i;
        struct sockaddr_in addr;
        uint len = sizeof(addr);
        SSL *ssl;

        read_fd_set=active_fd_set;

        if(select(FD_SETSIZE,&read_fd_set,NULL,NULL,NULL)<0)
        {
            std::cout<<"Error at select!"<<std::endl;
        }

            for(i=0;i<FD_SETSIZE;i++)
            {
                if(FD_ISSET(i,&read_fd_set)) //Is fd part of the set
                {
                    if(i==sock)
                    {

                        int client = accept(sock,(struct sockaddr*)&addr,&len);
                        if(client>0){std::cout<<"Client accepted"<<std::endl;}else{std::cout<<"Client failed"<<std::endl;}
                        ssl = SSL_new(ctx); //Create new ssl structure for connection
                        SSL_set_fd(ssl, client);
                        FD_SET(client,&active_fd_set);
                        if(SSL_accept(ssl)>0)
                        {
                            std::cout<<"ACCEPTED"<<std::endl;         
                        }
                    }
                    else
                    {
                        if(SSL_accept(ssl)>0)
                        {
                            std::cout<<"Down here"<<std::endl;
                            close(i);
                            FD_CLR(i,&active_fd_set);
                        }
                    }
                }
            }
    }

Does anyone have any tips on how to get select() working?

2
  • 1
    I recommend you actually read the documentation for SSL_accept, SSL_read etc and which error codes it returns in which cases. Depending on the error code you should then use select. It might also be a good idea to have a look at lots of information out there when searching for openssl+non-blocking instead of expecting somebody else to collect all these information for you. Commented Oct 2, 2016 at 20:51
  • I believe the crl app provides an example. Check in <openssl dir>/apps/crl.c. Commented Oct 3, 2016 at 0:19

1 Answer 1

3

First read SSL_accept(). Second use non-blocking BIO before calling SSL_accept(). Third, once you use non-blocking BIO, you should add the accepted connection sockets (client in your case) to select call and take action only if there is any activity on client socket. You will have to maintain state in that case.

Your current implementation is DOS attack prone.

Sign up to request clarification or add additional context in comments.

2 Comments

How can I make this safer?
Exactly how i mentioned in first three steps. You should look ath this SO post. Start to write that code and come back to this website if you have any further question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.