Back to index

tor  0.2.3.18-rc
Defines | Functions | Variables
transports.c File Reference

Pluggable Transports related code. More...

#include "or.h"
#include "config.h"
#include "circuitbuild.h"
#include "transports.h"
#include "util.h"
#include "router.h"

Go to the source code of this file.

Defines

#define PT_PRIVATE
#define parse_server_method_error(l)   parse_method_error(l, 1)
#define parse_client_method_error(l)   parse_method_error(l, 0)
#define PROTO_ENV_ERROR   "ENV-ERROR"
 Managed proxy protocol strings.
#define PROTO_NEG_SUCCESS   "VERSION"
#define PROTO_NEG_FAIL   "VERSION-ERROR no-version"
#define PROTO_CMETHOD   "CMETHOD"
#define PROTO_SMETHOD   "SMETHOD"
#define PROTO_CMETHOD_ERROR   "CMETHOD-ERROR"
#define PROTO_SMETHOD_ERROR   "SMETHOD-ERROR"
#define PROTO_CMETHODS_DONE   "CMETHODS DONE"
#define PROTO_SMETHODS_DONE   "SMETHODS DONE"
#define ENVIRON_SIZE_CLIENT   3
 Number of environment variables for managed proxy clients/servers.
#define ENVIRON_SIZE_SERVER   7 /* XXX known to be too high, but that's ok */
#define PROTO_VERSION_ONE   1
 The first and only supported - at the moment - configuration protocol version.

Functions

static process_environment_tcreate_managed_proxy_environment (const managed_proxy_t *mp)
 Return a newly allocated process_environment_t * for mp's process.
static INLINE int proxy_configuration_finished (const managed_proxy_t *mp)
 Return true if the configuration of the managed proxy mp is finished.
static void managed_proxy_destroy (managed_proxy_t *mp, int also_terminate_process)
 Free memory allocated by managed proxy mp.
static void handle_finished_proxy (managed_proxy_t *mp)
 Handle a configured or broken managed proxy mp.
static void configure_proxy (managed_proxy_t *mp)
 Attempt to continue configuring managed proxy mp.
static void parse_method_error (const char *line, int is_server)
 Parses {C,S}METHOD-ERROR line and warns the user accordingly.
static INLINE void free_execve_args (char **arg)
 Frees the array of pointers in arg used as arguments to execve(2).
int pt_proxies_configuration_pending (void)
 Return true if there are still unconfigured managed proxies, or proxies that need restarting.
static void assert_unconfigured_count_ok (void)
 Assert that the unconfigured_proxies_n value correctly matches the number of proxies in a state other than PT_PROTO_COMPLETE.
static int managed_proxy_has_argv (const managed_proxy_t *mp, char **proxy_argv)
 Return true if mp has the same argv as proxy_argv
static managed_proxy_t * get_managed_proxy_by_argv_and_type (char **proxy_argv, int is_server)
 Return a managed proxy with the same argv as proxy_argv.
static void add_transport_to_proxy (const char *transport, managed_proxy_t *mp)
 Add transport to managed proxy mp.
static int proxy_needs_restart (const managed_proxy_t *mp)
 Called when a SIGHUP occurs.
static void proxy_prepare_for_restart (managed_proxy_t *mp)
 Managed proxy mp must be restarted.
static int launch_managed_proxy (managed_proxy_t *mp)
 Launch managed proxy mp.
void pt_configure_remaining_proxies (void)
 Check if any of the managed proxies we are currently trying to configure have anything new to say.
static void register_server_proxy (managed_proxy_t *mp)
 Register server managed proxy mp transports to state.
static void register_client_proxy (managed_proxy_t *mp)
 Register all the transports supported by client managed proxy mp to the bridge subsystem.
static INLINE void register_proxy (managed_proxy_t *mp)
 Register the transports of managed proxy mp.
static void handle_methods_done (const managed_proxy_t *mp)
 This function is called when a proxy sends an {S,C}METHODS DONE message.
void handle_proxy_line (const char *line, managed_proxy_t *mp)
 Handle a configuration protocol line received from a managed proxy mp.
void parse_env_error (const char *line)
 Parses an ENV-ERROR line and warns the user accordingly.
int parse_version (const char *line, managed_proxy_t *mp)
 Handles a VERSION line.
int parse_smethod_line (const char *line, managed_proxy_t *mp)
 Parses an SMETHOD line and if well-formed it registers the new transport in mp.
int parse_cmethod_line (const char *line, managed_proxy_t *mp)
 Parses a CMETHOD line, and if well-formed it registers the new transport in mp.
static char * get_bindaddr_for_server_proxy (const managed_proxy_t *mp)
 Return the string that tor should place in TOR_PT_SERVER_BINDADDR while configuring the server managed proxy in mp.
static managed_proxy_t * managed_proxy_create (const smartlist_t *transport_list, char **proxy_argv, int is_server)
 Create and return a new managed proxy for transport using proxy_argv.
void pt_kickstart_proxy (const smartlist_t *transport_list, char **proxy_argv, int is_server)
 Register proxy with proxy_argv, supporting transports in transport_list, to the managed proxy subsystem.
void pt_prepare_proxy_list_for_config_read (void)
 Tor will read its config.
void sweep_proxy_list (void)
 The tor config was read.
void pt_free_all (void)
 Release all storage held by the pluggable transports subsystem.

Variables

static smartlist_tmanaged_proxy_list = NULL
 List of unconfigured managed proxies.
static int unconfigured_proxies_n = 0
 Number of still unconfigured proxies.
static int check_if_restarts_needed = 0
 Boolean: True iff we might need to restart some proxies.

Detailed Description

Pluggable Transports related code.

Each managed proxy is represented by a managed_proxy_t. Each managed proxy can support multiple transports. Each managed proxy gets configured through a multistep process.

managed_proxy_list contains all the managed proxies this tor instance is supporting. In the managed_proxy_list there are unconfigured_proxies_n managed proxies that are still unconfigured.

In every run_scheduled_event() tick, we attempt to launch and then configure the unconfiged managed proxies, using the configuration protocol defined in the 180_pluggable_transport.txt proposal. A managed proxy might need several ticks to get fully configured.

When a managed proxy is fully configured, we register all its transports to the circuitbuild.c subsystem. At that point the transports are owned by the circuitbuild.c subsystem.

When a managed proxy fails to follow the 180 configuration protocol, it gets marked as broken and gets destroyed.

In a little more detail:

While we are serially parsing torrc, we store all the transports that a proxy should spawn in its transports_to_launch element.

When we finish reading the torrc, we spawn the managed proxy and expect {S,C}METHOD lines from its output. We add transports described by METHOD lines to its transports element, as transport_t structs.

When the managed proxy stops spitting METHOD lines (signified by a '{S,C}METHODS DONE' message) we register all the transports collected to the circuitbuild.c subsystem. At this point, the pointers to transport_t can be transformed into dangling pointers at any point by the circuitbuild.c subsystem, and so we replace all transport_t pointers with strings describing the transport names. We can still go from a transport name to a transport_t using the fact that each transport name uniquely identifies a transport_t.

In even more detail, this is what happens when a SIGHUP occurs:

We immediately destroy all unconfigured proxies (We shouldn't have unconfigured proxies in the first place, except when SIGHUP rings immediately after tor is launched.).

We mark all managed proxies and transports to signify that they must be removed if they don't contribute by the new torrc (we mark using the marked_for_removal element). We also mark all managed proxies to signify that they might need to be restarted so that they end up supporting all the transports the new torrc wants them to support (using the got_hup element). We also clear their transports_to_launch list so that we can put there the transports we need to launch according to the new torrc.

We then start parsing torrc again.

Everytime we encounter a transport line using a known pre-SIGHUP managed proxy, we cleanse that proxy from the removal mark. We also mark it as unconfigured so that on the next scheduled events tick, we investigate whether we need to restart the proxy so that it also spawns the new transports. If the post-SIGHUP transports_to_launch list is identical to the pre-SIGHUP one, it means that no changes were introduced to this proxy during the SIGHUP and no restart has to take place.

During the post-SIGHUP torrc parsing, we unmark all transports spawned by managed proxies that we find in our torrc. We do that so that if we don't need to restart a managed proxy, we can continue using its old transports normally. If we end up restarting the proxy, we destroy and unregister all old transports from the circuitbuild.c subsystem.

Definition in file transports.c.


Define Documentation

#define ENVIRON_SIZE_CLIENT   3

Number of environment variables for managed proxy clients/servers.

Definition at line 123 of file transports.c.

#define ENVIRON_SIZE_SERVER   7 /* XXX known to be too high, but that's ok */

Definition at line 124 of file transports.c.

#define parse_client_method_error (   l)    parse_method_error(l, 0)

Definition at line 107 of file transports.c.

#define parse_server_method_error (   l)    parse_method_error(l, 1)

Definition at line 106 of file transports.c.

#define PROTO_CMETHOD   "CMETHOD"

Definition at line 115 of file transports.c.

#define PROTO_CMETHOD_ERROR   "CMETHOD-ERROR"

Definition at line 117 of file transports.c.

#define PROTO_CMETHODS_DONE   "CMETHODS DONE"

Definition at line 119 of file transports.c.

#define PROTO_ENV_ERROR   "ENV-ERROR"

Managed proxy protocol strings.

Definition at line 112 of file transports.c.

#define PROTO_NEG_FAIL   "VERSION-ERROR no-version"

Definition at line 114 of file transports.c.

#define PROTO_NEG_SUCCESS   "VERSION"

Definition at line 113 of file transports.c.

#define PROTO_SMETHOD   "SMETHOD"

Definition at line 116 of file transports.c.

#define PROTO_SMETHOD_ERROR   "SMETHOD-ERROR"

Definition at line 118 of file transports.c.

#define PROTO_SMETHODS_DONE   "SMETHODS DONE"

Definition at line 120 of file transports.c.

#define PROTO_VERSION_ONE   1

The first and only supported - at the moment - configuration protocol version.

Definition at line 128 of file transports.c.

#define PT_PRIVATE

Definition at line 86 of file transports.c.


Function Documentation

static void add_transport_to_proxy ( const char *  transport,
managed_proxy_t *  mp 
) [static]

Add transport to managed proxy mp.

Definition at line 205 of file transports.c.

{
  tor_assert(mp->transports_to_launch);
  if (!smartlist_string_isin(mp->transports_to_launch, transport))
    smartlist_add(mp->transports_to_launch, tor_strdup(transport));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void assert_unconfigured_count_ok ( void  ) [static]

Assert that the unconfigured_proxies_n value correctly matches the number of proxies in a state other than PT_PROTO_COMPLETE.

Definition at line 148 of file transports.c.

{
  int n_completed = 0;
  if (!managed_proxy_list) {
    tor_assert(unconfigured_proxies_n == 0);
    return;
  }

  SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp, {
      if (mp->conf_state == PT_PROTO_COMPLETED)
        ++n_completed;
  });

  tor_assert(n_completed + unconfigured_proxies_n ==
             smartlist_len(managed_proxy_list));
}

Here is the caller graph for this function:

static void configure_proxy ( managed_proxy_t *  mp) [static]

Attempt to continue configuring managed proxy mp.

Definition at line 424 of file transports.c.

{
  enum stream_status r;
  char stdout_buf[200];

  /* if we haven't launched the proxy yet, do it now */
  if (mp->conf_state == PT_PROTO_INFANT) {
    if (launch_managed_proxy(mp) < 0) { /* launch fail */
      mp->conf_state = PT_PROTO_FAILED_LAUNCH;
      handle_finished_proxy(mp);
    }
    return;
  }

  tor_assert(mp->conf_state != PT_PROTO_INFANT);
  tor_assert(mp->process_handle);

  while (1) {
    r = get_string_from_pipe(tor_process_get_stdout_pipe(mp->process_handle),
                             stdout_buf, sizeof(stdout_buf) - 1);

    if (r  == IO_STREAM_OKAY) { /* got a line; handle it! */
      handle_proxy_line((const char *)stdout_buf, mp);
    } else if (r == IO_STREAM_EAGAIN) { /* check back later */
      return;
    } else if (r == IO_STREAM_CLOSED || r == IO_STREAM_TERM) { /* snap! */
      log_warn(LD_GENERAL, "Our communication channel with the managed proxy "
               "'%s' closed. Most probably application stopped running.",
               mp->argv[0]);
      mp->conf_state = PT_PROTO_BROKEN;
    } else { /* unknown stream status */
      log_warn(LD_BUG, "Unknown stream status '%d' while configuring managed "
               "proxy '%s'.", (int)r, mp->argv[0]);
    }

    /* if the proxy finished configuring, exit the loop. */
    if (proxy_configuration_finished(mp)) {
      handle_finished_proxy(mp);
      return;
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static process_environment_t * create_managed_proxy_environment ( const managed_proxy_t *  mp) [static]

Return a newly allocated process_environment_t * for mp's process.

Definition at line 982 of file transports.c.

{
  /* Environment variables to be added to or set in mp's environment. */
  smartlist_t *envs = smartlist_new();
  /* XXXX The next time someone touches this code, shorten the name of
   * set_environment_variable_in_smartlist, add a
   * set_env_var_in_smartlist_asprintf function, and get rid of the
   * silly extra envs smartlist. */

  /* The final environment to be passed to mp. */
  smartlist_t *merged_env_vars = get_current_process_environment_variables();

  process_environment_t *env;

  {
    char *state_tmp = get_datadir_fname("pt_state/"); /* XXX temp */
    smartlist_add_asprintf(envs, "TOR_PT_STATE_LOCATION=%s", state_tmp);
    tor_free(state_tmp);
  }

  smartlist_add(envs, tor_strdup("TOR_PT_MANAGED_TRANSPORT_VER=1"));

  {
    char *transports_to_launch =
      smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);

    smartlist_add_asprintf(envs,
                           mp->is_server ?
                           "TOR_PT_SERVER_TRANSPORTS=%s" :
                           "TOR_PT_CLIENT_TRANSPORTS=%s",
                           transports_to_launch);

    tor_free(transports_to_launch);
  }

  if (mp->is_server) {
    {
      char *orport_tmp =
        get_first_listener_addrport_string(CONN_TYPE_OR_LISTENER);
      smartlist_add_asprintf(envs, "TOR_PT_ORPORT=%s", orport_tmp);
      tor_free(orport_tmp);
    }

    {
      char *bindaddr_tmp = get_bindaddr_for_server_proxy(mp);
      smartlist_add_asprintf(envs, "TOR_PT_SERVER_BINDADDR=%s", bindaddr_tmp);
      tor_free(bindaddr_tmp);
    }

    /* XXX024 Remove the '=' here once versions of obfsproxy which
     * assert that this env var exists are sufficiently dead.
     *
     * (If we remove this line entirely, some joker will stick this
     * variable in Tor's environment and crash PTs that try to parse
     * it even when not run in server mode.) */
    smartlist_add(envs, tor_strdup("TOR_PT_EXTENDED_SERVER_PORT="));
  }

  SMARTLIST_FOREACH_BEGIN(envs, const char *, env_var) {
    set_environment_variable_in_smartlist(merged_env_vars, env_var,
                                          _tor_free, 1);
  } SMARTLIST_FOREACH_END(env_var);

  env = process_environment_make(merged_env_vars);

  smartlist_free(envs);

  SMARTLIST_FOREACH(merged_env_vars, void *, x, tor_free(x));
  smartlist_free(merged_env_vars);

  return env;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static INLINE void free_execve_args ( char **  arg) [static]

Frees the array of pointers in arg used as arguments to execve(2).

Definition at line 1141 of file transports.c.

{
  char **tmp = arg;
  while (*tmp) /* use the fact that the last element of the array is a
                  NULL pointer to know when to stop freeing */
    _tor_free(*tmp++);

  tor_free(arg);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static char* get_bindaddr_for_server_proxy ( const managed_proxy_t *  mp) [static]

Return the string that tor should place in TOR_PT_SERVER_BINDADDR while configuring the server managed proxy in mp.

The string is stored in the heap, and it's the the responsibility of the caller to deallocate it after its use.

Definition at line 955 of file transports.c.

{
  char *bindaddr_result = NULL;
  char *bindaddr_tmp = NULL;
  smartlist_t *string_tmp = smartlist_new();

  tor_assert(mp->is_server);

  SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
    bindaddr_tmp = get_stored_bindaddr_for_server_transport(t);

    smartlist_add_asprintf(string_tmp, "%s-%s", t, bindaddr_tmp);

    tor_free(bindaddr_tmp);
  } SMARTLIST_FOREACH_END(t);

  bindaddr_result = smartlist_join_strings(string_tmp, ",", 0, NULL);

  SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
  smartlist_free(string_tmp);

  return bindaddr_result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static managed_proxy_t* get_managed_proxy_by_argv_and_type ( char **  proxy_argv,
int  is_server 
) [static]

Return a managed proxy with the same argv as proxy_argv.

If no such managed proxy exists, return NULL.

Definition at line 189 of file transports.c.

{
  if (!managed_proxy_list)
    return NULL;

  SMARTLIST_FOREACH_BEGIN(managed_proxy_list,  managed_proxy_t *, mp) {
    if (managed_proxy_has_argv(mp, proxy_argv) &&
        mp->is_server == is_server)
      return mp;
  } SMARTLIST_FOREACH_END(mp);

  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void handle_finished_proxy ( managed_proxy_t *  mp) [static]

Handle a configured or broken managed proxy mp.

Definition at line 571 of file transports.c.

{
  switch (mp->conf_state) {
  case PT_PROTO_BROKEN: /* if broken: */
    managed_proxy_destroy(mp, 1); /* annihilate it. */
    break;
  case PT_PROTO_FAILED_LAUNCH: /* if it failed before launching: */
    managed_proxy_destroy(mp, 0); /* destroy it but don't terminate */
    break;
  case PT_PROTO_CONFIGURED: /* if configured correctly: */
    register_proxy(mp); /* register its transports */
    mp->conf_state = PT_PROTO_COMPLETED; /* and mark it as completed. */
    break;
  case PT_PROTO_INFANT:
  case PT_PROTO_LAUNCHED:
  case PT_PROTO_ACCEPTING_METHODS:
  case PT_PROTO_COMPLETED:
  default:
    log_warn(LD_CONFIG, "Unexpected state '%d' of managed proxy '%s'.",
             (int)mp->conf_state, mp->argv[0]);
    tor_assert(0);
  }

  unconfigured_proxies_n--;
  tor_assert(unconfigured_proxies_n >= 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void handle_methods_done ( const managed_proxy_t *  mp) [static]

This function is called when a proxy sends an {S,C}METHODS DONE message.

Definition at line 610 of file transports.c.

{
  tor_assert(mp->transports);

  if (smartlist_len(mp->transports) == 0)
    log_notice(LD_GENERAL, "Managed proxy '%s' was spawned successfully, "
               "but it didn't launch any pluggable transport listeners!",
               mp->argv[0]);

  log_info(LD_CONFIG, "%s managed proxy '%s' configuration completed!",
           mp->is_server ? "Server" : "Client",
           mp->argv[0]);
}

Here is the caller graph for this function:

void handle_proxy_line ( const char *  line,
managed_proxy_t *  mp 
)

Handle a configuration protocol line received from a managed proxy mp.

Definition at line 627 of file transports.c.

{
  log_info(LD_GENERAL, "Got a line from managed proxy '%s': (%s)",
           mp->argv[0], line);

  if (!strcmpstart(line, PROTO_ENV_ERROR)) {
    if (mp->conf_state != PT_PROTO_LAUNCHED)
      goto err;

    parse_env_error(line);
    goto err;
  } else if (!strcmpstart(line, PROTO_NEG_FAIL)) {
    if (mp->conf_state != PT_PROTO_LAUNCHED)
      goto err;

    log_warn(LD_CONFIG, "Managed proxy could not pick a "
             "configuration protocol version.");
    goto err;
  } else if (!strcmpstart(line, PROTO_NEG_SUCCESS)) {
    if (mp->conf_state != PT_PROTO_LAUNCHED)
      goto err;

    if (parse_version(line,mp) < 0)
      goto err;

    tor_assert(mp->conf_protocol != 0);
    mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
    return;
  } else if (!strcmpstart(line, PROTO_CMETHODS_DONE)) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    handle_methods_done(mp);

    mp->conf_state = PT_PROTO_CONFIGURED;
    return;
  } else if (!strcmpstart(line, PROTO_SMETHODS_DONE)) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    handle_methods_done(mp);

    mp->conf_state = PT_PROTO_CONFIGURED;
    return;
  } else if (!strcmpstart(line, PROTO_CMETHOD_ERROR)) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    parse_client_method_error(line);
    goto err;
  } else if (!strcmpstart(line, PROTO_SMETHOD_ERROR)) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    parse_server_method_error(line);
    goto err;
  } else if (!strcmpstart(line, PROTO_CMETHOD)) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    if (parse_cmethod_line(line, mp) < 0)
      goto err;

    return;
  } else if (!strcmpstart(line, PROTO_SMETHOD)) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    if (parse_smethod_line(line, mp) < 0)
      goto err;

    return;
  } else if (!strcmpstart(line, SPAWN_ERROR_MESSAGE)) {
    /* managed proxy launch failed: parse error message to learn why. */
    int retval, child_state, saved_errno;
    retval = tor_sscanf(line, SPAWN_ERROR_MESSAGE "%x/%x",
                        &child_state, &saved_errno);
    if (retval == 2) {
      log_warn(LD_GENERAL,
               "Could not launch managed proxy executable at '%s' ('%s').",
               mp->argv[0], strerror(saved_errno));
    } else { /* failed to parse error message */
      log_warn(LD_GENERAL,"Could not launch managed proxy executable at '%s'.",
               mp->argv[0]);
    }

    mp->conf_state = PT_PROTO_FAILED_LAUNCH;
    return;
  }

  log_notice(LD_GENERAL, "Unknown line received by managed proxy (%s).", line);
  return;

 err:
  mp->conf_state = PT_PROTO_BROKEN;
  log_warn(LD_CONFIG, "Managed proxy at '%s' failed the configuration protocol"
           " and will be destroyed.", mp->argv[0]);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int launch_managed_proxy ( managed_proxy_t *  mp) [static]

Launch managed proxy mp.

Definition at line 277 of file transports.c.

{
  int retval;

  process_environment_t *env = create_managed_proxy_environment(mp);

#ifdef _WIN32
  /* Passing NULL as lpApplicationName makes Windows search for the .exe */
  retval = tor_spawn_background(NULL,
                                (const char **)mp->argv,
                                env,
                                &mp->process_handle);
#else
  retval = tor_spawn_background(mp->argv[0],
                                (const char **)mp->argv,
                                env,
                                &mp->process_handle);
#endif

  process_environment_free(env);

  if (retval == PROCESS_STATUS_ERROR) {
    log_warn(LD_GENERAL, "Managed proxy at '%s' failed at launch.",
             mp->argv[0]);
    return -1;
  }

  log_info(LD_CONFIG, "Managed proxy at '%s' has spawned with PID '%d'.",
           mp->argv[0], tor_process_get_pid(mp->process_handle));

  mp->conf_state = PT_PROTO_LAUNCHED;

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static managed_proxy_t* managed_proxy_create ( const smartlist_t transport_list,
char **  proxy_argv,
int  is_server 
) [static]

Create and return a new managed proxy for transport using proxy_argv.

Also, add it to the global managed proxy list. If is_server is true, it's a server managed proxy. Takes ownership of proxy_argv.

Requires that proxy_argv have at least one element.

Definition at line 1062 of file transports.c.

{
  managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  mp->conf_state = PT_PROTO_INFANT;
  mp->is_server = is_server;
  mp->argv = proxy_argv;
  mp->transports = smartlist_new();

  mp->transports_to_launch = smartlist_new();
  SMARTLIST_FOREACH(transport_list, const char *, transport,
                    add_transport_to_proxy(transport, mp));

  /* register the managed proxy */
  if (!managed_proxy_list)
    managed_proxy_list = smartlist_new();
  smartlist_add(managed_proxy_list, mp);
  unconfigured_proxies_n++;

  assert_unconfigured_count_ok();

  return mp;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void managed_proxy_destroy ( managed_proxy_t *  mp,
int  also_terminate_process 
) [static]

Free memory allocated by managed proxy mp.

Definition at line 542 of file transports.c.

{
  if (mp->conf_state != PT_PROTO_COMPLETED)
    SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  else
    SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));

  /* free the transports smartlist */
  smartlist_free(mp->transports);

  /* free the transports_to_launch smartlist */
  SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  smartlist_free(mp->transports_to_launch);

  /* remove it from the list of managed proxies */
  smartlist_remove(managed_proxy_list, mp);

  /* free the argv */
  free_execve_args(mp->argv);

  tor_process_handle_destroy(mp->process_handle, also_terminate_process);
  mp->process_handle = NULL;

  tor_free(mp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int managed_proxy_has_argv ( const managed_proxy_t *  mp,
char **  proxy_argv 
) [static]

Return true if mp has the same argv as proxy_argv

Definition at line 167 of file transports.c.

{
  char **tmp1=proxy_argv;
  char **tmp2=mp->argv;

  tor_assert(tmp1);
  tor_assert(tmp2);

  while (*tmp1 && *tmp2) {
    if (strcmp(*tmp1++, *tmp2++))
      return 0;
  }

  if (!*tmp1 && !*tmp2)
    return 1;

  return 0;
}

Here is the caller graph for this function:

int parse_cmethod_line ( const char *  line,
managed_proxy_t *  mp 
)

Parses a CMETHOD line, and if well-formed it registers the new transport in mp.

Definition at line 862 of file transports.c.

{
  int r;
  smartlist_t *items = NULL;

  char *method_name=NULL;

  char *socks_ver_str=NULL;
  int socks_ver=PROXY_NONE;

  char *addrport=NULL;
  tor_addr_t tor_addr;
  char *address=NULL;
  uint16_t port = 0;

  transport_t *transport=NULL;

  items = smartlist_new();
  smartlist_split_string(items, line, NULL,
                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  if (smartlist_len(items) < 4) {
    log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
             "with too few arguments.");
    goto err;
  }

  tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));

  method_name = smartlist_get(items,1);
  if (!string_is_C_identifier(method_name)) {
    log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
             method_name);
    goto err;
  }

  socks_ver_str = smartlist_get(items,2);

  if (!strcmp(socks_ver_str,"socks4")) {
    socks_ver = PROXY_SOCKS4;
  } else if (!strcmp(socks_ver_str,"socks5")) {
    socks_ver = PROXY_SOCKS5;
  } else {
    log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
             "we don't recognize. (%s)", socks_ver_str);
    goto err;
  }

  addrport = smartlist_get(items, 3);
  if (tor_addr_port_split(LOG_PROTOCOL_WARN, addrport, &address, &port)<0) {
    log_warn(LD_CONFIG, "Error parsing transport "
             "address '%s'", addrport);
    goto err;
  }

  if (!port) {
    log_warn(LD_CONFIG,
             "Transport address '%s' has no port.", addrport);
    goto err;
  }

  if (tor_addr_parse(&tor_addr, address) < 0) {
    log_warn(LD_CONFIG, "Error parsing transport address '%s'", address);
    goto err;
  }

  transport = transport_new(&tor_addr, port, method_name, socks_ver);
  if (!transport)
    goto err;

  smartlist_add(mp->transports, transport);

  log_info(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
           "Attached to managed proxy.",
           method_name, address, (int)port, socks_ver);

  r=0;
  goto done;

 err:
  r = -1;

 done:
  SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  smartlist_free(items);
  tor_free(address);
  return r;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void parse_env_error ( const char *  line)

Parses an ENV-ERROR line and warns the user accordingly.

Definition at line 728 of file transports.c.

{
  /* (Length of the protocol string) plus (a space) and (the first char of
     the error message) */
  if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
    log_notice(LD_CONFIG, "Managed proxy sent us an %s without an error "
               "message.", PROTO_ENV_ERROR);

  log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
           "pluggable transport environment variables. (%s)",
           line+strlen(PROTO_ENV_ERROR)+1);
}

Here is the caller graph for this function:

static void parse_method_error ( const char *  line,
int  is_server 
) [static]

Parses {C,S}METHOD-ERROR line and warns the user accordingly.

If is_server it is an SMETHOD-ERROR, otherwise it is a CMETHOD-ERROR.

Definition at line 766 of file transports.c.

{
  const char* error = is_server ?
    PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;

  /* (Length of the protocol string) plus (a space) and (the first char of
     the error message) */
  if (strlen(line) < (strlen(error) + 2))
    log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
             "message.", error);

  log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
           is_server ? "Server" : "Client",
           line+strlen(error)+1);
}
int parse_smethod_line ( const char *  line,
managed_proxy_t *  mp 
)

Parses an SMETHOD line and if well-formed it registers the new transport in mp.

Definition at line 785 of file transports.c.

{
  int r;
  smartlist_t *items = NULL;

  char *method_name=NULL;

  char *addrport=NULL;
  tor_addr_t tor_addr;
  char *address=NULL;
  uint16_t port = 0;

  transport_t *transport=NULL;

  items = smartlist_new();
  smartlist_split_string(items, line, NULL,
                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  if (smartlist_len(items) < 3) {
    log_warn(LD_CONFIG, "Server managed proxy sent us a SMETHOD line "
             "with too few arguments.");
    goto err;
  }

  tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));

  method_name = smartlist_get(items,1);
  if (!string_is_C_identifier(method_name)) {
    log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
             method_name);
    goto err;
  }

  addrport = smartlist_get(items, 2);
  if (tor_addr_port_split(LOG_PROTOCOL_WARN, addrport, &address, &port)<0) {
    log_warn(LD_CONFIG, "Error parsing transport "
             "address '%s'", addrport);
    goto err;
  }

  if (!port) {
    log_warn(LD_CONFIG,
             "Transport address '%s' has no port.", addrport);
    goto err;
  }

  if (tor_addr_parse(&tor_addr, address) < 0) {
    log_warn(LD_CONFIG, "Error parsing transport address '%s'", address);
    goto err;
  }

  transport = transport_new(&tor_addr, port, method_name, PROXY_NONE);
  if (!transport)
    goto err;

  smartlist_add(mp->transports, transport);

  /* For now, notify the user so that he knows where the server
     transport is listening. */
  log_info(LD_CONFIG, "Server transport %s at %s:%d.",
           method_name, address, (int)port);

  r=0;
  goto done;

 err:
  r = -1;

 done:
  SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  smartlist_free(items);
  tor_free(address);
  return r;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int parse_version ( const char *  line,
managed_proxy_t *  mp 
)

Handles a VERSION line.

Updates the configuration protocol version in mp.

Definition at line 744 of file transports.c.

{
  if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
    log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
             PROTO_NEG_SUCCESS);
    return -1;
  }

  if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) { /* hardcoded temp */
    log_warn(LD_CONFIG, "Managed proxy tried to negotiate on version '%s'. "
             "We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
    return -1;
  }

  mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
  return 0;
}

Here is the caller graph for this function:

static INLINE int proxy_configuration_finished ( const managed_proxy_t *  mp) [static]

Return true if the configuration of the managed proxy mp is finished.

Definition at line 601 of file transports.c.

{
  return (mp->conf_state == PT_PROTO_CONFIGURED ||
          mp->conf_state == PT_PROTO_BROKEN ||
          mp->conf_state == PT_PROTO_FAILED_LAUNCH);
}

Here is the caller graph for this function:

static int proxy_needs_restart ( const managed_proxy_t *  mp) [static]

Called when a SIGHUP occurs.

Returns true if managed proxy mp needs to be restarted after the SIGHUP, based on the new torrc.

Definition at line 216 of file transports.c.

{
  /* mp->transport_to_launch is populated with the names of the
     transports that must be launched *after* the SIGHUP.
     mp->transports is populated with the names of the transports that
     were launched *before* the SIGHUP.

     If the two lists contain the same strings, we don't need to
     restart the proxy, since it already does what we want. */

  tor_assert(smartlist_len(mp->transports_to_launch) > 0);
  tor_assert(mp->conf_state == PT_PROTO_COMPLETED);

  if (smartlist_len(mp->transports_to_launch) != smartlist_len(mp->transports))
    goto needs_restart;

  SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t_t_l) {
    if (!smartlist_string_isin(mp->transports, t_t_l))
      goto needs_restart;

  } SMARTLIST_FOREACH_END(t_t_l);

  return 0;

 needs_restart:
  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void proxy_prepare_for_restart ( managed_proxy_t *  mp) [static]

Managed proxy mp must be restarted.

Do all the necessary preparations and then flag its state so that it will be relaunched in the next tick.

Definition at line 248 of file transports.c.

{
  transport_t *t_tmp = NULL;

  tor_assert(mp->conf_state == PT_PROTO_COMPLETED);

  /* destroy the process handle and terminate the process. */
  tor_process_handle_destroy(mp->process_handle, 1);
  mp->process_handle = NULL;

  /* destroy all its old transports. we no longer use them. */
  SMARTLIST_FOREACH_BEGIN(mp->transports, const char *, t_name) {
    t_tmp = transport_get_by_name(t_name);
    if (t_tmp)
      t_tmp->marked_for_removal = 1;
  } SMARTLIST_FOREACH_END(t_name);
  sweep_transport_list();

  /* free the transport names in mp->transports */
  SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
  smartlist_clear(mp->transports);

  /* flag it as an infant proxy so that it gets launched on next tick */
  mp->conf_state = PT_PROTO_INFANT;
  unconfigured_proxies_n++;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Check if any of the managed proxies we are currently trying to configure have anything new to say.

This is called from run_scheduled_events().

Definition at line 316 of file transports.c.

{
  smartlist_t *tmp = smartlist_new();

  log_debug(LD_CONFIG, "Configuring remaining managed proxies (%d)!",
            unconfigured_proxies_n);

  /* Iterate over tmp, not managed_proxy_list, since configure_proxy can
   * remove elements from managed_proxy_list. */
  smartlist_add_all(tmp, managed_proxy_list);

  assert_unconfigured_count_ok();

  SMARTLIST_FOREACH_BEGIN(tmp,  managed_proxy_t *, mp) {
    tor_assert(mp->conf_state != PT_PROTO_BROKEN ||
               mp->conf_state != PT_PROTO_FAILED_LAUNCH);

    if (mp->got_hup) {
      mp->got_hup = 0;

      /* This proxy is marked by a SIGHUP. Check whether we need to
         restart it. */
      if (proxy_needs_restart(mp)) {
        log_info(LD_GENERAL, "Preparing managed proxy '%s' for restart.",
                 mp->argv[0]);
        proxy_prepare_for_restart(mp);
      } else { /* it doesn't need to be restarted. */
        log_info(LD_GENERAL, "Nothing changed for managed proxy '%s' after "
                 "HUP: not restarting.", mp->argv[0]);
      }

      continue;
    }

    /* If the proxy is not fully configured, try to configure it
       futher. */
    if (!proxy_configuration_finished(mp))
      configure_proxy(mp);

  } SMARTLIST_FOREACH_END(mp);

  smartlist_free(tmp);
  check_if_restarts_needed = 0;
  assert_unconfigured_count_ok();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void pt_free_all ( void  )

Release all storage held by the pluggable transports subsystem.

Definition at line 1205 of file transports.c.

{
  if (managed_proxy_list) {
    /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
       transports and it's the duty of the circuitbuild.c subsystem to
       free them. Otherwise, it hasn't registered its transports yet
       and we should free them here. */
    SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp, {
        SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
        managed_proxy_destroy(mp, 1);
    });

    smartlist_free(managed_proxy_list);
    managed_proxy_list=NULL;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void pt_kickstart_proxy ( const smartlist_t transport_list,
char **  proxy_argv,
int  is_server 
)

Register proxy with proxy_argv, supporting transports in transport_list, to the managed proxy subsystem.

If is_server is true, then the proxy is a server proxy.

Takes ownership of proxy_argv.

Requires that proxy_argv be a NULL-terminated array of command-line elements, containing at least one element.

Definition at line 1096 of file transports.c.

{
  managed_proxy_t *mp=NULL;
  transport_t *old_transport = NULL;

  if (!proxy_argv || !proxy_argv[0]) {
    return;
  }

  mp = get_managed_proxy_by_argv_and_type(proxy_argv, is_server);

  if (!mp) { /* we haven't seen this proxy before */
    managed_proxy_create(transport_list, proxy_argv, is_server);

  } else { /* known proxy. add its transport to its transport list */
    if (mp->got_hup) {
      /* If the managed proxy we found is marked by a SIGHUP, it means
         that it's not useless and should be kept. If it's marked for
         removal, unmark it and increase the unconfigured proxies so
         that we try to restart it if we need to. Afterwards, check if
         a transport_t for 'transport' used to exist before the SIGHUP
         and make sure it doesn't get deleted because we might reuse
         it. */
      if (mp->marked_for_removal) {
        mp->marked_for_removal = 0;
        check_if_restarts_needed = 1;
      }

      SMARTLIST_FOREACH_BEGIN(transport_list, const char *, transport) {
        old_transport = transport_get_by_name(transport);
        if (old_transport)
          old_transport->marked_for_removal = 0;
      } SMARTLIST_FOREACH_END(transport);
    }

    SMARTLIST_FOREACH(transport_list, const char *, transport,
                      add_transport_to_proxy(transport, mp));
    free_execve_args(proxy_argv);
  }
}

Here is the call graph for this function:

Tor will read its config.

Prepare the managed proxy list so that proxies not used in the new config will shutdown, and proxies that need to spawn different transports will do so.

Definition at line 1156 of file transports.c.

{
  if (!managed_proxy_list)
    return;

  assert_unconfigured_count_ok();
  SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
    /* Destroy unconfigured proxies. */
    if (mp->conf_state != PT_PROTO_COMPLETED) {
      SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
      managed_proxy_destroy(mp, 1);
      unconfigured_proxies_n--;
      continue;
    }

    tor_assert(mp->conf_state == PT_PROTO_COMPLETED);

    mp->marked_for_removal = 1;
    mp->got_hup = 1;
    SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
    smartlist_clear(mp->transports_to_launch);
  } SMARTLIST_FOREACH_END(mp);

  assert_unconfigured_count_ok();

  tor_assert(unconfigured_proxies_n == 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Return true if there are still unconfigured managed proxies, or proxies that need restarting.

Definition at line 140 of file transports.c.

Here is the caller graph for this function:

static void register_client_proxy ( managed_proxy_t *  mp) [static]

Register all the transports supported by client managed proxy mp to the bridge subsystem.

Definition at line 498 of file transports.c.

{
  int r;
  /* After we register this proxy's transports, we switch its
     mp->transports to a list containing strings of its transport
     names. (See transports.h) */
  smartlist_t *sm_tmp = smartlist_new();

  tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
  SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
    r = transport_add(t);
    switch (r) {
    case -1:
      log_notice(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
      transport_free(t);
      break;
    case 0:
      log_info(LD_GENERAL, "Succesfully registered transport %s", t->name);
      smartlist_add(sm_tmp, tor_strdup(t->name));
      break;
    case 1:
      log_info(LD_GENERAL, "Succesfully registered transport %s", t->name);
      smartlist_add(sm_tmp, tor_strdup(t->name));
      transport_free(t);
      break;
    }
  } SMARTLIST_FOREACH_END(t);

  smartlist_free(mp->transports);
  mp->transports = sm_tmp;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static INLINE void register_proxy ( managed_proxy_t *  mp) [static]

Register the transports of managed proxy mp.

Definition at line 532 of file transports.c.

{
  if (mp->is_server)
    register_server_proxy(mp);
  else
    register_client_proxy(mp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void register_server_proxy ( managed_proxy_t *  mp) [static]

Register server managed proxy mp transports to state.

Definition at line 471 of file transports.c.

{
  /* After we register this proxy's transports, we switch its
     mp->transports to a list containing strings of its transport
     names. (See transports.h) */
  smartlist_t *sm_tmp = smartlist_new();

  tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
  SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
    save_transport_to_state(t->name, &t->addr, t->port);
    log_notice(LD_GENERAL, "Registered server transport '%s' at '%s:%d'",
               t->name, fmt_addr(&t->addr), (int)t->port);
    smartlist_add(sm_tmp, tor_strdup(t->name));
  } SMARTLIST_FOREACH_END(t);

  /* Since server proxies don't register their transports in the
     circuitbuild.c subsystem, it's our duty to free them when we
     switch mp->transports to strings. */
  SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  smartlist_free(mp->transports);

  mp->transports = sm_tmp;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sweep_proxy_list ( void  )

The tor config was read.

Destroy all managed proxies that were marked by a previous call to prepare_proxy_list_for_config_read() and are not used by the new config.

Definition at line 1189 of file transports.c.

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int check_if_restarts_needed = 0 [static]

Boolean: True iff we might need to restart some proxies.

Definition at line 135 of file transports.c.

smartlist_t* managed_proxy_list = NULL [static]

List of unconfigured managed proxies.

Definition at line 131 of file transports.c.

int unconfigured_proxies_n = 0 [static]

Number of still unconfigured proxies.

Definition at line 133 of file transports.c.