MQTTPacketOut.c 12.5 KB
Newer Older
麦壳饼's avatar
麦壳饼 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
/*******************************************************************************
 * Copyright (c) 2009, 2020 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * and Eclipse Distribution License v1.0 which accompany this distribution. 
 *
 * The Eclipse Public License is available at 
 *    https://www.eclipse.org/legal/epl-2.0/
 * and the Eclipse Distribution License is available at 
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Ian Craggs - initial API and implementation and/or initial documentation
 *    Ian Craggs, Allan Stockdill-Mander - SSL updates
 *    Ian Craggs - MQTT 3.1.1 support
 *    Rong Xiang, Ian Craggs - C++ compatibility
 *    Ian Craggs - binary password and will payload
 *    Ian Craggs - MQTT 5.0 support
 *******************************************************************************/

/**
 * @file
 * \brief functions to deal with reading and writing of MQTT packets from and to sockets
 *
 * Some other related functions are in the MQTTPacket module
 */


#include "MQTTPacketOut.h"
#include "Log.h"
#include "StackTrace.h"

#include <string.h>
#include <stdlib.h>

#include "Heap.h"


/**
 * Send an MQTT CONNECT packet down a socket for V5 or later
 * @param client a structure from which to get all the required values
 * @param MQTTVersion the MQTT version to connect with
 * @param connectProperties MQTT V5 properties for the connect packet
 * @param willProperties MQTT V5 properties for the will message, if any
 * @return the completion code (e.g. TCPSOCKET_COMPLETE)
 */
int MQTTPacket_send_connect(Clients* client, int MQTTVersion,
	               MQTTProperties* connectProperties, MQTTProperties* willProperties)
{
	char *buf, *ptr;
	Connect packet;
	int rc = SOCKET_ERROR, len;

	FUNC_ENTRY;
	packet.header.byte = 0;
	packet.header.bits.type = CONNECT;

	len = ((MQTTVersion == MQTTVERSION_3_1) ? 12 : 10) + (int)strlen(client->clientID)+2;
	if (client->will)
		len += (int)strlen(client->will->topic)+2 + client->will->payloadlen+2;
	if (client->username)
		len += (int)strlen(client->username)+2;
	if (client->password)
		len += client->passwordlen+2;
	if (MQTTVersion >= MQTTVERSION_5)
	{
		len += MQTTProperties_len(connectProperties);
		if (client->will)
			len += MQTTProperties_len(willProperties);
	}

	ptr = buf = malloc(len);
	if (ptr == NULL)
		goto exit_nofree;
	if (MQTTVersion == MQTTVERSION_3_1)
	{
		writeUTF(&ptr, "MQIsdp");
		writeChar(&ptr, (char)MQTTVERSION_3_1);
	}
	else if (MQTTVersion == MQTTVERSION_3_1_1 || MQTTVersion == MQTTVERSION_5)
	{
		writeUTF(&ptr, "MQTT");
		writeChar(&ptr, (char)MQTTVersion);
	}
	else
		goto exit;

	packet.flags.all = 0;
	if (MQTTVersion >= MQTTVERSION_5)
		packet.flags.bits.cleanstart = client->cleanstart;
	else
		packet.flags.bits.cleanstart = client->cleansession;
	packet.flags.bits.will = (client->will) ? 1 : 0;
	if (packet.flags.bits.will)
	{
		packet.flags.bits.willQoS = client->will->qos;
		packet.flags.bits.willRetain = client->will->retained;
	}
	if (client->username)
		packet.flags.bits.username = 1;
	if (client->password)
		packet.flags.bits.password = 1;

	writeChar(&ptr, packet.flags.all);
	writeInt(&ptr, client->keepAliveInterval);
	if (MQTTVersion >= MQTTVERSION_5)
		MQTTProperties_write(&ptr, connectProperties);
	writeUTF(&ptr, client->clientID);
	if (client->will)
	{
		if (MQTTVersion >= MQTTVERSION_5)
			MQTTProperties_write(&ptr, willProperties);
		writeUTF(&ptr, client->will->topic);
		writeData(&ptr, client->will->payload, client->will->payloadlen);
	}
	if (client->username)
		writeUTF(&ptr, client->username);
	if (client->password)
		writeData(&ptr, client->password, client->passwordlen);

	rc = MQTTPacket_send(&client->net, packet.header, buf, len, 1, MQTTVersion);
	Log(LOG_PROTOCOL, 0, NULL, client->net.socket, client->clientID,
			MQTTVersion, client->cleansession, rc);
exit:
	if (rc != TCPSOCKET_INTERRUPTED)
		free(buf);
exit_nofree:
	FUNC_EXIT_RC(rc);
	return rc;
}


/**
 * Function used in the new packets table to create connack packets.
 * @param MQTTVersion MQTT 5 or less?
 * @param aHeader the MQTT header byte
 * @param data the rest of the packet
 * @param datalen the length of the rest of the packet
 * @return pointer to the packet structure
 */
void* MQTTPacket_connack(int MQTTVersion, unsigned char aHeader, char* data, size_t datalen)
{
	Connack* pack = NULL;
	char* curdata = data;
	char* enddata = &data[datalen];

	FUNC_ENTRY;
	if ((pack = malloc(sizeof(Connack))) == NULL)
		goto exit;
	pack->MQTTVersion = MQTTVersion;
	pack->header.byte = aHeader;
	pack->flags.all = readChar(&curdata); /* connect flags */
	pack->rc = readChar(&curdata); /* reason code */
	if (MQTTVersion < MQTTVERSION_5)
	{
		if (datalen != 2)
		{
			free(pack);
			pack = NULL;
		}
	}
	else if (datalen > 2)
	{
		MQTTProperties props = MQTTProperties_initializer;
		pack->properties = props;
		if (MQTTProperties_read(&pack->properties, &curdata, enddata) != 1)
		{
			if (pack->properties.array)
				free(pack->properties.array);
			if (pack)
				free(pack);
			pack = NULL; /* signal protocol error */
			goto exit;
		}
	}
exit:
	FUNC_EXIT;
	return pack;
}


/**
 * Free allocated storage for a connack packet.
 * @param pack pointer to the connack packet structure
 */
void MQTTPacket_freeConnack(Connack* pack)
{
	FUNC_ENTRY;
	if (pack->MQTTVersion >= MQTTVERSION_5)
		MQTTProperties_free(&pack->properties);
	free(pack);
	FUNC_EXIT;
}


/**
 * Send an MQTT PINGREQ packet down a socket.
 * @param socket the open socket to send the data to
 * @param clientID the string client identifier, only used for tracing
 * @return the completion code (e.g. TCPSOCKET_COMPLETE)
 */
int MQTTPacket_send_pingreq(networkHandles* net, const char* clientID)
{
	Header header;
	int rc = 0;

	FUNC_ENTRY;
	header.byte = 0;
	header.bits.type = PINGREQ;
	rc = MQTTPacket_send(net, header, NULL, 0, 0, MQTTVERSION_3_1_1);
	Log(LOG_PROTOCOL, 20, NULL, net->socket, clientID, rc);
	FUNC_EXIT_RC(rc);
	return rc;
}


/**
 * Send an MQTT subscribe packet down a socket.
 * @param topics list of topics
 * @param qoss list of corresponding QoSs
 * @param msgid the MQTT message id to use
 * @param dup boolean - whether to set the MQTT DUP flag
 * @param socket the open socket to send the data to
 * @param clientID the string client identifier, only used for tracing
 * @return the completion code (e.g. TCPSOCKET_COMPLETE)
 */
int MQTTPacket_send_subscribe(List* topics, List* qoss, MQTTSubscribe_options* opts, MQTTProperties* props,
		int msgid, int dup, Clients* client)
{
	Header header;
	char *data, *ptr;
	int rc = -1;
	ListElement *elem = NULL, *qosElem = NULL;
	int datalen, i = 0;

	FUNC_ENTRY;
	header.bits.type = SUBSCRIBE;
	header.bits.dup = dup;
	header.bits.qos = 1;
	header.bits.retain = 0;

	datalen = 2 + topics->count * 3; /* utf length + char qos == 3 */
	while (ListNextElement(topics, &elem))
		datalen += (int)strlen((char*)(elem->content));
	if (client->MQTTVersion >= MQTTVERSION_5)
		datalen += MQTTProperties_len(props);

	ptr = data = malloc(datalen);
	if (ptr == NULL)
		goto exit;
	writeInt(&ptr, msgid);

	if (client->MQTTVersion >= MQTTVERSION_5)
		MQTTProperties_write(&ptr, props);

	elem = NULL;
	while (ListNextElement(topics, &elem))
	{
		char subopts = 0;

		ListNextElement(qoss, &qosElem);
		writeUTF(&ptr, (char*)(elem->content));
		subopts = *(int*)(qosElem->content);
		if (client->MQTTVersion >= MQTTVERSION_5 && opts != NULL)
		{
			subopts |= (opts[i].noLocal << 2); /* 1 bit */
			subopts |= (opts[i].retainAsPublished << 3); /* 1 bit */
			subopts |= (opts[i].retainHandling << 4); /* 2 bits */
		}
		writeChar(&ptr, subopts);
		++i;
	}
	rc = MQTTPacket_send(&client->net, header, data, datalen, 1, client->MQTTVersion);
	Log(LOG_PROTOCOL, 22, NULL, client->net.socket, client->clientID, msgid, rc);
	if (rc != TCPSOCKET_INTERRUPTED)
		free(data);
exit:
	FUNC_EXIT_RC(rc);
	return rc;
}


/**
 * Function used in the new packets table to create suback packets.
 * @param MQTTVersion the version of MQTT
 * @param aHeader the MQTT header byte
 * @param data the rest of the packet
 * @param datalen the length of the rest of the packet
 * @return pointer to the packet structure
 */
void* MQTTPacket_suback(int MQTTVersion, unsigned char aHeader, char* data, size_t datalen)
{
	Suback* pack = NULL;
	char* curdata = data;
	char* enddata = &data[datalen];

	FUNC_ENTRY;
	if ((pack = malloc(sizeof(Suback))) == NULL)
		goto exit;
	pack->MQTTVersion = MQTTVersion;
	pack->header.byte = aHeader;
	pack->msgId = readInt(&curdata);
	if (MQTTVersion >= MQTTVERSION_5)
	{
		MQTTProperties props = MQTTProperties_initializer;
		pack->properties = props;
		if (MQTTProperties_read(&pack->properties, &curdata, enddata) != 1)
		{
			if (pack->properties.array)
				free(pack->properties.array);
			if (pack)
				free(pack);
			pack = NULL; /* signal protocol error */
			goto exit;
		}
	}
	pack->qoss = ListInitialize();
	while ((size_t)(curdata - data) < datalen)
	{
		unsigned int* newint;
		newint = malloc(sizeof(unsigned int));
		if (newint == NULL)
		{
			if (pack->properties.array)
				free(pack->properties.array);
			if (pack)
				free(pack);
			pack = NULL; /* signal protocol error */
			goto exit;
		}
		*newint = (unsigned int)readChar(&curdata);
		ListAppend(pack->qoss, newint, sizeof(unsigned int));
	}
	if (pack->qoss->count == 0)
	{
		if (pack->properties.array)
			free(pack->properties.array);
		if (pack)
			free(pack);
		ListFree(pack->qoss);
		pack = NULL;
	}
exit:
	FUNC_EXIT;
	return pack;
}


/**
 * Send an MQTT unsubscribe packet down a socket.
 * @param topics list of topics
 * @param msgid the MQTT message id to use
 * @param dup boolean - whether to set the MQTT DUP flag
 * @param socket the open socket to send the data to
 * @param clientID the string client identifier, only used for tracing
 * @return the completion code (e.g. TCPSOCKET_COMPLETE)
 */
int MQTTPacket_send_unsubscribe(List* topics, MQTTProperties* props, int msgid, int dup, Clients* client)
{
	Header header;
	char *data, *ptr;
	int rc = SOCKET_ERROR;
	ListElement *elem = NULL;
	int datalen;

	FUNC_ENTRY;
	header.bits.type = UNSUBSCRIBE;
	header.bits.dup = dup;
	header.bits.qos = 1;
	header.bits.retain = 0;

	datalen = 2 + topics->count * 2; /* utf length == 2 */
	while (ListNextElement(topics, &elem))
		datalen += (int)strlen((char*)(elem->content));
	if (client->MQTTVersion >= MQTTVERSION_5)
		datalen += MQTTProperties_len(props);
	ptr = data = malloc(datalen);
	if (ptr == NULL)
		goto exit;

	writeInt(&ptr, msgid);

	if (client->MQTTVersion >= MQTTVERSION_5)
		MQTTProperties_write(&ptr, props);

	elem = NULL;
	while (ListNextElement(topics, &elem))
		writeUTF(&ptr, (char*)(elem->content));
	rc = MQTTPacket_send(&client->net, header, data, datalen, 1, client->MQTTVersion);
	Log(LOG_PROTOCOL, 25, NULL, client->net.socket, client->clientID, msgid, rc);
	if (rc != TCPSOCKET_INTERRUPTED)
		free(data);
exit:
	FUNC_EXIT_RC(rc);
	return rc;
}


/**
 * Function used in the new packets table to create unsuback packets.
 * @param MQTTVersion the version of MQTT
 * @param aHeader the MQTT header byte
 * @param data the rest of the packet
 * @param datalen the length of the rest of the packet
 * @return pointer to the packet structure
 */
void* MQTTPacket_unsuback(int MQTTVersion, unsigned char aHeader, char* data, size_t datalen)
{
	Unsuback* pack = NULL;
	char* curdata = data;
	char* enddata = &data[datalen];

	FUNC_ENTRY;
	if ((pack = malloc(sizeof(Unsuback))) == NULL)
		goto exit;
	pack->MQTTVersion = MQTTVersion;
	pack->header.byte = aHeader;
	pack->msgId = readInt(&curdata);
	pack->reasonCodes = NULL;
	if (MQTTVersion >= MQTTVERSION_5)
	{
		MQTTProperties props = MQTTProperties_initializer;
		pack->properties = props;
		if (MQTTProperties_read(&pack->properties, &curdata, enddata) != 1)
		{
			if (pack->properties.array)
				free(pack->properties.array);
			if (pack)
				free(pack);
			pack = NULL; /* signal protocol error */
			goto exit;
		}
		pack->reasonCodes = ListInitialize();
		while ((size_t)(curdata - data) < datalen)
		{
			enum MQTTReasonCodes* newrc;
			newrc = malloc(sizeof(enum MQTTReasonCodes));
			if (newrc == NULL)
			{
				if (pack->properties.array)
					free(pack->properties.array);
				if (pack)
					free(pack);
				pack = NULL; /* signal protocol error */
				goto exit;
			}
			*newrc = (enum MQTTReasonCodes)readChar(&curdata);
			ListAppend(pack->reasonCodes, newrc, sizeof(enum MQTTReasonCodes));
		}
		if (pack->reasonCodes->count == 0)
		{
			ListFree(pack->reasonCodes);
			if (pack->properties.array)
				free(pack->properties.array);
			if (pack)
				free(pack);
			pack = NULL;
		}
	}
exit:
	FUNC_EXIT;
	return pack;
}