Annotation of mqtt/src/daemon.c, revision 1.1.2.28
1.1.2.1 misho 1: #include "global.h"
1.1.2.16 misho 2: #include "rtlm.h"
1.1.2.24 misho 3: #include "utils.h"
1.1.2.11 misho 4: #include "mqttd.h"
1.1.2.1 misho 5:
6:
1.1.2.14 misho 7: static inline struct tagSession *
1.1.2.18 misho 8: initSession(int sock, ait_val_t * __restrict v)
1.1.2.14 misho 9: {
10: struct tagSession *sess = NULL;
11: const char *str;
12:
1.1.2.20 misho 13: ioTRACE(5);
1.1.2.15 misho 14:
1.1.2.18 misho 15: if (!v)
1.1.2.14 misho 16: return NULL;
17:
18: sess = malloc(sizeof(struct tagSession));
19: if (!sess) {
1.1.2.20 misho 20: ioDEBUG(3, "Error:: in malloc #%d - %s", errno, strerror(errno));
1.1.2.18 misho 21: io_freeVar(v);
1.1.2.14 misho 22: return NULL;
23: } else
24: memset(sess, 0, sizeof(struct tagSession));
25:
1.1.2.17 misho 26: str = (const char*) cfg_GetAttribute(&cfg, CFG("mqttd"), CFG("retry"));
1.1.2.14 misho 27: if (!str)
28: sess->sess_retry = DEFAULT_RETRY;
29: else
30: sess->sess_retry = strtol(str, NULL, 0);
31:
32: sess->sess_buf = mqtt_msgAlloc(0);
33: if (!sess->sess_buf) {
1.1.2.20 misho 34: ioDEBUG(3, "Error:: in msgAlloc #%d - %s", mqtt_GetErrno(), mqtt_GetError());
1.1.2.14 misho 35: free(sess);
1.1.2.18 misho 36: io_freeVar(v);
1.1.2.14 misho 37: return NULL;
38: }
39:
1.1.2.15 misho 40: sess->sess_sock = sock;
1.1.2.19 misho 41: strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr);
1.1.2.18 misho 42: io_freeVar(v);
1.1.2.14 misho 43: return sess;
44: }
45:
46: static void
47: finiSession(struct tagSession *sess)
48: {
49: register int i;
50: struct tagStore *store;
51:
1.1.2.20 misho 52: ioTRACE(5);
1.1.2.15 misho 53:
1.1.2.14 misho 54: if (!sess)
55: return;
56:
57: for (i = 0; i < MQTT_QOS_RESERVED; i++)
58: while ((store = SLIST_FIRST(&sess->sess_store[i]))) {
59: SLIST_REMOVE_HEAD(&sess->sess_store[i], st_node);
60: free(store);
61: }
62:
63: if (sess->sess_will.msg)
64: free(sess->sess_will.msg);
65: if (sess->sess_will.topic)
66: free(sess->sess_will.topic);
67:
1.1.2.24 misho 68: if (sess->sess_sock > STDERR_FILENO)
69: srv_Close(sess->sess_sock);
1.1.2.14 misho 70:
71: mqtt_msgFree(&sess->sess_buf, 42);
72:
73: free(sess);
74: }
75:
1.1.2.24 misho 76: static void
77: stopSession(struct tagSession *sess)
1.1.2.15 misho 78: {
1.1.2.24 misho 79: mqtt_msg_t msg = { NULL, 0 };
80: int ret;
81:
1.1.2.26 misho 82: ioTRACE(4);
83:
84: assert(sess);
85:
1.1.2.24 misho 86: pthread_mutex_lock(&mtx_sess);
87: TAILQ_REMOVE(&Sessions, sess, sess_node);
88: pthread_mutex_unlock(&mtx_sess);
89:
90: ret = mqtt_msgDISCONNECT(&msg);
91: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1)
92: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
93: else {
94: ioDEBUG(5, "Sended %d bytes for disconnect", ret);
95: free(msg.msg_base);
1.1.2.27 misho 96: memset(&msg, 0, sizeof msg);
1.1.2.18 misho 97: }
98:
1.1.2.24 misho 99: ioDEBUG(1, "Close socket=%d", sess->sess_sock);
100: finiSession(sess);
1.1.2.27 misho 101:
1.1.2.28! misho 102: call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid,
1.1.2.27 misho 103: sess->sess_addr, sess->sess_user);
1.1.2.24 misho 104: }
105:
1.1.2.26 misho 106: static int
107: KASession(struct tagSession *sess)
108: {
109: u_char basebuf[USHRT_MAX];
110: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
111: int ret;
112: struct pollfd pfd;
113:
114: ioTRACE(4);
115:
116: assert(sess);
117:
118: /* ping request */
119: ret = mqtt_msgPINGREQ(&msg);
120: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
121: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
122: return -1;
123: } else {
124: ioDEBUG(5, "Sended %d bytes for ping request", ret);
125: free(msg.msg_base);
126: memset(&msg, 0, sizeof msg);
127: }
128:
129: pfd.fd = sess->sess_sock;
130: pfd.events = POLLIN | POLLPRI;
1.1.2.28! misho 131: if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1 ||
! 132: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.1.2.26 misho 133: ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
134: return -1;
135: } else if (!ret) {
136: /* problem!!! session is abandoned ... must be disconnect! */
137: return 1;
138: }
139: /* receive & decode packet */
140: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
141: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
142: return -1;
143: }
144: if (mqtt_readPINGRESP(&buf)) {
145: /* problem!!! session is broken, not hear ping response ... must be disconnect! */
146: return 2;
147: }
148:
149: /* Keep Alive is OK! */
150: return 0;
151: }
152:
1.1.2.24 misho 153: static void *
154: thrSession(struct tagSession *sess)
155: {
1.1.2.28! misho 156: u_char basebuf[USHRT_MAX];
! 157: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
! 158: int ret;
! 159: struct pollfd pfd;
! 160: struct mqtthdr *hdr;
1.1.2.18 misho 161:
1.1.2.28! misho 162: pthread_cleanup_push((void(*)(void*)) stopSession, sess);
1.1.2.20 misho 163: ioTRACE(2);
1.1.2.15 misho 164:
1.1.2.28! misho 165: pfd.fd = sess->sess_sock;
! 166: pfd.events = POLLIN | POLLPRI;
! 167: while (!Kill) {
! 168: if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1 ||
! 169: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
! 170: ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
! 171: break;
! 172: } else if (!ret && (ret = KASession(sess))) {
! 173: call.LOG(logg, "Session %s keep-alive missing from %s for user %s ...\n",
! 174: sess->sess_cid, sess->sess_addr, sess->sess_user);
! 175: break;
! 176: }
! 177: /* receive & decode packet */
! 178: if ((ret = recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0)) == -1) {
! 179: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
! 180: break;
! 181: } else if (!ret) {
! 182: ioDEBUG(4, "Session %s EOF received.", sess->sess_cid);
! 183: break;
! 184: } else
! 185: hdr = (struct mqtthdr*) buf.msg_base;
! 186:
! 187: switch (hdr->mqtt_msg.type) {
! 188: default:
! 189: ioDEBUG(5, "Error:: Session %s, wrong command %d - DISCARDED",
! 190: sess->sess_cid, hdr->mqtt_msg.type);
! 191: break;
! 192: }
! 193: }
! 194:
1.1.2.18 misho 195: pthread_cleanup_pop(42);
1.1.2.15 misho 196: pthread_exit(NULL);
197: }
198:
199: static void *
1.1.2.5 misho 200: startSession(sched_task_t *task)
1.1.2.4 misho 201: {
1.1.2.11 misho 202: u_char basebuf[USHRT_MAX];
1.1.2.18 misho 203: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
1.1.2.10 misho 204: mqtthdr_connflgs_t flg;
1.1.2.13 misho 205: mqtthdr_connack_t cack;
1.1.2.24 misho 206: struct tagSession *s, *sess = NULL;
1.1.2.18 misho 207: int ret;
208: pthread_attr_t attr;
1.1.2.6 misho 209:
1.1.2.20 misho 210: ioTRACE(4);
1.1.2.6 misho 211:
1.1.2.26 misho 212: assert(task);
213:
1.1.2.14 misho 214: sess = initSession(TASK_FD(task), TASK_ARG(task));
1.1.2.15 misho 215: if (!sess) {
1.1.2.18 misho 216: io_freeVar(TASK_ARG(task));
1.1.2.15 misho 217: close(TASK_FD(task));
1.1.2.14 misho 218: return NULL;
1.1.2.15 misho 219: }
1.1.2.6 misho 220:
1.1.2.18 misho 221: /* receive & decode packet */
1.1.2.14 misho 222: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
1.1.2.20 misho 223: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 224: finiSession(sess);
225: return NULL;
1.1.2.9 misho 226: }
1.1.2.14 misho 227: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
1.1.2.12 misho 228: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
1.1.2.13 misho 229: &sess->sess_will.topic, &sess->sess_will.msg);
1.1.2.18 misho 230: ret = cack.retcode;
1.1.2.14 misho 231: flg.flags = cack.reserved;
232: if (flg.reserved) {
1.1.2.20 misho 233: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
1.1.2.10 misho 234: goto end;
1.1.2.14 misho 235: } else {
236: sess->sess_clean = flg.clean_sess;
237: sess->sess_will.qos = flg.will_qos;
238: sess->sess_will.retain = flg.will_retain;
239: sess->sess_will.flag = flg.will_flg;
1.1.2.12 misho 240: }
1.1.2.6 misho 241:
1.1.2.8 misho 242: /* check online table for user */
1.1.2.18 misho 243: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
1.1.2.20 misho 244: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
1.1.2.16 misho 245: sess->sess_user, sess->sess_pass);
1.1.2.21 misho 246: ret = MQTT_RETCODE_DENIED;
1.1.2.16 misho 247: goto end;
1.1.2.21 misho 248: } else {
1.1.2.24 misho 249: ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
1.1.2.21 misho 250: ret = MQTT_RETCODE_ACCEPTED;
251: }
1.1.2.23 misho 252: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
1.1.2.24 misho 253: ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
254: TAILQ_FOREACH(s, &Sessions, sess_node)
255: if (!strcmp(s->sess_cid, sess->sess_cid)) {
1.1.2.25 misho 256: /* found stale session & disconnect it! */
1.1.2.24 misho 257: stopSession(s);
258: break;
259: }
1.1.2.23 misho 260: }
261: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
262: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
263: sess->sess_will.qos, sess->sess_will.retain) == -1) {
264: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
265: ret = MQTT_RETCODE_DENIED;
266: goto end;
267: } else {
268: ioDEBUG(0, "Session %s from %s and username %s is started",
269: sess->sess_cid, sess->sess_addr, sess->sess_user);
270: ret = MQTT_RETCODE_ACCEPTED;
271: }
1.1.2.6 misho 272:
1.1.2.18 misho 273: ret = mqtt_msgCONNACK(&msg, ret);
274: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20 misho 275: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 276: finiSession(sess);
277: return NULL;
278: } else {
1.1.2.20 misho 279: ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19 misho 280: free(msg.msg_base);
1.1.2.27 misho 281: memset(&msg, 0, sizeof msg);
1.1.2.18 misho 282: }
283:
1.1.2.15 misho 284: /* Start session thread OK ... */
1.1.2.18 misho 285: pthread_attr_init(&attr);
286: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
287:
288: pthread_mutex_lock(&mtx_sess);
1.1.2.15 misho 289: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.1.2.18 misho 290: pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
291: pthread_mutex_unlock(&mtx_sess);
292:
1.1.2.23 misho 293: call.LOG(logg, "Session %s started from %s for user %s OK!\n", sess->sess_cid,
294: sess->sess_addr, sess->sess_user);
1.1.2.18 misho 295:
296: pthread_attr_destroy(&attr);
1.1.2.15 misho 297: return NULL;
1.1.2.6 misho 298: end: /* close client connection */
1.1.2.18 misho 299: ret = mqtt_msgCONNACK(&msg, ret);
300: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20 misho 301: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 302: } else {
1.1.2.20 misho 303: ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19 misho 304: free(msg.msg_base);
1.1.2.27 misho 305: memset(&msg, 0, sizeof msg);
1.1.2.18 misho 306: }
307:
1.1.2.20 misho 308: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
1.1.2.15 misho 309: finiSession(sess);
1.1.2.6 misho 310: return NULL;
1.1.2.4 misho 311: }
312:
313: /* ----------------------------------------------------------------------- */
314:
1.1.2.5 misho 315: static void *
316: thrSched(void *arg __unused)
317: {
1.1.2.18 misho 318: struct tagSession *sess;
1.1.2.5 misho 319:
1.1.2.20 misho 320: ioTRACE(1);
321:
322: schedRun(root, &Kill);
1.1.2.18 misho 323:
324: TAILQ_FOREACH(sess, &Sessions, sess_node)
325: if (sess->sess_tid)
326: pthread_cancel(sess->sess_tid);
1.1.2.5 misho 327: pthread_exit(NULL);
328: }
329:
1.1.2.2 misho 330: int
1.1.2.3 misho 331: Run(int sock)
1.1.2.2 misho 332: {
1.1.2.4 misho 333: io_sockaddr_t sa;
334: socklen_t sslen = sizeof sa.ss;
335: int cli;
1.1.2.5 misho 336: pthread_t tid;
1.1.2.18 misho 337: ait_val_t *v;
1.1.2.23 misho 338: char str[STRSIZ];
1.1.2.4 misho 339:
1.1.2.20 misho 340: ioTRACE(1);
1.1.2.2 misho 341:
1.1.2.5 misho 342: if (pthread_create(&tid, NULL, thrSched, NULL)) {
1.1.2.20 misho 343: ioLOGERR(0);
1.1.2.5 misho 344: return -1;
345: } else
346: pthread_detach(tid);
1.1.2.20 misho 347: ioDEBUG(2, "Run scheduler management thread");
1.1.2.5 misho 348:
1.1.2.24 misho 349: if (listen(sock, SOMAXCONN) == -1) {
1.1.2.20 misho 350: ioLOGERR(0);
1.1.2.3 misho 351: return -1;
352: }
353:
1.1.2.4 misho 354: while (!Kill) {
355: if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
1.1.2.20 misho 356: if (!Kill)
357: continue;
358: ioLOGERR(0);
359: break;
1.1.2.18 misho 360: }
361: v = io_allocVar();
362: if (!v) {
363: ioLIBERR(mqtt);
364: break;
1.1.2.23 misho 365: } else {
366: memset(str, 0, sizeof str);
367: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
368: AIT_SET_STR(v, str);
369: }
370: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
1.1.2.4 misho 371:
1.1.2.18 misho 372: if (!schedRead(root, startSession, v, cli)) {
373: io_freeVar(v);
1.1.2.4 misho 374: close(cli);
1.1.2.20 misho 375: ioDEBUG(1, "Terminated client with socket=%d", cli);
1.1.2.4 misho 376: }
377: }
378:
1.1.2.2 misho 379: return 0;
380: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>