src/_bonsai/ldapconnectiter.c
static PyObject *
binding(LDAPConnectIter *self) {
    int rc = -1;
    int err = 0;
    int ppres = 0;
    unsigned int pperr = 0;
    struct timeval polltime;
    LDAPControl **returned_ctrls = NULL;
    LDAPMessage *res;
    PyObject *ctrl_obj = NULL;
    PyObject *value = NULL;

    if (self->timeout == -1) {
        polltime.tv_sec = 0L;
        polltime.tv_usec = 10L;
    } else {
        polltime.tv_sec = self->timeout / 1000;
        polltime.tv_usec = (self->timeout % 1000) * 1000;
    }

    DEBUG("binding [state:%d]", self->state);
    if (self->state == 3) {
        /* First call of bind. */
        rc = _ldap_bind(self->conn->ld, self->info, self->conn->ppolicy,
                NULL, &(self->message_id));
        if (rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS) {
            close_socketpair(self->conn->socketpair);
            set_exception(self->conn->ld, rc);
            return NULL;
        }
        if (self->conn->csock != -1) {
            /* Dummy sockets are no longer needed. Dispose. */
            self->conn->csock = -1;
            close_socketpair(self->conn->socketpair);
        }
        self->state = 4;
        Py_RETURN_NONE;
    } else {
        if (self->conn->async == 0) {
            /* Block until the server response. */
            if (self->timeout == -1) {
                rc = ldap_result(self->conn->ld, self->message_id, LDAP_MSG_ALL, NULL, &res);
            } else {
                rc = ldap_result(self->conn->ld, self->message_id, LDAP_MSG_ALL, &polltime, &res);
            }
        } else {
            /* Binding is already in progress, poll result from the server. */
            rc = ldap_result(self->conn->ld, self->message_id, LDAP_MSG_ALL, &polltime, &res);
        }
        switch (rc) {
        case -1:
            /* Error occurred during the operation. */
            ldap_msgfree(res);
            set_exception(self->conn->ld, 0);
            return NULL;
        case 0:
            /* Timeout exceeded.*/
            ldap_msgfree(res);
            if (self->conn->async == 0) {
                set_exception(self->conn->ld, -5);
                return NULL;
            }
            Py_RETURN_NONE;
        case LDAP_RES_BIND:
            /* Response is arrived from the server. */
            rc = ldap_parse_result(self->conn->ld, res, &err, NULL, NULL, NULL, &returned_ctrls, 0);
            if (rc != LDAP_SUCCESS) {
                ldap_msgfree(res);
                return NULL;
            }

            ppres = create_ppolicy_control(self->conn->ld, returned_ctrls,
                    &ctrl_obj, &pperr);
            if (returned_ctrls != NULL) ldap_controls_free(returned_ctrls);
            if (ppres == -1) return NULL;

            if (err != LDAP_SASL_BIND_IN_PROGRESS && err != LDAP_SUCCESS) {
                /* Connection is failed. */
                ldap_msgfree(res);
                if (ppres == 1 && pperr != 65535) set_ppolicy_err(pperr, ctrl_obj);
                else set_exception(self->conn->ld, err);

                return NULL;
            }

            if (strcmp(self->info->mech, "SIMPLE") != 0) {
                /* Continue SASL binding procedure. */
                rc = _ldap_bind(self->conn->ld, self->info, self->conn->ppolicy,
                        res, &(self->message_id));

                if (rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS) {
                    set_exception(self->conn->ld, rc);
                    return NULL;
                }

                if (rc == LDAP_SASL_BIND_IN_PROGRESS) {
                    Py_RETURN_NONE;
                }
            } else {
                ldap_msgfree(res);
            }

            if (rc == LDAP_SUCCESS) {
                /* The binding is successfully finished. */
                self->state = 5;
                self->conn->closed = 0;
                if (self->conn->ppolicy == 1) {
                    /* If ppolicy is not available set control to None. */
                    if (ppres != 1) ctrl_obj = Py_None;

                    /* Create (result, ctrl) tuple as return value. */
                    value = Py_BuildValue("(O,O)", self->conn, ctrl_obj);
                    Py_DECREF(ctrl_obj);
                    if (value == NULL) return NULL;
                    return value;
                } else {
                    Py_INCREF(self->conn);
                    return (PyObject *)self->conn;
                }
            }
            Py_RETURN_NONE;
        default:
            /* Invalid return value, it never should happen. */
            PyErr_BadInternalCall();
            return NULL;
        }
    }
}
/* Poll the answer of the async function calls of the binding process.
 Returns NULL in case of error, Py_None for timeout, and the LDAPConnection
 object if successfully finished the binding. */
Poll the answer of the async function calls of the binding process.
Returns NULL in case of error, Py_None for timeout, and the LDAPConnection
object if successfully finished the binding.
Poll the answer of the async function calls of the binding process . Returns NULL in case of error Py_None for timeout and the LDAPConnection object if successfully finished the binding .
static PyObject *
binding(LDAPConnectIter *self) {
    int rc = -1;
    int err = 0;
    int ppres = 0;
    unsigned int pperr = 0;
    struct timeval polltime;
    LDAPControl **returned_ctrls = NULL;
    LDAPMessage *res;
    PyObject *ctrl_obj = NULL;
    PyObject *value = NULL;
    if (self->timeout == -1) {
        polltime.tv_sec = 0L;
        polltime.tv_usec = 10L;
    } else {
        polltime.tv_sec = self->timeout / 1000;
        polltime.tv_usec = (self->timeout % 1000) * 1000;
    }
    DEBUG("binding [state:%d]", self->state);
    if (self->state == 3) {
        rc = _ldap_bind(self->conn->ld, self->info, self->conn->ppolicy,
                NULL, &(self->message_id));
        if (rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS) {
            close_socketpair(self->conn->socketpair);
            set_exception(self->conn->ld, rc);
            return NULL;
        }
        if (self->conn->csock != -1) {
            self->conn->csock = -1;
            close_socketpair(self->conn->socketpair);
        }
        self->state = 4;
        Py_RETURN_NONE;
    } else {
        if (self->conn->async == 0) {
            if (self->timeout == -1) {
                rc = ldap_result(self->conn->ld, self->message_id, LDAP_MSG_ALL, NULL, &res);
            } else {
                rc = ldap_result(self->conn->ld, self->message_id, LDAP_MSG_ALL, &polltime, &res);
            }
        } else {
            rc = ldap_result(self->conn->ld, self->message_id, LDAP_MSG_ALL, &polltime, &res);
        }
        switch (rc) {
        case -1:
            ldap_msgfree(res);
            set_exception(self->conn->ld, 0);
            return NULL;
        case 0:
            ldap_msgfree(res);
            if (self->conn->async == 0) {
                set_exception(self->conn->ld, -5);
                return NULL;
            }
            Py_RETURN_NONE;
        case LDAP_RES_BIND:
            rc = ldap_parse_result(self->conn->ld, res, &err, NULL, NULL, NULL, &returned_ctrls, 0);
            if (rc != LDAP_SUCCESS) {
                ldap_msgfree(res);
                return NULL;
            }
            ppres = create_ppolicy_control(self->conn->ld, returned_ctrls,
                    &ctrl_obj, &pperr);
            if (returned_ctrls != NULL) ldap_controls_free(returned_ctrls);
            if (ppres == -1) return NULL;
            if (err != LDAP_SASL_BIND_IN_PROGRESS && err != LDAP_SUCCESS) {
                ldap_msgfree(res);
                if (ppres == 1 && pperr != 65535) set_ppolicy_err(pperr, ctrl_obj);
                else set_exception(self->conn->ld, err);
                return NULL;
            }
            if (strcmp(self->info->mech, "SIMPLE") != 0) {
                rc = _ldap_bind(self->conn->ld, self->info, self->conn->ppolicy,
                        res, &(self->message_id));
                if (rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS) {
                    set_exception(self->conn->ld, rc);
                    return NULL;
                }
                if (rc == LDAP_SASL_BIND_IN_PROGRESS) {
                    Py_RETURN_NONE;
                }
            } else {
                ldap_msgfree(res);
            }
            if (rc == LDAP_SUCCESS) {
                self->state = 5;
                self->conn->closed = 0;
                if (self->conn->ppolicy == 1) {
                    if (ppres != 1) ctrl_obj = Py_None;
                    value = Py_BuildValue("(O,O)", self->conn, ctrl_obj);
                    Py_DECREF(ctrl_obj);
                    if (value == NULL) return NULL;
                    return value;
                } else {
                    Py_INCREF(self->conn);
                    return (PyObject *)self->conn;
                }
            }
            Py_RETURN_NONE;
        default:
            PyErr_BadInternalCall();
            return NULL;
        }
    }
}
static PyObject * binding ( LDAPConnectIter * self ) { int rc = -1 ; int err = 0 ; int ppres = 0 ; unsigned int pperr = 0 ; struct timeval polltime ; LDAPControl * * returned_ctrls = NULL ; LDAPMessage * res ; PyObject * ctrl_obj = NULL ; PyObject * value = NULL ; if ( self -> timeout == -1 ) { polltime . tv_sec = 0L ; polltime . tv_usec = 10L ; } else { polltime . tv_sec = self -> timeout / 1000 ; polltime . tv_usec = ( self -> timeout % 1000 ) * 1000 ; } DEBUG ( " " , self -> state ) ; if ( self -> state == 3 ) { rc = _ldap_bind ( self -> conn -> ld , self -> info , self -> conn -> ppolicy , NULL , & ( self -> message_id ) ) ; if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) { close_socketpair ( self -> conn -> socketpair ) ; set_exception ( self -> conn -> ld , rc ) ; return NULL ; } if ( self -> conn -> csock != -1 ) { self -> conn -> csock = -1 ; close_socketpair ( self -> conn -> socketpair ) ; } self -> state = 4 ; Py_RETURN_NONE ; } else { if ( self -> conn -> async == 0 ) { if ( self -> timeout == -1 ) { rc = ldap_result ( self -> conn -> ld , self -> message_id , LDAP_MSG_ALL , NULL , & res ) ; } else { rc = ldap_result ( self -> conn -> ld , self -> message_id , LDAP_MSG_ALL , & polltime , & res ) ; } } else { rc = ldap_result ( self -> conn -> ld , self -> message_id , LDAP_MSG_ALL , & polltime , & res ) ; } switch ( rc ) { case -1 : ldap_msgfree ( res ) ; set_exception ( self -> conn -> ld , 0 ) ; return NULL ; case 0 : ldap_msgfree ( res ) ; if ( self -> conn -> async == 0 ) { set_exception ( self -> conn -> ld , -5 ) ; return NULL ; } Py_RETURN_NONE ; case LDAP_RES_BIND : rc = ldap_parse_result ( self -> conn -> ld , res , & err , NULL , NULL , NULL , & returned_ctrls , 0 ) ; if ( rc != LDAP_SUCCESS ) { ldap_msgfree ( res ) ; return NULL ; } ppres = create_ppolicy_control ( self -> conn -> ld , returned_ctrls , & ctrl_obj , & pperr ) ; if ( returned_ctrls != NULL ) ldap_controls_free ( returned_ctrls ) ; if ( ppres == -1 ) return NULL ; if ( err != LDAP_SASL_BIND_IN_PROGRESS && err != LDAP_SUCCESS ) { ldap_msgfree ( res ) ; if ( ppres == 1 && pperr != 65535 ) set_ppolicy_err ( pperr , ctrl_obj ) ; else set_exception ( self -> conn -> ld , err ) ; return NULL ; } if ( strcmp ( self -> info -> mech , " " ) != 0 ) { rc = _ldap_bind ( self -> conn -> ld , self -> info , self -> conn -> ppolicy , res , & ( self -> message_id ) ) ; if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) { set_exception ( self -> conn -> ld , rc ) ; return NULL ; } if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) { Py_RETURN_NONE ; } } else { ldap_msgfree ( res ) ; } if ( rc == LDAP_SUCCESS ) { self -> state = 5 ; self -> conn -> closed = 0 ; if ( self -> conn -> ppolicy == 1 ) { if ( ppres != 1 ) ctrl_obj = Py_None ; value = Py_BuildValue ( " " , self -> conn , ctrl_obj ) ; Py_DECREF ( ctrl_obj ) ; if ( value == NULL ) return NULL ; return value ; } else { Py_INCREF ( self -> conn ) ; return ( PyObject * ) self -> conn ; } } Py_RETURN_NONE ; default : PyErr_BadInternalCall ( ) ; return NULL ; } } }
Poll the answer of the async function calls of the binding process.
Poll the answer of the async function calls of the binding process .
/* First call of bind. */ /* Dummy sockets are no longer needed. Dispose. */ /* Block until the server response. */ /* Binding is already in progress, poll result from the server. */ /* Error occurred during the operation. */ /* Timeout exceeded.*/ /* Response is arrived from the server. */ /* Connection is failed. */ /* Continue SASL binding procedure. */ /* The binding is successfully finished. */ /* If ppolicy is not available set control to None. */ /* Create (result, ctrl) tuple as return value. */ /* Invalid return value, it never should happen. */