Annotation of mqtt/src/daemon.c, revision 1.1.2.15
1.1.2.1 misho 1: #include "global.h"
1.1.2.11 misho 2: #include "mqttd.h"
1.1.2.1 misho 3:
4:
1.1.2.7 misho 5: extern char cliCmd[], *cliStr[];
6:
7:
1.1.2.14 misho 8: static inline struct tagSession *
9: initSession(int sock, char * __restrict addr)
10: {
11: struct tagSession *sess = NULL;
12: const char *str;
13:
1.1.2.15! misho 14: FTRACE(5);
! 15:
1.1.2.14 misho 16: if (!addr)
17: return NULL;
18:
19: sess = malloc(sizeof(struct tagSession));
20: if (!sess) {
21: VERB(3) syslog(LOG_ERR, "Error:: in malloc #%d - %s", errno, strerror(errno));
22: return NULL;
23: } else
24: memset(sess, 0, sizeof(struct tagSession));
25:
26: str = 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: VERB(3) syslog(LOG_ERR, "Error:: in msgAlloc #%d - %s", mqtt_GetErrno(), mqtt_GetError());
35: free(sess);
36: return NULL;
37: }
38:
1.1.2.15! misho 39: sess->sess_sock = sock;
! 40: strlcpy(sess->sess_addr, addr, sizeof sess->sess_addr);
! 41: free(addr);
1.1.2.14 misho 42: return sess;
43: }
44:
45: static void
46: finiSession(struct tagSession *sess)
47: {
48: register int i;
49: struct tagStore *store;
50:
1.1.2.15! misho 51: FTRACE(5);
! 52:
1.1.2.14 misho 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: if (sess->sess_tid)
75: pthread_cancel(sess->sess_tid);
76: free(sess);
77: }
78:
1.1.2.5 misho 79: static void *
1.1.2.15! misho 80: thrSession(struct tagSession *sess)
! 81: {
! 82: FTRACE(2);
! 83:
! 84: TAILQ_REMOVE(&Sessions, sess, sess_node);
! 85: pthread_exit(NULL);
! 86: return NULL;
! 87: }
! 88:
! 89: static void *
1.1.2.5 misho 90: startSession(sched_task_t *task)
1.1.2.4 misho 91: {
1.1.2.11 misho 92: u_char basebuf[USHRT_MAX];
1.1.2.12 misho 93: mqtt_msg_t buf = { basebuf, sizeof basebuf };
1.1.2.10 misho 94: mqtthdr_connflgs_t flg;
1.1.2.13 misho 95: mqtthdr_connack_t cack;
1.1.2.12 misho 96: struct tagSession *sess = NULL;
1.1.2.6 misho 97:
98: FTRACE(4);
99:
1.1.2.14 misho 100: sess = initSession(TASK_FD(task), TASK_ARG(task));
1.1.2.15! misho 101: if (!sess) {
! 102: if (TASK_ARG(task))
! 103: free(TASK_ARG(task));
! 104: close(TASK_FD(task));
1.1.2.14 misho 105: return NULL;
1.1.2.15! misho 106: }
1.1.2.6 misho 107:
1.1.2.14 misho 108: if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
109: VERB(3) syslog(LOG_ERR, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.6 misho 110: goto end;
1.1.2.9 misho 111: }
1.1.2.14 misho 112: cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid,
1.1.2.12 misho 113: sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass,
1.1.2.13 misho 114: &sess->sess_will.topic, &sess->sess_will.msg);
1.1.2.14 misho 115: flg.flags = cack.reserved;
116: if (flg.reserved) {
1.1.2.13 misho 117: VERB(3) syslog(LOG_ERR, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
1.1.2.10 misho 118: goto end;
1.1.2.14 misho 119: } else {
120: sess->sess_clean = flg.clean_sess;
121: sess->sess_will.qos = flg.will_qos;
122: sess->sess_will.retain = flg.will_retain;
123: sess->sess_will.flag = flg.will_flg;
1.1.2.12 misho 124: }
1.1.2.6 misho 125:
1.1.2.8 misho 126: /* check online table for user */
1.1.2.6 misho 127:
1.1.2.15! misho 128: /* Start session thread OK ... */
! 129: TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
! 130: pthread_create(&sess->sess_tid, NULL, (void*(*)(void*)) thrSession, sess);
! 131: pthread_detach(sess->sess_tid);
! 132: VERB(1) syslog(LOG_DEBUG, "ConnID=%s(%s) for %s login OK!",
! 133: sess->sess_cid, sess->sess_user, sess->sess_addr);
! 134: return NULL;
1.1.2.6 misho 135: end: /* close client connection */
1.1.2.5 misho 136: VERB(1) syslog(LOG_DEBUG, "Close client %s with socket=%d",
137: (char*) TASK_ARG(task), (int) TASK_FD(task));
1.1.2.15! misho 138: finiSession(sess);
1.1.2.6 misho 139: return NULL;
1.1.2.4 misho 140: }
141:
142: /* ----------------------------------------------------------------------- */
143:
1.1.2.5 misho 144: static void *
145: thrSched(void *arg __unused)
146: {
147: FTRACE(1);
148:
149: schedRun(root, (intptr_t*) &Kill);
150: pthread_exit(NULL);
151: }
152:
1.1.2.2 misho 153: int
1.1.2.3 misho 154: Run(int sock)
1.1.2.2 misho 155: {
1.1.2.4 misho 156: io_sockaddr_t sa;
157: socklen_t sslen = sizeof sa.ss;
158: int cli;
1.1.2.5 misho 159: char *str = NULL, szAddr[STRSIZ] = { 0 };
160: pthread_t tid;
1.1.2.4 misho 161:
1.1.2.2 misho 162: FTRACE(1);
163:
1.1.2.5 misho 164: if (pthread_create(&tid, NULL, thrSched, NULL)) {
165: syslog(LOG_ERR, "Error:: thread scheduler #%d - %s", errno, strerror(errno));
166: return -1;
167: } else
168: pthread_detach(tid);
169: VERB(2) syslog(LOG_DEBUG, "Run scheduler management thread");
170:
1.1.2.3 misho 171: if (listen(sock, SOMAXCONN) == -1) {
1.1.2.4 misho 172: syslog(LOG_ERR, "Error:: listen(%d) #%d - %s\n", sock, errno, strerror(errno));
1.1.2.5 misho 173: pthread_cancel(tid);
1.1.2.3 misho 174: return -1;
175: }
176:
1.1.2.4 misho 177: while (!Kill) {
178: if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
179: syslog(LOG_ERR, "Error:: accept() #%d - %s", errno, strerror(errno));
180: continue;
181: } else
182: VERB(1) {
183: switch (sa.sa.sa_family) {
184: case AF_INET:
185: inet_ntop(AF_INET, &sa.sin.sin_addr, szAddr, sslen);
186: snprintf(szAddr, sizeof szAddr, "%s:%d",
187: szAddr, ntohs(sa.sin.sin_port));
188: break;
189: case AF_INET6:
190: inet_ntop(AF_INET6, &sa.sin6.sin6_addr, szAddr, sslen);
191: snprintf(szAddr, sizeof szAddr, "%s:%d",
192: szAddr, ntohs(sa.sin6.sin6_port));
193: break;
194: case AF_LOCAL:
195: strlcpy(szAddr, sa.sun.sun_path, sizeof szAddr);
196: break;
197: default:
198: close(cli);
199: syslog(LOG_ERR, "Error:: unsupported address type %d",
200: sa.sa.sa_family);
201: continue;
202: }
1.1.2.5 misho 203: str = strdup(szAddr);
204: syslog(LOG_DEBUG, "Connected client with socket=%d from %s", cli, str);
1.1.2.4 misho 205: }
206:
1.1.2.5 misho 207: if (!schedRead(root, startSession, str, cli)) {
1.1.2.4 misho 208: close(cli);
209: VERB(1) syslog(LOG_DEBUG, "Terminated client with socket=%d", cli);
1.1.2.5 misho 210: if (str)
211: free(str);
1.1.2.4 misho 212: }
213: }
214:
1.1.2.2 misho 215: return 0;
216: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>