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