• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 struct lws *
lws_mqtt_client_send_connect(struct lws * wsi)31 lws_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 		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 += c->username->len + 2;
60 			if (c->conn_flags & LMQCFT_PASSWORD)
61 				len += (c->password ? c->password->len : 0) + 2;
62 		}
63 		if (c->conn_flags & LMQCFT_WILL_FLAG && c->will.topic) {
64 			len += c->will.topic->len + 2;
65 			len += (c->will.message ? c->will.message->len : 0) + 2;
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++ = 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_CLEAN_START) == 0) {
108 			*p++ = 0; /* no properties */
109 			break;
110 		}
111 		if (c->conn_flags & LMQCFT_WILL_FLAG) {
112 			if (lws_mqtt_str_is_not_empty(c->will.topic)) {
113 				lws_ser_wu16be(p, c->will.topic->len);
114 				p += 2;
115 				memcpy(p, c->will.topic->buf, c->will.topic->len);
116 				p += c->will.topic->len;
117 				if (lws_mqtt_str_is_not_empty(c->will.message)) {
118 					lws_ser_wu16be(p, c->will.topic->len);
119 					p += 2;
120 					memcpy(p, c->will.message->buf,
121 					       c->will.message->len);
122 					p += c->will.message->len;
123 				} else {
124 					lws_ser_wu16be(p, 0);
125 					p += 2;
126 				}
127 			} else {
128 				lwsl_err("%s: Missing Will Topic\n", __func__);
129 				return NULL;
130 			}
131 		}
132 		if (c->conn_flags & LMQCFT_USERNAME) {
133 			/*
134 			 * Detailed sanity check on the username and
135 			 * password strings.
136 			 */
137 			if (lws_mqtt_str_is_not_empty(c->username)) {
138 				lws_ser_wu16be(p, c->username->len);
139 				p += 2;
140 				memcpy(p, c->username->buf, c->username->len);
141 				p += c->username->len;
142 			} else {
143 				lwsl_err("%s: Empty / missing Username!\n",
144 					 __func__);
145 				return NULL;
146 			}
147 			if (c->conn_flags & LMQCFT_PASSWORD) {
148 				if (lws_mqtt_str_is_not_empty(c->password)) {
149 					lws_ser_wu16be(p, c->password->len);
150 					p += 2;
151 					memcpy(p, c->password->buf,
152 					       c->password->len);
153 					p += c->password->len;
154 				} else {
155 					lws_ser_wu16be(p, 0);
156 					p += 2;
157 				}
158 			}
159 		} else if (c->conn_flags & LMQCFT_PASSWORD) {
160 			lwsl_err("%s: Unsupported - Password without username\n",
161 				 __func__);
162 			return NULL;
163 		}
164 		break;
165 	default:
166 		lwsl_err("%s: unexpected state %d\n", __func__, lwsi_state(wsi));
167 
168 		return NULL;
169 	}
170 
171 	/*
172 	 * Perform the actual write
173 	 */
174 	if (lws_write(wsi, (unsigned char *)&b[LWS_PRE], lws_ptr_diff(p, start),
175 		  LWS_WRITE_BINARY) != lws_ptr_diff(p, start)) {
176 		lwsl_notice("%s: write failed\n", __func__);
177 
178 		return NULL;
179 	}
180 
181 	return wsi;
182 }
183