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