Annotation of mqtt/src/daemon.c, revision 1.2.2.12
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 void *startSession(sched_task_t *task);
1.2.2.10 misho 9: static pthread_attr_t attr;
1.2 misho 10:
11:
12: static inline struct tagSession *
13: initSession(int sock, ait_val_t * __restrict v)
14: {
15: struct tagSession *sess = NULL;
16: const char *str;
17:
18: ioTRACE(5);
19:
20: if (!v)
21: return NULL;
22:
23: sess = malloc(sizeof(struct tagSession));
24: if (!sess) {
1.2.2.9 misho 25: ioSYSERR(0);
1.2 misho 26: io_freeVar(v);
27: return NULL;
28: } else
29: memset(sess, 0, sizeof(struct tagSession));
30:
1.2.2.4 misho 31: pthread_mutex_init(&sess->sess_mtx, NULL);
32:
1.2.2.9 misho 33: SLIST_INIT(&sess->sess_subscr);
1.2 misho 34:
1.2.2.5 misho 35: str = cfg_getAttribute(&cfg, "mqttd", "retry");
1.2 misho 36: if (!str)
37: sess->sess_retry = DEFAULT_RETRY;
38: else
39: sess->sess_retry = strtol(str, NULL, 0);
40:
1.2.2.9 misho 41: if (!(sess->sess_root = schedBegin())) {
42: ioLIBERR(sched);
43: free(sess);
44: io_freeVar(v);
45: return NULL;
46: }
47:
1.2 misho 48: sess->sess_buf = mqtt_msgAlloc(USHRT_MAX);
49: if (!sess->sess_buf) {
1.2.2.9 misho 50: ioLIBERR(mqtt);
51: schedEnd(&sess->sess_root);
1.2 misho 52: free(sess);
53: io_freeVar(v);
54: return NULL;
55: }
56:
1.2.2.8 misho 57: /* init server actor */
1.2.2.2 misho 58: sess->sess_srv = mqtt_srv_Init(sock, sess->sess_buf);
59: if (!sess->sess_srv) {
60: ioDEBUG(3, "Error:: in srv_Init #%d - %s", mqtt_GetErrno(), mqtt_GetError());
61: mqtt_msgFree(&sess->sess_buf, 42);
1.2.2.9 misho 62: schedEnd(&sess->sess_root);
1.2.2.2 misho 63: free(sess);
64: io_freeVar(v);
65: return NULL;
1.2.2.3 misho 66: } else {
67: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_CONNECT, cmdCONNECT);
68: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PUBLISH, cmdPUBLISH);
69: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PUBREL, cmdPUBREL);
70: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_SUBSCRIBE, cmdSUBSCRIBE);
71: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_UNSUBSCRIBE, cmdUNSUBSCRIBE);
72: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PINGREQ, cmdPINGREQ);
73: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_DISCONNECT, cmdDISCONNECT);
1.2.2.2 misho 74: }
75:
1.2 misho 76: sess->sess_sock = sock;
77: strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr);
78: io_freeVar(v);
79: return sess;
80: }
81:
82: static void
1.2.2.9 misho 83: finiSession(struct tagSession *sess)
1.2 misho 84: {
85: struct tagStore *store;
86:
87: ioTRACE(5);
88:
89: if (!sess)
90: return;
91:
92: if (call.FiniSessPUB)
93: call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%");
94:
1.2.2.6 misho 95: SESS_ELEM_LOCK(sess);
1.2 misho 96:
1.2.2.9 misho 97: while ((store = SLIST_FIRST(&sess->sess_subscr))) {
98: SLIST_REMOVE_HEAD(&sess->sess_subscr, st_node);
99:
100: if (store->st_subscr.sub_topic.msg_base)
101: free(store->st_subscr.sub_topic.msg_base);
102: if (store->st_subscr.sub_value.msg_base)
103: free(store->st_subscr.sub_value.msg_base);
104:
105: free(store);
106: }
1.2.2.6 misho 107: SESS_ELEM_UNLOCK(sess);
1.2.2.4 misho 108: pthread_mutex_destroy(&sess->sess_mtx);
1.2 misho 109:
1.2.2.9 misho 110: schedEnd(&sess->sess_root);
111:
1.2 misho 112: if (sess->sess_will.msg)
113: free(sess->sess_will.msg);
114: if (sess->sess_will.topic)
115: free(sess->sess_will.topic);
116:
1.2.2.9 misho 117: if (sess->sess_sock > STDERR_FILENO)
1.2 misho 118: srv_Close(sess->sess_sock);
119:
1.2.2.2 misho 120: mqtt_srv_Fini(&sess->sess_srv);
1.2 misho 121: mqtt_msgFree(&sess->sess_buf, 42);
122:
123: free(sess);
124: }
125:
126: static void
127: stopSession(struct tagSession *sess)
128: {
129: mqtt_msg_t msg = { NULL, 0 };
130: int ret;
131:
132: ioTRACE(4);
133:
134: assert(sess);
135:
1.2.2.6 misho 136: SESS_LOCK;
1.2 misho 137: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.6 misho 138: SESS_UNLOCK;
1.2 misho 139:
140: ret = mqtt_msgDISCONNECT(&msg);
1.2.2.9 misho 141: send(sess->sess_sock, msg.msg_base, ret, MSG_NOSIGNAL);
142: free(msg.msg_base);
1.2 misho 143:
144: ioDEBUG(1, "Close socket=%d", sess->sess_sock);
1.2.2.9 misho 145: finiSession(sess);
1.2 misho 146:
147: call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid,
148: sess->sess_addr, sess->sess_user);
149: }
150:
151: static int
152: KASession(struct tagSession *sess)
153: {
154: mqtt_msg_t msg = { NULL, 0 };
155: int ret;
156: struct pollfd pfd;
157:
158: ioTRACE(4);
159:
160: assert(sess);
161:
162: /* ping request */
163: ret = mqtt_msgPINGREQ(&msg);
1.2.2.9 misho 164: if ((ret = send(sess->sess_sock, msg.msg_base, ret, MSG_NOSIGNAL)) == -1) {
1.2 misho 165: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
166: return -1;
167: } else {
168: ioDEBUG(5, "Sended %d bytes for ping request", ret);
169: free(msg.msg_base);
170: memset(&msg, 0, sizeof msg);
171: }
172:
173: pfd.fd = sess->sess_sock;
174: pfd.events = POLLIN | POLLPRI;
175: if ((ret = poll(&pfd, 1, sess->sess_ka * 1000)) == -1 ||
176: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
177: ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
178: return -1;
179: } else if (!ret) {
180: ioDEBUG(5, "Warning:: Session is abandoned ... must be disconnect!");
181: return 1;
182: }
183: /* receive & decode packet */
184: if (recv(sess->sess_sock, sess->sess_buf->msg_base, sess->sess_buf->msg_len, 0) == -1) {
185: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
186: return -1;
187: }
188: if (mqtt_readPINGRESP(sess->sess_buf)) {
189: ioDEBUG(5, "Warning:: Session is broken, not hear ping response ... must be disconnect!");
190: return 2;
191: }
192:
193: /* Keep Alive is OK! */
194: return 0;
195: }
196:
197: static void *
198: thrSession(struct tagSession *sess)
199: {
200: int ret, locKill = 42;
201: struct pollfd pfd;
202: struct mqtthdr *hdr;
203: ait_val_t *v;
204:
205: pthread_cleanup_push((void(*)(void*)) stopSession, sess);
206: ioTRACE(2);
207:
208: pfd.fd = sess->sess_sock;
209: pfd.events = POLLIN | POLLPRI;
210: while (!Kill && locKill) {
211: if ((ret = poll(&pfd, 1, sess->sess_ka * 1000)) == -1 ||
212: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
213: ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
214: break;
215: } else if (!ret && (ret = KASession(sess))) {
216: call.LOG(logg, "Session %s keep-alive missing from %s for user %s ...\n",
217: sess->sess_cid, sess->sess_addr, sess->sess_user);
218: break;
219: }
220: /* receive & decode packet */
221: if ((ret = recv(sess->sess_sock, sess->sess_buf->msg_base, sess->sess_buf->msg_len, 0)) == -1) {
222: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
223: break;
224: } else if (!ret) {
225: ioDEBUG(4, "Session %s EOF received.", sess->sess_cid);
226: break;
227: } else
228: hdr = (struct mqtthdr*) sess->sess_buf->msg_base;
229:
230: /* dispatch message type */
1.2.2.3 misho 231: if (mqtt_srv_Dispatch(sess->sess_srv, sess))
232: ioLIBERR(mqtt);
1.2.2.11 misho 233: locKill ^= locKill;
1.2 misho 234: switch (hdr->mqtt_msg.type) {
235: case MQTT_TYPE_CONNECT:
236: ioDEBUG(5, "Exec CONNECT session");
237: if ((v = io_allocVar())) {
238: AIT_SET_STR(v, sess->sess_addr);
239: if (!schedEvent(root, startSession, v, (u_long) sess->sess_sock, sess, ret))
240: io_freeVar(v);
241: } else
242: ioLIBERR(mqtt);
243: continue;
244: case MQTT_TYPE_DISCONNECT:
245: ioDEBUG(5, "Exec DISCONNECT session");
1.2.2.9 misho 246: finiSession(sess);
1.2.2.11 misho 247: locKill = 42;
1.2 misho 248: continue;
249: case MQTT_TYPE_PUBLISH:
250: ioDEBUG(5, "Exec PUBLISH topic QoS=%d", hdr->mqtt_msg.qos);
1.2.2.3 misho 251: /*
252: if (cmdPUBLISH(sess))
1.2 misho 253: locKill ^= locKill;
1.2.2.3 misho 254: */
1.2 misho 255: break;
256: case MQTT_TYPE_PUBREL:
257: break;
258: case MQTT_TYPE_SUBSCRIBE:
259: break;
260: case MQTT_TYPE_UNSUBSCRIBE:
261: break;
262: case MQTT_TYPE_PINGREQ:
1.2.2.4 misho 263: ioDEBUG(5, "Exec PINGREQ session");
1.2 misho 264: break;
1.2.2.11 misho 265: case MQTT_TYPE_PINGRESP:
266: ioDEBUG(5, "Exec PINGRESP session");
267: break;
1.2 misho 268: default:
269: ioDEBUG(5, "Error:: Session %s, wrong command %d - DISCARDED",
270: sess->sess_cid, hdr->mqtt_msg.type);
271: break;
272: }
273: }
274:
275: pthread_cleanup_pop(locKill);
276: pthread_exit(NULL);
1.2.2.11 misho 277: return NULL;
1.2 misho 278: }
279:
280: static void *
281: startSession(sched_task_t *task)
282: {
283: u_char basebuf[USHRT_MAX];
284: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
285: mqtthdr_connflgs_t flg;
286: mqtthdr_connack_t cack;
287: struct tagSession *s, *sess = NULL;
288: int ret;
289:
290: ioTRACE(4);
291:
292: assert(task);
293:
294: if (!TASK_DATA(task)) {
1.2.2.8 misho 295: /* flow from accept new clients */
1.2 misho 296: sess = initSession(TASK_FD(task), TASK_ARG(task));
297: if (!sess) {
298: io_freeVar(TASK_ARG(task));
299: close(TASK_FD(task));
300: return NULL;
301: }
302:
303: /* receive & decode packet */
304: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
305: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.2.2.9 misho 306: finiSession(sess);
1.2 misho 307: return NULL;
308: }
309: } else {
310: sess = TASK_DATA(task);
311: buf.msg_len = TASK_DATLEN(task) > sizeof basebuf ? sizeof basebuf : TASK_DATLEN(task);
312: memcpy(buf.msg_base, sess->sess_buf->msg_base, buf.msg_len);
313: }
314:
315: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
316: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
317: &sess->sess_will.topic, &sess->sess_will.msg);
318: ret = cack.retcode;
319: flg.flags = cack.reserved;
320: if (flg.reserved) {
321: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
322: goto end;
323: } else {
324: sess->sess_clean = flg.clean_sess;
325: sess->sess_will.qos = flg.will_qos;
326: sess->sess_will.retain = flg.will_retain;
327: sess->sess_will.flag = flg.will_flg;
328: }
329:
330: /* check online table for user */
331: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
332: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
333: sess->sess_user, sess->sess_pass);
334: ret = MQTT_RETCODE_DENIED;
335: goto end;
336: } else {
337: ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
338: ret = MQTT_RETCODE_ACCEPTED;
339: }
1.2.2.12! misho 340:
1.2 misho 341: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
342: ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
343: TAILQ_FOREACH(s, &Sessions, sess_node)
344: if (!strcmp(s->sess_cid, sess->sess_cid)) {
345: /* found stale session & disconnect it! */
346: stopSession(s);
347: break;
348: }
349: }
1.2.2.12! misho 350:
1.2 misho 351: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
352: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
353: sess->sess_will.qos, sess->sess_will.retain) == -1) {
354: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
355: ret = MQTT_RETCODE_DENIED;
356: goto end;
357: } else {
358: ioDEBUG(0, "Session %s from %s and username %s is started",
359: sess->sess_cid, sess->sess_addr, sess->sess_user);
360: ret = MQTT_RETCODE_ACCEPTED;
361: }
362:
363: ret = mqtt_msgCONNACK(&msg, ret);
364: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
365: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.2.2.9 misho 366: finiSession(sess);
1.2 misho 367: return NULL;
368: } else {
369: ioDEBUG(5, "Sended %d bytes", ret);
370: free(msg.msg_base);
371: memset(&msg, 0, sizeof msg);
372: }
373:
374: /* Start session thread OK ... */
1.2.2.6 misho 375: SESS_LOCK;
1.2 misho 376: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.2.2.12! misho 377: pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
1.2.2.6 misho 378: SESS_UNLOCK;
1.2 misho 379:
380: call.LOG(logg, "Session %s started from %s for user %s (timeout=%d) OK!\n", sess->sess_cid,
381: sess->sess_addr, sess->sess_user, sess->sess_ka);
382: return NULL;
383: end: /* close client connection */
384: ret = mqtt_msgCONNACK(&msg, ret);
385: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
386: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
387: } else {
388: ioDEBUG(5, "Sended %d bytes", ret);
389: free(msg.msg_base);
390: memset(&msg, 0, sizeof msg);
391: }
392:
393: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
1.2.2.9 misho 394: finiSession(sess);
1.2 misho 395: return NULL;
396: }
397:
398: static void *
1.2.2.9 misho 399: acceptClient(sched_task_t *task)
1.2 misho 400: {
1.2.2.9 misho 401: int cli;
402: io_sockaddr_t sa;
403: socklen_t sslen = sizeof sa.ss;
404: ait_val_t *v;
405: char str[STRSIZ];
1.2 misho 406:
1.2.2.9 misho 407: ioTRACE(4);
1.2 misho 408:
1.2.2.9 misho 409: assert(task);
1.2 misho 410:
1.2.2.9 misho 411: if ((cli = accept(TASK_FD(task), &sa.sa, &sslen)) == -1)
412: goto end;
413: else
414: fcntl(TASK_FD(task), F_SETFL, fcntl(TASK_FD(task), F_GETFL, 0) | O_NONBLOCK);
415:
416: v = io_allocVar();
417: if (!v) {
418: ioLIBERR(io);
419: close(cli);
420: goto end;
421: } else {
422: memset(str, 0, sizeof str);
423: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
424: AIT_SET_STR(v, str);
425: }
426: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
427:
428: if (!schedRead(root, startSession, v, cli, NULL, 0)) {
429: io_freeVar(v);
430: close(cli);
431: ioDEBUG(1, "Terminated client with socket=%d", cli);
432: }
433: end:
1.2.2.11 misho 434: if (!schedRead(TASK_ROOT(task), acceptClient, NULL, TASK_FD(task), NULL, 0))
435: ioLIBERR(sched);
1.2.2.9 misho 436: return NULL;
1.2 misho 437: }
438:
1.2.2.9 misho 439: /* ----------------------------------------------------------------------- */
440:
1.2 misho 441: int
442: Run(int sock)
443: {
1.2.2.9 misho 444: struct tagPub *pub;
445: struct timespec pl = { 0, 100000000 };
1.2 misho 446:
447: ioTRACE(1);
448:
1.2.2.9 misho 449: if (listen(sock, SOMAXCONN) == -1) {
1.2 misho 450: ioSYSERR(0);
451: return -1;
452: } else
1.2.2.9 misho 453: fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
1.2 misho 454:
1.2.2.9 misho 455: /* state machine - accept new connections */
456: if (!schedRead(root, acceptClient, NULL, sock, NULL, 0)) {
457: ioLIBERR(sched);
1.2 misho 458: return -1;
459: }
460:
1.2.2.10 misho 461: pthread_attr_init(&attr);
462: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
463:
1.2.2.9 misho 464: schedPolling(root, &pl, NULL);
465: schedRun(root, &Kill);
1.2 misho 466:
1.2.2.10 misho 467: pthread_attr_destroy(&attr);
468:
1.2.2.9 misho 469: /* free all undeleted elements into lists */
470: PUBS_LOCK;
471: TAILQ_FOREACH(pub, &Pubs, pub_node) {
472: TAILQ_REMOVE(&Pubs, pub, pub_node);
473:
474: AIT_FREE_VAL(&pub->pub_name);
475: if (pub->pub_packet.msg_base)
476: free(pub->pub_packet.msg_base);
1.2 misho 477: }
1.2.2.9 misho 478: PUBS_UNLOCK;
1.2 misho 479: return 0;
480: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>