OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / test / OSPF / ospf_lsa.c
1 /*
2  *  $Id: ospf_lsa.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet
5  *  ospf_lsa.c - OSPF LSA packet builder
6  *
7  *  Copyright (c) 1999, 2000 Andrew Reiter <areiter@bindview.com>
8  *  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  */
32
33 #if (HAVE_CONFIG_H)
34 #include "../../include/config.h"
35 #endif
36 #include "../libnet_test.h"
37
38
39 void    help(char *);
40
41 int
42 main(int argc, char **argv)
43 {
44     int sock, warn, len;
45     u_char *pack;
46     char *from, *to;
47     u_long src, dst, addrid, addaid, auth[2];
48
49     if (argc != 3) 
50         help(argv[0]);
51
52     len = LIBNET_IP_H + LIBNET_OSPF_H + LIBNET_AUTH_H + LIBNET_LSA_H +
53                 LIBNET_LS_NET_LEN;
54     pack = (u_char *)malloc(len);
55
56     from = argv[1];
57     to = argv[2];
58
59     src = libnet_name_resolve(from, 0);
60     dst = libnet_name_resolve(to, 0);
61
62     addrid = 0xff00ff00;
63     addaid = 0xd00dd00d;
64
65     sock = libnet_open_raw_sock(IPPROTO_RAW);
66     if (sock == -1) {
67         perror("socket");
68         free(pack);
69         exit(sock);
70     }
71
72     libnet_build_ip(LIBNET_OSPF_H + LIBNET_AUTH_H + LIBNET_LSA_H +
73                 LIBNET_LS_NET_LEN,
74                         0x00, 
75                         (u_short)rand(), 
76                         IP_DF,
77                         0xfe,
78                         IPPROTO_OSPF,
79                         src,
80                         dst,
81                         NULL,
82                         0,
83                         pack);
84
85     libnet_build_ospf(LIBNET_AUTH_H + LIBNET_LSA_H + LIBNET_LS_NET_LEN,
86                         LIBNET_OSPF_LSA,
87                         addrid,
88                         addaid,
89                         LIBNET_OSPF_AUTH_NULL,
90                         NULL,
91                         0,
92                         pack + LIBNET_IP_H);
93
94     memset(auth, 0, sizeof(auth));
95     LIBNET_OSPF_AUTHCPY(pack + LIBNET_OSPF_H + LIBNET_IP_H, auth);
96
97     libnet_build_ospf_lsa(40, 
98                           0x00, 
99                           LIBNET_LS_TYPE_NET,
100                           addrid,
101                           src, 
102                           0xf0f0f00f,
103                           LIBNET_LS_NET_LEN,
104                           NULL,
105                           0,
106                           pack + LIBNET_AUTH_H + LIBNET_OSPF_H + LIBNET_IP_H);
107
108     libnet_build_ospf_lsa_net(0xffffff00, 
109                                 0xc0ffee00, 
110                                 NULL,
111                                 0,
112                                 pack + LIBNET_LSA_H + LIBNET_AUTH_H +
113                                 LIBNET_OSPF_H + LIBNET_IP_H);
114
115     libnet_do_checksum(pack, IPPROTO_OSPF, len);
116     libnet_do_checksum(pack + LIBNET_IP_H + LIBNET_OSPF_H + LIBNET_AUTH_H,
117                     IPPROTO_OSPF_LSA, LIBNET_LS_NET_LEN + LIBNET_LSA_H);
118
119
120     warn = libnet_write_ip(sock, pack, len);
121     if (warn == -1) {
122         printf("Error writing packet to the wire\n");
123         free(pack);
124         exit(warn);
125     }
126
127     printf("%d bytes written\n", warn);
128     free(pack);
129     return (0);
130 }
131
132 void
133 help(char *pname)
134 {
135     printf("Usage: %s <source ip> <dest. ip>\n", pname);
136     printf("[Use x.x.x.x format]\n");
137     exit(0);
138 }