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