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