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

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.1.1.1.2.2! misho       6:  * $Id: ansh3.c,v 1.1.1.1.2.1 2011/10/05 23:57:24 misho Exp $
1.1       misho       7:  *
                      8:  *************************************************************************/
                      9: #include "global.h"
                     10: #include "ansh.h"
                     11: 
                     12: 
1.1.1.1.2.2! misho      13: int Timeout, Verbose, Kill;
        !            14: u_int Crypted;
1.1       misho      15: 
                     16: extern char compiled[], compiledby[], compilehost[];
                     17: 
                     18: static void
                     19: Usage()
                     20: {
                     21:        printf( " -= ansh =- ELWIX Layer3 remote management client over ICMP\n"
                     22:                "=== %s === %s@%s ===\n\n"
                     23:                " Syntax: ansh [options] <connect2host>\n\n"
                     24:                "\t-i <id>\tService ID (default is 42)\n"
                     25:                "\t-t <timeout>\tClient session timeout (default is 0 sec)\n"
                     26:                "\t-u\t\tSwitch to unencrypted traffic between hosts\n"
                     27:                "\t-v\t\tVerbose (more -v, more verbosity ...)\n"
                     28:                "\t-h\t\tThis help screen!\n"
                     29:                "\n", compiled, compiledby, compilehost);
                     30: }
                     31: 
                     32: int
                     33: main(int argc, char **argv)
                     34: {
                     35:        struct sockaddr sa = { 0 };
                     36:        struct sockaddr_in *sin4 = (struct sockaddr_in*) &sa;
                     37:        struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) &sa;
                     38:        struct hostent *host;
                     39:        char ch;
                     40:        int h, len;
                     41:        u_short id = ANSH_ID;
                     42: 
1.1.1.1.2.2! misho      43:        srandomdev();
        !            44:        do {
        !            45:                Crypted = (u_int) random() % UINT_MAX;
        !            46:        } while (!Crypted);
        !            47: 
1.1       misho      48:        while ((ch = getopt(argc, argv, "hvui:t:")) != -1)
                     49:                switch (ch) {
                     50:                        case 't':
                     51:                                Timeout = abs(strtol(optarg, NULL, 0));
                     52:                                break;
                     53:                        case 'i':
                     54:                                id = strtol(optarg, NULL, 0);
                     55:                                break;
                     56:                        case 'u':
                     57:                                Crypted ^= Crypted;
                     58:                                break;
                     59:                        case 'v':
                     60:                                Verbose++;
                     61:                                break;
                     62:                        case 'h':
                     63:                        default:
                     64:                                Usage();
                     65:                                return 1;
                     66:                }
                     67:        argc -= optind;
                     68:        argv += optind;
                     69:        if (!argc) {
                     70:                printf("Error:: not specified address for connect ...\n");
                     71:                return 1;
                     72:        }
                     73: 
                     74:        host = gethostbyname(argv[0]);
                     75:        if (!host) {
                     76:                printf("Error:: in bind address '%s' #%d - %s\n",
                     77:                                argv[0], h_errno, hstrerror(h_errno));
                     78:                return 1;
                     79:        }
                     80:        switch (host->h_addrtype) {
                     81:                case AF_INET:
                     82:                        sin4->sin_len = sizeof(struct sockaddr_in);
                     83:                        sin4->sin_family = AF_INET;
                     84:                        memcpy(&sin4->sin_addr.s_addr, host->h_addr, host->h_length);
                     85:                        break;
                     86:                case AF_INET6:
                     87:                        sin6->sin6_len = sizeof(struct sockaddr_in6);
                     88:                        sin6->sin6_family = AF_INET6;
                     89:                        memcpy(&sin6->sin6_addr.s6_addr, host->h_addr, host->h_length);
                     90:                        break;
                     91:                default:
                     92:                        printf("Error:: Unknown address type %d !!!\n", host->h_addrtype);
                     93:                        return 1;
                     94:        }
                     95: 
                     96:        h = PrepareL3(&sa, &len);
                     97:        if (h == -1) {
                     98:                printf("Error:: Descriptor not opened ... \n");
                     99:                return 1;
                    100:        }
                    101: 
                    102:        ConnectL3(h, id, &sa, len);
                    103: 
1.1.1.1.2.1  misho     104:        VERB(1) printf("\nFinish client.\n");
1.1       misho     105:        close(h);
                    106:        return 0;
                    107: }

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