version 1.2.2.32, 2012/06/29 15:43:13
|
version 1.3, 2012/07/03 09:02:50
|
Line 42 sendPacket(sched_task_t *task)
|
Line 42 sendPacket(sched_task_t *task)
|
return NULL; |
return NULL; |
} |
} |
|
|
|
ioDEBUG(7, "Send packet length %d for socket %d\n", AIT_LEN(p), (u_int) TASK_FD(task)); |
|
|
for (slen = AIT_LEN(p), pos = AIT_GET_BUF(p); slen > 0; slen -= n, pos += n) { |
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); |
n = send(TASK_FD(task), pos, slen, MSG_NOSIGNAL); |
if (n == -1) { |
if (n == -1) { |
Line 54 sendPacket(sched_task_t *task)
|
Line 56 sendPacket(sched_task_t *task)
|
return NULL; |
return NULL; |
} |
} |
|
|
/* --------------------------------------------------- */ |
|
|
|
static int |
static int |
pubOnce(struct tagSession *sess, char * __restrict psTopic, int datlen) | search4send(struct tagSession * __restrict sess, const char *topic, int datlen, char qos) |
{ |
{ |
|
regex_t re; |
|
regmatch_t match; |
ait_val_t *p = NULL; |
ait_val_t *p = NULL; |
struct tagSession *s = NULL; |
struct tagSession *s = NULL; |
struct tagStore *st_, *st = NULL; |
struct tagStore *st_, *st = NULL; |
regex_t re; |
|
regmatch_t match; |
|
int ret; |
|
char szStr[STRSIZ]; |
char szStr[STRSIZ]; |
|
int ret; |
|
|
|
assert(sess); |
|
|
TAILQ_FOREACH(s, &Sessions, sess_node) { |
TAILQ_FOREACH(s, &Sessions, sess_node) { |
SLIST_FOREACH_SAFE(st, &s->sess_subscr, st_node, st_) { |
SLIST_FOREACH_SAFE(st, &s->sess_subscr, st_node, st_) { |
if ((ret = regcomp(&re, st->st_subscr.sub_topic.msg_base, REG_EXTENDED))) { | /* check for QoS */ |
regerror(ret, &re, szStr, sizeof szStr); | if (st->st_subscr.sub_ret >= qos) { |
| 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, topic, 1, &match, 0)) { |
| /* MATCH */ |
| p = mkPkt(sess->sess_buf->msg_base, datlen); |
| schedWrite(root, sendPacket, p, s->sess_sock, NULL, 0); |
| } |
| |
regfree(&re); |
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; |
return 0; |
} |
} |
|
|
|
/* --------------------------------------------------- */ |
|
|
|
void * |
|
sendRetain(sched_task_t *task) |
|
{ |
|
mqtt_subscr_t *subs, *s; |
|
struct tagSession *sess; |
|
int siz; |
|
|
|
ioTRACE(2); |
|
|
|
assert(task); |
|
|
|
sess = TASK_ARG(task); |
|
assert(sess); |
|
|
|
if (!sess->sess_buf) { |
|
ioDEBUG(9, "WARNING! No allocated buffer!?!\n"); |
|
return NULL; |
|
} |
|
|
|
subs = call.ReadPUB_topic(&cfg, pub, "%", "%", 1); |
|
if (!subs) |
|
return NULL; |
|
|
|
for (s = subs; s && s->sub_topic.msg_base; s++) { |
|
siz = s->sub_value.msg_len; |
|
memcpy(sess->sess_buf->msg_base, s->sub_value.msg_base, |
|
MIN(sess->sess_buf->msg_len, s->sub_value.msg_len)); |
|
ioDEBUG(7, "Sending retain message %d bytes, QoS %hhd topic '%s' data length %d\n", |
|
siz, s->sub_ret, (char*) s->sub_topic.msg_base, s->sub_value.msg_len); |
|
if (siz > 0) |
|
search4send(sess, s->sub_topic.msg_base, siz, s->sub_ret); |
|
} |
|
|
|
mqtt_subFree(&subs); |
|
return NULL; |
|
} |
|
|
|
int |
|
pubWill(struct tagSession * __restrict sess) |
|
{ |
|
int datlen; |
|
|
|
ioTRACE(2); |
|
|
|
/* prepare will packet */ |
|
datlen = mqtt_msgPUBLISH(sess->sess_buf, sess->sess_will.topic, 0xDEAD, 0, 1, 0, |
|
sess->sess_will.msg, sess->sess_will.msg ? strlen(sess->sess_will.msg) : 0); |
|
if (datlen == -1) |
|
return -1; /* error */ |
|
|
|
return search4send(sess, sess->sess_will.topic, datlen, MQTT_QOS_ACK); |
|
} |
|
|
static int |
static int |
|
pubOnce(struct tagSession *sess, char * __restrict psTopic, int datlen) |
|
{ |
|
return search4send(sess, psTopic, datlen, MQTT_QOS_ONCE); |
|
} |
|
|
|
static int |
pubAck(struct tagSession *sess, u_short mid, char * __restrict psTopic, int datlen) |
pubAck(struct tagSession *sess, u_short mid, char * __restrict psTopic, int datlen) |
{ |
{ |
ait_val_t *p = NULL; | struct mqtthdr *hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
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 */ |
/* write topic to database */ |
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, |
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, |
sess->sess_addr, hdr->mqtt_msg.retain); |
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, |
sess->sess_user, sess->sess_addr, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain); | sess->sess_buf->msg_base, datlen, sess->sess_user, sess->sess_addr, |
| hdr->mqtt_msg.qos, hdr->mqtt_msg.retain); |
|
|
TAILQ_FOREACH(s, &Sessions, sess_node) { | search4send(sess, psTopic, datlen, MQTT_QOS_ACK); |
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))) { |
|
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 */ |
/* delete not retain message */ |
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, |
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, |
sess->sess_addr, 0); |
sess->sess_addr, 0); |
|
|
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 mqtthdr *hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
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 */ |
/* write topic to database */ |
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, |
call.DeletePUB_topic(&cfg, pub, sess->sess_cid, mid, psTopic, sess->sess_user, |
sess->sess_addr, hdr->mqtt_msg.retain); |
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, |
sess->sess_user, sess->sess_addr, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain); | sess->sess_buf->msg_base, datlen, sess->sess_user, sess->sess_addr, |
| hdr->mqtt_msg.qos, hdr->mqtt_msg.retain); |
|
|
TAILQ_FOREACH(s, &Sessions, sess_node) { | return search4send(sess, psTopic, datlen, MQTT_QOS_EXACTLY); |
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; | |
} |
} |
|
|
|
|
Line 306 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 308 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++) { |
store = io_malloc(sizeof(struct tagStore)); |
store = io_malloc(sizeof(struct tagStore)); |
if (!store) { |
if (!store) { |
ioSYSERR(0); |
ioSYSERR(0); |
Line 314 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 316 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
} else { |
} else { |
store->st_msgid = mid; |
store->st_msgid = mid; |
mqtt_subCopy(&store->st_subscr, &subs[i]); |
mqtt_subCopy(&store->st_subscr, &subs[i]); |
|
subs[i].sub_ret = MQTT_QOS_DENY; |
} |
} |
|
|
/* add to cache */ |
/* add to cache */ |
Line 322 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 325 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
/* convert topic to regexp */ |
/* convert topic to regexp */ |
if (mqtt_expandTopic(subs[i].sub_topic.msg_base, buf, sizeof buf, 1, 1) == -1) { |
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()); |
ioDEBUG(5, "Error:: in regexp #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
|
|
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) { |
Line 336 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
Line 337 cmdSUBSCRIBE(void *srv, int len, void *arg)
|
store->st_subscr.sub_topic.msg_len); |
store->st_subscr.sub_topic.msg_len); |
} |
} |
|
|
call.LOG(logg, "Added [%s] SUBSCRIBE '%s'(%d) from %s\n", sess->sess_cid, | /* store to db */ |
store->st_subscr.sub_topic.msg_base, | call.WritePUB_subscribe(&cfg, pub, sess->sess_cid, mid, buf, |
store->st_subscr.sub_topic.msg_len, sess->sess_addr); | sess->sess_user, sess->sess_addr, store->st_subscr.sub_ret); |
| /* subscribe pass */ |
subs[i].sub_ret = MQTT_QOS_PASS; |
subs[i].sub_ret = MQTT_QOS_PASS; |
} |
|
|
|
call.WritePUB_subscribe(&cfg, pub, sess->sess_cid, mid, buf, | call.LOG(logg, "Added [%s] SUBSCRIBE '%s'(%d) QoS=%d from %s\n", sess->sess_cid, |
sess->sess_user, sess->sess_addr, subs[i].sub_ret); | store->st_subscr.sub_topic.msg_base, |
| store->st_subscr.sub_topic.msg_len, |
| store->st_subscr.sub_ret, sess->sess_addr); |
| } |
} |
} |
|
|
/* send acknowledge */ |
/* send acknowledge */ |
Line 463 cmdCONNECT(void *srv, int len, void *arg)
|
Line 466 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); |
|
|
|
schedCancelby(root, taskTIMER, CRITERIA_CALL, sendRetain, NULL); |
|
|
if (sess->sess_clean) { |
if (sess->sess_clean) { |
if (call.FiniSessPUB) |
if (call.FiniSessPUB) |
call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%"); |
call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%"); |
Line 484 cmdCONNECT(void *srv, int len, void *arg)
|
Line 489 cmdCONNECT(void *srv, int len, void *arg)
|
} |
} |
|
|
if (sess->sess_will.flag) |
if (sess->sess_will.flag) |
srv_Will(sess); | pubWill(sess); |
|
|
if (sess->sess_will.msg) |
if (sess->sess_will.msg) |
free(sess->sess_will.msg); |
free(sess->sess_will.msg); |