From 56d249e5ad798f65a6cf97ecc9ae22b4c86ddf5b Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 25 Jun 2018 15:18:14 +0000 Subject: [PATCH] Add a MAX_HOSTNAME_LENGTH constant. Fixes #946. --- toxcore/tox.api.h | 17 ++++++++++++++--- toxcore/tox.h | 19 ++++++++++++++++--- toxcore/tox_api.c | 1 + 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/toxcore/tox.api.h b/toxcore/tox.api.h index 241441b8..4e20429e 100644 --- a/toxcore/tox.api.h +++ b/toxcore/tox.api.h @@ -307,6 +307,16 @@ const FILE_ID_LENGTH = 32; */ const MAX_FILENAME_LENGTH = 255; +/** + * Maximum length of a hostname, e.g. proxy or bootstrap node names. + * + * This length includes the NUL byte. Hostnames are NUL-terminated C strings, so + * they are 255 characters plus one NUL byte. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. + */ +const MAX_HOSTNAME_LENGTH = 256; + /******************************************************************************* * @@ -496,8 +506,8 @@ static class options { * The IP address or DNS name of the proxy to be used. * * If used, this must be non-NULL and be a valid DNS name. The name must not - * exceed 255 characters, and be in a NUL-terminated C string format - * (255 chars + 1 NUL byte). + * exceed $MAX_HOSTNAME_LENGTH characters, and be in a NUL-terminated C string + * format ($MAX_HOSTNAME_LENGTH includes the NUL byte). * * This member is ignored (it can be NULL) if proxy_type is ${PROXY_TYPE.NONE}. * @@ -746,7 +756,8 @@ uint8_t[size] savedata { * This function will attempt to connect to the node using UDP. You must use * this function even if ${options.this.udp_enabled} was set to false. * - * @param address The hostname or IP address (IPv4 or IPv6) of the node. + * @param address The hostname or IP address (IPv4 or IPv6) of the node. Must be + * at most $MAX_HOSTNAME_LENGTH chars, including the NUL byte. * @param port The port on the host on which the bootstrap Tox instance is * listening. * @param public_key The long term public key of the bootstrap node diff --git a/toxcore/tox.h b/toxcore/tox.h index 1b4ce7be..b71e1a02 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -332,6 +332,18 @@ uint32_t tox_file_id_length(void); uint32_t tox_max_filename_length(void); +/** + * Maximum length of a hostname, e.g. proxy or bootstrap node names. + * + * This length includes the NUL byte. Hostnames are NUL-terminated C strings, so + * they are 255 characters plus one NUL byte. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. + */ +#define TOX_MAX_HOSTNAME_LENGTH 256 + +uint32_t tox_max_hostname_length(void); + /******************************************************************************* * @@ -551,8 +563,8 @@ struct Tox_Options { * The IP address or DNS name of the proxy to be used. * * If used, this must be non-NULL and be a valid DNS name. The name must not - * exceed 255 characters, and be in a NUL-terminated C string format - * (255 chars + 1 NUL byte). + * exceed TOX_MAX_HOSTNAME_LENGTH characters, and be in a NUL-terminated C string + * format (TOX_MAX_HOSTNAME_LENGTH includes the NUL byte). * * This member is ignored (it can be NULL) if proxy_type is TOX_PROXY_TYPE_NONE. * @@ -913,7 +925,8 @@ typedef enum TOX_ERR_BOOTSTRAP { * This function will attempt to connect to the node using UDP. You must use * this function even if Tox_Options.udp_enabled was set to false. * - * @param address The hostname or IP address (IPv4 or IPv6) of the node. + * @param address The hostname or IP address (IPv4 or IPv6) of the node. Must be + * at most TOX_MAX_HOSTNAME_LENGTH chars, including the NUL byte. * @param port The port on the host on which the bootstrap Tox instance is * listening. * @param public_key The long term public key of the bootstrap node diff --git a/toxcore/tox_api.c b/toxcore/tox_api.c index 8fee8c80..31f21158 100644 --- a/toxcore/tox_api.c +++ b/toxcore/tox_api.c @@ -29,6 +29,7 @@ CONST_FUNCTION(max_custom_packet_size, MAX_CUSTOM_PACKET_SIZE) CONST_FUNCTION(hash_length, HASH_LENGTH) CONST_FUNCTION(file_id_length, FILE_ID_LENGTH) CONST_FUNCTION(max_filename_length, MAX_FILENAME_LENGTH) +CONST_FUNCTION(max_hostname_length, MAX_HOSTNAME_LENGTH) #define ACCESSORS(type, ns, name) \