Annotation of mqtt/src/daemon.c, revision 1.2.2.26
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) {
156: if (len == -1)
157: ioLIBERR(mqtt);
1.2.2.26! misho 158: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.25 misho 159: finiSession(sess);
1.2.2.24 misho 160: } else
161: ret -= len;
1.2.2.25 misho 162: } while (len > 0 && ret > 0);
1.2.2.23 misho 163:
1.2.2.25 misho 164: if (len >= 0 && !schedRead(root, dispatchSession, TASK_ARG(task), TASK_FD(task), NULL, 0)) {
1.2.2.23 misho 165: ioLIBERR(sched);
1.2.2.26! misho 166: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.25 misho 167: finiSession(sess);
168: }
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:
1.2.2.25 misho 257: /* Start session task OK ... */
258: if (!schedRead(root, dispatchSession, sess, TASK_FD(task), NULL, 0)) {
1.2.2.23 misho 259: ioLIBERR(sched);
1.2.2.25 misho 260: ret = MQTT_RETCODE_DENIED;
1.2.2.26! misho 261: } else
! 262: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.2.2.23 misho 263:
1.2 misho 264: call.LOG(logg, "Session %s started from %s for user %s (timeout=%d) OK!\n", sess->sess_cid,
265: sess->sess_addr, sess->sess_user, sess->sess_ka);
266: end: /* close client connection */
1.2.2.23 misho 267: wlen = mqtt_msgCONNACK(sess->sess_buf, ret);
268: if ((wlen = send(TASK_FD(task), sess->sess_buf->msg_base, wlen, 0)) == -1) {
269: ioDEBUG(3, "Error:: send(%d) #%d - %s", TASK_FD(task), errno, strerror(errno));
270: } else
271: ioDEBUG(5, "Sended %d bytes", wlen);
1.2 misho 272:
1.2.2.23 misho 273: if (ret != MQTT_RETCODE_ACCEPTED) {
274: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, TASK_FD(task));
275: finiSession(sess);
276: }
1.2 misho 277: return NULL;
278: }
279:
280: static void *
1.2.2.9 misho 281: acceptClient(sched_task_t *task)
1.2 misho 282: {
1.2.2.9 misho 283: int cli;
284: io_sockaddr_t sa;
285: socklen_t sslen = sizeof sa.ss;
286: ait_val_t *v;
287: char str[STRSIZ];
1.2 misho 288:
1.2.2.9 misho 289: ioTRACE(4);
1.2 misho 290:
1.2.2.9 misho 291: assert(task);
1.2 misho 292:
1.2.2.9 misho 293: if ((cli = accept(TASK_FD(task), &sa.sa, &sslen)) == -1)
294: goto end;
295: else
1.2.2.21 misho 296: fcntl(cli, F_SETFL, fcntl(cli, F_GETFL, 0) | O_NONBLOCK);
1.2.2.9 misho 297:
298: v = io_allocVar();
299: if (!v) {
300: ioLIBERR(io);
301: close(cli);
302: goto end;
303: } else {
304: memset(str, 0, sizeof str);
305: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
1.2.2.17 misho 306: AIT_FREE_VAL(v);
1.2.2.9 misho 307: AIT_SET_STR(v, str);
308: }
309: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
310:
311: if (!schedRead(root, startSession, v, cli, NULL, 0)) {
312: io_freeVar(v);
313: close(cli);
314: ioDEBUG(1, "Terminated client with socket=%d", cli);
315: }
316: end:
1.2.2.22 misho 317: if (!schedRead(root, acceptClient, NULL, TASK_FD(task), NULL, 0))
1.2.2.11 misho 318: ioLIBERR(sched);
1.2.2.9 misho 319: return NULL;
1.2 misho 320: }
321:
1.2.2.9 misho 322: /* ----------------------------------------------------------------------- */
323:
1.2 misho 324: int
325: Run(int sock)
326: {
1.2.2.9 misho 327: struct tagPub *pub;
1.2.2.25 misho 328: struct tagSession *sess;
1.2.2.9 misho 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: TAILQ_FOREACH(pub, &Pubs, pub_node) {
350: TAILQ_REMOVE(&Pubs, pub, pub_node);
351:
352: AIT_FREE_VAL(&pub->pub_name);
353: if (pub->pub_packet.msg_base)
354: free(pub->pub_packet.msg_base);
1.2 misho 355: }
1.2.2.25 misho 356: TAILQ_FOREACH(sess, &Sessions, sess_node) {
357: TAILQ_REMOVE(&Sessions, sess, sess_node);
1.2.2.26! misho 358:
1.2.2.25 misho 359: finiSession(sess);
360: }
1.2 misho 361: return 0;
362: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>