mechanism.hpp 3.88 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
#include "options.hpp"
#include "blob.hpp"
26
#include "metadata.hpp"
27 28 29 30 31 32 33 34 35 36 37 38 39

namespace zmq
{

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

    class msg_t;

    class mechanism_t
    {
    public:

40 41 42 43 44 45
        enum status_t {
            handshaking,
            ready,
            error
        };

46 47 48 49
        mechanism_t (const options_t &options_);

        virtual ~mechanism_t ();

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

53 54
        //  Process the handshake command received from the peer.
        virtual int process_handshake_command (msg_t *msg_) = 0;
55

56
        virtual int encode (msg_t *) { return 0; }
57

58
        virtual int decode (msg_t *) { return 0; }
59

60 61 62
        //  Notifies mechanism about availability of ZAP message.
        virtual int zap_msg_available () { return 0; }

63 64
        //  Returns the status of this mechanism.
        virtual status_t status () const = 0;
65 66 67 68 69

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

        void peer_identity (msg_t *msg_);

70 71 72 73
        void set_user_id (const void *user_id, size_t size);

        blob_t get_user_id () const;

74 75 76 77 78 79 80
        const metadata_t::dict_t& get_zmtp_properties () {
            return zmtp_properties;
        }

        const metadata_t::dict_t& get_zap_properties () {
            return zap_properties;
        }
81

82 83
    protected:

84 85
        //  Only used to identify the socket for the Socket-Type
        //  property in the wire protocol.
86 87 88 89 90
        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;

91 92 93 94
        //  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.
95 96
        int parse_metadata (
            const unsigned char *ptr_, size_t length, bool zap_flag = false);
97 98 99 100

        //  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
101
        //  set errno. Signaling error prevents parser from
102 103 104
        //  parsing remaining data.
        //  Derived classes are supposed to override this
        //  method to handle custom processing.
105
        virtual int property (const std::string& name_,
106 107
                              const void *value_, size_t length_);

108 109 110 111 112
        //  Properties received from ZMTP peer.
        metadata_t::dict_t zmtp_properties;

        //  Properties received from ZAP server.
        metadata_t::dict_t zap_properties;
113

114 115 116 117 118
        options_t options;

    private:

        blob_t identity;
119

120 121
        blob_t user_id;

122 123
        //  Returns true iff socket associated with the mechanism
        //  is compatible with a given socket type 'type_'.
124
        bool check_socket_type (const std::string& type_) const;
125 126 127 128 129
    };

}

#endif