mechanism.hpp 3.35 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __ZMQ_MECHANISM_HPP_INCLUDED__
#define __ZMQ_MECHANISM_HPP_INCLUDED__

23
#include "stdint.hpp"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include "options.hpp"
#include "blob.hpp"

namespace zmq
{

    //  Abstract class representing security mechanism.
    //  Different mechanism extedns this class.

    class msg_t;

    class mechanism_t
    {
    public:

        mechanism_t (const options_t &options_);

        virtual ~mechanism_t ();

43 44
        //  Prepare next handshake command that is to be sent to the peer.
        virtual int next_handshake_command (msg_t *msg_) = 0;
45

46 47
        //  Process the handshake command received from the peer.
        virtual int process_handshake_command (msg_t *msg_) = 0;
48

49
        virtual int encode (msg_t *) { return 0; }
50

51
        virtual int decode (msg_t *) { return 0; }
52

53 54 55
        //  Notifies mechanism about availability of ZAP message.
        virtual int zap_msg_available () { return 0; }

56 57 58 59 60 61 62
        //  True iff the handshake stage is complete?
        virtual bool is_handshake_complete () const = 0;

        void set_peer_identity (const void *id_ptr, size_t id_size);

        void peer_identity (msg_t *msg_);

63 64 65 66
        void set_user_id (const void *user_id, size_t size);

        blob_t get_user_id () const;

67 68
    protected:

69 70
        //  Only used to identify the socket for the Socket-Type
        //  property in the wire protocol.
71 72 73 74 75
        const char *socket_type_string (int socket_type) const;

        size_t add_property (unsigned char *ptr, const char *name,
            const void *value, size_t value_len) const;

76 77 78 79 80
        //  Parses a metadata.
        //  Metadata consists of a list of properties consisting of
        //  name and value as size-specified strings.
        //  Returns 0 on success and -1 on error, in which case errno is set.
        int parse_metadata (const unsigned char *ptr_, size_t length);
81 82 83 84

        //  This is called by parse_property method whenever it
        //  parses a new property. The function should return 0
        //  on success and -1 on error, in which case it should
85
        //  set errno. Signaling error prevents parser from
86 87 88
        //  parsing remaining data.
        //  Derived classes are supposed to override this
        //  method to handle custom processing.
89
        virtual int property (const std::string& name_,
90 91
                              const void *value_, size_t length_);

92 93 94 95 96
        options_t options;

    private:

        blob_t identity;
97

98 99
        blob_t user_id;

100 101
        //  Returns true iff socket associated with the mechanism
        //  is compatible with a given socket type 'type_'.
102
        bool check_socket_type (const std::string& type_) const;
103 104 105 106 107
    };

}

#endif