File:  [ELWIX - Embedded LightWeight unIX -] / ansh / src / ansh3.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Mon Oct 17 20:14:02 2011 UTC (12 years, 8 months ago) by misho
Branches: MAIN
CVS tags: ansh1_1, HEAD, ANSH1_0
ver 1.0

    1: /*************************************************************************
    2:  * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
    3:  *  by Michael Pounov <misho@elwix.org>
    4:  *
    5:  * $Author: misho $
    6:  * $Id: ansh3.c,v 1.2 2011/10/17 20:14:02 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: #include "ansh.h"
   48: 
   49: 
   50: intptr_t Kill;
   51: int Timeout, Verbose;
   52: u_int Crypted;
   53: char Key[STRSIZ];
   54: 
   55: extern char compiled[], compiledby[], compilehost[];
   56: 
   57: static void
   58: Usage()
   59: {
   60: 	printf(	" -= ansh =- ELWIX Layer3 remote management client over ICMP\n"
   61: 		"=== %s === %s@%s ===\n\n"
   62: 		" Syntax: ansh [options] <connect2host>\n\n"
   63: 		"\t-i <id>\tService ID (default is 42)\n"
   64: 		"\t-t <timeout>\tClient session timeout (default is 0 sec)\n"
   65: 		"\t-k <key>\tService cipher key\n"
   66: 		"\t-u\t\tSwitch to unencrypted traffic between hosts\n"
   67: 		"\t-v\t\tVerbose (more -v, more verbosity ...)\n"
   68: 		"\t-h\t\tThis help screen!\n"
   69: 		"\n", compiled, compiledby, compilehost);
   70: }
   71: 
   72: int
   73: main(int argc, char **argv)
   74: {
   75: 	struct sockaddr sa = { 0 };
   76: 	struct sockaddr_in *sin4 = (struct sockaddr_in*) &sa;
   77: 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) &sa;
   78: 	struct hostent *host;
   79: 	char ch;
   80: 	int h, len;
   81: 	u_short id = ANSH_ID;
   82: 
   83: 	srandomdev();
   84: 	do {
   85: 		Crypted = (u_int) random() % UINT_MAX;
   86: 	} while (!Crypted);
   87: 
   88: 	strlcpy(Key, DEFAULT_KEY, sizeof Key);
   89: 
   90: 	while ((ch = getopt(argc, argv, "hvui:t:k:")) != -1)
   91: 		switch (ch) {
   92: 			case 't':
   93: 				Timeout = abs(strtol(optarg, NULL, 0));
   94: 				break;
   95: 			case 'i':
   96: 				id = strtol(optarg, NULL, 0);
   97: 				break;
   98: 			case 'k':
   99: 				strlcpy(Key, optarg, sizeof Key);
  100: 				break;
  101: 			case 'u':
  102: 				Crypted ^= Crypted;
  103: 				break;
  104: 			case 'v':
  105: 				Verbose++;
  106: 				break;
  107: 			case 'h':
  108: 			default:
  109: 				Usage();
  110: 				return 1;
  111: 		}
  112: 	argc -= optind;
  113: 	argv += optind;
  114: 	if (!argc) {
  115: 		printf("Error:: not specified address for connect ...\n");
  116: 		return 1;
  117: 	}
  118: 
  119: 	host = gethostbyname(argv[0]);
  120: 	if (!host) {
  121: 		printf("Error:: in bind address '%s' #%d - %s\n",
  122: 				argv[0], h_errno, hstrerror(h_errno));
  123: 		return 1;
  124: 	}
  125: 	switch (host->h_addrtype) {
  126: 		case AF_INET:
  127: 			sin4->sin_len = sizeof(struct sockaddr_in);
  128: 			sin4->sin_family = AF_INET;
  129: 			memcpy(&sin4->sin_addr.s_addr, host->h_addr, host->h_length);
  130: 			break;
  131: 		case AF_INET6:
  132: 			sin6->sin6_len = sizeof(struct sockaddr_in6);
  133: 			sin6->sin6_family = AF_INET6;
  134: 			memcpy(&sin6->sin6_addr.s6_addr, host->h_addr, host->h_length);
  135: 			break;
  136: 		default:
  137: 			printf("Error:: Unknown address type %d !!!\n", host->h_addrtype);
  138: 			return 1;
  139: 	}
  140: 
  141: 	h = PrepareL3(&sa, &len);
  142: 	if (h == -1) {
  143: 		printf("Error:: Descriptor not opened ... \n");
  144: 		return 1;
  145: 	}
  146: 
  147: 	ConnectL3(h, id, &sa, len);
  148: 
  149: 	VERB(1) printf("\r\nFinish client.\r\n");
  150: 	close(h);
  151: 	return 0;
  152: }

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