Annotation of embedaddon/libnet/sample/icmp_timeexceed.c, revision 1.1
1.1 ! misho 1: /*
! 2: * $Id: icmp_timeexceed.c,v 1.3 2004/03/16 18:40:58 mike Exp $
! 3: *
! 4: * libnet 1.1
! 5: * Build an ICMP timeexceed packet
! 6: *
! 7: * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
! 8: * Copyright (c) 2003 Frédéric Raynal <pappy@security-labs.org>
! 9: * All rights reserved.
! 10: *
! 11: * Redistribution and use in source and binary forms, with or without
! 12: * modification, are permitted provided that the following conditions
! 13: * are met:
! 14: * 1. Redistributions of source code must retain the above copyright
! 15: * notice, this list of conditions and the following disclaimer.
! 16: * 2. Redistributions in binary form must reproduce the above copyright
! 17: * notice, this list of conditions and the following disclaimer in the
! 18: * documentation and/or other materials provided with the distribution.
! 19: *
! 20: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
! 21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 23: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
! 24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 30: * SUCH DAMAGE.
! 31: *
! 32: */
! 33:
! 34: #if (HAVE_CONFIG_H)
! 35: #include "../include/config.h"
! 36: #endif
! 37: #include "./libnet_test.h"
! 38:
! 39: int
! 40: main(int argc, char **argv)
! 41: {
! 42: int c;
! 43: libnet_t *l;
! 44: libnet_ptag_t t;
! 45: u_long src_ip, dst_ip;
! 46: u_char payload[8] = {0x11, 0x11, 0x22, 0x22, 0x00, 0x08, 0xc6, 0xa5};
! 47: u_long payload_s = 8;
! 48: char errbuf[LIBNET_ERRBUF_SIZE];
! 49:
! 50: printf("libnet 1.1 packet shaping: ICMP timeexceed[link]\n");
! 51:
! 52: /*
! 53: * Initialize the library. Root priviledges are required.
! 54: */
! 55: l = libnet_init(
! 56: LIBNET_LINK, /* injection type */
! 57: NULL, /* network interface */
! 58: errbuf); /* errbuf */
! 59:
! 60: if (l == NULL)
! 61: {
! 62: fprintf(stderr, "libnet_init() failed: %s", errbuf);
! 63: exit(EXIT_FAILURE);
! 64: }
! 65:
! 66: src_ip = 0;
! 67: dst_ip = 0;
! 68: while((c = getopt(argc, argv, "d:s:")) != EOF)
! 69: {
! 70: switch (c)
! 71: {
! 72: case 'd':
! 73: if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
! 74: {
! 75: fprintf(stderr, "Bad destination IP address: %s\n", optarg);
! 76: exit(1);
! 77: }
! 78: break;
! 79: case 's':
! 80: if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
! 81: {
! 82: fprintf(stderr, "Bad source IP address: %s\n", optarg);
! 83: exit(1);
! 84: }
! 85: break;
! 86: }
! 87: }
! 88: if (!src_ip || !dst_ip)
! 89: {
! 90: usage(argv[0]);
! 91: exit(EXIT_FAILURE);
! 92: }
! 93: t = libnet_build_ipv4(
! 94: LIBNET_IPV4_H + payload_s, /* length */
! 95: IPTOS_LOWDELAY | IPTOS_THROUGHPUT, /* TOS */
! 96: 0xee, /* IP ID */
! 97: 0, /* IP Frag */
! 98: 64, /* TTL */
! 99: IPPROTO_UDP, /* protocol */
! 100: 0, /* checksum */
! 101: dst_ip, /* source IP */
! 102: src_ip, /* destination IP */
! 103: payload, /* payload */
! 104: payload_s, /* payload size */
! 105: l, /* libnet handle */
! 106: 0);
! 107: if (t == -1)
! 108: {
! 109: fprintf(stderr, "Can't build error IP header: %s\n",
! 110: libnet_geterror(l));
! 111: goto bad;
! 112: }
! 113:
! 114: t = libnet_build_icmpv4_timeexceed(
! 115: ICMP_TIMXCEED, /* type */
! 116: ICMP_TIMXCEED_INTRANS, /* code */
! 117: 0, /* checksum */
! 118: NULL,
! 119: 0,
! 120: l, /* libnet handle */
! 121: 0);
! 122: if (t == -1)
! 123: {
! 124: fprintf(stderr, "Can't build ICMP header: %s\n", libnet_geterror(l));
! 125: goto bad;
! 126: }
! 127:
! 128: t = libnet_build_ipv4(
! 129: LIBNET_IPV4_H + LIBNET_ICMPV4_TIMXCEED_H +
! 130: LIBNET_IPV4_H + payload_s, /* length */
! 131: IPTOS_LOWDELAY | IPTOS_THROUGHPUT, /* TOS */
! 132: 0xee, /* IP ID */
! 133: 0, /* IP Frag */
! 134: 64, /* TTL */
! 135: IPPROTO_ICMP, /* protocol */
! 136: 0, /* checksum */
! 137: src_ip, /* source IP */
! 138: dst_ip, /* destination IP */
! 139: NULL, /* payload */
! 140: 0, /* payload size */
! 141: l, /* libnet handle */
! 142: 0);
! 143: if (t == -1)
! 144: {
! 145: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
! 146: goto bad;
! 147: }
! 148:
! 149: t = libnet_build_ethernet(
! 150: enet_dst, /* ethernet destination */
! 151: enet_src, /* ethernet source */
! 152: ETHERTYPE_IP, /* protocol type */
! 153: NULL, /* payload */
! 154: 0, /* payload size */
! 155: l, /* libnet handle */
! 156: 0); /* libnet id */
! 157: if (t == -1)
! 158: {
! 159: fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
! 160: goto bad;
! 161: }
! 162:
! 163: /*
! 164: * Write it to the wire.
! 165: */
! 166: c = libnet_write(l);
! 167: if (c == -1)
! 168: {
! 169: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
! 170: goto bad;
! 171: }
! 172: else
! 173: {
! 174: fprintf(stderr, "Wrote %d byte ICMP packet; check the wire.\n", c);
! 175: }
! 176: libnet_destroy(l);
! 177: return (EXIT_SUCCESS);
! 178: bad:
! 179: libnet_destroy(l);
! 180: return (EXIT_FAILURE);
! 181: }
! 182:
! 183:
! 184: void
! 185: usage(char *name)
! 186: {
! 187: fprintf(stderr, "usage: %s -s source_ip -d destination_ip\n ", name);
! 188: }
! 189:
! 190: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>