Annotation of ansh/src/client2.c, revision 1.1.1.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 $
                      6:  * $Id: global.h,v 1.2 2011/06/08 12:45:40 misho Exp $
                      7:  *
                      8:  *************************************************************************/
                      9: #include "global.h"
                     10: 
                     11: 
                     12: static int
                     13: SetRemoteWinz(int h, u_short id, struct ether_addr *ea, 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 (pktSend(h, id, ANSH_FLG_WINZ, buf, sizeof ws, ea) == -1)
                     31:                return -1;
                     32: 
                     33:        return 0;
                     34: }
                     35: 
                     36: int
                     37: ConnectL2(int h, u_short id, struct ether_addr *ea, int len)
                     38: {
                     39:        fd_set rfd;
                     40:        struct ether_header eth;
                     41:        struct timeval tv = { 10, 0 };
                     42:        struct termios otio;
                     43:        int rlen, ret = 0;
                     44:        char flg;
                     45:        u_char *buf;
                     46: 
                     47:        FTRACE(3);
                     48: 
                     49:        if (!(buf = malloc(len))) {
                     50:                printf("Error:: no enough memory #%d - %s\n", errno, strerror(errno));
                     51:                return -1;
                     52:        }
                     53: 
                     54:        if (SetRemoteWinz(h, id, ea, buf, len) == -1) {
                     55:                free(buf);
                     56:                return -1;
                     57:        }
                     58: 
                     59:        ioSetRAWMode(STDIN_FILENO, &otio);
                     60: 
                     61:        while (!Kill) {
                     62:                FD_ZERO(&rfd);
                     63:                FD_SET(h, &rfd);
                     64:                FD_SET(STDIN_FILENO, &rfd);
                     65:                if (select(h + 1, &rfd, NULL, NULL, &tv) < 1) {
                     66:                        ret = -1;
                     67:                        break;
                     68:                }
                     69:                if (FD_ISSET(STDIN_FILENO, &rfd)) {
                     70:                        memset(buf, 0, len);
                     71:                        rlen = read(STDIN_FILENO, buf, len);
                     72:                        if (rlen == -1) {
                     73:                                printf("Error:: read(stdin) #%d - %s\n", errno, strerror(errno));
                     74:                                ret = -1;
                     75:                                break;
                     76:                        }
                     77:                        rlen = pktSend(h, id, ANSH_FLG_CPOUT, buf, rlen, ea);
                     78:                        if (rlen == ANSH_FLG_ERR) {
                     79:                                ret = -1;
                     80:                                break;
                     81:                        }
                     82:                } else {
                     83:                        rlen = len;
                     84:                        memset(buf, 0, rlen);
                     85:                        flg = pktRecv(h, buf, &rlen, &eth);
                     86:                        if (flg == ANSH_FLG_ERR) {
                     87:                                ret = -1;
                     88:                                break;
                     89:                        }
                     90:                        if (ntohs(eth.ether_type) != id) {
                     91:                                VERB(4) LOG("different service id %d / %d\n", ntohs(eth.ether_type), id);
                     92:                                continue;
                     93:                        }
                     94:                        if (flg == ANSH_FLG_EOF) {
                     95:                                VERB(3) LOG("receive EOF going down.\n");
                     96:                                Kill++;
                     97:                        }
                     98:                        if (flg != ANSH_FLG_OK)
                     99:                                continue;
                    100:                        rlen = write(STDOUT_FILENO, buf, rlen);
                    101:                        if (rlen == -1) {
                    102:                                printf("Error:: write(stdout) #%d - %s\n", errno, strerror(errno));
                    103:                                ret = -1;
                    104:                                break;
                    105:                        }
                    106:                }
                    107:        }
                    108: 
                    109:        ioRestoreMode(STDIN_FILENO, otio);
                    110:        free(buf);
                    111:        return ret;
                    112: }

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