1: #include "global.h"
2: #include "rtlm.h"
3: #include "utils.h"
4: #include "mqttd.h"
5:
6:
7: static inline struct tagSession *
8: initSession(int sock, ait_val_t * __restrict v)
9: {
10: struct tagSession *sess = NULL;
11: const char *str;
12:
13: ioTRACE(5);
14:
15: if (!v)
16: return NULL;
17:
18: sess = malloc(sizeof(struct tagSession));
19: if (!sess) {
20: ioDEBUG(3, "Error:: in malloc #%d - %s", errno, strerror(errno));
21: io_freeVar(v);
22: return NULL;
23: } else
24: memset(sess, 0, sizeof(struct tagSession));
25:
26: str = (const char*) cfg_GetAttribute(&cfg, CFG("mqttd"), CFG("retry"));
27: if (!str)
28: sess->sess_retry = DEFAULT_RETRY;
29: else
30: sess->sess_retry = strtol(str, NULL, 0);
31:
32: sess->sess_buf = mqtt_msgAlloc(0);
33: if (!sess->sess_buf) {
34: ioDEBUG(3, "Error:: in msgAlloc #%d - %s", mqtt_GetErrno(), mqtt_GetError());
35: free(sess);
36: io_freeVar(v);
37: return NULL;
38: }
39:
40: sess->sess_sock = sock;
41: strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr);
42: io_freeVar(v);
43: return sess;
44: }
45:
46: static void
47: finiSession(struct tagSession *sess)
48: {
49: register int i;
50: struct tagStore *store;
51:
52: ioTRACE(5);
53:
54: if (!sess)
55: return;
56:
57: for (i = 0; i < MQTT_QOS_RESERVED; i++)
58: while ((store = SLIST_FIRST(&sess->sess_store[i]))) {
59: SLIST_REMOVE_HEAD(&sess->sess_store[i], st_node);
60: free(store);
61: }
62:
63: if (sess->sess_will.msg)
64: free(sess->sess_will.msg);
65: if (sess->sess_will.topic)
66: free(sess->sess_will.topic);
67:
68: if (sess->sess_sock > STDERR_FILENO)
69: srv_Close(sess->sess_sock);
70:
71: mqtt_msgFree(&sess->sess_buf, 42);
72:
73: free(sess);
74: }
75:
76: static void
77: stopSession(struct tagSession *sess)
78: {
79: mqtt_msg_t msg = { NULL, 0 };
80: int ret;
81:
82: ioTRACE(4);
83:
84: assert(sess);
85:
86: pthread_mutex_lock(&mtx_sess);
87: TAILQ_REMOVE(&Sessions, sess, sess_node);
88: pthread_mutex_unlock(&mtx_sess);
89:
90: ret = mqtt_msgDISCONNECT(&msg);
91: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1)
92: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
93: else {
94: ioDEBUG(5, "Sended %d bytes for disconnect", ret);
95: free(msg.msg_base);
96: memset(&msg, 0, sizeof msg);
97: }
98:
99: ioDEBUG(1, "Close socket=%d", sess->sess_sock);
100: finiSession(sess);
101:
102: call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid,
103: sess->sess_addr, sess->sess_user);
104: }
105:
106: static int
107: KASession(struct tagSession *sess)
108: {
109: u_char basebuf[USHRT_MAX];
110: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
111: int ret;
112: struct pollfd pfd;
113:
114: ioTRACE(4);
115:
116: assert(sess);
117:
118: /* ping request */
119: ret = mqtt_msgPINGREQ(&msg);
120: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
121: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
122: return -1;
123: } else {
124: ioDEBUG(5, "Sended %d bytes for ping request", ret);
125: free(msg.msg_base);
126: memset(&msg, 0, sizeof msg);
127: }
128:
129: pfd.fd = sess->sess_sock;
130: pfd.events = POLLIN | POLLPRI;
131: if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1 ||
132: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
133: ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
134: return -1;
135: } else if (!ret) {
136: /* problem!!! session is abandoned ... must be disconnect! */
137: return 1;
138: }
139: /* receive & decode packet */
140: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
141: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
142: return -1;
143: }
144: if (mqtt_readPINGRESP(&buf)) {
145: /* problem!!! session is broken, not hear ping response ... must be disconnect! */
146: return 2;
147: }
148:
149: /* Keep Alive is OK! */
150: return 0;
151: }
152:
153: static void *
154: thrSession(struct tagSession *sess)
155: {
156: u_char basebuf[USHRT_MAX];
157: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
158: int ret;
159: struct pollfd pfd;
160: struct mqtthdr *hdr;
161:
162: pthread_cleanup_push((void(*)(void*)) stopSession, sess);
163: ioTRACE(2);
164:
165: pfd.fd = sess->sess_sock;
166: pfd.events = POLLIN | POLLPRI;
167: while (!Kill) {
168: if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1 ||
169: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
170: ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
171: break;
172: } else if (!ret && (ret = KASession(sess))) {
173: call.LOG(logg, "Session %s keep-alive missing from %s for user %s ...\n",
174: sess->sess_cid, sess->sess_addr, sess->sess_user);
175: break;
176: }
177: /* receive & decode packet */
178: if ((ret = recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0)) == -1) {
179: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
180: break;
181: } else if (!ret) {
182: ioDEBUG(4, "Session %s EOF received.", sess->sess_cid);
183: break;
184: } else
185: hdr = (struct mqtthdr*) buf.msg_base;
186:
187: /* dispatch message type */
188: switch (hdr->mqtt_msg.type) {
189: default:
190: ioDEBUG(5, "Error:: Session %s, wrong command %d - DISCARDED",
191: sess->sess_cid, hdr->mqtt_msg.type);
192: break;
193: }
194: }
195:
196: pthread_cleanup_pop(42);
197: pthread_exit(NULL);
198: }
199:
200: static void *
201: startSession(sched_task_t *task)
202: {
203: u_char basebuf[USHRT_MAX];
204: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
205: mqtthdr_connflgs_t flg;
206: mqtthdr_connack_t cack;
207: struct tagSession *s, *sess = NULL;
208: int ret;
209: pthread_attr_t attr;
210:
211: ioTRACE(4);
212:
213: assert(task);
214:
215: sess = initSession(TASK_FD(task), TASK_ARG(task));
216: if (!sess) {
217: io_freeVar(TASK_ARG(task));
218: close(TASK_FD(task));
219: return NULL;
220: }
221:
222: /* receive & decode packet */
223: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
224: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
225: finiSession(sess);
226: return NULL;
227: }
228: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
229: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
230: &sess->sess_will.topic, &sess->sess_will.msg);
231: ret = cack.retcode;
232: flg.flags = cack.reserved;
233: if (flg.reserved) {
234: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
235: goto end;
236: } else {
237: sess->sess_clean = flg.clean_sess;
238: sess->sess_will.qos = flg.will_qos;
239: sess->sess_will.retain = flg.will_retain;
240: sess->sess_will.flag = flg.will_flg;
241: }
242:
243: /* check online table for user */
244: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
245: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
246: sess->sess_user, sess->sess_pass);
247: ret = MQTT_RETCODE_DENIED;
248: goto end;
249: } else {
250: ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
251: ret = MQTT_RETCODE_ACCEPTED;
252: }
253: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
254: ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
255: TAILQ_FOREACH(s, &Sessions, sess_node)
256: if (!strcmp(s->sess_cid, sess->sess_cid)) {
257: /* found stale session & disconnect it! */
258: stopSession(s);
259: break;
260: }
261: }
262: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
263: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
264: sess->sess_will.qos, sess->sess_will.retain) == -1) {
265: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
266: ret = MQTT_RETCODE_DENIED;
267: goto end;
268: } else {
269: ioDEBUG(0, "Session %s from %s and username %s is started",
270: sess->sess_cid, sess->sess_addr, sess->sess_user);
271: ret = MQTT_RETCODE_ACCEPTED;
272: }
273:
274: ret = mqtt_msgCONNACK(&msg, ret);
275: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
276: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
277: finiSession(sess);
278: return NULL;
279: } else {
280: ioDEBUG(5, "Sended %d bytes", ret);
281: free(msg.msg_base);
282: memset(&msg, 0, sizeof msg);
283: }
284:
285: /* Start session thread OK ... */
286: pthread_attr_init(&attr);
287: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
288:
289: pthread_mutex_lock(&mtx_sess);
290: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
291: pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
292: pthread_mutex_unlock(&mtx_sess);
293:
294: call.LOG(logg, "Session %s started from %s for user %s OK!\n", sess->sess_cid,
295: sess->sess_addr, sess->sess_user);
296:
297: pthread_attr_destroy(&attr);
298: return NULL;
299: end: /* close client connection */
300: ret = mqtt_msgCONNACK(&msg, ret);
301: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
302: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
303: } else {
304: ioDEBUG(5, "Sended %d bytes", ret);
305: free(msg.msg_base);
306: memset(&msg, 0, sizeof msg);
307: }
308:
309: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
310: finiSession(sess);
311: return NULL;
312: }
313:
314: /* ----------------------------------------------------------------------- */
315:
316: static void *
317: thrSched(void *arg __unused)
318: {
319: struct tagSession *sess;
320:
321: ioTRACE(1);
322:
323: schedRun(root, &Kill);
324:
325: TAILQ_FOREACH(sess, &Sessions, sess_node)
326: if (sess->sess_tid)
327: pthread_cancel(sess->sess_tid);
328: pthread_exit(NULL);
329: }
330:
331: int
332: Run(int sock)
333: {
334: io_sockaddr_t sa;
335: socklen_t sslen = sizeof sa.ss;
336: int cli;
337: pthread_t tid;
338: ait_val_t *v;
339: char str[STRSIZ];
340:
341: ioTRACE(1);
342:
343: if (pthread_create(&tid, NULL, thrSched, NULL)) {
344: ioLOGERR(0);
345: return -1;
346: } else
347: pthread_detach(tid);
348: ioDEBUG(2, "Run scheduler management thread");
349:
350: if (listen(sock, SOMAXCONN) == -1) {
351: ioLOGERR(0);
352: return -1;
353: }
354:
355: while (!Kill) {
356: if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
357: if (!Kill)
358: continue;
359: ioLOGERR(0);
360: break;
361: }
362: v = io_allocVar();
363: if (!v) {
364: ioLIBERR(mqtt);
365: break;
366: } else {
367: memset(str, 0, sizeof str);
368: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
369: AIT_SET_STR(v, str);
370: }
371: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
372:
373: if (!schedRead(root, startSession, v, cli)) {
374: io_freeVar(v);
375: close(cli);
376: ioDEBUG(1, "Terminated client with socket=%d", cli);
377: }
378: }
379:
380: return 0;
381: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>