I'm trying to run OpenLDAP on a Cortex-M7 processor, but to be a "good citizen" 
my LDAP client code needs to connect asynchronously. (The code connects 
synchronously fine.)

Stripped down to the bare minimum (i.e. no error handling) for clarity here 
(the actual code has tons of error handling), my asynchronous connection code 
looks like this:

ldap_set_option(pstContext->ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_ON); // Enable 
async connection
ldap_connect(ld); // Do the LDAP connection

As you'd expect, the next step is to test (asynchronously) whether the 
connection has completed, but it can't see any OpenLDAP functionality to do 
this directly.

So here's what my test currently looks like:

/**
 * Determine whether a connection has yet connected
 *
 * @param[in]   LDAP connection instance to test
 *
 * @retval  0 if connection has connected
 * @retval -2 if connection is still waiting
 * @return ...otherwise an unexpected problem was encountered
 */
int ldap_connection_connected(LDAP * ld)
{
        ber_socket_t sd;
        int iRetVal;

        iRetVal = -99;          // Default to "an unexpected problem was 
encountered"
        if (ld->ld_defconn != NULL)
        {
                if (ld->ld_defconn->lconn_status == LDAP_CONNST_CONNECTED)
                {
                        iRetVal = 0;    // i.e. the connection has already 
connected, so exit early!
                }
                else
                {
                        // Get the connection's sockbuffer
                        ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb, 
LBER_SB_OPT_GET_FD, &sd );

                        // Check to see if a result has yet been received via 
this sockbuffer
                        iRetVal = ldap_int_check_async_open( ld, sd );
                }
        }

        return iRetVal;
}

However, my bind code (i.e. that executes immediately after this) fails to 
connect to the server after an asynchronous ldap_connect(), even though it 
works fine after a synchronous ldap_connect(). Moreover, it looks to me (from 
the timings) as though my test for asynchronous connection is signalling 
completion earlier than I'm expecting, so I'm wondering if this is not waiting 
properly, e.g. if it's returning after the very first message response when it 
should be waiting for a whole series of message responses to arrive.

Am I missing some clever OpenLDAP trick for correctly detecting when 
asynchronous ldap_connect() calls have fully connected? Thanks!

Reply via email to