Blame __dist_sample/sample/isl.c

Packit Service b25606
/*
Packit Service b25606
 *  $Id: isl.c,v 1.2 2004/01/03 20:31:01 mike Exp $
Packit Service b25606
 *
Packit Service b25606
 *  libnet 1.1
Packit Service b25606
 *  Build an ISL 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
#include "../include/config.h"
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, len;
Packit Service b25606
    libnet_t *l;
Packit Service b25606
    libnet_ptag_t t;
Packit Service b25606
    u_char *dst;
Packit Service b25606
    u_long ip;
Packit Service b25606
    u_char dhost[5] = {0x01, 0x00, 0x0c, 0x00, 0x00};
Packit Service b25606
    u_char snap[6]  = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
Packit Service b25606
    char *device = NULL;
Packit Service b25606
    char errbuf[LIBNET_ERRBUF_SIZE];
Packit Service b25606
Packit Service b25606
    printf("libnet 1.1 packet shaping: [ISL]\n");
Packit Service b25606
Packit Service b25606
    device = NULL;
Packit Service b25606
    dst = NULL;
Packit Service b25606
    while ((c = getopt(argc, argv, "i:d:")) != EOF)
Packit Service b25606
    {
Packit Service b25606
        switch (c)
Packit Service b25606
        {
Packit Service b25606
            case 'd':
Packit Service b25606
                dst = libnet_hex_aton(optarg, &len;;
Packit Service b25606
                break;
Packit Service b25606
            case 'i':
Packit Service b25606
                device = optarg;
Packit Service b25606
                break;
Packit Service b25606
        }
Packit Service b25606
    }
Packit Service b25606
Packit Service b25606
    if (dst == NULL)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "usage %s -d dst [-i interface]\n",
Packit Service b25606
                argv[0]);
Packit Service b25606
        exit(EXIT_FAILURE);
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
    ip = 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
            (u_char *)&ip,                          /* sender protocol addr */
Packit Service b25606
            enet_dst,                               /* target hardware addr */
Packit Service b25606
            (u_char *)&ip,                          /* target protocol addr */
Packit Service b25606
            NULL,                                   /* payload */
Packit Service b25606
            0,                                      /* payload size */
Packit Service b25606
            l,                                      /* libnet handle */
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
            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
    t = libnet_build_isl(
Packit Service b25606
            dhost,
Packit Service b25606
            0x0,
Packit Service b25606
            0x0,
Packit Service b25606
            enet_src,
Packit Service b25606
            10 + 42,
Packit Service b25606
            snap,
Packit Service b25606
            10,
Packit Service b25606
            0x8000,
Packit Service b25606
            0,
Packit Service b25606
            NULL,                                   /* payload */
Packit Service b25606
            0,                                      /* payload size */
Packit Service b25606
            l,                                      /* libnet handle */
Packit Service b25606
            0);                                     /* libnet id */
Packit Service b25606
    if (t == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build ISL 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
    /*
Packit Service b25606
     *  Write it to the wire.
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 ISL packet; check the wire.\n", c);
Packit Service b25606
    }
Packit Service b25606
    free(dst);
Packit Service b25606
    libnet_destroy(l);
Packit Service b25606
    return (EXIT_SUCCESS);
Packit Service b25606
bad:
Packit Service b25606
    free(dst);
Packit Service b25606
    libnet_destroy(l);
Packit Service b25606
    return (EXIT_FAILURE);
Packit Service b25606
}
Packit Service b25606
Packit Service b25606
/* EOF */