version 1.1.2.7, 2011/12/05 15:30:49
|
version 1.1.2.12, 2011/12/09 11:46:48
|
Line 32 mqtt_msgCONNECT(mqtt_msg_t * __restrict buf, const cha
|
Line 32 mqtt_msgCONNECT(mqtt_msg_t * __restrict buf, const cha
|
|
|
if (!buf || !csConnID) |
if (!buf || !csConnID) |
return -1; |
return -1; |
if (strlen(csConnID) > 23) { | if (strlen(csConnID) > 24) { |
mqtt_SetErr(EINVAL, "Error:: invalid argument ConnID is too long (max 23 bytes)"); |
mqtt_SetErr(EINVAL, "Error:: invalid argument ConnID is too long (max 23 bytes)"); |
return -1; |
return -1; |
} |
} |
if (csUser && strlen(csUser) > 12) { | if (csUser && strlen(csUser) > 13) { |
mqtt_SetErr(EINVAL, "Error:: invalid argument Username is too long (max 12 bytes)"); |
mqtt_SetErr(EINVAL, "Error:: invalid argument Username is too long (max 12 bytes)"); |
return -1; |
return -1; |
} |
} |
if (csPass && strlen(csPass) > 12) { | if (csPass && strlen(csPass) > 13) { |
mqtt_SetErr(EINVAL, "Error:: invalid argument Password is too long (max 12 bytes)"); |
mqtt_SetErr(EINVAL, "Error:: invalid argument Password is too long (max 12 bytes)"); |
return -1; |
return -1; |
} |
} |
Line 253 mqtt_msgDISCONNECT(mqtt_msg_t * __restrict buf)
|
Line 253 mqtt_msgDISCONNECT(mqtt_msg_t * __restrict buf)
|
* @userLen = Username length |
* @userLen = Username length |
* @psPass = Password for Username, only if csUser is set |
* @psPass = Password for Username, only if csUser is set |
* @passLen = Password length |
* @passLen = Password length |
* @psWillTopic = Will Topic if !=NULL Will Flags set into message | * @psWillTopic = Will Topic if !=NULL Will Flags set into message and must be free() |
* @topicLen = Will Topic length | * @psWillMessage = Will Message, may be NULL if !NULL must be free() after use! |
* @psWillMessage = Will Message, may be NULL | |
* @msgLen = Will Message length | |
* return: .reserved == 1 is error or == 0 connection flags & msg ok |
* return: .reserved == 1 is error or == 0 connection flags & msg ok |
*/ |
*/ |
mqtthdr_connflgs_t | mqtthdr_connack_t |
mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short *kasec, char * __restrict psConnID, int connLen, |
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 psUser, int userLen, char * __restrict psPass, int passLen, |
char * __restrict psWillTopic, int topicLen, char * __restrict psWillMessage, int msgLen) | char ** __restrict psWillTopic, char ** __restrict psWillMessage) |
{ |
{ |
mqtthdr_connflgs_t flg = MQTT_CONNFLGS_INIT; | mqtthdr_connflgs_t flg = { MQTT_CONNFLGS_INIT }; |
| mqtthdr_connack_t cack = { 1, MQTT_RETCODE_DENIED }; |
struct mqtthdr *hdr; |
struct mqtthdr *hdr; |
mqtthdr_var_t *var; |
mqtthdr_var_t *var; |
mqtt_v_t *ka; |
mqtt_v_t *ka; |
Line 273 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
Line 272 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
caddr_t pos; |
caddr_t pos; |
|
|
if (!buf || !buf->msg_base || !buf->msg_len || !psConnID || !connLen) |
if (!buf || !buf->msg_base || !buf->msg_len || !psConnID || !connLen) |
return flg; | return cack; |
|
|
hdr = (struct mqtthdr*) buf->msg_base; | hdr = _mqtt_readHEADER(buf, MQTT_TYPE_CONNECT, &ret, &len); |
if (hdr->mqtt_msg.type != MQTT_TYPE_CONNECT) { | if (!hdr) |
mqtt_SetErr(EINVAL, "Error:: wrong command #%d", hdr->mqtt_msg.type); | return cack; |
return flg; | |
} else | |
len = mqtt_decodeLen(hdr->mqtt_len, &ret); | |
if (len < 12) { |
if (len < 12) { |
mqtt_SetErr(EINVAL, "Error:: short message length %d", len); |
mqtt_SetErr(EINVAL, "Error:: short message length %d", len); |
return flg; | return cack; |
} else { |
} else { |
pos = buf->msg_base + ret + 1; |
pos = buf->msg_base + ret + 1; |
var = (mqtthdr_var_t*) pos; |
var = (mqtthdr_var_t*) pos; |
Line 292 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
Line 288 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
if (var->var_sb.sb.l != 6 || strncmp(var->var_data, MQTT_CONN_STR, 6)) { |
if (var->var_sb.sb.l != 6 || strncmp(var->var_data, MQTT_CONN_STR, 6)) { |
mqtt_SetErr(EINVAL, "Error:: invalid init string %.6s(%d)", |
mqtt_SetErr(EINVAL, "Error:: invalid init string %.6s(%d)", |
var->var_data, var->var_sb.sb.l); |
var->var_data, var->var_sb.sb.l); |
return flg; | cack.retcode = MQTT_RETCODE_REFUSE_UNAVAIL; |
| return cack; |
} else { |
} else { |
pos += var->var_sb.sb.l + sizeof(mqtt_v_t); |
pos += var->var_sb.sb.l + sizeof(mqtt_v_t); |
proto = (mqtthdr_protover_t*) pos; |
proto = (mqtthdr_protover_t*) pos; |
} |
} |
if (*proto != MQTT_PROTO_VER) { |
if (*proto != MQTT_PROTO_VER) { |
mqtt_SetErr(EINVAL, "Error:: invalid protocol version %d", *pos); |
mqtt_SetErr(EINVAL, "Error:: invalid protocol version %d", *pos); |
return flg; | cack.retcode = MQTT_RETCODE_REFUSE_VER; |
| return cack; |
} else |
} else |
pos++; |
pos++; |
flg = *(mqtthdr_connflgs_t*) pos; |
flg = *(mqtthdr_connflgs_t*) pos; |
Line 315 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
Line 313 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
len -= MQTTHDR_VAR_SIZEOF(var); |
len -= MQTTHDR_VAR_SIZEOF(var); |
if (len < 0) { |
if (len < 0) { |
mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Connection ID %d", len); |
mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Connection ID %d", len); |
flg.reserved = 1; | cack.retcode = MQTT_RETCODE_REFUSE_ID; |
return flg; | return cack; |
} else { |
} else { |
memset(psConnID, 0, connLen--); |
memset(psConnID, 0, connLen--); |
memcpy(psConnID, var->var_data, ntohs(var->var_sb.val) > connLen ? connLen : ntohs(var->var_sb.val)); |
memcpy(psConnID, var->var_data, ntohs(var->var_sb.val) > connLen ? connLen : ntohs(var->var_sb.val)); |
Line 329 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
Line 327 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
len -= MQTTHDR_VAR_SIZEOF(var); |
len -= MQTTHDR_VAR_SIZEOF(var); |
if (len < 0) { |
if (len < 0) { |
mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Will Topic %d", len); |
mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Will Topic %d", len); |
flg.reserved = 1; | cack.retcode = MQTT_RETCODE_REFUSE_ID; |
return flg; | return cack; |
} else { |
} else { |
if (psWillTopic && topicLen) { | if (psWillTopic) { |
memset(psWillTopic, 0, topicLen--); | *psWillTopic = malloc(ntohs(var->var_sb.val) + 1); |
memcpy(psWillTopic, var->var_data, | if (!*psWillTopic) { |
ntohs(var->var_sb.val) > topicLen ? topicLen : ntohs(var->var_sb.val)); | 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); |
pos += MQTTHDR_VAR_SIZEOF(var); |
} |
} |
Line 344 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
Line 347 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
len -= MQTTHDR_VAR_SIZEOF(var); |
len -= MQTTHDR_VAR_SIZEOF(var); |
if (len < 0) { |
if (len < 0) { |
mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Will Message %d", len); |
mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Will Message %d", len); |
flg.reserved = 1; | cack.retcode = MQTT_RETCODE_REFUSE_ID; |
return flg; | return cack; |
} else { |
} else { |
if (psWillMessage && msgLen) { | if (psWillMessage) { |
memset(psWillMessage, 0, msgLen--); | *psWillMessage = malloc(ntohs(var->var_sb.val) + 1); |
memcpy(psWillMessage, var->var_data, | if (!*psWillMessage) { |
ntohs(var->var_sb.val) > msgLen ? msgLen : ntohs(var->var_sb.val)); | 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); |
pos += MQTTHDR_VAR_SIZEOF(var); |
} |
} |
Line 362 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
Line 370 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
len -= MQTTHDR_VAR_SIZEOF(var); |
len -= MQTTHDR_VAR_SIZEOF(var); |
if (len < 0) { |
if (len < 0) { |
mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Username %d", len); |
mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Username %d", len); |
flg.reserved = 1; | cack.retcode = MQTT_RETCODE_REFUSE_USERPASS; |
return flg; | return cack; |
} else { |
} else { |
if (psUser && userLen) { |
if (psUser && userLen) { |
memset(psUser, 0, userLen--); |
memset(psUser, 0, userLen--); |
Line 378 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
Line 386 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
len -= MQTTHDR_VAR_SIZEOF(var); |
len -= MQTTHDR_VAR_SIZEOF(var); |
if (len < 0) { |
if (len < 0) { |
mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Password %d", len); |
mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Password %d", len); |
flg.reserved = 1; | cack.retcode = MQTT_RETCODE_REFUSE_USERPASS; |
return flg; | return cack; |
} else { |
} else { |
if (psPass && passLen) { |
if (psPass && passLen) { |
memset(psPass, 0, passLen--); |
memset(psPass, 0, passLen--); |
Line 390 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
Line 398 mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short
|
} |
} |
} |
} |
|
|
return flg; | flg.reserved = 0; |
| cack.reserved = flg.flags; |
| cack.retcode = MQTT_RETCODE_ACCEPTED; |
| return cack; |
} |
} |
|
|
/* |
/* |
Line 410 mqtt_readCONNACK(mqtt_msg_t * __restrict buf)
|
Line 421 mqtt_readCONNACK(mqtt_msg_t * __restrict buf)
|
if (!buf || !buf->msg_base || !buf->msg_len) |
if (!buf || !buf->msg_base || !buf->msg_len) |
return (u_char) -1; |
return (u_char) -1; |
|
|
hdr = (struct mqtthdr*) buf->msg_base; | hdr = _mqtt_readHEADER(buf, MQTT_TYPE_CONNACK, &ret, &len); |
if (hdr->mqtt_msg.type != MQTT_TYPE_CONNACK) { | if (!hdr) |
mqtt_SetErr(EINVAL, "Error:: wrong command #%d", hdr->mqtt_msg.type); | |
return (u_char) -1; |
return (u_char) -1; |
} else |
|
len = mqtt_decodeLen(hdr->mqtt_len, &ret); |
|
if (len < sizeof(mqtthdr_connack_t)) { |
if (len < sizeof(mqtthdr_connack_t)) { |
mqtt_SetErr(EINVAL, "Error:: short message length %d", len); |
mqtt_SetErr(EINVAL, "Error:: short message length %d", len); |
return (u_char) -1; |
return (u_char) -1; |
Line 430 mqtt_readCONNACK(mqtt_msg_t * __restrict buf)
|
Line 438 mqtt_readCONNACK(mqtt_msg_t * __restrict buf)
|
} |
} |
|
|
return ack->retcode; |
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; |
} |
} |