tor  0.4.2.0-alpha-dev
tor_api.c
Go to the documentation of this file.
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 
11 #ifdef _WIN32
12 #include <winsock2.h>
13 #include <windows.h>
14 #endif
15 
16 #include "feature/api/tor_api.h"
17 
18 // Include this after the above headers, to insure that they don't
19 // depend on anything else.
20 #include "orconfig.h"
21 #include "lib/cc/torint.h"
22 #include "feature/api/tor_api_internal.h"
23 #include "lib/cc/compat_compiler.h"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 // We don't want to use tor_malloc and tor_free here, since this needs
30 // to run before anything is initialized at all, and ought to run when
31 // we're not linked to anything at all.
32 
33 #define raw_malloc malloc
34 #define raw_free free
35 #define raw_realloc realloc
36 #define raw_strdup strdup
37 
38 #ifdef _WIN32
39 #include "lib/net/socketpair.h"
40 #define raw_socketpair tor_ersatz_socketpair
41 #define raw_closesocket closesocket
42 #define snprintf _snprintf
43 #else /* !(defined(_WIN32)) */
44 #define raw_socketpair socketpair
45 #define raw_closesocket close
46 #endif /* defined(_WIN32) */
47 
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51 
56 static int
58 {
59  /* We aren't using amortized realloc here, because libc should do it for us,
60  * and because this function is not critical-path. */
61  char **new_argv = raw_realloc(cfg->argv_owned,
62  sizeof(char*) * (cfg->argc_owned+1));
63  if (new_argv == NULL)
64  return -1;
65  cfg->argv_owned = new_argv;
66  if (NULL == (cfg->argv_owned[cfg->argc_owned] = raw_strdup(arg)))
67  return -1;
68  ++cfg->argc_owned;
69  return 0;
70 }
71 
74 {
75  static const char *fake_argv[] = { "tor" };
76  tor_main_configuration_t *cfg = raw_malloc(sizeof(*cfg));
77  if (cfg == NULL)
78  return NULL;
79 
80  memset(cfg, 0, sizeof(*cfg));
81 
82  cfg->argc = 1;
83  cfg->argv = (char **) fake_argv;
84 
86 
87  return cfg;
88 }
89 
90 int
92  int argc, char *argv[])
93 {
94  if (cfg == NULL)
95  return -1;
96  cfg->argc = argc;
97  cfg->argv = argv;
98  return 0;
99 }
100 
101 tor_control_socket_t
103 {
105  return INVALID_TOR_CONTROL_SOCKET;
106 
107  tor_socket_t fds[2];
108  if (raw_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
109  return INVALID_TOR_CONTROL_SOCKET;
110  }
111  char buf[32];
112  snprintf(buf, sizeof(buf), "%"PRIu64, (uint64_t)fds[1]);
113 
114  cfg_add_owned_arg(cfg, "__OwningControllerFD");
115  cfg_add_owned_arg(cfg, buf);
116 
117  cfg->owning_controller_socket = fds[1];
118  return fds[0];
119 }
120 
121 void
123 {
124  if (cfg == NULL)
125  return;
126  if (cfg->argv_owned) {
127  for (int i = 0; i < cfg->argc_owned; ++i) {
128  raw_free(cfg->argv_owned[i]);
129  }
130  raw_free(cfg->argv_owned);
131  }
133  raw_closesocket(cfg->owning_controller_socket);
134  }
135  raw_free(cfg);
136 }
137 
138 const char *
140 {
141  return "tor " VERSION;
142 }
143 
144 /* Main entry point for the Tor process. Called from main().
145  *
146  * This function is distinct from main() only so we can link main.c into
147  * the unittest binary without conflicting with the unittests' main.
148  *
149  * Some embedders have historically called this function; but that usage is
150  * deprecated: they should use tor_run_main() instead.
151  */
152 int
153 tor_main(int argc, char *argv[])
154 {
156  if (!cfg) {
157  puts("INTERNAL ERROR: Allocation failure. Cannot proceed");
158  return 1;
159  }
160  if (tor_main_configuration_set_command_line(cfg, argc, argv) < 0) {
161  puts("INTERNAL ERROR: Can't set command line. Cannot proceed.");
162  return 1;
163  }
164  int rv = tor_run_main(cfg);
166  return rv;
167 }
static int cfg_add_owned_arg(tor_main_configuration_t *cfg, const char *arg)
Definition: tor_api.c:57
Integer definitions used throughout Tor.
void tor_main_configuration_free(tor_main_configuration_t *cfg)
Definition: tor_api.c:122
tor_control_socket_t tor_main_configuration_setup_control_socket(tor_main_configuration_t *cfg)
Definition: tor_api.c:102
const char * tor_api_get_provider_version(void)
Definition: tor_api.c:139
#define SOCKET_OK(s)
Definition: nettypes.h:39
tor_main_configuration_t * tor_main_configuration_new(void)
Definition: tor_api.c:73
Utility macros to handle different features and behavior in different compilers.
Public C API for the Tor network service.
int tor_main(int argc, char **argv)
#define tor_socket_t
Definition: nettypes.h:36
#define TOR_INVALID_SOCKET
Definition: nettypes.h:41
int tor_run_main(const tor_main_configuration_t *)
Definition: main.c:1268
tor_socket_t owning_controller_socket
int tor_main_configuration_set_command_line(tor_main_configuration_t *cfg, int argc, char *argv[])
Definition: tor_api.c:91