File:  [ELWIX - Embedded LightWeight unIX -] / ansh / src / client2.c
Revision 1.1.1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Thu Oct 13 14:29:30 2011 UTC (12 years, 8 months ago) by misho
Branches: ansh1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
new features::
- reordering instructions in clients
- add sequence number in ansh protocol
- detect lost packets

    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.3 2011/10/13 14:29:30 misho Exp $
    7:  *
    8:  *************************************************************************/
    9: #include "global.h"
   10: 
   11: 
   12: static int
   13: SetRemoteWinz(int h, 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, 0, ANSH_FLG_WINZ, Crypted, buf, sizeof ws, ea) == -1)
   31: 		return -1;
   32: 
   33: 	return 0;
   34: }
   35: 
   36: int
   37: ConnectL2(int h, 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, nl = 0;
   45: 	u_char *buf, *str;
   46: 	u_int s, Seq = 0;
   47: 
   48: 	FTRACE(3);
   49: 
   50: 	if (!(buf = malloc(len))) {
   51: 		printf("Error:: no enough memory #%d - %s\n", errno, strerror(errno));
   52: 		return -1;
   53: 	}
   54: 
   55: 	if (SetRemoteWinz(h, ea, buf, len) == -1) {
   56: 		free(buf);
   57: 		return -1;
   58: 	}
   59: 
   60: 	ioSetRAWMode(STDIN_FILENO, &otio);
   61: 
   62: 	while (!Kill) {
   63: 		FD_ZERO(&rfd);
   64: 		FD_SET(h, &rfd);
   65: 		FD_SET(STDIN_FILENO, &rfd);
   66: 		if (select(h + 1, &rfd, NULL, NULL, (Timeout ? &tv : NULL)) < 1) {
   67: 			ret = -1;
   68: 			break;
   69: 		}
   70: 		if (FD_ISSET(h, &rfd)) {
   71: 			rlen = len;
   72: 			memset(buf, 0, rlen);
   73: 			flg = pktRecv(h, &s, &Crypted, buf, &rlen, &eth);
   74: 			if (flg == ANSH_FLG_ERR) {
   75: 				ret = -1;
   76: 				break;
   77: 			}
   78: 			if (ntohs(eth.ether_type) != ANSH_ID) {
   79: 				VERB(4) LOG("different service id %d / %d\n", eth.ether_type, ANSH_ID);
   80: 				continue;
   81: 			}
   82: 			if (flg == ANSH_FLG_EOF) {
   83: 				VERB(3) LOG("receive EOF going down.\n");
   84: 				Kill++;
   85: 			}
   86: 			if (flg != ANSH_FLG_OK)
   87: 				continue;
   88: 			if (s <= Seq)
   89: 				continue;
   90: 			else if (s > (Seq + 1))
   91: 				VERB(1) LOG("LOST PACKET(s) detect: %d; received seq=%d - %d", 
   92: 						s - (Seq + 1), s, Seq);
   93: 			Seq = s;
   94: 
   95: 			if (Crypted) {
   96: 				str = cryptBuffer(buf, rlen, Crypted);
   97: 				if (str) {
   98: 					memcpy(buf, str, rlen);
   99: 					free(str);
  100: 				}
  101: 			}
  102: 
  103: 			rlen = write(STDOUT_FILENO, buf, rlen);
  104: 			if (rlen == -1) {
  105: 				printf("Error:: write(stdout) #%d - %s\n", errno, strerror(errno));
  106: 				ret = -1;
  107: 				break;
  108: 			}
  109: 		} else {
  110: 			memset(buf, 0, len);
  111: 			rlen = read(STDIN_FILENO, buf, len);
  112: 			if (rlen == -1) {
  113: 				printf("Error:: read(stdin) #%d - %s\n", errno, strerror(errno));
  114: 				ret = -1;
  115: 				break;
  116: 			}
  117: 
  118: 			/* local command handling */
  119: 			if (rlen) {
  120: 				/* execute local command */
  121: 			       	if (nl == 2) {
  122: 					switch (*buf) {
  123: 						case '.':
  124: 							Kill++;
  125: 							printf("\n");
  126: 							VERB(1) LOG("Exit from client\n");
  127: 							continue;
  128: 						case '~':
  129: 						default:
  130: 							nl ^= nl;
  131: 							/* send buffer, unknown command  */
  132: 							break;
  133: 					}
  134: 				}
  135: 				/* skip buffer and wait for local command */
  136: 				if (nl == 1 && *buf == '~') {
  137: 					nl++;
  138: 					continue;
  139: 				}
  140: 				/* send buffer if detect NL */
  141: 				if (*buf == 0xa || *buf == 0xd)
  142: 					nl = 1;
  143: 				else
  144: 					nl ^= nl;
  145: 			} else
  146: 				nl ^= nl;
  147: 
  148: 			if (Crypted) {
  149: 				str = cryptBuffer(buf, rlen, Crypted);
  150: 				if (str) {
  151: 					memcpy(buf, str, rlen);
  152: 					free(str);
  153: 				}
  154: 			}
  155: 
  156: 			rlen = pktSend(h, ++Seq, ANSH_FLG_CPOUT, Crypted, buf, rlen, ea);
  157: 			if (rlen == ANSH_FLG_ERR) {
  158: 				ret = -1;
  159: 				break;
  160: 			}
  161: 		}
  162: 	}
  163: 
  164: 	ioRestoreMode(STDIN_FILENO, otio);
  165: 	free(buf);
  166: 	return ret;
  167: }

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