File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / arp-new.c
Revision 1.1.1.2 (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: arp-new.c,v 1.1.1.2 2023/09/27 11:11:38 misho Exp $
    3:  *
    4:  *  libnet 1.1
    5:  *  Build an ARP packet
    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: 
   38: int
   39: main(int argc, char *argv[])
   40: {
   41:     int c;
   42:     uint32_t i;
   43:     libnet_t *l;
   44:     libnet_ptag_t t;
   45:     char *device = NULL;
   46:     uint8_t *packet;
   47:     uint32_t packet_s;
   48:     char errbuf[LIBNET_ERRBUF_SIZE];
   49: 
   50:     printf("libnet 1.1 packet shaping: ARP[link -- autobuilding ethernet]\n"); 
   51: 
   52:     if (argc > 1)
   53:     {
   54:          device = argv[1];
   55:     }
   56: 
   57:     l = libnet_init(
   58:             LIBNET_LINK_ADV,                        /* injection type */
   59:             device,                                 /* network interface */
   60:             errbuf);                                /* errbuf */
   61: 
   62:     if (l == NULL)
   63:     {
   64:         fprintf(stderr, "%s", errbuf);
   65:         exit(EXIT_FAILURE);
   66:     }
   67: 	else
   68: 
   69:     /*
   70:      *  Build the packet, remmebering that order IS important.  We must
   71:      *  build the packet from lowest protocol type on up as it would
   72:      *  appear on the wire.  So for our ARP packet:
   73:      *
   74:      *  -------------------------------------------
   75:      *  |  Ethernet   |           ARP             |
   76:      *  -------------------------------------------
   77:      *         ^                     ^
   78:      *         |------------------   |
   79:      *  libnet_build_ethernet()--|   |
   80:      *                               |
   81:      *  libnet_build_arp()-----------|
   82:      */
   83: 	 
   84:     i = libnet_get_ipaddr4(l);
   85:   
   86:     t = libnet_autobuild_arp(
   87:             ARPOP_REPLY,                            /* operation type */
   88:             enet_src,                               /* sender hardware addr */
   89:             (uint8_t *)&i,                         /* sender protocol addr */
   90:             enet_dst,                               /* target hardware addr */
   91:             (uint8_t *)&i,                         /* target protocol addr */
   92:             l);                                     /* libnet context */
   93:     if (t == -1)
   94:     {
   95:         fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l));
   96:         goto bad;
   97:     }
   98: 
   99:     t = libnet_autobuild_ethernet(
  100:             enet_dst,                               /* ethernet destination */
  101:             ETHERTYPE_ARP,                          /* protocol type */
  102:             l);                                     /* libnet handle */
  103:     if (t == -1)
  104:     {
  105:         fprintf(stderr, "Can't build ethernet header: %s\n",
  106:                 libnet_geterror(l));
  107:         goto bad;
  108:     }
  109: 
  110: 
  111:     if (libnet_adv_cull_packet(l, &packet, &packet_s) == -1)
  112:     {
  113:         fprintf(stderr, "%s", libnet_geterror(l));
  114:     }
  115:     else
  116:     {
  117:         fprintf(stderr, "packet size: %d\n", packet_s);
  118:         libnet_adv_free_packet(l, packet);
  119:     }
  120: 
  121:     c = libnet_write(l);
  122: 
  123:     if (c == -1)
  124:     {
  125:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
  126:         goto bad;
  127:     }
  128:     else
  129:     {
  130:         fprintf(stderr, "Wrote %d byte ARP packet from context \"%s\"; "
  131:                 "check the wire.\n", c, libnet_cq_getlabel(l));
  132:     }
  133:     libnet_destroy(l);
  134:     return (EXIT_SUCCESS);
  135: bad:
  136:     libnet_destroy(l);
  137:     return (EXIT_FAILURE);
  138: }
  139: 

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