Annotation of mqtt/src/daemon.c, revision 1.2.2.25
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));
143: finiSession(sess);
144: return NULL;
145: } else if (!ret) {
146: ioDEBUG(4, "Session %s EOF received.", sess->sess_cid);
147: finiSession(sess);
148: return NULL;
1.2.2.24 misho 149: }
1.2.2.23 misho 150:
1.2.2.24 misho 151: do {
152: /* dispatch message type */
1.2.2.25! misho 153: if ((len = mqtt_srv_Dispatch(sess->sess_srv, ret, sess)) < 0) {
! 154: if (len == -1)
! 155: ioLIBERR(mqtt);
! 156: finiSession(sess);
1.2.2.24 misho 157: } else
158: ret -= len;
1.2.2.25! misho 159: } while (len > 0 && ret > 0);
1.2.2.23 misho 160:
1.2.2.25! misho 161: if (len >= 0 && !schedRead(root, dispatchSession, TASK_ARG(task), TASK_FD(task), NULL, 0)) {
1.2.2.23 misho 162: ioLIBERR(sched);
1.2.2.25! misho 163: finiSession(sess);
! 164: }
1.2.2.11 misho 165: return NULL;
1.2 misho 166: }
167:
1.2.2.24 misho 168: void *
1.2 misho 169: startSession(sched_task_t *task)
170: {
171: u_char basebuf[USHRT_MAX];
1.2.2.23 misho 172: mqtt_msg_t buf = { basebuf, sizeof basebuf };
1.2 misho 173: mqtthdr_connflgs_t flg;
174: mqtthdr_connack_t cack;
175: struct tagSession *s, *sess = NULL;
1.2.2.23 misho 176: int ret, wlen;
1.2 misho 177:
178: ioTRACE(4);
179:
180: assert(task);
181:
182: if (!TASK_DATA(task)) {
1.2.2.8 misho 183: /* flow from accept new clients */
1.2 misho 184: sess = initSession(TASK_FD(task), TASK_ARG(task));
1.2.2.17 misho 185: io_freeVar(TASK_ARG(task));
1.2 misho 186: if (!sess) {
187: close(TASK_FD(task));
188: return NULL;
189: }
190:
191: /* receive & decode packet */
192: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
193: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.2.2.9 misho 194: finiSession(sess);
1.2 misho 195: return NULL;
196: }
197: } else {
198: sess = TASK_DATA(task);
1.2.2.23 misho 199: assert(sess);
200:
1.2 misho 201: buf.msg_len = TASK_DATLEN(task) > sizeof basebuf ? sizeof basebuf : TASK_DATLEN(task);
202: memcpy(buf.msg_base, sess->sess_buf->msg_base, buf.msg_len);
203: }
204:
205: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
206: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
207: &sess->sess_will.topic, &sess->sess_will.msg);
208: ret = cack.retcode;
209: flg.flags = cack.reserved;
210: if (flg.reserved) {
211: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
212: goto end;
213: } else {
214: sess->sess_clean = flg.clean_sess;
215: sess->sess_will.qos = flg.will_qos;
216: sess->sess_will.retain = flg.will_retain;
217: sess->sess_will.flag = flg.will_flg;
218: }
219:
220: /* check online table for user */
221: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
222: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
223: sess->sess_user, sess->sess_pass);
224: ret = MQTT_RETCODE_DENIED;
225: goto end;
226: } else {
227: ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
228: ret = MQTT_RETCODE_ACCEPTED;
229: }
1.2.2.12 misho 230:
1.2 misho 231: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
232: ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
233: TAILQ_FOREACH(s, &Sessions, sess_node)
234: if (!strcmp(s->sess_cid, sess->sess_cid)) {
235: /* found stale session & disconnect it! */
1.2.2.23 misho 236: schedWrite(root, leaveClient, sess, TASK_FD(task), NULL, 0);
1.2 misho 237: break;
238: }
239: }
1.2.2.12 misho 240:
1.2 misho 241: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
242: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
243: sess->sess_will.qos, sess->sess_will.retain) == -1) {
244: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
245: ret = MQTT_RETCODE_DENIED;
246: goto end;
247: } else {
248: ioDEBUG(0, "Session %s from %s and username %s is started",
249: sess->sess_cid, sess->sess_addr, sess->sess_user);
250: ret = MQTT_RETCODE_ACCEPTED;
251: }
252:
1.2.2.25! misho 253: /* Start session task OK ... */
1.2 misho 254: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
255:
1.2.2.25! misho 256: if (!schedRead(root, dispatchSession, sess, TASK_FD(task), NULL, 0)) {
1.2.2.23 misho 257: ioLIBERR(sched);
1.2.2.25! misho 258: ret = MQTT_RETCODE_DENIED;
! 259: }
1.2.2.23 misho 260:
1.2 misho 261: call.LOG(logg, "Session %s started from %s for user %s (timeout=%d) OK!\n", sess->sess_cid,
262: sess->sess_addr, sess->sess_user, sess->sess_ka);
263: end: /* close client connection */
1.2.2.23 misho 264: wlen = mqtt_msgCONNACK(sess->sess_buf, ret);
265: if ((wlen = send(TASK_FD(task), sess->sess_buf->msg_base, wlen, 0)) == -1) {
266: ioDEBUG(3, "Error:: send(%d) #%d - %s", TASK_FD(task), errno, strerror(errno));
267: } else
268: ioDEBUG(5, "Sended %d bytes", wlen);
1.2 misho 269:
1.2.2.23 misho 270: if (ret != MQTT_RETCODE_ACCEPTED) {
271: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, TASK_FD(task));
272: finiSession(sess);
273: }
1.2 misho 274: return NULL;
275: }
276:
277: static void *
1.2.2.9 misho 278: acceptClient(sched_task_t *task)
1.2 misho 279: {
1.2.2.9 misho 280: int cli;
281: io_sockaddr_t sa;
282: socklen_t sslen = sizeof sa.ss;
283: ait_val_t *v;
284: char str[STRSIZ];
1.2 misho 285:
1.2.2.9 misho 286: ioTRACE(4);
1.2 misho 287:
1.2.2.9 misho 288: assert(task);
1.2 misho 289:
1.2.2.9 misho 290: if ((cli = accept(TASK_FD(task), &sa.sa, &sslen)) == -1)
291: goto end;
292: else
1.2.2.21 misho 293: fcntl(cli, F_SETFL, fcntl(cli, F_GETFL, 0) | O_NONBLOCK);
1.2.2.9 misho 294:
295: v = io_allocVar();
296: if (!v) {
297: ioLIBERR(io);
298: close(cli);
299: goto end;
300: } else {
301: memset(str, 0, sizeof str);
302: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
1.2.2.17 misho 303: AIT_FREE_VAL(v);
1.2.2.9 misho 304: AIT_SET_STR(v, str);
305: }
306: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
307:
308: if (!schedRead(root, startSession, v, cli, NULL, 0)) {
309: io_freeVar(v);
310: close(cli);
311: ioDEBUG(1, "Terminated client with socket=%d", cli);
312: }
313: end:
1.2.2.22 misho 314: if (!schedRead(root, acceptClient, NULL, TASK_FD(task), NULL, 0))
1.2.2.11 misho 315: ioLIBERR(sched);
1.2.2.9 misho 316: return NULL;
1.2 misho 317: }
318:
1.2.2.9 misho 319: /* ----------------------------------------------------------------------- */
320:
1.2 misho 321: int
322: Run(int sock)
323: {
1.2.2.9 misho 324: struct tagPub *pub;
1.2.2.25! misho 325: struct tagSession *sess;
1.2.2.9 misho 326: struct timespec pl = { 0, 100000000 };
1.2 misho 327:
328: ioTRACE(1);
329:
1.2.2.9 misho 330: if (listen(sock, SOMAXCONN) == -1) {
1.2 misho 331: ioSYSERR(0);
332: return -1;
333: } else
1.2.2.9 misho 334: fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
1.2 misho 335:
1.2.2.9 misho 336: /* state machine - accept new connections */
337: if (!schedRead(root, acceptClient, NULL, sock, NULL, 0)) {
338: ioLIBERR(sched);
1.2 misho 339: return -1;
340: }
341:
1.2.2.9 misho 342: schedPolling(root, &pl, NULL);
343: schedRun(root, &Kill);
1.2 misho 344:
1.2.2.9 misho 345: /* free all undeleted elements into lists */
346: TAILQ_FOREACH(pub, &Pubs, pub_node) {
347: TAILQ_REMOVE(&Pubs, pub, pub_node);
348:
349: AIT_FREE_VAL(&pub->pub_name);
350: if (pub->pub_packet.msg_base)
351: free(pub->pub_packet.msg_base);
1.2 misho 352: }
1.2.2.25! misho 353: TAILQ_FOREACH(sess, &Sessions, sess_node) {
! 354: TAILQ_REMOVE(&Sessions, sess, sess_node);
! 355: finiSession(sess);
! 356: }
1.2 misho 357: return 0;
358: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>