Annotation of mqtt/src/daemon.c, revision 1.5.2.2
1.4 misho 1: /*************************************************************************
2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.5.2.2 ! misho 6: * $Id: daemon.c,v 1.5.2.1 2013/01/18 10:17:22 misho Exp $
1.4 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.5.2.1 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
1.4 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
1.2 misho 46: #include "global.h"
47: #include "rtlm.h"
48: #include "mqttd.h"
49: #include "mqttd_calls.h"
1.3 misho 50: #include "utils.h"
1.2 misho 51:
52:
53: static inline struct tagSession *
54: initSession(int sock, ait_val_t * __restrict v)
55: {
56: struct tagSession *sess = NULL;
57: const char *str;
58:
1.5.2.2 ! misho 59: ETRACE();
1.2 misho 60:
61: if (!v)
62: return NULL;
63:
1.5.2.1 misho 64: sess = e_malloc(sizeof(struct tagSession));
1.2 misho 65: if (!sess) {
1.5.2.1 misho 66: ELIBERR(elwix);
1.2 misho 67: return NULL;
68: } else
69: memset(sess, 0, sizeof(struct tagSession));
70:
1.3 misho 71: SLIST_INIT(&sess->sess_subscr);
1.2 misho 72:
1.3 misho 73: str = cfg_getAttribute(&cfg, "mqttd", "retry");
1.2 misho 74: if (!str)
75: sess->sess_retry = DEFAULT_RETRY;
76: else
77: sess->sess_retry = strtol(str, NULL, 0);
78:
79: sess->sess_buf = mqtt_msgAlloc(USHRT_MAX);
80: if (!sess->sess_buf) {
1.5.2.1 misho 81: ELIBERR(mqtt);
82: e_free(sess);
1.2 misho 83: return NULL;
84: }
85:
1.3 misho 86: /* init server actor */
87: sess->sess_srv = mqtt_srv_cliInit(sock, sess->sess_buf, sess->sess_ka, 1);
88: if (!sess->sess_srv) {
1.5.2.1 misho 89: EVERBOSE(3, "Error:: in srv_Init #%d - %s", mqtt_GetErrno(), mqtt_GetError());
1.3 misho 90: mqtt_msgFree(&sess->sess_buf, 42);
1.5.2.1 misho 91: e_free(sess);
1.3 misho 92: return NULL;
93: } else {
94: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_CONNECT, cmdCONNECT);
95: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PUBLISH, cmdPUBLISH);
96: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PUBREL, cmdPUBREL);
97: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_SUBSCRIBE, cmdSUBSCRIBE);
98: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_UNSUBSCRIBE, cmdUNSUBSCRIBE);
99: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PINGREQ, cmdPINGREQ);
100: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_DISCONNECT, cmdDISCONNECT);
101: }
102:
1.2 misho 103: sess->sess_sock = sock;
104: strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr);
105: return sess;
106: }
107:
108: static void
1.3 misho 109: finiSession(struct tagSession *sess)
1.2 misho 110: {
111: struct tagStore *store;
112:
1.5.2.2 ! misho 113: ETRACE();
1.2 misho 114:
115: if (!sess)
116: return;
117:
1.3 misho 118: schedCancelby(root, taskTIMER, CRITERIA_CALL, sendRetain, NULL);
1.2 misho 119:
1.3 misho 120: if (sess->sess_clean) {
121: if (call.FiniSessPUB)
122: call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%");
123: if (call.DeletePUB_subscribe)
124: call.DeletePUB_subscribe(&cfg, pub, sess->sess_cid, "%", sess->sess_user, "%");
125: if (call.WipePUB_topic) /* only dynamic messages */
126: call.WipePUB_topic(&cfg, pub, sess->sess_cid, sess->sess_user, 0);
127: }
128:
129: while ((store = SLIST_FIRST(&sess->sess_subscr))) {
130: SLIST_REMOVE_HEAD(&sess->sess_subscr, st_node);
1.2 misho 131:
1.3 misho 132: if (store->st_subscr.sub_topic.msg_base)
133: free(store->st_subscr.sub_topic.msg_base);
134: if (store->st_subscr.sub_value.msg_base)
135: free(store->st_subscr.sub_value.msg_base);
1.2 misho 136:
1.5.2.1 misho 137: e_free(store);
1.2 misho 138: }
139:
1.3 misho 140: if (sess->sess_will.flag)
141: pubWill(sess);
142:
143: if (sess->sess_will.topic)
144: free(sess->sess_will.topic);
1.2 misho 145: if (sess->sess_will.msg)
146: free(sess->sess_will.msg);
147:
1.3 misho 148: if (sess->sess_sock > STDERR_FILENO)
1.2 misho 149: srv_Close(sess->sess_sock);
150:
1.3 misho 151: mqtt_srv_cliFini(&sess->sess_srv);
1.2 misho 152: mqtt_msgFree(&sess->sess_buf, 42);
153:
1.5.2.1 misho 154: e_free(sess);
1.2 misho 155: }
156:
1.3 misho 157: static void *
158: leaveClient(sched_task_t *task)
1.2 misho 159: {
1.3 misho 160: struct tagSession *sess;
1.2 misho 161: int ret;
162:
1.5.2.2 ! misho 163: ETRACE();
1.2 misho 164:
1.3 misho 165: assert(task);
166:
167: sess = TASK_ARG(task);
1.2 misho 168: assert(sess);
169:
170: TAILQ_REMOVE(&Sessions, sess, sess_node);
171:
1.3 misho 172: ret = mqtt_msgDISCONNECT(sess->sess_buf);
173: send(TASK_FD(task), sess->sess_buf->msg_base, ret, MSG_NOSIGNAL);
1.2 misho 174:
1.5.2.1 misho 175: EVERBOSE(1, "Close socket=%ld", (long) TASK_FD(task));
1.2 misho 176: call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid,
177: sess->sess_addr, sess->sess_user);
1.3 misho 178:
179: finiSession(sess);
180: return NULL;
1.2 misho 181: }
182:
1.3 misho 183: static void *
184: dispatchSession(sched_task_t *task)
1.2 misho 185: {
1.3 misho 186: int ret, len = 0;
187: struct tagSession *sess;
188:
1.5.2.2 ! misho 189: ETRACE();
1.2 misho 190:
1.3 misho 191: assert(task);
1.2 misho 192:
1.3 misho 193: sess = TASK_ARG(task);
1.2 misho 194: assert(sess);
195:
196: /* receive & decode packet */
1.3 misho 197: if ((ret = recv(TASK_FD(task), sess->sess_buf->msg_base, sess->sess_buf->msg_len, 0)) == -1) {
1.5.2.1 misho 198: EVERBOSE(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.3 misho 199: TAILQ_REMOVE(&Sessions, sess, sess_node);
200: finiSession(sess);
201: return NULL;
202: } else if (!ret) {
1.5.2.1 misho 203: EVERBOSE(4, "Session %s EOF received.", sess->sess_cid);
1.3 misho 204: TAILQ_REMOVE(&Sessions, sess, sess_node);
205: finiSession(sess);
206: return NULL;
1.2 misho 207: }
1.3 misho 208:
209: do {
210: /* dispatch message type */
211: if ((len = mqtt_srv_cliDispatch(sess->sess_srv, ret, sess)) < 0) {
212: if (len == -1) {
1.5.2.1 misho 213: ELIBERR(mqtt);
1.3 misho 214: finiSession(sess);
215: } else if (len == -2) {
216: TAILQ_REMOVE(&Sessions, sess, sess_node);
217: finiSession(sess);
218: } else if (len == -3)
219: schedEvent(root, startSession, NULL, (u_long) TASK_FD(task), sess, ret);
220: } else
221: ret -= len;
222: } while (len > 0 && ret > 0);
223:
224: if (len >= 0 && !schedRead(root, dispatchSession, TASK_ARG(task), TASK_FD(task), NULL, 0)) {
1.5.2.1 misho 225: ELIBERR(sched);
1.3 misho 226: TAILQ_REMOVE(&Sessions, sess, sess_node);
227: finiSession(sess);
1.2 misho 228: }
1.3 misho 229: return NULL;
1.2 misho 230: }
231:
1.3 misho 232: static int
233: loadSubscribes(struct tagSession * __restrict sess, mqtt_subscr_t * __restrict subs)
1.2 misho 234: {
1.3 misho 235: register int i;
1.2 misho 236: struct tagStore *store;
237:
1.3 misho 238: if (!subs)
239: return -1;
1.2 misho 240:
1.3 misho 241: for (i = 0; subs[i].sub_topic.msg_base; i++) {
1.5.2.1 misho 242: store = e_malloc(sizeof(struct tagStore));
1.3 misho 243: if (!store) {
1.5.2.1 misho 244: ELIBERR(elwix);
1.3 misho 245: continue;
246: } else {
247: store->st_msgid = 0;
248: mqtt_subCopy(&store->st_subscr, &subs[i]);
1.2 misho 249: }
250:
1.3 misho 251: /* add to cache */
252: SLIST_INSERT_HEAD(&sess->sess_subscr, store, st_node);
1.2 misho 253: }
254:
1.3 misho 255: return 0;
1.2 misho 256: }
257:
1.3 misho 258: void *
1.2 misho 259: startSession(sched_task_t *task)
260: {
261: u_char basebuf[USHRT_MAX];
1.3 misho 262: mqtt_msg_t buf = { basebuf, sizeof basebuf };
1.2 misho 263: mqtthdr_connflgs_t flg;
264: mqtthdr_connack_t cack;
1.3 misho 265: ait_val_t *v;
1.2 misho 266: struct tagSession *s, *sess = NULL;
1.3 misho 267: int ret, wlen;
268: mqtt_subscr_t *subs;
269: struct timespec ts = { RETAIN_TIMEOUT, 0 };
270: intptr_t sock;
1.2 misho 271:
1.5.2.2 ! misho 272: ETRACE();
1.2 misho 273:
274: assert(task);
275:
276: if (!TASK_DATA(task)) {
1.3 misho 277: v = TASK_ARG(task);
278: /* flow from accept new clients */
279: sess = initSession(TASK_FD(task), v);
1.5.2.1 misho 280: ait_freeVar(&v);
1.2 misho 281: if (!sess) {
282: close(TASK_FD(task));
283: return NULL;
284: }
285:
286: /* receive & decode packet */
287: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
1.5.2.1 misho 288: EVERBOSE(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.3 misho 289: finiSession(sess);
1.2 misho 290: return NULL;
291: }
292: } else {
293: sess = TASK_DATA(task);
1.3 misho 294: assert(sess);
295:
1.2 misho 296: buf.msg_len = TASK_DATLEN(task) > sizeof basebuf ? sizeof basebuf : TASK_DATLEN(task);
297: memcpy(buf.msg_base, sess->sess_buf->msg_base, buf.msg_len);
298: }
299:
300: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
301: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
302: &sess->sess_will.topic, &sess->sess_will.msg);
303: ret = cack.retcode;
304: flg.flags = cack.reserved;
305: if (flg.reserved) {
1.5.2.1 misho 306: EVERBOSE(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
1.2 misho 307: goto end;
308: } else {
309: sess->sess_clean = flg.clean_sess;
310: sess->sess_will.qos = flg.will_qos;
311: sess->sess_will.retain = flg.will_retain;
312: sess->sess_will.flag = flg.will_flg;
1.3 misho 313:
314: sess->sess_srv->timeout = sess->sess_ka;
1.2 misho 315: }
316:
317: /* check online table for user */
318: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
1.5.2.1 misho 319: EVERBOSE(0, "Login:: DENIED for username %s and password %s",
1.2 misho 320: sess->sess_user, sess->sess_pass);
321: ret = MQTT_RETCODE_DENIED;
322: goto end;
323: } else {
1.5.2.1 misho 324: EVERBOSE(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
1.2 misho 325: ret = MQTT_RETCODE_ACCEPTED;
326: }
1.3 misho 327:
328: /* db management */
1.2 misho 329: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
1.5.2.1 misho 330: EVERBOSE(2, "Old session %s should be disconnect!", sess->sess_cid);
1.2 misho 331: TAILQ_FOREACH(s, &Sessions, sess_node)
332: if (!strcmp(s->sess_cid, sess->sess_cid)) {
333: /* found stale session & disconnect it! */
1.3 misho 334: sock = s->sess_sock;
335: schedCancelby(root, taskMAX, CRITERIA_FD, (void*) sock, NULL);
336: schedWrite(root, leaveClient, s, s->sess_sock, NULL, 0);
1.2 misho 337: break;
338: }
339: }
340: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
341: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
342: sess->sess_will.qos, sess->sess_will.retain) == -1) {
1.5.2.1 misho 343: EVERBOSE(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
1.2 misho 344: ret = MQTT_RETCODE_DENIED;
345: goto end;
346: } else {
1.5.2.1 misho 347: EVERBOSE(0, "Session %s from %s and username %s is started",
1.2 misho 348: sess->sess_cid, sess->sess_addr, sess->sess_user);
349: ret = MQTT_RETCODE_ACCEPTED;
350: }
1.3 misho 351: /* clean/load session if requested */
352: if (sess->sess_clean) {
353: if (call.DeletePUB_subscribe)
354: call.DeletePUB_subscribe(&cfg, pub, sess->sess_cid, "%", sess->sess_user, "%");
355: if (call.WipePUB_topic) /* delete ALL messages */
356: call.WipePUB_topic(&cfg, pub, sess->sess_cid, sess->sess_user, -1);
357: } else if (call.ReadPUB_subscribe) {
358: /* load subscribes */
359: subs = call.ReadPUB_subscribe(&cfg, pub, sess->sess_cid, "%");
360: loadSubscribes(sess, subs);
361: mqtt_subFree(&subs);
362: }
363:
364: /* timer event for retain messages */
365: if (call.ReadPUB_topic)
366: schedTimer(root, sendRetain, sess, ts, NULL, 0);
367:
368: /* Start session task OK ... */
369: if (!schedRead(root, dispatchSession, sess, TASK_FD(task), NULL, 0)) {
1.5.2.1 misho 370: ELIBERR(sched);
1.3 misho 371: ret = MQTT_RETCODE_DENIED;
372: } else
373: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.2 misho 374:
375: call.LOG(logg, "Session %s started from %s for user %s (timeout=%d) OK!\n", sess->sess_cid,
376: sess->sess_addr, sess->sess_user, sess->sess_ka);
1.3 misho 377: end: /* close client connection */
378: wlen = mqtt_msgCONNACK(sess->sess_buf, ret);
379: if ((wlen = send(TASK_FD(task), sess->sess_buf->msg_base, wlen, 0)) == -1) {
1.5.2.1 misho 380: EVERBOSE(3, "Error:: send(%ld) #%d - %s", (long) TASK_FD(task), errno, strerror(errno));
1.3 misho 381: } else
1.5.2.1 misho 382: EVERBOSE(5, "Sended %d bytes", wlen);
1.2 misho 383:
1.3 misho 384: if (ret != MQTT_RETCODE_ACCEPTED) {
1.5.2.1 misho 385: EVERBOSE(1, "Close client %s with socket=%ld", sess->sess_addr, (long) TASK_FD(task));
1.3 misho 386: finiSession(sess);
1.2 misho 387: }
388: return NULL;
389: }
390:
391: static void *
1.3 misho 392: acceptClient(sched_task_t *task)
1.2 misho 393: {
1.3 misho 394: int cli;
1.5.2.1 misho 395: sockaddr_t sa;
1.3 misho 396: socklen_t sslen = sizeof sa.ss;
397: ait_val_t *v;
398: char str[STRSIZ];
399:
1.5.2.2 ! misho 400: ETRACE();
1.2 misho 401:
1.3 misho 402: assert(task);
1.2 misho 403:
1.3 misho 404: if ((cli = accept(TASK_FD(task), &sa.sa, &sslen)) == -1)
405: goto end;
1.5 misho 406: else
407: fcntl(cli, F_SETFL, fcntl(cli, F_GETFL) | O_NONBLOCK);
1.2 misho 408:
1.5.2.1 misho 409: v = ait_allocVar();
1.3 misho 410: if (!v) {
1.5.2.1 misho 411: ELIBERR(elwix);
1.3 misho 412: close(cli);
413: goto end;
414: } else {
415: memset(str, 0, sizeof str);
1.5.2.1 misho 416: snprintf(str, sizeof str, "%s:%hu", e_n2addr(&sa, v), e_n2port(&sa));
1.3 misho 417: AIT_FREE_VAL(v);
418: AIT_SET_STR(v, str);
419: }
1.5.2.1 misho 420: EVERBOSE(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
1.3 misho 421:
422: if (!schedRead(root, startSession, v, cli, NULL, 0)) {
1.5.2.1 misho 423: ait_freeVar(&v);
1.3 misho 424: close(cli);
1.5.2.1 misho 425: EVERBOSE(1, "Terminated client with socket=%d", cli);
1.3 misho 426: }
427: end:
428: if (!schedRead(root, acceptClient, NULL, TASK_FD(task), NULL, 0))
1.5.2.1 misho 429: ELIBERR(sched);
1.3 misho 430: return NULL;
1.2 misho 431: }
432:
1.3 misho 433: /* ----------------------------------------------------------------------- */
434:
1.2 misho 435: int
436: Run(int sock)
437: {
1.3 misho 438: struct tagSession *sess;
439: struct timespec pl = { 0, 100000000 };
1.2 misho 440:
1.5.2.2 ! misho 441: ETRACE();
1.2 misho 442:
1.3 misho 443: if (mqtt_srv_Listen(sock, 0, 1) == -1) {
1.5.2.1 misho 444: ELIBERR(mqtt);
1.2 misho 445: return -1;
1.3 misho 446: }
1.2 misho 447:
1.3 misho 448: /* state machine - accept new connections */
449: if (!schedRead(root, acceptClient, NULL, sock, NULL, 0)) {
1.5.2.1 misho 450: ELIBERR(sched);
1.2 misho 451: return -1;
452: }
453:
1.3 misho 454: schedPolling(root, &pl, NULL);
455: schedRun(root, &Kill);
456:
457: schedCancelby(root, taskTIMER, CRITERIA_CALL, sendRetain, NULL);
458:
459: /* free all undeleted elements into lists */
460: TAILQ_FOREACH(sess, &Sessions, sess_node) {
461: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2 misho 462:
1.3 misho 463: finiSession(sess);
1.2 misho 464: }
465: return 0;
466: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>