Blame __dist_sample/sample/ip_raw.c

Packit Service b25606
/*
Packit Service b25606
 *
Packit Service b25606
 * libnet 1.1
Packit Service b25606
 * Build a IPv4 packet with what you want as payload
Packit Service b25606
 *
Packit Service b25606
 * Copyright (c) 2003 Frédéric Raynal <pappy@security-labs.org>
Packit Service b25606
 * All rights reserved.
Packit Service b25606
 *
Packit Service b25606
 * Ex:
Packit Service b25606
 * - send an UDP packet from port 4369 to port 8738
Packit Service b25606
 *    ./ip -s 1.1.1.1 -d 2.2.2.2
Packit Service b25606
 *
Packit Service b25606
 * - send a TCP SYN from port 4369 to port 8738
Packit Service b25606
 *   ./ip -s 1.1.1.1 -d 2.2.2.2 -t -p `printf "\x04\x57\x08\xae\x01\x01\x01\x01\x02\x02\x02\x02\x50\x02\x7f\xff\xd5\x91\x41\x41"`
Packit Service b25606
 *
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
#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;
Packit Service b25606
    libnet_t *l;
Packit Service b25606
    char *device = NULL;
Packit Service b25606
    char *dst = "2.2.2.2", *src = "1.1.1.1";
Packit Service b25606
    u_long src_ip, dst_ip;
Packit Service b25606
    char errbuf[LIBNET_ERRBUF_SIZE];
Packit Service b25606
    libnet_ptag_t ip_ptag = 0;
Packit Service b25606
    u_short proto = IPPROTO_UDP;
Packit Service b25606
    u_char payload[255] = {0x11, 0x11, 0x22, 0x22, 0x00, 0x08, 0xc6, 0xa5};
Packit Service b25606
    u_long payload_s = 8;
Packit Service b25606
Packit Service b25606
    printf("libnet 1.1 packet shaping: IP + payload[raw]\n");
Packit Service b25606
Packit Service b25606
    /*
Packit Service b25606
     * handle options
Packit Service b25606
     */ 
Packit Service b25606
    while ((c = getopt(argc, argv, "d:s:tp:i:h")) != EOF)
Packit Service b25606
    {
Packit Service b25606
        switch (c)
Packit Service b25606
        {
Packit Service b25606
            case 'd':
Packit Service b25606
		dst = optarg;
Packit Service b25606
                break;
Packit Service b25606
Packit Service b25606
            case 's':
Packit Service b25606
		src = optarg;
Packit Service b25606
                break;
Packit Service b25606
Packit Service b25606
	    case 'i':
Packit Service b25606
		device = optarg;
Packit Service b25606
		break;
Packit Service b25606
Packit Service b25606
	    case 't':
Packit Service b25606
		proto = IPPROTO_TCP;
Packit Service b25606
		break;
Packit Service b25606
Packit Service b25606
	    case 'p':
Packit Service b25606
		strncpy(payload, optarg, sizeof(payload)-1);
Packit Service b25606
		payload_s = strlen(payload);
Packit Service b25606
		break;
Packit Service b25606
Packit Service b25606
	    case 'h':
Packit Service b25606
		usage(argv[0]);
Packit Service b25606
		exit(EXIT_SUCCESS);
Packit Service b25606
Packit Service b25606
            default:
Packit Service b25606
                exit(EXIT_FAILURE);
Packit Service b25606
        }
Packit Service b25606
    }
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_RAW4,                            /* injection type */
Packit Service b25606
	    device,                                 /* network interface */
Packit Service b25606
            errbuf);                                /* error buffer */
Packit Service b25606
Packit Service b25606
    printf("Using device %s\n", l->device);
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
    if ((dst_ip = libnet_name2addr4(l, dst, LIBNET_RESOLVE)) == -1)
Packit Service b25606
    {
Packit Service b25606
	fprintf(stderr, "Bad destination IP address: %s\n", dst);
Packit Service b25606
	exit(EXIT_FAILURE);
Packit Service b25606
    }
Packit Service b25606
    
Packit Service b25606
    if ((src_ip = libnet_name2addr4(l, src, LIBNET_RESOLVE)) == -1)
Packit Service b25606
    {
Packit Service b25606
	fprintf(stderr, "Bad source IP address: %s\n", src);
Packit Service b25606
	exit(EXIT_FAILURE);
Packit Service b25606
    }
Packit Service b25606
    
Packit Service b25606
Packit Service b25606
    /*
Packit Service b25606
     * Build the packet
Packit Service b25606
     */ 
Packit Service b25606
    ip_ptag = libnet_build_ipv4(
Packit Service b25606
        LIBNET_IPV4_H + payload_s,                  /* length */
Packit Service b25606
        0,                                          /* TOS */
Packit Service b25606
        242,                                        /* IP ID */
Packit Service b25606
        0,                                          /* IP Frag */
Packit Service b25606
        64,                                         /* TTL */
Packit Service b25606
        proto,                                      /* protocol */
Packit Service b25606
        0,                                          /* checksum */
Packit Service b25606
        src_ip,                                     /* source IP */
Packit Service b25606
        dst_ip,                                     /* destination IP */
Packit Service b25606
        payload,                                    /* payload */
Packit Service b25606
        payload_s,                                  /* payload size */
Packit Service b25606
        l,                                          /* libnet handle */
Packit Service b25606
        ip_ptag);                                   /* libnet id */
Packit Service b25606
    if (ip_ptag == -1)
Packit Service b25606
    {
Packit Service b25606
        fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
Packit Service b25606
        goto bad;
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
    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 IP packet; check the wire.\n", c);
Packit Service b25606
    }
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
void
Packit Service b25606
usage(char *name)
Packit Service b25606
{
Packit Service b25606
    fprintf(stderr,
Packit Service b25606
        "usage: %s [-s source_ip] [-d destination_ip]"
Packit Service b25606
	    " [-i iface] [-p payload] [-t]\n",
Packit Service b25606
	    name);
Packit Service b25606
}
Packit Service b25606
Packit Service b25606
/* EOF */