File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / get_addr.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (9 months, 1 week ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *  $Id: get_addr.c,v 1.1.1.3 2023/09/27 11:11:38 misho Exp $
    3:  *
    4:  *  libnet 1.1
    5:  *  get_addr.c - Retrieve the MAC and IP address of an interface
    6:  *
    7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
    8:  *  All rights reserved.
    9:  *
   10:  * Redistribution and use in source and binary forms, with or without
   11:  * modification, are permitted provided that the following conditions
   12:  * are met:
   13:  * 1. Redistributions of source code must retain the above copyright
   14:  *    notice, this list of conditions and the following disclaimer.
   15:  * 2. Redistributions in binary form must reproduce the above copyright
   16:  *    notice, this list of conditions and the following disclaimer in the
   17:  *    documentation and/or other materials provided with the distribution.
   18:  *
   19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29:  * SUCH DAMAGE.
   30:  *
   31:  */
   32: 
   33: #include "./libnet_test.h"
   34: 
   35: int
   36: main(int argc, char *argv[])
   37: {
   38:     int c;
   39:     u_long i;
   40:     libnet_t *l;
   41:     char *device = NULL;
   42:     struct libnet_ether_addr *e;
   43:     char errbuf[LIBNET_ERRBUF_SIZE];
   44: 
   45:     printf("libnet 1.1 address getter\n");
   46: 
   47:     while ((c = getopt(argc, argv, "i:")) != EOF)
   48:     {
   49:         switch (c)
   50:         {
   51:             case 'i':
   52:                 device = optarg;
   53:                 break;
   54:             default:
   55:                 exit(EXIT_FAILURE);
   56:         }
   57:     }
   58: 
   59:     /*
   60:      *  Initialize the library.  Root priviledges are required.
   61:      */
   62:     l = libnet_init(
   63:             LIBNET_LINK,                            /* injection type */
   64:             device,                                 /* network interface */
   65:             errbuf);                                /* errbuf */
   66: 
   67:     if (l == NULL)
   68:     {
   69:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
   70:         exit(EXIT_FAILURE);
   71:     }
   72: 
   73:     printf("Interface:\t%s\n", libnet_getdevice(l));
   74:     e = libnet_get_hwaddr(l);
   75:     if (e == NULL)
   76:     {
   77:         fprintf(stderr, "Can't get hardware address: %s\n", libnet_geterror(l));
   78:     }
   79:     else
   80:     {
   81:         printf("MAC address:\t");
   82:         for (c = 0; c < 6; c++)
   83:         {
   84:             printf("%2.2x", e->ether_addr_octet[c]);
   85:             if (c != 5)
   86:             {
   87:                 printf(":");
   88:             }
   89:         }
   90:         printf("\n");
   91:     }
   92: 
   93:     i = libnet_get_ipaddr4(l);
   94:     if (i == -1)
   95:     {
   96:         fprintf(stderr, "Can't get ip address: %s\n", libnet_geterror(l));
   97:     }
   98:     else
   99:     {
  100:         printf("IP  address:\t");
  101:         printf("%s\n", libnet_addr2name4(i, LIBNET_DONT_RESOLVE));
  102:     }
  103:     exit(EXIT_SUCCESS);
  104: }
  105: 

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