Annotation of mqtt/src/daemon.c, revision 1.1.2.9
1.1.2.1 misho 1: #include "global.h"
2:
3:
1.1.2.7 misho 4: extern char cliCmd[], *cliStr[];
5:
6:
1.1.2.5 misho 7: static void *
8: startSession(sched_task_t *task)
1.1.2.4 misho 9: {
1.1.2.8 misho 10: static u_char basebuf[USHRT_MAX];
1.1.2.6 misho 11: mqtt_cb_t cbs[MQTT_TYPE_MAX + 1] = { 0 };
1.1.2.9 ! misho 12: mqtt_msg_t *buf;
! 13: int ret = 0;
1.1.2.6 misho 14:
15: FTRACE(4);
16:
1.1.2.8 misho 17: /*
1.1.2.6 misho 18: buf = mqtt_msgAlloc(USHRT_MAX);
19: if (!buf) {
20: syslog(LOG_ERR, "Error:: allocate message buf (%d) #%d - %s", (int) TASK_FD(task),
21: mqtt_GetErrno(), mqtt_GetError());
22: goto end;
23: }
1.1.2.8 misho 24: */
1.1.2.6 misho 25:
1.1.2.8 misho 26: if (recv(TASK_FD(task), basebuf, sizeof basebuf, 0) == -1) {
27: VERB(3) syslog(LOG_ERR, "Error:: recv(%d) #%d - %s", (int) TASK_FD(task),
1.1.2.6 misho 28: errno, strerror(errno));
29: goto end;
1.1.2.9 ! misho 30: }
1.1.2.8 misho 31:
32: /*
1.1.2.7 misho 33: for (ret = i = 0; cliCmd[i] && !(ret = (hdr->mqtt_msg.type == cliCmd[i])); i++);
34: if (!ret) {
1.1.2.8 misho 35: VERB(2) syslog(LOG_ERR, "Error:: wrong command type #%d %s",
1.1.2.7 misho 36: hdr->mqtt_msg.type, cliStr[i]);
37: goto end;
38: }
1.1.2.8 misho 39: */
1.1.2.6 misho 40:
1.1.2.8 misho 41: /* check online table for user */
42: // ChkSessPUB(&cfg, );
1.1.2.6 misho 43:
1.1.2.8 misho 44: ret = mqttDispatcher(cbs, buf);
1.1.2.9 ! misho 45: // mqtt_msgFree(&buf, 42);
1.1.2.6 misho 46:
47: switch (ret) {
48: case -1:
49: syslog(LOG_ERR, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
50: break;
51: default:
52: return NULL;
53: }
54: end: /* close client connection */
1.1.2.5 misho 55: close(TASK_FD(task));
56: VERB(1) syslog(LOG_DEBUG, "Close client %s with socket=%d",
57: (char*) TASK_ARG(task), (int) TASK_FD(task));
58: if (TASK_ARG(task))
59: free(TASK_ARG(task));
1.1.2.6 misho 60: return NULL;
1.1.2.4 misho 61: }
62:
63: /* ----------------------------------------------------------------------- */
64:
1.1.2.5 misho 65: static void *
66: thrSched(void *arg __unused)
67: {
68: FTRACE(1);
69:
70: schedRun(root, (intptr_t*) &Kill);
71: pthread_exit(NULL);
72: }
73:
1.1.2.2 misho 74: int
1.1.2.3 misho 75: Run(int sock)
1.1.2.2 misho 76: {
1.1.2.4 misho 77: io_sockaddr_t sa;
78: socklen_t sslen = sizeof sa.ss;
79: int cli;
1.1.2.5 misho 80: char *str = NULL, szAddr[STRSIZ] = { 0 };
81: pthread_t tid;
1.1.2.4 misho 82:
1.1.2.2 misho 83: FTRACE(1);
84:
1.1.2.5 misho 85: if (pthread_create(&tid, NULL, thrSched, NULL)) {
86: syslog(LOG_ERR, "Error:: thread scheduler #%d - %s", errno, strerror(errno));
87: return -1;
88: } else
89: pthread_detach(tid);
90: VERB(2) syslog(LOG_DEBUG, "Run scheduler management thread");
91:
1.1.2.3 misho 92: if (listen(sock, SOMAXCONN) == -1) {
1.1.2.4 misho 93: syslog(LOG_ERR, "Error:: listen(%d) #%d - %s\n", sock, errno, strerror(errno));
1.1.2.5 misho 94: pthread_cancel(tid);
1.1.2.3 misho 95: return -1;
96: }
97:
1.1.2.4 misho 98: while (!Kill) {
99: if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
100: syslog(LOG_ERR, "Error:: accept() #%d - %s", errno, strerror(errno));
101: continue;
102: } else
103: VERB(1) {
104: switch (sa.sa.sa_family) {
105: case AF_INET:
106: inet_ntop(AF_INET, &sa.sin.sin_addr, szAddr, sslen);
107: snprintf(szAddr, sizeof szAddr, "%s:%d",
108: szAddr, ntohs(sa.sin.sin_port));
109: break;
110: case AF_INET6:
111: inet_ntop(AF_INET6, &sa.sin6.sin6_addr, szAddr, sslen);
112: snprintf(szAddr, sizeof szAddr, "%s:%d",
113: szAddr, ntohs(sa.sin6.sin6_port));
114: break;
115: case AF_LOCAL:
116: strlcpy(szAddr, sa.sun.sun_path, sizeof szAddr);
117: break;
118: default:
119: close(cli);
120: syslog(LOG_ERR, "Error:: unsupported address type %d",
121: sa.sa.sa_family);
122: continue;
123: }
1.1.2.5 misho 124: str = strdup(szAddr);
125: syslog(LOG_DEBUG, "Connected client with socket=%d from %s", cli, str);
1.1.2.4 misho 126: }
127:
1.1.2.5 misho 128: if (!schedRead(root, startSession, str, cli)) {
1.1.2.4 misho 129: close(cli);
130: VERB(1) syslog(LOG_DEBUG, "Terminated client with socket=%d", cli);
1.1.2.5 misho 131: if (str)
132: free(str);
1.1.2.4 misho 133: }
134: }
135:
1.1.2.2 misho 136: return 0;
137: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>