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