routermode.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "core/or/or.h"
  7. #include "app/config/config.h"
  8. #include "core/mainloop/connection.h"
  9. #include "core/or/port_cfg_st.h"
  10. #include "feature/relay/router.h"
  11. #include "feature/relay/routermode.h"
  12. /** Return 1 if we are configured to accept either relay or directory requests
  13. * from clients and we aren't at risk of exceeding our bandwidth limits, thus
  14. * we should be a directory server. If not, return 0.
  15. */
  16. int
  17. dir_server_mode(const or_options_t *options)
  18. {
  19. if (!options->DirCache)
  20. return 0;
  21. return options->DirPort_set ||
  22. (server_mode(options) && router_has_bandwidth_to_be_dirserver(options));
  23. }
  24. /** Return true iff we are trying to proxy client connections. */
  25. int
  26. proxy_mode(const or_options_t *options)
  27. {
  28. (void)options;
  29. SMARTLIST_FOREACH_BEGIN(get_configured_ports(), const port_cfg_t *, p) {
  30. if (p->type == CONN_TYPE_AP_LISTENER ||
  31. p->type == CONN_TYPE_AP_TRANS_LISTENER ||
  32. p->type == CONN_TYPE_AP_DNS_LISTENER ||
  33. p->type == CONN_TYPE_AP_NATD_LISTENER)
  34. return 1;
  35. } SMARTLIST_FOREACH_END(p);
  36. return 0;
  37. }
  38. /** Return true iff we are trying to be a server.
  39. */
  40. MOCK_IMPL(int,
  41. server_mode,(const or_options_t *options))
  42. {
  43. if (options->ClientOnly) return 0;
  44. return (options->ORPort_set);
  45. }
  46. /** Return true iff we are trying to be a non-bridge server.
  47. */
  48. MOCK_IMPL(int,
  49. public_server_mode,(const or_options_t *options))
  50. {
  51. if (!server_mode(options)) return 0;
  52. return (!options->BridgeRelay);
  53. }
  54. /** Remember if we've advertised ourselves to the dirservers. */
  55. static int server_is_advertised=0;
  56. /** Return true iff we have published our descriptor lately.
  57. */
  58. MOCK_IMPL(int,
  59. advertised_server_mode,(void))
  60. {
  61. return server_is_advertised;
  62. }
  63. /**
  64. * Called with a boolean: set whether we have recently published our
  65. * descriptor.
  66. */
  67. void
  68. set_server_advertised(int s)
  69. {
  70. server_is_advertised = s;
  71. }