src/_bonsai/ldapconnectiter.c
PyObject *
LDAPConnectIter_Next(LDAPConnectIter *self, int timeout) {
    int rc = -1;
    PyObject *val = NULL;
    char buff[1];

    /* The connection is already binded. */
    if (self->conn->closed == 0) {
        return PyErr_Format(PyExc_StopIteration, "Connection is already open.");
    }

    DEBUG("LDAPConnectIter_Next (self:%p, timeout:%d)"
        " [tls:%d, state:%d]", self, timeout, self->tls, self->state);
    if (self->timeout == -1 && timeout >= 0) {
        self->timeout = timeout;
    }

    /* Initialise LDAP struct. */
    if (self->state == 0) {
        rc = _ldap_finish_init_thread(self->conn->async, self->init_thread, &(self->timeout),
                self->init_thread_data, &(self->conn->ld));
        if (rc == -1) return NULL; /* Error is happened. */
        if (rc == 1) {
            /* Initialisation is finished. */
            self->state = 1;
            if (self->conn->csock != -1) {
                /* Read and drop the data from the dummy socket. */
                if (recv(self->conn->csock, buff, 1, 0) == -1) return NULL;
            }
            /* Set CA cert dir, CA cert and client cert. */
            if (set_certificates(self) != 0) {
                PyErr_BadInternalCall();
                return NULL;
            }
        }
    }

    /* Start building TLS Connection, if needed. */
    if (self->state == 1) {
        if (self->tls == 1) {
            rc = ldap_start_tls(self->conn->ld, NULL, NULL, &(self->tls_id));
            if (rc == LDAP_SUCCESS) {
                self->state = 2;
            } else {
                set_exception(self->conn->ld, rc);
                return NULL;
            }
        } else {
            /* TLS connection is not needed. */
            self->state = 3;
        }
    }

    /* Finish building TLS connection. */
    if (self->state == 2) {
        rc = check_tls_result(self->conn->ld, self->tls_id, self->timeout,
            self->conn->async, self->conn->csock);
        if (rc == -1) return NULL;
        if (rc == 1) {
            self->state = 3;
        }
    }

    /* Start binding procedure. */
    if (self->state > 2) {
        val = binding(self);
        if (val == NULL) return NULL; /* It is an error. */
        if (val != Py_None) return val;
        Py_DECREF(val);
    }

    if (self->conn->async == 0) {
        /* If the function is blocking, then call next() */
        /* automatically, until an error or the LDAPConnection occurs.  */
        return LDAPConnectIter_Next(self, self->timeout);
    } else {
        Py_RETURN_NONE;
    }
}
/* Step the connection process into the next stage. */
Step the connection process into the next stage.
Step the connection process into the next stage .
PyObject *
LDAPConnectIter_Next(LDAPConnectIter *self, int timeout) {
    int rc = -1;
    PyObject *val = NULL;
    char buff[1];
    if (self->conn->closed == 0) {
        return PyErr_Format(PyExc_StopIteration, "Connection is already open.");
    }
    DEBUG("LDAPConnectIter_Next (self:%p, timeout:%d)"
        " [tls:%d, state:%d]", self, timeout, self->tls, self->state);
    if (self->timeout == -1 && timeout >= 0) {
        self->timeout = timeout;
    }
    if (self->state == 0) {
        rc = _ldap_finish_init_thread(self->conn->async, self->init_thread, &(self->timeout),
                self->init_thread_data, &(self->conn->ld));
        if (rc == -1) return NULL; 
        if (rc == 1) {
            self->state = 1;
            if (self->conn->csock != -1) {
                if (recv(self->conn->csock, buff, 1, 0) == -1) return NULL;
            }
            if (set_certificates(self) != 0) {
                PyErr_BadInternalCall();
                return NULL;
            }
        }
    }
    if (self->state == 1) {
        if (self->tls == 1) {
            rc = ldap_start_tls(self->conn->ld, NULL, NULL, &(self->tls_id));
            if (rc == LDAP_SUCCESS) {
                self->state = 2;
            } else {
                set_exception(self->conn->ld, rc);
                return NULL;
            }
        } else {
            self->state = 3;
        }
    }
    if (self->state == 2) {
        rc = check_tls_result(self->conn->ld, self->tls_id, self->timeout,
            self->conn->async, self->conn->csock);
        if (rc == -1) return NULL;
        if (rc == 1) {
            self->state = 3;
        }
    }
    if (self->state > 2) {
        val = binding(self);
        if (val == NULL) return NULL; 
        if (val != Py_None) return val;
        Py_DECREF(val);
    }
    if (self->conn->async == 0) {
        return LDAPConnectIter_Next(self, self->timeout);
    } else {
        Py_RETURN_NONE;
    }
}
PyObject * LDAPConnectIter_Next ( LDAPConnectIter * self , int timeout ) { int rc = -1 ; PyObject * val = NULL ; char buff [ 1 ] ; if ( self -> conn -> closed == 0 ) { return PyErr_Format ( PyExc_StopIteration , " " ) ; } DEBUG ( " " " " , self , timeout , self -> tls , self -> state ) ; if ( self -> timeout == -1 && timeout >= 0 ) { self -> timeout = timeout ; } if ( self -> state == 0 ) { rc = _ldap_finish_init_thread ( self -> conn -> async , self -> init_thread , & ( self -> timeout ) , self -> init_thread_data , & ( self -> conn -> ld ) ) ; if ( rc == -1 ) return NULL ; if ( rc == 1 ) { self -> state = 1 ; if ( self -> conn -> csock != -1 ) { if ( recv ( self -> conn -> csock , buff , 1 , 0 ) == -1 ) return NULL ; } if ( set_certificates ( self ) != 0 ) { PyErr_BadInternalCall ( ) ; return NULL ; } } } if ( self -> state == 1 ) { if ( self -> tls == 1 ) { rc = ldap_start_tls ( self -> conn -> ld , NULL , NULL , & ( self -> tls_id ) ) ; if ( rc == LDAP_SUCCESS ) { self -> state = 2 ; } else { set_exception ( self -> conn -> ld , rc ) ; return NULL ; } } else { self -> state = 3 ; } } if ( self -> state == 2 ) { rc = check_tls_result ( self -> conn -> ld , self -> tls_id , self -> timeout , self -> conn -> async , self -> conn -> csock ) ; if ( rc == -1 ) return NULL ; if ( rc == 1 ) { self -> state = 3 ; } } if ( self -> state > 2 ) { val = binding ( self ) ; if ( val == NULL ) return NULL ; if ( val != Py_None ) return val ; Py_DECREF ( val ) ; } if ( self -> conn -> async == 0 ) { return LDAPConnectIter_Next ( self , self -> timeout ) ; } else { Py_RETURN_NONE ; } }
Step the connection process into the next stage.
Step the connection process into the next stage .
/* The connection is already binded. */ /* Initialise LDAP struct. */ /* Error is happened. */ /* Initialisation is finished. */ /* Read and drop the data from the dummy socket. */ /* Set CA cert dir, CA cert and client cert. */ /* Start building TLS Connection, if needed. */ /* TLS connection is not needed. */ /* Finish building TLS connection. */ /* Start binding procedure. */ /* It is an error. */ /* If the function is blocking, then call next() */ /* automatically, until an error or the LDAPConnection occurs.  */