paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
Paul Jakma | 19a9372 | 2008-08-26 14:49:40 +0100 | [diff] [blame] | 2 | * $Quagga: $Format:%an, %ai, %h$ $ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3 | * |
| 4 | * GNU Zebra client test main routine. |
| 5 | * Copyright (C) 1997 Kunihiro Ishiguro |
| 6 | * |
| 7 | * This file is part of GNU Zebra. |
| 8 | * |
| 9 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2, or (at your option) any |
| 12 | * later version. |
| 13 | * |
| 14 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 21 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 22 | * 02111-1307, USA. |
| 23 | */ |
| 24 | |
| 25 | #include <zebra.h> |
| 26 | |
| 27 | #include "prefix.h" |
| 28 | #include "stream.h" |
| 29 | #include "zclient.h" |
| 30 | #include "thread.h" |
| 31 | #include "table.h" |
| 32 | #include "zebra/rib.h" |
| 33 | #include "zebra/zserv.h" |
| 34 | |
| 35 | struct thread *master; |
| 36 | |
| 37 | /* Zebra client structure. */ |
| 38 | struct zclient *zclient = NULL; |
| 39 | |
| 40 | /* Zebra socket. */ |
| 41 | int sock; |
| 42 | |
| 43 | /* IPv4 route add and delete test. */ |
| 44 | void |
| 45 | zebra_test_ipv4 (int command, int type, char *prefix, char *gateway, |
| 46 | u_char distance) |
| 47 | { |
| 48 | struct zapi_ipv4 api; |
| 49 | struct prefix_ipv4 p; |
| 50 | struct in_addr gate; |
| 51 | struct in_addr *gpnt; |
| 52 | |
| 53 | str2prefix_ipv4 (prefix, &p); |
| 54 | inet_aton (gateway, &gate); |
| 55 | gpnt = &gate; |
| 56 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 57 | api.vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | { |
| 68 | SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE); |
| 69 | api.distance = distance; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | switch (command) |
| 74 | { |
| 75 | case ZEBRA_IPV4_ROUTE_ADD: |
| 76 | zapi_ipv4_add (zclient, &p, &api); |
| 77 | break; |
| 78 | case ZEBRA_IPV4_ROUTE_DELETE: |
| 79 | zapi_ipv4_delete (zclient, &p, &api); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #ifdef HAVE_IPV6 |
| 85 | /* IPv6 route add and delete test. */ |
| 86 | void |
| 87 | zebra_test_v6 (int sock) |
| 88 | { |
| 89 | struct prefix_ipv6 p; |
| 90 | struct in6_addr nexthop; |
| 91 | |
| 92 | str2prefix_ipv6 ("3ffe:506::2/128", &p); |
| 93 | inet_pton (AF_INET6, "::1", &nexthop); |
| 94 | |
| 95 | /* zebra_ipv6_add (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */ |
| 96 | |
| 97 | sleep (5); |
| 98 | /* zebra_ipv6_delete (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */ |
| 99 | } |
| 100 | #endif /* HAVE_IPV6 */ |
| 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 }, |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 119 | { "babel", ZEBRA_ROUTE_BABEL }, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 120 | { "ospf", ZEBRA_ROUTE_OSPF }, |
| 121 | { "ospf6", ZEBRA_ROUTE_OSPF6 }, |
| 122 | { "bgp", ZEBRA_ROUTE_BGP }, |
Timo Teräs | dafa05e | 2017-01-19 17:27:01 +0200 | [diff] [blame] | 123 | { "nhrp", ZEBRA_ROUTE_NHRP }, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | { |
Donald Sharp | 7125293 | 2015-09-24 09:25:19 -0400 | [diff] [blame] | 197 | struct thread_master *master; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | FILE *fp; |
| 199 | |
| 200 | if (argc == 1) |
| 201 | usage_exit (); |
| 202 | |
Donald Sharp | 7125293 | 2015-09-24 09:25:19 -0400 | [diff] [blame] | 203 | master = thread_master_create (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 204 | /* Establish connection to zebra. */ |
Donald Sharp | 7125293 | 2015-09-24 09:25:19 -0400 | [diff] [blame] | 205 | zclient = zclient_new (master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | { |
| 217 | fprintf (stderr, "can't open %s\n", argv[1]); |
| 218 | exit (1); |
| 219 | } |
| 220 | |
| 221 | /* Do main work. */ |
| 222 | zebra_sim (fp); |
| 223 | |
| 224 | sleep (100); |
| 225 | |
| 226 | fclose (fp); |
| 227 | close (sock); |
| 228 | |
| 229 | return 0; |
| 230 | } |