]> git.proxmox.com Git - mirror_frr.git/blob - zebra/client_main.c
Use only the ISC license for .proto files.
[mirror_frr.git] / zebra / client_main.c
1 /*
2 * $Quagga: $Format:%an, %ai, %h$ $
3 *
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
35 struct thread *master;
36
37 /* Zebra client structure. */
38 struct zclient *zclient = NULL;
39
40 /* Zebra socket. */
41 int sock;
42
43 /* IPv4 route add and delete test. */
44 void
45 zebra_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
57 api.vrf_id = VRF_DEFAULT;
58 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. */
86 void
87 zebra_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. */
103 void
104 usage_exit ()
105 {
106 fprintf (stderr, "Usage: client filename\n");
107 exit (1);
108 }
109
110 struct 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 },
119 { "ospf", ZEBRA_ROUTE_OSPF },
120 { "ospf6", ZEBRA_ROUTE_OSPF6 },
121 { "bgp", ZEBRA_ROUTE_BGP },
122 { NULL, 0 }
123 };
124
125 /* Zebra route simulator. */
126 void
127 zebra_sim (FILE *fp)
128 {
129 char buf[BUFSIZ];
130 char distance_str[BUFSIZ];
131 u_char distance;
132
133 while (fgets (buf, sizeof buf, fp))
134 {
135 int i;
136 int ret;
137 int type;
138 char str[BUFSIZ], command[BUFSIZ], prefix[BUFSIZ], gateway[BUFSIZ];
139
140 distance = 0;
141
142 if (*buf == '#')
143 continue;
144
145 type = ZEBRA_ROUTE_STATIC;
146
147 ret = sscanf (buf, "%s %s %s %s %s\n", command, str, prefix, gateway,
148 distance_str);
149
150 if (ret == 5)
151 {
152 distance = atoi (distance_str);
153 }
154 else
155 {
156 ret = sscanf (buf, "%s %s %s %s\n", command, str, prefix, gateway);
157
158 if (ret != 4)
159 continue;
160 }
161
162 for (i = 0; i < 10; i++)
163 {
164 if (!zebra_type[i].str)
165 break;
166 if (strcmp (zebra_type[i].str, str) == 0)
167 {
168 type = zebra_type[i].type;
169 break;
170 }
171 }
172
173 if (strcmp (command, "add") == 0)
174 {
175 zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_ADD, type, prefix, gateway,
176 distance);
177 printf ("%s", buf);
178 continue;
179 }
180
181 if (strcmp (command, "del") == 0)
182 {
183 zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_DELETE, type, prefix, gateway,
184 distance);
185 printf ("%s", buf);
186 continue;
187 }
188 }
189 }
190
191 /* Test zebra client main routine. */
192 int
193 main (int argc, char **argv)
194 {
195 struct thread_master *master;
196 FILE *fp;
197
198 if (argc == 1)
199 usage_exit ();
200
201 master = thread_master_create();
202 /* Establish connection to zebra. */
203 zclient = zclient_new(master);
204 zclient->enable = 1;
205 #ifdef HAVE_TCP_ZEBRA
206 zclient->sock = zclient_socket ();
207 #else
208 zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH);
209 #endif /* HAVE_TCP_ZEBRA */
210
211 /* Open simulation file. */
212 fp = fopen (argv[1], "r");
213 if (fp == NULL)
214 {
215 fprintf (stderr,"%% Can't open configuration file %s due to '%s'\n",
216 argv[1], safe_strerror(errno));
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 }