Annotation of mqtt/src/daemon.c, revision 1.1.2.25
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:
82: pthread_mutex_lock(&mtx_sess);
83: TAILQ_REMOVE(&Sessions, sess, sess_node);
84: pthread_mutex_unlock(&mtx_sess);
85:
86: ret = mqtt_msgDISCONNECT(&msg);
87: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1)
88: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
89: else {
90: ioDEBUG(5, "Sended %d bytes for disconnect", ret);
91: free(msg.msg_base);
92: msg.msg_len = 0;
1.1.2.18 misho 93: }
94:
1.1.2.24 misho 95: ioDEBUG(1, "Close socket=%d", sess->sess_sock);
96: finiSession(sess);
97: }
98:
99: static void *
100: thrSession(struct tagSession *sess)
101: {
102: pthread_cleanup_push((void(*)(void*)) stopSession, sess);
1.1.2.18 misho 103:
1.1.2.20 misho 104: ioTRACE(2);
1.1.2.15 misho 105:
1.1.2.18 misho 106: pthread_cleanup_pop(42);
1.1.2.15 misho 107: pthread_exit(NULL);
108: }
109:
110: static void *
1.1.2.5 misho 111: startSession(sched_task_t *task)
1.1.2.4 misho 112: {
1.1.2.11 misho 113: u_char basebuf[USHRT_MAX];
1.1.2.18 misho 114: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
1.1.2.10 misho 115: mqtthdr_connflgs_t flg;
1.1.2.13 misho 116: mqtthdr_connack_t cack;
1.1.2.24 misho 117: struct tagSession *s, *sess = NULL;
1.1.2.18 misho 118: int ret;
119: pthread_attr_t attr;
1.1.2.6 misho 120:
1.1.2.20 misho 121: ioTRACE(4);
1.1.2.6 misho 122:
1.1.2.14 misho 123: sess = initSession(TASK_FD(task), TASK_ARG(task));
1.1.2.15 misho 124: if (!sess) {
1.1.2.18 misho 125: io_freeVar(TASK_ARG(task));
1.1.2.15 misho 126: close(TASK_FD(task));
1.1.2.14 misho 127: return NULL;
1.1.2.15 misho 128: }
1.1.2.6 misho 129:
1.1.2.18 misho 130: /* receive & decode packet */
1.1.2.14 misho 131: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
1.1.2.20 misho 132: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 133: finiSession(sess);
134: return NULL;
1.1.2.9 misho 135: }
1.1.2.14 misho 136: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
1.1.2.12 misho 137: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
1.1.2.13 misho 138: &sess->sess_will.topic, &sess->sess_will.msg);
1.1.2.18 misho 139: ret = cack.retcode;
1.1.2.14 misho 140: flg.flags = cack.reserved;
141: if (flg.reserved) {
1.1.2.20 misho 142: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
1.1.2.10 misho 143: goto end;
1.1.2.14 misho 144: } else {
145: sess->sess_clean = flg.clean_sess;
146: sess->sess_will.qos = flg.will_qos;
147: sess->sess_will.retain = flg.will_retain;
148: sess->sess_will.flag = flg.will_flg;
1.1.2.12 misho 149: }
1.1.2.6 misho 150:
1.1.2.8 misho 151: /* check online table for user */
1.1.2.18 misho 152: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
1.1.2.20 misho 153: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
1.1.2.16 misho 154: sess->sess_user, sess->sess_pass);
1.1.2.21 misho 155: ret = MQTT_RETCODE_DENIED;
1.1.2.16 misho 156: goto end;
1.1.2.21 misho 157: } else {
1.1.2.24 misho 158: ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
1.1.2.21 misho 159: ret = MQTT_RETCODE_ACCEPTED;
160: }
1.1.2.23 misho 161: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
1.1.2.24 misho 162: ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
163: TAILQ_FOREACH(s, &Sessions, sess_node)
164: if (!strcmp(s->sess_cid, sess->sess_cid)) {
1.1.2.25! misho 165: /* found stale session & disconnect it! */
1.1.2.24 misho 166: stopSession(s);
167: break;
168: }
1.1.2.23 misho 169: }
170: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
171: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
172: sess->sess_will.qos, sess->sess_will.retain) == -1) {
173: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
174: ret = MQTT_RETCODE_DENIED;
175: goto end;
176: } else {
177: ioDEBUG(0, "Session %s from %s and username %s is started",
178: sess->sess_cid, sess->sess_addr, sess->sess_user);
179: ret = MQTT_RETCODE_ACCEPTED;
180: }
1.1.2.6 misho 181:
1.1.2.18 misho 182: ret = mqtt_msgCONNACK(&msg, ret);
183: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20 misho 184: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 185: finiSession(sess);
186: return NULL;
187: } else {
1.1.2.20 misho 188: ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19 misho 189: free(msg.msg_base);
190: msg.msg_len = 0;
1.1.2.18 misho 191: }
192:
1.1.2.15 misho 193: /* Start session thread OK ... */
1.1.2.18 misho 194: pthread_attr_init(&attr);
195: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
196:
197: pthread_mutex_lock(&mtx_sess);
1.1.2.15 misho 198: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.1.2.18 misho 199: pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
200: pthread_mutex_unlock(&mtx_sess);
201:
1.1.2.23 misho 202: call.LOG(logg, "Session %s started from %s for user %s OK!\n", sess->sess_cid,
203: sess->sess_addr, sess->sess_user);
1.1.2.18 misho 204:
205: pthread_attr_destroy(&attr);
1.1.2.15 misho 206: return NULL;
1.1.2.6 misho 207: end: /* close client connection */
1.1.2.18 misho 208: ret = mqtt_msgCONNACK(&msg, ret);
209: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20 misho 210: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 211: } else {
1.1.2.20 misho 212: ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19 misho 213: free(msg.msg_base);
214: msg.msg_len = 0;
1.1.2.18 misho 215: }
216:
1.1.2.20 misho 217: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
1.1.2.15 misho 218: finiSession(sess);
1.1.2.6 misho 219: return NULL;
1.1.2.4 misho 220: }
221:
222: /* ----------------------------------------------------------------------- */
223:
1.1.2.5 misho 224: static void *
225: thrSched(void *arg __unused)
226: {
1.1.2.18 misho 227: struct tagSession *sess;
1.1.2.5 misho 228:
1.1.2.20 misho 229: ioTRACE(1);
230:
231: schedRun(root, &Kill);
1.1.2.18 misho 232:
233: TAILQ_FOREACH(sess, &Sessions, sess_node)
234: if (sess->sess_tid)
235: pthread_cancel(sess->sess_tid);
1.1.2.5 misho 236: pthread_exit(NULL);
237: }
238:
1.1.2.2 misho 239: int
1.1.2.3 misho 240: Run(int sock)
1.1.2.2 misho 241: {
1.1.2.4 misho 242: io_sockaddr_t sa;
243: socklen_t sslen = sizeof sa.ss;
244: int cli;
1.1.2.5 misho 245: pthread_t tid;
1.1.2.18 misho 246: ait_val_t *v;
1.1.2.23 misho 247: char str[STRSIZ];
1.1.2.4 misho 248:
1.1.2.20 misho 249: ioTRACE(1);
1.1.2.2 misho 250:
1.1.2.5 misho 251: if (pthread_create(&tid, NULL, thrSched, NULL)) {
1.1.2.20 misho 252: ioLOGERR(0);
1.1.2.5 misho 253: return -1;
254: } else
255: pthread_detach(tid);
1.1.2.20 misho 256: ioDEBUG(2, "Run scheduler management thread");
1.1.2.5 misho 257:
1.1.2.24 misho 258: if (listen(sock, SOMAXCONN) == -1) {
1.1.2.20 misho 259: ioLOGERR(0);
1.1.2.3 misho 260: return -1;
261: }
262:
1.1.2.4 misho 263: while (!Kill) {
264: if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
1.1.2.20 misho 265: if (!Kill)
266: continue;
267: ioLOGERR(0);
268: break;
1.1.2.18 misho 269: }
270: v = io_allocVar();
271: if (!v) {
272: ioLIBERR(mqtt);
273: break;
1.1.2.23 misho 274: } else {
275: memset(str, 0, sizeof str);
276: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
277: AIT_SET_STR(v, str);
278: }
279: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
1.1.2.4 misho 280:
1.1.2.18 misho 281: if (!schedRead(root, startSession, v, cli)) {
282: io_freeVar(v);
1.1.2.4 misho 283: close(cli);
1.1.2.20 misho 284: ioDEBUG(1, "Terminated client with socket=%d", cli);
1.1.2.4 misho 285: }
286: }
287:
1.1.2.2 misho 288: return 0;
289: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>