1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
5 *                           Sakthi Kannan <saktr@amazon.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to
9 * deal in the Software without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26#include <private-lib-core.h>
27
28#define MQTT_CONNECT_MSG_BASE_LEN (12)
29
30struct lws *
31lws_mqtt_client_send_connect(struct lws *wsi)
32{
33	/* static int */
34	/* 	lws_mqttc_abs_writeable(lws_abs_protocol_inst_t *api, size_t budget) */
35	const lws_mqttc_t *c = &wsi->mqtt->client;
36	uint8_t b[256 + LWS_PRE], *start = b + LWS_PRE, *p = start;
37	unsigned int len = MQTT_CONNECT_MSG_BASE_LEN;
38
39	switch (lwsi_state(wsi)) {
40	case LRS_MQTTC_IDLE:
41		/*
42		 * Transport connected - this is our chance to do the
43		 * protocol connect action.
44		 */
45
46		/* 1. Fixed Headers */
47		if (lws_mqtt_fill_fixed_header(p++, LMQCP_CTOS_CONNECT, 0, 0, 0)) {
48			lwsl_err("%s: Failled to fill fixed header\n", __func__);
49			return NULL;
50		}
51
52		/*
53		 * 2. Remaining length - Add the lengths of client ID,
54		 * username and password and their length fields if
55		 * the respective flags are set.
56		 */
57		len +=  c->id->len;
58		if (c->conn_flags & LMQCFT_USERNAME && c->username) {
59			len = len + (unsigned int)c->username->len + 2;
60			if (c->conn_flags & LMQCFT_PASSWORD)
61				len += (unsigned int)(c->password ? c->password->len : 0) + 2u;
62		}
63		if (c->conn_flags & LMQCFT_WILL_FLAG && c->will.topic) {
64			len = len + (unsigned int)c->will.topic->len + 2;
65			len += (c->will.message ? c->will.message->len : 0) + 2u;
66		}
67		p += lws_mqtt_vbi_encode(len, p);
68
69		/*
70		 * 3. Variable Header - Protocol name & level, Connect
71		 * flags and keep alive time (in secs).
72		 */
73		lws_ser_wu16be(p, 4); /* Length of protocol name */
74		p += 2;
75		*p++ = 'M';
76		*p++ = 'Q';
77		*p++ = 'T';
78		*p++ = 'T';
79		*p++ = MQTT_VER_3_1_1;
80		*p++ = (uint8_t)c->conn_flags;
81		lws_ser_wu16be(p, c->keep_alive_secs);
82		p += 2;
83
84		/*
85		 * 4. Payload - Client ID, Will topic & message,
86		 * Username & password.
87		 */
88		if (lws_mqtt_str_is_not_empty(c->id)) {
89			lws_ser_wu16be(p, c->id->len);
90			p += 2;
91			memcpy(p, c->id->buf, c->id->len);
92			p += c->id->len;
93		} else {
94			/*
95			 * If the Client supplies a zero-byte
96			 * ClientId, the Client MUST also set
97			 * CleanSession to 1 [MQTT-3.1.3-7].
98			 */
99			if (!(c->conn_flags & LMQCFT_CLEAN_START)) {
100				lwsl_err("%s: Empty client ID needs a clean start\n",
101					 __func__);
102				return NULL;
103			}
104			*p++ = 0;
105		}
106
107		if (c->conn_flags & LMQCFT_WILL_FLAG) {
108			if (lws_mqtt_str_is_not_empty(c->will.topic)) {
109				lws_ser_wu16be(p, c->will.topic->len);
110				p += 2;
111				memcpy(p, c->will.topic->buf, c->will.topic->len);
112				p += c->will.topic->len;
113				if (lws_mqtt_str_is_not_empty(c->will.message)) {
114					lws_ser_wu16be(p, c->will.message->len);
115					p += 2;
116					memcpy(p, c->will.message->buf,
117					       c->will.message->len);
118					p += c->will.message->len;
119				} else {
120					lws_ser_wu16be(p, 0);
121					p += 2;
122				}
123			} else {
124				lwsl_err("%s: Missing Will Topic\n", __func__);
125				return NULL;
126			}
127		}
128		if (c->conn_flags & LMQCFT_USERNAME) {
129			/*
130			 * Detailed sanity check on the username and
131			 * password strings.
132			 */
133			if (lws_mqtt_str_is_not_empty(c->username)) {
134				lws_ser_wu16be(p, c->username->len);
135				p += 2;
136				memcpy(p, c->username->buf, c->username->len);
137				p += c->username->len;
138			} else {
139				lwsl_err("%s: Empty / missing Username!\n",
140					 __func__);
141				return NULL;
142			}
143			if (c->conn_flags & LMQCFT_PASSWORD) {
144				if (lws_mqtt_str_is_not_empty(c->password)) {
145					lws_ser_wu16be(p, c->password->len);
146					p += 2;
147					memcpy(p, c->password->buf,
148					       c->password->len);
149					p += c->password->len;
150				} else {
151					lws_ser_wu16be(p, 0);
152					p += 2;
153				}
154			}
155		} else if (c->conn_flags & LMQCFT_PASSWORD) {
156			lwsl_err("%s: Unsupported - Password without username\n",
157				 __func__);
158			return NULL;
159		}
160		break;
161	default:
162		lwsl_err("%s: unexpected state %d\n", __func__, lwsi_state(wsi));
163
164		return NULL;
165	}
166
167	/*
168	 * Perform the actual write
169	 */
170	if (lws_write(wsi, (unsigned char *)&b[LWS_PRE], lws_ptr_diff_size_t(p, start),
171		  LWS_WRITE_BINARY) != lws_ptr_diff(p, start)) {
172		lwsl_notice("%s: write failed\n", __func__);
173
174		return NULL;
175	}
176
177	return wsi;
178}
179
180struct lws *
181lws_mqtt_client_send_disconnect(struct lws *wsi)
182{
183	uint8_t b[256 + LWS_PRE], *start = b + LWS_PRE, *p = start;
184
185	/* 1. Fixed Headers */
186	if (lws_mqtt_fill_fixed_header(p++, LMQCP_DISCONNECT, 0, 0, 0))
187	{
188		lwsl_err("%s: Failled to fill fixed header\n", __func__);
189		return NULL;
190	}
191	*p++ = 0;
192	if (lws_write(wsi, (unsigned char *)&b[LWS_PRE], lws_ptr_diff_size_t(p, start),
193				LWS_WRITE_BINARY) != lws_ptr_diff(p, start)) {
194		lwsl_err("%s: write failed\n", __func__);
195
196		return NULL;
197	}
198
199	return wsi;
200}
201