Annotation of ansh/src/client2.c, revision 1.2

1.1       misho       1: /*************************************************************************
                      2:  * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
                      3:  *  by Michael Pounov <misho@elwix.org>
                      4:  *
                      5:  * $Author: misho $
1.2     ! misho       6:  * $Id: client2.c,v 1.1.1.1.2.6 2011/10/17 09:28:19 misho Exp $
1.1       misho       7:  *
1.2     ! misho       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: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
        !            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.1       misho      46: #include "global.h"
                     47: 
                     48: 
                     49: static int
1.2     ! misho      50: SetRemoteWinz(int h, struct io_ether_addr *ea, u_char *buf, int buflen)
1.1       misho      51: {
                     52:        u_short *pos = (u_short*) buf;
                     53:        struct winsize ws;
                     54: 
                     55:        FTRACE(5);
                     56: 
                     57:        memset(buf, 0, buflen);
                     58:        if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) {
                     59:                printf("Error:: ioctl(winsize) #%d - %s\n", errno, strerror(errno));
                     60:                return -1;
                     61:        }
                     62: 
                     63:        pos[0] = htons(ws.ws_row);
                     64:        pos[1] = htons(ws.ws_col);
                     65:        pos[2] = htons(ws.ws_xpixel);
                     66:        pos[3] = htons(ws.ws_ypixel);
1.2     ! misho      67:        if (pktSend(h, 0, ANSH_FLG_WINZ, Crypted, buf, sizeof ws, ea) == -1)
1.1       misho      68:                return -1;
                     69: 
                     70:        return 0;
                     71: }
                     72: 
                     73: int
1.2     ! misho      74: ConnectL2(int h, struct io_ether_addr *ea, int len)
1.1       misho      75: {
                     76:        fd_set rfd;
                     77:        struct ether_header eth;
                     78:        struct timeval tv = { 10, 0 };
                     79:        struct termios otio;
                     80:        int rlen, ret = 0;
1.2     ! misho      81:        char flg, nl = 0;
1.1       misho      82:        u_char *buf;
1.2     ! misho      83:        u_int s, Seq = 0;
1.1       misho      84: 
                     85:        FTRACE(3);
                     86: 
                     87:        if (!(buf = malloc(len))) {
                     88:                printf("Error:: no enough memory #%d - %s\n", errno, strerror(errno));
                     89:                return -1;
                     90:        }
                     91: 
1.2     ! misho      92:        if (SetRemoteWinz(h, ea, buf, len) == -1) {
1.1       misho      93:                free(buf);
                     94:                return -1;
                     95:        }
                     96: 
                     97:        ioSetRAWMode(STDIN_FILENO, &otio);
                     98: 
                     99:        while (!Kill) {
                    100:                FD_ZERO(&rfd);
                    101:                FD_SET(h, &rfd);
                    102:                FD_SET(STDIN_FILENO, &rfd);
1.2     ! misho     103:                if (select(h + 1, &rfd, NULL, NULL, (Timeout ? &tv : NULL)) < 1) {
1.1       misho     104:                        ret = -1;
                    105:                        break;
                    106:                }
1.2     ! misho     107:                if (FD_ISSET(h, &rfd)) {
1.1       misho     108:                        rlen = len;
                    109:                        memset(buf, 0, rlen);
1.2     ! misho     110:                        flg = pktRecv(h, &s, &Crypted, buf, &rlen, &eth);
1.1       misho     111:                        if (flg == ANSH_FLG_ERR) {
                    112:                                ret = -1;
                    113:                                break;
                    114:                        }
1.2     ! misho     115:                        if (ntohs(eth.ether_type) != ANSH_ID) {
        !           116:                                VERB(4) LOG("different service id %d / %d\n", eth.ether_type, ANSH_ID);
1.1       misho     117:                                continue;
                    118:                        }
                    119:                        if (flg == ANSH_FLG_EOF) {
                    120:                                VERB(3) LOG("receive EOF going down.\n");
                    121:                                Kill++;
                    122:                        }
                    123:                        if (flg != ANSH_FLG_OK)
                    124:                                continue;
1.2     ! misho     125:                        if (s <= Seq)
        !           126:                                continue;
        !           127:                        else if (s > (Seq + 1))
        !           128:                                VERB(1) LOG("LOST PACKET(s) detect: %d; received seq=%d - %d", 
        !           129:                                                s - (Seq + 1), s, Seq);
        !           130:                        Seq = s;
        !           131: 
1.1       misho     132:                        rlen = write(STDOUT_FILENO, buf, rlen);
                    133:                        if (rlen == -1) {
                    134:                                printf("Error:: write(stdout) #%d - %s\n", errno, strerror(errno));
                    135:                                ret = -1;
                    136:                                break;
                    137:                        }
1.2     ! misho     138:                } else {
        !           139:                        memset(buf, 0, len);
        !           140:                        rlen = read(STDIN_FILENO, buf, len);
        !           141:                        if (rlen == -1) {
        !           142:                                printf("Error:: read(stdin) #%d - %s\n", errno, strerror(errno));
        !           143:                                ret = -1;
        !           144:                                break;
        !           145:                        }
        !           146: 
        !           147:                        /* local command handling */
        !           148:                        if (rlen) {
        !           149:                                /* execute local command */
        !           150:                                if (nl == 2) {
        !           151:                                        switch (*buf) {
        !           152:                                                case '.':
        !           153:                                                        Kill++;
        !           154:                                                        printf("\n");
        !           155:                                                        VERB(1) LOG("Exit from client\n");
        !           156:                                                        continue;
        !           157:                                                case '~':
        !           158:                                                default:
        !           159:                                                        nl ^= nl;
        !           160:                                                        /* send buffer, unknown command  */
        !           161:                                                        break;
        !           162:                                        }
        !           163:                                }
        !           164:                                /* skip buffer and wait for local command */
        !           165:                                if (nl == 1 && *buf == '~') {
        !           166:                                        nl++;
        !           167:                                        continue;
        !           168:                                }
        !           169:                                /* send buffer if detect NL */
        !           170:                                if (*buf == 0xa || *buf == 0xd)
        !           171:                                        nl = 1;
        !           172:                                else
        !           173:                                        nl ^= nl;
        !           174:                        } else
        !           175:                                nl ^= nl;
        !           176: 
        !           177:                        rlen = pktSend(h, ++Seq, ANSH_FLG_CPOUT, Crypted, buf, rlen, ea);
        !           178:                        if (rlen == ANSH_FLG_ERR) {
        !           179:                                ret = -1;
        !           180:                                break;
        !           181:                        }
1.1       misho     182:                }
                    183:        }
                    184: 
                    185:        ioRestoreMode(STDIN_FILENO, otio);
                    186:        free(buf);
                    187:        return ret;
                    188: }

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