Annotation of mqtt/src/daemon.c, revision 1.1.2.27
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:
! 102: call.LOG(logg, "Session %s stopped from %s for user %s OK!\n", sess->sess_cid,
! 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: stopSession(sess);
123: return -1;
124: } else {
125: ioDEBUG(5, "Sended %d bytes for ping request", ret);
126: free(msg.msg_base);
127: memset(&msg, 0, sizeof msg);
128: }
129:
130: pfd.fd = sess->sess_sock;
131: pfd.events = POLLIN | POLLPRI;
132: if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1) {
133: ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
134: stopSession(sess);
135: return -1;
136: } else if (!ret) {
137: /* problem!!! session is abandoned ... must be disconnect! */
138: stopSession(sess);
139: return 1;
140: }
141: /* receive & decode packet */
142: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
143: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
144: stopSession(sess);
145: return -1;
146: }
147: if (mqtt_readPINGRESP(&buf)) {
148: /* problem!!! session is broken, not hear ping response ... must be disconnect! */
149: stopSession(sess);
150: return 2;
151: }
152:
153: /* Keep Alive is OK! */
154: return 0;
155: }
156:
1.1.2.24 misho 157: static void *
158: thrSession(struct tagSession *sess)
159: {
160: pthread_cleanup_push((void(*)(void*)) stopSession, sess);
1.1.2.18 misho 161:
1.1.2.20 misho 162: ioTRACE(2);
1.1.2.15 misho 163:
1.1.2.18 misho 164: pthread_cleanup_pop(42);
1.1.2.15 misho 165: pthread_exit(NULL);
166: }
167:
168: static void *
1.1.2.5 misho 169: startSession(sched_task_t *task)
1.1.2.4 misho 170: {
1.1.2.11 misho 171: u_char basebuf[USHRT_MAX];
1.1.2.18 misho 172: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
1.1.2.10 misho 173: mqtthdr_connflgs_t flg;
1.1.2.13 misho 174: mqtthdr_connack_t cack;
1.1.2.24 misho 175: struct tagSession *s, *sess = NULL;
1.1.2.18 misho 176: int ret;
177: pthread_attr_t attr;
1.1.2.6 misho 178:
1.1.2.20 misho 179: ioTRACE(4);
1.1.2.6 misho 180:
1.1.2.26 misho 181: assert(task);
182:
1.1.2.14 misho 183: sess = initSession(TASK_FD(task), TASK_ARG(task));
1.1.2.15 misho 184: if (!sess) {
1.1.2.18 misho 185: io_freeVar(TASK_ARG(task));
1.1.2.15 misho 186: close(TASK_FD(task));
1.1.2.14 misho 187: return NULL;
1.1.2.15 misho 188: }
1.1.2.6 misho 189:
1.1.2.18 misho 190: /* receive & decode packet */
1.1.2.14 misho 191: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
1.1.2.20 misho 192: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 193: finiSession(sess);
194: return NULL;
1.1.2.9 misho 195: }
1.1.2.14 misho 196: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
1.1.2.12 misho 197: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
1.1.2.13 misho 198: &sess->sess_will.topic, &sess->sess_will.msg);
1.1.2.18 misho 199: ret = cack.retcode;
1.1.2.14 misho 200: flg.flags = cack.reserved;
201: if (flg.reserved) {
1.1.2.20 misho 202: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
1.1.2.10 misho 203: goto end;
1.1.2.14 misho 204: } else {
205: sess->sess_clean = flg.clean_sess;
206: sess->sess_will.qos = flg.will_qos;
207: sess->sess_will.retain = flg.will_retain;
208: sess->sess_will.flag = flg.will_flg;
1.1.2.12 misho 209: }
1.1.2.6 misho 210:
1.1.2.8 misho 211: /* check online table for user */
1.1.2.18 misho 212: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
1.1.2.20 misho 213: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
1.1.2.16 misho 214: sess->sess_user, sess->sess_pass);
1.1.2.21 misho 215: ret = MQTT_RETCODE_DENIED;
1.1.2.16 misho 216: goto end;
1.1.2.21 misho 217: } else {
1.1.2.24 misho 218: ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
1.1.2.21 misho 219: ret = MQTT_RETCODE_ACCEPTED;
220: }
1.1.2.23 misho 221: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
1.1.2.24 misho 222: ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
223: TAILQ_FOREACH(s, &Sessions, sess_node)
224: if (!strcmp(s->sess_cid, sess->sess_cid)) {
1.1.2.25 misho 225: /* found stale session & disconnect it! */
1.1.2.24 misho 226: stopSession(s);
227: break;
228: }
1.1.2.23 misho 229: }
230: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
231: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
232: sess->sess_will.qos, sess->sess_will.retain) == -1) {
233: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
234: ret = MQTT_RETCODE_DENIED;
235: goto end;
236: } else {
237: ioDEBUG(0, "Session %s from %s and username %s is started",
238: sess->sess_cid, sess->sess_addr, sess->sess_user);
239: ret = MQTT_RETCODE_ACCEPTED;
240: }
1.1.2.6 misho 241:
1.1.2.18 misho 242: ret = mqtt_msgCONNACK(&msg, ret);
243: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20 misho 244: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 245: finiSession(sess);
246: return NULL;
247: } else {
1.1.2.20 misho 248: ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19 misho 249: free(msg.msg_base);
1.1.2.27! misho 250: memset(&msg, 0, sizeof msg);
1.1.2.18 misho 251: }
252:
1.1.2.15 misho 253: /* Start session thread OK ... */
1.1.2.18 misho 254: pthread_attr_init(&attr);
255: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
256:
257: pthread_mutex_lock(&mtx_sess);
1.1.2.15 misho 258: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.1.2.18 misho 259: pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
260: pthread_mutex_unlock(&mtx_sess);
261:
1.1.2.23 misho 262: call.LOG(logg, "Session %s started from %s for user %s OK!\n", sess->sess_cid,
263: sess->sess_addr, sess->sess_user);
1.1.2.18 misho 264:
265: pthread_attr_destroy(&attr);
1.1.2.15 misho 266: return NULL;
1.1.2.6 misho 267: end: /* close client connection */
1.1.2.18 misho 268: ret = mqtt_msgCONNACK(&msg, ret);
269: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20 misho 270: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 271: } else {
1.1.2.20 misho 272: ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19 misho 273: free(msg.msg_base);
1.1.2.27! misho 274: memset(&msg, 0, sizeof msg);
1.1.2.18 misho 275: }
276:
1.1.2.20 misho 277: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
1.1.2.15 misho 278: finiSession(sess);
1.1.2.6 misho 279: return NULL;
1.1.2.4 misho 280: }
281:
282: /* ----------------------------------------------------------------------- */
283:
1.1.2.5 misho 284: static void *
285: thrSched(void *arg __unused)
286: {
1.1.2.18 misho 287: struct tagSession *sess;
1.1.2.5 misho 288:
1.1.2.20 misho 289: ioTRACE(1);
290:
291: schedRun(root, &Kill);
1.1.2.18 misho 292:
293: TAILQ_FOREACH(sess, &Sessions, sess_node)
294: if (sess->sess_tid)
295: pthread_cancel(sess->sess_tid);
1.1.2.5 misho 296: pthread_exit(NULL);
297: }
298:
1.1.2.2 misho 299: int
1.1.2.3 misho 300: Run(int sock)
1.1.2.2 misho 301: {
1.1.2.4 misho 302: io_sockaddr_t sa;
303: socklen_t sslen = sizeof sa.ss;
304: int cli;
1.1.2.5 misho 305: pthread_t tid;
1.1.2.18 misho 306: ait_val_t *v;
1.1.2.23 misho 307: char str[STRSIZ];
1.1.2.4 misho 308:
1.1.2.20 misho 309: ioTRACE(1);
1.1.2.2 misho 310:
1.1.2.5 misho 311: if (pthread_create(&tid, NULL, thrSched, NULL)) {
1.1.2.20 misho 312: ioLOGERR(0);
1.1.2.5 misho 313: return -1;
314: } else
315: pthread_detach(tid);
1.1.2.20 misho 316: ioDEBUG(2, "Run scheduler management thread");
1.1.2.5 misho 317:
1.1.2.24 misho 318: if (listen(sock, SOMAXCONN) == -1) {
1.1.2.20 misho 319: ioLOGERR(0);
1.1.2.3 misho 320: return -1;
321: }
322:
1.1.2.4 misho 323: while (!Kill) {
324: if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
1.1.2.20 misho 325: if (!Kill)
326: continue;
327: ioLOGERR(0);
328: break;
1.1.2.18 misho 329: }
330: v = io_allocVar();
331: if (!v) {
332: ioLIBERR(mqtt);
333: break;
1.1.2.23 misho 334: } else {
335: memset(str, 0, sizeof str);
336: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
337: AIT_SET_STR(v, str);
338: }
339: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
1.1.2.4 misho 340:
1.1.2.18 misho 341: if (!schedRead(root, startSession, v, cli)) {
342: io_freeVar(v);
1.1.2.4 misho 343: close(cli);
1.1.2.20 misho 344: ioDEBUG(1, "Terminated client with socket=%d", cli);
1.1.2.4 misho 345: }
346: }
347:
1.1.2.2 misho 348: return 0;
349: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>