decorated ipv6 address for directory send command

This commit is contained in:
Malek 2016-02-06 16:30:49 -05:00 committed by Nick Mathewson
parent 1f5cdf2b6c
commit 061586e36c

View File

@ -1159,6 +1159,23 @@ directory_get_consensus_url(const char *resource)
return url; return url;
} }
/**
* Copies the ipv6 from source to destination, subject to buffer size limit
* size. If decorate is true, makes sure the copied address is decorated.
*/
static void
copy_ipv6_address(char* destination, const char* source, size_t len,
int decorate) {
tor_assert(destination);
tor_assert(source);
if (decorate && source[0] != '[') {
tor_snprintf(destination, len, "[%s]", source);
} else {
strlcpy(destination, source, len);
}
}
/** Queue an appropriate HTTP command on conn-\>outbuf. The other args /** Queue an appropriate HTTP command on conn-\>outbuf. The other args
* are as in directory_initiate_command(). * are as in directory_initiate_command().
*/ */
@ -1170,6 +1187,9 @@ directory_send_command(dir_connection_t *conn,
{ {
char proxystring[256]; char proxystring[256];
char hoststring[128]; char hoststring[128];
/* NEEDS to be the same size hoststring.
Will be decorated with brackets around it if it is ipv6. */
char decorated_address[128];
smartlist_t *headers = smartlist_new(); smartlist_t *headers = smartlist_new();
char *url; char *url;
char request[8192]; char request[8192];
@ -1182,12 +1202,20 @@ directory_send_command(dir_connection_t *conn,
if (resource) if (resource)
conn->requested_resource = tor_strdup(resource); conn->requested_resource = tor_strdup(resource);
/* decorate the ip address if it is ipv6 */
if(strchr(conn->base_.address, ':')) {
copy_ipv6_address(decorated_address, conn->base_.address,
sizeof(decorated_address), 1);
} else {
strlcpy(decorated_address, conn->base_.address, sizeof(decorated_address));
}
/* come up with a string for which Host: we want */ /* come up with a string for which Host: we want */
if (conn->base_.port == 80) { if (conn->base_.port == 80) {
strlcpy(hoststring, conn->base_.address, sizeof(hoststring)); strlcpy(hoststring, decorated_address, sizeof(hoststring));
} else { } else {
tor_snprintf(hoststring, sizeof(hoststring),"%s:%d", tor_snprintf(hoststring, sizeof(hoststring), "%s:%d",
conn->base_.address, conn->base_.port); decorated_address, conn->base_.port);
} }
/* Format if-modified-since */ /* Format if-modified-since */
@ -4157,4 +4185,3 @@ dir_split_resource_into_fingerprints(const char *resource,
smartlist_free(fp_tmp); smartlist_free(fp_tmp);
return 0; return 0;
} }