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

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);
                     96:                msg.msg_len = 0;
1.1.2.18  misho      97:        }
                     98: 
1.1.2.24  misho      99:        ioDEBUG(1, "Close socket=%d", sess->sess_sock);
                    100:        finiSession(sess);
                    101: }
                    102: 
1.1.2.26! misho     103: static int
        !           104: KASession(struct tagSession *sess)
        !           105: {
        !           106:        u_char basebuf[USHRT_MAX];
        !           107:        mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
        !           108:        int ret;
        !           109:        struct pollfd pfd;
        !           110: 
        !           111:        ioTRACE(4);
        !           112: 
        !           113:        assert(sess);
        !           114: 
        !           115:        /* ping request */
        !           116:        ret = mqtt_msgPINGREQ(&msg);
        !           117:        if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
        !           118:                ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
        !           119:                stopSession(sess);
        !           120:                return -1;
        !           121:        } else {
        !           122:                ioDEBUG(5, "Sended %d bytes for ping request", ret);
        !           123:                free(msg.msg_base);
        !           124:                memset(&msg, 0, sizeof msg);
        !           125:        }
        !           126: 
        !           127:        pfd.fd = sess->sess_sock;
        !           128:        pfd.events = POLLIN | POLLPRI;
        !           129:        if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1) {
        !           130:                ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
        !           131:                stopSession(sess);
        !           132:                return -1;
        !           133:        } else if (!ret) {
        !           134:                /* problem!!! session is abandoned ... must be disconnect! */
        !           135:                stopSession(sess);
        !           136:                return 1;
        !           137:        }
        !           138:        /* receive & decode packet */
        !           139:        if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
        !           140:                ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
        !           141:                stopSession(sess);
        !           142:                return -1;
        !           143:        }
        !           144:        if (mqtt_readPINGRESP(&buf)) {
        !           145:                /* problem!!! session is broken, not hear ping response ... must be disconnect! */
        !           146:                stopSession(sess);
        !           147:                return 2;
        !           148:        }
        !           149: 
        !           150:        /* Keep Alive is OK! */
        !           151:        return 0;
        !           152: }
        !           153: 
1.1.2.24  misho     154: static void *
                    155: thrSession(struct tagSession *sess)
                    156: {
                    157:        pthread_cleanup_push((void(*)(void*)) stopSession, sess);
1.1.2.18  misho     158: 
1.1.2.20  misho     159:        ioTRACE(2);
1.1.2.15  misho     160: 
1.1.2.18  misho     161:        pthread_cleanup_pop(42);
1.1.2.15  misho     162:        pthread_exit(NULL);
                    163: }
                    164: 
                    165: static void *
1.1.2.5   misho     166: startSession(sched_task_t *task)
1.1.2.4   misho     167: {
1.1.2.11  misho     168:        u_char basebuf[USHRT_MAX];
1.1.2.18  misho     169:        mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf };
1.1.2.10  misho     170:        mqtthdr_connflgs_t flg;
1.1.2.13  misho     171:        mqtthdr_connack_t cack;
1.1.2.24  misho     172:        struct tagSession *s, *sess = NULL;
1.1.2.18  misho     173:        int ret;
                    174:        pthread_attr_t attr;
1.1.2.6   misho     175: 
1.1.2.20  misho     176:        ioTRACE(4);
1.1.2.6   misho     177: 
1.1.2.26! misho     178:        assert(task);
        !           179: 
1.1.2.14  misho     180:        sess = initSession(TASK_FD(task), TASK_ARG(task));
1.1.2.15  misho     181:        if (!sess) {
1.1.2.18  misho     182:                io_freeVar(TASK_ARG(task));
1.1.2.15  misho     183:                close(TASK_FD(task));
1.1.2.14  misho     184:                return NULL;
1.1.2.15  misho     185:        }
1.1.2.6   misho     186: 
1.1.2.18  misho     187:        /* receive & decode packet */
1.1.2.14  misho     188:        if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) {
1.1.2.20  misho     189:                ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18  misho     190:                finiSession(sess);
                    191:                return NULL;
1.1.2.9   misho     192:        }
1.1.2.14  misho     193:        cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid, 
1.1.2.12  misho     194:                        sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass, 
1.1.2.13  misho     195:                        &sess->sess_will.topic, &sess->sess_will.msg);
1.1.2.18  misho     196:        ret = cack.retcode;
1.1.2.14  misho     197:        flg.flags = cack.reserved;
                    198:        if (flg.reserved) {
1.1.2.20  misho     199:                ioDEBUG(3, "Error:: in MQTT protocol #%d - %s", mqtt_GetErrno(), mqtt_GetError());
1.1.2.10  misho     200:                goto end;
1.1.2.14  misho     201:        } else {
                    202:                sess->sess_clean = flg.clean_sess;
                    203:                sess->sess_will.qos = flg.will_qos;
                    204:                sess->sess_will.retain = flg.will_retain;
                    205:                sess->sess_will.flag = flg.will_flg;
1.1.2.12  misho     206:        }
1.1.2.6   misho     207: 
1.1.2.8   misho     208:        /* check online table for user */
1.1.2.18  misho     209:        if (call.LoginACC(&cfg, acc, sess->sess_user, sess->sess_pass) < 1) {
1.1.2.20  misho     210:                ioDEBUG(0, "Login:: DENIED for username %s and password %s", 
1.1.2.16  misho     211:                                sess->sess_user, sess->sess_pass);
1.1.2.21  misho     212:                ret = MQTT_RETCODE_DENIED;
1.1.2.16  misho     213:                goto end;
1.1.2.21  misho     214:        } else {
1.1.2.24  misho     215:                ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user);
1.1.2.21  misho     216:                ret = MQTT_RETCODE_ACCEPTED;
                    217:        }
1.1.2.23  misho     218:        if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) {
1.1.2.24  misho     219:                ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid);
                    220:                TAILQ_FOREACH(s, &Sessions, sess_node)
                    221:                        if (!strcmp(s->sess_cid, sess->sess_cid)) {
1.1.2.25  misho     222:                                /* found stale session & disconnect it! */
1.1.2.24  misho     223:                                stopSession(s);
                    224:                                break;
                    225:                        }
1.1.2.23  misho     226:        }
                    227:        if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr, 
                    228:                                sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg, 
                    229:                                sess->sess_will.qos, sess->sess_will.retain) == -1) {
                    230:                ioDEBUG(0, "Session %s DENIED for username %s", sess->sess_cid, sess->sess_user);
                    231:                ret = MQTT_RETCODE_DENIED;
                    232:                goto end;
                    233:        } else {
                    234:                ioDEBUG(0, "Session %s from %s and username %s is started", 
                    235:                                sess->sess_cid, sess->sess_addr, sess->sess_user);
                    236:                ret = MQTT_RETCODE_ACCEPTED;
                    237:        }
1.1.2.6   misho     238: 
1.1.2.18  misho     239:        ret = mqtt_msgCONNACK(&msg, ret);
                    240:        if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20  misho     241:                ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18  misho     242:                finiSession(sess);
                    243:                return NULL;
                    244:        } else {
1.1.2.20  misho     245:                ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19  misho     246:                free(msg.msg_base);
                    247:                msg.msg_len = 0;
1.1.2.18  misho     248:        }
                    249: 
1.1.2.15  misho     250:        /* Start session thread OK ... */
1.1.2.18  misho     251:        pthread_attr_init(&attr);
                    252:        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
                    253: 
                    254:        pthread_mutex_lock(&mtx_sess);
1.1.2.15  misho     255:        TAILQ_INSERT_TAIL(&Sessions, sess, sess_node);
1.1.2.18  misho     256:        pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess);
                    257:        pthread_mutex_unlock(&mtx_sess);
                    258: 
1.1.2.23  misho     259:        call.LOG(logg, "Session %s started from %s for user %s OK!\n", sess->sess_cid, 
                    260:                        sess->sess_addr, sess->sess_user);
1.1.2.18  misho     261: 
                    262:        pthread_attr_destroy(&attr);
1.1.2.15  misho     263:        return NULL;
1.1.2.6   misho     264: end:   /* close client connection */
1.1.2.18  misho     265:        ret = mqtt_msgCONNACK(&msg, ret);
                    266:        if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) {
1.1.2.20  misho     267:                ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno));
1.1.2.18  misho     268:        } else {
1.1.2.20  misho     269:                ioDEBUG(5, "Sended %d bytes", ret);
1.1.2.19  misho     270:                free(msg.msg_base);
                    271:                msg.msg_len = 0;
1.1.2.18  misho     272:        }
                    273: 
1.1.2.20  misho     274:        ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock);
1.1.2.15  misho     275:        finiSession(sess);
1.1.2.6   misho     276:        return NULL;
1.1.2.4   misho     277: }
                    278: 
                    279: /* ----------------------------------------------------------------------- */
                    280: 
1.1.2.5   misho     281: static void *
                    282: thrSched(void *arg __unused)
                    283: {
1.1.2.18  misho     284:        struct tagSession *sess;
1.1.2.5   misho     285: 
1.1.2.20  misho     286:        ioTRACE(1);
                    287: 
                    288:        schedRun(root, &Kill);
1.1.2.18  misho     289: 
                    290:        TAILQ_FOREACH(sess, &Sessions, sess_node)
                    291:                if (sess->sess_tid)
                    292:                        pthread_cancel(sess->sess_tid);
1.1.2.5   misho     293:        pthread_exit(NULL);
                    294: }
                    295: 
1.1.2.2   misho     296: int
1.1.2.3   misho     297: Run(int sock)
1.1.2.2   misho     298: {
1.1.2.4   misho     299:        io_sockaddr_t sa;
                    300:        socklen_t sslen = sizeof sa.ss;
                    301:        int cli;
1.1.2.5   misho     302:        pthread_t tid;
1.1.2.18  misho     303:        ait_val_t *v;
1.1.2.23  misho     304:        char str[STRSIZ];
1.1.2.4   misho     305: 
1.1.2.20  misho     306:        ioTRACE(1);
1.1.2.2   misho     307: 
1.1.2.5   misho     308:        if (pthread_create(&tid, NULL, thrSched, NULL)) {
1.1.2.20  misho     309:                ioLOGERR(0);
1.1.2.5   misho     310:                return -1;
                    311:        } else
                    312:                pthread_detach(tid);
1.1.2.20  misho     313:        ioDEBUG(2, "Run scheduler management thread");
1.1.2.5   misho     314: 
1.1.2.24  misho     315:        if (listen(sock, SOMAXCONN) == -1) {
1.1.2.20  misho     316:                ioLOGERR(0);
1.1.2.3   misho     317:                return -1;
                    318:        }
                    319: 
1.1.2.4   misho     320:        while (!Kill) {
                    321:                if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
1.1.2.20  misho     322:                        if (!Kill)
                    323:                                continue;
                    324:                        ioLOGERR(0);
                    325:                        break;
1.1.2.18  misho     326:                }
                    327:                v = io_allocVar();
                    328:                if (!v) {
                    329:                        ioLIBERR(mqtt);
                    330:                        break;
1.1.2.23  misho     331:                } else {
                    332:                        memset(str, 0, sizeof str);
                    333:                        snprintf(str, sizeof str, "%s:%hu", io_n2addr(&sa, v), io_n2port(&sa));
                    334:                        AIT_SET_STR(v, str);
                    335:                }
                    336:                ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v));
1.1.2.4   misho     337: 
1.1.2.18  misho     338:                if (!schedRead(root, startSession, v, cli)) {
                    339:                        io_freeVar(v);
1.1.2.4   misho     340:                        close(cli);
1.1.2.20  misho     341:                        ioDEBUG(1, "Terminated client with socket=%d", cli);
1.1.2.4   misho     342:                }
                    343:        }
                    344: 
1.1.2.2   misho     345:        return 0;
                    346: }

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