Annotation of embedaddon/libnet/sample/icmp_redirect.c, revision 1.1
1.1 ! misho 1: /*
! 2: * $Id: icmp_redirect.c,v 1.3 2004/03/16 18:40:58 mike Exp $
! 3: *
! 4: * libnet 1.1
! 5: * Build an ICMP redirect packet
! 6: *
! 7: * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
! 8: * Copyright (c) 2003 Alberto Ornaghi <alor@antifork.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, gw_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 redirect[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: gw_ip = 0;
! 69: while((c = getopt(argc, argv, "d:s:g:")) != EOF)
! 70: {
! 71: switch (c)
! 72: {
! 73: case 'd':
! 74: if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
! 75: {
! 76: fprintf(stderr, "Bad destination IP address: %s\n", optarg);
! 77: exit(1);
! 78: }
! 79: break;
! 80: case 's':
! 81: if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
! 82: {
! 83: fprintf(stderr, "Bad source IP address: %s\n", optarg);
! 84: exit(1);
! 85: }
! 86: break;
! 87: case 'g':
! 88: if ((gw_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
! 89: {
! 90: fprintf(stderr, "Bad gateway IP address: %s\n", optarg);
! 91: exit(1);
! 92: }
! 93: break;
! 94: }
! 95: }
! 96: if (!src_ip || !dst_ip || !gw_ip)
! 97: {
! 98: usage(argv[0]);
! 99: exit(EXIT_FAILURE);
! 100: }
! 101:
! 102: t = libnet_build_ipv4(
! 103: LIBNET_IPV4_H + payload_s, /* length */
! 104: IPTOS_LOWDELAY | IPTOS_THROUGHPUT, /* TOS */
! 105: 0x42, /* IP ID */
! 106: 0, /* IP Frag */
! 107: 64, /* TTL */
! 108: IPPROTO_ICMP, /* protocol */
! 109: 0, /* checksum */
! 110: dst_ip, /* source IP */
! 111: src_ip, /* destination IP */
! 112: payload, /* payload */
! 113: payload_s, /* payload size */
! 114: l, /* libnet handle */
! 115: 0);
! 116: if (t == -1)
! 117: {
! 118: fprintf(stderr, "Can't build error IP header: %s\n",
! 119: libnet_geterror(l));
! 120: goto bad;
! 121: }
! 122:
! 123: t = libnet_build_icmpv4_redirect(
! 124: ICMP_REDIRECT, /* type */
! 125: ICMP_REDIRECT_HOST, /* code */
! 126: 0, /* checksum */
! 127: gw_ip,
! 128: NULL,
! 129: 0,
! 130: l, /* libnet handle */
! 131: 0); /* libnet id */
! 132: if (t == -1)
! 133: {
! 134: fprintf(stderr, "Can't build ICMP header: %s\n", libnet_geterror(l));
! 135: goto bad;
! 136: }
! 137:
! 138: t = libnet_build_ipv4(
! 139: LIBNET_IPV4_H + LIBNET_ICMPV4_REDIRECT_H +
! 140: LIBNET_IPV4_H + payload_s, /* length */
! 141: IPTOS_LOWDELAY | IPTOS_THROUGHPUT, /* TOS */
! 142: 0xee, /* IP ID */
! 143: 0, /* IP Frag */
! 144: 64, /* TTL */
! 145: IPPROTO_ICMP, /* protocol */
! 146: 0, /* checksum */
! 147: src_ip, /* source IP */
! 148: dst_ip, /* destination IP */
! 149: NULL, /* payload */
! 150: 0, /* payload size */
! 151: l, /* libnet handle */
! 152: 0);
! 153: if (t == -1)
! 154: {
! 155: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
! 156: goto bad;
! 157: }
! 158:
! 159: t = libnet_build_ethernet(
! 160: enet_dst, /* ethernet destination */
! 161: enet_src, /* ethernet source */
! 162: ETHERTYPE_IP, /* protocol type */
! 163: NULL, /* payload */
! 164: 0, /* payload size */
! 165: l, /* libnet handle */
! 166: 0); /* libnet id */
! 167: if (t == -1)
! 168: {
! 169: fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
! 170: goto bad;
! 171: }
! 172:
! 173: /*
! 174: * Write it to the wire.
! 175: */
! 176: c = libnet_write(l);
! 177: if (c == -1)
! 178: {
! 179: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
! 180: goto bad;
! 181: }
! 182: else
! 183: {
! 184: fprintf(stderr, "Wrote %d byte ICMP packet; check the wire.\n", c);
! 185: }
! 186: libnet_destroy(l);
! 187: return (EXIT_SUCCESS);
! 188: bad:
! 189: libnet_destroy(l);
! 190: return (EXIT_FAILURE);
! 191: }
! 192:
! 193:
! 194: void
! 195: usage(char *name)
! 196: {
! 197: fprintf(stderr, "usage: %s -s source_ip -d destination_ip -g gateway_ip\n ", name);
! 198: }
! 199:
! 200: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>