File:  [ELWIX - Embedded LightWeight unIX -] / ansh / src / client2.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Mon Jan 23 10:34:12 2012 UTC (12 years, 5 months ago) by misho
Branches: MAIN
CVS tags: ansh1_2, HEAD, ANSH1_1
version 1.1

    1: /*************************************************************************
    2:  * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
    3:  *  by Michael Pounov <misho@elwix.org>
    4:  *
    5:  * $Author: misho $
    6:  * $Id: client2.c,v 1.3 2012/01/23 10:34:12 misho Exp $
    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: 
   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: */
   46: #include "global.h"
   47: 
   48: 
   49: static int
   50: SetRemoteWinz(int h, io_ether_addr_t *ea, u_char *buf, int buflen)
   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);
   67: 	if (pktSend(h, 0, ANSH_FLG_WINZ, Crypted, buf, sizeof ws, ea) == -1)
   68: 		return -1;
   69: 
   70: 	return 0;
   71: }
   72: 
   73: int
   74: ConnectL2(int h, io_ether_addr_t *ea, int len)
   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;
   81: 	char flg, nl = 0;
   82: 	u_char *buf;
   83: 	u_int s, Seq = 0;
   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: 
   92: 	if (SetRemoteWinz(h, ea, buf, len) == -1) {
   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);
  103: 		if (select(h + 1, &rfd, NULL, NULL, (Timeout ? &tv : NULL)) < 1) {
  104: 			ret = -1;
  105: 			break;
  106: 		}
  107: 		if (FD_ISSET(h, &rfd)) {
  108: 			rlen = len;
  109: 			memset(buf, 0, rlen);
  110: 			flg = pktRecv(h, &s, &Crypted, buf, &rlen, &eth);
  111: 			if (flg == ANSH_FLG_ERR) {
  112: 				ret = -1;
  113: 				break;
  114: 			}
  115: 			if (ntohs(eth.ether_type) != ANSH_ID) {
  116: 				VERB(4) LOG("different service id %d / %d\n", eth.ether_type, ANSH_ID);
  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;
  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: 
  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: 			}
  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: 			}
  182: 		}
  183: 	}
  184: 
  185: 	ioRestoreMode(STDIN_FILENO, otio);
  186: 	free(buf);
  187: 	return ret;
  188: }

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