Annotation of mqtt/src/daemon.c, revision 1.1.2.29

1.1.2.1   misho       1: #include "global.h"
1.1.2.16  misho       2: #include "rtlm.h"
1.1.2.24  misho       3: #include "utils.h"
1.1.2.11  misho       4: #include "mqttd.h"
1.1.2.1   misho       5: 
                      6: 
1.1.2.14  misho       7: static inline struct tagSession *
1.1.2.18  misho       8: initSession(int sock, ait_val_t * __restrict v)
1.1.2.14  misho       9: {
                     10:        struct tagSession *sess = NULL;
                     11:        const char *str;
                     12: 
1.1.2.20  misho      13:        ioTRACE(5);
1.1.2.15  misho      14: 
1.1.2.18  misho      15:        if (!v)
1.1.2.14  misho      16:                return NULL;
                     17: 
                     18:        sess = malloc(sizeof(struct tagSession));
                     19:        if (!sess) {
1.1.2.20  misho      20:                ioDEBUG(3, "Error:: in malloc #%d - %s", errno, strerror(errno));
1.1.2.18  misho      21:                io_freeVar(v);
1.1.2.14  misho      22:                return NULL;
                     23:        } else
                     24:                memset(sess, 0, sizeof(struct tagSession));
                     25: 
1.1.2.17  misho      26:        str = (const char*) cfg_GetAttribute(&cfg, CFG("mqttd"), CFG("retry"));
1.1.2.14  misho      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) {
1.1.2.20  misho      34:                ioDEBUG(3, "Error:: in msgAlloc #%d - %s", mqtt_GetErrno(), mqtt_GetError());
1.1.2.14  misho      35:                free(sess);
1.1.2.18  misho      36:                io_freeVar(v);
1.1.2.14  misho      37:                return NULL;
                     38:        }
                     39: 
1.1.2.15  misho      40:        sess->sess_sock = sock;
1.1.2.19  misho      41:        strlcpy(sess->sess_addr, (char*) AIT_GET_STR(v), sizeof sess->sess_addr);
1.1.2.18  misho      42:        io_freeVar(v);
1.1.2.14  misho      43:        return sess;
                     44: }
                     45: 
                     46: static void
                     47: finiSession(struct tagSession *sess)
                     48: {
                     49:        register int i;
                     50:        struct tagStore *store;
                     51: 
1.1.2.20  misho      52:        ioTRACE(5);
1.1.2.15  misho      53: 
1.1.2.14  misho      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: 
1.1.2.24  misho      68:        if (sess->sess_sock > STDERR_FILENO)
                     69:                srv_Close(sess->sess_sock);
1.1.2.14  misho      70: 
                     71:        mqtt_msgFree(&sess->sess_buf, 42);
                     72: 
                     73:        free(sess);
                     74: }
                     75: 
1.1.2.24  misho      76: static void
                     77: stopSession(struct tagSession *sess)
1.1.2.15  misho      78: {
1.1.2.24  misho      79:        mqtt_msg_t msg = { NULL, 0 };
                     80:        int ret;
                     81: 
1.1.2.26  misho      82:        ioTRACE(4);
                     83: 
                     84:        assert(sess);
                     85: 
1.1.2.24  misho      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);
1.1.2.27  misho      96:                memset(&msg, 0, sizeof msg);
1.1.2.18  misho      97:        }
                     98: 
1.1.2.24  misho      99:        ioDEBUG(1, "Close socket=%d", sess->sess_sock);
                    100:        finiSession(sess);
1.1.2.27  misho     101: 
1.1.2.28  misho     102:        call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid, 
1.1.2.27  misho     103:                        sess->sess_addr, sess->sess_user);
1.1.2.24  misho     104: }
                    105: 
1.1.2.26  misho     106: static int
                    107: KASession(struct tagSession *sess)
                    108: {
                    109:        u_char basebuf[USHRT_MAX];
                    110:        mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
                    111:        int ret;
                    112:        struct pollfd pfd;
                    113: 
                    114:        ioTRACE(4);
                    115: 
                    116:        assert(sess);
                    117: 
                    118:        /* ping request */
                    119:        ret = mqtt_msgPINGREQ(&msg);
                    120:        if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
                    121:                ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
                    122:                return -1;
                    123:        } else {
                    124:                ioDEBUG(5, "Sended %d bytes for ping request", ret);
                    125:                free(msg.msg_base);
                    126:                memset(&msg, 0, sizeof msg);
                    127:        }
                    128: 
                    129:        pfd.fd = sess->sess_sock;
                    130:        pfd.events = POLLIN | POLLPRI;
1.1.2.28  misho     131:        if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1 || 
                    132:                        pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.1.2.26  misho     133:                ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
                    134:                return -1;
                    135:        } else if (!ret) {
                    136:                /* problem!!! session is abandoned ... must be disconnect! */
                    137:                return 1;
                    138:        }
                    139:        /* receive & decode packet */
                    140:        if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
                    141:                ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
                    142:                return -1;
                    143:        }
                    144:        if (mqtt_readPINGRESP(&buf)) {
                    145:                /* problem!!! session is broken, not hear ping response ... must be disconnect! */
                    146:                return 2;
                    147:        }
                    148: 
                    149:        /* Keep Alive is OK! */
                    150:        return 0;
                    151: }
                    152: 
1.1.2.24  misho     153: static void *
                    154: thrSession(struct tagSession *sess)
                    155: {
1.1.2.28  misho     156:        u_char basebuf[USHRT_MAX];
                    157:        mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
                    158:        int ret;
                    159:        struct pollfd pfd;
                    160:        struct mqtthdr *hdr;
1.1.2.18  misho     161: 
1.1.2.28  misho     162:        pthread_cleanup_push((void(*)(void*)) stopSession, sess);
1.1.2.20  misho     163:        ioTRACE(2);
1.1.2.15  misho     164: 
1.1.2.28  misho     165:        pfd.fd = sess->sess_sock;
                    166:        pfd.events = POLLIN | POLLPRI;
                    167:        while (!Kill) {
                    168:                if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1 || 
                    169:                                pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
                    170:                        ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
                    171:                        break;
                    172:                } else if (!ret && (ret = KASession(sess))) {
                    173:                        call.LOG(logg, "Session %s keep-alive missing from %s for user %s ...\n", 
                    174:                                        sess->sess_cid, sess->sess_addr, sess->sess_user);
                    175:                        break;
                    176:                }
                    177:                /* receive & decode packet */
                    178:                if ((ret = recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0)) == -1) {
                    179:                        ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
                    180:                        break;
                    181:                } else if (!ret) {
                    182:                        ioDEBUG(4, "Session %s EOF received.", sess->sess_cid);
                    183:                        break;
                    184:                } else
                    185:                        hdr = (struct mqtthdr*) buf.msg_base;
                    186: 
1.1.2.29! misho     187:                /* dispatch message type */
1.1.2.28  misho     188:                switch (hdr->mqtt_msg.type) {
                    189:                        default:
                    190:                                ioDEBUG(5, "Error:: Session %s, wrong command %d - DISCARDED", 
                    191:                                                sess->sess_cid, hdr->mqtt_msg.type);
                    192:                                break;
                    193:                }
                    194:        }
                    195: 
1.1.2.18  misho     196:        pthread_cleanup_pop(42);
1.1.2.15  misho     197:        pthread_exit(NULL);
                    198: }
                    199: 
                    200: static void *
1.1.2.5   misho     201: startSession(sched_task_t *task)
1.1.2.4   misho     202: {
1.1.2.11  misho     203:        u_char basebuf[USHRT_MAX];
1.1.2.18  misho     204:        mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
1.1.2.10  misho     205:        mqtthdr_connflgs_t flg;
1.1.2.13  misho     206:        mqtthdr_connack_t cack;
1.1.2.24  misho     207:        struct tagSession *s, *sess = NULL;
1.1.2.18  misho     208:        int ret;
                    209:        pthread_attr_t attr;
1.1.2.6   misho     210: 
1.1.2.20  misho     211:        ioTRACE(4);
1.1.2.6   misho     212: 
1.1.2.26  misho     213:        assert(task);
                    214: 
1.1.2.14  misho     215:        sess = initSession(TASK_FD(task), TASK_ARG(task));
1.1.2.15  misho     216:        if (!sess) {
1.1.2.18  misho     217:                io_freeVar(TASK_ARG(task));
1.1.2.15  misho     218:                close(TASK_FD(task));
1.1.2.14  misho     219:                return NULL;
1.1.2.15  misho     220:        }
1.1.2.6   misho     221: 
1.1.2.18  misho     222:        /* receive & decode packet */
1.1.2.14  misho     223:        if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
1.1.2.20  misho     224:                ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18  misho     225:                finiSession(sess);
                    226:                return NULL;
1.1.2.9   misho     227:        }
1.1.2.14  misho     228:        cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid, 
1.1.2.12  misho     229:                        sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass, 
1.1.2.13  misho     230:                        &sess->sess_will.topic, &sess->sess_will.msg);
1.1.2.18  misho     231:        ret = cack.retcode;
1.1.2.14  misho     232:        flg.flags = cack.reserved;
                    233:        if (flg.reserved) {
1.1.2.20  misho     234:                ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
1.1.2.10  misho     235:                goto end;
1.1.2.14  misho     236:        } else {
                    237:                sess->sess_clean = flg.clean_sess;
                    238:                sess->sess_will.qos = flg.will_qos;
                    239:                sess->sess_will.retain = flg.will_retain;
                    240:                sess->sess_will.flag = flg.will_flg;
1.1.2.12  misho     241:        }
1.1.2.6   misho     242: 
1.1.2.8   misho     243:        /* check online table for user */
1.1.2.18  misho     244:        if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
1.1.2.20  misho     245:                ioDEBUG(0, "Login:: DENIED for username %s and password %s", 
1.1.2.16  misho     246:                                sess->sess_user, sess->sess_pass);
1.1.2.21  misho     247:                ret = MQTT_RETCODE_DENIED;
1.1.2.16  misho     248:                goto end;
1.1.2.21  misho     249:        } else {
1.1.2.24  misho     250:                ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
1.1.2.21  misho     251:                ret = MQTT_RETCODE_ACCEPTED;
                    252:        }
1.1.2.23  misho     253:        if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
1.1.2.24  misho     254:                ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
                    255:                TAILQ_FOREACH(s, &Sessions, sess_node)
                    256:                        if (!strcmp(s->sess_cid, sess->sess_cid)) {
1.1.2.25  misho     257:                                /* found stale session & disconnect it! */
1.1.2.24  misho     258:                                stopSession(s);
                    259:                                break;
                    260:                        }
1.1.2.23  misho     261:        }
                    262:        if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr, 
                    263:                                sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg, 
                    264:                                sess->sess_will.qos, sess->sess_will.retain) == -1) {
                    265:                ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
                    266:                ret = MQTT_RETCODE_DENIED;
                    267:                goto end;
                    268:        } else {
                    269:                ioDEBUG(0, "Session %s from %s and username %s is started", 
                    270:                                sess->sess_cid, sess->sess_addr, sess->sess_user);
                    271:                ret = MQTT_RETCODE_ACCEPTED;
                    272:        }
1.1.2.6   misho     273: 
1.1.2.18  misho     274:        ret = mqtt_msgCONNACK(&msg, ret);
                    275:        if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20  misho     276:                ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18  misho     277:                finiSession(sess);
                    278:                return NULL;
                    279:        } else {
1.1.2.20  misho     280:                ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19  misho     281:                free(msg.msg_base);
1.1.2.27  misho     282:                memset(&msg, 0, sizeof msg);
1.1.2.18  misho     283:        }
                    284: 
1.1.2.15  misho     285:        /* Start session thread OK ... */
1.1.2.18  misho     286:        pthread_attr_init(&attr);
                    287:        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
                    288: 
                    289:        pthread_mutex_lock(&mtx_sess);
1.1.2.15  misho     290:        TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.1.2.18  misho     291:        pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
                    292:        pthread_mutex_unlock(&mtx_sess);
                    293: 
1.1.2.23  misho     294:        call.LOG(logg, "Session %s started from %s for user %s OK!\n", sess->sess_cid, 
                    295:                        sess->sess_addr, sess->sess_user);
1.1.2.18  misho     296: 
                    297:        pthread_attr_destroy(&attr);
1.1.2.15  misho     298:        return NULL;
1.1.2.6   misho     299: end:   /* close client connection */
1.1.2.18  misho     300:        ret = mqtt_msgCONNACK(&msg, ret);
                    301:        if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20  misho     302:                ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18  misho     303:        } else {
1.1.2.20  misho     304:                ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19  misho     305:                free(msg.msg_base);
1.1.2.27  misho     306:                memset(&msg, 0, sizeof msg);
1.1.2.18  misho     307:        }
                    308: 
1.1.2.20  misho     309:        ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
1.1.2.15  misho     310:        finiSession(sess);
1.1.2.6   misho     311:        return NULL;
1.1.2.4   misho     312: }
                    313: 
                    314: /* ----------------------------------------------------------------------- */
                    315: 
1.1.2.5   misho     316: static void *
                    317: thrSched(void *arg __unused)
                    318: {
1.1.2.18  misho     319:        struct tagSession *sess;
1.1.2.5   misho     320: 
1.1.2.20  misho     321:        ioTRACE(1);
                    322: 
                    323:        schedRun(root, &Kill);
1.1.2.18  misho     324: 
                    325:        TAILQ_FOREACH(sess, &Sessions, sess_node)
                    326:                if (sess->sess_tid)
                    327:                        pthread_cancel(sess->sess_tid);
1.1.2.5   misho     328:        pthread_exit(NULL);
                    329: }
                    330: 
1.1.2.2   misho     331: int
1.1.2.3   misho     332: Run(int sock)
1.1.2.2   misho     333: {
1.1.2.4   misho     334:        io_sockaddr_t sa;
                    335:        socklen_t sslen = sizeof sa.ss;
                    336:        int cli;
1.1.2.5   misho     337:        pthread_t tid;
1.1.2.18  misho     338:        ait_val_t *v;
1.1.2.23  misho     339:        char str[STRSIZ];
1.1.2.4   misho     340: 
1.1.2.20  misho     341:        ioTRACE(1);
1.1.2.2   misho     342: 
1.1.2.5   misho     343:        if (pthread_create(&tid, NULL, thrSched, NULL)) {
1.1.2.20  misho     344:                ioLOGERR(0);
1.1.2.5   misho     345:                return -1;
                    346:        } else
                    347:                pthread_detach(tid);
1.1.2.20  misho     348:        ioDEBUG(2, "Run scheduler management thread");
1.1.2.5   misho     349: 
1.1.2.24  misho     350:        if (listen(sock, SOMAXCONN) == -1) {
1.1.2.20  misho     351:                ioLOGERR(0);
1.1.2.3   misho     352:                return -1;
                    353:        }
                    354: 
1.1.2.4   misho     355:        while (!Kill) {
                    356:                if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
1.1.2.20  misho     357:                        if (!Kill)
                    358:                                continue;
                    359:                        ioLOGERR(0);
                    360:                        break;
1.1.2.18  misho     361:                }
                    362:                v = io_allocVar();
                    363:                if (!v) {
                    364:                        ioLIBERR(mqtt);
                    365:                        break;
1.1.2.23  misho     366:                } else {
                    367:                        memset(str, 0, sizeof str);
                    368:                        snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
                    369:                        AIT_SET_STR(v, str);
                    370:                }
                    371:                ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
1.1.2.4   misho     372: 
1.1.2.18  misho     373:                if (!schedRead(root, startSession, v, cli)) {
                    374:                        io_freeVar(v);
1.1.2.4   misho     375:                        close(cli);
1.1.2.20  misho     376:                        ioDEBUG(1, "Terminated client with socket=%d", cli);
1.1.2.4   misho     377:                }
                    378:        }
                    379: 
1.1.2.2   misho     380:        return 0;
                    381: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>