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