Annotation of mqtt/src/daemon.c, revision 1.1.2.24
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)) {
! 165: stopSession(s);
! 166: break;
! 167: }
1.1.2.23 misho 168: }
169: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
170: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
171: sess->sess_will.qos, sess->sess_will.retain) == -1) {
172: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
173: ret = MQTT_RETCODE_DENIED;
174: goto end;
175: } else {
176: ioDEBUG(0, "Session %s from %s and username %s is started",
177: sess->sess_cid, sess->sess_addr, sess->sess_user);
178: ret = MQTT_RETCODE_ACCEPTED;
179: }
1.1.2.6 misho 180:
1.1.2.18 misho 181: ret = mqtt_msgCONNACK(&msg, ret);
182: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20 misho 183: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 184: finiSession(sess);
185: return NULL;
186: } else {
1.1.2.20 misho 187: ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19 misho 188: free(msg.msg_base);
189: msg.msg_len = 0;
1.1.2.18 misho 190: }
191:
1.1.2.15 misho 192: /* Start session thread OK ... */
1.1.2.18 misho 193: pthread_attr_init(&attr);
194: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
195:
196: pthread_mutex_lock(&mtx_sess);
1.1.2.15 misho 197: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.1.2.18 misho 198: pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
199: pthread_mutex_unlock(&mtx_sess);
200:
1.1.2.23 misho 201: call.LOG(logg, "Session %s started from %s for user %s OK!\n", sess->sess_cid,
202: sess->sess_addr, sess->sess_user);
1.1.2.18 misho 203:
204: pthread_attr_destroy(&attr);
1.1.2.15 misho 205: return NULL;
1.1.2.6 misho 206: end: /* close client connection */
1.1.2.18 misho 207: ret = mqtt_msgCONNACK(&msg, ret);
208: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20 misho 209: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18 misho 210: } else {
1.1.2.20 misho 211: ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19 misho 212: free(msg.msg_base);
213: msg.msg_len = 0;
1.1.2.18 misho 214: }
215:
1.1.2.20 misho 216: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
1.1.2.15 misho 217: finiSession(sess);
1.1.2.6 misho 218: return NULL;
1.1.2.4 misho 219: }
220:
221: /* ----------------------------------------------------------------------- */
222:
1.1.2.5 misho 223: static void *
224: thrSched(void *arg __unused)
225: {
1.1.2.18 misho 226: struct tagSession *sess;
1.1.2.5 misho 227:
1.1.2.20 misho 228: ioTRACE(1);
229:
230: schedRun(root, &Kill);
1.1.2.18 misho 231:
232: TAILQ_FOREACH(sess, &Sessions, sess_node)
233: if (sess->sess_tid)
234: pthread_cancel(sess->sess_tid);
1.1.2.5 misho 235: pthread_exit(NULL);
236: }
237:
1.1.2.2 misho 238: int
1.1.2.3 misho 239: Run(int sock)
1.1.2.2 misho 240: {
1.1.2.4 misho 241: io_sockaddr_t sa;
242: socklen_t sslen = sizeof sa.ss;
243: int cli;
1.1.2.5 misho 244: pthread_t tid;
1.1.2.18 misho 245: ait_val_t *v;
1.1.2.23 misho 246: char str[STRSIZ];
1.1.2.4 misho 247:
1.1.2.20 misho 248: ioTRACE(1);
1.1.2.2 misho 249:
1.1.2.5 misho 250: if (pthread_create(&tid, NULL, thrSched, NULL)) {
1.1.2.20 misho 251: ioLOGERR(0);
1.1.2.5 misho 252: return -1;
253: } else
254: pthread_detach(tid);
1.1.2.20 misho 255: ioDEBUG(2, "Run scheduler management thread");
1.1.2.5 misho 256:
1.1.2.24! misho 257: if (listen(sock, SOMAXCONN) == -1) {
1.1.2.20 misho 258: ioLOGERR(0);
1.1.2.3 misho 259: return -1;
260: }
261:
1.1.2.4 misho 262: while (!Kill) {
263: if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
1.1.2.20 misho 264: if (!Kill)
265: continue;
266: ioLOGERR(0);
267: break;
1.1.2.18 misho 268: }
269: v = io_allocVar();
270: if (!v) {
271: ioLIBERR(mqtt);
272: break;
1.1.2.23 misho 273: } else {
274: memset(str, 0, sizeof str);
275: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
276: AIT_SET_STR(v, str);
277: }
278: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
1.1.2.4 misho 279:
1.1.2.18 misho 280: if (!schedRead(root, startSession, v, cli)) {
281: io_freeVar(v);
1.1.2.4 misho 282: close(cli);
1.1.2.20 misho 283: ioDEBUG(1, "Terminated client with socket=%d", cli);
1.1.2.4 misho 284: }
285: }
286:
1.1.2.2 misho 287: return 0;
288: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>