summaryrefslogtreecommitdiffstats
path: root/contrib/ldapc++/src/LDAPMessageQueue.cpp
blob: 92ca02d2cdbeb9ab5f1378a7f7c19f580c771a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// $OpenLDAP$
/*
 * Copyright 2000-2021 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */


#include "config.h"
#include "debug.h"
#include "LDAPMessageQueue.h"
#include "LDAPRequest.h"
#include "LDAPResult.h"
#include "LDAPSearchReference.h"
#include "LDAPSearchRequest.h"
#include "LDAPUrl.h"
#include "LDAPUrlList.h"
#include "LDAPException.h"

using namespace std;

// TODO: How to handle unsolicited notifications, like notice of
//       disconnection

LDAPMessageQueue::LDAPMessageQueue(LDAPRequest *req){
    DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPMessageQueue::LDAPMessageQueue()" << endl);
	m_activeReq.push(req);
    m_issuedReq.push_back(req);
}

LDAPMessageQueue::~LDAPMessageQueue(){
    DEBUG(LDAP_DEBUG_DESTROY, "LDAPMessageQueue::~LDAPMessageQueue()" << endl);
    for(LDAPRequestList::iterator i=m_issuedReq.begin(); 
            i != m_issuedReq.end(); i++){
        delete *i;
    }
    m_issuedReq.clear();
}


LDAPMsg *LDAPMessageQueue::getNext(){
    DEBUG(LDAP_DEBUG_TRACE,"LDAPMessageQueue::getNext()" << endl);

    if ( m_activeReq.empty() ) {
        return 0;
    }

    LDAPRequest *req=m_activeReq.top();
    LDAPMsg *ret=0;

    try{
        ret = req->getNextMessage();
    }catch(LDAPException e){
        //do some clean up
        m_activeReq.pop();
        throw;   
    }

    const LDAPConstraints *constr=req->getConstraints();
    switch (ret->getMessageType()) {
        case LDAPMsg::SEARCH_REFERENCE : 
            if (constr->getReferralChase() ){
                //throws Exception (limit Exceeded)
                LDAPRequest *refReq=chaseReferral(ret);
                if(refReq != 0){
                    m_activeReq.push(refReq);
                    m_issuedReq.push_back(refReq);
                    delete ret;
                    return getNext();
                }
            }
            return ret;
        break;
        case LDAPMsg::SEARCH_ENTRY :
            return ret;
        break;
        case LDAPMsg::SEARCH_DONE :
            if(req->isReferral()){
                req->unbind();
            }
            switch ( ((LDAPResult*)ret)->getResultCode()) {
                case LDAPResult::REFERRAL :
                    if(constr->getReferralChase()){
                        //throws Exception (limit Exceeded)
                        LDAPRequest *refReq=chaseReferral(ret);
                        if(refReq != 0){
                            m_activeReq.pop();
                            m_activeReq.push(refReq);
                            m_issuedReq.push_back(refReq);
                            delete ret;
                            return getNext();
                        }
                    }    
                    return ret;
                break;
                case LDAPResult::SUCCESS :
                    if(req->isReferral()){
                        delete ret;
                        m_activeReq.pop();
                        return getNext();
                    }else{
                        m_activeReq.pop();
                        return ret;
                    }
                break;
                default:
                    m_activeReq.pop();
                    return ret;
                break;
            }
        break;
        //must be some kind of LDAPResultMessage
        default:
            if(req->isReferral()){
                req->unbind();
            }
            LDAPResult* res_p=(LDAPResult*)ret;
            switch (res_p->getResultCode()) {
                case LDAPResult::REFERRAL :
                    if(constr->getReferralChase()){
                        //throws Exception (limit Exceeded)
                        LDAPRequest *refReq=chaseReferral(ret);
                        if(refReq != 0){
                            m_activeReq.pop();
                            m_activeReq.push(refReq);
                            m_issuedReq.push_back(refReq);
                            delete ret;
                            return getNext();
                        }
                    }    
                    return ret;
                break;
                default:
                    m_activeReq.pop();
                    return ret;
            }
        break;
    }
}

// TODO Maybe moved to LDAPRequest::followReferral seems more reasonable
//there
LDAPRequest* LDAPMessageQueue::chaseReferral(LDAPMsg* ref){
    DEBUG(LDAP_DEBUG_TRACE,"LDAPMessageQueue::chaseReferral()" << endl);
    LDAPRequest *req=m_activeReq.top();
    LDAPRequest *refReq=req->followReferral(ref);
    if(refReq !=0){
        if(refReq->getConstraints()->getHopLimit() < refReq->getHopCount()){
            delete(refReq);
            throw LDAPException(LDAP_REFERRAL_LIMIT_EXCEEDED);
        }
        if(refReq->isCycle()){
            delete(refReq);
            throw LDAPException(LDAP_CLIENT_LOOP);
        }
        try {
            refReq->sendRequest();
            return refReq;
        }catch (LDAPException e){
            DEBUG(LDAP_DEBUG_TRACE,"   caught exception" << endl);
            return 0;
        }
    }else{ 
        return 0;
    }
}

LDAPRequestStack* LDAPMessageQueue::getRequestStack(){
    DEBUG(LDAP_DEBUG_TRACE,"LDAPMessageQueue::getRequestStack()" << endl);
    return &m_activeReq;
}