mechanism.hpp 5.14 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

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

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq 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.
25 26 27 28 29 30 31 32

    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__

33
#include "stdint.hpp"
34 35
#include "options.hpp"
#include "blob.hpp"
36
#include "metadata.hpp"
37 38 39 40

namespace zmq
{

41 42 43
    class msg_t;
    class session_base_t;

44
    //  Abstract class representing security mechanism.
45
    //  Different mechanism extends this class.
46 47 48 49 50

    class mechanism_t
    {
    public:

51 52 53 54 55 56
        enum status_t {
            handshaking,
            ready,
            error
        };

57 58 59 60
        mechanism_t (const options_t &options_);

        virtual ~mechanism_t ();

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

64 65
        //  Process the handshake command received from the peer.
        virtual int process_handshake_command (msg_t *msg_) = 0;
66

67
        virtual int encode (msg_t *) { return 0; }
68

69
        virtual int decode (msg_t *) { return 0; }
70

71 72 73
        //  Notifies mechanism about availability of ZAP message.
        virtual int zap_msg_available () { return 0; }

74 75
        //  Returns the status of this mechanism.
        virtual status_t status () const = 0;
76

77
        void set_peer_routing_id (const void *id_ptr, size_t id_size);
78

79
        void peer_routing_id (msg_t *msg_);
80

81 82 83 84
        void set_user_id (const void *user_id, size_t size);

        blob_t get_user_id () const;

85 86 87 88 89 90 91
        const metadata_t::dict_t& get_zmtp_properties () {
            return zmtp_properties;
        }

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

93 94
    protected:

95 96
        //  Only used to identify the socket for the Socket-Type
        //  property in the wire protocol.
97 98
        const char *socket_type_string (int socket_type) const;

99
        static size_t add_property (unsigned char *ptr,
100
                                    size_t ptr_capacity,
101 102 103
                                    const char *name,
                                    const void *value,
                                    size_t value_len);
104 105 106 107 108 109 110 111 112
        static size_t property_len (const char *name,
                                    size_t value_len);

        size_t add_basic_properties (unsigned char *ptr, size_t ptr_capacity) const;
        size_t basic_properties_len () const;

        void make_command_with_basic_properties (msg_t *msg_,
                                                 const char *prefix,
                                                 size_t prefix_len) const;
113

114 115 116 117
        //  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.
118 119
        int parse_metadata (
            const unsigned char *ptr_, size_t length, bool zap_flag = false);
120 121 122 123

        //  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
124
        //  set errno. Signaling error prevents parser from
125 126 127
        //  parsing remaining data.
        //  Derived classes are supposed to override this
        //  method to handle custom processing.
128
        virtual int property (const std::string& name_,
129 130
                              const void *value_, size_t length_);

131 132 133 134 135
        //  Properties received from ZMTP peer.
        metadata_t::dict_t zmtp_properties;

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

137 138 139 140
        options_t options;

    private:

141
        blob_t routing_id;
142

143 144
        blob_t user_id;

145 146
        //  Returns true iff socket associated with the mechanism
        //  is compatible with a given socket type 'type_'.
147
        bool check_socket_type (const std::string& type_) const;
148 149
    };

150
}
151 152

#endif