Annotation of embedaddon/libnet/sample/get_addr.c, revision 1.1.1.2

1.1       misho       1: /*
1.1.1.2 ! misho       2:  *  $Id: get_addr.c,v 1.4 2004/11/09 07:05:07 mike Exp $
1.1       misho       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: #if (HAVE_CONFIG_H)
                     34: #include "../include/config.h"
                     35: #endif
                     36: #include "./libnet_test.h"
                     37: #ifdef __WIN32__
                     38: #include "../include/win32/getopt.h"
                     39: #endif
                     40: 
                     41: int
                     42: main(int argc, char *argv[])
                     43: {
                     44:     int c;
                     45:     u_long i;
                     46:     libnet_t *l;
                     47:     char *device = NULL;
                     48:     struct libnet_ether_addr *e;
                     49:     char errbuf[LIBNET_ERRBUF_SIZE];
                     50: 
                     51:     printf("libnet 1.1 address getter\n");
                     52: 
                     53:     while ((c = getopt(argc, argv, "i:")) != EOF)
                     54:     {
                     55:         switch (c)
                     56:         {
                     57:             case 'i':
                     58:                 device = optarg;
                     59:                 break;
                     60:             default:
                     61:                 exit(EXIT_FAILURE);
                     62:         }
                     63:     }
                     64: 
                     65:     /*
                     66:      *  Initialize the library.  Root priviledges are required.
                     67:      */
                     68:     l = libnet_init(
                     69:             LIBNET_LINK,                            /* injection type */
                     70:             device,                                 /* network interface */
                     71:             errbuf);                                /* errbuf */
                     72: 
                     73:     if (l == NULL)
                     74:     {
                     75:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
                     76:         exit(EXIT_FAILURE);
                     77:     }
                     78: 
1.1.1.2 ! misho      79:     printf("Interface:\t%s\n", libnet_getdevice(l));
1.1       misho      80:     e = libnet_get_hwaddr(l);
                     81:     if (e == NULL)
                     82:     {
                     83:         fprintf(stderr, "Can't get hardware address: %s\n", libnet_geterror(l));
                     84:     }
                     85:     else
                     86:     {
                     87:         printf("MAC address:\t");
                     88:         for (c = 0; c < 6; c++)
                     89:         {
                     90:             printf("%2.2x", e->ether_addr_octet[c]);
                     91:             if (c != 5)
                     92:             {
                     93:                 printf(":");
                     94:             }
                     95:         }
                     96:         printf("\n");
                     97:     }
                     98: 
                     99:     i = libnet_get_ipaddr4(l);
                    100:     if (i == -1)
                    101:     {
                    102:         fprintf(stderr, "Can't get ip address: %s\n", libnet_geterror(l));
                    103:     }
                    104:     else
                    105:     {
                    106:         printf("IP  address:\t");
                    107:         printf("%s\n", libnet_addr2name4(i, LIBNET_DONT_RESOLVE));
                    108:     }
                    109:     exit(EXIT_SUCCESS);
                    110: }
                    111: 
                    112: /* EOF */

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