client_main.c 4.5 KB

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