paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * $Id: client_main.c,v 1.1 2002/12/13 20:15:30 paul Exp $ |
| 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 | |
| 57 | api.type = type; |
| 58 | api.flags = 0; |
| 59 | |
| 60 | api.message = 0; |
| 61 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
| 62 | api.nexthop_num = 1; |
| 63 | api.nexthop = &gpnt; |
| 64 | api.ifindex_num = 0; |
| 65 | if (distance) |
| 66 | { |
| 67 | SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE); |
| 68 | api.distance = distance; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | switch (command) |
| 73 | { |
| 74 | case ZEBRA_IPV4_ROUTE_ADD: |
| 75 | zapi_ipv4_add (zclient, &p, &api); |
| 76 | break; |
| 77 | case ZEBRA_IPV4_ROUTE_DELETE: |
| 78 | zapi_ipv4_delete (zclient, &p, &api); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #ifdef HAVE_IPV6 |
| 84 | /* IPv6 route add and delete test. */ |
| 85 | void |
| 86 | zebra_test_v6 (int sock) |
| 87 | { |
| 88 | struct prefix_ipv6 p; |
| 89 | struct in6_addr nexthop; |
| 90 | |
| 91 | str2prefix_ipv6 ("3ffe:506::2/128", &p); |
| 92 | inet_pton (AF_INET6, "::1", &nexthop); |
| 93 | |
| 94 | /* zebra_ipv6_add (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */ |
| 95 | |
| 96 | sleep (5); |
| 97 | /* zebra_ipv6_delete (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */ |
| 98 | } |
| 99 | #endif /* HAVE_IPV6 */ |
| 100 | |
| 101 | /* Print out usage and exit. */ |
| 102 | void |
| 103 | usage_exit () |
| 104 | { |
| 105 | fprintf (stderr, "Usage: client filename\n"); |
| 106 | exit (1); |
| 107 | } |
| 108 | |
| 109 | struct zebra_info |
| 110 | { |
| 111 | char *str; |
| 112 | int type; |
| 113 | } zebra_type[] = |
| 114 | { |
| 115 | { "static", ZEBRA_ROUTE_STATIC }, |
| 116 | { "rip", ZEBRA_ROUTE_RIP }, |
| 117 | { "ripng", ZEBRA_ROUTE_RIPNG }, |
| 118 | { "ospf", ZEBRA_ROUTE_OSPF }, |
| 119 | { "ospf6", ZEBRA_ROUTE_OSPF6 }, |
| 120 | { "bgp", ZEBRA_ROUTE_BGP }, |
| 121 | { NULL, 0 } |
| 122 | }; |
| 123 | |
| 124 | /* Zebra route simulator. */ |
| 125 | void |
| 126 | zebra_sim (FILE *fp) |
| 127 | { |
| 128 | char buf[BUFSIZ]; |
| 129 | char distance_str[BUFSIZ]; |
| 130 | u_char distance; |
| 131 | |
| 132 | while (fgets (buf, sizeof buf, fp)) |
| 133 | { |
| 134 | int i; |
| 135 | int ret; |
| 136 | int type; |
| 137 | char str[BUFSIZ], command[BUFSIZ], prefix[BUFSIZ], gateway[BUFSIZ]; |
| 138 | |
| 139 | distance = 0; |
| 140 | |
| 141 | if (*buf == '#') |
| 142 | continue; |
| 143 | |
| 144 | type = ZEBRA_ROUTE_STATIC; |
| 145 | |
| 146 | ret = sscanf (buf, "%s %s %s %s %s\n", command, str, prefix, gateway, |
| 147 | distance_str); |
| 148 | |
| 149 | if (ret == 5) |
| 150 | { |
| 151 | distance = atoi (distance_str); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | ret = sscanf (buf, "%s %s %s %s\n", command, str, prefix, gateway); |
| 156 | |
| 157 | if (ret != 4) |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | for (i = 0; i < 10; i++) |
| 162 | { |
| 163 | if (!zebra_type[i].str) |
| 164 | break; |
| 165 | if (strcmp (zebra_type[i].str, str) == 0) |
| 166 | { |
| 167 | type = zebra_type[i].type; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (strcmp (command, "add") == 0) |
| 173 | { |
| 174 | zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_ADD, type, prefix, gateway, |
| 175 | distance); |
| 176 | printf ("%s", buf); |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | if (strcmp (command, "del") == 0) |
| 181 | { |
| 182 | zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_DELETE, type, prefix, gateway, |
| 183 | distance); |
| 184 | printf ("%s", buf); |
| 185 | continue; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* Test zebra client main routine. */ |
| 191 | int |
| 192 | main (int argc, char **argv) |
| 193 | { |
| 194 | FILE *fp; |
| 195 | |
| 196 | if (argc == 1) |
| 197 | usage_exit (); |
| 198 | |
| 199 | /* Establish connection to zebra. */ |
| 200 | zclient = zclient_new (); |
| 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 | { |
| 212 | fprintf (stderr, "can't open %s\n", argv[1]); |
| 213 | exit (1); |
| 214 | } |
| 215 | |
| 216 | /* Do main work. */ |
| 217 | zebra_sim (fp); |
| 218 | |
| 219 | sleep (100); |
| 220 | |
| 221 | fclose (fp); |
| 222 | close (sock); |
| 223 | |
| 224 | return 0; |
| 225 | } |