Annotation of embedaddon/libnet/sample/ospf_lsa.c, revision 1.1
1.1 ! misho 1: /*
! 2: * $Id: ospf_lsa.c,v 1.2 2004/01/03 20:31:01 mike Exp $
! 3: *
! 4: * libnet 1.1
! 5: * Build an OSPF LSA packet
! 6: *
! 7: * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
! 8: * All rights reserved.
! 9: *
! 10: * Copyright (c) 1999, 2000 Andrew Reiter <areiter@bindview.com>
! 11: * All rights reserved.
! 12: *
! 13: * Redistribution and use in source and binary forms, with or without
! 14: * modification, are permitted provided that the following conditions
! 15: * are met:
! 16: * 1. Redistributions of source code must retain the above copyright
! 17: * notice, this list of conditions and the following disclaimer.
! 18: * 2. Redistributions in binary form must reproduce the above copyright
! 19: * notice, this list of conditions and the following disclaimer in the
! 20: * documentation and/or other materials provided with the distribution.
! 21: *
! 22: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
! 23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 25: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
! 26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 32: * SUCH DAMAGE.
! 33: *
! 34: */
! 35:
! 36: #if (HAVE_CONFIG_H)
! 37: #include "../include/config.h"
! 38: #endif
! 39: #include "./libnet_test.h"
! 40:
! 41:
! 42: int
! 43: main(int argc, char **argv)
! 44: {
! 45: int c;
! 46: libnet_t *l;
! 47: libnet_ptag_t t;
! 48: u_long src, dst;
! 49: u_char auth[8] = {0,0,0,0,0,0,0,0};
! 50: char *from, *to, errbuf[LIBNET_ERRBUF_SIZE];
! 51:
! 52: printf("libnet 1.1 OSPF LSA packet shaping[raw]\n");
! 53:
! 54: if (argc != 3)
! 55: {
! 56: usage(argv[0]);
! 57: }
! 58:
! 59: from = argv[1];
! 60: to = argv[2];
! 61:
! 62: /*
! 63: * Initialize the library. Root priviledges are required.
! 64: */
! 65: l = libnet_init(
! 66: LIBNET_RAW4, /* injection type */
! 67: NULL, /* network interface */
! 68: errbuf); /* errbuf */
! 69:
! 70: if (l == NULL)
! 71: {
! 72: fprintf(stderr, "libnet_init() failed: %s", errbuf);
! 73: exit(EXIT_FAILURE);
! 74: }
! 75:
! 76: /* Too lazy to check for error */
! 77: src = libnet_name2addr4(l, from, LIBNET_DONT_RESOLVE);
! 78: dst = libnet_name2addr4(l, to, LIBNET_DONT_RESOLVE);
! 79:
! 80: t = libnet_build_ospfv2_lsa_net(
! 81: 0xffffff00, /* netmask */
! 82: 0xc0ffee00, /* router id */
! 83: NULL, /* payload */
! 84: 0, /* payload size */
! 85: l, /* libnet handle */
! 86: 0); /* libnet id */
! 87: if (t == -1)
! 88: {
! 89: fprintf(stderr, "Can't build LSA net header: %s\n", libnet_geterror(l));
! 90: goto bad;
! 91: }
! 92:
! 93: t = libnet_build_ospfv2_lsa(
! 94: 40, /* packet age */
! 95: 0, /* options */
! 96: LIBNET_LS_TYPE_NET, /* type */
! 97: htonl(0xc0ffee00), /* LS id */
! 98: src, /* ad router */
! 99: 0x11111111, /* seq */
! 100: 0xffff, /* checksum */
! 101: LIBNET_OSPF_LS_NET_H + LIBNET_OSPF_LSA_H, /* packet length */
! 102: NULL, /* payload */
! 103: 0, /* payload size */
! 104: l, /* libnet handle */
! 105: 0); /* libnet id */
! 106: if (t == -1)
! 107: {
! 108: fprintf(stderr, "Can't build LSA header: %s\n", libnet_geterror(l));
! 109: goto bad;
! 110: }
! 111:
! 112: /* authentication data */
! 113: t = libnet_build_data(
! 114: auth, /* auth data */
! 115: LIBNET_OSPF_AUTH_H, /* payload size */
! 116: l, /* libnet handle */
! 117: 0); /* libnet id */
! 118: if (t == -1)
! 119: {
! 120: fprintf(stderr, "Can't build OSPF AUTH header: %s\n", libnet_geterror(l));
! 121: goto bad;
! 122: }
! 123:
! 124: t = libnet_build_ospfv2(
! 125: LIBNET_OSPF_LSA_H + LIBNET_OSPF_AUTH_H +
! 126: LIBNET_OSPF_LS_NET_H, /* OSPF packet length */
! 127: LIBNET_OSPF_LSA, /* OSPF packet type */
! 128: htonl(0xd000000d), /* router id */
! 129: htonl(0xc0ffee00), /* area id */
! 130: 0, /* checksum */
! 131: LIBNET_OSPF_AUTH_NULL, /* auth type */
! 132: NULL, /* payload */
! 133: 0, /* payload size */
! 134: l, /* libnet handle */
! 135: 0); /* libnet id */
! 136: if (t == -1)
! 137: {
! 138: fprintf(stderr, "Can't build OSPF header: %s\n", libnet_geterror(l));
! 139: goto bad;
! 140: }
! 141:
! 142: t = libnet_build_ipv4(
! 143: LIBNET_IPV4_H + LIBNET_OSPF_H + LIBNET_OSPF_AUTH_H +
! 144: LIBNET_OSPF_LSA_H + LIBNET_OSPF_LS_NET_H, /* packet total length */
! 145: 0, /* TOS */
! 146: 101, /* IP iD */
! 147: IP_DF, /* IP frag */
! 148: 254, /* TTL */
! 149: IPPROTO_OSPF, /* protocol */
! 150: 0, /* checksum */
! 151: src, /* source IP */
! 152: dst, /* destination IP */
! 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 IP 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 OSPF packet; check the wire.\n",
! 175: c);
! 176: }
! 177: libnet_destroy(l);
! 178: return (EXIT_SUCCESS);
! 179: bad:
! 180: libnet_destroy(l);
! 181: return (EXIT_FAILURE);
! 182: }
! 183:
! 184:
! 185: void
! 186: usage(char *pname)
! 187: {
! 188: printf("Usage: %s <source ip> <dest. ip>\n", pname);
! 189: exit(0);
! 190: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>