Annotation of mqtt/src/daemon.c, revision 1.2.2.35
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.23 misho 264: schedWrite(root, leaveClient, sess, TASK_FD(task), NULL, 0);
1.2 misho 265: break;
266: }
267: }
268: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
269: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
270: sess->sess_will.qos, sess->sess_will.retain) == -1) {
271: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
272: ret = MQTT_RETCODE_DENIED;
273: goto end;
274: } else {
275: ioDEBUG(0, "Session %s from %s and username %s is started",
276: sess->sess_cid, sess->sess_addr, sess->sess_user);
277: ret = MQTT_RETCODE_ACCEPTED;
278: }
1.2.2.30 misho 279: /* clean/load session if requested */
280: if (sess->sess_clean) {
281: if (call.DeletePUB_subscribe)
282: call.DeletePUB_subscribe(&cfg, pub, sess->sess_cid, "%", sess->sess_user, "%");
283: if (call.WipePUB_topic)
284: call.WipePUB_topic(&cfg, pub, sess->sess_cid, sess->sess_user, -1);
285: } else {
286: // todo: read_sql subs and prepare publish
287: }
1.2 misho 288:
1.2.2.25 misho 289: /* Start session task OK ... */
290: if (!schedRead(root, dispatchSession, sess, TASK_FD(task), NULL, 0)) {
1.2.2.23 misho 291: ioLIBERR(sched);
1.2.2.25 misho 292: ret = MQTT_RETCODE_DENIED;
1.2.2.26 misho 293: } else
294: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.2.2.23 misho 295:
1.2 misho 296: call.LOG(logg, "Session %s started from %s for user %s (timeout=%d) OK!\n", sess->sess_cid,
297: sess->sess_addr, sess->sess_user, sess->sess_ka);
298: end: /* close client connection */
1.2.2.23 misho 299: wlen = mqtt_msgCONNACK(sess->sess_buf, ret);
300: if ((wlen = send(TASK_FD(task), sess->sess_buf->msg_base, wlen, 0)) == -1) {
1.2.2.34 misho 301: ioDEBUG(3, "Error:: send(%ld) #%d - %s", (long) TASK_FD(task), errno, strerror(errno));
1.2.2.23 misho 302: } else
303: ioDEBUG(5, "Sended %d bytes", wlen);
1.2 misho 304:
1.2.2.23 misho 305: if (ret != MQTT_RETCODE_ACCEPTED) {
1.2.2.34 misho 306: ioDEBUG(1, "Close client %s with socket=%ld", sess->sess_addr, (long) TASK_FD(task));
1.2.2.23 misho 307: finiSession(sess);
308: }
1.2 misho 309: return NULL;
310: }
311:
312: static void *
1.2.2.9 misho 313: acceptClient(sched_task_t *task)
1.2 misho 314: {
1.2.2.9 misho 315: int cli;
316: io_sockaddr_t sa;
317: socklen_t sslen = sizeof sa.ss;
318: ait_val_t *v;
319: char str[STRSIZ];
1.2 misho 320:
1.2.2.9 misho 321: ioTRACE(4);
1.2 misho 322:
1.2.2.9 misho 323: assert(task);
1.2 misho 324:
1.2.2.9 misho 325: if ((cli = accept(TASK_FD(task), &sa.sa, &sslen)) == -1)
326: goto end;
327: else
1.2.2.21 misho 328: fcntl(cli, F_SETFL, fcntl(cli, F_GETFL, 0) | O_NONBLOCK);
1.2.2.9 misho 329:
330: v = io_allocVar();
331: if (!v) {
332: ioLIBERR(io);
333: close(cli);
334: goto end;
335: } else {
336: memset(str, 0, sizeof str);
337: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
1.2.2.17 misho 338: AIT_FREE_VAL(v);
1.2.2.9 misho 339: AIT_SET_STR(v, str);
340: }
341: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
342:
343: if (!schedRead(root, startSession, v, cli, NULL, 0)) {
1.2.2.31 misho 344: io_freeVar(&v);
1.2.2.9 misho 345: close(cli);
346: ioDEBUG(1, "Terminated client with socket=%d", cli);
347: }
348: end:
1.2.2.22 misho 349: if (!schedRead(root, acceptClient, NULL, TASK_FD(task), NULL, 0))
1.2.2.11 misho 350: ioLIBERR(sched);
1.2.2.9 misho 351: return NULL;
1.2 misho 352: }
353:
1.2.2.9 misho 354: /* ----------------------------------------------------------------------- */
355:
1.2 misho 356: int
357: Run(int sock)
358: {
1.2.2.25 misho 359: struct tagSession *sess;
1.2.2.9 misho 360: struct timespec pl = { 0, 100000000 };
1.2 misho 361:
362: ioTRACE(1);
363:
1.2.2.9 misho 364: if (listen(sock, SOMAXCONN) == -1) {
1.2 misho 365: ioSYSERR(0);
366: return -1;
367: } else
1.2.2.9 misho 368: fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
1.2 misho 369:
1.2.2.9 misho 370: /* state machine - accept new connections */
371: if (!schedRead(root, acceptClient, NULL, sock, NULL, 0)) {
372: ioLIBERR(sched);
1.2 misho 373: return -1;
374: }
375:
1.2.2.9 misho 376: schedPolling(root, &pl, NULL);
377: schedRun(root, &Kill);
1.2 misho 378:
1.2.2.9 misho 379: /* free all undeleted elements into lists */
1.2.2.25 misho 380: TAILQ_FOREACH(sess, &Sessions, sess_node) {
381: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.26 misho 382:
1.2.2.25 misho 383: finiSession(sess);
384: }
1.2 misho 385: return 0;
386: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>