blob: 43ab29978aafb05759e649354894e0d2da78cb34 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
Paul Jakma19a93722008-08-26 14:49:40 +01002 * $Quagga: $Format:%an, %ai, %h$ $
paul718e3742002-12-13 20:15:29 +00003 *
4 * GNU Zebra client test main routine.
5 * Copyright (C) 1997 Kunihiro Ishiguro
6 *
7 * This file is part of GNU Zebra.
8 *
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Zebra; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
23 */
24
25#include <zebra.h>
26
27#include "prefix.h"
28#include "stream.h"
29#include "zclient.h"
30#include "thread.h"
31#include "table.h"
32#include "zebra/rib.h"
33#include "zebra/zserv.h"
34
35struct thread *master;
36
37/* Zebra client structure. */
38struct zclient *zclient = NULL;
39
40/* Zebra socket. */
41int sock;
42
43/* IPv4 route add and delete test. */
44void
45zebra_test_ipv4 (int command, int type, char *prefix, char *gateway,
46 u_char distance)
47{
48 struct zapi_ipv4 api;
49 struct prefix_ipv4 p;
50 struct in_addr gate;
51 struct in_addr *gpnt;
52
53 str2prefix_ipv4 (prefix, &p);
54 inet_aton (gateway, &gate);
55 gpnt = &gate;
56
Feng Luc99f3482014-10-16 09:52:36 +080057 api.vrf_id = VRF_DEFAULT;
paul718e3742002-12-13 20:15:29 +000058 api.type = type;
59 api.flags = 0;
60
61 api.message = 0;
62 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
63 api.nexthop_num = 1;
64 api.nexthop = &gpnt;
65 api.ifindex_num = 0;
66 if (distance)
67 {
68 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
69 api.distance = distance;
70 }
71
72
73 switch (command)
74 {
75 case ZEBRA_IPV4_ROUTE_ADD:
76 zapi_ipv4_add (zclient, &p, &api);
77 break;
78 case ZEBRA_IPV4_ROUTE_DELETE:
79 zapi_ipv4_delete (zclient, &p, &api);
80 break;
81 }
82}
83
84#ifdef HAVE_IPV6
85/* IPv6 route add and delete test. */
86void
87zebra_test_v6 (int sock)
88{
89 struct prefix_ipv6 p;
90 struct in6_addr nexthop;
91
92 str2prefix_ipv6 ("3ffe:506::2/128", &p);
93 inet_pton (AF_INET6, "::1", &nexthop);
94
95 /* zebra_ipv6_add (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */
96
97 sleep (5);
98 /* zebra_ipv6_delete (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */
99}
100#endif /* HAVE_IPV6 */
101
102/* Print out usage and exit. */
103void
104usage_exit ()
105{
106 fprintf (stderr, "Usage: client filename\n");
107 exit (1);
108}
109
110struct zebra_info
111{
112 char *str;
113 int type;
114} zebra_type[] =
115{
116 { "static", ZEBRA_ROUTE_STATIC },
117 { "rip", ZEBRA_ROUTE_RIP },
118 { "ripng", ZEBRA_ROUTE_RIPNG },
Paul Jakma57345092011-12-25 17:52:09 +0100119 { "babel", ZEBRA_ROUTE_BABEL },
paul718e3742002-12-13 20:15:29 +0000120 { "ospf", ZEBRA_ROUTE_OSPF },
121 { "ospf6", ZEBRA_ROUTE_OSPF6 },
122 { "bgp", ZEBRA_ROUTE_BGP },
123 { NULL, 0 }
124};
125
126/* Zebra route simulator. */
127void
128zebra_sim (FILE *fp)
129{
130 char buf[BUFSIZ];
131 char distance_str[BUFSIZ];
132 u_char distance;
133
134 while (fgets (buf, sizeof buf, fp))
135 {
136 int i;
137 int ret;
138 int type;
139 char str[BUFSIZ], command[BUFSIZ], prefix[BUFSIZ], gateway[BUFSIZ];
140
141 distance = 0;
142
143 if (*buf == '#')
144 continue;
145
146 type = ZEBRA_ROUTE_STATIC;
147
148 ret = sscanf (buf, "%s %s %s %s %s\n", command, str, prefix, gateway,
149 distance_str);
150
151 if (ret == 5)
152 {
153 distance = atoi (distance_str);
154 }
155 else
156 {
157 ret = sscanf (buf, "%s %s %s %s\n", command, str, prefix, gateway);
158
159 if (ret != 4)
160 continue;
161 }
162
163 for (i = 0; i < 10; i++)
164 {
165 if (!zebra_type[i].str)
166 break;
167 if (strcmp (zebra_type[i].str, str) == 0)
168 {
169 type = zebra_type[i].type;
170 break;
171 }
172 }
173
174 if (strcmp (command, "add") == 0)
175 {
176 zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_ADD, type, prefix, gateway,
177 distance);
178 printf ("%s", buf);
179 continue;
180 }
181
182 if (strcmp (command, "del") == 0)
183 {
184 zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_DELETE, type, prefix, gateway,
185 distance);
186 printf ("%s", buf);
187 continue;
188 }
189 }
190}
191
192/* Test zebra client main routine. */
193int
194main (int argc, char **argv)
195{
Donald Sharp71252932015-09-24 09:25:19 -0400196 struct thread_master *master;
paul718e3742002-12-13 20:15:29 +0000197 FILE *fp;
198
199 if (argc == 1)
200 usage_exit ();
201
Donald Sharp71252932015-09-24 09:25:19 -0400202 master = thread_master_create ();
paul718e3742002-12-13 20:15:29 +0000203 /* Establish connection to zebra. */
Donald Sharp71252932015-09-24 09:25:19 -0400204 zclient = zclient_new (master);
paul718e3742002-12-13 20:15:29 +0000205 zclient->enable = 1;
206#ifdef HAVE_TCP_ZEBRA
207 zclient->sock = zclient_socket ();
208#else
209 zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH);
210#endif /* HAVE_TCP_ZEBRA */
211
212 /* Open simulation file. */
213 fp = fopen (argv[1], "r");
214 if (fp == NULL)
215 {
216 fprintf (stderr, "can't open %s\n", argv[1]);
217 exit (1);
218 }
219
220 /* Do main work. */
221 zebra_sim (fp);
222
223 sleep (100);
224
225 fclose (fp);
226 close (sock);
227
228 return 0;
229}