--- ansh/src/ansh3.c 2011/10/04 22:37:46 1.1 +++ ansh/src/ansh3.c 2012/01/23 10:34:12 1.3 @@ -3,25 +3,67 @@ * by Michael Pounov * * $Author: misho $ - * $Id: ansh3.c,v 1.1 2011/10/04 22:37:46 misho Exp $ + * $Id: ansh3.c,v 1.3 2012/01/23 10:34:12 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 + +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 + by Michael Pounov . 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 +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" -int Timeout, Verbose, Kill, Crypted = 1; +intptr_t Kill; +int Timeout, Verbose; +u_int Crypted; +char Key[STRSIZ]; extern char compiled[], compiledby[], compilehost[]; static void Usage() { - printf( " -= ansh =- ELWIX Layer3 remote management client over ICMP\n" + printf( " -= ansh3 =- ELWIX Layer3 remote management client over ICMP\n" "=== %s === %s@%s ===\n\n" " Syntax: ansh [options] \n\n" + "\t-a
\tBind to address\n" "\t-i \tService ID (default is 42)\n" "\t-t \tClient session timeout (default is 0 sec)\n" + "\t-k \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" @@ -31,22 +73,56 @@ Usage() int main(int argc, char **argv) { - struct sockaddr sa = { 0 }; - struct sockaddr_in *sin4 = (struct sockaddr_in*) &sa; - struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) &sa; + io_sockaddr_t sa, ba; struct hostent *host; char ch; int h, len; u_short id = ANSH_ID; - while ((ch = getopt(argc, argv, "hvui:t:")) != -1) + 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; @@ -73,21 +149,21 @@ main(int argc, char **argv) } switch (host->h_addrtype) { case AF_INET: - sin4->sin_len = sizeof(struct sockaddr_in); - sin4->sin_family = AF_INET; - memcpy(&sin4->sin_addr.s_addr, host->h_addr, host->h_length); + 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: - sin6->sin6_len = sizeof(struct sockaddr_in6); - sin6->sin6_family = AF_INET6; - memcpy(&sin6->sin6_addr.s6_addr, host->h_addr, host->h_length); + 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(&sa, &len); + h = PrepareL3(&ba, &len); if (h == -1) { printf("Error:: Descriptor not opened ... \n"); return 1; @@ -95,7 +171,7 @@ main(int argc, char **argv) ConnectL3(h, id, &sa, len); - VERB(1) printf("Finish client.\n"); + VERB(1) printf("\r\nFinish client.\r\n"); close(h); return 0; }