From b8532bcb1ec51fcfae4ceff869be116fec4ccbb9 Mon Sep 17 00:00:00 2001 From: George Kadianakis Date: Mon, 17 Dec 2012 14:14:09 +0200 Subject: Add utility functions needed for SOCKS argument parsing. --- src/common/util.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/common/util.h | 4 ++++ 2 files changed, 71 insertions(+) (limited to 'src/common') diff --git a/src/common/util.c b/src/common/util.c index 93e2ba8e1..b2f12bfb6 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -865,6 +865,36 @@ tor_digest_is_zero(const char *digest) return tor_memeq(digest, ZERO_DIGEST, DIGEST_LEN); } +/** Return true if string is a valid '=' string. + * is optional, to indicate the empty string. */ +int +string_is_key_value(const char *string) +{ + /* position of equal sign in string */ + char *equal_sign_pos = NULL; + + tor_assert(string); + + if (strlen(string) < 2) { /* "x=a" is shortest args string */ + log_warn(LD_GENERAL, "'%s' is too short to be a k=v value.", string); + return 0; + } + + equal_sign_pos = strchr(string, '='); + if (!equal_sign_pos) { + log_warn(LD_GENERAL, "'%s' is not a k=v value.", string); + return 0; + } + + /* validate that the '=' is not in the beginning of the string. */ + if (equal_sign_pos == string) { + log_warn(LD_GENERAL, "'%s' is not a valid k=v value.", string); + return 0; + } + + return 1; +} + /** Return true iff the DIGEST256_LEN bytes in digest are all zero. */ int tor_digest256_is_zero(const char *digest) @@ -1249,6 +1279,43 @@ wrap_string(smartlist_t *out, const char *string, size_t width, } } +/** Escape every character of string that belongs to the set of + * characters set. Use escape_char as the character to + * use for escaping. */ +char * +tor_escape_str_for_socks_arg(const char *string) +{ + char *new_string = NULL; + char *new_cp = NULL; + size_t length, new_length; + static const char *chars_to_escape = ";\\"; + + tor_assert(string); + + length = strlen(string); + + if (!length) + return NULL; + /* (new_length > SIZE_MAX) => ((length * 2) + 1 > SIZE_MAX) => + (length*2 > SIZE_MAX - 1) => (length > (SIZE_MAX - 1)/2) */ + if (length > (SIZE_MAX - 1)/2) /* check for overflow */ + return NULL; + + /* this should be enough even if all characters must be escaped */ + new_length = (length * 2) + 1; + + new_string = new_cp = tor_malloc_zero(new_length); + + while (*string) { + if (strchr(chars_to_escape, *string)) + *new_cp++ = '\\'; + + *new_cp++ = *string++; + } + + return new_string; +} + /* ===== * Time * ===== */ diff --git a/src/common/util.h b/src/common/util.h index 911b1b5a3..e3cd72118 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -209,12 +209,16 @@ const char *find_whitespace_eos(const char *s, const char *eos); const char *find_str_at_start_of_line(const char *haystack, const char *needle); int string_is_C_identifier(const char *string); +int string_is_key_value(const char *string); int tor_mem_is_zero(const char *mem, size_t len); int tor_digest_is_zero(const char *digest); int tor_digest256_is_zero(const char *digest); char *esc_for_log(const char *string) ATTR_MALLOC; const char *escaped(const char *string); + +char *tor_escape_str_for_socks_arg(const char *string); + struct smartlist_t; void wrap_string(struct smartlist_t *out, const char *string, size_t width, const char *prefix0, const char *prefixRest); -- cgit v1.2.3 From b5dceab1751dfa12b27b3042a49d90e0b02c2e0c Mon Sep 17 00:00:00 2001 From: George Kadianakis Date: Sat, 9 Feb 2013 18:46:10 +0000 Subject: Fix various issues pointed out by Nick and Andrea. - Document the key=value format. - Constify equal_sign_pos. - Pass some strings that are about to be logged to escape(). - Update documentation and fix some bugs in tor_escape_str_for_socks_arg(). - Use string_is_key_value() in parse_bridge_line(). - Parenthesize a forgotten #define - Add some more comments. - Add some more unit test cases. --- src/common/util.c | 27 +++++++++++++++------------ src/or/config.c | 11 ++++++----- src/or/connection.c | 7 ++++++- src/test/test_util.c | 14 ++++++++++++-- 4 files changed, 39 insertions(+), 20 deletions(-) (limited to 'src/common') diff --git a/src/common/util.c b/src/common/util.c index b2f12bfb6..9aba7d6c5 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -865,30 +865,30 @@ tor_digest_is_zero(const char *digest) return tor_memeq(digest, ZERO_DIGEST, DIGEST_LEN); } -/** Return true if string is a valid '=' string. +/** Return true if string is a valid '=[]' string. * is optional, to indicate the empty string. */ int string_is_key_value(const char *string) { /* position of equal sign in string */ - char *equal_sign_pos = NULL; + const char *equal_sign_pos = NULL; tor_assert(string); - if (strlen(string) < 2) { /* "x=a" is shortest args string */ - log_warn(LD_GENERAL, "'%s' is too short to be a k=v value.", string); + if (strlen(string) < 2) { /* "x=" is shortest args string */ + log_warn(LD_GENERAL, "'%s' is too short to be a k=v value.", escaped(string)); return 0; } equal_sign_pos = strchr(string, '='); if (!equal_sign_pos) { - log_warn(LD_GENERAL, "'%s' is not a k=v value.", string); + log_warn(LD_GENERAL, "'%s' is not a k=v value.", escaped(string)); return 0; } /* validate that the '=' is not in the beginning of the string. */ if (equal_sign_pos == string) { - log_warn(LD_GENERAL, "'%s' is not a valid k=v value.", string); + log_warn(LD_GENERAL, "'%s' is not a valid k=v value.", escaped(string)); return 0; } @@ -1279,9 +1279,10 @@ wrap_string(smartlist_t *out, const char *string, size_t width, } } -/** Escape every character of string that belongs to the set of - * characters set. Use escape_char as the character to - * use for escaping. */ +/** Escape every ";" or "\" character of string. Use + * escape_char as the character to use for escaping. + * The returned string is allocated on the heap and it's the + * responsibility of the caller to free it. */ char * tor_escape_str_for_socks_arg(const char *string) { @@ -1294,8 +1295,8 @@ tor_escape_str_for_socks_arg(const char *string) length = strlen(string); - if (!length) - return NULL; + if (!length) /* If we were given the empty string, return the same. */ + return tor_strdup(""); /* (new_length > SIZE_MAX) => ((length * 2) + 1 > SIZE_MAX) => (length*2 > SIZE_MAX - 1) => (length > (SIZE_MAX - 1)/2) */ if (length > (SIZE_MAX - 1)/2) /* check for overflow */ @@ -1304,7 +1305,7 @@ tor_escape_str_for_socks_arg(const char *string) /* this should be enough even if all characters must be escaped */ new_length = (length * 2) + 1; - new_string = new_cp = tor_malloc_zero(new_length); + new_string = new_cp = tor_malloc(new_length); while (*string) { if (strchr(chars_to_escape, *string)) @@ -1313,6 +1314,8 @@ tor_escape_str_for_socks_arg(const char *string) *new_cp++ = *string++; } + *new_cp = '\0'; /* NUL-terminate the new string */ + return new_string; } diff --git a/src/or/config.c b/src/or/config.c index d057dd8ae..a09dda996 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -2875,14 +2875,14 @@ options_validate(or_options_t *old_options, or_options_t *options, size_t len; len = strlen(options->Socks5ProxyUsername); - if (len < 1 || len > 255) + if (len < 1 || len > MAX_SOCKS5_AUTH_FIELD_SIZE) REJECT("Socks5ProxyUsername must be between 1 and 255 characters."); if (!options->Socks5ProxyPassword) REJECT("Socks5ProxyPassword must be included with Socks5ProxyUsername."); len = strlen(options->Socks5ProxyPassword); - if (len < 1 || len > 255) + if (len < 1 || len > MAX_SOCKS5_AUTH_FIELD_SIZE) REJECT("Socks5ProxyPassword must be between 1 and 255 characters."); } else if (options->Socks5ProxyPassword) REJECT("Socks5ProxyPassword must be included with Socks5ProxyUsername."); @@ -4120,11 +4120,12 @@ parse_bridge_line(const char *line, int validate_only) field = smartlist_get(items, 0); smartlist_del_keeporder(items, 0); - /* If '=', it's a k=v value pair. */ - if (strchr(field, '=')) { + /* If it's a key=value pair, then it's a SOCKS argument for the + transport proxy... */ + if (string_is_key_value(field)) { socks_args = smartlist_new(); smartlist_add(socks_args, field); - } else { /* If no '=', it's the fingerprint. */ + } else { /* ...otherwise, it's the bridge fingerprint. */ fingerprint = field; } diff --git a/src/or/connection.c b/src/or/connection.c index 6bac59b20..b0fbe520b 100644 --- a/src/or/connection.c +++ b/src/or/connection.c @@ -1585,7 +1585,7 @@ get_proxy_type(void) /* One byte for the version, one for the command, two for the port, and four for the addr... and, one more for the username NUL: */ -#define SOCKS4_STANDARD_BUFFER_SIZE 1 + 1 + 2 + 4 + 1 +#define SOCKS4_STANDARD_BUFFER_SIZE (1 + 1 + 2 + 4 + 1) /** Write a proxy request of type (socks4, socks5, https) to conn * for conn->addr:conn->port, authenticating with the auth details given @@ -1688,6 +1688,9 @@ connection_proxy_connect(connection_t *conn, int type) memcpy(buf + 2, &portn, 2); /* port */ memcpy(buf + 4, &ip4addr, 4); /* addr */ + /* Next packet field is the userid. If we have pluggable + transport SOCKS arguments, we have to embed them + there. Otherwise, we use an empty userid. */ if (socks_args_string) { /* place the SOCKS args string: */ tor_assert(strlen(socks_args_string) > 0); tor_assert(buf_size >= @@ -1951,6 +1954,8 @@ connection_read_proxy_handshake(connection_t *conn) break; } + /* Username and password lengths should have been checked + above and during torrc parsing. */ tor_assert(usize <= MAX_SOCKS5_AUTH_FIELD_SIZE && psize <= MAX_SOCKS5_AUTH_FIELD_SIZE); reqsize = 3 + usize + psize; diff --git a/src/test/test_util.c b/src/test/test_util.c index b41f23571..a307a79c8 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -813,9 +813,11 @@ test_util_escape_string_socks(void) test_streq(escaped_string, "First rule: Do not use \\;"); tor_free(escaped_string); - /** Ilegal: Empty string. */ + /** Empty string. */ escaped_string = tor_escape_str_for_socks_arg(""); - test_assert(!escaped_string); + test_assert(escaped_string); + test_streq(escaped_string, ""); + tor_free(escaped_string); /** Escape all characters. */ escaped_string = tor_escape_str_for_socks_arg(";\\;\\"); @@ -823,6 +825,11 @@ test_util_escape_string_socks(void) test_streq(escaped_string, "\\;\\\\\\;\\\\"); tor_free(escaped_string); + escaped_string = tor_escape_str_for_socks_arg(";"); + test_assert(escaped_string); + test_streq(escaped_string, "\\;"); + tor_free(escaped_string); + done: tor_free(escaped_string); } @@ -834,7 +841,10 @@ test_util_string_is_key_value(void *ptr) test_assert(string_is_key_value("key=value")); test_assert(string_is_key_value("k=v")); test_assert(string_is_key_value("key=")); + test_assert(string_is_key_value("x=")); + test_assert(string_is_key_value("xx=")); test_assert(!string_is_key_value("=value")); + test_assert(!string_is_key_value("=x")); test_assert(!string_is_key_value("=")); /* ??? */ -- cgit v1.2.3 From 266f8cddd87f8cf507e094725b3f6028bb8d803b Mon Sep 17 00:00:00 2001 From: George Kadianakis Date: Mon, 11 Feb 2013 13:43:20 +0000 Subject: Refactoring to make parse_bridge_line() unittestable. - Make parse_bridge_line() return a struct. - Make bridge_add_from_config() accept a struct. - Make string_is_key_value() less hysterical. --- src/common/util.c | 13 +++-- src/common/util.h | 2 +- src/or/config.c | 133 ++++++++++++++++++++++++--------------------------- src/or/config.h | 14 ++++++ src/or/entrynodes.c | 57 ++++++++++++++-------- src/or/entrynodes.h | 7 +-- src/test/test_util.c | 18 +++---- 7 files changed, 133 insertions(+), 111 deletions(-) (limited to 'src/common') diff --git a/src/common/util.c b/src/common/util.c index 9aba7d6c5..bcb69f208 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -866,9 +866,10 @@ tor_digest_is_zero(const char *digest) } /** Return true if string is a valid '=[]' string. - * is optional, to indicate the empty string. */ + * is optional, to indicate the empty string. Log at logging + * severity if something ugly happens. */ int -string_is_key_value(const char *string) +string_is_key_value(int severity, const char *string) { /* position of equal sign in string */ const char *equal_sign_pos = NULL; @@ -876,19 +877,21 @@ string_is_key_value(const char *string) tor_assert(string); if (strlen(string) < 2) { /* "x=" is shortest args string */ - log_warn(LD_GENERAL, "'%s' is too short to be a k=v value.", escaped(string)); + tor_log(severity, LD_GENERAL, "'%s' is too short to be a k=v value.", + escaped(string)); return 0; } equal_sign_pos = strchr(string, '='); if (!equal_sign_pos) { - log_warn(LD_GENERAL, "'%s' is not a k=v value.", escaped(string)); + tor_log(severity, LD_GENERAL, "'%s' is not a k=v value.", escaped(string)); return 0; } /* validate that the '=' is not in the beginning of the string. */ if (equal_sign_pos == string) { - log_warn(LD_GENERAL, "'%s' is not a valid k=v value.", escaped(string)); + tor_log(severity, LD_GENERAL, "'%s' is not a valid k=v value.", + escaped(string)); return 0; } diff --git a/src/common/util.h b/src/common/util.h index e3cd72118..624202c8d 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -209,7 +209,7 @@ const char *find_whitespace_eos(const char *s, const char *eos); const char *find_str_at_start_of_line(const char *haystack, const char *needle); int string_is_C_identifier(const char *string); -int string_is_key_value(const char *string); +int string_is_key_value(int severity, const char *string); int tor_mem_is_zero(const char *mem, size_t len); int tor_digest_is_zero(const char *digest); diff --git a/src/or/config.c b/src/or/config.c index a09dda996..9d0d56436 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -481,7 +481,6 @@ static int options_transition_affects_descriptor( const or_options_t *old_options, const or_options_t *new_options); static int check_nickname_list(const char *lst, const char *name, char **msg); -static int parse_bridge_line(const char *line, int validate_only); static int parse_client_transport_line(const char *line, int validate_only); static int parse_server_transport_line(const char *line, int validate_only); @@ -1293,11 +1292,13 @@ options_act(const or_options_t *old_options) if (options->Bridges) { mark_bridge_list(); for (cl = options->Bridges; cl; cl = cl->next) { - if (parse_bridge_line(cl->value, 0)<0) { + bridge_line_t *bridge_line = parse_bridge_line(cl->value); + if (!bridge_line) { log_warn(LD_BUG, "Previously validated Bridge line could not be added!"); return -1; } + bridge_add_from_config(bridge_line); } sweep_bridge_list(); } @@ -2966,8 +2967,10 @@ options_validate(or_options_t *old_options, or_options_t *options, REJECT("If you set UseBridges, you must set TunnelDirConns."); for (cl = options->Bridges; cl; cl = cl->next) { - if (parse_bridge_line(cl->value, 1)<0) - REJECT("Bridge line did not parse. See logs for details."); + bridge_line_t *bridge_line = parse_bridge_line(cl->value); + if (!bridge_line) + REJECT("Bridge line did not parse. See logs for details."); + bridge_line_free(bridge_line); } for (cl = options->ClientTransportPlugin; cl; cl = cl->next) { @@ -4038,8 +4041,10 @@ validate_transport_socks_arguments(const smartlist_t *args) tor_assert(smartlist_len(args) > 0); SMARTLIST_FOREACH_BEGIN(args, const char *, s) { - if (!string_is_key_value(s)) /* arguments should be k=v items */ + if (!string_is_key_value(LOG_WARN, s)) { /* items should be k=v items */ + log_warn(LD_CONFIG, "'%s' is not a k=v item.", s); return -1; + } } SMARTLIST_FOREACH_END(s); socks_string = pt_stringify_socks_args(args); @@ -4059,22 +4064,36 @@ validate_transport_socks_arguments(const smartlist_t *args) return 0; } +/** Deallocate a bridge_line_t structure. */ +/* private */ void +bridge_line_free(bridge_line_t *bridge_line) +{ + if (!bridge_line) + return; + + if (bridge_line->socks_args) { + SMARTLIST_FOREACH(bridge_line->socks_args, char*, s, tor_free(s)); + smartlist_free(bridge_line->socks_args); + } + tor_free(bridge_line->transport_name); + tor_free(bridge_line); +} + /** Read the contents of a Bridge line from line. Return 0 * if the line is well-formed, and -1 if it isn't. If * validate_only is 0, and the line is well-formed, then add - * the bridge described in the line to our internal bridge list. */ -static int -parse_bridge_line(const char *line, int validate_only) + * the bridge described in the line to our internal bridge list. + * + * Bridge line format: + * Bridge [transport] IP:PORT [id-fingerprint] [k=v] [k=v] ... + */ +/* private */ bridge_line_t * +parse_bridge_line(const char *line) { smartlist_t *items = NULL; - int r; char *addrport=NULL, *fingerprint=NULL; - char *transport_name=NULL; char *field=NULL; - tor_addr_t addr; - uint16_t port = 0; - char digest[DIGEST_LEN]; - smartlist_t *socks_args = NULL; + bridge_line_t *bridge_line = tor_malloc_zero(sizeof(bridge_line_t)); items = smartlist_new(); smartlist_split_string(items, line, NULL, @@ -4084,47 +4103,49 @@ parse_bridge_line(const char *line, int validate_only) goto err; } - /* field is either a transport name or addrport */ + /* first field is either a transport name or addrport */ field = smartlist_get(items, 0); smartlist_del_keeporder(items, 0); - if (!(strstr(field, ".") || strstr(field, ":"))) { - /* new-style bridge line */ - transport_name = field; + if (string_is_C_identifier(field)) { + /* It's a transport name. */ + bridge_line->transport_name = field; if (smartlist_len(items) < 1) { log_warn(LD_CONFIG, "Too few items to Bridge line."); goto err; } - addrport = smartlist_get(items, 0); + addrport = smartlist_get(items, 0); /* Next field is addrport then. */ smartlist_del_keeporder(items, 0); } else { addrport = field; } - if (tor_addr_port_lookup(addrport, &addr, &port)<0) { + /* Parse addrport. */ + if (tor_addr_port_lookup(addrport, + &bridge_line->addr, &bridge_line->port)<0) { log_warn(LD_CONFIG, "Error parsing Bridge address '%s'", addrport); goto err; } - if (!port) { + if (!bridge_line->port) { log_info(LD_CONFIG, "Bridge address '%s' has no port; using default port 443.", addrport); - port = 443; + bridge_line->port = 443; } /* If transports are enabled, next field could be a fingerprint or a - socks argument. If transports are disabled, next field should be + socks argument. If transports are disabled, next field must be a fingerprint. */ if (smartlist_len(items)) { - if (transport_name) { /* transports enabled: */ + if (bridge_line->transport_name) { /* transports enabled: */ field = smartlist_get(items, 0); smartlist_del_keeporder(items, 0); /* If it's a key=value pair, then it's a SOCKS argument for the transport proxy... */ - if (string_is_key_value(field)) { - socks_args = smartlist_new(); - smartlist_add(socks_args, field); + if (string_is_key_value(LOG_DEBUG, field)) { + bridge_line->socks_args = smartlist_new(); + smartlist_add(bridge_line->socks_args, field); } else { /* ...otherwise, it's the bridge fingerprint. */ fingerprint = field; } @@ -4134,78 +4155,50 @@ parse_bridge_line(const char *line, int validate_only) } } + /* Handle fingerprint, if it was provided. */ if (fingerprint) { if (strlen(fingerprint) != HEX_DIGEST_LEN) { log_warn(LD_CONFIG, "Key digest for Bridge is wrong length."); goto err; } - if (base16_decode(digest, DIGEST_LEN, fingerprint, HEX_DIGEST_LEN)<0) { + if (base16_decode(bridge_line->digest, DIGEST_LEN, + fingerprint, HEX_DIGEST_LEN)<0) { log_warn(LD_CONFIG, "Unable to decode Bridge key digest."); goto err; } } /* If we are using transports, any remaining items in the smartlist - must be k=v values. */ - if (transport_name && smartlist_len(items)) { - if (!socks_args) - socks_args = smartlist_new(); + should be k=v values. */ + if (bridge_line->transport_name && smartlist_len(items)) { + if (!bridge_line->socks_args) + bridge_line->socks_args = smartlist_new(); /* append remaining items of 'items' to 'socks_args' */ - smartlist_add_all(socks_args, items); + smartlist_add_all(bridge_line->socks_args, items); smartlist_clear(items); - tor_assert(smartlist_len(socks_args) > 0); + tor_assert(smartlist_len(bridge_line->socks_args) > 0); } - if (!validate_only) { - log_debug(LD_DIR, "Bridge at %s (transport: %s) (%s)", - fmt_addrport(&addr, port), - transport_name ? transport_name : "no transport", - fingerprint ? fingerprint : "no key listed"); - - if (socks_args) { /* print socks arguments */ - int i = 0; - - tor_assert(smartlist_len(socks_args) > 0); - - log_debug(LD_DIR, "Bridge uses %d SOCKS arguments:", - smartlist_len(socks_args)); - SMARTLIST_FOREACH(socks_args, const char *, arg, - log_debug(LD_CONFIG, "%d: %s", ++i, arg)); - } - - bridge_add_from_config(&addr, port, - fingerprint ? digest : NULL, - transport_name, socks_args); - } else { - if (socks_args) { - if (validate_transport_socks_arguments(socks_args) < 0) - goto err; - } + if (bridge_line->socks_args) { + if (validate_transport_socks_arguments(bridge_line->socks_args) < 0) + goto err; } - r = 0; goto done; err: - r = -1; + bridge_line_free(bridge_line); + bridge_line = NULL; done: SMARTLIST_FOREACH(items, char*, s, tor_free(s)); smartlist_free(items); tor_free(addrport); - tor_free(transport_name); tor_free(fingerprint); - /* We only have to free socks_args if we are validating, since - otherwise bridge_add_from_config() steals its reference. */ - if (socks_args && validate_only) { - SMARTLIST_FOREACH(socks_args, char*, s, tor_free(s)); - smartlist_free(socks_args); - } - - return r; + return bridge_line; } /** Read the contents of a ClientTransportPlugin line from diff --git a/src/or/config.h b/src/or/config.h index 8e3465580..b5c0c734b 100644 --- a/src/or/config.h +++ b/src/or/config.h @@ -96,5 +96,19 @@ int addressmap_register_auto(const char *from, const char *to, addressmap_entry_source_t addrmap_source, const char **msg); +/** Represents the information stored in a torrc Bridge line. */ +typedef struct bridge_line_t { + tor_addr_t addr; /* The IP address of the bridge. */ + uint16_t port; /* The TCP port of the bridge. */ + char *transport_name; /* The name of the pluggable transport that + should be used to connect to the bridge. */ + char digest[DIGEST_LEN]; /* The bridge's identity key digest. */ + smartlist_t *socks_args;; /* SOCKS arguments for the pluggable + transport proxy. */ +} bridge_line_t; + +void bridge_line_free(bridge_line_t *bridge_line); +bridge_line_t *parse_bridge_line(const char *line); + #endif diff --git a/src/or/entrynodes.c b/src/or/entrynodes.c index a07670bbd..44041d35d 100644 --- a/src/or/entrynodes.c +++ b/src/or/entrynodes.c @@ -1633,37 +1633,52 @@ bridge_resolve_conflicts(const tor_addr_t *addr, uint16_t port, } SMARTLIST_FOREACH_END(bridge); } -/** Remember a new bridge at addr:port. If digest - * is set, it tells us the identity key too. If we already had the - * bridge in our list, unmark it, and don't actually add anything new. - * If transport_name is non-NULL - the bridge is associated with a - * pluggable transport - we assign the transport to the bridge. - * If transport_name is non-NULL - the bridge is associated - * with a pluggable transport - we assign the transport to the bridge. - * If socks_args is non-NULL, it's a smartlist carrying - * key=value pairs to be passed to the pluggable transports - * proxy. This function steals reference of the smartlist. */ +/** Register the bridge information in bridge_line to the + * bridge subsystem. Steals reference of bridge_line. */ void -bridge_add_from_config(const tor_addr_t *addr, uint16_t port, - const char *digest, const char *transport_name, - smartlist_t *socks_args) +bridge_add_from_config(bridge_line_t *bridge_line) { bridge_info_t *b; - bridge_resolve_conflicts(addr, port, digest, transport_name); + { /* Log the bridge we are about to register: */ + log_debug(LD_GENERAL, "Registering bridge at %s (transport: %s) (%s)", + fmt_addrport(&bridge_line->addr, bridge_line->port), + bridge_line->transport_name ? + bridge_line->transport_name : "no transport", + tor_digest_is_zero(bridge_line->digest) ? + "no key listed" : hex_str(bridge_line->digest, DIGEST_LEN)); + + if (bridge_line->socks_args) { /* print socks arguments */ + int i = 0; + + tor_assert(smartlist_len(bridge_line->socks_args) > 0); + + log_debug(LD_GENERAL, "Bridge uses %d SOCKS arguments:", + smartlist_len(bridge_line->socks_args)); + SMARTLIST_FOREACH(bridge_line->socks_args, const char *, arg, + log_debug(LD_CONFIG, "%d: %s", ++i, arg)); + } + } + + bridge_resolve_conflicts(&bridge_line->addr, + bridge_line->port, + bridge_line->digest, + bridge_line->transport_name); b = tor_malloc_zero(sizeof(bridge_info_t)); - tor_addr_copy(&b->addr, addr); - b->port = port; - if (digest) - memcpy(b->identity, digest, DIGEST_LEN); - if (transport_name) - b->transport_name = tor_strdup(transport_name); + tor_addr_copy(&b->addr, &bridge_line->addr); + b->port = bridge_line->port; + if (bridge_line->digest) + memcpy(b->identity, bridge_line->digest, DIGEST_LEN); + if (bridge_line->transport_name) + b->transport_name = bridge_line->transport_name; b->fetch_status.schedule = DL_SCHED_BRIDGE; - b->socks_args = socks_args; + b->socks_args = bridge_line->socks_args; if (!bridge_list) bridge_list = smartlist_new(); + tor_free(bridge_line); /* Deallocate bridge_line now. */ + smartlist_add(bridge_list, b); } diff --git a/src/or/entrynodes.h b/src/or/entrynodes.h index 48f678a18..6a4bcea48 100644 --- a/src/or/entrynodes.h +++ b/src/or/entrynodes.h @@ -97,11 +97,8 @@ int routerinfo_is_a_configured_bridge(const routerinfo_t *ri); int node_is_a_configured_bridge(const node_t *node); void learned_router_identity(const tor_addr_t *addr, uint16_t port, const char *digest); -struct smartlist_t; -void bridge_add_from_config(const tor_addr_t *addr, uint16_t port, - const char *digest, - const char *transport_name, - struct smartlist_t *socks_args); +struct bridge_line_t; +void bridge_add_from_config(struct bridge_line_t *bridge_line); void retry_bridge_descriptor_fetch_directly(const char *digest); void fetch_bridge_descriptors(const or_options_t *options, time_t now); void learned_bridge_descriptor(routerinfo_t *ri, int from_cache); diff --git a/src/test/test_util.c b/src/test/test_util.c index a307a79c8..606f8316a 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -838,17 +838,17 @@ static void test_util_string_is_key_value(void *ptr) { (void)ptr; - test_assert(string_is_key_value("key=value")); - test_assert(string_is_key_value("k=v")); - test_assert(string_is_key_value("key=")); - test_assert(string_is_key_value("x=")); - test_assert(string_is_key_value("xx=")); - test_assert(!string_is_key_value("=value")); - test_assert(!string_is_key_value("=x")); - test_assert(!string_is_key_value("=")); + test_assert(string_is_key_value(LOG_WARN, "key=value")); + test_assert(string_is_key_value(LOG_WARN, "k=v")); + test_assert(string_is_key_value(LOG_WARN, "key=")); + test_assert(string_is_key_value(LOG_WARN, "x=")); + test_assert(string_is_key_value(LOG_WARN, "xx=")); + test_assert(!string_is_key_value(LOG_WARN, "=value")); + test_assert(!string_is_key_value(LOG_WARN, "=x")); + test_assert(!string_is_key_value(LOG_WARN, "=")); /* ??? */ - /* test_assert(!string_is_key_value("===")); */ + /* test_assert(!string_is_key_value(LOG_WARN, "===")); */ done: ; } -- cgit v1.2.3