tor_api.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file tor_api.h
  8. * \brief Public C API for the Tor network service.
  9. *
  10. * This interface is intended for use by programs that need to link Tor as
  11. * a library, and launch it in a separate thread. If you have the ability
  12. * to run Tor as a separate executable, you should probably do that instead
  13. * of embedding it as a library.
  14. *
  15. * To use this API, first construct a tor_main_configuration_t object using
  16. * tor_main_configuration_new(). Then, you use one or more other function
  17. * calls (such as tor_main_configuration_set_command_line() to configure how
  18. * Tor should be run. Finally, you pass the configuration object to
  19. * tor_run_main().
  20. *
  21. * At this point, tor_run_main() will block its thread to run a Tor daemon;
  22. * when the Tor daemon exits, it will return. See notes on bugs and
  23. * limitations below.
  24. *
  25. * There is no other public C API to Tor: calling any C Tor function not
  26. * documented in this file is not guaranteed to be stable.
  27. **/
  28. #ifndef TOR_API_H
  29. #define TOR_API_H
  30. typedef struct tor_main_configuration_t tor_main_configuration_t;
  31. /**
  32. * Create and return a new tor_main_configuration().
  33. */
  34. tor_main_configuration_t *tor_main_configuration_new(void);
  35. /**
  36. * Set the command-line arguments in <b>cfg</b>.
  37. *
  38. * The <b>argc</b> and <b>argv</b> values here are as for main(). The
  39. * contents of the argv pointer must remain unchanged until tor_run_main() has
  40. * finished and you call tor_main_configuration_free().
  41. *
  42. * Return 0 on success, -1 on failure.
  43. */
  44. int tor_main_configuration_set_command_line(tor_main_configuration_t *cfg,
  45. int argc, char *argv[]);
  46. #ifdef _WIN32
  47. typedef SOCKET tor_control_socket_t;
  48. #define INVALID_TOR_CONTROL_SOCKET INVALID_SOCKET
  49. #else
  50. typedef int tor_control_socket_t;
  51. #define INVALID_TOR_CONTROL_SOCKET (-1)
  52. #endif
  53. /** DOCDOC */
  54. tor_control_socket_t tor_main_configuration_setup_control_socket(
  55. tor_main_configuration_t *cfg);
  56. /**
  57. * Release all storage held in <b>cfg</b>.
  58. *
  59. * Once you have passed a tor_main_configuration_t to tor_run_main(), you
  60. * must not free it until tor_run_main() has finished.
  61. */
  62. void tor_main_configuration_free(tor_main_configuration_t *cfg);
  63. /**
  64. * Return the name and version of the software implementing the tor_api
  65. * functionality. Current implementors are "tor" and "libtorrunner".
  66. *
  67. * Note that if you're using libtorrunner, you'll see the version of
  68. * libtorrunner, not the version of Tor that it's invoking for you.
  69. *
  70. * Added in Tor 0.3.5.1-alpha.
  71. *
  72. * Example return values include "tor 0.3.5.1-alpha" when linked directly
  73. * against tor, and "libtorrunner 0.3.5.1-alpha" when linked against
  74. * libtorrunner while it is invoking an arbitrary version of Tor. HOWEVER,
  75. * the user MUST NOT depend on any particular format or contents of this
  76. * string: there may be other things that implement Tor in the future.
  77. **/
  78. const char *tor_api_get_provider_version(void);
  79. /**
  80. * Run the tor process, as if from the command line.
  81. *
  82. * The command line arguments from tor_main_configuration_set_command_line()
  83. * are taken as if they had been passed to main().
  84. *
  85. * This function will not return until Tor is done running. It returns zero
  86. * on success, and nonzero on failure.
  87. *
  88. * If you want to control when Tor exits, make sure to configure a control
  89. * socket. The OwningControllerFD option may be helpful there.
  90. *
  91. * BUG 23847: Sometimes, if you call tor_main a second time (after it has
  92. * returned), Tor may crash or behave strangely. We have fixed all issues of
  93. * this type that we could find, but more may remain.
  94. *
  95. * LIMITATION: You cannot run more than one instance of Tor in the same
  96. * process at the same time. Concurrent calls will cause undefined behavior.
  97. * We do not currently have plans to change this.
  98. *
  99. * LIMITATION: While we will try to fix any problems found here, you
  100. * should be aware that Tor was originally written to run as its own
  101. * process, and that the functionality of this file was added later. If
  102. * you find any bugs or strange behavior, please report them, and we'll
  103. * try to straighten them out.
  104. */
  105. int tor_run_main(const tor_main_configuration_t *);
  106. /**
  107. * Run the tor process, as if from the command line.
  108. *
  109. * @deprecated Using this function from outside Tor is deprecated; you should
  110. * use tor_run_main() instead.
  111. *
  112. * BUGS: This function has all the same bugs as tor_run_main().
  113. *
  114. * LIMITATIONS: This function has all the limitations of tor_run_main().
  115. */
  116. int tor_main(int argc, char **argv);
  117. #endif /* !defined(TOR_API_H) */