]> git.proxmox.com Git - mirror_frr.git/blob - zebra/client_main.c
Merge pull request #2788 from ton31337/fix/print_ipv6_route_if_afi
[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 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
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
32 struct thread *master;
33
34 /* Zebra client structure. */
35 struct zclient *zclient = NULL;
36
37 /* Zebra socket. */
38 int sock;
39
40 /* IPv4 route add and delete test. */
41 void zebra_test_ipv4(int command, int type, char *prefix, char *gateway,
42 uint8_t distance)
43 {
44 struct zapi_ipv4 api;
45 struct prefix_ipv4 p;
46 struct in_addr gate;
47 struct in_addr *gpnt;
48
49 str2prefix_ipv4(prefix, &p);
50 if (!inet_aton(gateway, &gate)) {
51 printf("Gateway specified: %s is illegal\n", gateway);
52 return;
53 }
54
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 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
68 api.distance = distance;
69 }
70
71
72 switch (command) {
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 zebra_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 }
96
97 /* Print out usage and exit. */
98 void usage_exit()
99 {
100 fprintf(stderr, "Usage: client filename\n");
101 exit(1);
102 }
103
104 struct zebra_info {
105 char *str;
106 int type;
107 } zebra_type[] = {{"static", ZEBRA_ROUTE_STATIC},
108 {"rip", ZEBRA_ROUTE_RIP},
109 {"ripng", ZEBRA_ROUTE_RIPNG},
110 {"ospf", ZEBRA_ROUTE_OSPF},
111 {"ospf6", ZEBRA_ROUTE_OSPF6},
112 {"bgp", ZEBRA_ROUTE_BGP},
113 {"nhrp", ZEBRA_ROUTE_NHRP},
114 {"pim", ZEBRA_ROUTE_PIM},
115 {NULL, 0}};
116
117 /* Zebra route simulator. */
118 void zebra_sim(FILE *fp)
119 {
120 char buf[BUFSIZ];
121 char distance_str[BUFSIZ];
122 uint8_t distance;
123
124 while (fgets(buf, sizeof buf, fp)) {
125 int i;
126 int ret;
127 int type;
128 char str[BUFSIZ], command[BUFSIZ], prefix[BUFSIZ],
129 gateway[BUFSIZ];
130
131 distance = 0;
132
133 if (*buf == '#')
134 continue;
135
136 type = ZEBRA_ROUTE_STATIC;
137
138 ret = sscanf(buf, "%s %s %s %s %s\n", command, str, prefix,
139 gateway, distance_str);
140
141 if (ret == 5) {
142 distance = atoi(distance_str);
143 } else {
144 ret = sscanf(buf, "%s %s %s %s\n", command, str, prefix,
145 gateway);
146
147 if (ret != 4)
148 continue;
149 }
150
151 i = 0;
152 while (zebra_type[i++].str) {
153 if (strcmp(zebra_type[i].str, str) == 0) {
154 type = zebra_type[i].type;
155 break;
156 }
157 }
158
159 if (strcmp(command, "add") == 0) {
160 zebra_test_ipv4(ZEBRA_IPV4_ROUTE_ADD, type, prefix,
161 gateway, distance);
162 printf("%s", buf);
163 continue;
164 }
165
166 if (strcmp(command, "del") == 0) {
167 zebra_test_ipv4(ZEBRA_IPV4_ROUTE_DELETE, type, prefix,
168 gateway, distance);
169 printf("%s", buf);
170 continue;
171 }
172 }
173 }
174
175 /* Test zebra client main routine. */
176 int main(int argc, char **argv)
177 {
178 struct thread_master *master;
179 FILE *fp;
180
181 if (argc == 1)
182 usage_exit();
183
184 master = thread_master_create(NULL);
185 /* Establish connection to zebra. */
186 zclient = zclient_new_notify(master, &zclient_options_default);
187 zclient->enable = 1;
188 zclient_socket_connect(zclient);
189
190 /* Open simulation file. */
191 fp = fopen(argv[1], "r");
192 if (fp == NULL) {
193 fprintf(stderr,
194 "%% Can't open configuration file %s due to '%s'\n",
195 argv[1], safe_strerror(errno));
196 exit(1);
197 }
198
199 /* Do main work. */
200 zebra_sim(fp);
201
202 sleep(100);
203
204 fclose(fp);
205 close(sock);
206
207 return 0;
208 }