]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | |
42 | zebra_test_ipv4 (int command, int type, char *prefix, char *gateway, | |
43 | u_char distance) | |
44 | { | |
45 | struct zapi_ipv4 api; | |
46 | struct prefix_ipv4 p; | |
47 | struct in_addr gate; | |
48 | struct in_addr *gpnt; | |
49 | ||
50 | str2prefix_ipv4 (prefix, &p); | |
51 | inet_aton (gateway, &gate); | |
52 | gpnt = &gate; | |
53 | ||
7076bb2f | 54 | api.vrf_id = VRF_DEFAULT; |
718e3744 | 55 | api.type = type; |
56 | api.flags = 0; | |
57 | ||
58 | api.message = 0; | |
59 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); | |
60 | api.nexthop_num = 1; | |
61 | api.nexthop = &gpnt; | |
62 | api.ifindex_num = 0; | |
63 | if (distance) | |
64 | { | |
65 | SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE); | |
66 | api.distance = distance; | |
67 | } | |
68 | ||
69 | ||
70 | switch (command) | |
71 | { | |
72 | case ZEBRA_IPV4_ROUTE_ADD: | |
73 | zapi_ipv4_add (zclient, &p, &api); | |
74 | break; | |
75 | case ZEBRA_IPV4_ROUTE_DELETE: | |
76 | zapi_ipv4_delete (zclient, &p, &api); | |
77 | break; | |
78 | } | |
79 | } | |
80 | ||
718e3744 | 81 | /* IPv6 route add and delete test. */ |
82 | void | |
83 | 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 | } | |
718e3744 | 96 | |
97 | /* Print out usage and exit. */ | |
98 | void | |
99 | usage_exit () | |
100 | { | |
101 | fprintf (stderr, "Usage: client filename\n"); | |
102 | exit (1); | |
103 | } | |
104 | ||
105 | struct zebra_info | |
106 | { | |
107 | char *str; | |
108 | int type; | |
109 | } zebra_type[] = | |
110 | { | |
111 | { "static", ZEBRA_ROUTE_STATIC }, | |
112 | { "rip", ZEBRA_ROUTE_RIP }, | |
113 | { "ripng", ZEBRA_ROUTE_RIPNG }, | |
114 | { "ospf", ZEBRA_ROUTE_OSPF }, | |
115 | { "ospf6", ZEBRA_ROUTE_OSPF6 }, | |
116 | { "bgp", ZEBRA_ROUTE_BGP }, | |
2fb975da | 117 | { "nhrp", ZEBRA_ROUTE_NHRP }, |
ba4eb1bc | 118 | { "pim", ZEBRA_ROUTE_PIM }, |
718e3744 | 119 | { NULL, 0 } |
120 | }; | |
121 | ||
122 | /* Zebra route simulator. */ | |
123 | void | |
124 | zebra_sim (FILE *fp) | |
125 | { | |
126 | char buf[BUFSIZ]; | |
127 | char distance_str[BUFSIZ]; | |
128 | u_char distance; | |
129 | ||
130 | while (fgets (buf, sizeof buf, fp)) | |
131 | { | |
132 | int i; | |
133 | int ret; | |
134 | int type; | |
135 | char str[BUFSIZ], command[BUFSIZ], prefix[BUFSIZ], gateway[BUFSIZ]; | |
136 | ||
137 | distance = 0; | |
138 | ||
139 | if (*buf == '#') | |
140 | continue; | |
141 | ||
142 | type = ZEBRA_ROUTE_STATIC; | |
143 | ||
144 | ret = sscanf (buf, "%s %s %s %s %s\n", command, str, prefix, gateway, | |
145 | distance_str); | |
146 | ||
147 | if (ret == 5) | |
148 | { | |
149 | distance = atoi (distance_str); | |
150 | } | |
151 | else | |
152 | { | |
153 | ret = sscanf (buf, "%s %s %s %s\n", command, str, prefix, gateway); | |
154 | ||
155 | if (ret != 4) | |
156 | continue; | |
157 | } | |
158 | ||
159 | for (i = 0; i < 10; i++) | |
160 | { | |
161 | if (!zebra_type[i].str) | |
162 | break; | |
163 | if (strcmp (zebra_type[i].str, str) == 0) | |
164 | { | |
165 | type = zebra_type[i].type; | |
166 | break; | |
167 | } | |
168 | } | |
169 | ||
170 | if (strcmp (command, "add") == 0) | |
171 | { | |
172 | zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_ADD, type, prefix, gateway, | |
173 | distance); | |
174 | printf ("%s", buf); | |
175 | continue; | |
176 | } | |
177 | ||
178 | if (strcmp (command, "del") == 0) | |
179 | { | |
180 | zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_DELETE, type, prefix, gateway, | |
181 | distance); | |
182 | printf ("%s", buf); | |
183 | continue; | |
184 | } | |
185 | } | |
186 | } | |
187 | ||
188 | /* Test zebra client main routine. */ | |
189 | int | |
190 | main (int argc, char **argv) | |
191 | { | |
4140ca4d | 192 | struct thread_master *master; |
718e3744 | 193 | FILE *fp; |
194 | ||
195 | if (argc == 1) | |
196 | usage_exit (); | |
197 | ||
4140ca4d | 198 | master = thread_master_create(); |
718e3744 | 199 | /* Establish connection to zebra. */ |
4140ca4d | 200 | zclient = zclient_new(master); |
718e3744 | 201 | zclient->enable = 1; |
202 | #ifdef HAVE_TCP_ZEBRA | |
203 | zclient->sock = zclient_socket (); | |
204 | #else | |
205 | zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH); | |
206 | #endif /* HAVE_TCP_ZEBRA */ | |
207 | ||
208 | /* Open simulation file. */ | |
209 | fp = fopen (argv[1], "r"); | |
210 | if (fp == NULL) | |
211 | { | |
1db63918 DS |
212 | fprintf (stderr,"%% Can't open configuration file %s due to '%s'\n", |
213 | argv[1], safe_strerror(errno)); | |
718e3744 | 214 | exit (1); |
215 | } | |
216 | ||
217 | /* Do main work. */ | |
218 | zebra_sim (fp); | |
219 | ||
220 | sleep (100); | |
221 | ||
222 | fclose (fp); | |
223 | close (sock); | |
224 | ||
225 | return 0; | |
226 | } |