]> git.proxmox.com Git - mirror_frr.git/blob - zebra/client_main.c
f40d9954667e359e564ec579971ca0e843a62a19
[mirror_frr.git] / zebra / client_main.c
1 /*
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 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #include <zebra.h>
24
25 #include "prefix.h"
26 #include "stream.h"
27 #include "zclient.h"
28 #include "thread.h"
29 #include "table.h"
30 #include "zebra/rib.h"
31 #include "zebra/zserv.h"
32
33 struct thread *master;
34
35 /* Zebra client structure. */
36 struct zclient *zclient = NULL;
37
38 /* Zebra socket. */
39 int sock;
40
41 /* IPv4 route add and delete test. */
42 void
43 zebra_test_ipv4 (int command, int type, char *prefix, char *gateway,
44 u_char distance)
45 {
46 struct zapi_ipv4 api;
47 struct prefix_ipv4 p;
48 struct in_addr gate;
49 struct in_addr *gpnt;
50
51 str2prefix_ipv4 (prefix, &p);
52 inet_aton (gateway, &gate);
53 gpnt = &gate;
54
55 api.vrf_id = VRF_DEFAULT;
56 api.type = type;
57 api.flags = 0;
58
59 api.message = 0;
60 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
61 api.nexthop_num = 1;
62 api.nexthop = &gpnt;
63 api.ifindex_num = 0;
64 if (distance)
65 {
66 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
67 api.distance = distance;
68 }
69
70
71 switch (command)
72 {
73 case ZEBRA_IPV4_ROUTE_ADD:
74 zapi_ipv4_add (zclient, &p, &api);
75 break;
76 case ZEBRA_IPV4_ROUTE_DELETE:
77 zapi_ipv4_delete (zclient, &p, &api);
78 break;
79 }
80 }
81
82 /* IPv6 route add and delete test. */
83 void
84 zebra_test_v6 (int sock)
85 {
86 struct prefix_ipv6 p;
87 struct in6_addr nexthop;
88
89 str2prefix_ipv6 ("3ffe:506::2/128", &p);
90 inet_pton (AF_INET6, "::1", &nexthop);
91
92 /* zebra_ipv6_add (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */
93
94 sleep (5);
95 /* zebra_ipv6_delete (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */
96 }
97
98 /* Print out usage and exit. */
99 void
100 usage_exit ()
101 {
102 fprintf (stderr, "Usage: client filename\n");
103 exit (1);
104 }
105
106 struct zebra_info
107 {
108 char *str;
109 int type;
110 } zebra_type[] =
111 {
112 { "static", ZEBRA_ROUTE_STATIC },
113 { "rip", ZEBRA_ROUTE_RIP },
114 { "ripng", ZEBRA_ROUTE_RIPNG },
115 { "ospf", ZEBRA_ROUTE_OSPF },
116 { "ospf6", ZEBRA_ROUTE_OSPF6 },
117 { "bgp", ZEBRA_ROUTE_BGP },
118 { NULL, 0 }
119 };
120
121 /* Zebra route simulator. */
122 void
123 zebra_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. */
188 int
189 main (int argc, char **argv)
190 {
191 struct thread_master *master;
192 FILE *fp;
193
194 if (argc == 1)
195 usage_exit ();
196
197 master = thread_master_create();
198 /* Establish connection to zebra. */
199 zclient = zclient_new(master);
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 {
211 fprintf (stderr,"%% Can't open configuration file %s due to '%s'\n",
212 argv[1], safe_strerror(errno));
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 }