version 1.2.2.24, 2012/06/20 14:51:38
|
version 1.2.2.29, 2012/06/29 08:47:28
|
Line 4
|
Line 4
|
#include "mqttd_calls.h" |
#include "mqttd_calls.h" |
|
|
|
|
static inline struct tagPkt * | static inline ait_val_t * |
mkPkt(void * __restrict data, int dlen) |
mkPkt(void * __restrict data, int dlen) |
{ |
{ |
struct tagPkt *p = NULL; | ait_val_t *p = NULL; |
|
|
p = io_malloc(sizeof(struct tagPkt)); | if (!(p = io_allocVar())) { |
if (!p) { | |
ioDEBUG(7, "Error:: in send packet prepare #%d - %s", io_GetErrno(), io_GetError()); |
ioDEBUG(7, "Error:: in send packet prepare #%d - %s", io_GetErrno(), io_GetError()); |
return NULL; |
return NULL; |
} else |
|
memset(p, 0, sizeof(struct tagPkt)); |
|
|
|
p->pkt_data = io_allocVar(); |
|
if (!p->pkt_data) { |
|
ioDEBUG(7, "Error:: in send packet prepare #%d - %s", io_GetErrno(), io_GetError()); |
|
io_free(p); |
|
return NULL; |
|
} |
} |
|
|
if (data && dlen > 0) |
if (data && dlen > 0) |
AIT_SET_BUF(p->pkt_data, data, dlen); | AIT_SET_BUF(p, data, dlen); |
|
|
return p; |
return p; |
} |
} |
|
|
static inline void |
static inline void |
freePkt(struct tagPkt * __restrict p) | freePkt(ait_val_t ** __restrict p) |
{ |
{ |
if (!p) |
if (!p) |
return; |
return; |
|
|
io_freeVar(&p->pkt_data); | io_freeVar(p); |
io_free(p); | |
} |
} |
|
|
static void * |
static void * |
sendPacket(sched_task_t *task) |
sendPacket(sched_task_t *task) |
{ |
{ |
struct tagPkt *p = TASK_ARG(task); | ait_val_t *p = TASK_ARG(task); |
register int n, slen; |
register int n, slen; |
u_char *pos; |
u_char *pos; |
|
|
if (!p || !p->pkt_data || AIT_ISEMPTY(p->pkt_data)) { | if (!p || AIT_ISEMPTY(p)) { |
ioDEBUG(9, "Error:: invalid packet or found empty content ..."); |
ioDEBUG(9, "Error:: invalid packet or found empty content ..."); |
return NULL; |
return NULL; |
} |
} |
|
|
for (slen = AIT_LEN(p->pkt_data), pos = AIT_GET_BUF(p->pkt_data); slen > 0; | for (slen = AIT_LEN(p), pos = AIT_GET_BUF(p); slen > 0; slen -= n, pos += n) { |
slen -= n, pos += n) { | |
n = send(TASK_FD(task), pos, slen, MSG_NOSIGNAL); |
n = send(TASK_FD(task), pos, slen, MSG_NOSIGNAL); |
if (n == -1) { |
if (n == -1) { |
ioSYSERR(0); |
ioSYSERR(0); |
Line 60 sendPacket(sched_task_t *task)
|
Line 49 sendPacket(sched_task_t *task)
|
} |
} |
} |
} |
|
|
freePkt(p); | freePkt(&p); |
return NULL; |
return NULL; |
} |
} |
|
|
Line 69 sendPacket(sched_task_t *task)
|
Line 58 sendPacket(sched_task_t *task)
|
static int |
static int |
pubOnce(struct tagSession *sess, char * __restrict psTopic, int datlen) |
pubOnce(struct tagSession *sess, char * __restrict psTopic, int datlen) |
{ |
{ |
struct tagPkt *p = NULL; | ait_val_t *p = NULL; |
struct tagSession *s = NULL; |
struct tagSession *s = NULL; |
struct tagStore *st = NULL; |
struct tagStore *st = NULL; |
regex_t re; |
regex_t re; |
Line 87 pubOnce(struct tagSession *sess, char * __restrict psT
|
Line 76 pubOnce(struct tagSession *sess, char * __restrict psT
|
} |
} |
if (!regexec(&re, psTopic, 1, &match, 0)) { |
if (!regexec(&re, psTopic, 1, &match, 0)) { |
/* MATCH */ |
/* MATCH */ |
ioDEBUG(1, "+++ dlen=%d\n", datlen); |
|
p = mkPkt(sess->sess_buf->msg_base, datlen); |
p = mkPkt(sess->sess_buf->msg_base, datlen); |
schedWrite(root, sendPacket, p, s->sess_sock, NULL, 0); |
schedWrite(root, sendPacket, p, s->sess_sock, NULL, 0); |
} |
} |
Line 100 pubOnce(struct tagSession *sess, char * __restrict psT
|
Line 88 pubOnce(struct tagSession *sess, char * __restrict psT
|
} |
} |
|
|
static int |
static int |
pubAck(struct tagSession *sess, char * __restrict psTopic, int datlen) | pubAck(struct tagSession *sess, u_short mid, char * __restrict psTopic, int datlen) |
{ |
{ |
|
ait_val_t *p = NULL; |
|
struct tagSession *s = NULL; |
|
struct tagStore *st = NULL; |
|
regex_t re; |
|
regmatch_t match; |
|
int ret; |
|
char szStr[STRSIZ]; |
|
struct mqtthdr *hdr; |
|
|
|
p = mkPkt(sess->sess_buf->msg_base, datlen); |
|
hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
|
|
|
/* write topic to database */ |
|
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, |
|
sess->sess_addr, hdr->mqtt_msg.retain); |
|
call.WritePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, AIT_GET_BUF(p), AIT_LEN(p), |
|
sess->sess_user, sess->sess_addr, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain); |
|
|
|
TAILQ_FOREACH(s, &Sessions, sess_node) { |
|
SLIST_FOREACH(st, &s->sess_subscr, st_node) { |
|
/* check for QoS */ |
|
if (st->st_subscr.sub_ret < MQTT_QOS_ACK) |
|
continue; |
|
|
|
if ((ret = regcomp(&re, st->st_subscr.sub_topic.msg_base, REG_EXTENDED))) { |
|
regerror(ret, &re, szStr, sizeof szStr); |
|
regfree(&re); |
|
ioDEBUG(3, "Error:: regcomp(%s) %s\n", (char*) |
|
st->st_subscr.sub_topic.msg_base, szStr); |
|
} |
|
if (!regexec(&re, psTopic, 1, &match, 0)) { |
|
/* MATCH */ |
|
schedWrite(root, sendPacket, mkPkt(AIT_GET_BUF(p), AIT_LEN(p)), |
|
s->sess_sock, NULL, 0); |
|
} |
|
|
|
regfree(&re); |
|
} |
|
} |
|
|
|
/* delete not retain message */ |
|
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, |
|
sess->sess_addr, 0); |
|
|
|
freePkt(&p); |
return 0; |
return 0; |
} |
} |
|
|
static int |
static int |
pubExactly(struct tagSession *sess, char * __restrict psTopic, int datlen) | pubExactly(struct tagSession *sess, u_short mid, char * __restrict psTopic, int datlen) |
{ |
{ |
|
ait_val_t *p = NULL; |
|
struct tagSession *s = NULL; |
|
struct tagStore *st = NULL; |
|
regex_t re; |
|
regmatch_t match; |
|
int ret; |
|
char szStr[STRSIZ]; |
|
struct mqtthdr *hdr; |
|
|
|
p = mkPkt(sess->sess_buf->msg_base, datlen); |
|
hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
|
|
|
/* write topic to database */ |
|
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, |
|
sess->sess_addr, hdr->mqtt_msg.retain); |
|
call.WritePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, AIT_GET_BUF(p), AIT_LEN(p), |
|
sess->sess_user, sess->sess_addr, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain); |
|
|
|
TAILQ_FOREACH(s, &Sessions, sess_node) { |
|
SLIST_FOREACH(st, &s->sess_subscr, st_node) { |
|
/* check for QoS */ |
|
if (st->st_subscr.sub_ret < MQTT_QOS_EXACTLY) |
|
continue; |
|
|
|
if ((ret = regcomp(&re, st->st_subscr.sub_topic.msg_base, REG_EXTENDED))) { |
|
regerror(ret, &re, szStr, sizeof szStr); |
|
regfree(&re); |
|
ioDEBUG(3, "Error:: regcomp(%s) %s\n", (char*) |
|
st->st_subscr.sub_topic.msg_base, szStr); |
|
} |
|
if (!regexec(&re, psTopic, 1, &match, 0)) { |
|
/* MATCH */ |
|
schedWrite(root, sendPacket, mkPkt(AIT_GET_BUF(p), AIT_LEN(p)), |
|
s->sess_sock, NULL, 0); |
|
} |
|
|
|
regfree(&re); |
|
} |
|
} |
|
|
|
freePkt(&p); |
return 0; |
return 0; |
} |
} |
|
|
Line 120 cmdPUBLISH(void *srv, int len, void *arg)
|
Line 194 cmdPUBLISH(void *srv, int len, void *arg)
|
char szTopic[STRSIZ] = { 0 }; |
char szTopic[STRSIZ] = { 0 }; |
int siz = 0; |
int siz = 0; |
u_short mid = 0; |
u_short mid = 0; |
struct tagPkt *p = NULL; | ait_val_t *p = NULL; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
Line 134 cmdPUBLISH(void *srv, int len, void *arg)
|
Line 208 cmdPUBLISH(void *srv, int len, void *arg)
|
return 0; |
return 0; |
} |
} |
|
|
/* duplicate packet for retransmit to subscribers */ |
|
/* |
|
pubpkt = mqtt_msgDup(sess->sess_buf); |
|
if (!pubpkt) { |
|
ioDEBUG(5, "Error:: in duplicate packet #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
|
return 0; |
|
} else |
|
*/ |
|
|
|
hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
switch (hdr->mqtt_msg.qos) { |
switch (hdr->mqtt_msg.qos) { |
case MQTT_QOS_ACK: |
case MQTT_QOS_ACK: |
pubAck(sess, szTopic, mqtt_pktLen(hdr)); | if (pubAck(sess, mid, szTopic, mqtt_pktLen(hdr))) |
| return 0; |
siz = mqtt_msgPUBACK(sess->sess_buf, mid); |
siz = mqtt_msgPUBACK(sess->sess_buf, mid); |
if (siz == -1) { |
if (siz == -1) { |
ioDEBUG(5, "Error:: in msgPUBACK #%d - %s", |
ioDEBUG(5, "Error:: in msgPUBACK #%d - %s", |
Line 155 cmdPUBLISH(void *srv, int len, void *arg)
|
Line 221 cmdPUBLISH(void *srv, int len, void *arg)
|
} |
} |
break; |
break; |
case MQTT_QOS_EXACTLY: |
case MQTT_QOS_EXACTLY: |
pubExactly(sess, szTopic, mqtt_pktLen(hdr)); | if (pubExactly(sess, mid, szTopic, mqtt_pktLen(hdr))) |
| return 0; |
siz = mqtt_msgPUBREC(sess->sess_buf, mid); |
siz = mqtt_msgPUBREC(sess->sess_buf, mid); |
if (siz == -1) { |
if (siz == -1) { |
ioDEBUG(5, "Error:: in msgPUBREC #%d - %s", |
ioDEBUG(5, "Error:: in msgPUBREC #%d - %s", |
Line 181 cmdPUBREL(void *srv, int len, void *arg)
|
Line 248 cmdPUBREL(void *srv, int len, void *arg)
|
struct tagSession *sess = (struct tagSession*) arg; |
struct tagSession *sess = (struct tagSession*) arg; |
int siz = 0; |
int siz = 0; |
u_short mid = 0; |
u_short mid = 0; |
struct tagPkt *p = NULL; | ait_val_t *p = NULL; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
Line 195 cmdPUBREL(void *srv, int len, void *arg)
|
Line 262 cmdPUBREL(void *srv, int len, void *arg)
|
return 0; |
return 0; |
} |
} |
|
|
// TODO:: Delete from database topic | /* delete not retain message */ |
| call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, "%", sess->sess_user, |
| sess->sess_addr, 0); |
|
|
siz = mqtt_msgPUBCOMP(sess->sess_buf, mid); |
siz = mqtt_msgPUBCOMP(sess->sess_buf, mid); |
if (siz == -1) { |
if (siz == -1) { |
ioDEBUG(5, "Error:: in msgPUBCOMP #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
ioDEBUG(5, "Error:: in msgPUBCOMP #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
return 0; |
return 0; |
} else { |
|
p = mkPkt(sess->sess_buf->msg_base, siz); |
|
memset(sess->sess_buf->msg_base, 0, sess->sess_buf->msg_len); |
|
} |
} |
|
|
|
p = mkPkt(sess->sess_buf->msg_base, siz); |
|
memset(sess->sess_buf->msg_base, 0, sess->sess_buf->msg_len); |
schedWrite(root, sendPacket, p, sess->sess_sock, NULL, 0); |
schedWrite(root, sendPacket, p, sess->sess_sock, NULL, 0); |
return 0; |
return 0; |
} |
} |
Line 221 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 289 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
struct tagStore *store; |
struct tagStore *store; |
char buf[BUFSIZ]; |
char buf[BUFSIZ]; |
void *ptr; |
void *ptr; |
struct tagPkt *p = NULL; | ait_val_t *p = NULL; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
Line 236 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 304 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
} |
} |
|
|
/* add to db */ |
/* add to db */ |
for (i = 0; i < siz; i++) { | for (i = 0; i < siz; i++, subs[i].sub_ret = MQTT_QOS_DENY) { |
/* convert topic to sql search statement */ |
/* convert topic to sql search statement */ |
if (mqtt_sqlTopic(subs[i].sub_topic.msg_base, buf, sizeof buf) == -1) { |
if (mqtt_sqlTopic(subs[i].sub_topic.msg_base, buf, sizeof buf) == -1) { |
ioDEBUG(5, "Error:: in db #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
ioDEBUG(5, "Error:: in db #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
goto end; | continue; |
} |
} |
if (call.WritePUB_subscribe(&cfg, pub, sess->sess_cid, mid, buf, |
if (call.WritePUB_subscribe(&cfg, pub, sess->sess_cid, mid, buf, |
sess->sess_user, sess->sess_addr, subs[i].sub_ret) > 0) { |
sess->sess_user, sess->sess_addr, subs[i].sub_ret) > 0) { |
store = io_malloc(sizeof(struct tagStore)); |
store = io_malloc(sizeof(struct tagStore)); |
if (!store) { |
if (!store) { |
ioSYSERR(0); |
ioSYSERR(0); |
goto end; | continue; |
} else { |
} else { |
store->st_msgid = mid; |
store->st_msgid = mid; |
mqtt_subCopy(&store->st_subscr, &subs[i]); |
mqtt_subCopy(&store->st_subscr, &subs[i]); |
Line 263 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 331 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
subs[i].sub_ret = MQTT_QOS_DENY; |
subs[i].sub_ret = MQTT_QOS_DENY; |
} else { |
} else { |
ptr = realloc(store->st_subscr.sub_topic.msg_base, strlen(buf) + 1); |
ptr = realloc(store->st_subscr.sub_topic.msg_base, strlen(buf) + 1); |
if (!ptr) { | if (!ptr) |
ioSYSERR(0); |
ioSYSERR(0); |
| else { |
subs[i].sub_ret = MQTT_QOS_DENY; | |
} else { | |
store->st_subscr.sub_topic.msg_base = ptr; |
store->st_subscr.sub_topic.msg_base = ptr; |
store->st_subscr.sub_topic.msg_len = strlen(buf) + 1; |
store->st_subscr.sub_topic.msg_len = strlen(buf) + 1; |
memcpy(store->st_subscr.sub_topic.msg_base, buf, |
memcpy(store->st_subscr.sub_topic.msg_base, buf, |
Line 280 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 346 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
|
|
subs[i].sub_ret = MQTT_QOS_PASS; |
subs[i].sub_ret = MQTT_QOS_PASS; |
} |
} |
} else | } |
subs[i].sub_ret = MQTT_QOS_DENY; | |
} |
} |
|
|
/* send acknowledge */ |
/* send acknowledge */ |
Line 309 cmdUNSUBSCRIBE(void *srv, int len, void *arg)
|
Line 374 cmdUNSUBSCRIBE(void *srv, int len, void *arg)
|
u_short mid = 0; |
u_short mid = 0; |
register int i; |
register int i; |
struct tagStore *store, *tmp; |
struct tagStore *store, *tmp; |
struct tagPkt *p = NULL; | ait_val_t *p = NULL; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
Line 365 cmdPINGREQ(void *srv, int len, void *arg)
|
Line 430 cmdPINGREQ(void *srv, int len, void *arg)
|
{ |
{ |
struct tagSession *sess = (struct tagSession*) arg; |
struct tagSession *sess = (struct tagSession*) arg; |
int siz = 0; |
int siz = 0; |
struct tagPkt *p = NULL; | ait_val_t *p = NULL; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
Line 390 int
|
Line 455 int
|
cmdCONNECT(void *srv, int len, void *arg) |
cmdCONNECT(void *srv, int len, void *arg) |
{ |
{ |
struct tagStore *store; |
struct tagStore *store; |
struct tagPkt *p; |
|
struct tagSession *sess = (struct tagSession*) arg; |
struct tagSession *sess = (struct tagSession*) arg; |
|
|
ioTRACE(2); |
ioTRACE(2); |
Line 419 cmdCONNECT(void *srv, int len, void *arg)
|
Line 483 cmdCONNECT(void *srv, int len, void *arg)
|
free(store->st_subscr.sub_value.msg_base); |
free(store->st_subscr.sub_value.msg_base); |
|
|
io_free(store); |
io_free(store); |
} |
|
|
|
while ((p = SLIST_FIRST(&sess->sess_sndpkt))) { |
|
SLIST_REMOVE_HEAD(&sess->sess_sndpkt, pkt_node); |
|
|
|
io_freeVar(&p->pkt_data); |
|
io_free(p); |
|
} |
} |
|
|
if (sess->sess_will.msg) |
if (sess->sess_will.msg) |