October 25, 2024
Chicago 12, Melborne City, USA
C#

Why does SSL_write in BoringSSL cause a 2-second delay?


I’m developing an application on Android 10 using C code, compiled into a .so library. The application sends data using BoringSSL’s SSL_write() in blocking mode.

SSL_write() returns successfully, but Wireshark captures the corresponding Application Data packet with a 2-second delay.
This delay occurs intermittently, roughly 1 in 10-20 attempts.

Code Example:
Here is the code snippet that sends data using SSL.

// set to blocking before write
fcntl(g_ssl_sock_id, F_SETFL, flags & (~O_NONBLOCK));

// Since the original function contains other judgment logic such as TCP, the SSL_write code is extracted separately
static int ssl_write()
{
    int send_len, len = total_len;;
    unsigned char *cur_p = send_data;
    print_timestamp("Before SSL_write");

    while (len > 0)
    {
        send_len = (len > TCP_MAX_SSL_SEND_BUFFER_SIZE) ? TCP_MAX_SSL_SEND_BUFFER_SIZE : len;

        ret = SSL_write((SSL *)ssl_handle.handle, cur_p, send_len);
        print_timestamp("After SSL_write");

        if (ret != send_len)
        {
            LOG("SslSend data error: %d, exp: %d\n", ret, send_len);
            memset(send_data, 0, total_len);
            if (dyn_allocate)
            {
                uai_free(send_data);
            }
            return (g_err_code = UAI_ERROR);
        }

        len -= send_len;
        cur_p += send_len;
    }

    memset(send_data, 0, total_len);
    if (dyn_allocate)
    {
        uai_free(send_data);
    }
    return (g_err_code = UAI_OK);
}

Other settings:

//setsockopt TCP_NODELAY to disable nagle algorithm but still not work.

Log infomation:

<2024-10-16 11:13:55.505-0574700106>[info_callback]:SSL state: SSLv3 write client hello A
<2024-10-16 11:14:00.909-0574705510>[print_timestamp]:BBB ssl time=[2024-10-16 11:14:00.909600], Before SSL_write
<2024-10-16 11:14:00.909-0574705510>[print_timestamp]:BBB ssl time=[2024-10-16 11:14:00.909912], After SSL_write
E<2024-10-16 11:14:02.890-0574707490>info_callback]:SSL alert: close notify

enter image description here

Has anyone encountered a similar issue or know what might be causing this? I’d appreciate any insights or suggestions to troubleshoot this further. Thanks in advance!



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video