proc_net.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include "proc.h"
  10. #include "procfs.h"
  11. #include <rthw.h>
  12. #include <rtdbg.h>
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <dfs_dentry.h>
  16. #ifdef RT_USING_LWIP
  17. #include "lwip/opt.h"
  18. #endif
  19. #if LWIP_ROUTE
  20. extern int inet_route_foreach(void (*func)(const char *name, uint32_t ip_addr, uint32_t netmask, void *parameter), void *parameter);
  21. #endif
  22. static void *seq_start(struct dfs_seq_file *seq, off_t *index)
  23. {
  24. off_t i = *index; // seq->index
  25. return NULL + (i == 0);
  26. }
  27. static void seq_stop(struct dfs_seq_file *seq, void *data)
  28. {
  29. }
  30. static void *seq_next(struct dfs_seq_file *seq, void *data, off_t *index)
  31. {
  32. /* data: The return value of the start or next*/
  33. off_t i = *index + 1; // seq->index
  34. *index = i;
  35. return NULL;
  36. }
  37. #if LWIP_ROUTE
  38. static void route_show(const char *name, uint32_t ip_addr, uint32_t netmask, void *parameter)
  39. {
  40. struct dfs_seq_file *seq = (struct dfs_seq_file *)parameter;
  41. /* "Iface\tDestination\tGateway "
  42. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  43. "\tWindow\tIRTT"); */
  44. /* "%63s%lx%lx%X%d%d%d%lx%d%d%d\n" */
  45. dfs_seq_printf(seq, "%s ", name);
  46. dfs_seq_printf(seq, "%lx ", ip_addr);
  47. dfs_seq_printf(seq, "%lx ", 0);
  48. dfs_seq_printf(seq, "%X ", 1);
  49. dfs_seq_printf(seq, "%d ", 0);
  50. dfs_seq_printf(seq, "%d ", 0);
  51. dfs_seq_printf(seq, "%d ", 0);
  52. dfs_seq_printf(seq, "%lx ", netmask);
  53. dfs_seq_printf(seq, "%d ", 0);
  54. dfs_seq_printf(seq, "%d ", 0);
  55. dfs_seq_printf(seq, "%d\n", 0);
  56. }
  57. #endif
  58. static int seq_show(struct dfs_seq_file *seq, void *data)
  59. {
  60. /* data: The return value of the start or next*/
  61. dfs_seq_printf(seq, "\n");
  62. #if LWIP_ROUTE
  63. inet_route_foreach(route_show, seq);
  64. #endif
  65. return 0;
  66. }
  67. static const struct dfs_seq_ops seq_ops = {
  68. .start = seq_start,
  69. .stop = seq_stop,
  70. .next = seq_next,
  71. .show = seq_show,
  72. };
  73. int proc_net_init(void)
  74. {
  75. struct proc_dentry *dentry;
  76. dentry = proc_mkdir("net", NULL);
  77. if (!dentry)
  78. return -1;
  79. proc_release(dentry);
  80. dentry = proc_create_data("net/route", 0, NULL, NULL, NULL);
  81. if (dentry)
  82. {
  83. dentry->seq_ops = &seq_ops;
  84. }
  85. proc_release(dentry);
  86. return 0;
  87. }
  88. INIT_ENV_EXPORT(proc_net_init);