client_main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. { "nhrp", ZEBRA_ROUTE_NHRP },
  107. { NULL, 0 }
  108. };
  109. /* Zebra route simulator. */
  110. void
  111. zebra_sim (FILE *fp)
  112. {
  113. char buf[BUFSIZ];
  114. char distance_str[BUFSIZ];
  115. u_char distance;
  116. while (fgets (buf, sizeof buf, fp))
  117. {
  118. int i;
  119. int ret;
  120. int type;
  121. char str[BUFSIZ], command[BUFSIZ], prefix[BUFSIZ], gateway[BUFSIZ];
  122. distance = 0;
  123. if (*buf == '#')
  124. continue;
  125. type = ZEBRA_ROUTE_STATIC;
  126. ret = sscanf (buf, "%s %s %s %s %s\n", command, str, prefix, gateway,
  127. distance_str);
  128. if (ret == 5)
  129. {
  130. distance = atoi (distance_str);
  131. }
  132. else
  133. {
  134. ret = sscanf (buf, "%s %s %s %s\n", command, str, prefix, gateway);
  135. if (ret != 4)
  136. continue;
  137. }
  138. for (i = 0; i < 10; i++)
  139. {
  140. if (!zebra_type[i].str)
  141. break;
  142. if (strcmp (zebra_type[i].str, str) == 0)
  143. {
  144. type = zebra_type[i].type;
  145. break;
  146. }
  147. }
  148. if (strcmp (command, "add") == 0)
  149. {
  150. zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_ADD, type, prefix, gateway,
  151. distance);
  152. printf ("%s", buf);
  153. continue;
  154. }
  155. if (strcmp (command, "del") == 0)
  156. {
  157. zebra_test_ipv4 (ZEBRA_IPV4_ROUTE_DELETE, type, prefix, gateway,
  158. distance);
  159. printf ("%s", buf);
  160. continue;
  161. }
  162. }
  163. }
  164. /* Test zebra client main routine. */
  165. int
  166. main (int argc, char **argv)
  167. {
  168. struct thread_master *master;
  169. FILE *fp;
  170. if (argc == 1)
  171. usage_exit ();
  172. master = thread_master_create ();
  173. /* Establish connection to zebra. */
  174. zclient = zclient_new (master);
  175. zclient->enable = 1;
  176. #ifdef HAVE_TCP_ZEBRA
  177. zclient->sock = zclient_socket ();
  178. #else
  179. zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH);
  180. #endif /* HAVE_TCP_ZEBRA */
  181. /* Open simulation file. */
  182. fp = fopen (argv[1], "r");
  183. if (fp == NULL)
  184. {
  185. fprintf (stderr, "can't open %s\n", argv[1]);
  186. exit (1);
  187. }
  188. /* Do main work. */
  189. zebra_sim (fp);
  190. sleep (100);
  191. fclose (fp);
  192. close (sock);
  193. return 0;
  194. }