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