version 1.1.2.7, 2011/12/05 08:15:50
|
version 1.1.2.13, 2011/12/09 11:46:48
|
Line 1
|
Line 1
|
#include "global.h" |
#include "global.h" |
|
#include "mqttd.h" |
|
|
|
|
extern char cliCmd[], *cliStr[]; |
extern char cliCmd[], *cliStr[]; |
Line 7 extern char cliCmd[], *cliStr[];
|
Line 8 extern char cliCmd[], *cliStr[];
|
static void * |
static void * |
startSession(sched_task_t *task) |
startSession(sched_task_t *task) |
{ |
{ |
mqtt_msg_t *buf; | u_char basebuf[USHRT_MAX]; |
mqtt_cb_t cbs[MQTT_TYPE_MAX + 1] = { 0 }; |
mqtt_cb_t cbs[MQTT_TYPE_MAX + 1] = { 0 }; |
struct mqtthdr *hdr; | mqtt_msg_t buf = { basebuf, sizeof basebuf }; |
| mqtthdr_connflgs_t flg; |
| mqtthdr_connack_t cack; |
int ret = 0; |
int ret = 0; |
register int i; | struct timeval tv = { 0 }; |
| u_short ka; |
| struct tagSession *sess = NULL; |
|
|
FTRACE(4); |
FTRACE(4); |
|
|
buf = mqtt_msgAlloc(USHRT_MAX); | sess = malloc(sizeof(struct tagSession)); |
if (!buf) { | if (!sess) { |
syslog(LOG_ERR, "Error:: allocate message buf (%d) #%d - %s", (int) TASK_FD(task), | VERB(3) syslog(LOG_ERR, "Error:: in malloc #%d - %s", errno, strerror(errno)); |
mqtt_GetErrno(), mqtt_GetError()); | |
goto end; |
goto end; |
} |
} |
|
|
if (recv(TASK_FD(task), buf->msg_base, buf->msg_len, 0) == -1) { | if (recv(TASK_FD(task), buf.msg_base, buf.msg_len, 0) == -1) { |
syslog(LOG_ERR, "Error:: recv(%d) #%d - %s", (int) TASK_FD(task), | VERB(3) syslog(LOG_ERR, "Error:: recv(%d) #%d - %s", (int) TASK_FD(task), |
errno, strerror(errno)); |
errno, strerror(errno)); |
mqtt_msgFree(&buf, 42); |
|
goto end; |
goto end; |
} else { |
|
hdr = (struct mqtthdr*) buf->msg_base; |
|
for (ret = i = 0; cliCmd[i] && !(ret = (hdr->mqtt_msg.type == cliCmd[i])); i++); |
|
if (!ret) { |
|
syslog(LOG_ERR, "Error:: wrong command type #%d %s", |
|
hdr->mqtt_msg.type, cliStr[i]); |
|
mqtt_msgFree(&buf, 42); |
|
goto end; |
|
} |
|
} |
} |
|
cack = mqtt_readCONNECT(&buf, &ka, sess->sess_cid, sizeof sess->sess_cid, |
|
sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass, |
|
&sess->sess_will.topic, &sess->sess_will.msg); |
|
if (cack.reserved) { |
|
VERB(3) syslog(LOG_ERR, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
|
goto end; |
|
} |
|
|
ret = mqttDispatcher(cbs, buf); | /* check online table for user */ |
|
|
mqtt_msgFree(&buf, 42); |
|
|
|
switch (ret) { |
|
case -1: |
|
syslog(LOG_ERR, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
|
break; |
|
default: |
|
return NULL; |
|
} |
|
end: /* close client connection */ |
end: /* close client connection */ |
|
if (sess) |
|
free(sess); |
close(TASK_FD(task)); |
close(TASK_FD(task)); |
VERB(1) syslog(LOG_DEBUG, "Close client %s with socket=%d", |
VERB(1) syslog(LOG_DEBUG, "Close client %s with socket=%d", |
(char*) TASK_ARG(task), (int) TASK_FD(task)); |
(char*) TASK_ARG(task), (int) TASK_FD(task)); |