version 1.2.2.26, 2012/06/26 08:05:58
|
version 1.2.2.32, 2012/06/29 15:43:13
|
Line 1
|
Line 1
|
#include "global.h" |
#include "global.h" |
#include "mqttd.h" |
#include "mqttd.h" |
|
#include "utils.h" |
#include "rtlm.h" |
#include "rtlm.h" |
#include "mqttd_calls.h" |
#include "mqttd_calls.h" |
|
|
Line 60 pubOnce(struct tagSession *sess, char * __restrict psT
|
Line 61 pubOnce(struct tagSession *sess, char * __restrict psT
|
{ |
{ |
ait_val_t *p = NULL; |
ait_val_t *p = NULL; |
struct tagSession *s = NULL; |
struct tagSession *s = NULL; |
struct tagStore *st = NULL; | struct tagStore *st_, *st = NULL; |
regex_t re; |
regex_t re; |
regmatch_t match; |
regmatch_t match; |
int ret; |
int ret; |
char szStr[STRSIZ]; |
char szStr[STRSIZ]; |
|
|
|
|
TAILQ_FOREACH(s, &Sessions, sess_node) { |
TAILQ_FOREACH(s, &Sessions, sess_node) { |
SLIST_FOREACH(st, &s->sess_subscr, st_node) { | SLIST_FOREACH_SAFE(st, &s->sess_subscr, st_node, st_) { |
if ((ret = regcomp(&re, st->st_subscr.sub_topic.msg_base, REG_EXTENDED))) { |
if ((ret = regcomp(&re, st->st_subscr.sub_topic.msg_base, REG_EXTENDED))) { |
regerror(ret, &re, szStr, sizeof szStr); |
regerror(ret, &re, szStr, sizeof szStr); |
regfree(&re); |
regfree(&re); |
Line 92 pubAck(struct tagSession *sess, u_short mid, char * __
|
Line 94 pubAck(struct tagSession *sess, u_short mid, char * __
|
{ |
{ |
ait_val_t *p = NULL; |
ait_val_t *p = NULL; |
struct tagSession *s = NULL; |
struct tagSession *s = NULL; |
struct tagStore *st = NULL; | struct tagStore *st_, *st = NULL; |
regex_t re; |
regex_t re; |
regmatch_t match; |
regmatch_t match; |
int ret, flg = 0; | int ret; |
char szStr[STRSIZ]; |
char szStr[STRSIZ]; |
struct mqtthdr *hdr; |
struct mqtthdr *hdr; |
|
|
Line 103 pubAck(struct tagSession *sess, u_short mid, char * __
|
Line 105 pubAck(struct tagSession *sess, u_short mid, char * __
|
hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
|
|
/* write topic to database */ |
/* 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), |
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.retain); | sess->sess_user, sess->sess_addr, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain); |
|
|
TAILQ_FOREACH(s, &Sessions, sess_node) { |
TAILQ_FOREACH(s, &Sessions, sess_node) { |
SLIST_FOREACH(st, &s->sess_subscr, st_node) { | SLIST_FOREACH_SAFE(st, &s->sess_subscr, st_node, st_) { |
| /* 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))) { |
if ((ret = regcomp(&re, st->st_subscr.sub_topic.msg_base, REG_EXTENDED))) { |
regerror(ret, &re, szStr, sizeof szStr); |
regerror(ret, &re, szStr, sizeof szStr); |
regfree(&re); |
regfree(&re); |
Line 115 pubAck(struct tagSession *sess, u_short mid, char * __
|
Line 123 pubAck(struct tagSession *sess, u_short mid, char * __
|
st->st_subscr.sub_topic.msg_base, szStr); |
st->st_subscr.sub_topic.msg_base, szStr); |
} |
} |
if (!regexec(&re, psTopic, 1, &match, 0)) { |
if (!regexec(&re, psTopic, 1, &match, 0)) { |
flg = 1; /* MATCH */ | /* MATCH */ |
schedWrite(root, sendPacket, p, s->sess_sock, NULL, 0); | schedWrite(root, sendPacket, mkPkt(AIT_GET_BUF(p), AIT_LEN(p)), |
| s->sess_sock, NULL, 0); |
} |
} |
|
|
regfree(&re); |
regfree(&re); |
} |
} |
} |
} |
|
|
/* if subscriber not found then free packet */ | /* delete not retain message */ |
if (!flg) | call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, |
freePkt(&p); | sess->sess_addr, 0); |
|
|
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, sess->sess_addr, -1); | freePkt(&p); |
return 0; |
return 0; |
} |
} |
|
|
static int |
static int |
pubExactly(struct tagSession *sess, u_short mid, 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_, *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_SAFE(st, &s->sess_subscr, st_node, st_) { |
|
/* 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 214 cmdPUBREL(void *srv, int len, void *arg)
|
Line 264 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 256 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 307 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
|
|
/* add to db */ |
/* add to db */ |
for (i = 0; i < siz; i++, subs[i].sub_ret = MQTT_QOS_DENY) { |
for (i = 0; i < siz; i++, subs[i].sub_ret = MQTT_QOS_DENY) { |
/* convert topic to sql search statement */ | store = io_malloc(sizeof(struct tagStore)); |
if (mqtt_sqlTopic(subs[i].sub_topic.msg_base, buf, sizeof buf) == -1) { | if (!store) { |
ioDEBUG(5, "Error:: in db #%d - %s", mqtt_GetErrno(), mqtt_GetError()); | ioSYSERR(0); |
continue; |
continue; |
|
} else { |
|
store->st_msgid = mid; |
|
mqtt_subCopy(&store->st_subscr, &subs[i]); |
} |
} |
if (call.WritePUB_subscribe(&cfg, pub, sess->sess_cid, mid, buf, | |
sess->sess_user, sess->sess_addr, subs[i].sub_ret) > 0) { | /* add to cache */ |
store = io_malloc(sizeof(struct tagStore)); | SLIST_INSERT_HEAD(&sess->sess_subscr, store, st_node); |
if (!store) { | |
| /* convert topic to regexp */ |
| if (mqtt_expandTopic(subs[i].sub_topic.msg_base, buf, sizeof buf, 1, 1) == -1) { |
| ioDEBUG(5, "Error:: in regexp #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
| |
| subs[i].sub_ret = MQTT_QOS_DENY; |
| } else { |
| ptr = realloc(store->st_subscr.sub_topic.msg_base, strlen(buf) + 1); |
| if (!ptr) { |
ioSYSERR(0); |
ioSYSERR(0); |
continue; |
continue; |
} else { |
} else { |
store->st_msgid = mid; | store->st_subscr.sub_topic.msg_base = ptr; |
mqtt_subCopy(&store->st_subscr, &subs[i]); | store->st_subscr.sub_topic.msg_len = strlen(buf) + 1; |
| memcpy(store->st_subscr.sub_topic.msg_base, buf, |
| store->st_subscr.sub_topic.msg_len); |
} |
} |
|
|
/* add to cache */ | call.LOG(logg, "Added [%s] SUBSCRIBE '%s'(%d) from %s\n", sess->sess_cid, |
SLIST_INSERT_HEAD(&sess->sess_subscr, store, st_node); | store->st_subscr.sub_topic.msg_base, |
| store->st_subscr.sub_topic.msg_len, sess->sess_addr); |
|
|
/* convert topic to regexp */ | subs[i].sub_ret = MQTT_QOS_PASS; |
if (mqtt_expandTopic(subs[i].sub_topic.msg_base, buf, sizeof buf, 1, 1) == -1) { | |
ioDEBUG(5, "Error:: in regexp #%d - %s", mqtt_GetErrno(), mqtt_GetError()); | |
| |
subs[i].sub_ret = MQTT_QOS_DENY; | |
} else { | |
ptr = realloc(store->st_subscr.sub_topic.msg_base, strlen(buf) + 1); | |
if (!ptr) | |
ioSYSERR(0); | |
else { | |
store->st_subscr.sub_topic.msg_base = ptr; | |
store->st_subscr.sub_topic.msg_len = strlen(buf) + 1; | |
memcpy(store->st_subscr.sub_topic.msg_base, buf, | |
store->st_subscr.sub_topic.msg_len); | |
} | |
| |
call.LOG(logg, "Added [%s] SUBSCRIBE '%s'(%d) from %s\n", sess->sess_cid, | |
store->st_subscr.sub_topic.msg_base, | |
store->st_subscr.sub_topic.msg_len, sess->sess_addr); | |
| |
subs[i].sub_ret = MQTT_QOS_PASS; | |
} | |
} |
} |
|
|
|
call.WritePUB_subscribe(&cfg, pub, sess->sess_cid, mid, buf, |
|
sess->sess_user, sess->sess_addr, subs[i].sub_ret); |
} |
} |
|
|
/* send acknowledge */ |
/* send acknowledge */ |
Line 435 cmdCONNECT(void *srv, int len, void *arg)
|
Line 482 cmdCONNECT(void *srv, int len, void *arg)
|
|
|
io_free(store); |
io_free(store); |
} |
} |
|
|
|
if (sess->sess_will.flag) |
|
srv_Will(sess); |
|
|
if (sess->sess_will.msg) |
if (sess->sess_will.msg) |
free(sess->sess_will.msg); |
free(sess->sess_will.msg); |