client_main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * $Quagga: $Format:%an, %ai, %h$ $
  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. #include <zebra.h>
  25. #include "prefix.h"
  26. #include "stream.h"
  27. #include "zclient.h"
  28. #include "thread.h"
  29. #include "table.h"
  30. #include "zebra/rib.h"
  31. #include "zebra/zserv.h"
  32. struct thread *master;
  33. /* Zebra client structure. */
  34. struct zclient *zclient = NULL;
  35. /* Zebra socket. */
  36. int sock;
  37. /* IPv4 route add and delete test. */
  38. void
  39. zebra_test_ipv4 (int command, int type, char *prefix, char *gateway,
  40. u_char distance)
  41. {
  42. struct zapi_ipv4 api;
  43. struct prefix_ipv4 p;
  44. struct in_addr gate;
  45. struct in_addr *gpnt;
  46. str2prefix_ipv4 (prefix, &p);
  47. inet_aton (gateway, &gate);
  48. gpnt = &gate;
  49. api.vrf_id = VRF_DEFAULT;
  50. api.type = type;
  51. api.flags = 0;
  52. api.message = 0;
  53. SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
  54. api.nexthop_num = 1;
  55. api.nexthop = &gpnt;
  56. api.ifindex_num = 0;
  57. if (distance)
  58. {
  59. SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
  60. api.distance = distance;
  61. }
  62. switch (command)
  63. {
  64. case ZEBRA_IPV4_ROUTE_ADD:
  65. zapi_ipv4_add (zclient, &p, &api);
  66. break;
  67. case ZEBRA_IPV4_ROUTE_DELETE:
  68. zapi_ipv4_delete (zclient, &p, &api);
  69. break;
  70. }
  71. }
  72. #ifdef HAVE_IPV6
  73. /* IPv6 route add and delete test. */
  74. void
  75. zebra_test_v6 (int sock)
  76. {
  77. struct prefix_ipv6 p;
  78. struct in6_addr nexthop;
  79. str2prefix_ipv6 ("3ffe:506::2/128", &p);
  80. inet_pton (AF_INET6, "::1", &nexthop);
  81. /* zebra_ipv6_add (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */
  82. sleep (5);
  83. /* zebra_ipv6_delete (sock, ZEBRA_ROUTE_STATIC, 0, &p, &nexthop, 1); */
  84. }
  85. #endif /* HAVE_IPV6 */
  86. /* Print out usage and exit. */
  87. void
  88. usage_exit ()
  89. {
  90. fprintf (stderr, "Usage: client filename\n");
  91. exit (1);
  92. }
  93. struct zebra_info
  94. {
  95. char *str;
  96. int type;
  97. } zebra_type[] =
  98. {
  99. { "static", ZEBRA_ROUTE_STATIC },
  100. { "rip", ZEBRA_ROUTE_RIP },
  101. { "ripng", ZEBRA_ROUTE_RIPNG },
  102. { "babel", ZEBRA_ROUTE_BABEL },
  103. { "ospf", ZEBRA_ROUTE_OSPF },
  104. { "ospf6", ZEBRA_ROUTE_OSPF6 },
  105. { "bgp", ZEBRA_ROUTE_BGP },
  106. { NULL, 0 }
  107. };
  108. /* Zebra route simulator. */
  109. void
  110. zebra_sim (FILE *fp)
  111. {
  112. char buf[BUFSIZ];
  113. char distance_str[BUFSIZ];
  114. u_char distance;
  115. while (fgets (buf, sizeof buf, fp))
  116. {
  117. int i;
  118. int ret;
  119. int type;
  120. char str[BUFSIZ], command[BUFSIZ], prefix[BUFSIZ], gateway[BUFSIZ];
  121. distance = 0;
  122. if (*buf == '#')
  123. continue;
  124. type = ZEBRA_ROUTE_STATIC;
  125. ret = sscanf (buf, "%s %s %s %s %s\n", command, str, prefix, gateway,
  126. distance_str);
  127. if (ret == 5)
  128. {
  129. distance = atoi (distance_str);
  130. }
  131. else
  132. {
  133. ret = sscanf (buf, "%s %s %s %s\n", command, str, prefix, gateway);
  134. if (ret != 4)
  135. continue;
  136. }
  137. for (i = 0; i < 10; i++)
  138. {
  139. if (!zebra_type[i].str)
  140. break;
  141. if (strcmp (zebra_type[i].str, str) == 0)
  142. {
  143. type = zebra_type[i].type;
  144. break;
  145. }
  146. }
  147. if (strcmp (command, "add") == 0)
  148. {
  149. zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_ADD, type, prefix, gateway,
  150. distance);
  151. printf ("%s", buf);
  152. continue;
  153. }
  154. if (strcmp (command, "del") == 0)
  155. {
  156. zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_DELETE, type, prefix, gateway,
  157. distance);
  158. printf ("%s", buf);
  159. continue;
  160. }
  161. }
  162. }
  163. /* Test zebra client main routine. */
  164. int
  165. main (int argc, char **argv)
  166. {
  167. FILE *fp;
  168. if (argc == 1)
  169. usage_exit ();
  170. /* Establish connection to zebra. */
  171. zclient = zclient_new ();
  172. zclient->enable = 1;
  173. #ifdef HAVE_TCP_ZEBRA
  174. zclient->sock = zclient_socket ();
  175. #else
  176. zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH);
  177. #endif /* HAVE_TCP_ZEBRA */
  178. /* Open simulation file. */
  179. fp = fopen (argv[1], "r");
  180. if (fp == NULL)
  181. {
  182. fprintf (stderr, "can't open %s\n", argv[1]);
  183. exit (1);
  184. }
  185. /* Do main work. */
  186. zebra_sim (fp);
  187. sleep (100);
  188. fclose (fp);
  189. close (sock);
  190. return 0;
  191. }