Annotation of mqtt/src/client.c, revision 1.4.4.1

1.4       misho       1: /*************************************************************************
                      2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.4.4.1 ! misho       6: * $Id: client.c,v 1.4 2012/07/03 12:46:00 misho Exp $
1.4       misho       7: *
                      8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
1.4.4.1 ! misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
1.4       misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
1.2       misho      46: #include "global.h"
                     47: #include "mqtt.h"
                     48: #include "client.h"
                     49: 
                     50: 
                     51: int
                     52: ConnectClient(int sock)
                     53: {
                     54:        int siz = 0;
                     55:        struct pollfd pfd;
                     56: 
                     57:        siz = mqtt_msgCONNECT(args->msg, (char*) AIT_GET_STR(&args->ConnID), args->ka, 
                     58:                        (char*) AIT_GET_STR(&args->User), (char*) AIT_GET_STR(&args->Pass), 
                     59:                        (char*) args->Will.Topic.val.string, (char*) args->Will.Msg.val.string, 
                     60:                        !args->notClear, args->QoS, args->Retain);
                     61:        if (siz == -1) {
                     62:                printf("Error:: msgCONNECT #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                     63:                return -1;
                     64:        }
                     65: 
                     66:        siz = send(sock, args->msg->msg_base, siz, 0);
                     67:        if (siz == -1) {
                     68:                printf("Error:: send() #%d - %s\n", errno, strerror(errno));
                     69:                return -1;
                     70:        } else
1.4.4.1 ! misho      71:                EVERBS(3) printf("Sended CONNECT %d bytes\n", siz);
1.2       misho      72: 
                     73:        pfd.fd = sock;
                     74:        pfd.events = POLLIN | POLLPRI;
                     75:        switch (poll(&pfd, 1, args->ka * 1000)) {
                     76:                case -1:
                     77:                        printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
                     78:                        return -1;
                     79:                case 0:
1.4.4.1 ! misho      80:                        EVERBS(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
1.2       misho      81:                        return -1;
                     82:        }
                     83:        if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
                     84:                return -1;
                     85: 
1.3       misho      86:        memset(args->msg->msg_base, 0, args->msg->msg_len);
1.2       misho      87:        siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
                     88:        if (siz == -1) {
                     89:                printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
                     90:                return -1;
                     91:        } else
1.4.4.1 ! misho      92:                EVERBS(3) printf("Received %d bytes\n", siz);
1.2       misho      93: 
                     94:        return (u_char) mqtt_readCONNACK(args->msg);
                     95: }
                     96: 
                     97: void *
                     98: OpenFile(void)
                     99: {
                    100:        int f, siz = 0;
                    101:        void *mem;
                    102: 
                    103:        if (!args->isFile)
                    104:                return NULL;
                    105: 
                    106:        f = open(AIT_GET_STR(&args->Value), O_RDONLY);
                    107:        if (f == -1) {
                    108:                printf("Error:: in open file #%d - %s\n", errno, strerror(errno));
                    109:                return NULL;
                    110:        }
                    111:        mem = mmap(NULL, siz, PROT_READ, MAP_PRIVATE, f, 0);
                    112:        if (mem == MAP_FAILED) {
                    113:                printf("Error:: in map file #%d - %s\n", errno, strerror(errno));
                    114:                close(f);
                    115:                return NULL;
                    116:        } else
                    117:                close(f);
                    118: 
                    119:        AIT_SET_PTR(&args->Value, mem, siz);
                    120:        return mem;
                    121: }
                    122: 
                    123: void
                    124: CloseFile(void)
                    125: {
                    126:        if (args->isFile) {
                    127:                munmap(AIT_GET_PTR(&args->Value), AIT_LEN(&args->Value));
                    128:                AIT_FREE_VAL(&args->Value);
                    129:        }
                    130: }
                    131: 
                    132: inline int
                    133: SendTo(int sock, int siz)
                    134: {
1.3       misho     135:        siz = send(sock, args->msg->msg_base, siz, MSG_NOSIGNAL);
1.2       misho     136:        if (siz == -1) {
                    137:                printf("Error:: send() #%d - %s\n", errno, strerror(errno));
                    138:                return -1;
                    139:        } else
1.4.4.1 ! misho     140:                EVERBS(3) printf("Sended %d bytes\n", siz);
1.2       misho     141: 
                    142:        return siz;
                    143: }
                    144: 
                    145: inline int
                    146: RecvFrom(int sock)
                    147: {
                    148:        struct pollfd pfd;
                    149:        int siz = 0;
                    150: 
1.3       misho     151:        memset(args->msg->msg_base, 0, args->msg->msg_len);
                    152: 
1.2       misho     153:        pfd.fd = sock;
                    154:        pfd.events = POLLIN | POLLPRI;
                    155:        do {
                    156:                switch (poll(&pfd, 1, args->ka * 1000)) {
                    157:                        case -1:
                    158:                                printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
                    159:                                return -1;
                    160:                        case 0:
1.4.4.1 ! misho     161:                                EVERBS(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
1.3       misho     162:                                if (mqtt_KeepAlive(sock, args->ka, 1) == -1)
1.2       misho     163:                                        return -1;
                    164:                                continue;
                    165:                }
                    166:                if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
                    167:                        return -1;
                    168:        } while (0);
                    169: 
                    170:        siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
                    171:        if (siz == -1) {
                    172:                printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
                    173:                return -1;
                    174:        } else
1.4.4.1 ! misho     175:                EVERBS(3) printf("Received %d bytes\n", siz);
1.2       misho     176: 
                    177:        return siz;
                    178: }

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