]> git.proxmox.com Git - mirror_frr.git/blame - zebra/client_main.c
Merge pull request #2057 from donaldsharp/fix_1916
[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,
d7c0a89a 42 uint8_t 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];
d7c0a89a 122 uint8_t distance;
d62a17ae 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
6c223294
QY
151 i = 0;
152 while (zebra_type[i++].str) {
d62a17ae 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 }
718e3744 172 }
718e3744 173}
174
175/* Test zebra client main routine. */
d62a17ae 176int main(int argc, char **argv)
718e3744 177{
d62a17ae 178 struct thread_master *master;
179 FILE *fp;
718e3744 180
d62a17ae 181 if (argc == 1)
182 usage_exit();
718e3744 183
d62a17ae 184 master = thread_master_create(NULL);
185 /* Establish connection to zebra. */
e1a1880d 186 zclient = zclient_new_notify(master, &zclient_options_default);
d62a17ae 187 zclient->enable = 1;
689f5a8c 188 zclient_socket_connect(zclient);
718e3744 189
d62a17ae 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 }
718e3744 198
d62a17ae 199 /* Do main work. */
200 zebra_sim(fp);
718e3744 201
d62a17ae 202 sleep(100);
718e3744 203
d62a17ae 204 fclose(fp);
205 close(sock);
718e3744 206
d62a17ae 207 return 0;
718e3744 208}