2017-10-18 20:26:29 +02:00
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2019-01-16 18:33:22 +01:00
|
|
|
* Copyright (c) 2007-2019, The Tor Project, Inc. */
|
2017-10-18 20:26:29 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file tor_api.c
|
|
|
|
**/
|
|
|
|
|
2018-08-01 16:36:10 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "feature/api/tor_api.h"
|
2017-10-18 20:26:29 +02:00
|
|
|
|
|
|
|
// Include this after the above headers, to insure that they don't
|
|
|
|
// depend on anything else.
|
|
|
|
#include "orconfig.h"
|
2018-08-01 16:36:10 +02:00
|
|
|
#include "lib/cc/torint.h"
|
2018-09-14 13:11:37 +02:00
|
|
|
#include "feature/api/tor_api_internal.h"
|
2018-08-01 16:36:10 +02:00
|
|
|
#include "lib/cc/compat_compiler.h"
|
2017-10-18 20:26:29 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// We don't want to use tor_malloc and tor_free here, since this needs
|
|
|
|
// to run before anything is initialized at all, and ought to run when
|
|
|
|
// we're not linked to anything at all.
|
|
|
|
|
|
|
|
#define raw_malloc malloc
|
|
|
|
#define raw_free free
|
2018-08-01 16:15:44 +02:00
|
|
|
#define raw_realloc realloc
|
|
|
|
#define raw_strdup strdup
|
|
|
|
|
2018-08-01 16:36:10 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "lib/net/socketpair.h"
|
|
|
|
#define raw_socketpair tor_ersatz_socketpair
|
|
|
|
#define raw_closesocket closesocket
|
|
|
|
#define snprintf _snprintf
|
2019-09-30 14:57:37 +02:00
|
|
|
#else /* !defined(_WIN32) */
|
2018-08-01 16:36:10 +02:00
|
|
|
#define raw_socketpair socketpair
|
|
|
|
#define raw_closesocket close
|
2019-06-05 15:33:35 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2018-08-01 16:36:10 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2018-08-01 16:15:44 +02:00
|
|
|
/**
|
|
|
|
* Helper: Add a copy of <b>arg</b> to the owned arguments of <b>cfg</b>.
|
|
|
|
* Return 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
cfg_add_owned_arg(tor_main_configuration_t *cfg, const char *arg)
|
|
|
|
{
|
|
|
|
/* We aren't using amortized realloc here, because libc should do it for us,
|
|
|
|
* and because this function is not critical-path. */
|
|
|
|
char **new_argv = raw_realloc(cfg->argv_owned,
|
|
|
|
sizeof(char*) * (cfg->argc_owned+1));
|
|
|
|
if (new_argv == NULL)
|
|
|
|
return -1;
|
|
|
|
cfg->argv_owned = new_argv;
|
|
|
|
if (NULL == (cfg->argv_owned[cfg->argc_owned] = raw_strdup(arg)))
|
|
|
|
return -1;
|
|
|
|
++cfg->argc_owned;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-18 20:26:29 +02:00
|
|
|
|
|
|
|
tor_main_configuration_t *
|
|
|
|
tor_main_configuration_new(void)
|
|
|
|
{
|
|
|
|
static const char *fake_argv[] = { "tor" };
|
|
|
|
tor_main_configuration_t *cfg = raw_malloc(sizeof(*cfg));
|
|
|
|
if (cfg == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(cfg, 0, sizeof(*cfg));
|
|
|
|
|
|
|
|
cfg->argc = 1;
|
|
|
|
cfg->argv = (char **) fake_argv;
|
|
|
|
|
2018-08-01 16:36:10 +02:00
|
|
|
cfg->owning_controller_socket = TOR_INVALID_SOCKET;
|
|
|
|
|
2017-10-18 20:26:29 +02:00
|
|
|
return cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tor_main_configuration_set_command_line(tor_main_configuration_t *cfg,
|
|
|
|
int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (cfg == NULL)
|
|
|
|
return -1;
|
|
|
|
cfg->argc = argc;
|
|
|
|
cfg->argv = argv;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:36:10 +02:00
|
|
|
tor_control_socket_t
|
|
|
|
tor_main_configuration_setup_control_socket(tor_main_configuration_t *cfg)
|
|
|
|
{
|
|
|
|
if (SOCKET_OK(cfg->owning_controller_socket))
|
|
|
|
return INVALID_TOR_CONTROL_SOCKET;
|
|
|
|
|
|
|
|
tor_socket_t fds[2];
|
|
|
|
if (raw_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
|
|
|
|
return INVALID_TOR_CONTROL_SOCKET;
|
|
|
|
}
|
|
|
|
char buf[32];
|
|
|
|
snprintf(buf, sizeof(buf), "%"PRIu64, (uint64_t)fds[1]);
|
|
|
|
|
|
|
|
cfg_add_owned_arg(cfg, "__OwningControllerFD");
|
|
|
|
cfg_add_owned_arg(cfg, buf);
|
|
|
|
|
|
|
|
cfg->owning_controller_socket = fds[1];
|
|
|
|
return fds[0];
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:26:29 +02:00
|
|
|
void
|
|
|
|
tor_main_configuration_free(tor_main_configuration_t *cfg)
|
|
|
|
{
|
|
|
|
if (cfg == NULL)
|
|
|
|
return;
|
2018-08-01 16:15:44 +02:00
|
|
|
if (cfg->argv_owned) {
|
2018-09-04 17:04:55 +02:00
|
|
|
for (int i = 0; i < cfg->argc_owned; ++i) {
|
2018-08-01 16:15:44 +02:00
|
|
|
raw_free(cfg->argv_owned[i]);
|
|
|
|
}
|
|
|
|
raw_free(cfg->argv_owned);
|
|
|
|
}
|
2018-08-01 16:36:10 +02:00
|
|
|
if (SOCKET_OK(cfg->owning_controller_socket)) {
|
|
|
|
raw_closesocket(cfg->owning_controller_socket);
|
|
|
|
}
|
2017-10-18 20:26:29 +02:00
|
|
|
raw_free(cfg);
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:17:32 +02:00
|
|
|
const char *
|
|
|
|
tor_api_get_provider_version(void)
|
|
|
|
{
|
|
|
|
return "tor " VERSION;
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:26:29 +02:00
|
|
|
/* Main entry point for the Tor process. Called from main().
|
|
|
|
*
|
|
|
|
* This function is distinct from main() only so we can link main.c into
|
|
|
|
* the unittest binary without conflicting with the unittests' main.
|
|
|
|
*
|
|
|
|
* Some embedders have historically called this function; but that usage is
|
|
|
|
* deprecated: they should use tor_run_main() instead.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
tor_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
tor_main_configuration_t *cfg = tor_main_configuration_new();
|
|
|
|
if (!cfg) {
|
|
|
|
puts("INTERNAL ERROR: Allocation failure. Cannot proceed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (tor_main_configuration_set_command_line(cfg, argc, argv) < 0) {
|
|
|
|
puts("INTERNAL ERROR: Can't set command line. Cannot proceed.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int rv = tor_run_main(cfg);
|
|
|
|
tor_main_configuration_free(cfg);
|
|
|
|
return rv;
|
|
|
|
}
|