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: msg.msg_len = 0;
97: }
98:
99: ioDEBUG(1, "Close socket=%d", sess->sess_sock);
100: finiSession(sess);
101: }
102:
103: static int
104: KASession(struct tagSession *sess)
105: {
106: u_char basebuf[USHRT_MAX];
107: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
108: int ret;
109: struct pollfd pfd;
110:
111: ioTRACE(4);
112:
113: assert(sess);
114:
115: /* ping request */
116: ret = mqtt_msgPINGREQ(&msg);
117: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
118: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
119: stopSession(sess);
120: return -1;
121: } else {
122: ioDEBUG(5, "Sended %d bytes for ping request", ret);
123: free(msg.msg_base);
124: memset(&msg, 0, sizeof msg);
125: }
126:
127: pfd.fd = sess->sess_sock;
128: pfd.events = POLLIN | POLLPRI;
129: if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1) {
130: ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
131: stopSession(sess);
132: return -1;
133: } else if (!ret) {
134: /* problem!!! session is abandoned ... must be disconnect! */
135: stopSession(sess);
136: return 1;
137: }
138: /* receive & decode packet */
139: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
140: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
141: stopSession(sess);
142: return -1;
143: }
144: if (mqtt_readPINGRESP(&buf)) {
145: /* problem!!! session is broken, not hear ping response ... must be disconnect! */
146: stopSession(sess);
147: return 2;
148: }
149:
150: /* Keep Alive is OK! */
151: return 0;
152: }
153:
154: static void *
155: thrSession(struct tagSession *sess)
156: {
157: pthread_cleanup_push((void(*)(void*)) stopSession, sess);
158:
159: ioTRACE(2);
160:
161: pthread_cleanup_pop(42);
162: pthread_exit(NULL);
163: }
164:
165: static void *
166: startSession(sched_task_t *task)
167: {
168: u_char basebuf[USHRT_MAX];
169: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
170: mqtthdr_connflgs_t flg;
171: mqtthdr_connack_t cack;
172: struct tagSession *s, *sess = NULL;
173: int ret;
174: pthread_attr_t attr;
175:
176: ioTRACE(4);
177:
178: assert(task);
179:
180: sess = initSession(TASK_FD(task), TASK_ARG(task));
181: if (!sess) {
182: io_freeVar(TASK_ARG(task));
183: close(TASK_FD(task));
184: return NULL;
185: }
186:
187: /* receive & decode packet */
188: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
189: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
190: finiSession(sess);
191: return NULL;
192: }
193: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
194: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
195: &sess->sess_will.topic, &sess->sess_will.msg);
196: ret = cack.retcode;
197: flg.flags = cack.reserved;
198: if (flg.reserved) {
199: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
200: goto end;
201: } else {
202: sess->sess_clean = flg.clean_sess;
203: sess->sess_will.qos = flg.will_qos;
204: sess->sess_will.retain = flg.will_retain;
205: sess->sess_will.flag = flg.will_flg;
206: }
207:
208: /* check online table for user */
209: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
210: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
211: sess->sess_user, sess->sess_pass);
212: ret = MQTT_RETCODE_DENIED;
213: goto end;
214: } else {
215: ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
216: ret = MQTT_RETCODE_ACCEPTED;
217: }
218: if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
219: ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
220: TAILQ_FOREACH(s, &Sessions, sess_node)
221: if (!strcmp(s->sess_cid, sess->sess_cid)) {
222: /* found stale session & disconnect it! */
223: stopSession(s);
224: break;
225: }
226: }
227: if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr,
228: sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg,
229: sess->sess_will.qos, sess->sess_will.retain) == -1) {
230: ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
231: ret = MQTT_RETCODE_DENIED;
232: goto end;
233: } else {
234: ioDEBUG(0, "Session %s from %s and username %s is started",
235: sess->sess_cid, sess->sess_addr, sess->sess_user);
236: ret = MQTT_RETCODE_ACCEPTED;
237: }
238:
239: ret = mqtt_msgCONNACK(&msg, ret);
240: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
241: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
242: finiSession(sess);
243: return NULL;
244: } else {
245: ioDEBUG(5, "Sended %d bytes", ret);
246: free(msg.msg_base);
247: msg.msg_len = 0;
248: }
249:
250: /* Start session thread OK ... */
251: pthread_attr_init(&attr);
252: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
253:
254: pthread_mutex_lock(&mtx_sess);
255: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
256: pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
257: pthread_mutex_unlock(&mtx_sess);
258:
259: call.LOG(logg, "Session %s started from %s for user %s OK!\n", sess->sess_cid,
260: sess->sess_addr, sess->sess_user);
261:
262: pthread_attr_destroy(&attr);
263: return NULL;
264: end: /* close client connection */
265: ret = mqtt_msgCONNACK(&msg, ret);
266: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
267: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
268: } else {
269: ioDEBUG(5, "Sended %d bytes", ret);
270: free(msg.msg_base);
271: msg.msg_len = 0;
272: }
273:
274: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
275: finiSession(sess);
276: return NULL;
277: }
278:
279: /* ----------------------------------------------------------------------- */
280:
281: static void *
282: thrSched(void *arg __unused)
283: {
284: struct tagSession *sess;
285:
286: ioTRACE(1);
287:
288: schedRun(root, &Kill);
289:
290: TAILQ_FOREACH(sess, &Sessions, sess_node)
291: if (sess->sess_tid)
292: pthread_cancel(sess->sess_tid);
293: pthread_exit(NULL);
294: }
295:
296: int
297: Run(int sock)
298: {
299: io_sockaddr_t sa;
300: socklen_t sslen = sizeof sa.ss;
301: int cli;
302: pthread_t tid;
303: ait_val_t *v;
304: char str[STRSIZ];
305:
306: ioTRACE(1);
307:
308: if (pthread_create(&tid, NULL, thrSched, NULL)) {
309: ioLOGERR(0);
310: return -1;
311: } else
312: pthread_detach(tid);
313: ioDEBUG(2, "Run scheduler management thread");
314:
315: if (listen(sock, SOMAXCONN) == -1) {
316: ioLOGERR(0);
317: return -1;
318: }
319:
320: while (!Kill) {
321: if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
322: if (!Kill)
323: continue;
324: ioLOGERR(0);
325: break;
326: }
327: v = io_allocVar();
328: if (!v) {
329: ioLIBERR(mqtt);
330: break;
331: } else {
332: memset(str, 0, sizeof str);
333: snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
334: AIT_SET_STR(v, str);
335: }
336: ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
337:
338: if (!schedRead(root, startSession, v, cli)) {
339: io_freeVar(v);
340: close(cli);
341: ioDEBUG(1, "Terminated client with socket=%d", cli);
342: }
343: }
344:
345: return 0;
346: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>