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