Tor  0.4.7.0-alpha-dev
nickname.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file nickname.c
9  * \brief Check and manipulate relay nicknames.
10  */
11 
12 #include "core/or/or.h"
14 
15 /** Return true iff <b>s</b> is a valid server nickname. (That is, a string
16  * containing between 1 and MAX_NICKNAME_LEN characters from
17  * LEGAL_NICKNAME_CHARACTERS.) */
18 int
19 is_legal_nickname(const char *s)
20 {
21  size_t len;
22  tor_assert(s);
23  len = strlen(s);
24  return len > 0 && len <= MAX_NICKNAME_LEN &&
25  strspn(s,LEGAL_NICKNAME_CHARACTERS) == len;
26 }
27 
28 /** Return true iff <b>s</b> is a valid server nickname or
29  * hex-encoded identity-key digest. */
30 int
32 {
33  if (*s!='$')
34  return is_legal_nickname(s);
35  else
36  return is_legal_hexdigest(s);
37 }
38 
39 /** Return true iff <b>s</b> is a valid hex-encoded identity-key
40  * digest. (That is, an optional $, followed by 40 hex characters,
41  * followed by either nothing, or = or ~ followed by a nickname, or
42  * a character other than =, ~, or a hex character.)
43  */
44 int
45 is_legal_hexdigest(const char *s)
46 {
47  size_t len;
48  tor_assert(s);
49  if (s[0] == '$') s++;
50  len = strlen(s);
51  if (len > HEX_DIGEST_LEN) {
52  if (s[HEX_DIGEST_LEN] == '=' ||
53  s[HEX_DIGEST_LEN] == '~') {
55  return 0;
56  } else {
57  return 0;
58  }
59  }
60  return (len >= HEX_DIGEST_LEN &&
61  strspn(s,HEX_CHARACTERS)==HEX_DIGEST_LEN);
62 }
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
int is_legal_nickname(const char *s)
Definition: nickname.c:19
int is_legal_nickname_or_hexdigest(const char *s)
Definition: nickname.c:31
int is_legal_hexdigest(const char *s)
Definition: nickname.c:45
Header file for nickname.c.
Master header file for Tor-specific functionality.
#define MAX_NICKNAME_LEN
Definition: or.h:112
#define LEGAL_NICKNAME_CHARACTERS
Definition: or.h:433
#define tor_assert(expr)
Definition: util_bug.h:102
#define HEX_CHARACTERS
Definition: util_string.h:31