version 1.2.2.14, 2012/05/08 13:10:41
|
version 1.2.2.18, 2012/05/27 10:04:05
|
Line 4
|
Line 4
|
#include "mqttd_calls.h" |
#include "mqttd_calls.h" |
|
|
|
|
|
static int |
|
pubOnce(struct tagSession *sess, u_short mid, char * __restrict psTopic, |
|
int topicLen, char * __restrict data, int datlen) |
|
{ |
|
return 0; |
|
} |
|
|
|
static int |
|
pubAck(struct tagSession *sess, u_short mid, char * __restrict psTopic, |
|
int topicLen, char * __restrict data, int datlen) |
|
{ |
|
return 0; |
|
} |
|
|
|
static int |
|
pubExactly(struct tagSession *sess, u_short mid, char * __restrict psTopic, |
|
int topicLen, char * __restrict data, int datlen) |
|
{ |
|
return 0; |
|
} |
|
|
|
|
int |
int |
cmdPUBLISH(void *srv, int len, 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; |
|
void *data = NULL; |
|
char szTopic[STRSIZ] = { 0 }; |
|
int siz = 0; |
|
u_short mid = 0; |
|
|
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, &data); |
|
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: |
|
pubAck(sess, mid, szTopic, sizeof szTopic, data, siz); |
|
siz = mqtt_msgPUBACK(sess->sess_buf, mid); |
|
if (siz == -1) { |
|
ioDEBUG(5, "Error:: in msgPUBACK #%d - %s", |
|
mqtt_GetErrno(), mqtt_GetError()); |
|
goto end; |
|
} |
break; |
break; |
case MQTT_QOS_EXACTLY: |
case MQTT_QOS_EXACTLY: |
|
pubExactly(sess, mid, szTopic, sizeof szTopic, data, siz); |
|
siz = mqtt_msgPUBREC(sess->sess_buf, mid); |
|
if (siz == -1) { |
|
ioDEBUG(5, "Error:: in msgPUBREC #%d - %s", |
|
mqtt_GetErrno(), mqtt_GetError()); |
|
goto end; |
|
} |
break; |
break; |
|
case MQTT_QOS_ONCE: |
|
pubOnce(sess, mid, szTopic, sizeof szTopic, data, siz); |
default: |
default: |
ioDEBUG(1, "Error:: Unknown QoS %d - rejected publishing request", | goto end; |
hdr->mqtt_msg.qos); | |
return 0; | |
} |
} |
|
|
|
if ((siz = send(sess->sess_sock, sess->sess_buf->msg_base, siz, MSG_NOSIGNAL)) == -1) |
|
ioSYSERR(0); |
|
else { |
|
ioDEBUG(5, "Sended %d bytes.", siz); |
|
memset(sess->sess_buf->msg_base, 0, sess->sess_buf->msg_len); |
|
} |
|
end: |
|
if (data) |
|
free(data); |
return 0; |
return 0; |
} |
} |
|
|
int |
int |
cmdPUBREL(void *srv, int len, 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; |
|
|
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; |
|
} |
|
if ((siz = send(sess->sess_sock, sess->sess_buf->msg_base, siz, MSG_NOSIGNAL)) == -1) |
|
ioSYSERR(0); |
|
else { |
|
ioDEBUG(5, "Sended %d bytes.", siz); |
|
memset(sess->sess_buf->msg_base, 0, sess->sess_buf->msg_len); |
|
} |
|
|
return 0; |
return 0; |
} |
} |
|
|
Line 57 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 131 cmdSUBSCRIBE(void *srv, int len, 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; |
|
|
ioTRACE(2); |
ioTRACE(2); |
|
|
Line 72 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 148 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++) { |
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()); |
| goto end; |
| } |
| 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; |
goto end; |
Line 86 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 167 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
/* add to cache */ |
/* add to cache */ |
SLIST_INSERT_HEAD(&sess->sess_subscr, store, st_node); |
SLIST_INSERT_HEAD(&sess->sess_subscr, store, st_node); |
|
|
|
/* convert topic to regexp */ |
|
if (mqtt_expandTopic(subs[i].sub_topic.msg_base, buf, sizeof buf, 1, 0) == -1) { |
|
ioDEBUG(5, "Error:: in regexp #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
|
goto end; |
|
} else { |
|
ptr = realloc(store->st_subscr.sub_topic.msg_base, strlen(buf) + 1); |
|
if (!ptr) { |
|
ioSYSERR(0); |
|
goto end; |
|
} 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; |
subs[i].sub_ret = MQTT_QOS_PASS; |
} else |
} else |
subs[i].sub_ret = MQTT_QOS_DENY; |
subs[i].sub_ret = MQTT_QOS_DENY; |
Line 143 cmdUNSUBSCRIBE(void *srv, int len, void *arg)
|
Line 245 cmdUNSUBSCRIBE(void *srv, int len, void *arg)
|
free(store->st_subscr.sub_topic.msg_base); |
free(store->st_subscr.sub_topic.msg_base); |
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); |
} |
} |
} |
} |
|
|
call.DeletePUB_subscribe(&cfg, pub, subs[i].sub_topic.msg_base, | call.DeletePUB_subscribe(&cfg, pub, sess->sess_cid, subs[i].sub_topic.msg_base, |
sess->sess_user, sess->sess_addr); | sess->sess_user, "%"); |
} |
} |
|
|
/* send acknowledge */ |
/* send acknowledge */ |
Line 210 cmdCONNECT(void *srv, int len, void *arg)
|
Line 312 cmdCONNECT(void *srv, int len, void *arg)
|
ioDEBUG(5, "Exec CONNECT session"); |
ioDEBUG(5, "Exec CONNECT session"); |
TAILQ_REMOVE(&Sessions, sess, sess_node); |
TAILQ_REMOVE(&Sessions, sess, sess_node); |
|
|
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); |
| } |
|
|
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 221 cmdCONNECT(void *srv, int len, void *arg)
|
Line 329 cmdCONNECT(void *srv, int len, 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); |
} |
} |
|
|
if (sess->sess_will.msg) |
if (sess->sess_will.msg) |