Blame __dist_sample/sample/arp.c

Packit Service b25606
/*
Packit Service b25606
 *  $Id: arp.c,v 1.7 2004/11/09 07:05:07 mike Exp $
Packit Service b25606
 *
Packit Service b25606
 *  libnet 1.1
Packit Service b25606
 *  Build an ARP packet
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
#if ((_WIN32) && !(__CYGWIN__)) 
Packit Service b25606
#include "../include/win32/config.h"
Packit Service b25606
#else
Packit Service b25606
#include "../include/config.h"
Packit Service b25606
#endif
Packit Service b25606
#endif
Packit Service b25606
#include "./libnet_test.h"
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
    uint32_t i;
Packit Service b25606
    libnet_t *l;
Packit Service b25606
    libnet_ptag_t t;
Packit Service b25606
    char *device = NULL;
Packit Service b25606
    uint8_t *packet;
Packit Service b25606
    uint32_t packet_s;
Packit Service b25606
    char errbuf[LIBNET_ERRBUF_SIZE];
Packit Service b25606
Packit Service b25606
    printf("libnet 1.1 packet shaping: ARP[link -- autobuilding ethernet]\n"); 
Packit Service b25606
Packit Service b25606
    if (argc > 1)
Packit Service b25606
    {
Packit Service b25606
         device = argv[1];
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    l = libnet_init(
Packit Service b25606
            LIBNET_LINK_ADV,                        /* 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, "%s", errbuf);
Packit Service b25606
        exit(EXIT_FAILURE);
Packit Service b25606
    }
Packit Service b25606
	else
Packit Service b25606
Packit Service b25606
    i = libnet_get_ipaddr4(l);
Packit Service b25606
  
Packit Service b25606
    t = libnet_build_arp(
Packit Service b25606
            ARPHRD_ETHER,                           /* hardware addr */
Packit Service b25606
            ETHERTYPE_IP,                           /* protocol addr */
Packit Service b25606
            6,                                      /* hardware addr size */
Packit Service b25606
            4,                                      /* protocol addr size */
Packit Service b25606
            ARPOP_REPLY,                            /* operation type */
Packit Service b25606
            enet_src,                               /* sender hardware addr */
Packit Service b25606
            (uint8_t *)&i,                         /* sender protocol addr */
Packit Service b25606
            enet_dst,                               /* target hardware addr */
Packit Service b25606
            (uint8_t *)&i,                         /* target protocol addr */
Packit Service b25606
            NULL,                                   /* payload */
Packit Service b25606
            0,                                      /* payload size */
Packit Service b25606
            l,                                      /* libnet context */
Packit Service b25606
            0);                                     /* libnet id */
Packit Service b25606
    if (t == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    t = libnet_autobuild_ethernet(
Packit Service b25606
            enet_dst,                               /* ethernet destination */
Packit Service b25606
            ETHERTYPE_ARP,                          /* protocol type */
Packit Service b25606
            l);                                     /* libnet handle */
Packit Service b25606
    if (t == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build ethernet header: %s\n",
Packit Service b25606
                libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
Packit Service b25606
    if (libnet_adv_cull_packet(l, &packet, &packet_s) == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "%s", libnet_geterror(l));
Packit Service b25606
    }
Packit Service b25606
    else
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "packet size: %d\n", packet_s);
Packit Service b25606
        libnet_adv_free_packet(l, packet);
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    c = libnet_write(l);
Packit Service b25606
Packit Service b25606
    if (c == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
Packit Service b25606
    }
Packit Service b25606
    else
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Wrote %d byte ARP packet from context \"%s\"; "
Packit Service b25606
                "check the wire.\n", c, libnet_cq_getlabel(l));
Packit Service b25606
    }
Packit Service b25606
    libnet_destroy(l);
Packit Service b25606
    return (EXIT_SUCCESS);
Packit Service b25606
bad:
Packit Service b25606
    libnet_destroy(l);
Packit Service b25606
    return (EXIT_FAILURE);
Packit Service b25606
}
Packit Service b25606
Packit Service b25606
/* EOF */