1: #include "global.h"
2: #include "rtlm.h"
3: #include "mqttd.h"
4:
5:
6: static inline struct tagSession *
7: initSession(int sock, ait_val_t * __restrict v)
8: {
9: struct tagSession *sess = NULL;
10: const char *str;
11:
12: ioTRACE(5);
13:
14: if (!v)
15: return NULL;
16:
17: sess = malloc(sizeof(struct tagSession));
18: if (!sess) {
19: ioDEBUG(3, "Error:: in malloc #%d - %s", errno, strerror(errno));
20: io_freeVar(v);
21: return NULL;
22: } else
23: memset(sess, 0, sizeof(struct tagSession));
24:
25: str = (const char*) cfg_GetAttribute(&cfg, CFG("mqttd"), CFG("retry"));
26: if (!str)
27: sess->sess_retry = DEFAULT_RETRY;
28: else
29: sess->sess_retry = strtol(str, NULL, 0);
30:
31: sess->sess_buf = mqtt_msgAlloc(0);
32: if (!sess->sess_buf) {
33: ioDEBUG(3, "Error:: in msgAlloc #%d - %s", mqtt_GetErrno(), mqtt_GetError());
34: free(sess);
35: io_freeVar(v);
36: return NULL;
37: }
38:
39: sess->sess_sock = sock;
40: strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr);
41: io_freeVar(v);
42: return sess;
43: }
44:
45: static void
46: finiSession(struct tagSession *sess)
47: {
48: register int i;
49: struct tagStore *store;
50:
51: ioTRACE(5);
52:
53: if (!sess)
54: return;
55:
56: for (i = 0; i < MQTT_QOS_RESERVED; i++)
57: while ((store = SLIST_FIRST(&sess->sess_store[i]))) {
58: SLIST_REMOVE_HEAD(&sess->sess_store[i], st_node);
59: free(store);
60: }
61:
62: if (sess->sess_will.msg)
63: free(sess->sess_will.msg);
64: if (sess->sess_will.topic)
65: free(sess->sess_will.topic);
66:
67: if (sess->sess_sock > STDERR_FILENO) {
68: shutdown(sess->sess_sock, SHUT_RDWR);
69: close(sess->sess_sock);
70: }
71:
72: mqtt_msgFree(&sess->sess_buf, 42);
73:
74: free(sess);
75: }
76:
77: static void *
78: thrSession(struct tagSession *sess)
79: {
80: void thrClean(struct tagSession *sess)
81: {
82: pthread_mutex_lock(&mtx_sess);
83: TAILQ_REMOVE(&Sessions, sess, sess_node);
84: pthread_mutex_unlock(&mtx_sess);
85: ioDEBUG(1, "Close socket=%d", sess->sess_sock);
86: finiSession(sess);
87: }
88:
89: pthread_cleanup_push((void(*)(void*)) thrClean, sess);
90:
91: ioTRACE(2);
92:
93: pthread_cleanup_pop(42);
94: pthread_exit(NULL);
95: }
96:
97: static void *
98: startSession(sched_task_t *task)
99: {
100: u_char basebuf[USHRT_MAX];
101: mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
102: mqtthdr_connflgs_t flg;
103: mqtthdr_connack_t cack;
104: struct tagSession *sess = NULL;
105: int ret;
106: pthread_attr_t attr;
107:
108: ioTRACE(4);
109:
110: sess = initSession(TASK_FD(task), TASK_ARG(task));
111: if (!sess) {
112: io_freeVar(TASK_ARG(task));
113: close(TASK_FD(task));
114: return NULL;
115: }
116:
117: /* receive & decode packet */
118: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
119: ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
120: finiSession(sess);
121: return NULL;
122: }
123: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
124: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
125: &sess->sess_will.topic, &sess->sess_will.msg);
126: ret = cack.retcode;
127: flg.flags = cack.reserved;
128: if (flg.reserved) {
129: ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
130: goto end;
131: } else {
132: sess->sess_clean = flg.clean_sess;
133: sess->sess_will.qos = flg.will_qos;
134: sess->sess_will.retain = flg.will_retain;
135: sess->sess_will.flag = flg.will_flg;
136: }
137:
138: /* check online table for user */
139: if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
140: ioDEBUG(0, "Login:: DENIED for username %s and password %s",
141: sess->sess_user, sess->sess_pass);
142: ret = MQTT_RETCODE_DENIED;
143: goto end;
144: } else {
145: ioDEBUG(0, "Login:: ALLOWED for username %s ...", sess->sess_user);
146: ret = MQTT_RETCODE_ACCEPTED;
147: }
148:
149: ret = mqtt_msgCONNACK(&msg, ret);
150: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
151: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
152: finiSession(sess);
153: return NULL;
154: } else {
155: ioDEBUG(5, "Sended %d bytes", ret);
156: free(msg.msg_base);
157: msg.msg_len = 0;
158: }
159:
160: /* Start session thread OK ... */
161: pthread_attr_init(&attr);
162: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
163:
164: pthread_mutex_lock(&mtx_sess);
165: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
166: pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
167: pthread_mutex_unlock(&mtx_sess);
168:
169: ioDEBUG(1, "ConnID=%s(%s) for %s login OK!", sess->sess_cid, sess->sess_user, sess->sess_addr);
170:
171: pthread_attr_destroy(&attr);
172: return NULL;
173: end: /* close client connection */
174: ret = mqtt_msgCONNACK(&msg, ret);
175: if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
176: ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
177: } else {
178: ioDEBUG(5, "Sended %d bytes", ret);
179: free(msg.msg_base);
180: msg.msg_len = 0;
181: }
182:
183: ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
184: finiSession(sess);
185: return NULL;
186: }
187:
188: /* ----------------------------------------------------------------------- */
189:
190: static void *
191: thrSched(void *arg __unused)
192: {
193: struct tagSession *sess;
194:
195: ioTRACE(1);
196:
197: schedRun(root, &Kill);
198:
199: TAILQ_FOREACH(sess, &Sessions, sess_node)
200: if (sess->sess_tid)
201: pthread_cancel(sess->sess_tid);
202: pthread_exit(NULL);
203: }
204:
205: int
206: Run(int sock)
207: {
208: io_sockaddr_t sa;
209: socklen_t sslen = sizeof sa.ss;
210: int cli;
211: pthread_t tid;
212: ait_val_t *v;
213:
214: ioTRACE(1);
215:
216: if (pthread_create(&tid, NULL, thrSched, NULL)) {
217: ioLOGERR(0);
218: return -1;
219: } else
220: pthread_detach(tid);
221: ioDEBUG(2, "Run scheduler management thread");
222:
223: if (listen(sock, 5 /*SOMAXCONN*/) == -1) {
224: ioLOGERR(0);
225: return -1;
226: }
227:
228: while (!Kill) {
229: printf("%%%%%%%%%%rrrrrrrrrrrrrrrrrrrrr errno=%d\n", errno);
230: if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
231: printf("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr errno=%d\n", errno);
232: if (!Kill)
233: continue;
234: ioLOGERR(0);
235: break;
236: }
237: v = io_allocVar();
238: if (!v) {
239: ioLIBERR(mqtt);
240: break;
241: } else
242: io_n2addr(&sa, v);
243: ioDEBUG(1, "Connected client with socket=%d from %s:%d", cli,
244: AIT_GET_STR(v), io_n2port(&sa));
245:
246: if (!schedRead(root, startSession, v, cli)) {
247: io_freeVar(v);
248: close(cli);
249: ioDEBUG(1, "Terminated client with socket=%d", cli);
250: }
251: }
252:
253: return 0;
254: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>