File:
[ELWIX - Embedded LightWeight unIX -] /
libaitmqtt /
src /
conn.c
Revision
1.3:
download - view:
text,
annotated -
select for diffs -
revision graph
Thu Jun 28 11:06:17 2012 UTC (12 years, 2 months ago) by
misho
Branches:
MAIN
CVS tags:
mqtt1_8,
mqtt1_7,
mqtt1_6,
mqtt1_5,
mqtt1_4,
mqtt1_3,
MQTT1_7,
MQTT1_6,
MQTT1_5,
MQTT1_4,
MQTT1_3,
MQTT1_2,
HEAD
ver 1.2
/*************************************************************************
* (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
* by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: conn.c,v 1.3 2012/06/28 11:06:17 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:
All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
by Michael Pounov <misho@elwix.org>. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"
/*
* mqtt_msgCONNECT() Create CONNECT message
*
* @buf = Message buffer
* @csConnID = ConnectID
* @kasec = Keep alive timeout, if =0 default timeout for MQTT
* @csUser = Username if !=NULL
* @csPass = Password for Username, only if csUser is set
* @csWillTopic = Will Topic if !=NULL Will Flags set into message
* @csWillMessage = Will Message, may be NULL
* @ClrSess = Clear Session subscriptions after disconnect
* @WillQOS = Will QOS if csWillTopic is set
* @WillRetain = Will Retain Will Message if csWillTopic is set
* return: -1 error or >-1 message size for send
*/
int
mqtt_msgCONNECT(mqtt_msg_t * __restrict buf, const char *csConnID,
u_short kasec, const char *csUser, const char *csPass,
const char *csWillTopic, const char *csWillMessage,
u_char ClrSess, u_char WillQOS, u_char WillRetain)
{
int len, siz = 0;
u_int n, *l;
struct mqtthdr *hdr;
mqtthdr_var_t *var, *cid, *topic, *wmsg, *user, *pass;
mqtthdr_protover_t *proto;
mqtthdr_connflgs_t *flags;
mqtt_len_t *ka;
void *data;
if (!buf || !csConnID)
return -1;
if (strlen(csConnID) > 23) {
mqtt_SetErr(EINVAL, "Invalid argument ConnID is too long (max 23 bytes)");
return -1;
}
if (csUser && strlen(csUser) > 12) {
mqtt_SetErr(EINVAL, "Invalid argument Username is too long (max 12 bytes)");
return -1;
}
if (csPass && strlen(csPass) > 12) {
mqtt_SetErr(EINVAL, "Invalid argument Password is too long (max 12 bytes)");
return -1;
}
if (WillQOS > MQTT_QOS_EXACTLY) {
mqtt_SetErr(EINVAL, "Invalid argument WillQOS - unknown QOS value");
return -1;
}
/* calculate message size */
len = 10 + sizeof(mqtt_len_t); /* connect arguments */
len += sizeof(mqtt_len_t) + strlen(csConnID); /* connect id */
if (csUser && *csUser) { /* user/pass */
len += sizeof(mqtt_len_t) + strlen(csUser);
if (csPass)
len += sizeof(mqtt_len_t) + strlen(csPass);
}
if (csWillTopic && *csWillTopic) { /* will messages */
len += sizeof(mqtt_len_t) + strlen(csWillTopic);
len += sizeof(mqtt_len_t) + (csWillMessage ? strlen(csWillMessage) : 0);
}
/* calculate header size */
siz = sizeof(struct mqtthdr); /* mqtt fixed header */
n = mqtt_encodeLen(len); /* message size */
siz += mqtt_sizeLen(n) - 1; /* length size */
if (mqtt_msgRealloc(buf, siz + len) == -1)
return -1;
else {
data = buf->msg_base;
hdr = (struct mqtthdr *) data;
}
/* fixed header */
MQTTHDR_MSGINIT(hdr);
hdr->mqtt_msg.type = MQTT_TYPE_CONNECT;
l = (u_int*) hdr->mqtt_len;
*l = n;
data += siz;
/* variable header */
var = (mqtthdr_var_t*) data;
var->var_sb.val = htons(strlen(MQTT_CONN_STR));
memcpy(var->var_data, MQTT_CONN_STR, ntohs(var->var_sb.val));
data += MQTTHDR_VAR_SIZEOF(var);
/* protocol version */
proto = (mqtthdr_protover_t*) data++;
*proto = MQTT_PROTO_VER;
/* CONNECT header */
flags = (mqtthdr_connflgs_t*) data++;
flags->clean_sess = ClrSess ? 1 : 0;
if (csUser && *csUser) {
flags->username = 1;
flags->password = csPass ? 1 : 0;
} else {
flags->username = 0;
flags->password = 0;
}
if (csWillTopic && *csWillTopic) {
flags->will_flg = 1;
flags->will_qos = WillQOS;
flags->will_retain = WillRetain ? 1 : 0;
} else {
flags->will_flg = 0;
flags->will_qos = 0;
flags->will_retain = 0;
}
/* keep alive */
ka = (mqtt_len_t*) data;
ka->val = kasec ? htons(kasec) : htons(MQTT_KEEPALIVE);
data += sizeof(mqtt_len_t);
/* ConnID */
cid = (mqtthdr_var_t*) data;
cid->var_sb.val = htons(strlen(csConnID));
memcpy(cid->var_data, csConnID, ntohs(cid->var_sb.val));
data += MQTTHDR_VAR_SIZEOF(cid);
/* If Will Flags setup */
if (csWillTopic && *csWillTopic) {
topic = (mqtthdr_var_t*) data;
topic->var_sb.val = htons(strlen(csWillTopic));
memcpy(topic->var_data, csWillTopic, ntohs(topic->var_sb.val));
data += MQTTHDR_VAR_SIZEOF(topic);
wmsg = (mqtthdr_var_t*) data;
if (csWillMessage && *csWillMessage) {
wmsg->var_sb.val = htons(strlen(csWillMessage));
memcpy(wmsg->var_data, csWillMessage, ntohs(wmsg->var_sb.val));
} else
wmsg->var_sb.val = 0;
data += MQTTHDR_VAR_SIZEOF(wmsg);
}
/* If defined Username & Password */
if (csUser && *csUser) {
user = (mqtthdr_var_t*) data;
user->var_sb.val = htons(strlen(csUser));
memcpy(user->var_data, csUser, ntohs(user->var_sb.val));
data += MQTTHDR_VAR_SIZEOF(user);
if (csPass && *csPass) {
pass = (mqtthdr_var_t*) data;
pass->var_sb.val = htons(strlen(csPass));
memcpy(pass->var_data, csPass, ntohs(pass->var_sb.val));
data += MQTTHDR_VAR_SIZEOF(pass);
}
}
return siz + len;
}
/*
* mqtt_msgCONNACK() Create CONNACK message
*
* @buf = Message buffer
* @retcode = Return code
* return: -1 error or >-1 message size for send
*/
int
mqtt_msgCONNACK(mqtt_msg_t * __restrict buf, u_char retcode)
{
int siz = 0;
struct mqtthdr *hdr;
mqtthdr_connack_t *ack;
if (!buf)
return -1;
if (retcode > MQTT_RETCODE_DENIED) {
mqtt_SetErr(EINVAL, "Invalid retcode");
return -1;
}
if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr) + sizeof(mqtthdr_connack_t)) == -1)
return -1;
else {
hdr = (struct mqtthdr *) (buf->msg_base + siz);
siz += sizeof(struct mqtthdr);
ack = (mqtthdr_connack_t*) (buf->msg_base + siz);
siz += sizeof(mqtthdr_connack_t);
}
/* fixed header */
MQTTHDR_MSGINIT(hdr);
hdr->mqtt_msg.type = MQTT_TYPE_CONNACK;
*hdr->mqtt_len = sizeof(mqtthdr_connack_t);
/* CONNACK header */
ack->reserved = 0;
ack->retcode = retcode;
return siz;
}
static int
_mqtt_msgSIMPLE_(mqtt_msg_t * __restrict buf, u_char cmd)
{
int siz = 0;
struct mqtthdr *hdr;
if (!buf)
return -1;
if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr)) == -1)
return -1;
else {
hdr = (struct mqtthdr *) (buf->msg_base + siz);
siz += sizeof(struct mqtthdr);
}
/* fixed header */
MQTTHDR_MSGINIT(hdr);
hdr->mqtt_msg.type = cmd;
*hdr->mqtt_len = 0;
return siz;
}
/*
* mqtt_msgPINGREQ() Create PINGREQ message
*
* @buf = Message buffer
* return: -1 error or >-1 message size for send
*/
int
mqtt_msgPINGREQ(mqtt_msg_t * __restrict buf)
{
return _mqtt_msgSIMPLE_(buf, MQTT_TYPE_PINGREQ);
}
/*
* mqtt_msgPINGRESP() Create PINGRESP message
*
* @buf = Message buffer
* return: -1 error or >-1 message size for send
*/
int
mqtt_msgPINGRESP(mqtt_msg_t * __restrict buf)
{
return _mqtt_msgSIMPLE_(buf, MQTT_TYPE_PINGRESP);
}
/*
* mqtt_msgDISCONNECT() Create DISCONNECT message
*
* @buf = Message buffer
* return: -1 error or >-1 message size for send
*/
int
mqtt_msgDISCONNECT(mqtt_msg_t * __restrict buf)
{
return _mqtt_msgSIMPLE_(buf, MQTT_TYPE_DISCONNECT);
}
/* ============= decode ============ */
/*
* mqtt_readCONNECT() Read elements from CONNECT message
*
* @buf = Message buffer
* @kasec = Keep Alive in seconds for current connection
* @psConnID = ConnectID
* @connLen = ConnectID length
* @psUser = Username if !=NULL
* @userLen = Username length
* @psPass = Password for Username, only if csUser is set
* @passLen = Password length
* @psWillTopic = Will Topic if !=NULL Will Flags set into message and must be free()
* @psWillMessage = Will Message, may be NULL if !NULL must be free() after use!
* return: .reserved == 1 is error or == 0 connection flags & msg ok
*/
mqtthdr_connack_t
mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short *kasec, char * __restrict psConnID, int connLen,
char * __restrict psUser, int userLen, char * __restrict psPass, int passLen,
char ** __restrict psWillTopic, char ** __restrict psWillMessage)
{
mqtthdr_connflgs_t flg = { MQTT_CONNFLGS_INIT };
mqtthdr_connack_t cack = { 1, MQTT_RETCODE_DENIED };
struct mqtthdr *hdr;
mqtthdr_var_t *var;
mqtt_len_t *ka;
mqtthdr_protover_t *proto;
int len, ret;
caddr_t pos;
if (!buf || !buf->msg_base || !buf->msg_len || !psConnID || !connLen)
return cack;
hdr = _mqtt_readHEADER(buf, MQTT_TYPE_CONNECT, &ret, &len);
if (!hdr)
return cack;
if (len < 12) {
mqtt_SetErr(EINVAL, "Short message length %d", len);
return cack;
} else {
pos = buf->msg_base + ret + 1;
var = (mqtthdr_var_t*) pos;
}
/* check init string & protocol */
if (var->var_sb.sb.l != 6 || strncmp((char*) var->var_data, MQTT_CONN_STR, 6)) {
mqtt_SetErr(EINVAL, "Invalid init string %.6s(%d)",
var->var_data, var->var_sb.sb.l);
cack.retcode = MQTT_RETCODE_REFUSE_UNAVAIL;
return cack;
} else {
pos += var->var_sb.sb.l + sizeof(mqtt_len_t);
proto = (mqtthdr_protover_t*) pos;
}
if (*proto != MQTT_PROTO_VER) {
mqtt_SetErr(EINVAL, "Invalid protocol version %d", *pos);
cack.retcode = MQTT_RETCODE_REFUSE_VER;
return cack;
} else
pos++;
flg = *(mqtthdr_connflgs_t*) pos;
pos++;
ka = (mqtt_len_t*) pos;
*kasec = ntohs(ka->val);
pos += sizeof(mqtt_len_t);
len -= pos - (caddr_t) var;
/* get ConnID */
var = (mqtthdr_var_t*) pos;
len -= MQTTHDR_VAR_SIZEOF(var);
if (len < 0 || var->var_sb.sb.l > 23) {
mqtt_SetErr(EINVAL, "Unexpected EOM at Connection ID %d", len);
cack.retcode = MQTT_RETCODE_REFUSE_ID;
return cack;
} else {
memset(psConnID, 0, connLen--);
memcpy(psConnID, var->var_data, ntohs(var->var_sb.val) > connLen ? connLen : ntohs(var->var_sb.val));
pos += MQTTHDR_VAR_SIZEOF(var);
}
/* get Willz */
if (flg.will_flg) {
var = (mqtthdr_var_t*) pos;
len -= MQTTHDR_VAR_SIZEOF(var);
if (len < 0) {
mqtt_SetErr(EINVAL, "Unexpected EOM at Will Topic %d", len);
cack.retcode = MQTT_RETCODE_REFUSE_ID;
return cack;
} else {
if (psWillTopic) {
*psWillTopic = malloc(ntohs(var->var_sb.val) + 1);
if (!*psWillTopic) {
LOGERR;
cack.retcode = MQTT_RETCODE_REFUSE_UNAVAIL;
return cack;
} else
memset(*psWillTopic, 0, ntohs(var->var_sb.val) + 1);
memcpy(*psWillTopic, var->var_data, ntohs(var->var_sb.val));
}
pos += MQTTHDR_VAR_SIZEOF(var);
}
var = (mqtthdr_var_t*) pos;
len -= MQTTHDR_VAR_SIZEOF(var);
if (len < 0) {
mqtt_SetErr(EINVAL, "Unexpected EOM at Will Message %d", len);
cack.retcode = MQTT_RETCODE_REFUSE_ID;
return cack;
} else {
if (psWillMessage) {
*psWillMessage = malloc(ntohs(var->var_sb.val) + 1);
if (!*psWillMessage) {
LOGERR;
cack.retcode = MQTT_RETCODE_REFUSE_UNAVAIL;
return cack;
} else
memset(*psWillMessage, 0, ntohs(var->var_sb.val) + 1);
memcpy(*psWillMessage, var->var_data, ntohs(var->var_sb.val));
}
pos += MQTTHDR_VAR_SIZEOF(var);
}
}
/* get User/Pass */
if (flg.username) {
var = (mqtthdr_var_t*) pos;
len -= MQTTHDR_VAR_SIZEOF(var);
if (len < 0 || var->var_sb.sb.l > 12) {
mqtt_SetErr(EINVAL, "Unexpected EOM at Username %d", len);
cack.retcode = MQTT_RETCODE_REFUSE_USERPASS;
return cack;
} else {
if (psUser && userLen) {
memset(psUser, 0, userLen--);
memcpy(psUser, var->var_data,
ntohs(var->var_sb.val) > userLen ? userLen : ntohs(var->var_sb.val));
}
pos += MQTTHDR_VAR_SIZEOF(var);
}
}
if (flg.password) {
var = (mqtthdr_var_t*) pos;
len -= MQTTHDR_VAR_SIZEOF(var);
if (len < 0 || var->var_sb.sb.l > 12) {
mqtt_SetErr(EINVAL, "Unexpected EOM at Password %d", len);
cack.retcode = MQTT_RETCODE_REFUSE_USERPASS;
return cack;
} else {
if (psPass && passLen) {
memset(psPass, 0, passLen--);
memcpy(psPass, var->var_data,
ntohs(var->var_sb.val) > passLen ? passLen : ntohs(var->var_sb.val));
}
pos += MQTTHDR_VAR_SIZEOF(var);
}
}
flg.reserved = 0;
cack.reserved = flg.flags;
cack.retcode = MQTT_RETCODE_ACCEPTED;
return cack;
}
/*
* mqtt_readCONNACK() Read CONNACK message
*
* @buf = Message buffer
* return: -1 error or >-1 CONNECT message return code
*/
u_char
mqtt_readCONNACK(mqtt_msg_t * __restrict buf)
{
int len, ret;
struct mqtthdr *hdr;
mqtthdr_connack_t *ack;
caddr_t pos;
if (!buf || !buf->msg_base || !buf->msg_len)
return (u_char) -1;
hdr = _mqtt_readHEADER(buf, MQTT_TYPE_CONNACK, &ret, &len);
if (!hdr)
return (u_char) -1;
if (len < sizeof(mqtthdr_connack_t)) {
mqtt_SetErr(EINVAL, "Short message length %d", len);
return (u_char) -1;
} else {
pos = buf->msg_base + ret + 1;
ack = (mqtthdr_connack_t*) pos;
}
if (ack->retcode > MQTT_RETCODE_DENIED) {
mqtt_SetErr(EINVAL, "Invalid retcode %u", ack->retcode);
return (u_char) -1;
}
return ack->retcode;
}
/*
* mqtt_readDISCONNECT() Read DISCONNECT message
*
* @buf = Message buffer
* return: -1 error, 0 ok, >0 undefined result
*/
int
mqtt_readDISCONNECT(mqtt_msg_t * __restrict buf)
{
int len, ret;
struct mqtthdr *hdr;
hdr = _mqtt_readHEADER(buf, MQTT_TYPE_DISCONNECT, &ret, &len);
if (!hdr || ret != 1)
return -1;
return len;
}
/*
* mqtt_readPINGREQ() Read PINGREQ message
*
* @buf = Message buffer
* return: -1 error, 0 ok, >0 undefined result
*/
int
mqtt_readPINGREQ(mqtt_msg_t * __restrict buf)
{
int len, ret;
struct mqtthdr *hdr;
hdr = _mqtt_readHEADER(buf, MQTT_TYPE_PINGREQ, &ret, &len);
if (!hdr || ret != 1)
return -1;
return len;
}
/*
* mqtt_readPINGRESP() Read PINGRESP message
*
* @buf = Message buffer
* return: -1 error, 0 ok, >0 undefined result
*/
int
mqtt_readPINGRESP(mqtt_msg_t * __restrict buf)
{
int len, ret;
struct mqtthdr *hdr;
hdr = _mqtt_readHEADER(buf, MQTT_TYPE_PINGRESP, &ret, &len);
if (!hdr || ret != 1)
return -1;
return len;
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>