📄 compat.c
字号:
*/
uint32_t
get_uint32(const char *cp)
{
uint32_t v;
memcpy(&v,cp,4);
return v;
}
/**
* Set a 16-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
* *(uint16_t)(cp) = v, but will not cause segfaults on platforms that forbid
* unaligned memory access. */
void
set_uint16(char *cp, uint16_t v)
{
memcpy(cp,&v,2);
}
/**
* Set a 32-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
* *(uint32_t)(cp) = v, but will not cause segfaults on platforms that forbid
* unaligned memory access. */
void
set_uint32(char *cp, uint32_t v)
{
memcpy(cp,&v,4);
}
/**
* Rename the file <b>from</b> to the file <b>to</b>. On unix, this is
* the same as rename(2). On windows, this removes <b>to</b> first if
* it already exists.
* Returns 0 on success. Returns -1 and sets errno on failure.
*/
int
replace_file(const char *from, const char *to)
{
#ifndef MS_WINDOWS
return rename(from,to);
#else
switch (file_status(to))
{
case FN_NOENT:
break;
case FN_FILE:
if (unlink(to)) return -1;
break;
case FN_ERROR:
return -1;
case FN_DIR:
errno = EISDIR;
return -1;
}
return rename(from,to);
#endif
}
/** Change <b>fname</b>'s modification time to now. */
int
touch_file(const char *fname)
{
if (utime(fname, NULL)!=0)
return -1;
return 0;
}
#undef DEBUG_SOCKET_COUNTING
#ifdef DEBUG_SOCKET_COUNTING
/** A bitarray of all fds that should be passed to tor_socket_close(). Only
* used if DEBUG_SOCKET_COUNTING is defined. */
static bitarray_t *open_sockets = NULL;
/** The size of <b>open_sockets</b>, in bits. */
static int max_socket = -1;
#endif
/** Count of number of sockets currently open. (Undercounts sockets opened by
* eventdns and libevent.) */
static int n_sockets_open = 0;
/** As close(), but guaranteed to work for sockets across platforms (including
* Windows, where close()ing a socket doesn't work. Returns 0 on success, -1
* on failure. */
int
tor_close_socket(int s)
{
int r = 0;
#ifdef DEBUG_SOCKET_COUNTING
if (s > max_socket || ! bitarray_is_set(open_sockets, s)) {
log_warn(LD_BUG, "Closing a socket (%d) that wasn't returned by tor_open_"
"socket(), or that was already closed or something.", s);
} else {
tor_assert(open_sockets && s <= max_socket);
bitarray_clear(open_sockets, s);
}
#endif
/* On Windows, you have to call close() on fds returned by open(),
* and closesocket() on fds returned by socket(). On Unix, everything
* gets close()'d. We abstract this difference by always using
* tor_close_socket to close sockets, and always using close() on
* files.
*/
#ifdef USE_BSOCKETS
r = bclose(s);
#elif defined(MS_WINDOWS)
r = closesocket(s);
#else
r = close(s);
#endif
if (r == 0) {
--n_sockets_open;
} else {
int err = tor_socket_errno(-1);
log_info(LD_NET, "Close returned an error: %s", tor_socket_strerror(err));
#ifdef WIN32
if (err != WSAENOTSOCK)
--n_sockets_open;
#else
if (err != EBADF)
--n_sockets_open;
#endif
r = -1;
}
if (n_sockets_open < 0)
log_warn(LD_BUG, "Our socket count is below zero: %d. Please submit a "
"bug report.", n_sockets_open);
return r;
}
#ifdef DEBUG_SOCKET_COUNTING
/** Helper: if DEBUG_SOCKET_COUNTING is enabled, remember that <b>s</b> is
* now an open socket. */
static INLINE void
mark_socket_open(int s)
{
if (s > max_socket) {
if (max_socket == -1) {
open_sockets = bitarray_init_zero(s+128);
max_socket = s+128;
} else {
open_sockets = bitarray_expand(open_sockets, max_socket, s+128);
max_socket = s+128;
}
}
if (bitarray_is_set(open_sockets, s)) {
log_warn(LD_BUG, "I thought that %d was already open, but socket() just "
"gave it to me!", s);
}
bitarray_set(open_sockets, s);
}
#else
#define mark_socket_open(s) STMT_NIL
#endif
/** As socket(), but counts the number of open sockets. */
int
tor_open_socket(int domain, int type, int protocol)
{
int s = socket(domain, type, protocol);
if (s >= 0) {
++n_sockets_open;
mark_socket_open(s);
}
return s;
}
/** As socket(), but counts the number of open sockets. */
int
tor_accept_socket(int sockfd, struct sockaddr *addr, socklen_t *len)
{
int s = accept(sockfd, addr, len);
if (s >= 0) {
++n_sockets_open;
mark_socket_open(s);
}
return s;
}
/** Return the number of sockets we currently have opened. */
int
get_n_open_sockets(void)
{
return n_sockets_open;
}
/** Turn <b>socket</b> into a nonblocking socket.
*/
void
set_socket_nonblocking(int socket)
{
#if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
unsigned long nonblocking = 1;
ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
#else
fcntl(socket, F_SETFL, O_NONBLOCK);
#endif
}
/**
* Allocate a pair of connected sockets. (Like socketpair(family,
* type,protocol,fd), but works on systems that don't have
* socketpair.)
*
* Currently, only (AF_UNIX, SOCK_STREAM, 0) sockets are supported.
*
* Note that on systems without socketpair, this call will fail if
* localhost is inaccessible (for example, if the networking
* stack is down). And even if it succeeds, the socket pair will not
* be able to read while localhost is down later (the socket pair may
* even close, depending on OS-specific timeouts).
*
* Returns 0 on success and -errno on failure; do not rely on the value
* of errno or WSAGetLastError().
**/
/* It would be nicer just to set errno, but that won't work for windows. */
int
tor_socketpair(int family, int type, int protocol, int fd[2])
{
//don't use win32 socketpairs (they are always bad)
#if defined(HAVE_SOCKETPAIR) && !defined(MS_WINDOWS)
int r;
r = socketpair(family, type, protocol, fd);
if (r == 0) {
if (fd[0] >= 0) {
++n_sockets_open;
mark_socket_open(fd[0]);
}
if (fd[1] >= 0) {
++n_sockets_open;
mark_socket_open(fd[1]);
}
}
return r < 0 ? -errno : r;
#elif defined(USE_BSOCKETS)
return bsocketpair(family, type, protocol, fd);
#else
/* This socketpair does not work when localhost is down. So
* it's really not the same thing at all. But it's close enough
* for now, and really, when localhost is down sometimes, we
* have other problems too.
*/
int listener = -1;
int connector = -1;
int acceptor = -1;
struct sockaddr_in listen_addr;
struct sockaddr_in connect_addr;
int size;
int saved_errno = -1;
if (protocol
#ifdef AF_UNIX
|| family != AF_UNIX
#endif
) {
#ifdef MS_WINDOWS
return -WSAEAFNOSUPPORT;
#else
return -EAFNOSUPPORT;
#endif
}
if (!fd) {
return -EINVAL;
}
listener = tor_open_socket(AF_INET, type, 0);
if (listener < 0)
return -tor_socket_errno(-1);
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
listen_addr.sin_port = 0; /* kernel chooses port. */
if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
== -1)
goto tidy_up_and_fail;
if (listen(listener, 1) == -1)
goto tidy_up_and_fail;
connector = tor_open_socket(AF_INET, type, 0);
if (connector < 0)
goto tidy_up_and_fail;
/* We want to find out the port number to connect to. */
size = sizeof(connect_addr);
if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
goto tidy_up_and_fail;
if (size != sizeof (connect_addr))
goto abort_tidy_up_and_fail;
if (connect(connector, (struct sockaddr *) &connect_addr,
sizeof(connect_addr)) == -1)
goto tidy_up_and_fail;
size = sizeof(listen_addr);
acceptor = tor_accept_socket(listener,
(struct sockaddr *) &listen_addr, &size);
if (acceptor < 0)
goto tidy_up_and_fail;
if (size != sizeof(listen_addr))
goto abort_tidy_up_and_fail;
tor_close_socket(listener);
/* Now check we are talking to ourself by matching port and host on the
two sockets. */
if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
goto tidy_up_and_fail;
if (size != sizeof (connect_addr)
|| listen_addr.sin_family != connect_addr.sin_family
|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
|| listen_addr.sin_port != connect_addr.sin_port) {
goto abort_tidy_up_and_fail;
}
fd[0] = connector;
fd[1] = acceptor;
return 0;
abort_tidy_up_and_fail:
#ifdef MS_WINDOWS
saved_errno = WSAECONNABORTED;
#else
saved_errno = ECONNABORTED; /* I hope this is portable and appropriate. */
#endif
tidy_up_and_fail:
if (saved_errno < 0)
saved_errno = errno;
if (listener != -1)
tor_close_socket(listener);
if (connector != -1)
tor_close_socket(connector);
if (acceptor != -1)
tor_close_socket(acceptor);
return -saved_errno;
#endif
}
#define ULIMIT_BUFFER 32 /* keep 32 extra fd's beyond _ConnLimit */
/** Learn the maximum allowed number of file descriptors. (Some systems
* have a low soft limit.
*
* We compute this by finding the largest number that we can use.
* If we can't find a number greater than or equal to <b>limit</b>,
* then we fail: return -1.
*
* Otherwise, return 0 and store the maximum we found inside <b>max_out</b>.*/
int
set_max_file_descriptors(rlim_t limit, int *max_out)
{
/* Define some maximum connections values for systems where we cannot
* automatically determine a limit. Re Cygwin, see
* http://archives.seul.org/or/talk/Aug-2006/msg00210.html
* For an iPhone, 9999 should work. For Windows and all other unknown
* systems we use 15000 as the default. */
#ifndef HAVE_GETRLIMIT
#if defined(CYGWIN) || defined(__CYGWIN__)
const char *platform = "Cygwin";
const unsigned long MAX_CONNECTIONS = 3200;
#elif defined(IPHONE)
const char *platform = "iPhone";
const unsigned long MAX_CONNECTIONS = 9999;
#elif defined(MS_WINDOWS)
const char *platform = "Windows";
const unsigned long MAX_CONNECTIONS = 15000;
#else
const char *platform = "unknown platforms with no getrlimit()";
const unsigned long MAX_CONNECTIONS = 15000;
#endif
log_fn(LOG_INFO, LD_NET,
"This platform is missing getrlimit(). Proceeding.");
if (limit > MAX_CONNECTIONS) {
log_warn(LD_CONFIG,
"We do not support more than %lu file descriptors "
"on %s. Tried to raise to %lu.",
(unsigned long)MAX_CONNECTIONS, platform, (unsigned long)limit);
return -1;
}
limit = MAX_CONNECTIONS;
#else /* HAVE_GETRLIMIT */
struct rlimit rlim;
tor_assert(limit > 0);
if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
log_warn(LD_NET, "Could not get maximum number of file descriptors: %s",
strerror(errno));
return -1;
}
if (rlim.rlim_max < limit) {
log_warn(LD_CONFIG,"We need %lu file descriptors available, and we're "
"limited to %lu. Please change your ulimit -n.",
(unsigned long)limit, (unsigned long)rlim.rlim_max);
return -1;
}
if (rlim.rlim_max > rlim.rlim_cur) {
log_info(LD_NET,"Raising max file descriptors from %lu to %lu.",
(unsigned long)rlim.rlim_cur, (unsigned long)rlim.rlim_max);
}
rlim.rlim_cur = rlim.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
int bad = 1;
#ifdef OPEN_MAX
if (errno == EINVAL && OPEN_MAX < rlim.rlim_cur) {
/* On some platforms, OPEN_MAX is the real limit, and getrlimit() is
* full of nasty lies. I'm looking at you, OSX 10.5.... */
rlim.rlim_cur = OPEN_MAX;
if (setrlimit(RLIMIT_NOFILE, &rlim) == 0) {
if (rlim.rlim_cur < (rlim_t)limit) {
log_warn(LD_CONFIG, "We are limited to %lu file descriptors by "
"OPEN_MAX, and ConnLimit is %lu. Changing ConnLimit; sorry.",
(unsigned long)OPEN_MAX, (unsigned long)limit);
} else {
log_info(LD_CONFIG, "Dropped connection limit to OPEN_MAX (%lu); "
"Apparently, %lu was too high and rlimit lied to us.",
(unsigned long)OPEN_MAX, (unsigned long)rlim.rlim_max);
}
bad = 0;
}
}
#endif /* OPEN_MAX */
if (bad) {
log_warn(LD_CONFIG,"Couldn't set maximum number of file descriptors: %s",
strerror(errno));
return -1;
}
}
/* leave some overhead for logs, etc, */
limit = rlim.rlim_cur;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -