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