Annotation of mqtt/src/daemon.c, revision 1.2.2.47
1.2 misho 1: #include "global.h"
2: #include "rtlm.h"
3: #include "mqttd.h"
4: #include "mqttd_calls.h"
1.2.2.42 misho 5: #include "utils.h"
1.2 misho 6:
7:
8: static inline struct tagSession *
9: initSession(int sock, ait_val_t * __restrict v)
10: {
11: struct tagSession *sess = NULL;
12: const char *str;
13:
14: ioTRACE(5);
15:
16: if (!v)
17: return NULL;
18:
1.2.2.33 misho 19: sess = io_malloc(sizeof(struct tagSession));
1.2 misho 20: if (!sess) {
1.2.2.9 misho 21: ioSYSERR(0);
1.2 misho 22: return NULL;
23: } else
24: memset(sess, 0, sizeof(struct tagSession));
25:
1.2.2.9 misho 26: SLIST_INIT(&sess->sess_subscr);
1.2 misho 27:
1.2.2.5 misho 28: str = cfg_getAttribute(&cfg, "mqttd", "retry");
1.2 misho 29: if (!str)
30: sess->sess_retry = DEFAULT_RETRY;
31: else
32: sess->sess_retry = strtol(str, NULL, 0);
33:
34: sess->sess_buf = mqtt_msgAlloc(USHRT_MAX);
35: if (!sess->sess_buf) {
1.2.2.9 misho 36: ioLIBERR(mqtt);
1.2.2.33 misho 37: io_free(sess);
1.2 misho 38: return NULL;
39: }
40:
1.2.2.8 misho 41: /* init server actor */
1.2.2.40 misho 42: sess->sess_srv = mqtt_srv_cliInit(sock, sess->sess_buf, sess->sess_ka, 1);
1.2.2.2 misho 43: if (!sess->sess_srv) {
44: ioDEBUG(3, "Error:: in srv_Init #%d - %s", mqtt_GetErrno(), mqtt_GetError());
45: mqtt_msgFree(&sess->sess_buf, 42);
1.2.2.33 misho 46: io_free(sess);
1.2.2.2 misho 47: return NULL;
1.2.2.3 misho 48: } else {
49: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_CONNECT, cmdCONNECT);
50: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PUBLISH, cmdPUBLISH);
51: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PUBREL, cmdPUBREL);
52: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_SUBSCRIBE, cmdSUBSCRIBE);
53: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_UNSUBSCRIBE, cmdUNSUBSCRIBE);
54: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PINGREQ, cmdPINGREQ);
55: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_DISCONNECT, cmdDISCONNECT);
1.2.2.2 misho 56: }
57:
1.2 misho 58: sess->sess_sock = sock;
59: strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr);
60: return sess;
61: }
62:
1.2.2.25 misho 63: static void
1.2.2.9 misho 64: finiSession(struct tagSession *sess)
1.2 misho 65: {
66: struct tagStore *store;
67:
68: ioTRACE(5);
69:
70: if (!sess)
71: return;
72:
1.2.2.44 misho 73: schedCancelby(root, taskTIMER, CRITERIA_CALL, sendRetain, NULL);
74:
1.2.2.29 misho 75: if (sess->sess_clean) {
76: if (call.FiniSessPUB)
77: call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%");
78: if (call.DeletePUB_subscribe)
79: call.DeletePUB_subscribe(&cfg, pub, sess->sess_cid, "%", sess->sess_user, "%");
1.2.2.45 misho 80: if (call.WipePUB_topic) /* only dynamic messages */
81: call.WipePUB_topic(&cfg, pub, sess->sess_cid, sess->sess_user, 0);
1.2.2.29 misho 82: }
1.2 misho 83:
1.2.2.9 misho 84: while ((store = SLIST_FIRST(&sess->sess_subscr))) {
85: SLIST_REMOVE_HEAD(&sess->sess_subscr, st_node);
86:
87: if (store->st_subscr.sub_topic.msg_base)
88: free(store->st_subscr.sub_topic.msg_base);
89: if (store->st_subscr.sub_value.msg_base)
90: free(store->st_subscr.sub_value.msg_base);
91:
1.2.2.33 misho 92: io_free(store);
1.2.2.9 misho 93: }
1.2 misho 94:
1.2.2.42 misho 95: if (sess->sess_will.flag)
1.2.2.43 misho 96: pubWill(sess);
1.2.2.42 misho 97:
1.2 misho 98: if (sess->sess_will.topic)
99: free(sess->sess_will.topic);
1.2.2.42 misho 100: if (sess->sess_will.msg)
101: free(sess->sess_will.msg);
1.2 misho 102:
1.2.2.9 misho 103: if (sess->sess_sock > STDERR_FILENO)
1.2 misho 104: srv_Close(sess->sess_sock);
105:
1.2.2.40 misho 106: mqtt_srv_cliFini(&sess->sess_srv);
1.2 misho 107: mqtt_msgFree(&sess->sess_buf, 42);
108:
1.2.2.33 misho 109: io_free(sess);
1.2 misho 110: }
111:
1.2.2.23 misho 112: static void *
113: leaveClient(sched_task_t *task)
1.2 misho 114: {
1.2.2.23 misho 115: struct tagSession *sess;
1.2 misho 116: int ret;
117:
118: ioTRACE(4);
119:
1.2.2.23 misho 120: assert(task);
121:
122: sess = TASK_ARG(task);
1.2 misho 123: assert(sess);
124:
125: TAILQ_REMOVE(&Sessions, sess, sess_node);
126:
1.2.2.23 misho 127: ret = mqtt_msgDISCONNECT(sess->sess_buf);
128: send(TASK_FD(task), sess->sess_buf->msg_base, ret, MSG_NOSIGNAL);
1.2 misho 129:
1.2.2.34 misho 130: ioDEBUG(1, "Close socket=%ld", (long) TASK_FD(task));
1.2 misho 131: call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid,
132: sess->sess_addr, sess->sess_user);
1.2.2.20 misho 133:
134: finiSession(sess);
1.2.2.23 misho 135: return NULL;
1.2 misho 136: }
137:
138: static void *
1.2.2.23 misho 139: dispatchSession(sched_task_t *task)
1.2 misho 140: {
1.2.2.24 misho 141: int ret, len = 0;
1.2.2.23 misho 142: struct tagSession *sess;
1.2 misho 143:
144: ioTRACE(2);
145:
1.2.2.23 misho 146: assert(task);
1.2 misho 147:
1.2.2.23 misho 148: sess = TASK_ARG(task);
149: assert(sess);
150:
151: /* receive & decode packet */
152: if ((ret = recv(TASK_FD(task), sess->sess_buf->msg_base, sess->sess_buf->msg_len, 0)) == -1) {
153: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.2.2.26 misho 154: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.23 misho 155: finiSession(sess);
156: return NULL;
157: } else if (!ret) {
158: ioDEBUG(4, "Session %s EOF received.", sess->sess_cid);
1.2.2.26 misho 159: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.23 misho 160: finiSession(sess);
161: return NULL;
1.2.2.24 misho 162: }
1.2.2.23 misho 163:
1.2.2.24 misho 164: do {
165: /* dispatch message type */
1.2.2.40 misho 166: if ((len = mqtt_srv_cliDispatch(sess->sess_srv, ret, sess)) < 0) {
1.2.2.27 misho 167: if (len == -1) {
1.2.2.25 misho 168: ioLIBERR(mqtt);
1.2.2.27 misho 169: finiSession(sess);
170: } else if (len == -2) {
171: TAILQ_REMOVE(&Sessions, sess, sess_node);
172: finiSession(sess);
173: } else if (len == -3)
174: schedEvent(root, startSession, NULL, (u_long) TASK_FD(task), sess, ret);
1.2.2.24 misho 175: } else
176: ret -= len;
1.2.2.25 misho 177: } while (len > 0 && ret > 0);
1.2.2.23 misho 178:
1.2.2.25 misho 179: if (len >= 0 && !schedRead(root, dispatchSession, TASK_ARG(task), TASK_FD(task), NULL, 0)) {
1.2.2.23 misho 180: ioLIBERR(sched);
1.2.2.26 misho 181: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.25 misho 182: finiSession(sess);
183: }
1.2.2.11 misho 184: return NULL;
1.2 misho 185: }
186:
1.2.2.41 misho 187: static int
188: loadSubscribes(struct tagSession * __restrict sess, mqtt_subscr_t * __restrict subs)
189: {
190: register int i;
191: struct tagStore *store;
192:
193: if (!subs)
194: return -1;
195:
196: for (i = 0; subs[i].sub_topic.msg_base; i++) {
197: store = io_malloc(sizeof(struct tagStore));
198: if (!store) {
199: ioSYSERR(0);
200: continue;
201: } else {
202: store->st_msgid = 0;
203: mqtt_subCopy(&store->st_subscr, &subs[i]);
204: }
205:
206: /* add to cache */
207: SLIST_INSERT_HEAD(&sess->sess_subscr, store, st_node);
208: }
209:
210: return 0;
211: }
212:
1.2.2.24 misho 213: void *
1.2 misho 214: startSession(sched_task_t *task)
215: {
216: u_char basebuf[USHRT_MAX];
1.2.2.23 misho 217: mqtt_msg_t buf = { basebuf, sizeof basebuf };
1.2 misho 218: mqtthdr_connflgs_t flg;
219: mqtthdr_connack_t cack;
1.2.2.31 misho 220: ait_val_t *v;
1.2 misho 221: struct tagSession *s, *sess = NULL;
1.2.2.23 misho 222: int ret, wlen;
1.2.2.41 misho 223: mqtt_subscr_t *subs;
1.2.2.44 misho 224: struct timespec ts = { RETAIN_TIMEOUT, 0 };
1.2.2.47! misho 225: intptr_t sock;
1.2 misho 226:
227: ioTRACE(4);
228:
229: assert(task);
230:
231: if (!TASK_DATA(task)) {
1.2.2.31 misho 232: v = TASK_ARG(task);
1.2.2.8 misho 233: /* flow from accept new clients */
1.2.2.31 misho 234: sess = initSession(TASK_FD(task), v);
235: io_freeVar(&v);
1.2 misho 236: if (!sess) {
237: close(TASK_FD(task));
238: return NULL;
239: }
240:
241: /* receive & decode packet */
242: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
243: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.2.2.9 misho 244: finiSession(sess);
1.2 misho 245: return NULL;
246: }
247: } else {
248: sess = TASK_DATA(task);
1.2.2.23 misho 249: assert(sess);
250:
1.2 misho 251: buf.msg_len = TASK_DATLEN(task) > sizeof basebuf ? sizeof basebuf : TASK_DATLEN(task);
252: memcpy(buf.msg_base, sess->sess_buf->msg_base, buf.msg_len);
253: }
254:
255: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
256: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
257: &sess->sess_will.topic, &sess->sess_will.msg);
258: ret = cack.retcode;
259: flg.flags = cack.reserved;
260: if (flg.reserved) {
261: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
262: goto end;
263: } else {
264: sess->sess_clean = flg.clean_sess;
265: sess->sess_will.qos = flg.will_qos;
266: sess->sess_will.retain = flg.will_retain;
267: sess->sess_will.flag = flg.will_flg;
1.2.2.28 misho 268:
269: sess->sess_srv->timeout = sess->sess_ka;
1.2 misho 270: }
271:
272: /* check online table for user */
273: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
274: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
275: sess->sess_user, sess->sess_pass);
276: ret = MQTT_RETCODE_DENIED;
277: goto end;
278: } else {
279: ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
280: ret = MQTT_RETCODE_ACCEPTED;
281: }
1.2.2.12 misho 282:
1.2.2.30 misho 283: /* db management */
1.2 misho 284: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
285: ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
286: TAILQ_FOREACH(s, &Sessions, sess_node)
287: if (!strcmp(s->sess_cid, sess->sess_cid)) {
288: /* found stale session & disconnect it! */
1.2.2.47! misho 289: sock = s->sess_sock;
! 290: schedCancelby(root, taskMAX, CRITERIA_FD, (void*) sock, NULL);
1.2.2.36 misho 291: schedWrite(root, leaveClient, s, s->sess_sock, NULL, 0);
1.2 misho 292: break;
293: }
294: }
295: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
296: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
297: sess->sess_will.qos, sess->sess_will.retain) == -1) {
298: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
299: ret = MQTT_RETCODE_DENIED;
300: goto end;
301: } else {
302: ioDEBUG(0, "Session %s from %s and username %s is started",
303: sess->sess_cid, sess->sess_addr, sess->sess_user);
304: ret = MQTT_RETCODE_ACCEPTED;
305: }
1.2.2.30 misho 306: /* clean/load session if requested */
307: if (sess->sess_clean) {
308: if (call.DeletePUB_subscribe)
309: call.DeletePUB_subscribe(&cfg, pub, sess->sess_cid, "%", sess->sess_user, "%");
1.2.2.45 misho 310: if (call.WipePUB_topic) /* delete ALL messages */
1.2.2.30 misho 311: call.WipePUB_topic(&cfg, pub, sess->sess_cid, sess->sess_user, -1);
1.2.2.41 misho 312: } else if (call.ReadPUB_subscribe) {
1.2.2.44 misho 313: /* load subscribes */
1.2.2.41 misho 314: subs = call.ReadPUB_subscribe(&cfg, pub, sess->sess_cid, "%");
315: loadSubscribes(sess, subs);
316: mqtt_subFree(&subs);
1.2.2.30 misho 317: }
1.2 misho 318:
1.2.2.44 misho 319: /* timer event for retain messages */
320: if (call.ReadPUB_topic)
321: schedTimer(root, sendRetain, sess, ts, NULL, 0);
322:
1.2.2.25 misho 323: /* Start session task OK ... */
324: if (!schedRead(root, dispatchSession, sess, TASK_FD(task), NULL, 0)) {
1.2.2.23 misho 325: ioLIBERR(sched);
1.2.2.25 misho 326: ret = MQTT_RETCODE_DENIED;
1.2.2.26 misho 327: } else
328: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.2.2.23 misho 329:
1.2 misho 330: call.LOG(logg, "Session %s started from %s for user %s (timeout=%d) OK!\n", sess->sess_cid,
331: sess->sess_addr, sess->sess_user, sess->sess_ka);
332: end: /* close client connection */
1.2.2.23 misho 333: wlen = mqtt_msgCONNACK(sess->sess_buf, ret);
334: if ((wlen = send(TASK_FD(task), sess->sess_buf->msg_base, wlen, 0)) == -1) {
1.2.2.34 misho 335: ioDEBUG(3, "Error:: send(%ld) #%d - %s", (long) TASK_FD(task), errno, strerror(errno));
1.2.2.23 misho 336: } else
337: ioDEBUG(5, "Sended %d bytes", wlen);
1.2 misho 338:
1.2.2.23 misho 339: if (ret != MQTT_RETCODE_ACCEPTED) {
1.2.2.34 misho 340: ioDEBUG(1, "Close client %s with socket=%ld", sess->sess_addr, (long) TASK_FD(task));
1.2.2.23 misho 341: finiSession(sess);
342: }
1.2 misho 343: return NULL;
344: }
345:
346: static void *
1.2.2.9 misho 347: acceptClient(sched_task_t *task)
1.2 misho 348: {
1.2.2.9 misho 349: int cli;
350: io_sockaddr_t sa;
351: socklen_t sslen = sizeof sa.ss;
352: ait_val_t *v;
353: char str[STRSIZ];
1.2 misho 354:
1.2.2.9 misho 355: ioTRACE(4);
1.2 misho 356:
1.2.2.9 misho 357: assert(task);
1.2 misho 358:
1.2.2.9 misho 359: if ((cli = accept(TASK_FD(task), &sa.sa, &sslen)) == -1)
360: goto end;
361:
362: v = io_allocVar();
363: if (!v) {
364: ioLIBERR(io);
365: close(cli);
366: goto end;
367: } else {
368: memset(str, 0, sizeof str);
369: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
1.2.2.17 misho 370: AIT_FREE_VAL(v);
1.2.2.9 misho 371: AIT_SET_STR(v, str);
372: }
373: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
374:
375: if (!schedRead(root, startSession, v, cli, NULL, 0)) {
1.2.2.31 misho 376: io_freeVar(&v);
1.2.2.9 misho 377: close(cli);
378: ioDEBUG(1, "Terminated client with socket=%d", cli);
379: }
380: end:
1.2.2.22 misho 381: if (!schedRead(root, acceptClient, NULL, TASK_FD(task), NULL, 0))
1.2.2.11 misho 382: ioLIBERR(sched);
1.2.2.9 misho 383: return NULL;
1.2 misho 384: }
385:
1.2.2.9 misho 386: /* ----------------------------------------------------------------------- */
387:
1.2 misho 388: int
389: Run(int sock)
390: {
1.2.2.25 misho 391: struct tagSession *sess;
1.2.2.9 misho 392: struct timespec pl = { 0, 100000000 };
1.2 misho 393:
394: ioTRACE(1);
395:
1.2.2.40 misho 396: if (mqtt_srv_Listen(sock, 0, 1) == -1) {
397: ioLIBERR(mqtt);
1.2 misho 398: return -1;
1.2.2.40 misho 399: }
1.2 misho 400:
1.2.2.9 misho 401: /* state machine - accept new connections */
402: if (!schedRead(root, acceptClient, NULL, sock, NULL, 0)) {
403: ioLIBERR(sched);
1.2 misho 404: return -1;
405: }
406:
1.2.2.9 misho 407: schedPolling(root, &pl, NULL);
408: schedRun(root, &Kill);
1.2 misho 409:
1.2.2.46 misho 410: schedCancelby(root, taskTIMER, CRITERIA_CALL, sendRetain, NULL);
411:
1.2.2.9 misho 412: /* free all undeleted elements into lists */
1.2.2.25 misho 413: TAILQ_FOREACH(sess, &Sessions, sess_node) {
414: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.26 misho 415:
1.2.2.25 misho 416: finiSession(sess);
417: }
1.2 misho 418: return 0;
419: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>