Annotation of mqtt/src/daemon.c, revision 1.2.2.45
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 misho 225:
226: ioTRACE(4);
227:
228: assert(task);
229:
230: if (!TASK_DATA(task)) {
1.2.2.31 misho 231: v = TASK_ARG(task);
1.2.2.8 misho 232: /* flow from accept new clients */
1.2.2.31 misho 233: sess = initSession(TASK_FD(task), v);
234: io_freeVar(&v);
1.2 misho 235: if (!sess) {
236: close(TASK_FD(task));
237: return NULL;
238: }
239:
240: /* receive & decode packet */
241: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
242: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.2.2.9 misho 243: finiSession(sess);
1.2 misho 244: return NULL;
245: }
246: } else {
247: sess = TASK_DATA(task);
1.2.2.23 misho 248: assert(sess);
249:
1.2 misho 250: buf.msg_len = TASK_DATLEN(task) > sizeof basebuf ? sizeof basebuf : TASK_DATLEN(task);
251: memcpy(buf.msg_base, sess->sess_buf->msg_base, buf.msg_len);
252: }
253:
254: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
255: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
256: &sess->sess_will.topic, &sess->sess_will.msg);
257: ret = cack.retcode;
258: flg.flags = cack.reserved;
259: if (flg.reserved) {
260: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
261: goto end;
262: } else {
263: sess->sess_clean = flg.clean_sess;
264: sess->sess_will.qos = flg.will_qos;
265: sess->sess_will.retain = flg.will_retain;
266: sess->sess_will.flag = flg.will_flg;
1.2.2.28 misho 267:
268: sess->sess_srv->timeout = sess->sess_ka;
1.2 misho 269: }
270:
271: /* check online table for user */
272: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
273: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
274: sess->sess_user, sess->sess_pass);
275: ret = MQTT_RETCODE_DENIED;
276: goto end;
277: } else {
278: ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
279: ret = MQTT_RETCODE_ACCEPTED;
280: }
1.2.2.12 misho 281:
1.2.2.30 misho 282: /* db management */
1.2 misho 283: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
284: ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
285: TAILQ_FOREACH(s, &Sessions, sess_node)
286: if (!strcmp(s->sess_cid, sess->sess_cid)) {
287: /* found stale session & disconnect it! */
1.2.2.36 misho 288: schedCancelby(root, taskMAX, CRITERIA_FD, (void*) s->sess_sock, NULL);
289: schedWrite(root, leaveClient, s, s->sess_sock, NULL, 0);
1.2 misho 290: break;
291: }
292: }
293: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
294: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
295: sess->sess_will.qos, sess->sess_will.retain) == -1) {
296: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
297: ret = MQTT_RETCODE_DENIED;
298: goto end;
299: } else {
300: ioDEBUG(0, "Session %s from %s and username %s is started",
301: sess->sess_cid, sess->sess_addr, sess->sess_user);
302: ret = MQTT_RETCODE_ACCEPTED;
303: }
1.2.2.30 misho 304: /* clean/load session if requested */
305: if (sess->sess_clean) {
306: if (call.DeletePUB_subscribe)
307: call.DeletePUB_subscribe(&cfg, pub, sess->sess_cid, "%", sess->sess_user, "%");
1.2.2.45! misho 308: if (call.WipePUB_topic) /* delete ALL messages */
1.2.2.30 misho 309: call.WipePUB_topic(&cfg, pub, sess->sess_cid, sess->sess_user, -1);
1.2.2.41 misho 310: } else if (call.ReadPUB_subscribe) {
1.2.2.44 misho 311: /* load subscribes */
1.2.2.41 misho 312: subs = call.ReadPUB_subscribe(&cfg, pub, sess->sess_cid, "%");
313: loadSubscribes(sess, subs);
314: mqtt_subFree(&subs);
1.2.2.30 misho 315: }
1.2 misho 316:
1.2.2.44 misho 317: /* timer event for retain messages */
318: if (call.ReadPUB_topic)
319: schedTimer(root, sendRetain, sess, ts, NULL, 0);
320:
1.2.2.25 misho 321: /* Start session task OK ... */
322: if (!schedRead(root, dispatchSession, sess, TASK_FD(task), NULL, 0)) {
1.2.2.23 misho 323: ioLIBERR(sched);
1.2.2.25 misho 324: ret = MQTT_RETCODE_DENIED;
1.2.2.26 misho 325: } else
326: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.2.2.23 misho 327:
1.2 misho 328: call.LOG(logg, "Session %s started from %s for user %s (timeout=%d) OK!\n", sess->sess_cid,
329: sess->sess_addr, sess->sess_user, sess->sess_ka);
330: end: /* close client connection */
1.2.2.23 misho 331: wlen = mqtt_msgCONNACK(sess->sess_buf, ret);
332: if ((wlen = send(TASK_FD(task), sess->sess_buf->msg_base, wlen, 0)) == -1) {
1.2.2.34 misho 333: ioDEBUG(3, "Error:: send(%ld) #%d - %s", (long) TASK_FD(task), errno, strerror(errno));
1.2.2.23 misho 334: } else
335: ioDEBUG(5, "Sended %d bytes", wlen);
1.2 misho 336:
1.2.2.23 misho 337: if (ret != MQTT_RETCODE_ACCEPTED) {
1.2.2.34 misho 338: ioDEBUG(1, "Close client %s with socket=%ld", sess->sess_addr, (long) TASK_FD(task));
1.2.2.23 misho 339: finiSession(sess);
340: }
1.2 misho 341: return NULL;
342: }
343:
344: static void *
1.2.2.9 misho 345: acceptClient(sched_task_t *task)
1.2 misho 346: {
1.2.2.9 misho 347: int cli;
348: io_sockaddr_t sa;
349: socklen_t sslen = sizeof sa.ss;
350: ait_val_t *v;
351: char str[STRSIZ];
1.2 misho 352:
1.2.2.9 misho 353: ioTRACE(4);
1.2 misho 354:
1.2.2.9 misho 355: assert(task);
1.2 misho 356:
1.2.2.9 misho 357: if ((cli = accept(TASK_FD(task), &sa.sa, &sslen)) == -1)
358: goto end;
359:
360: v = io_allocVar();
361: if (!v) {
362: ioLIBERR(io);
363: close(cli);
364: goto end;
365: } else {
366: memset(str, 0, sizeof str);
367: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
1.2.2.17 misho 368: AIT_FREE_VAL(v);
1.2.2.9 misho 369: AIT_SET_STR(v, str);
370: }
371: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
372:
373: if (!schedRead(root, startSession, v, cli, NULL, 0)) {
1.2.2.31 misho 374: io_freeVar(&v);
1.2.2.9 misho 375: close(cli);
376: ioDEBUG(1, "Terminated client with socket=%d", cli);
377: }
378: end:
1.2.2.22 misho 379: if (!schedRead(root, acceptClient, NULL, TASK_FD(task), NULL, 0))
1.2.2.11 misho 380: ioLIBERR(sched);
1.2.2.9 misho 381: return NULL;
1.2 misho 382: }
383:
1.2.2.9 misho 384: /* ----------------------------------------------------------------------- */
385:
1.2 misho 386: int
387: Run(int sock)
388: {
1.2.2.25 misho 389: struct tagSession *sess;
1.2.2.9 misho 390: struct timespec pl = { 0, 100000000 };
1.2 misho 391:
392: ioTRACE(1);
393:
1.2.2.40 misho 394: if (mqtt_srv_Listen(sock, 0, 1) == -1) {
395: ioLIBERR(mqtt);
1.2 misho 396: return -1;
1.2.2.40 misho 397: }
1.2 misho 398:
1.2.2.9 misho 399: /* state machine - accept new connections */
400: if (!schedRead(root, acceptClient, NULL, sock, NULL, 0)) {
401: ioLIBERR(sched);
1.2 misho 402: return -1;
403: }
404:
1.2.2.9 misho 405: schedPolling(root, &pl, NULL);
406: schedRun(root, &Kill);
1.2 misho 407:
1.2.2.9 misho 408: /* free all undeleted elements into lists */
1.2.2.25 misho 409: TAILQ_FOREACH(sess, &Sessions, sess_node) {
410: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.26 misho 411:
1.2.2.25 misho 412: finiSession(sess);
413: }
1.2 misho 414: return 0;
415: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>