Annotation of ansh/src/ansh3.c, revision 1.3

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

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