File:  [ELWIX - Embedded LightWeight unIX -] / ansh / src / ansh3.c
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Tue May 19 23:25:30 2015 UTC (9 years ago) by misho
Branches: MAIN
CVS tags: ansh2_1, HEAD, ANSH2_0
version 2.0

/*************************************************************************
 * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
 *  by Michael Pounov <misho@elwix.org>
 *
 * $Author: misho $
 * $Id: ansh3.c,v 1.4 2015/05/19 23:25:30 misho Exp $
 *
 *************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"
#include "ansh.h"


intptr_t Kill;
int Timeout, Verbose;
u_int Crypted;
char Key[STRSIZ];

extern char compiled[], compiledby[], compilehost[];

static void
Usage()
{
	printf(	" -= ansh3 =- ELWIX Layer3 remote management client over ICMP\n"
		"=== %s === %s@%s ===\n\n"
		" Syntax: ansh [options] <connect2host>\n\n"
		"\t-a <address>\tBind to address\n"
		"\t-i <id>\tService ID (default is 42)\n"
		"\t-t <timeout>\tClient session timeout (default is 0 sec)\n"
		"\t-k <key>\tService cipher key\n"
		"\t-u\t\tSwitch to unencrypted traffic between hosts\n"
		"\t-v\t\tVerbose (more -v, more verbosity ...)\n"
		"\t-h\t\tThis help screen!\n"
		"\n", compiled, compiledby, compilehost);
}

int
main(int argc, char **argv)
{
	sockaddr_t sa, ba;
	struct hostent *host;
	char ch;
	int h, len;
	u_short id = ANSH_ID;

	memset(&sa, 0, sizeof sa);
	memset(&ba, 0, sizeof ba);

	srandomdev();
	do {
		Crypted = (u_int) random() % UINT_MAX;
	} while (!Crypted);

	strlcpy(Key, DEFAULT_KEY, sizeof Key);

	while ((ch = getopt(argc, argv, "hvui:t:k:a:")) != -1)
		switch (ch) {
			case 'a':
				host = gethostbyname(optarg);
				if (!host) {
					printf("Error:: in bind address '%s' #%d - %s\n",
						optarg, h_errno, hstrerror(h_errno));
					return 1;
				}
				switch (host->h_addrtype) {
					case AF_INET:
						ba.sin.sin_len = sizeof(struct sockaddr_in);
						ba.sin.sin_family = AF_INET;
						memcpy(&ba.sin.sin_addr.s_addr, host->h_addr, host->h_length);
						break;
					case AF_INET6:
						ba.sin6.sin6_len = sizeof(struct sockaddr_in6);
						ba.sin6.sin6_family = AF_INET6;
						memcpy(&ba.sin6.sin6_addr.s6_addr, host->h_addr, host->h_length);
						break;
					default:
						printf("Error:: Unknown address type %d !!!\n", host->h_addrtype);
						return 1;
				}
				break;
			case 't':
				Timeout = abs(strtol(optarg, NULL, 0));
				break;
			case 'i':
				id = strtol(optarg, NULL, 0);
				break;
			case 'k':
				strlcpy(Key, optarg, sizeof Key);
				break;
			case 'u':
				Crypted ^= Crypted;
				break;
			case 'v':
				Verbose++;
				break;
			case 'h':
			default:
				Usage();
				return 1;
		}
	argc -= optind;
	argv += optind;
	if (!argc) {
		printf("Error:: not specified address for connect ...\n");
		return 1;
	}

	host = gethostbyname(argv[0]);
	if (!host) {
		printf("Error:: in bind address '%s' #%d - %s\n",
				argv[0], h_errno, hstrerror(h_errno));
		return 1;
	}
	switch (host->h_addrtype) {
		case AF_INET:
			sa.sin.sin_len = sizeof(struct sockaddr_in);
			sa.sin.sin_family = AF_INET;
			memcpy(&sa.sin.sin_addr.s_addr, host->h_addr, host->h_length);
			break;
		case AF_INET6:
			sa.sin6.sin6_len = sizeof(struct sockaddr_in6);
			sa.sin6.sin6_family = AF_INET6;
			memcpy(&sa.sin6.sin6_addr.s6_addr, host->h_addr, host->h_length);
			break;
		default:
			printf("Error:: Unknown address type %d !!!\n", host->h_addrtype);
			return 1;
	}

	h = PrepareL3(&ba, &len);
	if (h == -1) {
		printf("Error:: Descriptor not opened ... \n");
		return 1;
	}

	ConnectL3(h, id, &sa, len);

	VERB(1) printf("\r\nFinish client.\r\n");
	close(h);
	return 0;
}

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