mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-30 15:43:32 +01:00
Add API for creating an owning controller FD and passing it to tor_main
This commit is contained in:
parent
9a89450b6d
commit
c77fe82155
@ -8,12 +8,19 @@
|
||||
* \file tor_api.c
|
||||
**/
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "feature/api/tor_api.h"
|
||||
#include "feature/api/tor_api_internal.h"
|
||||
|
||||
// Include this after the above headers, to insure that they don't
|
||||
// depend on anything else.
|
||||
#include "orconfig.h"
|
||||
#include "feature/api/tor_api_internal.h"
|
||||
#include "lib/cc/torint.h"
|
||||
#include "lib/cc/compat_compiler.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -28,6 +35,20 @@
|
||||
#define raw_realloc realloc
|
||||
#define raw_strdup strdup
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "lib/net/socketpair.h"
|
||||
#define raw_socketpair tor_ersatz_socketpair
|
||||
#define raw_closesocket closesocket
|
||||
#define snprintf _snprintf
|
||||
#else
|
||||
#define raw_socketpair socketpair
|
||||
#define raw_closesocket close
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Helper: Add a copy of <b>arg</b> to the owned arguments of <b>cfg</b>.
|
||||
* Return 0 on success, -1 on failure.
|
||||
@ -61,6 +82,8 @@ tor_main_configuration_new(void)
|
||||
cfg->argc = 1;
|
||||
cfg->argv = (char **) fake_argv;
|
||||
|
||||
cfg->owning_controller_socket = TOR_INVALID_SOCKET;
|
||||
|
||||
return cfg;
|
||||
}
|
||||
|
||||
@ -75,12 +98,31 @@ tor_main_configuration_set_command_line(tor_main_configuration_t *cfg,
|
||||
return 0;
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
void
|
||||
tor_main_configuration_free(tor_main_configuration_t *cfg)
|
||||
{
|
||||
if (cfg == NULL)
|
||||
return;
|
||||
cfg_add_owned_arg(cfg, "bye");//XXXX
|
||||
if (cfg->argv_owned) {
|
||||
int i;
|
||||
for (i = 0; i < cfg->argc_owned; ++i) {
|
||||
@ -88,6 +130,9 @@ tor_main_configuration_free(tor_main_configuration_t *cfg)
|
||||
}
|
||||
raw_free(cfg->argv_owned);
|
||||
}
|
||||
if (SOCKET_OK(cfg->owning_controller_socket)) {
|
||||
raw_closesocket(cfg->owning_controller_socket);
|
||||
}
|
||||
raw_free(cfg);
|
||||
}
|
||||
|
||||
|
@ -49,6 +49,18 @@ tor_main_configuration_t *tor_main_configuration_new(void);
|
||||
int tor_main_configuration_set_command_line(tor_main_configuration_t *cfg,
|
||||
int argc, char *argv[]);
|
||||
|
||||
#ifdef _WIN32
|
||||
typedef SOCKET tor_control_socket_t;
|
||||
#define INVALID_TOR_CONTROL_SOCKET INVALID_SOCKET
|
||||
#else
|
||||
typedef int tor_control_socket_t;
|
||||
#define INVALID_TOR_CONTROL_SOCKET (-1)
|
||||
#endif
|
||||
|
||||
/** DOCDOC */
|
||||
tor_control_socket_t tor_main_configuration_setup_control_socket(
|
||||
tor_main_configuration_t *cfg);
|
||||
|
||||
/**
|
||||
* Release all storage held in <b>cfg</b>.
|
||||
*
|
||||
|
@ -21,6 +21,9 @@ struct tor_main_configuration_t {
|
||||
int argc_owned;
|
||||
/** As argv, but is owned by the tor_main_configuration_t object. */
|
||||
char **argv_owned;
|
||||
|
||||
/** Socket that Tor will use as an owning control socket. Owned. */
|
||||
tor_socket_t owning_controller_socket;
|
||||
};
|
||||
|
||||
#endif /* !defined(TOR_API_INTERNAL_H) */
|
||||
|
Loading…
Reference in New Issue
Block a user