Annotation of ansh/src/client3.c, revision 1.1.1.1.2.3

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.1.1.1.2.3! misho       6:  * $Id: client3.c,v 1.1.1.1.2.2 2011/10/07 13:41:26 misho Exp $
1.1       misho       7:  *
                      8:  *************************************************************************/
                      9: #include "global.h"
                     10: 
                     11: 
                     12: static int
                     13: SetRemoteWinz(int h, u_short id, struct sockaddr *sa, u_char *buf, int buflen)
                     14: {
                     15:        u_short *pos = (u_short*) buf;
                     16:        struct winsize ws;
                     17: 
                     18:        FTRACE(5);
                     19: 
                     20:        memset(buf, 0, buflen);
                     21:        if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) {
                     22:                printf("Error:: ioctl(winsize) #%d - %s\n", errno, strerror(errno));
                     23:                return -1;
                     24:        }
                     25: 
                     26:        pos[0] = htons(ws.ws_row);
                     27:        pos[1] = htons(ws.ws_col);
                     28:        pos[2] = htons(ws.ws_xpixel);
                     29:        pos[3] = htons(ws.ws_ypixel);
1.1.1.1.2.3! misho      30:        if (icmpSend(h, 0, id, ANSH_FLG_WINZ, Crypted, buf, sizeof ws, sa, sizeof(struct sockaddr)) == -1)
1.1       misho      31:                return -1;
                     32: 
                     33:        return 0;
                     34: }
                     35: 
                     36: int
                     37: ConnectL3(int h, u_short id, struct sockaddr *sa, int len)
                     38: {
                     39:        fd_set rfd;
                     40:        struct timeval tv = { Timeout, 0 };
                     41:        struct termios otio;
                     42:        int rlen, ret = 0;
                     43:        u_short aid;
                     44:        struct sockaddr s;
1.1.1.1.2.1  misho      45:        char flg, nl = 0;
1.1.1.1.2.2  misho      46:        u_char *buf, *str;
1.1       misho      47:        socklen_t sl;
1.1.1.1.2.3! misho      48:        u_int sq, Seq = 0;
1.1       misho      49: 
                     50:        FTRACE(3);
                     51: 
                     52:        if (!(buf = malloc(len))) {
                     53:                printf("Error:: no enough memory #%d - %s\n", errno, strerror(errno));
                     54:                return -1;
                     55:        }
                     56: 
                     57:        if (SetRemoteWinz(h, id, sa, buf, len) == -1) {
                     58:                free(buf);
                     59:                return -1;
                     60:        }
                     61: 
                     62:        ioSetRAWMode(STDIN_FILENO, &otio);
                     63: 
                     64:        while (!Kill) {
                     65:                FD_ZERO(&rfd);
                     66:                FD_SET(h, &rfd);
                     67:                FD_SET(STDIN_FILENO, &rfd);
                     68:                if (select(h + 1, &rfd, NULL, NULL, (Timeout ? &tv : NULL)) < 1) {
                     69:                        ret = -1;
                     70:                        break;
                     71:                }
1.1.1.1.2.3! misho      72:                if (FD_ISSET(h, &rfd)) {
        !            73:                        sl = sizeof s;
        !            74:                        rlen = len;
        !            75:                        memset(buf, 0, rlen);
        !            76:                        flg = icmpRecv(h, &sq, &aid, &Crypted, buf, &rlen, &s, &sl);
        !            77:                        if (flg == ANSH_FLG_ERR) {
        !            78:                                ret = -1;
        !            79:                                break;
        !            80:                        }
        !            81:                        if (aid != id) {
        !            82:                                VERB(4) LOG("different service id %d / %d\n", aid, id);
        !            83:                                continue;
        !            84:                        }
        !            85:                        if (flg == ANSH_FLG_EOF) {
        !            86:                                VERB(3) LOG("receive EOF going down.\n");
        !            87:                                Kill++;
        !            88:                        }
        !            89:                        if (flg != ANSH_FLG_OK)
        !            90:                                continue;
        !            91:                        if (sq <= Seq)
        !            92:                                continue;
        !            93:                        else if (sq > (Seq + 1))
        !            94:                                VERB(1) LOG("LOST PACKET(s) detect: %d; received seq=%d - %d", 
        !            95:                                                sq - Seq + 1, sq, Seq);
        !            96:                        Seq = sq;
        !            97: 
        !            98:                        if (Crypted) {
        !            99:                                str = cryptBuffer(buf, rlen, Crypted);
        !           100:                                if (str) {
        !           101:                                        memcpy(buf, str, rlen);
        !           102:                                        free(str);
        !           103:                                }
        !           104:                        }
        !           105: 
        !           106:                        rlen = write(STDOUT_FILENO, buf, rlen);
        !           107:                        if (rlen == -1) {
        !           108:                                printf("Error:: write(stdout) #%d - %s\n", errno, strerror(errno));
        !           109:                                ret = -1;
        !           110:                                break;
        !           111:                        }
        !           112:                } else {
1.1       misho     113:                        memset(buf, 0, len);
                    114:                        rlen = read(STDIN_FILENO, buf, len);
                    115:                        if (rlen == -1) {
                    116:                                printf("Error:: read(stdin) #%d - %s\n", errno, strerror(errno));
                    117:                                ret = -1;
                    118:                                break;
                    119:                        }
1.1.1.1.2.1  misho     120: 
                    121:                        /* local command handling */
                    122:                        if (rlen) {
                    123:                                /* execute local command */
                    124:                                if (nl == 2) {
                    125:                                        switch (*buf) {
                    126:                                                case '.':
                    127:                                                        Kill++;
                    128:                                                        printf("\n");
                    129:                                                        VERB(1) LOG("Exit from client\n");
                    130:                                                        continue;
                    131:                                                case '~':
                    132:                                                default:
                    133:                                                        nl ^= nl;
                    134:                                                        /* send buffer, unknown command  */
                    135:                                                        break;
                    136:                                        }
                    137:                                }
                    138:                                /* skip buffer and wait for local command */
                    139:                                if (nl == 1 && *buf == '~') {
                    140:                                        nl++;
                    141:                                        continue;
                    142:                                }
                    143:                                /* send buffer if detect NL */
                    144:                                if (*buf == 0xa || *buf == 0xd)
                    145:                                        nl = 1;
                    146:                                else
                    147:                                        nl ^= nl;
                    148:                        } else
                    149:                                nl ^= nl;
                    150: 
1.1.1.1.2.2  misho     151:                        if (Crypted) {
                    152:                                str = cryptBuffer(buf, rlen, Crypted);
                    153:                                if (str) {
                    154:                                        memcpy(buf, str, rlen);
                    155:                                        free(str);
                    156:                                }
                    157:                        }
                    158: 
1.1.1.1.2.3! misho     159:                        rlen = icmpSend(h, ++Seq, id, ANSH_FLG_CPOUT, Crypted, buf, rlen, sa, sizeof(struct sockaddr));
1.1       misho     160:                        if (rlen == ANSH_FLG_ERR) {
                    161:                                ret = -1;
                    162:                                break;
                    163:                        }
                    164:                }
                    165:        }
                    166: 
                    167:        ioRestoreMode(STDIN_FILENO, otio);
                    168:        free(buf);
                    169:        return ret;
                    170: }

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