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

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.2.2.1 ! misho       6:  * $Id: ansh3.c,v 1.2 2011/10/17 20:14:02 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.2.2.1 ! 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.2.2.1 ! 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.2.2.1 ! misho      76:        struct sockaddr sa = { 0 }, *ba = NULL;
        !            77:        struct sockaddr_in *sin4;
        !            78:        struct sockaddr_in6 *sin6;
1.1       misho      79:        struct hostent *host;
                     80:        char ch;
                     81:        int h, len;
                     82:        u_short id = ANSH_ID;
                     83: 
1.2       misho      84:        srandomdev();
                     85:        do {
                     86:                Crypted = (u_int) random() % UINT_MAX;
                     87:        } while (!Crypted);
                     88: 
                     89:        strlcpy(Key, DEFAULT_KEY, sizeof Key);
                     90: 
1.2.2.1 ! misho      91:        while ((ch = getopt(argc, argv, "hvui:t:k:a:")) != -1)
1.1       misho      92:                switch (ch) {
1.2.2.1 ! misho      93:                        case 'a':
        !            94:                                host = gethostbyname(optarg);
        !            95:                                if (!host) {
        !            96:                                        printf("Error:: in bind address '%s' #%d - %s\n",
        !            97:                                                optarg, h_errno, hstrerror(h_errno));
        !            98:                                        return 1;
        !            99:                                }
        !           100:                                switch (host->h_addrtype) {
        !           101:                                        case AF_INET:
        !           102:                                                ba = malloc(sizeof(struct sockaddr_in));
        !           103:                                                if (!ba) {
        !           104:                                                        printf("Error:: malloc() #%d - %s ...\n", 
        !           105:                                                                        errno, strerror(errno));
        !           106:                                                        return 1;
        !           107:                                                } else {
        !           108:                                                        memset(ba, 0, sizeof(struct sockaddr_in));
        !           109:                                                        sin4 = (struct sockaddr_in*) ba;
        !           110:                                                }
        !           111:                                                sin4->sin_len = sizeof(struct sockaddr_in);
        !           112:                                                sin4->sin_family = AF_INET;
        !           113:                                                memcpy(&sin4->sin_addr.s_addr, host->h_addr, host->h_length);
        !           114:                                                break;
        !           115:                                        case AF_INET6:
        !           116:                                                ba = malloc(sizeof(struct sockaddr_in6));
        !           117:                                                if (!ba) {
        !           118:                                                        printf("Error:: malloc() #%d - %s ...\n", 
        !           119:                                                                        errno, strerror(errno));
        !           120:                                                        return 1;
        !           121:                                                } else {
        !           122:                                                        memset(ba, 0, sizeof(struct sockaddr_in6));
        !           123:                                                        sin6 = (struct sockaddr_in6*) ba;
        !           124:                                                }
        !           125:                                                sin6->sin6_len = sizeof(struct sockaddr_in6);
        !           126:                                                sin6->sin6_family = AF_INET6;
        !           127:                                                memcpy(&sin6->sin6_addr.s6_addr, host->h_addr, host->h_length);
        !           128:                                                break;
        !           129:                                        default:
        !           130:                                                printf("Error:: Unknown address type %d !!!\n", host->h_addrtype);
        !           131:                                                return 1;
        !           132:                                }
        !           133:                                break;
1.1       misho     134:                        case 't':
                    135:                                Timeout = abs(strtol(optarg, NULL, 0));
                    136:                                break;
                    137:                        case 'i':
                    138:                                id = strtol(optarg, NULL, 0);
                    139:                                break;
1.2       misho     140:                        case 'k':
                    141:                                strlcpy(Key, optarg, sizeof Key);
                    142:                                break;
1.1       misho     143:                        case 'u':
                    144:                                Crypted ^= Crypted;
                    145:                                break;
                    146:                        case 'v':
                    147:                                Verbose++;
                    148:                                break;
                    149:                        case 'h':
                    150:                        default:
                    151:                                Usage();
                    152:                                return 1;
                    153:                }
                    154:        argc -= optind;
                    155:        argv += optind;
                    156:        if (!argc) {
                    157:                printf("Error:: not specified address for connect ...\n");
1.2.2.1 ! misho     158:                if (ba)
        !           159:                        free(ba);
1.1       misho     160:                return 1;
                    161:        }
                    162: 
                    163:        host = gethostbyname(argv[0]);
                    164:        if (!host) {
                    165:                printf("Error:: in bind address '%s' #%d - %s\n",
                    166:                                argv[0], h_errno, hstrerror(h_errno));
1.2.2.1 ! misho     167:                if (ba)
        !           168:                        free(ba);
1.1       misho     169:                return 1;
                    170:        }
                    171:        switch (host->h_addrtype) {
                    172:                case AF_INET:
1.2.2.1 ! misho     173:                        sin4 = (struct sockaddr_in*) &sa;
1.1       misho     174:                        sin4->sin_len = sizeof(struct sockaddr_in);
                    175:                        sin4->sin_family = AF_INET;
                    176:                        memcpy(&sin4->sin_addr.s_addr, host->h_addr, host->h_length);
                    177:                        break;
                    178:                case AF_INET6:
1.2.2.1 ! misho     179:                        sin6 = (struct sockaddr_in6*) &sa;
1.1       misho     180:                        sin6->sin6_len = sizeof(struct sockaddr_in6);
                    181:                        sin6->sin6_family = AF_INET6;
                    182:                        memcpy(&sin6->sin6_addr.s6_addr, host->h_addr, host->h_length);
                    183:                        break;
                    184:                default:
                    185:                        printf("Error:: Unknown address type %d !!!\n", host->h_addrtype);
1.2.2.1 ! misho     186:                        if (ba)
        !           187:                                free(ba);
1.1       misho     188:                        return 1;
                    189:        }
                    190: 
1.2.2.1 ! misho     191:        h = PrepareL3(ba, &len);
1.1       misho     192:        if (h == -1) {
                    193:                printf("Error:: Descriptor not opened ... \n");
1.2.2.1 ! misho     194:                if (ba)
        !           195:                        free(ba);
1.1       misho     196:                return 1;
                    197:        }
                    198: 
                    199:        ConnectL3(h, id, &sa, len);
                    200: 
1.2       misho     201:        VERB(1) printf("\r\nFinish client.\r\n");
1.1       misho     202:        close(h);
1.2.2.1 ! misho     203:        if (ba)
        !           204:                free(ba);
1.1       misho     205:        return 0;
                    206: }

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