net_foreach_fileroute.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /****************************************************************************
  2. * net/route/net_foreach_fileroute.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <fcntl.h>
  41. #include <errno.h>
  42. #include <debug.h>
  43. #include <nuttx/fs/fs.h>
  44. #include "route/fileroute.h"
  45. #include "route/route.h"
  46. #if defined(CONFIG_ROUTE_IPv4_FILEROUTE) || defined(CONFIG_ROUTE_IPv6_FILEROUTE)
  47. /****************************************************************************
  48. * Public Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: net_foreachroute_ipv4 and net_foreachroute_ipv6
  52. *
  53. * Description:
  54. * Traverse the routing table
  55. *
  56. * Input Parameters:
  57. * handler - Will be called for each route in the routing table.
  58. * arg - An arbitrary value that will be passed tot he handler.
  59. *
  60. * Returned Value:
  61. * Zero (OK) returned if the entire table was search. A negated errno
  62. * value will be returned in the event of a failure. Handlers may also
  63. * terminate the search early with any non-zero, non-negative value.
  64. *
  65. ****************************************************************************/
  66. #ifdef CONFIG_ROUTE_IPv4_FILEROUTE
  67. int net_foreachroute_ipv4(route_handler_ipv4_t handler, FAR void *arg)
  68. {
  69. struct net_route_ipv4_s route;
  70. struct file fshandle;
  71. ssize_t nread;
  72. int ret = 0;
  73. /* Open the IPv4 routing table for read-only access */
  74. ret = net_openroute_ipv4(O_RDONLY, &fshandle);
  75. if (ret < 0)
  76. {
  77. /* Special case: the routing table has not yet been created. This is
  78. * not an error. We will just want to return successful completion of
  79. * the traversal.
  80. */
  81. if (ret == -ENOENT)
  82. {
  83. /* The routing table does not exit.. return successful completion */
  84. ninfo("The IPv4 routing table file does not exist\n");
  85. return OK;
  86. }
  87. /* Some other error occurred. */
  88. nerr("ERROR: Could not open IPv4 routing table: %d\n", ret);
  89. return ret;
  90. }
  91. /* Read each entry from the routing table */
  92. for (; ; )
  93. {
  94. nread = net_readroute_ipv4(&fshandle, &route);
  95. if (nread < 0)
  96. {
  97. /* File read error */
  98. nerr("ERROR: net_readroute_ipv4() failed: %ld\n", (long)nread);
  99. ret = (int)nread;
  100. break;
  101. }
  102. else if (nread == 0)
  103. {
  104. /* End of file */
  105. ret = OK;
  106. break;
  107. }
  108. /* Call the handler. */
  109. ret = handler(&route, arg);
  110. if (ret != OK)
  111. {
  112. /* Terminate early if the handler returns any non-zero value. */
  113. break;
  114. }
  115. }
  116. net_closeroute_ipv4(&fshandle);
  117. return ret;
  118. }
  119. #endif
  120. #ifdef CONFIG_ROUTE_IPv6_FILEROUTE
  121. int net_foreachroute_ipv6(route_handler_ipv6_t handler, FAR void *arg)
  122. {
  123. struct net_route_ipv6_s route;
  124. struct file fshandle;
  125. ssize_t nread;
  126. int ret = 0;
  127. /* Open the IPv6 routing table for read-only access */
  128. ret = net_openroute_ipv6(O_RDONLY, &fshandle);
  129. if (ret < 0)
  130. {
  131. /* Special case: the routing table has not yet been created. This is
  132. * not an error. We will just want to return successful completion of
  133. * the traversal.
  134. */
  135. if (ret == -ENOENT)
  136. {
  137. /* The routing table does not exit.. return successful completion */
  138. ninfo("The IPv6 routing table file does not exist\n");
  139. return OK;
  140. }
  141. /* Some other error occurred. */
  142. nerr("ERROR: Could not open IPv6 routing table: %d\n", ret);
  143. return ret;
  144. }
  145. /* Read each entry from the routing table */
  146. for (; ; )
  147. {
  148. nread = net_readroute_ipv6(&fshandle, &route);
  149. if (nread < 0)
  150. {
  151. /* File read error */
  152. nerr("ERROR: net_readroute_ipv6() failed: %ld\n", (long)nread);
  153. ret = (int)nread;
  154. break;
  155. }
  156. else if (nread == 0)
  157. {
  158. /* End of file */
  159. ret = OK;
  160. break;
  161. }
  162. /* Call the handler. */
  163. ret = handler(&route, arg);
  164. if (ret != OK)
  165. {
  166. /* Terminate early if the handler returns any non-zero value. */
  167. break;
  168. }
  169. }
  170. net_closeroute_ipv6(&fshandle);
  171. return ret;
  172. }
  173. #endif
  174. #endif /* CONFIG_ROUTE_IPv4_FILEROUTE || CONFIG_ROUTE_IPv6_FILEROUTE */