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