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