version 1.2.2.8, 2012/04/27 16:41:56
|
version 1.2.2.26, 2012/06/26 08:05:58
|
Line 4
|
Line 4
|
#include "mqttd_calls.h" |
#include "mqttd_calls.h" |
|
|
|
|
|
static inline ait_val_t * |
|
mkPkt(void * __restrict data, int dlen) |
|
{ |
|
ait_val_t *p = NULL; |
|
|
|
if (!(p = io_allocVar())) { |
|
ioDEBUG(7, "Error:: in send packet prepare #%d - %s", io_GetErrno(), io_GetError()); |
|
return NULL; |
|
} |
|
|
|
if (data && dlen > 0) |
|
AIT_SET_BUF(p, data, dlen); |
|
|
|
return p; |
|
} |
|
|
|
static inline void |
|
freePkt(ait_val_t ** __restrict p) |
|
{ |
|
if (!p) |
|
return; |
|
|
|
io_freeVar(p); |
|
} |
|
|
|
static void * |
|
sendPacket(sched_task_t *task) |
|
{ |
|
ait_val_t *p = TASK_ARG(task); |
|
register int n, slen; |
|
u_char *pos; |
|
|
|
if (!p || AIT_ISEMPTY(p)) { |
|
ioDEBUG(9, "Error:: invalid packet or found empty content ..."); |
|
return NULL; |
|
} |
|
|
|
for (slen = AIT_LEN(p), pos = AIT_GET_BUF(p); slen > 0; slen -= n, pos += n) { |
|
n = send(TASK_FD(task), pos, slen, MSG_NOSIGNAL); |
|
if (n == -1) { |
|
ioSYSERR(0); |
|
break; |
|
} |
|
} |
|
|
|
freePkt(&p); |
|
return NULL; |
|
} |
|
|
|
/* --------------------------------------------------- */ |
|
|
|
static int |
|
pubOnce(struct tagSession *sess, 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]; |
|
|
|
TAILQ_FOREACH(s, &Sessions, sess_node) { |
|
SLIST_FOREACH(st, &s->sess_subscr, st_node) { |
|
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 */ |
|
p = mkPkt(sess->sess_buf->msg_base, datlen); |
|
schedWrite(root, sendPacket, p, s->sess_sock, NULL, 0); |
|
} |
|
|
|
regfree(&re); |
|
} |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
static int |
|
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, flg = 0; |
|
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.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); |
|
|
|
TAILQ_FOREACH(s, &Sessions, sess_node) { |
|
SLIST_FOREACH(st, &s->sess_subscr, st_node) { |
|
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)) { |
|
flg = 1; /* MATCH */ |
|
schedWrite(root, sendPacket, p, s->sess_sock, NULL, 0); |
|
} |
|
|
|
regfree(&re); |
|
} |
|
} |
|
|
|
/* if subscriber not found then free packet */ |
|
if (!flg) |
|
freePkt(&p); |
|
|
|
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, sess->sess_addr, -1); |
|
return 0; |
|
} |
|
|
|
static int |
|
pubExactly(struct tagSession *sess, u_short mid, char * __restrict psTopic, int datlen) |
|
{ |
|
return 0; |
|
} |
|
|
|
|
int |
int |
cmdPUBLISH(void *srv, void *arg) | cmdPUBLISH(void *srv, int len, void *arg) |
{ |
{ |
struct mqtthdr *hdr; |
struct mqtthdr *hdr; |
struct tagSession *sess = (struct tagSession*) arg; |
struct tagSession *sess = (struct tagSession*) arg; |
|
char szTopic[STRSIZ] = { 0 }; |
|
int siz = 0; |
|
u_short mid = 0; |
|
ait_val_t *p = NULL; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
if (!sess) |
if (!sess) |
return -1; |
return -1; |
|
|
|
ioDEBUG(5, "Exec PUBLISH session"); |
|
siz = mqtt_readPUBLISH(sess->sess_buf, szTopic, sizeof szTopic, &mid, NULL); |
|
if (siz == -1) { |
|
ioDEBUG(5, "Error:: in readPUBLISH #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
|
return 0; |
|
} |
|
|
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_ONCE: |
|
break; |
|
case MQTT_QOS_ACK: |
case MQTT_QOS_ACK: |
|
if (pubAck(sess, mid, szTopic, mqtt_pktLen(hdr))) |
|
return 0; |
|
siz = mqtt_msgPUBACK(sess->sess_buf, mid); |
|
if (siz == -1) { |
|
ioDEBUG(5, "Error:: in msgPUBACK #%d - %s", |
|
mqtt_GetErrno(), mqtt_GetError()); |
|
return 0; |
|
} |
break; |
break; |
case MQTT_QOS_EXACTLY: |
case MQTT_QOS_EXACTLY: |
|
if (pubExactly(sess, mid, szTopic, mqtt_pktLen(hdr))) |
|
return 0; |
|
siz = mqtt_msgPUBREC(sess->sess_buf, mid); |
|
if (siz == -1) { |
|
ioDEBUG(5, "Error:: in msgPUBREC #%d - %s", |
|
mqtt_GetErrno(), mqtt_GetError()); |
|
return 0; |
|
} |
break; |
break; |
|
case MQTT_QOS_ONCE: |
|
pubOnce(sess, szTopic, mqtt_pktLen(hdr)); |
default: |
default: |
ioDEBUG(1, "Error:: Unknown QoS %d - rejected publishing request", |
|
hdr->mqtt_msg.qos); |
|
return 0; |
return 0; |
} |
} |
|
|
|
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); |
return 0; |
return 0; |
} |
} |
|
|
int |
int |
cmdPUBREL(void *srv, void *arg) | cmdPUBREL(void *srv, int len, void *arg) |
{ |
{ |
struct mqtthdr *hdr; |
|
struct tagSession *sess = (struct tagSession*) arg; |
struct tagSession *sess = (struct tagSession*) arg; |
|
int siz = 0; |
|
u_short mid = 0; |
|
ait_val_t *p = NULL; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
if (!sess) |
if (!sess) |
return -1; |
return -1; |
|
|
hdr = (struct mqtthdr*) sess->sess_buf->msg_base; | ioDEBUG(5, "Exec PUBREL session"); |
| mid = mqtt_readPUBREL(sess->sess_buf); |
| if (mid == (u_short) -1) { |
| ioDEBUG(5, "Error:: in readPUBREL #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
| return 0; |
| } |
|
|
|
// TODO:: Delete from database topic |
|
|
|
siz = mqtt_msgPUBCOMP(sess->sess_buf, mid); |
|
if (siz == -1) { |
|
ioDEBUG(5, "Error:: in msgPUBCOMP #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
|
return 0; |
|
} else { |
|
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); |
return 0; |
return 0; |
} |
} |
|
|
int |
int |
cmdSUBSCRIBE(void *srv, void *arg) | cmdSUBSCRIBE(void *srv, int len, void *arg) |
{ |
{ |
struct tagSession *sess = (struct tagSession*) arg; |
struct tagSession *sess = (struct tagSession*) arg; |
mqtt_subscr_t *subs = NULL; |
mqtt_subscr_t *subs = NULL; |
Line 57 cmdSUBSCRIBE(void *srv, void *arg)
|
Line 238 cmdSUBSCRIBE(void *srv, void *arg)
|
u_short mid = 0; |
u_short mid = 0; |
register int i; |
register int i; |
struct tagStore *store; |
struct tagStore *store; |
|
char buf[BUFSIZ]; |
|
void *ptr; |
|
ait_val_t *p = NULL; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
Line 71 cmdSUBSCRIBE(void *srv, void *arg)
|
Line 255 cmdSUBSCRIBE(void *srv, 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) { |
if (call.WritePUB_subscribe(&cfg, pub, mid, subs[i].sub_topic.msg_base, | /* convert topic to sql search statement */ |
| if (mqtt_sqlTopic(subs[i].sub_topic.msg_base, buf, sizeof buf) == -1) { |
| ioDEBUG(5, "Error:: in db #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
| continue; |
| } |
| 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 = 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]); |
} |
} |
|
|
/* add to cache */ |
/* add to cache */ |
SESS_ELEM_LOCK(sess); |
|
SLIST_INSERT_HEAD(&sess->sess_subscr, store, st_node); |
SLIST_INSERT_HEAD(&sess->sess_subscr, store, st_node); |
SESS_ELEM_UNLOCK(sess); |
|
|
|
subs[i].sub_ret = MQTT_QOS_PASS; | /* convert topic to regexp */ |
} else | if (mqtt_expandTopic(subs[i].sub_topic.msg_base, buf, sizeof buf, 1, 1) == -1) { |
subs[i].sub_ret = MQTT_QOS_DENY; | 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; |
| } |
| } |
} |
} |
|
|
/* send acknowledge */ |
/* send acknowledge */ |
Line 98 cmdSUBSCRIBE(void *srv, void *arg)
|
Line 305 cmdSUBSCRIBE(void *srv, void *arg)
|
if (siz == -1) { |
if (siz == -1) { |
ioDEBUG(5, "Error:: in msgSUBACK #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
ioDEBUG(5, "Error:: in msgSUBACK #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
goto end; |
goto end; |
} | } else { |
if ((siz = send(sess->sess_sock, sess->sess_buf->msg_base, siz, 0)) == -1) | p = mkPkt(sess->sess_buf->msg_base, siz); |
ioSYSERR(0); | |
else { | |
ioDEBUG(5, "Sended %d bytes.", siz); | |
memset(sess->sess_buf->msg_base, 0, sess->sess_buf->msg_len); |
memset(sess->sess_buf->msg_base, 0, sess->sess_buf->msg_len); |
} |
} |
|
|
|
schedWrite(root, sendPacket, p, sess->sess_sock, NULL, 0); |
end: |
end: |
mqtt_subFree(&subs); |
mqtt_subFree(&subs); |
return 0; |
return 0; |
} |
} |
|
|
int |
int |
cmdUNSUBSCRIBE(void *srv, void *arg) | cmdUNSUBSCRIBE(void *srv, int len, void *arg) |
{ |
{ |
struct tagSession *sess = (struct tagSession*) arg; |
struct tagSession *sess = (struct tagSession*) arg; |
|
mqtt_subscr_t *subs = NULL; |
|
int siz = 0; |
|
u_short mid = 0; |
|
register int i; |
|
struct tagStore *store, *tmp; |
|
ait_val_t *p = NULL; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
if (!sess) |
if (!sess) |
return -1; |
return -1; |
|
|
|
ioDEBUG(5, "Exec UNSUBSCRIBE session"); |
|
siz = mqtt_readUNSUBSCRIBE(sess->sess_buf, &mid, &subs); |
|
if (siz == -1) { |
|
ioDEBUG(5, "Error:: in readUNSUBSCRIBE #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
|
return 0; |
|
} |
|
|
|
/* del from db */ |
|
for (i = 0; i < siz; i++) { |
|
SLIST_FOREACH_SAFE(store, &sess->sess_subscr, st_node, tmp) { |
|
if (store->st_subscr.sub_ret == subs[i].sub_ret && |
|
store->st_subscr.sub_topic.msg_base && |
|
!strcmp(store->st_subscr.sub_topic.msg_base, |
|
subs[i].sub_topic.msg_base)) { |
|
SLIST_REMOVE(&sess->sess_subscr, store, tagStore, st_node); |
|
|
|
if (store->st_subscr.sub_topic.msg_base) |
|
free(store->st_subscr.sub_topic.msg_base); |
|
if (store->st_subscr.sub_value.msg_base) |
|
free(store->st_subscr.sub_value.msg_base); |
|
io_free(store); |
|
} |
|
} |
|
|
|
call.DeletePUB_subscribe(&cfg, pub, sess->sess_cid, subs[i].sub_topic.msg_base, |
|
sess->sess_user, "%"); |
|
} |
|
|
|
/* send acknowledge */ |
|
siz = mqtt_msgUNSUBACK(sess->sess_buf, mid); |
|
if (siz == -1) { |
|
ioDEBUG(5, "Error:: in msgUNSUBACK #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
|
goto end; |
|
} else { |
|
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); |
|
end: |
|
mqtt_subFree(&subs); |
return 0; |
return 0; |
} |
} |
|
|
int |
int |
cmdPINGREQ(void *srv, void *arg) | 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; |
|
ait_val_t *p = NULL; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
Line 139 cmdPINGREQ(void *srv, void *arg)
|
Line 393 cmdPINGREQ(void *srv, void *arg)
|
if (siz == -1) { |
if (siz == -1) { |
ioDEBUG(5, "Error:: in msgPINGRESP #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
ioDEBUG(5, "Error:: in msgPINGRESP #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
return 0; |
return 0; |
} |
|
if ((siz = send(sess->sess_sock, sess->sess_buf->msg_base, siz, 0)) == -1) { |
|
ioSYSERR(0); |
|
return 0; |
|
} else { |
} else { |
ioDEBUG(5, "Sended %d bytes.", siz); | p = mkPkt(sess->sess_buf->msg_base, siz); |
memset(sess->sess_buf->msg_base, 0, sess->sess_buf->msg_len); |
memset(sess->sess_buf->msg_base, 0, sess->sess_buf->msg_len); |
} |
} |
|
|
|
schedWrite(root, sendPacket, p, sess->sess_sock, NULL, 0); |
return 0; |
return 0; |
} |
} |
|
|
int |
int |
cmdCONNECT(void *srv, void *arg) | cmdCONNECT(void *srv, int len, void *arg) |
{ |
{ |
struct tagStore *store; |
struct tagStore *store; |
struct tagSession *sess = (struct tagSession*) arg; |
struct tagSession *sess = (struct tagSession*) arg; |
Line 163 cmdCONNECT(void *srv, void *arg)
|
Line 414 cmdCONNECT(void *srv, void *arg)
|
return -1; |
return -1; |
|
|
ioDEBUG(5, "Exec CONNECT session"); |
ioDEBUG(5, "Exec CONNECT session"); |
SESS_LOCK; |
|
TAILQ_REMOVE(&Sessions, sess, sess_node); |
TAILQ_REMOVE(&Sessions, sess, sess_node); |
SESS_UNLOCK; |
|
|
|
if (call.FiniSessPUB) | if (sess->sess_clean) { |
call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%"); | if (call.FiniSessPUB) |
| call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%"); |
| if (call.DeletePUB_subscribe) |
| call.DeletePUB_subscribe(&cfg, pub, sess->sess_cid, "%", sess->sess_user, "%"); |
| if (call.WipePUB_topic) |
| call.WipePUB_topic(&cfg, pub, sess->sess_cid, sess->sess_user, -1); |
| } |
|
|
SESS_ELEM_LOCK(sess); |
|
while ((store = SLIST_FIRST(&sess->sess_subscr))) { |
while ((store = SLIST_FIRST(&sess->sess_subscr))) { |
SLIST_REMOVE_HEAD(&sess->sess_subscr, st_node); |
SLIST_REMOVE_HEAD(&sess->sess_subscr, st_node); |
|
|
Line 179 cmdCONNECT(void *srv, void *arg)
|
Line 433 cmdCONNECT(void *srv, void *arg)
|
if (store->st_subscr.sub_value.msg_base) |
if (store->st_subscr.sub_value.msg_base) |
free(store->st_subscr.sub_value.msg_base); |
free(store->st_subscr.sub_value.msg_base); |
|
|
free(store); | io_free(store); |
} |
} |
SESS_ELEM_UNLOCK(sess); |
|
|
|
if (sess->sess_will.msg) |
if (sess->sess_will.msg) |
free(sess->sess_will.msg); |
free(sess->sess_will.msg); |
Line 190 cmdCONNECT(void *srv, void *arg)
|
Line 443 cmdCONNECT(void *srv, void *arg)
|
|
|
call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid, |
call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid, |
sess->sess_addr, sess->sess_user); |
sess->sess_addr, sess->sess_user); |
return 0; | |
| return -3; /* reconnect client */ |
} |
} |
|
|
int |
int |
cmdDISCONNECT(void *srv, void *arg) | cmdDISCONNECT(void *srv, int len, void *arg) |
{ |
{ |
struct tagSession *sess = (struct tagSession*) arg; |
struct tagSession *sess = (struct tagSession*) arg; |
|
|
Line 204 cmdDISCONNECT(void *srv, void *arg)
|
Line 458 cmdDISCONNECT(void *srv, void *arg)
|
return -1; |
return -1; |
|
|
ioDEBUG(5, "Exec DISCONNECT session"); |
ioDEBUG(5, "Exec DISCONNECT session"); |
SESS_LOCK; |
|
TAILQ_REMOVE(&Sessions, sess, sess_node); |
|
SESS_UNLOCK; |
|
|
|
call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid, |
call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid, |
sess->sess_addr, sess->sess_user); |
sess->sess_addr, sess->sess_user); |
return 0; | |
| return -2; /* must terminate dispatcher */ |
} |
} |