Blame __dist_sample/sample/get_addr.c

Packit Service b25606
/*
Packit Service b25606
 *  $Id: get_addr.c,v 1.4 2004/11/09 07:05:07 mike Exp $
Packit Service b25606
 *
Packit Service b25606
 *  libnet 1.1
Packit Service b25606
 *  get_addr.c - Retrieve the MAC and IP address of an interface
Packit Service b25606
 *
Packit Service b25606
 *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
Packit Service b25606
 *  All rights reserved.
Packit Service b25606
 *
Packit Service b25606
 * Redistribution and use in source and binary forms, with or without
Packit Service b25606
 * modification, are permitted provided that the following conditions
Packit Service b25606
 * are met:
Packit Service b25606
 * 1. Redistributions of source code must retain the above copyright
Packit Service b25606
 *    notice, this list of conditions and the following disclaimer.
Packit Service b25606
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service b25606
 *    notice, this list of conditions and the following disclaimer in the
Packit Service b25606
 *    documentation and/or other materials provided with the distribution.
Packit Service b25606
 *
Packit Service b25606
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
Packit Service b25606
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service b25606
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service b25606
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
Packit Service b25606
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service b25606
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit Service b25606
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service b25606
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit Service b25606
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit Service b25606
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service b25606
 * SUCH DAMAGE.
Packit Service b25606
 *
Packit Service b25606
 */
Packit Service b25606
Packit Service b25606
#if (HAVE_CONFIG_H)
Packit Service b25606
#include "../include/config.h"
Packit Service b25606
#endif
Packit Service b25606
#include "./libnet_test.h"
Packit Service b25606
#ifdef __WIN32__
Packit Service b25606
#include "../include/win32/getopt.h"
Packit Service b25606
#endif
Packit Service b25606
Packit Service b25606
int
Packit Service b25606
main(int argc, char *argv[])
Packit Service b25606
{
Packit Service b25606
    int c;
Packit Service b25606
    u_long i;
Packit Service b25606
    libnet_t *l;
Packit Service b25606
    char *device = NULL;
Packit Service b25606
    struct libnet_ether_addr *e;
Packit Service b25606
    char errbuf[LIBNET_ERRBUF_SIZE];
Packit Service b25606
Packit Service b25606
    printf("libnet 1.1 address getter\n");
Packit Service b25606
Packit Service b25606
    while ((c = getopt(argc, argv, "i:")) != EOF)
Packit Service b25606
    {
Packit Service b25606
        switch (c)
Packit Service b25606
        {
Packit Service b25606
            case 'i':
Packit Service b25606
                device = optarg;
Packit Service b25606
                break;
Packit Service b25606
            default:
Packit Service b25606
                exit(EXIT_FAILURE);
Packit Service b25606
        }
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    /*
Packit Service b25606
     *  Initialize the library.  Root priviledges are required.
Packit Service b25606
     */
Packit Service b25606
    l = libnet_init(
Packit Service b25606
            LIBNET_LINK,                            /* injection type */
Packit Service b25606
            device,                                 /* network interface */
Packit Service b25606
            errbuf);                                /* errbuf */
Packit Service b25606
Packit Service b25606
    if (l == NULL)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "libnet_init() failed: %s", errbuf);
Packit Service b25606
        exit(EXIT_FAILURE);
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    printf("Interface:\t%s\n", libnet_getdevice(l));
Packit Service b25606
    e = libnet_get_hwaddr(l);
Packit Service b25606
    if (e == NULL)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't get hardware address: %s\n", libnet_geterror(l));
Packit Service b25606
    }
Packit Service b25606
    else
Packit Service b25606
    {
Packit Service b25606
        printf("MAC address:\t");
Packit Service b25606
        for (c = 0; c < 6; c++)
Packit Service b25606
        {
Packit Service b25606
            printf("%2.2x", e->ether_addr_octet[c]);
Packit Service b25606
            if (c != 5)
Packit Service b25606
            {
Packit Service b25606
                printf(":");
Packit Service b25606
            }
Packit Service b25606
        }
Packit Service b25606
        printf("\n");
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    i = libnet_get_ipaddr4(l);
Packit Service b25606
    if (i == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't get ip address: %s\n", libnet_geterror(l));
Packit Service b25606
    }
Packit Service b25606
    else
Packit Service b25606
    {
Packit Service b25606
        printf("IP  address:\t");
Packit Service b25606
        printf("%s\n", libnet_addr2name4(i, LIBNET_DONT_RESOLVE));
Packit Service b25606
    }
Packit Service b25606
    exit(EXIT_SUCCESS);
Packit Service b25606
}
Packit Service b25606
Packit Service b25606
/* EOF */