leaf.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <stdarg.h>
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. /**
  6. * No error.
  7. */
  8. #define ERR_OK 0
  9. /**
  10. * Config path error.
  11. */
  12. #define ERR_CONFIG_PATH 1
  13. /**
  14. * Config parsing error.
  15. */
  16. #define ERR_CONFIG 2
  17. /**
  18. * IO error.
  19. */
  20. #define ERR_IO 3
  21. /**
  22. * Config file watcher error.
  23. */
  24. #define ERR_WATCHER 4
  25. /**
  26. * Async channel send error.
  27. */
  28. #define ERR_ASYNC_CHANNEL_SEND 5
  29. /**
  30. * Sync channel receive error.
  31. */
  32. #define ERR_SYNC_CHANNEL_RECV 6
  33. /**
  34. * Runtime manager error.
  35. */
  36. #define ERR_RUNTIME_MANAGER 7
  37. /**
  38. * No associated config file.
  39. */
  40. #define ERR_NO_CONFIG_FILE 8
  41. /**
  42. * Starts leaf with options, on a successful start this function blocks the current
  43. * thread.
  44. *
  45. * @note This is not a stable API, parameters will change from time to time.
  46. *
  47. * @param rt_id A unique ID to associate this leaf instance, this is required when
  48. * calling subsequent FFI functions, e.g. reload, shutdown.
  49. * @param config_path The path of the config file, must be a file with suffix .conf
  50. * or .json, according to the enabled features.
  51. * @param auto_reload Enabls auto reloading when config file changes are detected,
  52. * takes effect only when the "auto-reload" feature is enabled.
  53. * @param multi_thread Whether to use a multi-threaded runtime.
  54. * @param auto_threads Sets the number of runtime worker threads automatically,
  55. * takes effect only when multi_thread is true.
  56. * @param threads Sets the number of runtime worker threads, takes effect when
  57. * multi_thread is true, but can be overridden by auto_threads.
  58. * @param stack_size Sets stack size of the runtime worker threads, takes effect when
  59. * multi_thread is true.
  60. * @return ERR_OK on finish running, any other errors means a startup failure.
  61. */
  62. int32_t leaf_run_with_options(uint16_t rt_id,
  63. const char *config_path,
  64. bool auto_reload,
  65. bool multi_thread,
  66. bool auto_threads,
  67. int32_t threads,
  68. int32_t stack_size);
  69. /**
  70. * Starts leaf with a single-threaded runtime, on a successful start this function
  71. * blocks the current thread.
  72. *
  73. * @param rt_id A unique ID to associate this leaf instance, this is required when
  74. * calling subsequent FFI functions, e.g. reload, shutdown.
  75. * @param config_path The path of the config file, must be a file with suffix .conf
  76. * or .json, according to the enabled features.
  77. * @return ERR_OK on finish running, any other errors means a startup failure.
  78. */
  79. int32_t leaf_run(uint16_t rt_id, const char *config_path);
  80. /**
  81. * Reloads DNS servers, outbounds and routing rules from the config file.
  82. *
  83. * @param rt_id The ID of the leaf instance to reload.
  84. *
  85. * @return Returns ERR_OK on success.
  86. */
  87. int32_t leaf_reload(uint16_t rt_id);
  88. /**
  89. * Shuts down leaf.
  90. *
  91. * @param rt_id The ID of the leaf instance to reload.
  92. *
  93. * @return Returns true on success, false otherwise.
  94. */
  95. bool leaf_shutdown(uint16_t rt_id);
  96. /**
  97. * Tests the configuration.
  98. *
  99. * @param config_path The path of the config file, must be a file with suffix .conf
  100. * or .json, according to the enabled features.
  101. * @return Returns ERR_OK on success, i.e no syntax error.
  102. */
  103. int32_t leaf_test_config(const char *config_path);