1// $OpenLDAP$
2/*
3 * Copyright 2000-2011 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 */
6
7
8#include "debug.h"
9#include"LDAPResult.h"
10#include"LDAPAsynConnection.h"
11#include "LDAPRequest.h"
12#include "LDAPException.h"
13
14#include <cstdlib>
15
16using namespace std;
17
18LDAPResult::LDAPResult(const LDAPRequest *req, LDAPMessage *msg) :
19        LDAPMsg(msg){
20    if(msg != 0){
21        DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPResult::LDAPResult()" << endl);
22        const LDAPAsynConnection *con=req->getConnection();
23        char **refs=0;
24        LDAPControl** srvctrls=0;
25        char* matchedDN=0;
26        char* errMsg=0;
27        int err=ldap_parse_result(con->getSessionHandle(),msg,&m_resCode,
28                &matchedDN, &errMsg,&refs,&srvctrls,0);
29        if(err != LDAP_SUCCESS){
30            ber_memvfree((void**) refs);
31            ldap_controls_free(srvctrls);
32            throw LDAPException(err);
33        }else{
34            if (refs){
35                m_referrals=LDAPUrlList(refs);
36                ber_memvfree((void**) refs);
37            }
38            if (srvctrls){
39                m_srvControls = LDAPControlSet(srvctrls);
40                m_hasControls = true;
41                ldap_controls_free(srvctrls);
42            }else{
43                m_hasControls = false;
44            }
45            if(matchedDN != 0){
46                m_matchedDN=string(matchedDN);
47                free(matchedDN);
48            }
49            if(errMsg != 0){
50                m_errMsg=string(errMsg);
51                free(errMsg);
52            }
53        }
54    }
55}
56
57LDAPResult::LDAPResult(int type, int resultCode, const std::string &msg) :
58        LDAPMsg(type,0), m_resCode(resultCode), m_errMsg(msg)
59{}
60
61
62LDAPResult::~LDAPResult(){
63    DEBUG(LDAP_DEBUG_DESTROY,"LDAPResult::~LDAPResult()" << endl);
64}
65
66int LDAPResult::getResultCode() const{
67    DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getResultCode()" << endl);
68    return m_resCode;
69}
70
71string LDAPResult::resToString() const{
72    DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::resToString()" << endl);
73    return string(ldap_err2string(m_resCode));
74}
75
76const string& LDAPResult::getErrMsg() const{
77    DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getErrMsg()" << endl);
78    return m_errMsg;
79}
80
81const string& LDAPResult::getMatchedDN() const{
82    DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getMatchedDN()" << endl);
83    return m_matchedDN;
84}
85
86const LDAPUrlList& LDAPResult::getReferralUrls() const{
87    DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getReferralUrl()" << endl);
88    return m_referrals;
89}
90
91ostream& operator<<(ostream &s,LDAPResult &l){
92    return s << "Result: " << l.m_resCode << ": "
93        << ldap_err2string(l.m_resCode) << endl
94        << "Matched: " << l.m_matchedDN << endl << "ErrMsg: " << l.m_errMsg;
95}
96
97