Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

RCF - Interprocess Communication for C++

, 25 Oct 2011 CPOL
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
rcf-04.zip
RCF-0.4
demo
vs2003
RCF
Client
Server
doc
html
class_r_c_f_1_1_exception.png
class_r_c_f_1_1_filter.png
class_r_c_f_1_1_filter_factory.png
class_r_c_f_1_1_filter_service.png
class_r_c_f_1_1_identity_filter.png
class_r_c_f_1_1_identity_filter_factory.png
class_r_c_f_1_1_i___client_transport.png
class_r_c_f_1_1_i___endpoint.png
class_r_c_f_1_1_i___service.png
class_r_c_f_1_1_i___session_manager.png
class_r_c_f_1_1_multicast_client_transport.png
class_r_c_f_1_1_object_factory_service.png
class_r_c_f_1_1_open_ssl_encryption_filter.png
class_r_c_f_1_1_open_ssl_encryption_filter_factory.png
class_r_c_f_1_1_publishing_service.png
class_r_c_f_1_1_rcf_server.png
class_r_c_f_1_1_remote_exception.png
class_r_c_f_1_1_subscription_service.png
class_r_c_f_1_1_tcp_endpoint.png
class_r_c_f_1_1_udp_endpoint.png
class_r_c_f_1_1_zlib_stateful_compression_filter_factory.png
class_r_c_f_1_1_zlib_stateless_compression_filter_factory.png
doxygen.png
ftv2blank.png
ftv2doc.png
ftv2folderclosed.png
ftv2folderopen.png
ftv2lastnode.png
ftv2link.png
ftv2mlastnode.png
ftv2mnode.png
ftv2node.png
ftv2plastnode.png
ftv2pnode.png
ftv2vertline.png
tab_b.gif
tab_l.gif
tab_r.gif
latex
annotated.tex
class_r_c_f_1_1_client_stub.tex
class_r_c_f_1_1_exception.eps
class_r_c_f_1_1_exception.tex
class_r_c_f_1_1_filter.eps
class_r_c_f_1_1_filter.tex
class_r_c_f_1_1_filter_description.tex
class_r_c_f_1_1_filter_factory.eps
class_r_c_f_1_1_filter_factory.tex
class_r_c_f_1_1_filter_service.eps
class_r_c_f_1_1_filter_service.tex
class_r_c_f_1_1_identity_filter.eps
class_r_c_f_1_1_identity_filter.tex
class_r_c_f_1_1_identity_filter_factory.eps
class_r_c_f_1_1_identity_filter_factory.tex
class_r_c_f_1_1_i___client_transport.eps
class_r_c_f_1_1_i___client_transport.tex
class_r_c_f_1_1_i___endpoint.eps
class_r_c_f_1_1_i___endpoint.tex
class_r_c_f_1_1_i___proactor.tex
class_r_c_f_1_1_i___rcf_client.tex
class_r_c_f_1_1_i___server_transport.tex
class_r_c_f_1_1_i___server_transport_ex.tex
class_r_c_f_1_1_i___service.eps
class_r_c_f_1_1_i___service.tex
class_r_c_f_1_1_i___session.tex
class_r_c_f_1_1_i___session_manager.eps
class_r_c_f_1_1_i___session_manager.tex
class_r_c_f_1_1_multicast_client_transport.eps
class_r_c_f_1_1_multicast_client_transport.tex
class_r_c_f_1_1_object_factory_service.eps
class_r_c_f_1_1_object_factory_service.tex
class_r_c_f_1_1_open_ssl_encryption_filter.eps
class_r_c_f_1_1_open_ssl_encryption_filter.tex
class_r_c_f_1_1_open_ssl_encryption_filter_factory.eps
class_r_c_f_1_1_open_ssl_encryption_filter_factory.tex
class_r_c_f_1_1_publishing_service.eps
class_r_c_f_1_1_publishing_service.tex
class_r_c_f_1_1_rcf_server.eps
class_r_c_f_1_1_rcf_server.tex
class_r_c_f_1_1_remote_exception.eps
class_r_c_f_1_1_remote_exception.tex
class_r_c_f_1_1_subscription_service.eps
class_r_c_f_1_1_subscription_service.tex
class_r_c_f_1_1_tcp_endpoint.eps
class_r_c_f_1_1_tcp_endpoint.tex
class_r_c_f_1_1_udp_endpoint.eps
class_r_c_f_1_1_udp_endpoint.tex
class_r_c_f_1_1_zlib_stateful_compression_filter.tex
class_r_c_f_1_1_zlib_stateful_compression_filter_factory.eps
class_r_c_f_1_1_zlib_stateful_compression_filter_factory.tex
class_r_c_f_1_1_zlib_stateless_compression_filter.tex
class_r_c_f_1_1_zlib_stateless_compression_filter_factory.eps
class_r_c_f_1_1_zlib_stateless_compression_filter_factory.tex
dirs.tex
dir_G_3A_2FDevelopment_2Fbuild_2Fscripts_2Fwin_2FBuildRcf2_5FOutput_2FRCF_2D0_2E4_2Finclude_2F.tex
dir_G_3A_2FDevelopment_2Fbuild_2Fscripts_2Fwin_2FBuildRcf2_5FOutput_2FRCF_2D0_2E4_2Finclude_2FRCF_2F.tex
dir_G_3A_2FDevelopment_2Fbuild_2Fscripts_2Fwin_2FBuildRcf2_5FOutput_2FRCF_2D0_2E4_2Finclude_2FRCF_2FProtocol_2F.tex
doxygen.sty
Helvetica.ttf
hierarchy.tex
Makefile
refman.tex
struct_r_c_f_1_1_filter_id_comparison.tex
include
RCF
Marshal.inl
Protocol
RcfServer.inl
ServerStub.inl
test
util
Platform
Machine
SPARC
x86
OS
Unix
Windows
Threads
SF
src
RCF
Protocol
SF
test
borland
Jamfile
Jamrules
Jamfile
Jamrules
vs2003
RCF
RCF
RCFTest
client.pem
server.pem
rcf-09c.zip
RCF-0.9c
demo
vs2003
RCF
Client
Server
include
RCF
Marshal.inl
Protocol
RcfServer.inl
ServerStub.inl
test
util
Platform
Machine
SPARC
x86
OS
Unix
Windows
Threads
SF
src
RCF
Protocol
util
SF
test
bcc
Jamfile
Jamrules
data
caCertA.pem
caCertB.pem
certA.pem
certB.pem
ssCert1.pem
ssCert2.pem
Jamfile
Jamrules
vc6
Jamfile
Jamrules
#ifndef INCLUDE_RCF_SSPIFILTER_HPP
#define INCLUDE_RCF_SSPIFILTER_HPP

#include <memory>

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>

#include <RCF/AsyncFilter.hpp>
#include <RCF/Tools.hpp>

#include <RCF/util/Tchar.hpp>

#ifndef SECURITY_WIN32
#define SECURITY_WIN32
#endif

#include <security.h>
#include <tchar.h>

namespace RCF {

    typedef util::tstring tstring;

    tstring getMyUserName();
    tstring getMyDomain();
    tstring getMyMachineName();

    template<typename T1,typename T2>
    class RecursionState
    {
    public:
        RecursionState() :
            mRecursing(RCF_DEFAULT_INIT),
            mT1(RCF_DEFAULT_INIT),
            mT2(RCF_DEFAULT_INIT)
        {}

        void assign(const T1 &t1, const T2 &t2)
        {
            mT1 = t1;
            mT2 = t2;

            clearArg(t1);
            clearArg(t2);
        }

        void clear()
        {
            mRecursing = false;
            mT1 = T1();
            mT2 = T2();
        }

        bool    mRecursing;
        T1      mT1;
        T2      mT2;

    private:
        void clearArg_(const ByteBuffer &byteBuffer, boost::mpl::true_*)
        {
            const_cast<ByteBuffer &>(byteBuffer).clear();
        }

        template<typename T>
        void clearArg_(const T &, boost::mpl::false_*)
        {}

        template<typename T>
        void clearArg(const T &t)
        {
            typedef typename boost::is_same<T, ByteBuffer>::type type;
            clearArg_(t, (type*) 0);
        }
    };

    class SspiFilterBase;

    typedef boost::shared_ptr<SspiFilterBase> SspiFilterBasePtr;

    class SspiImpersonator
    {
    public:
        SspiImpersonator(SspiFilterBasePtr sspiFilterPtr);
        ~SspiImpersonator();
        bool impersonate();
        void revertToSelf() const;
    private:
        SspiFilterBasePtr mSspiFilterPtr;
    };

    //static const ULONG DefaultSspiContextRequirements = 2079;
    static const ULONG DefaultSspiContextRequirements =
        ISC_REQ_REPLAY_DETECT   |
        ISC_REQ_SEQUENCE_DETECT |
        ISC_REQ_CONFIDENTIALITY |
        ISC_REQ_INTEGRITY       |
        ISC_REQ_DELEGATE        |
        ISC_REQ_MUTUAL_AUTH;

    class SspiFilterBase :
        public IdentityFilter, // TODO: replace with Filter
        public boost::enable_shared_from_this<SspiFilterBase>
    {
    public:

        ~SspiFilterBase();

        enum QualityOfProtection
        {
            None,
            Encryption,
            Integrity
        };

        typedef SspiImpersonator Impersonator;

    protected:

        friend class SspiImpersonator;

        SspiFilterBase(
            const tstring &packageName,
            const tstring &packageList,
            bool server = false);

        SspiFilterBase(
            const tstring &target,
            QualityOfProtection qop,
            ULONG contextRequirements,
            const tstring &packageName,
            const tstring &packageList,
            bool server = false);

        SspiFilterBase(
            const tstring &userName,
            const tstring &password,
            const tstring &domain,
            const tstring &target,
            QualityOfProtection qop,
            ULONG contextRequirements,
            const tstring &packageName,
            const tstring &packageList,
            bool server = false);

        enum Event
        {
            ReadIssued,
            WriteIssued,
            ReadCompleted,
            WriteCompleted
        };

        enum ContextState
        {
            AuthContinue,
            AuthOk,
            AuthOkAck,
            AuthFailed
        };

        enum State
        {
            Ready,
            Reading,
            Writing
        };

        PSecurityFunctionTable getSecurityFunctionTable() const;

        void acquireCredentials(
            const tstring &userName = _T(""),
            const tstring &password = _T(""),
            const tstring &domain = _T(""));
        void freeCredentials();

        void init();
        void deinit();
        void reset();

        void read(const ByteBuffer &byteBuffer, std::size_t bytesRequested);
        void write(const std::vector<ByteBuffer> &byteBuffers);
        void onReadCompleted(const ByteBuffer &byteBuffer, int error);
        void onWriteCompleted(std::size_t bytesTransferred, int error);

        void handleEvent(Event event);
        void readBuffer();
        void writeBuffer();
        void encryptWriteBuffer();
        void decryptReadBuffer();
        bool completeReadBlock();
        bool completeWriteBlock();
        bool completeBlock();
        void resumeUserIo();
        void resizeReadBuffer(std::size_t newSize);
        void resizeWriteBuffer(std::size_t newSize);

        virtual void handleHandshakeEvent() = 0;

    protected:

        const tstring                           mPackageName;
        const tstring                           mPackageList;
        const tstring                           mTarget;
        QualityOfProtection                     mQop;
        ULONG                                    mContextRequirements;

        bool                                    mHaveContext;
        bool                                    mHaveCredentials;
        bool                                    mImplicitCredentials;
        SecPkgInfo                              mPkgInfo;
        CtxtHandle                              mContext;
        CredHandle                              mCredentials;

        ContextState                            mContextState;
        State                                   mPreState;
        State                                   mPostState;
        Event                                   mEvent;
        const bool                              mServer;

        ByteBuffer                              mReadByteBufferOrig;
        ByteBuffer                              mWriteByteBufferOrig;
        std::size_t                             mBytesRequestedOrig;

        ByteBuffer                              mReadByteBuffer;
        boost::shared_ptr<std::vector<char> >   mReadBufferVectorPtr;
        char *                                  mReadBuffer;
        std::size_t                             mReadBufferPos;
        std::size_t                             mReadBufferLen;

        ByteBuffer                              mWriteByteBuffer;
        boost::shared_ptr<std::vector<char> >   mWriteBufferVectorPtr;
        char *                                  mWriteBuffer;
        std::size_t                             mWriteBufferPos;
        std::size_t                             mWriteBufferLen;

        std::vector<ByteBuffer>                 mByteBuffers;
        ByteBuffer                              mTempByteBuffer;

    private:
        bool mLimitRecursion;
        RecursionState<ByteBuffer, int> mRecursionStateRead;
        RecursionState<std::size_t, int> mRecursionStateWrite;
        void onReadCompleted_(const ByteBuffer &byteBuffer, int error);
        void onWriteCompleted_(std::size_t bytesTransferred, int error);

        template<typename StateT, typename Pfn, typename T1, typename T2>
        void applyRecursionLimiter(StateT &state, Pfn pfn, const T1 &t1, const T2 &t2)
        {
            state.assign(t1, t2);
            if (state.mRecursing)
            {
                state.mRecursing = false;
            }
            else
            {
                // gcc295 seems to need the namespace qualifier for make_obj_guard anyway
                using namespace boost::multi_index::detail;
                scope_guard guard = boost::multi_index::detail::make_obj_guard(
                    state,
                    &StateT::clear);
                RCF_UNUSED_VARIABLE(guard);
                while (!state.mRecursing)
                {
                    state.mRecursing = true;
                    (this->*pfn)(state.mT1, state.mT2);
                }
            }
        }

    };

    // server filters

    class SspiServerFilter : public SspiFilterBase
    {
    public:
        SspiServerFilter(const tstring &packageName, const tstring &packageList);

    private:
        bool doHandshake();
        void handleHandshakeEvent();
    };

    class SspiNtlmServerFilter : public SspiServerFilter
    {
    public:
        SspiNtlmServerFilter();
        const FilterDescription & getFilterDescription() const;
        static const FilterDescription & sGetFilterDescription();

        // TODO: should be private
        static const FilterDescription *spFilterDescription;
    };

    class SspiKerberosServerFilter : public SspiServerFilter
    {
    public:
        SspiKerberosServerFilter();
        const FilterDescription & getFilterDescription() const;
        static const FilterDescription & sGetFilterDescription();

        // TODO: should be private
        static const FilterDescription *spFilterDescription;
    };

    class SspiNegotiateServerFilter : public SspiServerFilter
    {
    public:
        SspiNegotiateServerFilter(const tstring &packageList);
        const FilterDescription & getFilterDescription() const;
        static const FilterDescription & sGetFilterDescription();

        // TODO: should be private
        static const FilterDescription *spFilterDescription;
    };

    // filter factories

    class SspiNtlmFilterFactory : public FilterFactory
    {
    public:
        FilterPtr createFilter();
        const FilterDescription & getFilterDescription();
    };

    class SspiKerberosFilterFactory : public FilterFactory
    {
    public:
        FilterPtr createFilter();
        const FilterDescription & getFilterDescription();
    };

    class SspiNegotiateFilterFactory : public FilterFactory
    {
    public:
        SspiNegotiateFilterFactory(const tstring &packageList = _T("Kerberos, NTLM"));
        FilterPtr createFilter();
        const FilterDescription & getFilterDescription();
    private:
        tstring mPackageList;
    };

    // client filters

    class SspiClientFilter : public SspiFilterBase
    {
    public:
        SspiClientFilter(
            const tstring &userName,
            const tstring &password,
            const tstring &domain,
            const tstring &targetName,
            QualityOfProtection qop,
            ULONG contextRequirements,
            const tstring &packageName,
            const tstring &packageList) :
                SspiFilterBase(
                    userName, password, domain,
                    targetName, qop, contextRequirements, packageName, packageList)
        {}

        SspiClientFilter(
            const tstring &targetName,
            QualityOfProtection qop,
            ULONG contextRequirements,
            const tstring &packageName,
            const tstring &packageList) :
            SspiFilterBase(
                targetName, qop, contextRequirements, packageName, packageList)
        {}

    private:
        bool doHandshake();
        void handleHandshakeEvent();
    };

    class SspiNtlmClientFilter : public SspiClientFilter
    {
    public:
        SspiNtlmClientFilter(
            const tstring &userName,
            const tstring &password,
            const tstring &domain,
            QualityOfProtection qop = SspiFilterBase::Encryption,
            ULONG contextRequirements = DefaultSspiContextRequirements) :
                SspiClientFilter(
                    userName, password, domain,
                    _T(""), qop, contextRequirements, _T("NTLM"), _T(""))
        {}

        SspiNtlmClientFilter(
            QualityOfProtection qop = SspiFilterBase::Encryption,
            ULONG contextRequirements = DefaultSspiContextRequirements) :
            SspiClientFilter(
                _T(""), qop, contextRequirements, _T("NTLM"), _T(""))
        {}

        const FilterDescription & getFilterDescription() const
        {
            return sGetFilterDescription();
        }

        static const FilterDescription & sGetFilterDescription()
        {
            return SspiNtlmServerFilter::sGetFilterDescription();
        }

    };

    class SspiKerberosClientFilter : public SspiClientFilter
    {
    public:
        SspiKerberosClientFilter(
            const tstring &userName,
            const tstring &password,
            const tstring &domain,
            const tstring &targetName,
            QualityOfProtection qop = SspiFilterBase::None,
            ULONG contextRequirements = DefaultSspiContextRequirements) :
                SspiClientFilter(
                    userName, password, domain,
                    targetName, qop, contextRequirements, _T("Kerberos"), _T(""))
        {}

        SspiKerberosClientFilter(
            const tstring &targetName,
            QualityOfProtection qop = SspiFilterBase::Encryption,
            ULONG contextRequirements = DefaultSspiContextRequirements) :
                SspiClientFilter(
                    targetName,
                    qop, contextRequirements, _T("Kerberos"), _T(""))
        {}

        const FilterDescription & getFilterDescription() const
        {
            return sGetFilterDescription();
        }

        static const FilterDescription & sGetFilterDescription()
        {
            return SspiKerberosServerFilter::sGetFilterDescription();
        }
    };

    class SspiNegotiateClientFilter : public SspiClientFilter
    {
    public:
        SspiNegotiateClientFilter(
            const tstring &userName,
            const tstring &password,
            const tstring &domain,
            const tstring &targetName,
            QualityOfProtection qop = SspiFilterBase::None,
            ULONG contextRequirements = DefaultSspiContextRequirements) :
                SspiClientFilter(
                    userName, password, domain,
                    targetName, qop, contextRequirements, _T("Negotiate"), _T("Kerberos,NTLM"))
        {}

        SspiNegotiateClientFilter(
            const tstring &targetName,
            QualityOfProtection qop = SspiFilterBase::Encryption,
            ULONG contextRequirements = DefaultSspiContextRequirements) :
                SspiClientFilter(
                    targetName,
                    qop, contextRequirements, _T("Negotiate"), _T("Kerberos,NTLM"))
        {}

        const FilterDescription & getFilterDescription() const
        {
            return sGetFilterDescription();
        }

        static const FilterDescription & sGetFilterDescription()
        {
            return SspiNegotiateServerFilter::sGetFilterDescription();
        }
    };

    typedef SspiNtlmClientFilter        SspiNtlmFilter;
    typedef SspiKerberosClientFilter    SspiKerberosFilter;
    typedef SspiNegotiateClientFilter   SspiNegotiateFilter;

} // namespace RCF

#endif // ! INCLUDE_RCF_SSPIFILTER_HPP

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jarl Lindrud

Australia Australia
Software developer, ex-resident of Sweden and now living in Canberra, Australia, working on distributed C++ applications. Jarl enjoys programming, but prefers skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 25 Oct 2011
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid