1 /*******************************************************************************
2 * Copyright (c) 2014 IBM Corp.
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * Contributors:
14 * Ian Craggs - initial API and implementation and/or initial documentation
15 *******************************************************************************/
16
17 #include "StackTrace.h"
18 #include "MQTTPacket.h"
19 #include <string.h>
20
21 #define min(a, b) ((a < b) ? a : b)
22
23
24 /**
25 * Validates MQTT protocol name and version combinations
26 * @param protocol the MQTT protocol name as an MQTTString
27 * @param version the MQTT protocol version number, as in the connect packet
28 * @return correct MQTT combination? 1 is true, 0 is false
29 */
MQTTPacket_checkVersion(MQTTString * protocol,int version)30 int MQTTPacket_checkVersion(MQTTString* protocol, int version)
31 {
32 int rc = 0;
33
34 if (version == 3 && memcmp(protocol->lenstring.data, "MQIsdp",
35 min(6, protocol->lenstring.len)) == 0)
36 rc = 1;
37 else if (version == 4 && memcmp(protocol->lenstring.data, "MQTT",
38 min(4, protocol->lenstring.len)) == 0)
39 rc = 1;
40 return rc;
41 }
42
43
44 /**
45 * Deserializes the supplied (wire) buffer into connect data structure
46 * @param data the connect data structure to be filled out
47 * @param buf the raw buffer data, of the correct length determined by the remaining length field
48 * @param len the length in bytes of the data in the supplied buffer
49 * @return error code. 1 is success, 0 is failure
50 */
MQTTDeserialize_connect(MQTTPacket_connectData * data,unsigned char * buf,int len)51 int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len)
52 {
53 MQTTHeader header = {0};
54 MQTTConnectFlags flags = {0};
55 unsigned char* curdata = buf;
56 unsigned char* enddata = &buf[len];
57 int rc = 0;
58 MQTTString Protocol;
59 int version;
60 int mylen = 0;
61
62 FUNC_ENTRY;
63 header.byte = readChar(&curdata);
64 if (header.bits.type != CONNECT)
65 goto exit;
66
67 curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */
68
69 if (!readMQTTLenString(&Protocol, &curdata, enddata) ||
70 enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
71 goto exit;
72
73 version = (int)readChar(&curdata); /* Protocol version */
74 /* If we don't recognize the protocol version, we don't parse the connect packet on the
75 * basis that we don't know what the format will be.
76 */
77 if (MQTTPacket_checkVersion(&Protocol, version))
78 {
79 flags.all = readChar(&curdata);
80 data->cleansession = flags.bits.cleansession;
81 data->keepAliveInterval = readInt(&curdata);
82 if (!readMQTTLenString(&data->clientID, &curdata, enddata))
83 goto exit;
84 data->willFlag = flags.bits.will;
85 if (flags.bits.will)
86 {
87 data->will.qos = flags.bits.willQoS;
88 data->will.retained = flags.bits.willRetain;
89 if (!readMQTTLenString(&data->will.topicName, &curdata, enddata) ||
90 !readMQTTLenString(&data->will.message, &curdata, enddata))
91 goto exit;
92 }
93 if (flags.bits.username)
94 {
95 if (enddata - curdata < 3 || !readMQTTLenString(&data->username, &curdata, enddata))
96 goto exit; /* username flag set, but no username supplied - invalid */
97 if (flags.bits.password &&
98 (enddata - curdata < 3 || !readMQTTLenString(&data->password, &curdata, enddata)))
99 goto exit; /* password flag set, but no password supplied - invalid */
100 }
101 else if (flags.bits.password)
102 goto exit; /* password flag set without username - invalid */
103 rc = 1;
104 }
105 exit:
106 FUNC_EXIT_RC(rc);
107 return rc;
108 }
109
110
111 /**
112 * Serializes the connack packet into the supplied buffer.
113 * @param buf the buffer into which the packet will be serialized
114 * @param buflen the length in bytes of the supplied buffer
115 * @param connack_rc the integer connack return code to be used
116 * @param sessionPresent the MQTT 3.1.1 sessionPresent flag
117 * @return serialized length, or error if 0
118 */
MQTTSerialize_connack(unsigned char * buf,int buflen,unsigned char connack_rc,unsigned char sessionPresent)119 int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent)
120 {
121 MQTTHeader header = {0};
122 int rc = 0;
123 unsigned char *ptr = buf;
124 MQTTConnackFlags flags = {0};
125
126 FUNC_ENTRY;
127 if (buflen < 2)
128 {
129 rc = MQTTPACKET_BUFFER_TOO_SHORT;
130 goto exit;
131 }
132 header.byte = 0;
133 header.bits.type = CONNACK;
134 writeChar(&ptr, header.byte); /* write header */
135
136 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
137
138 flags.all = 0;
139 flags.bits.sessionpresent = sessionPresent;
140 writeChar(&ptr, flags.all);
141 writeChar(&ptr, connack_rc);
142
143 rc = ptr - buf;
144 exit:
145 FUNC_EXIT_RC(rc);
146 return rc;
147 }
148
149