#include "torlog.h"
#include "container.h"
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#include <process.h>
#include <windows.h>
#include <winsock2.h>
ent = err ? NULL : &hent;
#else
ent = gethostbyname(name);
-#ifdef MS_WINDOWS
+#ifdef _WIN32
err = WSAGetLastError();
#else
err = h_errno;
}
return 0;
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
return (err == WSATRY_AGAIN) ? 1 : -1;
#else
return (err == TRY_AGAIN) ? 1 : -1;
return -1;
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
typedef ULONG (WINAPI *GetAdaptersAddresses_fn_t)(
ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG);
#endif
freeifaddrs(ifa);
return result;
-#elif defined(MS_WINDOWS)
+#elif defined(_WIN32)
/* Windows XP began to provide GetAdaptersAddresses. Windows 2000 had a
"GetAdaptersInfo", but that's deprecated; let's just try
GetAdaptersAddresses and fall back to connect+getsockname.
#include "compat.h"
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#include <process.h>
#include <windows.h>
#include <sys/locking.h>
munmap((char*)handle->data, handle->mapping_size);
tor_free(handle);
}
-#elif defined(MS_WINDOWS)
+#elif defined(_WIN32)
tor_mmap_t *
tor_mmap_file(const char *filename)
{
return -1; /* no place for the NUL */
if (size > SIZE_T_CEILING)
return -1;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
r = _vsnprintf(str, size, format, args);
#else
r = vsnprintf(str, size, format, args);
return start;
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/** Take a filename and return a pointer to its final element. This
* function is called on __FILE__ to fix a MSVC nit where __FILE__
* contains the full path to the file. This is bad, because it
int
replace_file(const char *from, const char *to)
{
-#ifndef MS_WINDOWS
+#ifndef _WIN32
return rename(from,to);
#else
switch (file_status(to))
return NULL;
}
-#ifdef WIN32
+#ifdef _WIN32
_lseek(fd, 0, SEEK_SET);
if (_locking(fd, blocking ? _LK_LOCK : _LK_NBLCK, 1) < 0) {
if (errno != EACCES && errno != EDEADLOCK)
tor_assert(lockfile);
log_info(LD_FS, "Unlocking \"%s\"", lockfile->filename);
-#ifdef WIN32
+#ifdef _WIN32
_lseek(lockfile->fd, 0, SEEK_SET);
if (_locking(lockfile->fd, _LK_UNLCK, 1) < 0) {
log_warn(LD_FS,"Error unlocking \"%s\": %s", lockfile->filename,
off_t
tor_fd_getpos(int fd)
{
-#ifdef WIN32
+#ifdef _WIN32
return (off_t) _lseek(fd, 0, SEEK_CUR);
#else
return (off_t) lseek(fd, 0, SEEK_CUR);
int
tor_fd_seekend(int fd)
{
-#ifdef WIN32
+#ifdef _WIN32
return _lseek(fd, 0, SEEK_END) < 0 ? -1 : 0;
#else
return lseek(fd, 0, SEEK_END) < 0 ? -1 : 0;
* tor_close_socket to close sockets, and always using close() on
* files.
*/
-#if defined(MS_WINDOWS)
+#if defined(_WIN32)
r = closesocket(s);
#else
r = close(s);
} else {
int err = tor_socket_errno(-1);
log_info(LD_NET, "Close returned an error: %s", tor_socket_strerror(err));
-#ifdef WIN32
+#ifdef _WIN32
if (err != WSAENOTSOCK)
--n_sockets_open;
#else
void
set_socket_nonblocking(tor_socket_t socket)
{
-#if defined(MS_WINDOWS)
+#if defined(_WIN32)
unsigned long nonblocking = 1;
ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
#else
tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
{
//don't use win32 socketpairs (they are always bad)
-#if defined(HAVE_SOCKETPAIR) && !defined(MS_WINDOWS)
+#if defined(HAVE_SOCKETPAIR) && !defined(_WIN32)
int r;
#ifdef SOCK_CLOEXEC
type |= SOCK_CLOEXEC;
|| family != AF_UNIX
#endif
) {
-#ifdef MS_WINDOWS
+#ifdef _WIN32
return -WSAEAFNOSUPPORT;
#else
return -EAFNOSUPPORT;
return 0;
abort_tidy_up_and_fail:
-#ifdef MS_WINDOWS
+#ifdef _WIN32
saved_errno = WSAECONNABORTED;
#else
saved_errno = ECONNABORTED; /* I hope this is portable and appropriate. */
#if defined(CYGWIN) || defined(__CYGWIN__)
const char *platform = "Cygwin";
const unsigned long MAX_CONNECTIONS = 3200;
-#elif defined(MS_WINDOWS)
+#elif defined(_WIN32)
const char *platform = "Windows";
const unsigned long MAX_CONNECTIONS = 15000;
#else
return 0;
}
-#ifndef MS_WINDOWS
+#ifndef _WIN32
/** Log details of current user and group credentials. Return 0 on
* success. Logs and return -1 on failure.
*/
int
switch_id(const char *user)
{
-#ifndef MS_WINDOWS
+#ifndef _WIN32
struct passwd *pw = NULL;
uid_t old_uid;
gid_t old_gid;
char *cp;
int at_end = 1;
tor_assert(fname);
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/* If we start with, say, c:, then don't consider that the start of the path
*/
if (fname[0] && fname[1] == ':') {
at_end = 1;
while (--cp > fname) {
int is_sep = (*cp == '/'
-#ifdef MS_WINDOWS
+#ifdef _WIN32
|| *cp == '\\'
#endif
);
char *
make_path_absolute(char *fname)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
char *absfname_malloced = _fullpath(NULL, fname, 1);
/* We don't want to assume that tor_free can free a string allocated
void
tor_init_weak_random(unsigned seed)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
srand(seed);
#else
srandom(seed);
long
tor_weak_random(void)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
return rand();
#else
return random();
} else
#endif
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
OSVERSIONINFOEX info;
int i;
const char *plat = NULL;
static int
compute_num_cpus_impl(void)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
SYSTEM_INFO info;
memset(&info, 0, sizeof(info));
GetSystemInfo(&info);
void
tor_gettimeofday(struct timeval *timeval)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/* Epoch bias copied from perl: number of units between windows epoch and
* Unix epoch. */
#define EPOCH_BIAS U64_LITERAL(116444736000000000)
return;
}
-#if defined(TOR_IS_MULTITHREADED) && !defined(MS_WINDOWS)
+#if defined(TOR_IS_MULTITHREADED) && !defined(_WIN32)
/** Defined iff we need to add locks when defining fake versions of reentrant
* versions of time-related functions. */
#define TIME_FNS_NEED_LOCKS
* should call tor_socket_errno <em>at most once</em> on the failing
* socket to get the error.
*/
-#if defined(MS_WINDOWS)
+#if defined(_WIN32)
int
tor_socket_errno(tor_socket_t sock)
{
}
#endif
-#if defined(MS_WINDOWS)
+#if defined(_WIN32)
#define E(code, s) { code, (s " [" #code " ]") }
struct { int code; const char *msg; } windows_socket_errors[] = {
E(WSAEINTR, "Interrupted function call"),
int
network_init(void)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/* This silly exercise is necessary before windows will allow
* gethostbyname to work. */
WSADATA WSAData;
return 0;
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/** Return a newly allocated string describing the windows system error code
* <b>err</b>. Note that error codes are different from errno. Error codes
* come from GetLastError() when a winapi call fails. errno is set only when
#include "orconfig.h"
#include "torint.h"
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#ifndef WIN32_WINNT
#define WIN32_WINNT 0x400
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
-#if defined(HAVE_PTHREAD_H) && !defined(MS_WINDOWS)
+#if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
#include <pthread.h>
#endif
#include <stdarg.h>
#endif
/* inline is __inline on windows. */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#define INLINE __inline
#else
#define INLINE inline
#endif
/* ===== String compatibility */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/* Windows names string functions differently from most other platforms. */
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#ifdef HAVE_SYS_MMAN_H
size_t mapping_size; /**< Size of the actual mapping. (This is this file
* size, rounded up to the nearest page.) */
-#elif defined MS_WINDOWS
+#elif defined _WIN32
HANDLE file_handle;
HANDLE mmap_handle;
#endif
#define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
#endif
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#define _SHORT_FILE_ (tor_fix_source_file(__FILE__))
const char *tor_fix_source_file(const char *fname);
#else
off_t tor_fd_getpos(int fd);
int tor_fd_seekend(int fd);
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#define PATH_SEPARATOR "\\"
#else
#define PATH_SEPARATOR "/"
typedef int socklen_t;
#endif
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/* XXX Actually, this should arguably be SOCKET; we use intptr_t here so that
* any inadvertant checks for the socket being <= 0 or > 0 will probably
* still work. */
* errnos against expected values, and use tor_socket_errno to find
* the actual errno after a socket operation fails.
*/
-#if defined(MS_WINDOWS)
+#if defined(_WIN32)
/** Return true if e is EAGAIN or the local equivalent. */
#define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
/** Return true if e is EINPROGRESS or the local equivalent. */
int spawn_func(void (*func)(void *), void *data);
void spawn_exit(void) ATTR_NORETURN;
-#if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
+#if defined(ENABLE_THREADS) && defined(_WIN32)
#define USE_WIN32_THREADS
#define TOR_IS_MULTITHREADED 1
#elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
#endif
/* Platform-specific helpers. */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
char *format_win32_error(DWORD err);
#endif
/*for some reason my compiler doesn't have these version flags defined
a nice homework assignment for someone one day is to define the rest*/
//these are the values as given on MSDN
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#ifndef VER_SUITE_EMBEDDEDNT
#define VER_SUITE_EMBEDDEDNT 0x00000040
cfg = event_config_new();
tor_assert(cfg);
-#if defined(MS_WINDOWS) && defined(USE_BUFFEREVENTS)
+#if defined(_WIN32) && defined(USE_BUFFEREVENTS)
if (! torcfg->disable_iocp) {
evthread_use_windows_threads();
event_config_set_flag(cfg, EVENT_BASE_FLAG_STARTUP_IOCP);
/* This could be a socketpair() failure, which can happen sometimes on
* windows boxes with obnoxious firewall rules. Downgrade and try
* again. */
-#if defined(MS_WINDOWS) && defined(USE_BUFFEREVENTS)
+#if defined(_WIN32) && defined(USE_BUFFEREVENTS)
if (torcfg->disable_iocp == 0) {
log_warn(LD_GENERAL, "Unable to initialize Libevent. Trying again "
"with IOCP disabled.");
#include "orconfig.h"
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#ifndef WIN32_WINNT
#define WIN32_WINNT 0x400
#endif
int rand_poll_status = 0;
/* local variables */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
unsigned char buf[ADD_ENTROPY];
static int provider_set = 0;
static HCRYPTPROV provider;
log_warn(LD_CRYPTO, "RAND_poll() failed.");
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (!provider_set) {
if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
#include <errno.h>
#endif
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#include <windows.h>
/* Windows does not define pid_t, but _getpid() returns an int. */
* polls. */
pid_t pid;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/** Windows-only: Should we poll hproc? If false, poll pid
* instead. */
int poll_hproc;
procmon->pid = ppspec.pid;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
procmon->hproc = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE,
FALSE,
procmon->pid);
tor_assert(procmon != NULL);
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (procmon->poll_hproc) {
DWORD exit_code;
if (!GetExitCodeProcess(procmon->hproc, &exit_code)) {
if (procmon == NULL)
return;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (procmon->hproc != NULL)
CloseHandle(procmon->hproc);
#endif
#endif
#include <assert.h>
-#ifdef MS_WINDOWS /*wrkard for dtls1.h >= 0.9.8m of "#include <winsock.h>"*/
+#ifdef _WIN32 /*wrkard for dtls1.h >= 0.9.8m of "#include <winsock.h>"*/
#ifndef WIN32_WINNT
#define WIN32_WINNT 0x400
#endif
static int
tor_errno_to_tls_error(int e)
{
-#if defined(MS_WINDOWS)
+#if defined(_WIN32)
switch (e) {
case WSAECONNRESET: // most common
return TOR_TLS_ERROR_CONNRESET;
#include "container.h"
#include "address.h"
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#include <io.h>
#include <direct.h>
#include <process.h>
#ifdef HAVE_STRTOULL
r = (uint64_t)strtoull(s, &endptr, base);
-#elif defined(MS_WINDOWS)
+#elif defined(_WIN32)
#if defined(_MSC_VER) && _MSC_VER < 1300
tor_assert(base <= 10);
r = (uint64_t)_atoi64(s);
static void
clean_name_for_stat(char *name)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
size_t len = strlen(name);
if (!len)
return;
int r;
struct stat st;
char *f;
-#ifndef MS_WINDOWS
+#ifndef _WIN32
int mask;
struct passwd *pw = NULL;
uid_t running_uid;
}
if (check & CPD_CREATE) {
log_info(LD_GENERAL, "Creating directory %s", dirname);
-#if defined (MS_WINDOWS) && !defined (WINCE)
+#if defined (_WIN32) && !defined (WINCE)
r = mkdir(dirname);
#else
r = mkdir(dirname, 0700);
log_warn(LD_FS, "%s is not a directory", dirname);
return -1;
}
-#ifndef MS_WINDOWS
+#ifndef _WIN32
if (effective_user) {
/* Look up the user and group information.
* If we have a problem, bail out. */
int
write_str_to_file(const char *fname, const char *str, int bin)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (!bin && strchr(str, '\r')) {
log_warn(LD_BUG,
"We're writing a text string that already contains a CR.");
}
string[r] = '\0'; /* NUL-terminate the result. */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (!bin && strchr(string, '\r')) {
log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
"when reading %s. Coping.",
expand_filename(const char *filename)
{
tor_assert(filename);
-#ifdef MS_WINDOWS
+#ifdef _WIN32
return tor_strdup(filename);
#else
if (*filename == '~') {
tor_listdir(const char *dirname)
{
smartlist_t *result;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
char *pattern=NULL;
TCHAR tpattern[MAX_PATH] = {0};
char name[MAX_PATH] = {0};
{
if (filename && filename[0] == '/')
return 0;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
else if (filename && filename[0] == '\\')
return 0;
else if (filename && strlen(filename)>3 && TOR_ISALPHA(filename[0]) &&
* Process helpers
* ===== */
-#ifndef MS_WINDOWS
+#ifndef _WIN32
/* Based on code contributed by christian grothoff */
/** True iff we've called start_daemon(). */
static int start_daemon_called = 0;
log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
strerror(errno));
} else {
-#ifdef MS_WINDOWS
+#ifdef _WIN32
fprintf(pidfile, "%d\n", (int)_getpid());
#else
fprintf(pidfile, "%d\n", (int)getpid());
}
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
HANDLE
load_windows_system_library(const TCHAR *library_name)
{
int
tor_terminate_process(process_handle_t *process_handle)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (tor_get_exit_code(process_handle, 0, NULL) == PROCESS_EXIT_RUNNING) {
HANDLE handle;
/* If the signal is outside of what GenerateConsoleCtrlEvent can use,
int
tor_process_get_pid(process_handle_t *process_handle)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
return (int) process_handle->pid.dwProcessId;
#else
return (int) process_handle->pid;
#endif
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
HANDLE
tor_process_get_stdout_pipe(process_handle_t *process_handle)
{
{
process_handle_t *out = tor_malloc_zero(sizeof(process_handle_t));
-#ifndef MS_WINDOWS
+#ifndef _WIN32
out->stdout_pipe = -1;
out->stderr_pipe = -1;
#endif
*/
int
tor_spawn_background(const char *const filename, const char **argv,
-#ifdef MS_WINDOWS
+#ifdef _WIN32
LPVOID envp,
#else
const char **envp,
#endif
process_handle_t **process_handle_out)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
HANDLE stdout_pipe_read = NULL;
HANDLE stdout_pipe_write = NULL;
HANDLE stderr_pipe_read = NULL;
/* TODO: Close pipes on exit */
*process_handle_out = process_handle;
return status;
-#else // MS_WINDOWS
+#else // _WIN32
pid_t pid;
int stdout_pipe[2];
int stderr_pipe[2];
*process_handle_out = process_handle;
return process_handle->status;
-#endif // MS_WINDOWS
+#endif // _WIN32
}
/** Destroy all resources allocated by the process handle in
process_handle->status = PROCESS_STATUS_NOTRUNNING;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (process_handle->stdout_pipe)
CloseHandle(process_handle->stdout_pipe);
tor_get_exit_code(const process_handle_t *process_handle,
int block, int *exit_code)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
DWORD retval;
BOOL success;
if (exit_code != NULL)
*exit_code = WEXITSTATUS(stat_loc);
-#endif // MS_WINDOWS
+#endif // _WIN32
return PROCESS_EXIT_EXITED;
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/** Read from a handle <b>h</b> into <b>buf</b>, up to <b>count</b> bytes. If
* <b>hProcess</b> is NULL, the function will return immediately if there is
* nothing more to read. Otherwise <b>hProcess</b> should be set to the handle
tor_read_all_from_process_stdout(const process_handle_t *process_handle,
char *buf, size_t count)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
return tor_read_all_handle(process_handle->stdout_pipe, buf, count,
process_handle);
#else
tor_read_all_from_process_stderr(const process_handle_t *process_handle,
char *buf, size_t count)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
return tor_read_all_handle(process_handle->stderr_pipe, buf, count,
process_handle);
#else
return smartlist_len(sl);
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/** Read from stream, and send lines to log at the specified log level.
* Returns -1 if there is a error reading, and 0 otherwise.
* If the generated stream is flushed more often than on new lines, or
child_handle = NULL;
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/* Passing NULL as lpApplicationName makes Windows search for the .exe */
status = tor_spawn_background(NULL, argv, NULL, &child_handle);
#else
if (child_handle && PROCESS_STATUS_RUNNING == child_handle->status) {
/* Read from stdout/stderr and log result */
retval = 0;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
stdout_status = log_from_handle(child_handle->stdout_pipe, LOG_INFO);
stderr_status = log_from_handle(child_handle->stderr_pipe, LOG_WARN);
/* If we got this far (on Windows), the process started */
if (-1 == stdout_status || -1 == stderr_status)
/* There was a failure */
retval = -1;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
else if (!child_handle || tor_get_exit_code(child_handle, 0, NULL) !=
PROCESS_EXIT_RUNNING) {
/* process has exited or there was an error */
typedef struct process_handle_t process_handle_t;
int tor_spawn_background(const char *const filename, const char **argv,
-#ifdef MS_WINDOWS
+#ifdef _WIN32
LPVOID envp,
#else
const char **envp,
#define SPAWN_ERROR_MESSAGE "ERR: Failed to spawn background process - code "
-#ifdef MS_WINDOWS
+#ifdef _WIN32
HANDLE load_windows_system_library(const TCHAR *library_name);
#endif
/*DOCDOC*/
struct process_handle_t {
int status;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
HANDLE stdout_pipe;
HANDLE stderr_pipe;
PROCESS_INFORMATION pid;
FILE *stdout_handle;
FILE *stderr_handle;
pid_t pid;
-#endif // MS_WINDOWS
+#endif // _WIN32
};
#endif
int tor_get_exit_code(const process_handle_t *process_handle,
int block, int *exit_code);
int tor_split_lines(struct smartlist_t *sl, char *buf, int len);
-#ifdef MS_WINDOWS
+#ifdef _WIN32
ssize_t tor_read_all_handle(HANDLE h, char *buf, size_t count,
const process_handle_t *process);
#else
char *tor_join_win_cmdline(const char *argv[]);
int tor_process_get_pid(process_handle_t *process_handle);
-#ifdef MS_WINDOWS
+#ifdef _WIN32
HANDLE tor_process_get_stdout_pipe(process_handle_t *process_handle);
#else
FILE *tor_process_get_stdout_pipe(process_handle_t *process_handle);
if (read_result < 0) {
int e = tor_socket_errno(fd);
if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (e == WSAENOBUFS)
log_warn(LD_NET,"recv() failed: WSAENOBUFS. Not enough ram?");
#endif
if (write_result < 0) {
int e = tor_socket_errno(s);
if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (e == WSAENOBUFS)
log_warn(LD_NET,"write() failed: WSAENOBUFS. Not enough ram?");
#endif
#include "util.h"
#include "routerlist.h"
#include "transports.h"
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#include <shlobj.h>
#endif
V(FetchHidServDescriptors, BOOL, "1"),
V(FetchUselessDescriptors, BOOL, "0"),
V(FetchV2Networkstatus, BOOL, "0"),
-#ifdef WIN32
+#ifdef _WIN32
V(GeoIPFile, FILENAME, "<default>"),
#else
V(GeoIPFile, FILENAME,
*(uint32_t*)STRUCT_VAR_P(cfg,fmt->magic_offset)); \
STMT_END
-#ifdef MS_WINDOWS
+#ifdef _WIN32
static char *get_windows_conf_root(void);
#endif
static void config_line_append(config_line_t **lst,
* understand prefixes somehow. -NM */
/* XXXX023 Reload GeoIPFile on SIGHUP. -NM */
char *actual_fname = tor_strdup(options->GeoIPFile);
-#ifdef WIN32
+#ifdef _WIN32
if (!strcmp(actual_fname, "<default>")) {
const char *conf_root = get_windows_conf_root();
tor_free(actual_fname);
REJECT("Failed to resolve/guess local address. See logs for details.");
}
-#ifndef MS_WINDOWS
+#ifndef _WIN32
if (options->RunAsDaemon && torrc_fname && path_is_relative(torrc_fname))
REJECT("Can't use a relative path to torrc when RunAsDaemon is set.");
#endif
return 0;
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/** Return the directory on windows where we expect to find our application
* data. */
static char *
static const char *
get_default_conf_file(int defaults_file)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (defaults_file) {
static char defaults_path[MAX_PATH+1];
tor_snprintf(defaults_path, MAX_PATH, "%s\\torrc-defaults",
if (dflt && file_status(dflt) == FN_FILE) {
fname = tor_strdup(dflt);
} else {
-#ifndef MS_WINDOWS
+#ifndef _WIN32
char *fn = NULL;
if (!defaults_file)
fn = expand_filename("~/.torrc");
int ok;
smartlist_t *elts;
int daemon =
-#ifdef MS_WINDOWS
+#ifdef _WIN32
0;
#else
options->RunAsDaemon;
} else {
continue;
}
-#ifndef MS_WINDOWS
+#ifndef _WIN32
if (!port->no_advertise && port->port < 1024)
++n_low_port;
#endif
static int
normalize_data_directory(or_options_t *options)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
char *p;
if (options->DataDirectory)
return 0; /* all set */
static void
make_socket_reuseable(tor_socket_t sock)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
(void) sock;
#else
int one=1;
* Warn if we do, and refuse to make the connection. */
static ratelim_t disablenet_violated = RATELIM_INIT(30*60);
char *m;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
*socket_error = WSAENETUNREACH;
#else
*socket_error = ENETUNREACH;
#include "routerlist.h"
#include "routerparse.h"
-#ifndef MS_WINDOWS
+#ifndef _WIN32
#include <pwd.h>
#include <sys/resource.h>
#endif
log_warn(LD_CONTROL, "Writing %s failed: %s",
options->ControlPortWriteToFile, strerror(errno));
}
-#ifndef MS_WINDOWS
+#ifndef _WIN32
if (options->ControlPortFileGroupReadable) {
if (chmod(options->ControlPortWriteToFile, 0640)) {
log_warn(LD_FS,"Unable to make %s group-readable.",
} else if (!strcmp(question, "process/pid")) {
int myPid = -1;
- #ifdef MS_WINDOWS
+ #ifdef _WIN32
myPid = _getpid();
#else
myPid = getpid();
tor_asprintf(answer, "%d", myPid);
} else if (!strcmp(question, "process/uid")) {
- #ifdef MS_WINDOWS
+ #ifdef _WIN32
*answer = tor_strdup("-1");
#else
int myUid = geteuid();
tor_asprintf(answer, "%d", myUid);
#endif
} else if (!strcmp(question, "process/user")) {
- #ifdef MS_WINDOWS
+ #ifdef _WIN32
*answer = tor_strdup("");
#else
int myUid = geteuid();
}
#elif defined(CYGWIN) || defined(__CYGWIN__)
*answer = tor_strdup("3200");
- #elif defined(MS_WINDOWS)
+ #elif defined(_WIN32)
*answer = tor_strdup("15000");
#else
*answer = tor_strdup("15000");
tor_free(fname);
return -1;
}
-#ifndef MS_WINDOWS
+#ifndef _WIN32
if (get_options()->CookieAuthFileGroupReadable) {
if (chmod(fname, 0640)) {
log_warn(LD_FS,"Unable to make %s group-readable.", escaped(fname));
int r;
options = get_options();
conf_fname = options->ServerDNSResolvConfFile;
-#ifndef MS_WINDOWS
+#ifndef _WIN32
if (!conf_fname)
conf_fname = "/etc/resolv.conf";
#endif
if (nameservers_configured)
evdns_base_resume(the_evdns_base);
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
else {
if (nameservers_configured) {
evdns_base_search_clear(the_evdns_base);
#include "eventdns.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <windows.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <netinet/in6.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
typedef int socklen_t;
#endif
static int strtoint(const char *const str);
-#ifdef WIN32
+#ifdef _WIN32
static int
last_error(int sock)
{
if (!evdns_log_fn)
return;
va_start(args,fmt);
-#ifdef WIN32
+#ifdef _WIN32
_vsnprintf(buf, sizeof(buf), fmt, args);
#else
vsnprintf(buf, sizeof(buf), fmt, args);
ns->socket = tor_open_socket(address->sa_family, SOCK_DGRAM, 0);
if (ns->socket < 0) { err = 1; goto out1; }
-#ifdef WIN32
+#ifdef _WIN32
{
u_long nonblocking = 1;
ioctlsocket(ns->socket, FIONBIO, &nonblocking);
return err;
}
-#ifdef WIN32
+#ifdef _WIN32
/* Add multiple nameservers from a space-or-comma-separated list. */
static int
evdns_nameserver_ip_add_line(const char *ips) {
evdns_init(void)
{
int res = 0;
-#ifdef WIN32
+#ifdef _WIN32
evdns_config_windows_nameservers();
#else
res = evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
int evdns_resolve_reverse_ipv6(const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr);
int evdns_set_option(const char *option, const char *val, int flags);
int evdns_resolv_conf_parse(int flags, const char *);
-#ifdef MS_WINDOWS
+#ifdef _WIN32
int evdns_config_windows_nameservers(void);
#endif
void evdns_search_clear(void);
#ifndef HAVE_U_CHAR
typedef unsigned char u_char;
#endif
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#define inline __inline
#endif
#include "torint.h"
-#if defined(MS_WINDOWS) && !defined(WIN32)
+#if defined(_WIN32) && !defined(_WIN32)
/* How did _this_ happen? */
-#define WIN32
+#define _WIN32
#endif
/* These are for debugging possible memory leaks. */
}
#endif
-#if defined(MS_WINDOWS) && defined(USE_BUFFEREVENTS)
+#if defined(_WIN32) && defined(USE_BUFFEREVENTS)
/** Remove the kernel-space send and receive buffers for <b>s</b>. For use
* with IOCP only. */
static int
if (connection_type_uses_bufferevent(conn)) {
if (SOCKET_OK(conn->s) && !conn->linked) {
-#ifdef MS_WINDOWS
+#ifdef _WIN32
if (tor_libevent_using_iocp_bufferevents() &&
get_options()->UserspaceIOCPBuffers) {
set_buffer_lengths_to_zero(conn->s);
if (connection_handle_read(conn) < 0) {
if (!conn->marked_for_close) {
-#ifndef MS_WINDOWS
+#ifndef _WIN32
log_warn(LD_BUG,"Unhandled error on read for %s connection "
"(fd %d); removing",
conn_type_to_string(conn->type), (int)conn->s);
}
#endif
-#ifndef MS_WINDOWS
+#ifndef _WIN32
/** Called when a possibly ignorable libevent error occurs; ensures that we
* don't get into an infinite loop by ignoring too many errors from
* libevent. */
if (nt_service_is_stopping())
return 0;
-#ifndef MS_WINDOWS
+#ifndef _WIN32
/* Make it easier to tell whether libevent failure is our fault or not. */
errno = 0;
#endif
log_err(LD_NET,"libevent call with %s failed: %s [%d]",
tor_libevent_get_method(), tor_socket_strerror(e), e);
return -1;
-#ifndef MS_WINDOWS
+#ifndef _WIN32
} else if (e == EINVAL) {
log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
if (got_libevent_error())
}
}
-#ifndef MS_WINDOWS /* Only called when we're willing to use signals */
+#ifndef _WIN32 /* Only called when we're willing to use signals */
/** Libevent callback: invoked when we get a signal.
*/
static void
{
/* NOTE: If we ever daemonize, this gets called immediately. That's
* okay for now, because we only use this on Windows. */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
WSACleanup();
#endif
}
void
handle_signals(int is_parent)
{
-#ifndef MS_WINDOWS /* do signal stuff only on Unix */
+#ifndef _WIN32 /* do signal stuff only on Unix */
int i;
static const int signals[] = {
SIGINT, /* do a controlled slow shutdown */
return -1;
}
-#ifndef MS_WINDOWS
+#ifndef _WIN32
if (geteuid()==0)
log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
"and you probably shouldn't.");
log_warn(LD_GENERAL, "It looks like another Tor process is running "
"with the same data directory. Waiting 5 seconds to see "
"if it goes away.");
-#ifndef WIN32
+#ifndef _WIN32
sleep(5);
#else
Sleep(5000);
}
#endif
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/* Call SetProcessDEPPolicy to permanently enable DEP.
The function will not resolve on earlier versions of Windows,
and failure is not dangerous. */
#ifndef _TOR_NTMAIN_H
#define _TOR_NTMAIN_H
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#if !defined (WINCE)
#define NT_SERVICE
#endif
#endif
#endif
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#ifndef WIN32_WINNT
#define WIN32_WINNT 0x400
#endif
#include <time.h>
#endif
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#include <io.h>
#include <process.h>
#include <direct.h>
* E_CASE is for errors where windows has both a EFOO and a WSAEFOO
* version, and S_CASE is for errors where windows has only a WSAEFOO
* version. (The E is for 'error', the S is for 'socket'). */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#define E_CASE(s) case s: case WSA ## s
#define S_CASE(s) case WSA ## s
#else
#include "util.h"
#include "router.h"
-#ifdef MS_WINDOWS
+#ifdef _WIN32
static void set_managed_proxy_environment(LPVOID *envp,
const managed_proxy_t *mp);
#else
{
int retval;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
LPVOID envp=NULL;
} SMARTLIST_FOREACH_END(mp);
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/** Attempt to continue configuring managed proxy <b>mp</b>. */
static void
smartlist_free(lines);
}
-#else /* MS_WINDOWS */
+#else /* _WIN32 */
/** Attempt to continue configuring managed proxy <b>mp</b>. */
static void
}
}
-#endif /* MS_WINDOWS */
+#endif /* _WIN32 */
/** Register server managed proxy <b>mp</b> transports to state */
static void
return bindaddr_result;
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/** Prepare the environment <b>envp</b> of managed proxy <b>mp</b>.
* <b>envp</b> is allocated on the heap and should be freed by the
smartlist_free(envs);
}
-#else /* MS_WINDOWS */
+#else /* _WIN32 */
/** Prepare the environment <b>envp</b> of managed proxy <b>mp</b>.
* <b>envp</b> is allocated on the heap and should be freed by the
return r;
}
-#endif /* MS_WINDOWS */
+#endif /* _WIN32 */
/** Create and return a new managed proxy for <b>transport</b> using
* <b>proxy_argv</b>. If <b>is_server</b> is true, it's a server
#include <stdio.h>
#include "orconfig.h"
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
#else
succeed. Otherwise ReadFile() will get the entire output in one */
// XXX: Can we make stdio flush on newline?
fflush(stdout);
-#ifdef MS_WINDOWS
+#ifdef _WIN32
Sleep(1000);
#else
sleep(1);
#endif
fprintf(stdout, "DONE\n");
-#ifdef MS_WINDOWS
+#ifdef _WIN32
Sleep(1000);
#else
sleep(1);
#include <fcntl.h>
#endif
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/* For mkdir() */
#include <direct.h>
#else
int r;
if (is_setup) return;
-#ifdef MS_WINDOWS
+#ifdef _WIN32
{
char buf[MAX_PATH];
const char *tmp = buf;
#include "test.h"
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/* For mkdir() */
#include <direct.h>
#else
/* Possibly, turn this into a test setup/cleanup pair */
tor_free(options->DataDirectory);
options->DataDirectory = tor_strdup(get_fname("md_datadir_test"));
-#ifdef MS_WINDOWS
+#ifdef _WIN32
tt_int_op(0, ==, mkdir(options->DataDirectory));
#else
tt_int_op(0, ==, mkdir(options->DataDirectory, 0700));
char *s1 = NULL, *s2 = NULL;
int done = 0, timedout = 0;
time_t started;
-#ifndef MS_WINDOWS
+#ifndef _WIN32
struct timeval tv;
tv.tv_sec=0;
tv.tv_usec=10;
timedout = done = 1;
}
tor_mutex_release(_thread_test_mutex);
-#ifndef MS_WINDOWS
+#ifndef _WIN32
/* Prevent the main thread from starving the worker threads. */
select(0, NULL, NULL, NULL, &tv);
#endif
test_assert(mapping);
test_eq(mapping->size, strlen("Short file."));
test_streq(mapping->data, "Short file.");
-#ifdef MS_WINDOWS
+#ifdef _WIN32
tor_munmap_file(mapping);
mapping = NULL;
test_assert(unlink(fname1) == 0);
tor_free(cp);
}
-#ifdef MS_WINDOWS
+#ifdef _WIN32
static void
test_util_load_win_lib(void *ptr)
{
;
}
-#ifndef MS_WINDOWS
+#ifndef _WIN32
/** Check that fgets waits until a full line, and not return a partial line, on
* a EAGAIN with a non-blocking pipe */
static void
int status;
/* Start the program */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
status = tor_spawn_background(NULL, argv, NULL, &process_handle);
#else
status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
static void
test_util_spawn_background_ok(void *ptr)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
const char *argv[] = {"test-child.exe", "--test", NULL};
const char *expected_out = "OUT\r\n--test\r\nSLEEPING\r\nDONE\r\n";
const char *expected_err = "ERR\r\n";
const char *expected_err = "";
char expected_out[1024];
char code[32];
-#ifdef MS_WINDOWS
+#ifdef _WIN32
const int expected_status = PROCESS_STATUS_ERROR;
#else
/* TODO: Once we can signal failure to exec, set this to be
process_handle_t *process_handle=NULL;
int status;
char stdout_buf[100], stderr_buf[100];
-#ifdef MS_WINDOWS
+#ifdef _WIN32
const char *argv[] = {"test-child.exe", "--test", NULL};
const char *expected_out[] = { "OUT\r\n--test\r\nSLEEPING\r\n",
"DONE\r\n",
(void)ptr;
/* Start the program */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
status = tor_spawn_background(NULL, argv, NULL, &process_handle);
#else
status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
/* Check stdout */
for (expected_out_ctr = 0; expected_out[expected_out_ctr] != NULL;) {
-#ifdef MS_WINDOWS
+#ifdef _WIN32
pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
sizeof(stdout_buf) - 1, NULL);
#else
}
/* The process should have exited without writing more */
-#ifdef MS_WINDOWS
+#ifdef _WIN32
pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
sizeof(stdout_buf) - 1,
process_handle);
UTIL_TEST(asprintf, 0),
UTIL_TEST(listdir, 0),
UTIL_TEST(parent_dir, 0),
-#ifdef MS_WINDOWS
+#ifdef _WIN32
UTIL_TEST(load_win_lib, 0),
#endif
UTIL_TEST(exit_status, 0),
-#ifndef MS_WINDOWS
+#ifndef _WIN32
UTIL_TEST(fgets_eagain, TT_SKIP),
#endif
UTIL_TEST(spawn_background_ok, 0),
#include "orconfig.h"
#ifdef NAT_PMP
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#define STATICLIB
#endif
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
-#ifndef MS_WINDOWS
+#ifndef _WIN32
#include <arpa/inet.h>
#endif
#include "orconfig.h"
#ifdef MINIUPNPC
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#define STATICLIB
#endif
#include <stdint.h>
#include <time.h>
#include <string.h>
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#include <winsock2.h>
#endif
static int
network_init(void)
{
-#ifdef MS_WINDOWS
+#ifdef _WIN32
/* This silly exercise is necessary before windows will allow
* gethostbyname to work. */
WSADATA WSAData;
#include <errno.h>
#endif
-#ifdef MS_WINDOWS
+#ifdef _WIN32
#if defined(_MSC_VER) && (_MSC_VER <= 1300)
#include <winsock.h>
#else
*/
/* Windows-only defines. */
-#define MS_WINDOWS
-#define MS_WIN32
+#define _WIN32
#define CONFDIR ""
/* Define to 1 if you have the <arpa/inet.h> header file. */