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

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.1! misho       6:  * $Id: client3.c,v 1.1.1.1 2011/10/04 22:37:46 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);
                     30:        if (icmpSend(h, id, ANSH_FLG_WINZ, buf, sizeof ws, sa, sizeof(struct sockaddr)) == -1)
                     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       misho      46:        u_char *buf;
                     47:        socklen_t sl;
                     48: 
                     49:        FTRACE(3);
                     50: 
                     51:        if (!(buf = malloc(len))) {
                     52:                printf("Error:: no enough memory #%d - %s\n", errno, strerror(errno));
                     53:                return -1;
                     54:        }
                     55: 
                     56:        if (SetRemoteWinz(h, id, sa, buf, len) == -1) {
                     57:                free(buf);
                     58:                return -1;
                     59:        }
                     60: 
                     61:        ioSetRAWMode(STDIN_FILENO, &otio);
                     62: 
                     63:        while (!Kill) {
                     64:                FD_ZERO(&rfd);
                     65:                FD_SET(h, &rfd);
                     66:                FD_SET(STDIN_FILENO, &rfd);
                     67:                if (select(h + 1, &rfd, NULL, NULL, (Timeout ? &tv : NULL)) < 1) {
                     68:                        ret = -1;
                     69:                        break;
                     70:                }
                     71:                if (FD_ISSET(STDIN_FILENO, &rfd)) {
                     72:                        memset(buf, 0, len);
                     73:                        rlen = read(STDIN_FILENO, buf, len);
                     74:                        if (rlen == -1) {
                     75:                                printf("Error:: read(stdin) #%d - %s\n", errno, strerror(errno));
                     76:                                ret = -1;
                     77:                                break;
                     78:                        }
1.1.1.1.2.1! misho      79: 
        !            80:                        /* local command handling */
        !            81:                        if (rlen) {
        !            82:                                /* execute local command */
        !            83:                                if (nl == 2) {
        !            84:                                        switch (*buf) {
        !            85:                                                case '.':
        !            86:                                                        Kill++;
        !            87:                                                        printf("\n");
        !            88:                                                        VERB(1) LOG("Exit from client\n");
        !            89:                                                        continue;
        !            90:                                                case '~':
        !            91:                                                default:
        !            92:                                                        nl ^= nl;
        !            93:                                                        /* send buffer, unknown command  */
        !            94:                                                        break;
        !            95:                                        }
        !            96:                                }
        !            97:                                /* skip buffer and wait for local command */
        !            98:                                if (nl == 1 && *buf == '~') {
        !            99:                                        nl++;
        !           100:                                        continue;
        !           101:                                }
        !           102:                                /* send buffer if detect NL */
        !           103:                                if (*buf == 0xa || *buf == 0xd)
        !           104:                                        nl = 1;
        !           105:                                else
        !           106:                                        nl ^= nl;
        !           107:                        } else
        !           108:                                nl ^= nl;
        !           109: 
1.1       misho     110:                        rlen = icmpSend(h, id, ANSH_FLG_CPOUT, buf, rlen, sa, sizeof(struct sockaddr));
                    111:                        if (rlen == ANSH_FLG_ERR) {
                    112:                                ret = -1;
                    113:                                break;
                    114:                        }
                    115:                } else {
                    116:                        sl = sizeof s;
                    117:                        rlen = len;
                    118:                        memset(buf, 0, rlen);
                    119:                        flg = icmpRecv(h, &aid, buf, &rlen, &s, &sl);
                    120:                        if (flg == ANSH_FLG_ERR) {
                    121:                                ret = -1;
                    122:                                break;
                    123:                        }
                    124:                        if (aid != id) {
                    125:                                VERB(4) LOG("different service id %d / %d\n", aid, id);
                    126:                                continue;
                    127:                        }
                    128:                        if (flg == ANSH_FLG_EOF) {
                    129:                                VERB(3) LOG("receive EOF going down.\n");
                    130:                                Kill++;
                    131:                        }
                    132:                        if (flg != ANSH_FLG_OK)
                    133:                                continue;
                    134:                        rlen = write(STDOUT_FILENO, buf, rlen);
                    135:                        if (rlen == -1) {
                    136:                                printf("Error:: write(stdout) #%d - %s\n", errno, strerror(errno));
                    137:                                ret = -1;
                    138:                                break;
                    139:                        }
                    140:                }
                    141:        }
                    142: 
                    143:        ioRestoreMode(STDIN_FILENO, otio);
                    144:        free(buf);
                    145:        return ret;
                    146: }

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