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