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