1

I'm adding openSSL in an embedded system dedicated to video.

In the code there is C function sendfile. A socket as ouf_fd and a file descriptor as in_fd.

How can I rewrite this function using openSSL ? I tried using BIO or SSL_set_wfd/rfd but I am quite inexperienced in openSSL.

If possible I would like to keep kernel optimization like sendfile does.

Current code:

        SSL* ssl = SSL_new(ctx);
        SSL_set_wfd(ssl, hc->sd); // socket OUT
        SSL_set_rfd(ssl, fd); // file descriptor IN

        char buf[1000];
        int error = 0, written = 0;
        struct timeval timeout;
        fd_set fds;

        while(1) {
            written = SSL_write(ssl, buf, 1000);
            if(written > 0) {
                break;
            }
            else {
                error = SSL_get_error(hc->ssl, written);
                switch(error) {
                    case SSL_ERROR_NONE:
                        break;
                    case SSL_ERROR_WANT_READ:
                    {
                        int sock = SSL_get_rfd(hc->ssl);
                        FD_ZERO(&fds);
                        FD_SET(sock, &fds);

                        timeout.tv_sec = 5;
                        timeout.tv_usec = 0;

                        error = select(sock+1, NULL, &fds, NULL, &timeout);
                        if (error > 0)
                            continue; // can write more data now...
                    }
                }
            }   
        }

        SSL_set_rfd(ssl, hc->sd);
        while((written = SSL_read(ssl, buf, 1000)) <0) {
        }

Thanks

Render
  • 41
  • 6

0 Answers0