diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-09-25 16:22:11 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-09-25 16:22:11 -0400 |
commit | efa978124f067bf75c33c4ff9bba8dbf2f54a4ef (patch) | |
tree | 877cacc564b746bafde6b9cda24d4ea78e1d1ac0 /src/feature/nodelist | |
parent | 5c86f3c297229515f125c0e06abdc689c90768f4 (diff) | |
download | tor-efa978124f067bf75c33c4ff9bba8dbf2f54a4ef.tar.gz tor-efa978124f067bf75c33c4ff9bba8dbf2f54a4ef.zip |
Extract nickname-checking functions from router.c
Diffstat (limited to 'src/feature/nodelist')
-rw-r--r-- | src/feature/nodelist/describe.c | 5 | ||||
-rw-r--r-- | src/feature/nodelist/nickname.c | 62 | ||||
-rw-r--r-- | src/feature/nodelist/nickname.h | 19 |
3 files changed, 86 insertions, 0 deletions
diff --git a/src/feature/nodelist/describe.c b/src/feature/nodelist/describe.c index ccf27e02d6..0ef9e3e7f7 100644 --- a/src/feature/nodelist/describe.c +++ b/src/feature/nodelist/describe.c @@ -4,6 +4,11 @@ * Copyright (c) 2007-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +/** + * \file describe.c + * \brief Format short descriptions of relays. + */ + #include "core/or/or.h" #include "feature/nodelist/describe.h" #include "feature/relay/router.h" diff --git a/src/feature/nodelist/nickname.c b/src/feature/nodelist/nickname.c new file mode 100644 index 0000000000..7b0b29a934 --- /dev/null +++ b/src/feature/nodelist/nickname.c @@ -0,0 +1,62 @@ +/* Copyright (c) 2001 Matej Pfajfar. + * Copyright (c) 2001-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 nickname.c + * \brief Check and manipulate relay nicknames. + */ + +#include "core/or/or.h" +#include "feature/nodelist/nickname.h" + +/** Return true iff <b>s</b> is a valid server nickname. (That is, a string + * containing between 1 and MAX_NICKNAME_LEN characters from + * LEGAL_NICKNAME_CHARACTERS.) */ +int +is_legal_nickname(const char *s) +{ + size_t len; + tor_assert(s); + len = strlen(s); + return len > 0 && len <= MAX_NICKNAME_LEN && + strspn(s,LEGAL_NICKNAME_CHARACTERS) == len; +} + +/** Return true iff <b>s</b> is a valid server nickname or + * hex-encoded identity-key digest. */ +int +is_legal_nickname_or_hexdigest(const char *s) +{ + if (*s!='$') + return is_legal_nickname(s); + else + return is_legal_hexdigest(s); +} + +/** Return true iff <b>s</b> is a valid hex-encoded identity-key + * digest. (That is, an optional $, followed by 40 hex characters, + * followed by either nothing, or = or ~ followed by a nickname, or + * a character other than =, ~, or a hex character.) + */ +int +is_legal_hexdigest(const char *s) +{ + size_t len; + tor_assert(s); + if (s[0] == '$') s++; + len = strlen(s); + if (len > HEX_DIGEST_LEN) { + if (s[HEX_DIGEST_LEN] == '=' || + s[HEX_DIGEST_LEN] == '~') { + if (!is_legal_nickname(s+HEX_DIGEST_LEN+1)) + return 0; + } else { + return 0; + } + } + return (len >= HEX_DIGEST_LEN && + strspn(s,HEX_CHARACTERS)==HEX_DIGEST_LEN); +} diff --git a/src/feature/nodelist/nickname.h b/src/feature/nodelist/nickname.h new file mode 100644 index 0000000000..86d4309918 --- /dev/null +++ b/src/feature/nodelist/nickname.h @@ -0,0 +1,19 @@ +/* Copyright (c) 2001 Matej Pfajfar. + * Copyright (c) 2001-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 nickname.h + * \brief Header file for nickname.c. + **/ + +#ifndef TOR_NICKNAME_H +#define TOR_NICKNAME_H + +int is_legal_nickname(const char *s); +int is_legal_nickname_or_hexdigest(const char *s); +int is_legal_hexdigest(const char *s); + +#endif |