net_add_fileroute.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /****************************************************************************
  2. * net/route/net_add_fileroute.c
  3. *
  4. * Copyright (C) 2013, 2015, 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 <string.h>
  42. #include <errno.h>
  43. #include <debug.h>
  44. #include <nuttx/fs/fs.h>
  45. #include <nuttx/net/ip.h>
  46. #include "route/fileroute.h"
  47. #include "route/route.h"
  48. #if defined(CONFIG_ROUTE_IPv4_FILEROUTE) || defined(CONFIG_ROUTE_IPv6_FILEROUTE)
  49. /****************************************************************************
  50. * Public Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: net_addroute_ipv4 and net_addroute_ipv6
  54. *
  55. * Description:
  56. * Add a new route to the routing table
  57. *
  58. * Input Parameters:
  59. *
  60. * Returned Value:
  61. * OK on success; Negated errno on failure.
  62. *
  63. ****************************************************************************/
  64. #ifdef CONFIG_ROUTE_IPv4_FILEROUTE
  65. int net_addroute_ipv4(in_addr_t target, in_addr_t netmask, in_addr_t router)
  66. {
  67. struct net_route_ipv4_s route;
  68. struct file fshandle;
  69. ssize_t nwritten;
  70. int ret;
  71. /* Format the new routing table entry */
  72. net_ipv4addr_copy(route.target, target);
  73. net_ipv4addr_copy(route.netmask, netmask);
  74. net_ipv4addr_copy(route.router, router);
  75. net_ipv4_dumproute("New route", &route);
  76. /* Open the IPv4 routing table for append access */
  77. ret = net_openroute_ipv4(O_WRONLY | O_APPEND | O_CREAT, &fshandle);
  78. if (ret < 0)
  79. {
  80. nerr("ERROR: Could not open IPv4 routing table: %d\n", ret);
  81. return ret;
  82. }
  83. /* Then append the new entry to the end of the routing table */
  84. nwritten = net_writeroute_ipv4(&fshandle, &route);
  85. net_closeroute_ipv4(&fshandle);
  86. return nwritten >= 0 ? 0 : (int)nwritten;
  87. }
  88. #endif
  89. #ifdef CONFIG_ROUTE_IPv6_FILEROUTE
  90. int net_addroute_ipv6(net_ipv6addr_t target, net_ipv6addr_t netmask,
  91. net_ipv6addr_t router)
  92. {
  93. struct net_route_ipv6_s route;
  94. struct file fshandle;
  95. ssize_t nwritten;
  96. int ret;
  97. /* Format the new routing table entry */
  98. net_ipv6addr_copy(route.target, target);
  99. net_ipv6addr_copy(route.netmask, netmask);
  100. net_ipv6addr_copy(route.router, router);
  101. net_ipv6_dumproute("New route", &route);
  102. /* Open the IPv6 routing table for append access */
  103. ret = net_openroute_ipv6(O_WRONLY | O_APPEND | O_CREAT, &fshandle);
  104. if (ret < 0)
  105. {
  106. nerr("ERROR: Could not open IPv6 routing table: %d\n", ret);
  107. return ret;
  108. }
  109. /* Then append the new entry to the end of the routing table */
  110. nwritten = net_writeroute_ipv6(&fshandle, &route);
  111. net_closeroute_ipv6(&fshandle);
  112. return nwritten >= 0 ? 0 : (int)nwritten;
  113. }
  114. #endif
  115. #endif /* CONFIG_ROUTE_IPv4_FILEROUTE || CONFIG_ROUTE_IPv6_FILEROUTE */