diff options
112 files changed, 912 insertions, 416 deletions
diff --git a/changes/ticket26526 b/changes/ticket26526 new file mode 100644 index 0000000000..447b581df8 --- /dev/null +++ b/changes/ticket26526 @@ -0,0 +1,4 @@ + o Code simplification and refactoring: + - Utility functions that can perform a DNS lookup are now wholly + separated from those that can't, in separate headers and C + modules. Closes ticket 26526. diff --git a/changes/ticket26526_extra b/changes/ticket26526_extra new file mode 100644 index 0000000000..5495962ff7 --- /dev/null +++ b/changes/ticket26526_extra @@ -0,0 +1,3 @@ + o Minor features (tor-resolve): + - The tor-resolve utility can now be used with IPv6 SOCKS proxies. + Side-effect of the refactoring for ticket 26526. diff --git a/doc/HACKING/CodeStructure.md b/doc/HACKING/CodeStructure.md new file mode 100644 index 0000000000..736d6cd484 --- /dev/null +++ b/doc/HACKING/CodeStructure.md @@ -0,0 +1,129 @@ + +TODO: revise this to talk about how things are, rather than how things +have changed. + +TODO: Make this into good markdown. + + + +For quite a while now, the program "tor" has been built from source +code in just two directories: src/common and src/or. + +This has become more-or-less untenable, for a few reasons -- most +notably of which is that it has led our code to become more +spaghetti-ish than I can endorse with a clean conscience. + +So to fix that, we've gone and done a huge code movement in our git +master branch, which will land in a release once Tor 0.3.5.1-alpha is +out. + +Here's what we did: + + * src/common has been turned into a set of static libraries. These +all live in the "src/lib/*" directories. The dependencies between +these libraries should have no cycles. The libraries are: + + arch -- Headers to handle architectural differences + cc -- headers to handle differences among compilers + compress -- wraps zlib, zstd, lzma + container -- high-level container types + crypt_ops -- Cryptographic operations. Planning to split this into +a higher and lower level library + ctime -- Operations that need to run in constant-time. (Properly, +data-invariant time) + defs -- miscelaneous definitions needed throughout Tor. + encoding -- transforming one data type into another, and various +data types into strings. + err -- lowest-level error handling, in cases where we can't use +the logs because something that the logging system needs has broken. + evloop -- Generic event-loop handling logic + fdio -- Low-level IO wrapper functions for file descriptors. + fs -- Operations on the filesystem + intmath -- low-level integer math and misc bit-twiddling hacks + lock -- low-level locking code + log -- Tor's logging module. This library sits roughly halfway up +the library dependency diagram, since everything it depends on has to +be carefully crafted to *not* log. + malloc -- Low-level wrappers for the platform memory allocation functions. + math -- Higher-level mathematical functions, and floating-point math + memarea -- An arena allocator + meminfo -- Functions for querying the current process's memory +status and resources + net -- Networking compatibility and convenience code + osinfo -- Querying information about the operating system + process -- Launching and querying the status of other processes + sandbox -- Backend for the linux seccomp2 sandbox + smartlist_core -- The lowest-level of the smartlist_t data type. +Separated from the rest of the containers library because the logging +subsystem depends on it. + string -- Compatibility and convenience functions for manipulating +C strings. + term -- Terminal-related functions (currently limited to a getpass +function). + testsupport -- Macros for mocking, unit tests, etc. + thread -- Higher-level thread compatibility code + time -- Higher-level time management code, including format +conversions and monotonic time + tls -- Our wrapper around our TLS library + trace -- Formerly src/trace -- a generic event tracing API + wallclock -- Low-level time code, used by the log module. + + * To ensure that the dependency graph in src/common remains under +control, there is a tool that you can run called "make +check-includes". It verifies that each module in Tor only includes +the headers that it is permitted to include, using a per-directory +".may_include" file. + + * The src/or/or.h header has been split into numerous smaller +headers. Notably, many important structures are now declared in a +header called foo_st.h, where "foo" is the name of the structure. + + * The src/or directory, which had most of Tor's code, had been split +up into several directories. This is still a work in progress: This +code has not itself been refactored, and its dependency graph is still +a tangled web. I hope we'll be working on that over the coming +releases, but it will take a while to do. + + The new top-level source directories are: + + src/core -- Code necessary to actually perform or use onion routing. + src/feature -- Code used only by some onion routing +configurations, or only for a special purpose. + src/app -- Top-level code to run, invoke, and configure the +lower-level code + + The new second-level source directories are: + src/core/crypto -- High-level cryptographic protocols used in Tor + src/core/mainloop -- Tor's event loop, connection-handling, and +traffic-routing code. + src/core/or -- Parts related to handling onion routing itself + src/core/proto -- support for encoding and decoding different +wire protocols + + src/feature/api -- Support for making Tor embeddable + src/feature/client -- Functionality which only Tor clients need + src/feature/control -- Controller implementation + src/feature/dirauth -- Directory authority + src/feature/dircache -- Directory cache + src/feature/dirclient -- Directory client + src/feature/dircommon -- Shared code between the other directory modules + src/feature/hibernate -- Hibernating when Tor is out of bandwidth +or shutting down + src/feature/hs -- v3 onion service implementation + src/feature/hs_common -- shared code between both onion service +implementations + src/feature/nodelist -- storing and accessing the list of relays on +the network. + src/feature/relay -- code that only relay servers and exit servers need. + src/feature/rend -- v2 onion service implementation + src/feature/stats -- statistics and history + + src/app/config -- configuration and state for Tor + src/app/main -- Top-level functions to invoke the rest or Tor. + + * The "tor" executable is now built in src/app/tor rather than src/or/tor. + + * There are more static libraries than before that you need to build +into your application if you want to embed Tor. Rather than +maintaining this list yourself, I recommend that you run "make +show-libs" to have Tor emit a list of what you need to link. diff --git a/doc/HACKING/CodingStandards.md b/doc/HACKING/CodingStandards.md index b830ecea93..4f229348e4 100644 --- a/doc/HACKING/CodingStandards.md +++ b/doc/HACKING/CodingStandards.md @@ -200,8 +200,8 @@ We have some wrapper functions like `tor_malloc`, `tor_free`, `tor_strdup`, and always succeed or exit.) You can get a full list of the compatibility functions that Tor provides by -looking through `src/common/util*.h` and `src/common/compat*.h`. You can see the -available containers in `src/common/containers*.h`. You should probably +looking through `src/lib/*/*.h`. You can see the +available containers in `src/lib/containers/*.h`. You should probably familiarize yourself with these modules before you write too much code, or else you'll wind up reinventing the wheel. @@ -214,6 +214,24 @@ We don't call `memcmp()` directly. Use `fast_memeq()`, `fast_memneq()`, Also see a longer list of functions to avoid in: https://people.torproject.org/~nickm/tor-auto/internal/this-not-that.html +What code can use what other code? +---------------------------------- + +We're trying to simplify Tor's structure over time. In the long run, we want +Tor to be structured as a set of modules with *no circular dependencies*. + +This property is currently provided by the modules in src/lib, but not +throughout the rest of Tor. In general, higher-level libraries may use +lower-level libraries, but never the reverse. + +To prevent new circular dependencies from landing, we have a tool that +you can invoke with `make check-includes`, and which is run +automatically as part of `make check`. This tool will verify that, for +every source directory with a `.may_include` file, no local headers are +included except those specifically permitted by the `.may_include` file. +When editing one of these files, please make sure that you are not +introducing any cycles into Tor's dependency graph. + Floating point math is hard --------------------------- diff --git a/doc/HACKING/Module.md b/doc/HACKING/Module.md index 6684e258df..9cf36090b4 100644 --- a/doc/HACKING/Module.md +++ b/doc/HACKING/Module.md @@ -96,8 +96,8 @@ There are couples of "rules" you want to follow: filename as the one in the module. For example, this is a bad idea and should never be done: - - `src/or/shared_random.c` - - `src/or/dirauth/shared_random.c` + - `src/feature/dirclient/shared_random.c` + - `src/feature/dirauth/shared_random.c` * When you include headers from the module, **always** use the full module path in your statement. Example: diff --git a/doc/include.am b/doc/include.am index e429d05a49..0e533c1b3b 100644 --- a/doc/include.am +++ b/doc/include.am @@ -36,6 +36,7 @@ EXTRA_DIST+= doc/asciidoc-helper.sh \ doc/HACKING/README.1st.md \ doc/HACKING/CodingStandards.md \ doc/HACKING/CodingStandardsRust.md \ + doc/HACKING/CodeStructure.md \ doc/HACKING/Fuzzing.md \ doc/HACKING/GettingStarted.md \ doc/HACKING/GettingStartedRust.md \ diff --git a/src/app/config/config.c b/src/app/config/config.c index 665732ea56..1be1803f29 100644 --- a/src/app/config/config.c +++ b/src/app/config/config.c @@ -106,6 +106,7 @@ #include "feature/client/transports.h" #include "feature/relay/ext_orport.h" #include "feature/dircommon/voting_schedule.h" +#include "lib/net/resolve.h" #ifdef _WIN32 #include <shlobj.h> #endif @@ -6459,26 +6460,17 @@ parse_dir_authority_line(const char *line, dirinfo_type_t required_type, addrport = smartlist_get(items, 0); smartlist_del_keeporder(items, 0); - const char *addrport_sep = strchr(addrport, ':'); - if (!addrport_sep) { - log_warn(LD_CONFIG, "Error parsing DirAuthority address '%s' " - "(':' not found)", addrport); + if (tor_addr_port_split(LOG_WARN, addrport, &address, &dir_port) < 0) { + log_warn(LD_CONFIG, "Error parsing DirAuthority address '%s'.", addrport); goto err; } - address = tor_strndup(addrport, addrport_sep - addrport); if (!string_is_valid_ipv4_address(address)) { log_warn(LD_CONFIG, "Error parsing DirAuthority address '%s' " "(invalid IPv4 address)", address); goto err; } - tor_free(address); - - if (addr_port_lookup(LOG_WARN, addrport, &address, NULL, &dir_port)<0) { - log_warn(LD_CONFIG, "Error parsing DirAuthority address '%s'", addrport); - goto err; - } if (!dir_port) { log_warn(LD_CONFIG, "Missing port in DirAuthority address '%s'",addrport); goto err; diff --git a/src/app/config/confparse.h b/src/app/config/confparse.h index cbd2ea88e2..570428c904 100644 --- a/src/app/config/confparse.h +++ b/src/app/config/confparse.h @@ -4,6 +4,12 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file confparse.h + * + * \brief Header for confparse.c. + */ + #ifndef TOR_CONFPARSE_H #define TOR_CONFPARSE_H diff --git a/src/app/config/or_options_st.h b/src/app/config/or_options_st.h index 0c0c5d32bb..627b39aea3 100644 --- a/src/app/config/or_options_st.h +++ b/src/app/config/or_options_st.h @@ -4,6 +4,12 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file or_options_st.h + * + * \brief The or_options_t structure, which represents Tor's configuration. + */ + #ifndef TOR_OR_OPTIONS_ST_H #define TOR_OR_OPTIONS_ST_H diff --git a/src/app/config/or_state_st.h b/src/app/config/or_state_st.h index f1d5f981f1..d95df6236b 100644 --- a/src/app/config/or_state_st.h +++ b/src/app/config/or_state_st.h @@ -4,6 +4,12 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file or_state_t + * + * \brief The or_state_t structure, which represents Tor's state file. + */ + #ifndef TOR_OR_STATE_ST_H #define TOR_OR_STATE_ST_H diff --git a/src/app/config/statefile.c b/src/app/config/statefile.c index 8eeef45026..656dc2eec3 100644 --- a/src/app/config/statefile.c +++ b/src/app/config/statefile.c @@ -43,6 +43,7 @@ #include "lib/sandbox/sandbox.h" #include "app/config/statefile.h" #include "lib/encoding/confline.h" +#include "lib/net/resolve.h" #include "app/config/or_state_st.h" diff --git a/src/app/config/statefile.h b/src/app/config/statefile.h index e996d5b6e6..6433affa62 100644 --- a/src/app/config/statefile.h +++ b/src/app/config/statefile.h @@ -4,6 +4,12 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file statefile.h + * + * \brief Header for statefile.c + */ + #ifndef TOR_STATEFILE_H #define TOR_STATEFILE_H diff --git a/src/core/mainloop/main.c b/src/core/mainloop/main.c index c5773ddfc1..048397a2da 100644 --- a/src/core/mainloop/main.c +++ b/src/core/mainloop/main.c @@ -116,6 +116,7 @@ #include "lib/sandbox/sandbox.h" #include "lib/fs/lockfile.h" #include "lib/net/buffers_net.h" +#include "lib/net/resolve.h" #include "lib/tls/tortls.h" #include "lib/evloop/compat_libevent.h" #include "lib/encoding/confline.h" diff --git a/src/core/or/or.h b/src/core/or/or.h index 6edfd21dfb..2e419eefd5 100644 --- a/src/core/or/or.h +++ b/src/core/or/or.h @@ -49,9 +49,7 @@ #include "lib/log/util_bug.h" #include "lib/malloc/util_malloc.h" #include "lib/net/address.h" -#include "lib/net/ipv4.h" -#include "lib/net/ipv6.h" -#include "lib/net/resolve.h" +#include "lib/net/inaddr.h" #include "lib/net/socket.h" #include "lib/string/compat_ctype.h" #include "lib/string/compat_string.h" diff --git a/src/feature/nodelist/routerlist.c b/src/feature/nodelist/routerlist.c index 12226fee64..8b54329da9 100644 --- a/src/feature/nodelist/routerlist.c +++ b/src/feature/nodelist/routerlist.c @@ -123,6 +123,7 @@ #include "lib/sandbox/sandbox.h" #include "feature/nodelist/torcert.h" #include "lib/math/fp.h" +#include "lib/net/resolve.h" #include "feature/dirauth/dirvote.h" #include "feature/dirauth/mode.h" diff --git a/src/feature/rend/rendservice.c b/src/feature/rend/rendservice.c index da4a98b3d1..1a99bd56ed 100644 --- a/src/feature/rend/rendservice.c +++ b/src/feature/rend/rendservice.c @@ -37,6 +37,7 @@ #include "feature/nodelist/routerparse.h" #include "feature/nodelist/routerset.h" #include "lib/encoding/confline.h" +#include "lib/net/resolve.h" #include "core/or/cpath_build_state_st.h" #include "core/or/crypt_path_st.h" diff --git a/src/lib/cc/compat_compiler.h b/src/lib/cc/compat_compiler.h index 0f1acc381a..d45316b241 100644 --- a/src/lib/cc/compat_compiler.h +++ b/src/lib/cc/compat_compiler.h @@ -3,6 +3,12 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file compat_compiler.h + * \brief Utility macros to handle different features and behavior in different + * compilers. + **/ + #ifndef TOR_COMPAT_COMPILER_H #define TOR_COMPAT_COMPILER_H diff --git a/src/lib/cc/torint.h b/src/lib/cc/torint.h index 91db25833b..b97fc8d975 100644 --- a/src/lib/cc/torint.h +++ b/src/lib/cc/torint.h @@ -5,18 +5,13 @@ /** * \file torint.h - * \brief Header file to define uint32_t and friends + * + * \brief Integer definitions used throughout Tor. **/ #ifndef TOR_TORINT_H #define TOR_TORINT_H -/** - * \file torint.h - * - * \brief Integer definitions used throughout Tor. - **/ - #include "orconfig.h" #include <stdint.h> diff --git a/src/lib/crypt_ops/crypto_curve25519.h b/src/lib/crypt_ops/crypto_curve25519.h index acb36fde3b..1bab4a4197 100644 --- a/src/lib/crypt_ops/crypto_curve25519.h +++ b/src/lib/crypt_ops/crypto_curve25519.h @@ -1,6 +1,11 @@ /* Copyright (c) 2012-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file crypto_curve25519.h + * \brief Header for crypto_curve25519.c + **/ + #ifndef TOR_CRYPTO_CURVE25519_H #define TOR_CRYPTO_CURVE25519_H diff --git a/src/lib/crypt_ops/crypto_ed25519.h b/src/lib/crypt_ops/crypto_ed25519.h index 5ecd4530d8..03b3afe206 100644 --- a/src/lib/crypt_ops/crypto_ed25519.h +++ b/src/lib/crypt_ops/crypto_ed25519.h @@ -1,6 +1,11 @@ /* Copyright (c) 2012-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file crypto_ed25519.h + * \brief Header for crypto_ed25519.c + **/ + #ifndef TOR_CRYPTO_ED25519_H #define TOR_CRYPTO_ED25519_H diff --git a/src/lib/crypt_ops/crypto_format.h b/src/lib/crypt_ops/crypto_format.h index 4a29b07b3b..a246071458 100644 --- a/src/lib/crypt_ops/crypto_format.h +++ b/src/lib/crypt_ops/crypto_format.h @@ -4,6 +4,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file crypto_format.h + * \brief Header for crypto_format.c + **/ + #ifndef TOR_CRYPTO_FORMAT_H #define TOR_CRYPTO_FORMAT_H diff --git a/src/lib/crypt_ops/crypto_openssl_mgt.c b/src/lib/crypt_ops/crypto_openssl_mgt.c index d1affa7258..01de6a9d9e 100644 --- a/src/lib/crypt_ops/crypto_openssl_mgt.c +++ b/src/lib/crypt_ops/crypto_openssl_mgt.c @@ -5,7 +5,7 @@ /* See LICENSE for licensing information */ /** - * \file crypto_openssl.c + * \file crypto_openssl_mgt.c * * \brief Block of functions related to operations from OpenSSL. **/ diff --git a/src/lib/crypt_ops/crypto_openssl_mgt.h b/src/lib/crypt_ops/crypto_openssl_mgt.h index 8251f65ecf..a2c53302e1 100644 --- a/src/lib/crypt_ops/crypto_openssl_mgt.h +++ b/src/lib/crypt_ops/crypto_openssl_mgt.h @@ -5,9 +5,9 @@ /* See LICENSE for licensing information */ /** - * \file crypto_openssl.h + * \file crypto_openssl_mgt.h * - * \brief Headers for crypto_openssl.c + * \brief Headers for crypto_openssl_mgt.c **/ #ifndef TOR_CRYPTO_OPENSSL_H diff --git a/src/lib/defs/dh_sizes.h b/src/lib/defs/dh_sizes.h index b60957281c..a02ffc5281 100644 --- a/src/lib/defs/dh_sizes.h +++ b/src/lib/defs/dh_sizes.h @@ -4,6 +4,15 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file dh_sizes.h + + * \brief Definitions for sizes of Diffie-Hellman groups elements in Z_p. + * + * Tor uses these definitions throughout its codebase, even in parts that + * don't actually do any Diffie-Hellman calculations. + **/ + #ifndef TOR_DH_SIZES_H #define TOR_DH_SIZES_H diff --git a/src/lib/defs/x25519_sizes.h b/src/lib/defs/x25519_sizes.h index adaaab8c4d..d8ada46b97 100644 --- a/src/lib/defs/x25519_sizes.h +++ b/src/lib/defs/x25519_sizes.h @@ -4,6 +4,15 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file x25519_sizes.h + + * \brief Definitions for sizes of x25519 keys and elements. + * + * Tor uses these definitions throughout its codebase, even in parts that + * don't actually do any x25519 calculations. + **/ + #ifndef TOR_X25519_SIZES_H #define TOR_X25519_SIZES_H diff --git a/src/lib/evloop/compat_libevent.h b/src/lib/evloop/compat_libevent.h index 0a50cfa667..7a5469047c 100644 --- a/src/lib/evloop/compat_libevent.h +++ b/src/lib/evloop/compat_libevent.h @@ -1,6 +1,11 @@ /* Copyright (c) 2009-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file compat_libevent.h + * \brief Header for compat_libevent.c + **/ + #ifndef TOR_COMPAT_LIBEVENT_H #define TOR_COMPAT_LIBEVENT_H diff --git a/src/lib/evloop/timers.h b/src/lib/evloop/timers.h index 2348c7b7c1..4ffed1b458 100644 --- a/src/lib/evloop/timers.h +++ b/src/lib/evloop/timers.h @@ -1,6 +1,11 @@ /* Copyright (c) 2016-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file timers.h + * \brief Header for timers.c + **/ + #ifndef TOR_TIMERS_H #define TOR_TIMERS_H @@ -28,4 +33,3 @@ STATIC void timers_run_pending(void); #endif #endif /* !defined(TOR_TIMERS_H) */ - diff --git a/src/lib/evloop/token_bucket.h b/src/lib/evloop/token_bucket.h index 787317fa1f..f004358f47 100644 --- a/src/lib/evloop/token_bucket.h +++ b/src/lib/evloop/token_bucket.h @@ -2,8 +2,8 @@ /* See LICENSE for licensing information */ /** - * \file token_bucket_rw.h - * \brief Headers for token_bucket_rw.c + * \file token_bucket.h + * \brief Headers for token_bucket.c **/ #ifndef TOR_TOKEN_BUCKET_H @@ -115,4 +115,3 @@ STATIC uint32_t rate_per_sec_to_rate_per_step(uint32_t rate); #endif #endif /* TOR_TOKEN_BUCKET_H */ - diff --git a/src/lib/evloop/workqueue.h b/src/lib/evloop/workqueue.h index 4e5c424be6..da292d1f05 100644 --- a/src/lib/evloop/workqueue.h +++ b/src/lib/evloop/workqueue.h @@ -1,6 +1,11 @@ /* Copyright (c) 2013-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file workqueue.h + * \brief Header for workqueue.c + **/ + #ifndef TOR_WORKQUEUE_H #define TOR_WORKQUEUE_H diff --git a/src/lib/log/escape.c b/src/lib/log/escape.c index 7561710309..b6b20183ba 100644 --- a/src/lib/log/escape.c +++ b/src/lib/log/escape.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file escape.c + * \brief Escape untrusted strings before sending them to the log. + **/ + #include "lib/log/escape.h" #include "lib/log/util_bug.h" #include "lib/string/compat_ctype.h" diff --git a/src/lib/log/escape.h b/src/lib/log/escape.h index 5d2e79d6c2..f47e7e004d 100644 --- a/src/lib/log/escape.h +++ b/src/lib/log/escape.h @@ -4,6 +4,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file escape.h + * \brief Header for escape.c + **/ + #ifndef TOR_ESCAPE_H #define TOR_ESCAPE_H diff --git a/src/lib/log/ratelim.c b/src/lib/log/ratelim.c index 677c499110..fba702d7f0 100644 --- a/src/lib/log/ratelim.c +++ b/src/lib/log/ratelim.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file ratelim.c + * \brief Summarize similar messages that would otherwise flood the logs. + **/ + #include "lib/log/ratelim.h" #include "lib/malloc/util_malloc.h" #include "lib/string/printf.h" diff --git a/src/lib/log/ratelim.h b/src/lib/log/ratelim.h index 4ee6c5fed4..d423e10b85 100644 --- a/src/lib/log/ratelim.h +++ b/src/lib/log/ratelim.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file ratelim.h + * \brief Summarize similar messages that would otherwise flood the logs. + **/ + #ifndef TOR_RATELIM_H #define TOR_RATELIM_H diff --git a/src/lib/log/win32err.c b/src/lib/log/win32err.c index 4586c23c84..6603ed4648 100644 --- a/src/lib/log/win32err.c +++ b/src/lib/log/win32err.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file win32err.c + * \brief Convert windows error codes to useful C strings. + **/ + #ifdef _WIN32 #include "orconfig.h" #include "lib/log/win32err.h" diff --git a/src/lib/log/win32err.h b/src/lib/log/win32err.h index 61d3af57dd..92958c9879 100644 --- a/src/lib/log/win32err.h +++ b/src/lib/log/win32err.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file win32err.h + * \brief Header for win32err.c + **/ + #ifndef TOR_WIN32ERR_H #define TOR_WIN32ERR_H diff --git a/src/lib/net/address.c b/src/lib/net/address.c index f3eddca7bb..3b624da096 100644 --- a/src/lib/net/address.c +++ b/src/lib/net/address.c @@ -6,6 +6,9 @@ /** * \file address.c * \brief Functions to use and manipulate the tor_addr_t structure. + * + * This module doesn't have any support for the libc resolver: that is all in + * resolve.c. **/ #define ADDRESS_PRIVATE @@ -37,13 +40,12 @@ #include "lib/net/address.h" #include "lib/net/socket.h" -#include "lib/net/resolve.h" #include "lib/container/smartlist.h" #include "lib/ctime/di_ops.h" #include "lib/log/torlog.h" #include "lib/log/escape.h" #include "lib/malloc/util_malloc.h" -#include "lib/net/ipv4.h" +#include "lib/net/inaddr.h" #include "lib/string/compat_ctype.h" #include "lib/string/compat_string.h" #include "lib/string/parse_int.h" @@ -234,127 +236,6 @@ tor_addr_make_null(tor_addr_t *a, sa_family_t family) a->family = family; } -/** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set - * *<b>addr</b> to the proper IP address and family. The <b>family</b> - * argument (which must be AF_INET, AF_INET6, or AF_UNSPEC) declares a - * <i>preferred</i> family, though another one may be returned if only one - * family is implemented for this address. - * - * Return 0 on success, -1 on failure; 1 on transient failure. - */ -MOCK_IMPL(int, -tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr)) -{ - /* Perhaps eventually this should be replaced by a tor_getaddrinfo or - * something. - */ - struct in_addr iaddr; - struct in6_addr iaddr6; - tor_assert(name); - tor_assert(addr); - tor_assert(family == AF_INET || family == AF_INET6 || family == AF_UNSPEC); - if (!*name) { - /* Empty address is an error. */ - return -1; - } else if (tor_inet_pton(AF_INET, name, &iaddr)) { - /* It's an IPv4 IP. */ - if (family == AF_INET6) - return -1; - tor_addr_from_in(addr, &iaddr); - return 0; - } else if (tor_inet_pton(AF_INET6, name, &iaddr6)) { - if (family == AF_INET) - return -1; - tor_addr_from_in6(addr, &iaddr6); - return 0; - } else { -#ifdef HAVE_GETADDRINFO - int err; - struct addrinfo *res=NULL, *res_p; - struct addrinfo *best=NULL; - struct addrinfo hints; - int result = -1; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = family; - hints.ai_socktype = SOCK_STREAM; - err = tor_getaddrinfo(name, NULL, &hints, &res); - /* The check for 'res' here shouldn't be necessary, but it makes static - * analysis tools happy. */ - if (!err && res) { - best = NULL; - for (res_p = res; res_p; res_p = res_p->ai_next) { - if (family == AF_UNSPEC) { - if (res_p->ai_family == AF_INET) { - best = res_p; - break; - } else if (res_p->ai_family == AF_INET6 && !best) { - best = res_p; - } - } else if (family == res_p->ai_family) { - best = res_p; - break; - } - } - if (!best) - best = res; - if (best->ai_family == AF_INET) { - tor_addr_from_in(addr, - &((struct sockaddr_in*)best->ai_addr)->sin_addr); - result = 0; - } else if (best->ai_family == AF_INET6) { - tor_addr_from_in6(addr, - &((struct sockaddr_in6*)best->ai_addr)->sin6_addr); - result = 0; - } - tor_freeaddrinfo(res); - return result; - } - return (err == EAI_AGAIN) ? 1 : -1; -#else /* !(defined(HAVE_GETADDRINFO)) */ - struct hostent *ent; - int err; -#ifdef HAVE_GETHOSTBYNAME_R_6_ARG - char buf[2048]; - struct hostent hostent; - int r; - r = gethostbyname_r(name, &hostent, buf, sizeof(buf), &ent, &err); -#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) - char buf[2048]; - struct hostent hostent; - ent = gethostbyname_r(name, &hostent, buf, sizeof(buf), &err); -#elif defined(HAVE_GETHOSTBYNAME_R_3_ARG) - struct hostent_data data; - struct hostent hent; - memset(&data, 0, sizeof(data)); - err = gethostbyname_r(name, &hent, &data); - ent = err ? NULL : &hent; -#else - ent = gethostbyname(name); -#ifdef _WIN32 - err = WSAGetLastError(); -#else - err = h_errno; -#endif -#endif /* defined(HAVE_GETHOSTBYNAME_R_6_ARG) || ... */ - if (ent) { - if (ent->h_addrtype == AF_INET) { - tor_addr_from_in(addr, (struct in_addr*) ent->h_addr); - } else if (ent->h_addrtype == AF_INET6) { - tor_addr_from_in6(addr, (struct in6_addr*) ent->h_addr); - } else { - tor_assert(0); // LCOV_EXCL_LINE: gethostbyname() returned bizarre type - } - return 0; - } -#ifdef _WIN32 - return (err == WSATRY_AGAIN) ? 1 : -1; -#else - return (err == TRY_AGAIN) ? 1 : -1; -#endif -#endif /* defined(HAVE_GETADDRINFO) */ - } -} - /** Return true iff <b>ip</b> is an IP reserved to localhost or local networks * in RFC1918 or RFC4193 or RFC4291. (fec0::/10, deprecated by RFC3879, is * also treated as internal for now.) @@ -1324,64 +1205,6 @@ tor_addr_parse(tor_addr_t *addr, const char *src) return result; } -/** Parse an address or address-port combination from <b>s</b>, resolve the - * address as needed, and put the result in <b>addr_out</b> and (optionally) - * <b>port_out</b>. Return 0 on success, negative on failure. */ -int -tor_addr_port_lookup(const char *s, tor_addr_t *addr_out, uint16_t *port_out) -{ - const char *port; - tor_addr_t addr; - uint16_t portval; - char *tmp = NULL; - - tor_assert(s); - tor_assert(addr_out); - - s = eat_whitespace(s); - - if (*s == '[') { - port = strstr(s, "]"); - if (!port) - goto err; - tmp = tor_strndup(s+1, port-(s+1)); - port = port+1; - if (*port == ':') - port++; - else - port = NULL; - } else { - port = strchr(s, ':'); - if (port) - tmp = tor_strndup(s, port-s); - else - tmp = tor_strdup(s); - if (port) - ++port; - } - - if (tor_addr_lookup(tmp, AF_UNSPEC, &addr) != 0) - goto err; - tor_free(tmp); - - if (port) { - portval = (int) tor_parse_long(port, 10, 1, 65535, NULL, NULL); - if (!portval) - goto err; - } else { - portval = 0; - } - - if (port_out) - *port_out = portval; - tor_addr_copy(addr_out, &addr); - - return 0; - err: - tor_free(tmp); - return -1; -} - #ifdef _WIN32 typedef ULONG (WINAPI *GetAdaptersAddresses_fn_t)( ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG); @@ -1927,7 +1750,7 @@ tor_addr_port_split(int severity, const char *addrport, tor_assert(addrport); tor_assert(address_out); tor_assert(port_out); - /* We need to check for IPv6 manually because addr_port_lookup() doesn't + /* We need to check for IPv6 manually because the logic below doesn't * do a good job on IPv6 addresses that lack a port. */ if (tor_addr_parse(&a_tmp, addrport) == AF_INET6) { *port_out = 0; @@ -1935,30 +1758,11 @@ tor_addr_port_split(int severity, const char *addrport, return 0; } - return addr_port_lookup(severity, addrport, address_out, NULL, port_out); -} - -/** Parse a string of the form "host[:port]" from <b>addrport</b>. If - * <b>address</b> is provided, set *<b>address</b> to a copy of the - * host portion of the string. If <b>addr</b> is provided, try to - * resolve the host portion of the string and store it into - * *<b>addr</b> (in host byte order). If <b>port_out</b> is provided, - * store the port number into *<b>port_out</b>, or 0 if no port is given. - * If <b>port_out</b> is NULL, then there must be no port number in - * <b>addrport</b>. - * Return 0 on success, -1 on failure. - */ -int -addr_port_lookup(int severity, const char *addrport, char **address, - uint32_t *addr, uint16_t *port_out) -{ const char *colon; char *address_ = NULL; int port_; int ok = 1; - tor_assert(addrport); - colon = strrchr(addrport, ':'); if (colon) { address_ = tor_strndup(addrport, colon-addrport); @@ -1980,22 +1784,13 @@ addr_port_lookup(int severity, const char *addrport, char **address, port_ = 0; } - if (addr) { - /* There's an addr pointer, so we need to resolve the hostname. */ - if (tor_lookup_hostname(address_,addr)) { - log_fn(severity, LD_NET, "Couldn't look up %s", escaped(address_)); - ok = 0; - *addr = 0; - } - } - - if (address && ok) { - *address = address_; + if (ok) { + *address_out = address_; } else { - if (address) - *address = NULL; + *address_out = NULL; tor_free(address_); } + if (port_out) *port_out = ok ? ((uint16_t) port_) : 0; diff --git a/src/lib/net/address.h b/src/lib/net/address.h index f8ea573c30..e857b4068b 100644 --- a/src/lib/net/address.h +++ b/src/lib/net/address.h @@ -14,7 +14,7 @@ #include "orconfig.h" #include "lib/cc/torint.h" #include "lib/log/util_bug.h" -#include "lib/net/ipv6.h" +#include "lib/net/inaddr_st.h" #include "lib/net/nettypes.h" #ifdef HAVE_NETINET_IN_H @@ -204,8 +204,6 @@ tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u) */ #define TOR_ADDR_BUF_LEN 48 -MOCK_DECL(int, tor_addr_lookup,(const char *name, uint16_t family, - tor_addr_t *addr_out)); char *tor_addr_to_str_dup(const tor_addr_t *addr) ATTR_MALLOC; /** Wrapper function of fmt_addr_impl(). It does not decorate IPv6 @@ -263,9 +261,6 @@ int tor_addr_to_PTR_name(char *out, size_t outlen, int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address, int family, int accept_regular); -int tor_addr_port_lookup(const char *s, tor_addr_t *addr_out, - uint16_t *port_out); - /* Does the address * yield an AF_UNSPEC wildcard address (1), * which expands to corresponding wildcard IPv4 and IPv6 rules, and do we * allow *4 and *6 for IPv4 and IPv6 wildcards, respectively; @@ -330,8 +325,6 @@ int tor_addr_port_parse(int severity, const char *addrport, int tor_addr_hostname_is_local(const char *name); /* IPv4 helpers */ -int addr_port_lookup(int severity, const char *addrport, char **address, - uint32_t *addr, uint16_t *port_out); int parse_port_range(const char *port, uint16_t *port_min_out, uint16_t *port_max_out); int addr_mask_get_bits(uint32_t mask); diff --git a/src/lib/net/buffers_net.c b/src/lib/net/buffers_net.c index edc9954f22..2e6a096a06 100644 --- a/src/lib/net/buffers_net.c +++ b/src/lib/net/buffers_net.c @@ -4,6 +4,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file buffers_net.c + * \brief Read and write data on a buf_t object. + **/ + #define BUFFERS_PRIVATE #include "lib/net/buffers_net.h" #include "lib/container/buffers.h" diff --git a/src/lib/net/gethostname.c b/src/lib/net/gethostname.c index b6cc9b8e5f..1c4431af29 100644 --- a/src/lib/net/gethostname.c +++ b/src/lib/net/gethostname.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file gethostname.c + * \brief Mockable wrapper for gethostname(). + */ + #include "orconfig.h" #include "lib/net/gethostname.h" diff --git a/src/lib/net/gethostname.h b/src/lib/net/gethostname.h index d83c5fe096..7bf0ce5920 100644 --- a/src/lib/net/gethostname.h +++ b/src/lib/net/gethostname.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file gethostname.h + * \brief Header for gethostname.c + **/ + #ifndef TOR_GETHOSTNAME_H #define TOR_GETHOSTNAME_H diff --git a/src/lib/net/ipv6.c b/src/lib/net/inaddr.c index 35d7ddb901..dcd8fcdd65 100644 --- a/src/lib/net/ipv6.c +++ b/src/lib/net/inaddr.c @@ -3,14 +3,21 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ -#include "lib/net/ipv6.h" -#include "lib/net/ipv4.h" -#include "lib/string/util_string.h" -#include "lib/string/compat_string.h" +/** + * \file inaddr.c + * \brief Convert in_addr and in6_addr to and from strings. + **/ + +#include "lib/net/inaddr.h" + +#include "lib/cc/torint.h" +#include "lib/log/util_bug.h" +#include "lib/net/inaddr_st.h" #include "lib/string/compat_ctype.h" +#include "lib/string/compat_string.h" #include "lib/string/printf.h" #include "lib/string/scanf.h" -#include "lib/log/util_bug.h" +#include "lib/string/util_string.h" #ifdef HAVE_ARPA_INET_H #include <arpa/inet.h> @@ -19,6 +26,45 @@ #include <stdlib.h> #include <string.h> +#ifdef _WIN32 +#include <winsock2.h> +#endif + +/** Set *addr to the IP address (in dotted-quad notation) stored in *str. + * Return 1 on success, 0 if *str is badly formatted. + * (Like inet_aton(str,addr), but works on Windows and Solaris.) + */ +int +tor_inet_aton(const char *str, struct in_addr* addr) +{ + unsigned a,b,c,d; + char more; + if (tor_sscanf(str, "%3u.%3u.%3u.%3u%c", &a,&b,&c,&d,&more) != 4) + return 0; + if (a > 255) return 0; + if (b > 255) return 0; + if (c > 255) return 0; + if (d > 255) return 0; + addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d); + return 1; +} + +/** Given an IPv4 in_addr struct *<b>in</b> (in network order, as usual), + * write it as a string into the <b>buf_len</b>-byte buffer in + * <b>buf</b>. Returns a non-negative integer on success. + * Returns -1 on failure. + */ +int +tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len) +{ + uint32_t a = ntohl(in->s_addr); + return tor_snprintf(buf, buf_len, "%d.%d.%d.%d", + (int)(uint8_t)((a>>24)&0xff), + (int)(uint8_t)((a>>16)&0xff), + (int)(uint8_t)((a>>8 )&0xff), + (int)(uint8_t)((a )&0xff)); +} + /** Given <b>af</b>==AF_INET and <b>src</b> a struct in_addr, or * <b>af</b>==AF_INET6 and <b>src</b> a struct in6_addr, try to format the * address and store it in the <b>len</b>-byte buffer <b>dst</b>. Returns diff --git a/src/lib/net/ipv4.h b/src/lib/net/inaddr.h index 1ccc729970..121025a126 100644 --- a/src/lib/net/ipv4.h +++ b/src/lib/net/inaddr.h @@ -3,15 +3,25 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ -#ifndef TOR_IPV4_H -#define TOR_IPV4_H +/** + * \file inaddr.h + * \brief Header for inaddr.c. + **/ +#ifndef TOR_INADDR_H +#define TOR_INADDR_H + +#include "orconfig.h" #include <stddef.h> struct in_addr; + int tor_inet_aton(const char *str, struct in_addr *addr); /** Length of a buffer to allocate to hold the results of tor_inet_ntoa.*/ #define INET_NTOA_BUF_LEN 16 int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len); +const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len); +int tor_inet_pton(int af, const char *src, void *dst); + #endif diff --git a/src/lib/net/ipv6.h b/src/lib/net/inaddr_st.h index fd3fc12ba0..dc4c6e3a00 100644 --- a/src/lib/net/ipv6.h +++ b/src/lib/net/inaddr_st.h @@ -3,11 +3,19 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ -#ifndef TOR_IPV6_H -#define TOR_IPV6_H +/** + * \file inaddr_st.h + * + * \brief Define in6_addr, its members, and related types on platforms that + * lack it. + **/ + +#ifndef TOR_INADDR_ST_H +#define TOR_INADDR_ST_H #include "orconfig.h" #include <stddef.h> + #ifdef HAVE_ARPA_INET_H #include <arpa/inet.h> #endif @@ -20,13 +28,17 @@ #ifdef HAVE_SYS_SOCKET_H #include <sys/socket.h> #endif + #ifdef _WIN32 #include <winsock2.h> #include <ws2tcpip.h> #include <windows.h> #endif + #include "lib/cc/torint.h" +struct in_addr; + /** Implementation of struct in6_addr for platforms that do not have it. * Generally, these platforms are ones without IPv6 support, but we want to * have a working in6_addr there anyway, so we can use it to parse IPv6 @@ -89,7 +101,4 @@ struct sockaddr_in6 { }; #endif /* !defined(HAVE_STRUCT_SOCKADDR_IN6) */ -const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len); -int tor_inet_pton(int af, const char *src, void *dst); - -#endif +#endif /* TOR_INADDR_ST_H */ diff --git a/src/lib/net/include.am b/src/lib/net/include.am index 6fda173614..67db0d5af2 100644 --- a/src/lib/net/include.am +++ b/src/lib/net/include.am @@ -10,8 +10,7 @@ src_lib_libtor_net_a_SOURCES = \ src/lib/net/alertsock.c \ src/lib/net/buffers_net.c \ src/lib/net/gethostname.c \ - src/lib/net/ipv4.c \ - src/lib/net/ipv6.c \ + src/lib/net/inaddr.c \ src/lib/net/resolve.c \ src/lib/net/socket.c @@ -25,8 +24,8 @@ noinst_HEADERS += \ src/lib/net/alertsock.h \ src/lib/net/buffers_net.h \ src/lib/net/gethostname.h \ - src/lib/net/ipv4.h \ - src/lib/net/ipv6.h \ + src/lib/net/inaddr.h \ + src/lib/net/inaddr_st.h \ src/lib/net/nettypes.h \ src/lib/net/resolve.h \ src/lib/net/socket.h \ diff --git a/src/lib/net/ipv4.c b/src/lib/net/ipv4.c deleted file mode 100644 index 18e69761e2..0000000000 --- a/src/lib/net/ipv4.c +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright (c) 2003-2004, Roger Dingledine - * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2018, The Tor Project, Inc. */ -/* See LICENSE for licensing information */ - -#include "orconfig.h" -#include "lib/cc/torint.h" -#include "lib/net/ipv4.h" -#include "lib/string/printf.h" -#include "lib/string/scanf.h" - -#ifdef HAVE_ARPA_INET_H -#include <arpa/inet.h> -#endif -#ifdef _WIN32 -#include <winsock2.h> -#endif - -/** Set *addr to the IP address (in dotted-quad notation) stored in *str. - * Return 1 on success, 0 if *str is badly formatted. - * (Like inet_aton(str,addr), but works on Windows and Solaris.) - */ -int -tor_inet_aton(const char *str, struct in_addr* addr) -{ - unsigned a,b,c,d; - char more; - if (tor_sscanf(str, "%3u.%3u.%3u.%3u%c", &a,&b,&c,&d,&more) != 4) - return 0; - if (a > 255) return 0; - if (b > 255) return 0; - if (c > 255) return 0; - if (d > 255) return 0; - addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d); - return 1; -} - -/** Given an IPv4 in_addr struct *<b>in</b> (in network order, as usual), - * write it as a string into the <b>buf_len</b>-byte buffer in - * <b>buf</b>. Returns a non-negative integer on success. - * Returns -1 on failure. - */ -int -tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len) -{ - uint32_t a = ntohl(in->s_addr); - return tor_snprintf(buf, buf_len, "%d.%d.%d.%d", - (int)(uint8_t)((a>>24)&0xff), - (int)(uint8_t)((a>>16)&0xff), - (int)(uint8_t)((a>>8 )&0xff), - (int)(uint8_t)((a )&0xff)); -} diff --git a/src/lib/net/nettypes.h b/src/lib/net/nettypes.h index f212374368..f7f2ec7d6a 100644 --- a/src/lib/net/nettypes.h +++ b/src/lib/net/nettypes.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file nettypes.h + * \brief Declarations for types used throughout the Tor networking system + **/ + #ifndef TOR_NET_TYPES_H #define TOR_NET_TYPES_H diff --git a/src/lib/net/resolve.c b/src/lib/net/resolve.c index cbe368ccfb..ff9c93989a 100644 --- a/src/lib/net/resolve.c +++ b/src/lib/net/resolve.c @@ -3,9 +3,18 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file resolve.c + * \brief Use the libc DNS resolver to convert hostnames into addresses. + **/ + #include "lib/net/resolve.h" + #include "lib/net/address.h" +#include "lib/net/inaddr.h" #include "lib/malloc/util_malloc.h" +#include "lib/string/parse_int.h" +#include "lib/string/util_string.h" #include "siphash.h" #include "ht.h" @@ -47,6 +56,185 @@ tor_lookup_hostname,(const char *name, uint32_t *addr)) return -1; } +/** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set + * *<b>addr</b> to the proper IP address and family. The <b>family</b> + * argument (which must be AF_INET, AF_INET6, or AF_UNSPEC) declares a + * <i>preferred</i> family, though another one may be returned if only one + * family is implemented for this address. + * + * Return 0 on success, -1 on failure; 1 on transient failure. + */ +MOCK_IMPL(int, +tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr)) +{ + /* Perhaps eventually this should be replaced by a tor_getaddrinfo or + * something. + */ + struct in_addr iaddr; + struct in6_addr iaddr6; + tor_assert(name); + tor_assert(addr); + tor_assert(family == AF_INET || family == AF_INET6 || family == AF_UNSPEC); + if (!*name) { + /* Empty address is an error. */ + return -1; + } else if (tor_inet_pton(AF_INET, name, &iaddr)) { + /* It's an IPv4 IP. */ + if (family == AF_INET6) + return -1; + tor_addr_from_in(addr, &iaddr); + return 0; + } else if (tor_inet_pton(AF_INET6, name, &iaddr6)) { + if (family == AF_INET) + return -1; + tor_addr_from_in6(addr, &iaddr6); + return 0; + } else { +#ifdef HAVE_GETADDRINFO + int err; + struct addrinfo *res=NULL, *res_p; + struct addrinfo *best=NULL; + struct addrinfo hints; + int result = -1; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = family; + hints.ai_socktype = SOCK_STREAM; + err = tor_getaddrinfo(name, NULL, &hints, &res); + /* The check for 'res' here shouldn't be necessary, but it makes static + * analysis tools happy. */ + if (!err && res) { + best = NULL; + for (res_p = res; res_p; res_p = res_p->ai_next) { + if (family == AF_UNSPEC) { + if (res_p->ai_family == AF_INET) { + best = res_p; + break; + } else if (res_p->ai_family == AF_INET6 && !best) { + best = res_p; + } + } else if (family == res_p->ai_family) { + best = res_p; + break; + } + } + if (!best) + best = res; + if (best->ai_family == AF_INET) { + tor_addr_from_in(addr, + &((struct sockaddr_in*)best->ai_addr)->sin_addr); + result = 0; + } else if (best->ai_family == AF_INET6) { + tor_addr_from_in6(addr, + &((struct sockaddr_in6*)best->ai_addr)->sin6_addr); + result = 0; + } + tor_freeaddrinfo(res); + return result; + } + return (err == EAI_AGAIN) ? 1 : -1; +#else /* !(defined(HAVE_GETADDRINFO)) */ + struct hostent *ent; + int err; +#ifdef HAVE_GETHOSTBYNAME_R_6_ARG + char buf[2048]; + struct hostent hostent; + int r; + r = gethostbyname_r(name, &hostent, buf, sizeof(buf), &ent, &err); +#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) + char buf[2048]; + struct hostent hostent; + ent = gethostbyname_r(name, &hostent, buf, sizeof(buf), &err); +#elif defined(HAVE_GETHOSTBYNAME_R_3_ARG) + struct hostent_data data; + struct hostent hent; + memset(&data, 0, sizeof(data)); + err = gethostbyname_r(name, &hent, &data); + ent = err ? NULL : &hent; +#else + ent = gethostbyname(name); +#ifdef _WIN32 + err = WSAGetLastError(); +#else + err = h_errno; +#endif +#endif /* defined(HAVE_GETHOSTBYNAME_R_6_ARG) || ... */ + if (ent) { + if (ent->h_addrtype == AF_INET) { + tor_addr_from_in(addr, (struct in_addr*) ent->h_addr); + } else if (ent->h_addrtype == AF_INET6) { + tor_addr_from_in6(addr, (struct in6_addr*) ent->h_addr); + } else { + tor_assert(0); // LCOV_EXCL_LINE: gethostbyname() returned bizarre type + } + return 0; + } +#ifdef _WIN32 + return (err == WSATRY_AGAIN) ? 1 : -1; +#else + return (err == TRY_AGAIN) ? 1 : -1; +#endif +#endif /* defined(HAVE_GETADDRINFO) */ + } +} + +/** Parse an address or address-port combination from <b>s</b>, resolve the + * address as needed, and put the result in <b>addr_out</b> and (optionally) + * <b>port_out</b>. Return 0 on success, negative on failure. */ +int +tor_addr_port_lookup(const char *s, tor_addr_t *addr_out, uint16_t *port_out) +{ + const char *port; + tor_addr_t addr; + uint16_t portval; + char *tmp = NULL; + + tor_assert(s); + tor_assert(addr_out); + + s = eat_whitespace(s); + + if (*s == '[') { + port = strstr(s, "]"); + if (!port) + goto err; + tmp = tor_strndup(s+1, port-(s+1)); + port = port+1; + if (*port == ':') + port++; + else + port = NULL; + } else { + port = strchr(s, ':'); + if (port) + tmp = tor_strndup(s, port-s); + else + tmp = tor_strdup(s); + if (port) + ++port; + } + + if (tor_addr_lookup(tmp, AF_UNSPEC, &addr) != 0) + goto err; + tor_free(tmp); + + if (port) { + portval = (int) tor_parse_long(port, 10, 1, 65535, NULL, NULL); + if (!portval) + goto err; + } else { + portval = 0; + } + + if (port_out) + *port_out = portval; + tor_addr_copy(addr_out, &addr); + + return 0; + err: + tor_free(tmp); + return -1; +} + #ifdef USE_SANDBOX_GETADDRINFO /** True if we should only return cached values */ static int sandbox_getaddrinfo_is_active = 0; diff --git a/src/lib/net/resolve.h b/src/lib/net/resolve.h index f2280ae7e8..bf870c44c4 100644 --- a/src/lib/net/resolve.h +++ b/src/lib/net/resolve.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file resolve.h + * \brief Header for resolve.c + **/ + #ifndef TOR_RESOLVE_H #define TOR_RESOLVE_H @@ -17,7 +22,13 @@ #define USE_SANDBOX_GETADDRINFO #endif -MOCK_DECL(int,tor_lookup_hostname,(const char *name, uint32_t *addr)); +struct tor_addr_t; + +MOCK_DECL(int, tor_lookup_hostname,(const char *name, uint32_t *addr)); +MOCK_DECL(int, tor_addr_lookup,(const char *name, uint16_t family, + struct tor_addr_t *addr_out)); +int tor_addr_port_lookup(const char *s, struct tor_addr_t *addr_out, + uint16_t *port_out); struct addrinfo; #ifdef USE_SANDBOX_GETADDRINFO diff --git a/src/lib/net/socket.c b/src/lib/net/socket.c index dc3d1531ff..1b3238d998 100644 --- a/src/lib/net/socket.c +++ b/src/lib/net/socket.c @@ -3,6 +3,12 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file socket.c + * \brief Compatibility and utility functions for working with network + * sockets. + **/ + #define SOCKET_PRIVATE #include "lib/net/socket.h" #include "lib/net/address.h" diff --git a/src/lib/net/socket.h b/src/lib/net/socket.h index cb0ccbe817..e2092c727a 100644 --- a/src/lib/net/socket.h +++ b/src/lib/net/socket.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file socket.h + * \brief Header for socket.c + **/ + #ifndef TOR_SOCKET_H #define TOR_SOCKET_H diff --git a/src/lib/net/socks5_status.h b/src/lib/net/socks5_status.h index 74b9c91023..0f31132545 100644 --- a/src/lib/net/socks5_status.h +++ b/src/lib/net/socks5_status.h @@ -3,6 +3,16 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file socks5_status.h + * \brief Status codes used by the SOCKS5 protocol. + **/ + +/* NOTE: it probably isn't necessary to put this header in lib/net, but + * we need it in _some_ lower-level layer for now, since it is used by + * tools/tor-resolve.c. + */ + #ifndef TOR_SOCKS5_STATUS_H #define TOR_SOCKS5_STATUS_H diff --git a/src/lib/osinfo/uname.c b/src/lib/osinfo/uname.c index a0fa26d1d2..9d1923695d 100644 --- a/src/lib/osinfo/uname.c +++ b/src/lib/osinfo/uname.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file uname.c + * \brief Look up a description of the operating system. + **/ + #include "orconfig.h" #include "lib/osinfo/uname.h" diff --git a/src/lib/osinfo/uname.h b/src/lib/osinfo/uname.h index 1f0b78385f..ef8cd078ee 100644 --- a/src/lib/osinfo/uname.h +++ b/src/lib/osinfo/uname.h @@ -1,3 +1,12 @@ +/* Copyright (c) 2003-2004, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file uname.h + * \brief Header for uname.c + **/ #ifndef HAVE_TOR_UNAME_H #define HAVE_TOR_UNAME_H diff --git a/src/lib/process/daemon.c b/src/lib/process/daemon.c index edffb04683..6863d05d7e 100644 --- a/src/lib/process/daemon.c +++ b/src/lib/process/daemon.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file daemon.c + * \brief Run the tor process in the background (unix only) + **/ + #include "orconfig.h" #include "lib/process/daemon.h" diff --git a/src/lib/process/daemon.h b/src/lib/process/daemon.h index 48a65b22e6..1f26e92221 100644 --- a/src/lib/process/daemon.h +++ b/src/lib/process/daemon.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file daemon.h + * \brief Header for daemon.c + **/ + #ifndef TOR_DAEMON_H #define TOR_DAEMON_H diff --git a/src/lib/process/env.c b/src/lib/process/env.c index 731f609ac1..244cd4a4eb 100644 --- a/src/lib/process/env.c +++ b/src/lib/process/env.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file env.c + * \brief Inspect and manipulate the environment variables. + **/ + #include "orconfig.h" #include "lib/process/env.h" diff --git a/src/lib/process/env.h b/src/lib/process/env.h index f22599355d..288b923ace 100644 --- a/src/lib/process/env.h +++ b/src/lib/process/env.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file env.h + * \brief Header for env.c + **/ + #ifndef TOR_ENV_H #define TOR_ENV_H diff --git a/src/lib/process/pidfile.c b/src/lib/process/pidfile.c index f016f21697..17ff8be600 100644 --- a/src/lib/process/pidfile.c +++ b/src/lib/process/pidfile.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file pidfile.c + * \brief Record this process's PID to disk. + **/ + #include "orconfig.h" #include "lib/process/pidfile.h" diff --git a/src/lib/process/pidfile.h b/src/lib/process/pidfile.h index c85cd1905e..945edee990 100644 --- a/src/lib/process/pidfile.h +++ b/src/lib/process/pidfile.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file pidfile.h + * \brief Header for pidfile.c + **/ + #ifndef TOR_PIDFILE_H #define TOR_PIDFILE_H diff --git a/src/lib/process/restrict.c b/src/lib/process/restrict.c index bb44cc3d15..fc1a308806 100644 --- a/src/lib/process/restrict.c +++ b/src/lib/process/restrict.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file restrict.c + * \brief Drop privileges from the current process. + **/ + #include "orconfig.h" #include "lib/process/restrict.h" #include "lib/intmath/cmp.h" diff --git a/src/lib/process/restrict.h b/src/lib/process/restrict.h index c7f76f8233..2e78dc468c 100644 --- a/src/lib/process/restrict.h +++ b/src/lib/process/restrict.h @@ -4,8 +4,8 @@ /* See LICENSE for licensing information */ /** - * \file waitpid.h - * \brief Headers for waitpid.c + * \file restrict.h + * \brief Header for restrict.c **/ #ifndef TOR_RESTRICT_H diff --git a/src/lib/process/setuid.c b/src/lib/process/setuid.c index fa1cdc0f3f..5423259a66 100644 --- a/src/lib/process/setuid.c +++ b/src/lib/process/setuid.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file setuid.c + * \brief Change the user ID after Tor has started (Unix only) + **/ + #include "orconfig.h" #include "lib/process/setuid.h" diff --git a/src/lib/process/setuid.h b/src/lib/process/setuid.h index 61aeefe1b7..49751c97c2 100644 --- a/src/lib/process/setuid.h +++ b/src/lib/process/setuid.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file setuid.h + * \brief Header for setuid.c + **/ + #ifndef TOR_SETUID_H #define TOR_SETUID_H diff --git a/src/lib/process/subprocess.c b/src/lib/process/subprocess.c index 516494d105..9a12f5e76e 100644 --- a/src/lib/process/subprocess.c +++ b/src/lib/process/subprocess.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file subprocess.c + * \brief Launch and monitor other processes. + **/ + #define SUBPROCESS_PRIVATE #include "lib/process/subprocess.h" diff --git a/src/lib/process/subprocess.h b/src/lib/process/subprocess.h index a319b3505c..5b4318ef2b 100644 --- a/src/lib/process/subprocess.h +++ b/src/lib/process/subprocess.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file subprocess.h + * \brief Header for subprocess.c + **/ + #ifndef TOR_SUBPROCESS_H #define TOR_SUBPROCESS_H diff --git a/src/lib/process/waitpid.c b/src/lib/process/waitpid.c index 66c77b05f3..27f69b08f0 100644 --- a/src/lib/process/waitpid.c +++ b/src/lib/process/waitpid.c @@ -4,10 +4,8 @@ /* See LICENSE for licensing information */ /** - * \file util_process.c - * \brief utility functions for launching processes and checking their - * status. These functions are kept separately from procmon so that they - * won't require linking against libevent. + * \file waitpid.c + * \brief Convenience structures for handlers for handling waitpid(). **/ #include "orconfig.h" diff --git a/src/lib/smartlist_core/smartlist_core.h b/src/lib/smartlist_core/smartlist_core.h index b1adf2ebdb..974fb01758 100644 --- a/src/lib/smartlist_core/smartlist_core.h +++ b/src/lib/smartlist_core/smartlist_core.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file smartlist_core.h + * \brief Top-level declarations for the smartlist_t dynamic array type. + **/ + #ifndef TOR_SMARTLIST_CORE_H #define TOR_SMARTLIST_CORE_H diff --git a/src/lib/smartlist_core/smartlist_foreach.h b/src/lib/smartlist_core/smartlist_foreach.h index 4bef36d99c..54f08ac47d 100644 --- a/src/lib/smartlist_core/smartlist_foreach.h +++ b/src/lib/smartlist_core/smartlist_foreach.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file smartlist_foreach.h + * \brief Macros for iterating over the elements of a smartlist_t. + **/ + #ifndef TOR_SMARTLIST_FOREACH_H #define TOR_SMARTLIST_FOREACH_H diff --git a/src/lib/smartlist_core/smartlist_split.c b/src/lib/smartlist_core/smartlist_split.c index b9340e7924..9c8368f665 100644 --- a/src/lib/smartlist_core/smartlist_split.c +++ b/src/lib/smartlist_core/smartlist_split.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file smartlist_split.c + * \brief Split a string into a smartlist_t of substrings. + **/ + #include "lib/smartlist_core/smartlist_core.h" #include "lib/smartlist_core/smartlist_split.h" diff --git a/src/lib/smartlist_core/smartlist_split.h b/src/lib/smartlist_core/smartlist_split.h index 8ed2abafb8..4dd48295ce 100644 --- a/src/lib/smartlist_core/smartlist_split.h +++ b/src/lib/smartlist_core/smartlist_split.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file smartlist_split.h + * \brief Header for smartlist_split.c + **/ + #ifndef TOR_SMARTLIST_SPLIT_H #define TOR_SMARTLIST_SPLIT_H diff --git a/src/lib/string/compat_ctype.c b/src/lib/string/compat_ctype.c index d1d4ce0ffc..35f4ec6534 100644 --- a/src/lib/string/compat_ctype.c +++ b/src/lib/string/compat_ctype.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file compat_ctype.c + * \brief Locale-independent character-type inspection (backend) + **/ + #include "lib/string/compat_ctype.h" /** diff --git a/src/lib/string/compat_ctype.h b/src/lib/string/compat_ctype.h index 530a10270f..210c460c31 100644 --- a/src/lib/string/compat_ctype.h +++ b/src/lib/string/compat_ctype.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file compat_ctype.h + * \brief Locale-independent character-type inspection (header) + **/ + #ifndef TOR_COMPAT_CTYPE_H #define TOR_COMPAT_CTYPE_H diff --git a/src/lib/string/compat_string.c b/src/lib/string/compat_string.c index 8b063b7242..eae82fdae0 100644 --- a/src/lib/string/compat_string.c +++ b/src/lib/string/compat_string.c @@ -3,6 +3,12 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file compat_string.c + * \brief Useful string-processing functions that some platforms don't + * provide. + **/ + #include "lib/string/compat_string.h" #include "lib/err/torerr.h" diff --git a/src/lib/string/compat_string.h b/src/lib/string/compat_string.h index 4726d2b5b6..9292717337 100644 --- a/src/lib/string/compat_string.h +++ b/src/lib/string/compat_string.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file compat_string.h + * \brief Header for compat_string.c + **/ + #ifndef TOR_COMPAT_STRING_H #define TOR_COMPAT_STRING_H diff --git a/src/lib/string/parse_int.c b/src/lib/string/parse_int.c index e552730cc4..52ff49ef1e 100644 --- a/src/lib/string/parse_int.c +++ b/src/lib/string/parse_int.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file parse_int.c + * \brief Convert strings into the integers they encode, with bounds checking. + **/ + #include "lib/string/parse_int.h" #include <errno.h> diff --git a/src/lib/string/parse_int.h b/src/lib/string/parse_int.h index 6f56fc32a8..663a5acd74 100644 --- a/src/lib/string/parse_int.h +++ b/src/lib/string/parse_int.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file parse_int.h + * \brief Header for parse_int.c + **/ + #ifndef TOR_PARSE_INT_H #define TOR_PARSE_INT_H diff --git a/src/lib/string/printf.c b/src/lib/string/printf.c index 4443e25fb4..f8be3b4704 100644 --- a/src/lib/string/printf.c +++ b/src/lib/string/printf.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file printf.c + * \brief Compatibility wrappers around snprintf and its friends + **/ + #include "lib/string/printf.h" #include "lib/err/torerr.h" #include "lib/cc/torint.h" diff --git a/src/lib/string/printf.h b/src/lib/string/printf.h index 69b724379a..49c37d43e0 100644 --- a/src/lib/string/printf.h +++ b/src/lib/string/printf.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file printf.h + * \brief Header for printf.c + **/ + #ifndef TOR_UTIL_PRINTF_H #define TOR_UTIL_PRINTF_H diff --git a/src/lib/string/scanf.c b/src/lib/string/scanf.c index 0c5082799c..7b08442148 100644 --- a/src/lib/string/scanf.c +++ b/src/lib/string/scanf.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file scanf.c + * \brief Locale-independent minimal implementation of sscanf(). + **/ + #include "lib/string/scanf.h" #include "lib/string/compat_ctype.h" #include "lib/cc/torint.h" diff --git a/src/lib/string/scanf.h b/src/lib/string/scanf.h index 9cfa9cc6c1..ada2322bb1 100644 --- a/src/lib/string/scanf.h +++ b/src/lib/string/scanf.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file scanf.h + * \brief Header for scanf.c + **/ + #ifndef TOR_UTIL_SCANF_H #define TOR_UTIL_SCANF_H diff --git a/src/lib/string/util_string.c b/src/lib/string/util_string.c index e8ed3d4f54..a6b0a3d68a 100644 --- a/src/lib/string/util_string.c +++ b/src/lib/string/util_string.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file util_string.c + * \brief Non-standard string functions used throughout Tor. + **/ + #include "lib/string/util_string.h" #include "lib/string/compat_ctype.h" #include "lib/err/torerr.h" diff --git a/src/lib/string/util_string.h b/src/lib/string/util_string.h index 75407d5ffa..471613462a 100644 --- a/src/lib/string/util_string.h +++ b/src/lib/string/util_string.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file util_string.h + * \brief Header for util_string.c + **/ + #ifndef TOR_UTIL_STRING_H #define TOR_UTIL_STRING_H diff --git a/src/lib/term/getpass.c b/src/lib/term/getpass.c index 10c99914f8..590411b715 100644 --- a/src/lib/term/getpass.c +++ b/src/lib/term/getpass.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file getpass.c + * \brief Cross-platform wrapper to read passphrases from the terminal. + **/ + #include "lib/term/getpass.h" #include "lib/log/util_bug.h" diff --git a/src/lib/term/getpass.h b/src/lib/term/getpass.h index 9d03f7036c..e8347e7fe8 100644 --- a/src/lib/term/getpass.h +++ b/src/lib/term/getpass.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file getpass.h + * \brief Header for getpass.c + **/ + #ifndef TOR_GETPASS_H #define TOR_GETPASS_H diff --git a/src/lib/testsupport/testsupport.h b/src/lib/testsupport/testsupport.h index 9a55d306fc..3ae1b48f87 100644 --- a/src/lib/testsupport/testsupport.h +++ b/src/lib/testsupport/testsupport.h @@ -1,10 +1,24 @@ /* Copyright (c) 2013-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file testsupport.h + * + * \brief Macros to implement mocking and selective exposure for the test code. + * + * Each Tor source file is built twice: once with TOR_UNIT_TESTS defined, and + * once with it undefined. The only difference between these configurations + * should be that when building for the tests, more functions are exposed as + * non-static, and a number of functions are declared as mockable. + **/ + #ifndef TOR_TESTSUPPORT_H #define TOR_TESTSUPPORT_H #ifdef TOR_UNIT_TESTS +/** The "STATIC" macro marks a function or variable that is static when + * building Tor for production, but non-static when building the unit + * tests. */ #define STATIC #define EXTERN(type, name) extern type name; #else @@ -87,4 +101,3 @@ /** @} */ #endif /* !defined(TOR_TESTSUPPORT_H) */ - diff --git a/src/lib/thread/numcpus.c b/src/lib/thread/numcpus.c index 534b0570f8..b8763f118f 100644 --- a/src/lib/thread/numcpus.c +++ b/src/lib/thread/numcpus.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file numcpus.c + * \brief Compute the number of CPUs configured on this system. + **/ + #include "orconfig.h" #include "lib/thread/numcpus.h" #include "lib/log/torlog.h" diff --git a/src/lib/thread/numcpus.h b/src/lib/thread/numcpus.h index 2899a9ec8a..0b026e4249 100644 --- a/src/lib/thread/numcpus.h +++ b/src/lib/thread/numcpus.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file numcpus.h + * \brief Header for numcpus.c + **/ + #ifndef TOR_NUMCPUS_H #define TOR_NUMCPUS_H diff --git a/src/lib/thread/threads.h b/src/lib/thread/threads.h index fcccc643d5..89d2a9d93e 100644 --- a/src/lib/thread/threads.h +++ b/src/lib/thread/threads.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file threads.h + * \brief Header for threads.c + **/ + #ifndef TOR_COMPAT_THREADS_H #define TOR_COMPAT_THREADS_H diff --git a/src/lib/time/tvdiff.c b/src/lib/time/tvdiff.c index cfd1ace771..6af12501c7 100644 --- a/src/lib/time/tvdiff.c +++ b/src/lib/time/tvdiff.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file tvdiff.c + * \brief Compute the difference between timevals, in various units. + **/ + #include "lib/time/tvdiff.h" #include "lib/cc/compat_compiler.h" diff --git a/src/lib/time/tvdiff.h b/src/lib/time/tvdiff.h index 215de9cf37..d78330d7d8 100644 --- a/src/lib/time/tvdiff.h +++ b/src/lib/time/tvdiff.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file tvdiff.h + * \brief Header for tvdiff.c + **/ + #ifndef TOR_TVDIFF_H #define TOR_TVDIFF_H diff --git a/src/lib/tls/buffers_tls.c b/src/lib/tls/buffers_tls.c index 243e0eb0bc..0e112b59cf 100644 --- a/src/lib/tls/buffers_tls.c +++ b/src/lib/tls/buffers_tls.c @@ -4,6 +4,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file buffers_tls.c + * \brief Read and write data on a tor_tls_t connection from a buf_t object. + **/ + #define BUFFERS_PRIVATE #include "orconfig.h" #include <stddef.h> diff --git a/src/lib/tls/buffers_tls.h b/src/lib/tls/buffers_tls.h index d9d26c82bd..7a1ca6d16c 100644 --- a/src/lib/tls/buffers_tls.h +++ b/src/lib/tls/buffers_tls.h @@ -4,6 +4,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file buffers_tls.h + * \brief Header for buffers_tls.c + **/ + #ifndef TOR_BUFFERS_TLS_H #define TOR_BUFFERS_TLS_H @@ -16,4 +21,3 @@ int buf_flush_to_tls(struct buf_t *buf, struct tor_tls_t *tls, size_t sz, size_t *buf_flushlen); #endif /* !defined(TOR_BUFFERS_TLS_H) */ - diff --git a/src/lib/trace/debug.h b/src/lib/trace/debug.h index 9b5d9d05c8..a764f552ea 100644 --- a/src/lib/trace/debug.h +++ b/src/lib/trace/debug.h @@ -1,6 +1,11 @@ /* Copyright (c) 2017-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file debug.h + * \brief Macros for debugging our event-trace support. + **/ + #ifndef TOR_TRACE_LOG_DEBUG_H #define TOR_TRACE_LOG_DEBUG_H diff --git a/src/lib/trace/trace.c b/src/lib/trace/trace.c index c0bbbb0cc6..535ffde183 100644 --- a/src/lib/trace/trace.c +++ b/src/lib/trace/trace.c @@ -1,6 +1,13 @@ /* Copyright (c) 2017-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file trace.c + * \brief Common functions for event-tracing implementation + * + * See trace.h and doc/HACKING/Tracing.md for more information. + **/ + #include "lib/trace/trace.h" /** Initialize the tracing library. */ @@ -8,4 +15,3 @@ void tor_trace_init(void) { } - diff --git a/src/lib/trace/trace.h b/src/lib/trace/trace.h index 2dd51aace1..5f7b0ee7cd 100644 --- a/src/lib/trace/trace.h +++ b/src/lib/trace/trace.h @@ -1,10 +1,14 @@ /* Copyright (c) 2017-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file trace.h + * \brief Header for trace.c + **/ + #ifndef TOR_TRACE_TRACE_H #define TOR_TRACE_TRACE_H void tor_trace_init(void); #endif // TOR_TRACE_TRACE_H - diff --git a/src/lib/wallclock/approx_time.c b/src/lib/wallclock/approx_time.c index 2528954f13..bb9a292369 100644 --- a/src/lib/wallclock/approx_time.c +++ b/src/lib/wallclock/approx_time.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file approx_time.c + * \brief Cache the last result of time(), for performance and testing. + **/ + #include "orconfig.h" #include "lib/wallclock/approx_time.h" diff --git a/src/lib/wallclock/approx_time.h b/src/lib/wallclock/approx_time.h index c57ff5bcd3..becc632fe3 100644 --- a/src/lib/wallclock/approx_time.h +++ b/src/lib/wallclock/approx_time.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file approx_time.h + * \brief Header for approx_time.c + **/ + #ifndef TOR_APPROX_TIME_H #define TOR_APPROX_TIME_H diff --git a/src/lib/wallclock/timeval.c b/src/lib/wallclock/timeval.c deleted file mode 100644 index e69de29bb2..0000000000 --- a/src/lib/wallclock/timeval.c +++ /dev/null diff --git a/src/lib/wallclock/timeval.h b/src/lib/wallclock/timeval.h index 6a9b36a022..b34277cda6 100644 --- a/src/lib/wallclock/timeval.h +++ b/src/lib/wallclock/timeval.h @@ -3,6 +3,13 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file timeval.h + * + * \brief Declarations for timeval-related macros that some platforms + * are missing. + **/ + #ifndef TOR_TIMEVAL_H #define TOR_TIMEVAL_H diff --git a/src/lib/wallclock/tm_cvt.c b/src/lib/wallclock/tm_cvt.c index 31d929e635..4a51a4ab3a 100644 --- a/src/lib/wallclock/tm_cvt.c +++ b/src/lib/wallclock/tm_cvt.c @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file tm_cvt.c + * \brief Convert to and from struct tm, portably. + **/ + #include "orconfig.h" #include "lib/cc/torint.h" #include "lib/cc/compat_compiler.h" diff --git a/src/lib/wallclock/tm_cvt.h b/src/lib/wallclock/tm_cvt.h index 4d87acd4fa..a1cdc80ef1 100644 --- a/src/lib/wallclock/tm_cvt.h +++ b/src/lib/wallclock/tm_cvt.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file tm_cvt.h + * \brief Header for tm_cvt.c + **/ + #ifndef TOR_WALLCLOCK_TM_CVT_H #define TOR_WALLCLOCK_TM_CVT_H diff --git a/src/lib/wallclock/tor_gettimeofday.c b/src/lib/wallclock/tor_gettimeofday.c index eb902e681d..aefe796ad2 100644 --- a/src/lib/wallclock/tor_gettimeofday.c +++ b/src/lib/wallclock/tor_gettimeofday.c @@ -4,9 +4,9 @@ /* See LICENSE for licensing information */ /** - * \file compat_time.c - * \brief Portable wrappers for finding out the current time, running - * timers, etc. + * \file tor_gettimeofday.c + * \brief Implementat gettimeofday() for windows, and other platforms without + * it. **/ #include "orconfig.h" diff --git a/src/lib/wallclock/tor_gettimeofday.h b/src/lib/wallclock/tor_gettimeofday.h index 728ad9565d..aac6366a65 100644 --- a/src/lib/wallclock/tor_gettimeofday.h +++ b/src/lib/wallclock/tor_gettimeofday.h @@ -3,6 +3,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file tor_gettimeofday.h + * \brief Header for tor_gettimeofday.c + **/ + #ifndef TOR_GETTIMEOFDAY_H #define TOR_GETTIMEOFDAY_H diff --git a/src/test/test_addr.c b/src/test/test_addr.c index 9ab921c5b6..a9004048a5 100644 --- a/src/test/test_addr.c +++ b/src/test/test_addr.c @@ -10,71 +10,16 @@ #include "test/test.h" #include "feature/client/addressmap.h" #include "test/log_test_helpers.h" +#include "lib/net/resolve.h" #ifdef HAVE_SYS_UN_H #include <sys/un.h> #endif -/** Mocking replacement: only handles localhost. */ -static int -mock_tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out) -{ - if (!strcmp(name, "localhost")) { - if (family == AF_INET || family == AF_UNSPEC) { - tor_addr_from_ipv4h(addr_out, 0x7f000001); - return 0; - } else if (family == AF_INET6) { - char bytes[16] = { 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 1 }; - tor_addr_from_ipv6_bytes(addr_out, bytes); - return 0; - } - } - return -1; -} - static void test_addr_basic(void *arg) { - uint32_t u32; - uint16_t u16; - char *cp; - - /* Test addr_port_lookup */ - (void)arg; - cp = NULL; u32 = 3; u16 = 3; - tt_assert(!addr_port_lookup(LOG_WARN, "1.2.3.4", &cp, &u32, &u16)); - tt_str_op(cp,OP_EQ, "1.2.3.4"); - tt_int_op(u32,OP_EQ, 0x01020304u); - tt_int_op(u16,OP_EQ, 0); - tor_free(cp); - tt_assert(!addr_port_lookup(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16)); - tt_str_op(cp,OP_EQ, "4.3.2.1"); - tt_int_op(u32,OP_EQ, 0x04030201u); - tt_int_op(u16,OP_EQ, 99); - tor_free(cp); - - MOCK(tor_addr_lookup, mock_tor_addr_lookup); - - tt_assert(!addr_port_lookup(LOG_WARN, "nonexistent.address:4040", - &cp, NULL, &u16)); - tt_str_op(cp,OP_EQ, "nonexistent.address"); - tt_int_op(u16,OP_EQ, 4040); - tor_free(cp); - tt_assert(!addr_port_lookup(LOG_WARN, "localhost:9999", &cp, &u32, &u16)); - tt_str_op(cp,OP_EQ, "localhost"); - tt_int_op(u16,OP_EQ, 9999); - tt_int_op(u32,OP_EQ, 0x7f000001u); - tor_free(cp); - u32 = 3; - tt_assert(!addr_port_lookup(LOG_WARN, "localhost", NULL, &u32, &u16)); - tt_ptr_op(cp,OP_EQ, NULL); - tt_int_op(u32,OP_EQ, 0x7f000001u); - tt_int_op(u16,OP_EQ, 0); - tor_free(cp); - - tt_assert(addr_port_lookup(LOG_WARN, "localhost:3", &cp, &u32, NULL)); - tor_free(cp); + (void) arg; tt_int_op(0,OP_EQ, addr_mask_get_bits(0x0u)); tt_int_op(32,OP_EQ, addr_mask_get_bits(0xFFFFFFFFu)); @@ -102,8 +47,7 @@ test_addr_basic(void *arg) } done: - UNMOCK(tor_addr_lookup); - tor_free(cp); + ; } #define test_op_ip6_(a,op,b,e1,e2) \ diff --git a/src/test/test_config.c b/src/test/test_config.c index af3a8a7cfe..393378b4c8 100644 --- a/src/test/test_config.c +++ b/src/test/test_config.c @@ -10,6 +10,7 @@ #define ROUTERSET_PRIVATE #include "core/or/or.h" #include "lib/net/address.h" +#include "lib/net/resolve.h" #include "feature/client/addressmap.h" #include "feature/client/bridges.h" #include "core/or/circuitmux_ewma.h" diff --git a/src/test/test_connection.c b/src/test/test_connection.c index c423c6573f..e716c83fe1 100644 --- a/src/test/test_connection.c +++ b/src/test/test_connection.c @@ -20,6 +20,7 @@ #include "feature/rend/rendcache.h" #include "feature/dircache/directory.h" #include "core/or/connection_or.h" +#include "lib/net/resolve.h" #include "test/test_connection.h" #include "test/test_helpers.h" @@ -899,4 +900,3 @@ struct testcase_t connection_tests[] = { { "failed_orconn_tracker", test_failed_orconn_tracker, TT_FORK, NULL, NULL }, END_OF_TESTCASES }; - diff --git a/src/test/test_controller.c b/src/test/test_controller.c index 2ded04619c..d0aa868448 100644 --- a/src/test/test_controller.c +++ b/src/test/test_controller.c @@ -14,6 +14,7 @@ #include "feature/nodelist/nodelist.h" #include "test/test.h" #include "test/test_helpers.h" +#include "lib/net/resolve.h" #include "feature/control/control_connection_st.h" #include "feature/dirclient/download_status_st.h" diff --git a/src/test/test_helpers.c b/src/test/test_helpers.c index c666bca59a..c9138611d8 100644 --- a/src/test/test_helpers.c +++ b/src/test/test_helpers.c @@ -24,6 +24,7 @@ #include "core/or/relay.h" #include "feature/nodelist/routerlist.h" #include "lib/encoding/confline.h" +#include "lib/net/resolve.h" #include "core/or/cell_st.h" #include "core/or/connection_st.h" diff --git a/src/test/test_options.c b/src/test/test_options.c index 396965401e..4e890205b2 100644 --- a/src/test/test_options.c +++ b/src/test/test_options.c @@ -21,6 +21,7 @@ #include "lib/encoding/confline.h" #include "core/or/policies.h" #include "test/test_helpers.h" +#include "lib/net/resolve.h" #define NS_MODULE test_options diff --git a/src/test/test_pt.c b/src/test/test_pt.c index dea3791da2..d0160d1148 100644 --- a/src/test/test_pt.c +++ b/src/test/test_pt.c @@ -19,6 +19,7 @@ #include "test/test.h" #include "lib/process/subprocess.h" #include "lib/encoding/confline.h" +#include "lib/net/resolve.h" #include "app/config/or_state_st.h" diff --git a/src/tools/tor-gencert.c b/src/tools/tor-gencert.c index ce032ed643..efae621d09 100644 --- a/src/tools/tor-gencert.c +++ b/src/tools/tor-gencert.c @@ -41,7 +41,8 @@ ENABLE_GCC_WARNING(redundant-decls) #include "lib/log/torlog.h" #include "lib/malloc/util_malloc.h" #include "lib/net/address.h" -#include "lib/net/ipv4.h" +#include "lib/net/inaddr.h" +#include "lib/net/resolve.h" #include "lib/string/compat_string.h" #include "lib/string/printf.h" @@ -170,19 +171,22 @@ parse_commandline(int argc, char **argv) } else if (!strcmp(argv[i], "-v")) { verbose = 1; } else if (!strcmp(argv[i], "-a")) { - uint32_t addr; + tor_addr_t addr; uint16_t port; - char b[INET_NTOA_BUF_LEN]; - struct in_addr in; if (i+1>=argc) { fprintf(stderr, "No argument to -a\n"); return 1; } - if (addr_port_lookup(LOG_ERR, argv[++i], NULL, &addr, &port)<0) + const char *addr_arg = argv[++i]; + if (tor_addr_port_lookup(addr_arg, &addr, &port)<0) { + fprintf(stderr, "Can't resolve address/port for %s", addr_arg); return 1; - in.s_addr = htonl(addr); - tor_inet_ntoa(&in, b, sizeof(b)); - tor_asprintf(&address, "%s:%d", b, (int)port); + } + if (tor_addr_family(&addr) != AF_INET) { + fprintf(stderr, "%s must resolve to an IPv4 address", addr_arg); + return 1; + } + address = tor_strdup(fmt_addrport(&addr, port)); } else if (!strcmp(argv[i], "--create-identity-key")) { make_new_id = 1; } else if (!strcmp(argv[i], "--passphrase-fd")) { diff --git a/src/tools/tor-resolve.c b/src/tools/tor-resolve.c index 1532d5f201..9358cc8a6e 100644 --- a/src/tools/tor-resolve.c +++ b/src/tools/tor-resolve.c @@ -197,12 +197,14 @@ socks5_reason_to_string(char reason) * address (in host order) into *<b>result_addr</b>. */ static int -do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport, +do_resolve(const char *hostname, + const tor_addr_t *sockshost, uint16_t socksport, int reverse, int version, tor_addr_t *result_addr, char **result_hostname) { int s = -1; - struct sockaddr_in socksaddr; + struct sockaddr_storage ss; + socklen_t socklen; char *req = NULL; ssize_t len = 0; @@ -219,11 +221,10 @@ do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport, return -1; } - memset(&socksaddr, 0, sizeof(socksaddr)); - socksaddr.sin_family = AF_INET; - socksaddr.sin_port = htons(socksport); - socksaddr.sin_addr.s_addr = htonl(sockshost); - if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) { + socklen = tor_addr_to_sockaddr(sockshost, socksport, + (struct sockaddr *)&ss, sizeof(ss)); + + if (connect(s, (struct sockaddr*)&ss, sizeof(socklen))) { log_sock_error("connecting to SOCKS host", s); goto err; } @@ -346,7 +347,7 @@ usage(void) int main(int argc, char **argv) { - uint32_t sockshost; + tor_addr_t sockshost; uint16_t socksport = 0, port_option = 0; int isSocks4 = 0, isVerbose = 0, isReverse = 0; char **arg; @@ -414,7 +415,7 @@ main(int argc, char **argv) if (n_args == 1) { log_debug(LD_CONFIG, "defaulting to localhost"); - sockshost = 0x7f000001u; /* localhost */ + tor_addr_from_ipv4h(&sockshost, 0x7f000001u); /* localhost */ if (port_option) { log_debug(LD_CONFIG, "Using port %d", (int)port_option); socksport = port_option; @@ -423,7 +424,7 @@ main(int argc, char **argv) socksport = 9050; /* 9050 */ } } else if (n_args == 2) { - if (addr_port_lookup(LOG_WARN, arg[1], NULL, &sockshost, &socksport)<0) { + if (tor_addr_port_lookup(arg[1], &sockshost, &socksport)<0) { fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]); return 1; } @@ -445,7 +446,7 @@ main(int argc, char **argv) return 1; } - if (do_resolve(arg[0], sockshost, socksport, isReverse, + if (do_resolve(arg[0], &sockshost, socksport, isReverse, isSocks4 ? 4 : 5, &result, &result_hostname)) return 1; |