Annotation of mqtt/src/daemon.c, revision 1.2.2.24
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 inline struct tagSession *
9: initSession(int sock, ait_val_t * __restrict v)
10: {
11: struct tagSession *sess = NULL;
12: const char *str;
13:
14: ioTRACE(5);
15:
16: if (!v)
17: return NULL;
18:
19: sess = malloc(sizeof(struct tagSession));
20: if (!sess) {
1.2.2.9 misho 21: ioSYSERR(0);
1.2 misho 22: return NULL;
23: } else
24: memset(sess, 0, sizeof(struct tagSession));
25:
1.2.2.4 misho 26: pthread_mutex_init(&sess->sess_mtx, NULL);
27:
1.2.2.9 misho 28: SLIST_INIT(&sess->sess_subscr);
1.2 misho 29:
1.2.2.5 misho 30: str = cfg_getAttribute(&cfg, "mqttd", "retry");
1.2 misho 31: if (!str)
32: sess->sess_retry = DEFAULT_RETRY;
33: else
34: sess->sess_retry = strtol(str, NULL, 0);
35:
36: sess->sess_buf = mqtt_msgAlloc(USHRT_MAX);
37: if (!sess->sess_buf) {
1.2.2.9 misho 38: ioLIBERR(mqtt);
1.2 misho 39: free(sess);
40: return NULL;
41: }
42:
1.2.2.8 misho 43: /* init server actor */
1.2.2.2 misho 44: sess->sess_srv = mqtt_srv_Init(sock, sess->sess_buf);
45: if (!sess->sess_srv) {
46: ioDEBUG(3, "Error:: in srv_Init #%d - %s", mqtt_GetErrno(), mqtt_GetError());
47: mqtt_msgFree(&sess->sess_buf, 42);
48: free(sess);
49: return NULL;
1.2.2.3 misho 50: } else {
51: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_CONNECT, cmdCONNECT);
52: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PUBLISH, cmdPUBLISH);
53: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PUBREL, cmdPUBREL);
54: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_SUBSCRIBE, cmdSUBSCRIBE);
55: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_UNSUBSCRIBE, cmdUNSUBSCRIBE);
56: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_PINGREQ, cmdPINGREQ);
57: mqtt_srv_setCmd(sess->sess_srv, MQTT_TYPE_DISCONNECT, cmdDISCONNECT);
1.2.2.2 misho 58: }
59:
1.2 misho 60: sess->sess_sock = sock;
61: strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr);
62: return sess;
63: }
64:
1.2.2.24! misho 65: void
1.2.2.9 misho 66: finiSession(struct tagSession *sess)
1.2 misho 67: {
68: struct tagStore *store;
69:
70: ioTRACE(5);
71:
72: if (!sess)
73: return;
74:
75: if (call.FiniSessPUB)
76: call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%");
77:
1.2.2.6 misho 78: SESS_ELEM_LOCK(sess);
1.2.2.9 misho 79: while ((store = SLIST_FIRST(&sess->sess_subscr))) {
80: SLIST_REMOVE_HEAD(&sess->sess_subscr, st_node);
81:
82: if (store->st_subscr.sub_topic.msg_base)
83: free(store->st_subscr.sub_topic.msg_base);
84: if (store->st_subscr.sub_value.msg_base)
85: free(store->st_subscr.sub_value.msg_base);
86:
87: free(store);
88: }
1.2.2.6 misho 89: SESS_ELEM_UNLOCK(sess);
1.2.2.4 misho 90: pthread_mutex_destroy(&sess->sess_mtx);
1.2 misho 91:
92: if (sess->sess_will.msg)
93: free(sess->sess_will.msg);
94: if (sess->sess_will.topic)
95: free(sess->sess_will.topic);
96:
1.2.2.9 misho 97: if (sess->sess_sock > STDERR_FILENO)
1.2 misho 98: srv_Close(sess->sess_sock);
99:
1.2.2.2 misho 100: mqtt_srv_Fini(&sess->sess_srv);
1.2 misho 101: mqtt_msgFree(&sess->sess_buf, 42);
102:
103: free(sess);
104: }
105:
1.2.2.23 misho 106: static void *
107: leaveClient(sched_task_t *task)
1.2 misho 108: {
1.2.2.23 misho 109: struct tagSession *sess;
1.2 misho 110: int ret;
111:
112: ioTRACE(4);
113:
1.2.2.23 misho 114: assert(task);
115:
116: sess = TASK_ARG(task);
1.2 misho 117: assert(sess);
118:
1.2.2.6 misho 119: SESS_LOCK;
1.2 misho 120: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.6 misho 121: SESS_UNLOCK;
1.2 misho 122:
1.2.2.23 misho 123: ret = mqtt_msgDISCONNECT(sess->sess_buf);
124: send(TASK_FD(task), sess->sess_buf->msg_base, ret, MSG_NOSIGNAL);
1.2 misho 125:
1.2.2.23 misho 126: ioDEBUG(1, "Close socket=%d", TASK_FD(task));
1.2 misho 127: call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid,
128: sess->sess_addr, sess->sess_user);
1.2.2.20 misho 129:
130: finiSession(sess);
1.2.2.23 misho 131: return NULL;
1.2 misho 132: }
133:
134: static void *
1.2.2.23 misho 135: dispatchSession(sched_task_t *task)
1.2 misho 136: {
1.2.2.24! misho 137: int ret, len = 0;
1.2.2.23 misho 138: struct tagSession *sess;
1.2 misho 139:
140: ioTRACE(2);
141:
1.2.2.23 misho 142: assert(task);
1.2 misho 143:
1.2.2.23 misho 144: sess = TASK_ARG(task);
145: assert(sess);
146:
147: /* receive & decode packet */
148: if ((ret = recv(TASK_FD(task), sess->sess_buf->msg_base, sess->sess_buf->msg_len, 0)) == -1) {
149: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
150: finiSession(sess);
151: return NULL;
152: } else if (!ret) {
153: ioDEBUG(4, "Session %s EOF received.", sess->sess_cid);
154: finiSession(sess);
155: return NULL;
1.2.2.24! misho 156: }
1.2.2.23 misho 157:
1.2.2.24! misho 158: do {
! 159: /* dispatch message type */
! 160: if ((len = mqtt_srv_Dispatch(sess->sess_srv, ret, sess)) == -1) {
! 161: ioLIBERR(mqtt);
! 162: ret = 0;
! 163: } else
! 164: ret -= len;
! 165: } while (len && ret > 0);
1.2.2.23 misho 166:
167: if (!schedRead(root, dispatchSession, TASK_ARG(task), TASK_FD(task), NULL, 0))
168: ioLIBERR(sched);
1.2.2.11 misho 169: return NULL;
1.2 misho 170: }
171:
1.2.2.24! misho 172: void *
1.2 misho 173: startSession(sched_task_t *task)
174: {
175: u_char basebuf[USHRT_MAX];
1.2.2.23 misho 176: mqtt_msg_t buf = { basebuf, sizeof basebuf };
1.2 misho 177: mqtthdr_connflgs_t flg;
178: mqtthdr_connack_t cack;
179: struct tagSession *s, *sess = NULL;
1.2.2.23 misho 180: int ret, wlen;
1.2 misho 181:
182: ioTRACE(4);
183:
184: assert(task);
185:
186: if (!TASK_DATA(task)) {
1.2.2.8 misho 187: /* flow from accept new clients */
1.2 misho 188: sess = initSession(TASK_FD(task), TASK_ARG(task));
1.2.2.17 misho 189: io_freeVar(TASK_ARG(task));
1.2 misho 190: if (!sess) {
191: close(TASK_FD(task));
192: return NULL;
193: }
194:
195: /* receive & decode packet */
196: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
197: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.2.2.9 misho 198: finiSession(sess);
1.2 misho 199: return NULL;
200: }
201: } else {
202: sess = TASK_DATA(task);
1.2.2.23 misho 203: assert(sess);
204:
1.2 misho 205: buf.msg_len = TASK_DATLEN(task) > sizeof basebuf ? sizeof basebuf : TASK_DATLEN(task);
206: memcpy(buf.msg_base, sess->sess_buf->msg_base, buf.msg_len);
207: }
208:
209: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
210: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
211: &sess->sess_will.topic, &sess->sess_will.msg);
212: ret = cack.retcode;
213: flg.flags = cack.reserved;
214: if (flg.reserved) {
215: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
216: goto end;
217: } else {
218: sess->sess_clean = flg.clean_sess;
219: sess->sess_will.qos = flg.will_qos;
220: sess->sess_will.retain = flg.will_retain;
221: sess->sess_will.flag = flg.will_flg;
222: }
223:
224: /* check online table for user */
225: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
226: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
227: sess->sess_user, sess->sess_pass);
228: ret = MQTT_RETCODE_DENIED;
229: goto end;
230: } else {
231: ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
232: ret = MQTT_RETCODE_ACCEPTED;
233: }
1.2.2.12 misho 234:
1.2 misho 235: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
236: ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
237: TAILQ_FOREACH(s, &Sessions, sess_node)
238: if (!strcmp(s->sess_cid, sess->sess_cid)) {
239: /* found stale session & disconnect it! */
1.2.2.23 misho 240: schedWrite(root, leaveClient, sess, TASK_FD(task), NULL, 0);
1.2 misho 241: break;
242: }
243: }
1.2.2.12 misho 244:
1.2 misho 245: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
246: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
247: sess->sess_will.qos, sess->sess_will.retain) == -1) {
248: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
249: ret = MQTT_RETCODE_DENIED;
250: goto end;
251: } else {
252: ioDEBUG(0, "Session %s from %s and username %s is started",
253: sess->sess_cid, sess->sess_addr, sess->sess_user);
254: ret = MQTT_RETCODE_ACCEPTED;
255: }
256:
257: /* Start session thread OK ... */
1.2.2.6 misho 258: SESS_LOCK;
1.2 misho 259: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.2.2.6 misho 260: SESS_UNLOCK;
1.2 misho 261:
1.2.2.23 misho 262: if (!schedRead(root, dispatchSession, sess, TASK_FD(task), NULL, 0))
263: ioLIBERR(sched);
264:
1.2 misho 265: call.LOG(logg, "Session %s started from %s for user %s (timeout=%d) OK!\n", sess->sess_cid,
266: sess->sess_addr, sess->sess_user, sess->sess_ka);
267: end: /* close client connection */
1.2.2.23 misho 268: wlen = mqtt_msgCONNACK(sess->sess_buf, ret);
269: if ((wlen = send(TASK_FD(task), sess->sess_buf->msg_base, wlen, 0)) == -1) {
270: ioDEBUG(3, "Error:: send(%d) #%d - %s", TASK_FD(task), errno, strerror(errno));
271: } else
272: ioDEBUG(5, "Sended %d bytes", wlen);
1.2 misho 273:
1.2.2.23 misho 274: if (ret != MQTT_RETCODE_ACCEPTED) {
275: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, TASK_FD(task));
276: finiSession(sess);
277: }
1.2 misho 278: return NULL;
279: }
280:
281: static void *
1.2.2.9 misho 282: acceptClient(sched_task_t *task)
1.2 misho 283: {
1.2.2.9 misho 284: int cli;
285: io_sockaddr_t sa;
286: socklen_t sslen = sizeof sa.ss;
287: ait_val_t *v;
288: char str[STRSIZ];
1.2 misho 289:
1.2.2.9 misho 290: ioTRACE(4);
1.2 misho 291:
1.2.2.9 misho 292: assert(task);
1.2 misho 293:
1.2.2.9 misho 294: if ((cli = accept(TASK_FD(task), &sa.sa, &sslen)) == -1)
295: goto end;
296: else
1.2.2.21 misho 297: fcntl(cli, F_SETFL, fcntl(cli, F_GETFL, 0) | O_NONBLOCK);
1.2.2.9 misho 298:
299: v = io_allocVar();
300: if (!v) {
301: ioLIBERR(io);
302: close(cli);
303: goto end;
304: } else {
305: memset(str, 0, sizeof str);
306: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
1.2.2.17 misho 307: AIT_FREE_VAL(v);
1.2.2.9 misho 308: AIT_SET_STR(v, str);
309: }
310: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
311:
312: if (!schedRead(root, startSession, v, cli, NULL, 0)) {
313: io_freeVar(v);
314: close(cli);
315: ioDEBUG(1, "Terminated client with socket=%d", cli);
316: }
317: end:
1.2.2.22 misho 318: if (!schedRead(root, acceptClient, NULL, TASK_FD(task), NULL, 0))
1.2.2.11 misho 319: ioLIBERR(sched);
1.2.2.9 misho 320: return NULL;
1.2 misho 321: }
322:
1.2.2.9 misho 323: /* ----------------------------------------------------------------------- */
324:
1.2 misho 325: int
326: Run(int sock)
327: {
1.2.2.9 misho 328: struct tagPub *pub;
329: struct timespec pl = { 0, 100000000 };
1.2 misho 330:
331: ioTRACE(1);
332:
1.2.2.9 misho 333: if (listen(sock, SOMAXCONN) == -1) {
1.2 misho 334: ioSYSERR(0);
335: return -1;
336: } else
1.2.2.9 misho 337: fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
1.2 misho 338:
1.2.2.9 misho 339: /* state machine - accept new connections */
340: if (!schedRead(root, acceptClient, NULL, sock, NULL, 0)) {
341: ioLIBERR(sched);
1.2 misho 342: return -1;
343: }
344:
1.2.2.9 misho 345: schedPolling(root, &pl, NULL);
346: schedRun(root, &Kill);
1.2 misho 347:
1.2.2.9 misho 348: /* free all undeleted elements into lists */
349: PUBS_LOCK;
350: TAILQ_FOREACH(pub, &Pubs, pub_node) {
351: TAILQ_REMOVE(&Pubs, pub, pub_node);
352:
353: AIT_FREE_VAL(&pub->pub_name);
354: if (pub->pub_packet.msg_base)
355: free(pub->pub_packet.msg_base);
1.2 misho 356: }
1.2.2.9 misho 357: PUBS_UNLOCK;
1.2 misho 358: return 0;
359: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>