1 /*
2     Copyright (c) 2007-2019 Contributors as noted in the AUTHORS file
3 
4     This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6     libzmq is free software; you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 3 of the License, or
9     (at your option) any later version.
10 
11     As a special exception, the Contributors give you permission to link
12     this library with independent modules to produce an executable,
13     regardless of the license terms of these independent modules, and to
14     copy and distribute the resulting executable under terms of your choice,
15     provided that you also meet, for each linked independent module, the
16     terms and conditions of the license of that module. An independent
17     module is a module which is not derived from or based on this library.
18     If you modify this library, you must extend this exception to your
19     version of the library.
20 
21     libzmq is distributed in the hope that it will be useful, but WITHOUT
22     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23     FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24     License for more details.
25 
26     You should have received a copy of the GNU Lesser General Public License
27     along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "precompiled.hpp"
31 #include "ws_protocol.hpp"
32 #include "ws_encoder.hpp"
33 #include "msg.hpp"
34 #include "likely.hpp"
35 #include "wire.hpp"
36 #include "random.hpp"
37 
38 #include <limits.h>
39 
ws_encoder_t(size_t bufsize_,bool must_mask_)40 zmq::ws_encoder_t::ws_encoder_t (size_t bufsize_, bool must_mask_) :
41     encoder_base_t<ws_encoder_t> (bufsize_),
42     _must_mask (must_mask_)
43 {
44     //  Write 0 bytes to the batch and go to message_ready state.
45     next_step (NULL, 0, &ws_encoder_t::message_ready, true);
46     _masked_msg.init ();
47 }
48 
~ws_encoder_t()49 zmq::ws_encoder_t::~ws_encoder_t ()
50 {
51     _masked_msg.close ();
52 }
53 
message_ready()54 void zmq::ws_encoder_t::message_ready ()
55 {
56     int offset = 0;
57 
58     _is_binary = false;
59 
60     if (in_progress ()->is_ping ())
61         _tmp_buf[offset++] = 0x80 | zmq::ws_protocol_t::opcode_ping;
62     else if (in_progress ()->is_pong ())
63         _tmp_buf[offset++] = 0x80 | zmq::ws_protocol_t::opcode_pong;
64     else if (in_progress ()->is_close_cmd ())
65         _tmp_buf[offset++] = 0x80 | zmq::ws_protocol_t::opcode_close;
66     else {
67         _tmp_buf[offset++] = 0x82; // Final | binary
68         _is_binary = true;
69     }
70 
71     _tmp_buf[offset] = _must_mask ? 0x80 : 0x00;
72 
73     size_t size = in_progress ()->size ();
74     if (_is_binary)
75         size++;
76     //  TODO: create an opcode for subscribe/cancel
77     if (in_progress ()->is_subscribe () || in_progress ()->is_cancel ())
78         size++;
79 
80     if (size <= 125)
81         _tmp_buf[offset++] |= static_cast<unsigned char> (size & 127);
82     else if (size <= 0xFFFF) {
83         _tmp_buf[offset++] |= 126;
84         _tmp_buf[offset++] = static_cast<unsigned char> ((size >> 8) & 0xFF);
85         _tmp_buf[offset++] = static_cast<unsigned char> (size & 0xFF);
86     } else {
87         _tmp_buf[offset++] |= 127;
88         put_uint64 (_tmp_buf + offset, size);
89         offset += 8;
90     }
91 
92     if (_must_mask) {
93         const uint32_t random = generate_random ();
94         put_uint32 (_tmp_buf + offset, random);
95         put_uint32 (_mask, random);
96         offset += 4;
97     }
98 
99     int mask_index = 0;
100     if (_is_binary) {
101         //  Encode flags.
102         unsigned char protocol_flags = 0;
103         if (in_progress ()->flags () & msg_t::more)
104             protocol_flags |= ws_protocol_t::more_flag;
105         if (in_progress ()->flags () & msg_t::command)
106             protocol_flags |= ws_protocol_t::command_flag;
107 
108         _tmp_buf[offset++] =
109           _must_mask ? protocol_flags ^ _mask[mask_index++] : protocol_flags;
110     }
111 
112     //  Encode the subscribe/cancel byte.
113     //  TODO: remove once there is an opcode for subscribe/cancel
114     if (in_progress ()->is_subscribe ())
115         _tmp_buf[offset++] = _must_mask ? 1 ^ _mask[mask_index++] : 1;
116     else if (in_progress ()->is_cancel ())
117         _tmp_buf[offset++] = _must_mask ? 0 ^ _mask[mask_index++] : 0;
118 
119     next_step (_tmp_buf, offset, &ws_encoder_t::size_ready, false);
120 }
121 
size_ready()122 void zmq::ws_encoder_t::size_ready ()
123 {
124     if (_must_mask) {
125         assert (in_progress () != &_masked_msg);
126         const size_t size = in_progress ()->size ();
127 
128         unsigned char *src =
129           static_cast<unsigned char *> (in_progress ()->data ());
130         unsigned char *dest = src;
131 
132         //  If msg is shared or data is constant we cannot mask in-place, allocate a new msg for it
133         if (in_progress ()->flags () & msg_t::shared
134             || in_progress ()->is_cmsg ()) {
135             _masked_msg.close ();
136             _masked_msg.init_size (size);
137             dest = static_cast<unsigned char *> (_masked_msg.data ());
138         }
139 
140         int mask_index = 0;
141         if (_is_binary)
142             ++mask_index;
143         //  TODO: remove once there is an opcode for subscribe/cancel
144         if (in_progress ()->is_subscribe () || in_progress ()->is_cancel ())
145             ++mask_index;
146         for (size_t i = 0; i < size; ++i, mask_index++)
147             dest[i] = src[i] ^ _mask[mask_index % 4];
148 
149         next_step (dest, size, &ws_encoder_t::message_ready, true);
150     } else {
151         next_step (in_progress ()->data (), in_progress ()->size (),
152                    &ws_encoder_t::message_ready, true);
153     }
154 }
155