Annotation of embedaddon/libnet/sample/ospf_hello.c, revision 1.1
1.1 ! misho 1: /*
! 2: * $Id: ospf_hello.c,v 1.2 2004/01/03 20:31:01 mike Exp $
! 3: *
! 4: * libnet 1.1
! 5: * Build an OSPF Hello 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: int
! 42: main(int argc, char **argv)
! 43: {
! 44: int c;
! 45: libnet_t *l;
! 46: libnet_ptag_t t;
! 47: u_long src, dst, nbr;
! 48: char errbuf[LIBNET_ERRBUF_SIZE];
! 49: u_char *to, *from, *neighbor;
! 50: u_char auth[8] = {0,0,0,0,0,0,0,0};
! 51:
! 52:
! 53: printf("libnet 1.1 OSPF Hello packet shaping[raw]\n");
! 54:
! 55: if (argc != 4)
! 56: {
! 57: usage(argv[0]);
! 58: }
! 59:
! 60: from = argv[1];
! 61: to = argv[2];
! 62: neighbor = argv[3];
! 63:
! 64: /*
! 65: * Initialize the library. Root priviledges are required.
! 66: */
! 67: l = libnet_init(
! 68: LIBNET_RAW4, /* injection type */
! 69: NULL, /* network interface */
! 70: errbuf); /* errbuf */
! 71:
! 72: if (l == NULL)
! 73: {
! 74: fprintf(stderr, "libnet_init() failed: %s", errbuf);
! 75: exit(EXIT_FAILURE);
! 76: }
! 77:
! 78: /* Too lazy to check for error */
! 79: src = libnet_name2addr4(l, from, LIBNET_DONT_RESOLVE);
! 80: dst = libnet_name2addr4(l, to, LIBNET_DONT_RESOLVE);
! 81: nbr = libnet_name2addr4(l, neighbor, LIBNET_DONT_RESOLVE);
! 82:
! 83: t = libnet_build_ospfv2_hello(
! 84: 0xffffffff, /* netmask */
! 85: 2, /* interval */
! 86: 0x00, /* options */
! 87: 0x00, /* priority */
! 88: 30, /* dead int */
! 89: src, /* router */
! 90: src, /* router */
! 91: nbr, /* neighbor */
! 92: NULL, /* payload */
! 93: 0, /* payload size */
! 94: l, /* libnet handle */
! 95: 0); /* libnet id */
! 96: if (t == -1)
! 97: {
! 98: fprintf(stderr, "Can't build OSPF HELLO header: %s\n", libnet_geterror(l));
! 99: goto bad;
! 100: }
! 101:
! 102: /* authentication data */
! 103: t = libnet_build_data(
! 104: auth, /* auth data */
! 105: LIBNET_OSPF_AUTH_H, /* payload size */
! 106: l, /* libnet handle */
! 107: 0); /* libnet id */
! 108: if (t == -1)
! 109: {
! 110: fprintf(stderr, "Can't build OSPF auth header: %s\n", libnet_geterror(l));
! 111: goto bad;
! 112: }
! 113:
! 114: t = libnet_build_ospfv2(
! 115: LIBNET_OSPF_HELLO_H + LIBNET_OSPF_AUTH_H, /* OSPF packet length */
! 116: LIBNET_OSPF_HELLO, /* OSPF packet type */
! 117: htonl(0xd000000d), /* router id */
! 118: htonl(0xc0ffee00), /* area id */
! 119: 0, /* checksum */
! 120: LIBNET_OSPF_AUTH_NULL, /* auth type */
! 121: NULL, /* payload */
! 122: 0, /* payload size */
! 123: l, /* libnet handle */
! 124: 0); /* libnet id */
! 125:
! 126: if (t == -1)
! 127: {
! 128: fprintf(stderr, "Can't build OSPF header: %s\n", libnet_geterror(l));
! 129: goto bad;
! 130: }
! 131:
! 132: t = libnet_build_ipv4(
! 133: LIBNET_IPV4_H + LIBNET_OSPF_H +
! 134: LIBNET_OSPF_HELLO_H + LIBNET_OSPF_AUTH_H, /* packet total legnth */
! 135: 0, /* TOS */
! 136: 101, /* IP iD */
! 137: IP_DF, /* IP frag */
! 138: 254, /* TTL */
! 139: IPPROTO_OSPF, /* protocol */
! 140: 0, /* checksum */
! 141: src, /* source IP */
! 142: dst, /* destination IP */
! 143: NULL, /* payload */
! 144: 0, /* payload size */
! 145: l, /* libnet handle */
! 146: 0); /* libnet id */
! 147: if (t == -1)
! 148: {
! 149: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
! 150: goto bad;
! 151: }
! 152:
! 153: /*
! 154: * Write it to the wire.
! 155: */
! 156: c = libnet_write(l);
! 157: if (c == -1)
! 158: {
! 159: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
! 160: goto bad;
! 161: }
! 162: else
! 163: {
! 164: fprintf(stderr, "Wrote %d byte OSPF packet; check the wire.\n", c);
! 165: }
! 166: libnet_destroy(l);
! 167: return (EXIT_SUCCESS);
! 168: bad:
! 169: libnet_destroy(l);
! 170: return (EXIT_FAILURE);
! 171: }
! 172:
! 173:
! 174: void
! 175: usage(char *pname)
! 176: {
! 177: printf("Usage: %s <source ip> <dest. ip> <neighbor>\n", pname);
! 178: exit(EXIT_SUCCESS);
! 179: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>