File:  [ELWIX - Embedded LightWeight unIX -] / ansh / src / client2.c
Revision 1.1.1.1.2.4: download - view: text, annotated - select for diffs - revision graph
Thu Oct 13 16:08:52 2011 UTC (12 years, 11 months ago) by misho
Branches: ansh1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
added BSD license

    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.1.1.1.2.4 2011/10/13 16:08:52 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, struct ether_addr *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, struct ether_addr *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, *str;
   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: 			if (Crypted) {
  133: 				str = cryptBuffer(buf, rlen, Crypted);
  134: 				if (str) {
  135: 					memcpy(buf, str, rlen);
  136: 					free(str);
  137: 				}
  138: 			}
  139: 
  140: 			rlen = write(STDOUT_FILENO, buf, rlen);
  141: 			if (rlen == -1) {
  142: 				printf("Error:: write(stdout) #%d - %s\n", errno, strerror(errno));
  143: 				ret = -1;
  144: 				break;
  145: 			}
  146: 		} else {
  147: 			memset(buf, 0, len);
  148: 			rlen = read(STDIN_FILENO, buf, len);
  149: 			if (rlen == -1) {
  150: 				printf("Error:: read(stdin) #%d - %s\n", errno, strerror(errno));
  151: 				ret = -1;
  152: 				break;
  153: 			}
  154: 
  155: 			/* local command handling */
  156: 			if (rlen) {
  157: 				/* execute local command */
  158: 			       	if (nl == 2) {
  159: 					switch (*buf) {
  160: 						case '.':
  161: 							Kill++;
  162: 							printf("\n");
  163: 							VERB(1) LOG("Exit from client\n");
  164: 							continue;
  165: 						case '~':
  166: 						default:
  167: 							nl ^= nl;
  168: 							/* send buffer, unknown command  */
  169: 							break;
  170: 					}
  171: 				}
  172: 				/* skip buffer and wait for local command */
  173: 				if (nl == 1 && *buf == '~') {
  174: 					nl++;
  175: 					continue;
  176: 				}
  177: 				/* send buffer if detect NL */
  178: 				if (*buf == 0xa || *buf == 0xd)
  179: 					nl = 1;
  180: 				else
  181: 					nl ^= nl;
  182: 			} else
  183: 				nl ^= nl;
  184: 
  185: 			if (Crypted) {
  186: 				str = cryptBuffer(buf, rlen, Crypted);
  187: 				if (str) {
  188: 					memcpy(buf, str, rlen);
  189: 					free(str);
  190: 				}
  191: 			}
  192: 
  193: 			rlen = pktSend(h, ++Seq, ANSH_FLG_CPOUT, Crypted, buf, rlen, ea);
  194: 			if (rlen == ANSH_FLG_ERR) {
  195: 				ret = -1;
  196: 				break;
  197: 			}
  198: 		}
  199: 	}
  200: 
  201: 	ioRestoreMode(STDIN_FILENO, otio);
  202: 	free(buf);
  203: 	return ret;
  204: }

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