]> git.proxmox.com Git - mirror_frr.git/blame - zebra/client_main.c
*: Make zapi route install Notifications optional
[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. */
d62a17ae 41void zebra_test_ipv4(int command, int type, char *prefix, char *gateway,
42 u_char distance)
718e3744 43{
d62a17ae 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 }
718e3744 80}
81
718e3744 82/* IPv6 route add and delete test. */
d62a17ae 83void zebra_test_v6(int sock)
718e3744 84{
d62a17ae 85 struct prefix_ipv6 p;
86 struct in6_addr nexthop;
718e3744 87
d62a17ae 88 str2prefix_ipv6("3ffe:506::2/128", &p);
89 inet_pton(AF_INET6, "::1", &nexthop);
718e3744 90
d62a17ae 91 /* zebra_ipv6_add (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */
718e3744 92
d62a17ae 93 sleep(5);
94 /* zebra_ipv6_delete (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */
718e3744 95}
718e3744 96
97/* Print out usage and exit. */
d62a17ae 98void usage_exit()
718e3744 99{
d62a17ae 100 fprintf(stderr, "Usage: client filename\n");
101 exit(1);
718e3744 102}
103
d62a17ae 104struct 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}};
718e3744 116
117/* Zebra route simulator. */
d62a17ae 118void zebra_sim(FILE *fp)
718e3744 119{
d62a17ae 120 char buf[BUFSIZ];
121 char distance_str[BUFSIZ];
122 u_char 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 for (i = 0; i < 10; i++) {
152 if (!zebra_type[i].str)
153 break;
154 if (strcmp(zebra_type[i].str, str) == 0) {
155 type = zebra_type[i].type;
156 break;
157 }
158 }
159
160 if (strcmp(command, "add") == 0) {
161 zebra_test_ipv4(ZEBRA_IPV4_ROUTE_ADD, type, prefix,
162 gateway, distance);
163 printf("%s", buf);
164 continue;
165 }
166
167 if (strcmp(command, "del") == 0) {
168 zebra_test_ipv4(ZEBRA_IPV4_ROUTE_DELETE, type, prefix,
169 gateway, distance);
170 printf("%s", buf);
171 continue;
172 }
718e3744 173 }
718e3744 174}
175
176/* Test zebra client main routine. */
d62a17ae 177int main(int argc, char **argv)
718e3744 178{
d62a17ae 179 struct thread_master *master;
180 FILE *fp;
718e3744 181
d62a17ae 182 if (argc == 1)
183 usage_exit();
718e3744 184
d62a17ae 185 master = thread_master_create(NULL);
186 /* Establish connection to zebra. */
e1a1880d 187 zclient = zclient_new_notify(master, &zclient_options_default);
d62a17ae 188 zclient->enable = 1;
689f5a8c 189 zclient_socket_connect(zclient);
718e3744 190
d62a17ae 191 /* Open simulation file. */
192 fp = fopen(argv[1], "r");
193 if (fp == NULL) {
194 fprintf(stderr,
195 "%% Can't open configuration file %s due to '%s'\n",
196 argv[1], safe_strerror(errno));
197 exit(1);
198 }
718e3744 199
d62a17ae 200 /* Do main work. */
201 zebra_sim(fp);
718e3744 202
d62a17ae 203 sleep(100);
718e3744 204
d62a17ae 205 fclose(fp);
206 close(sock);
718e3744 207
d62a17ae 208 return 0;
718e3744 209}