LCOV - code coverage report
Current view: top level - feature/nodelist - nickname.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 20 20 100.0 %
Date: 2021-11-24 03:28:48 Functions: 3 3 100.0 %

          Line data    Source code
       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"
      13             : #include "feature/nodelist/nickname.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        4054 : is_legal_nickname(const char *s)
      20             : {
      21        4054 :   size_t len;
      22        4054 :   tor_assert(s);
      23        4054 :   len = strlen(s);
      24        4054 :   return len > 0 && len <= MAX_NICKNAME_LEN &&
      25        3993 :     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
      31          64 : is_legal_nickname_or_hexdigest(const char *s)
      32             : {
      33          64 :   if (*s!='$')
      34          13 :     return is_legal_nickname(s);
      35             :   else
      36          51 :     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         458 : is_legal_hexdigest(const char *s)
      46             : {
      47         458 :   size_t len;
      48         458 :   tor_assert(s);
      49         458 :   if (s[0] == '$') s++;
      50         458 :   len = strlen(s);
      51         458 :   if (len > HEX_DIGEST_LEN) {
      52          28 :     if (s[HEX_DIGEST_LEN] == '=' ||
      53             :         s[HEX_DIGEST_LEN] == '~') {
      54          17 :       if (!is_legal_nickname(s+HEX_DIGEST_LEN+1))
      55             :         return 0;
      56             :     } else {
      57             :       return 0;
      58             :     }
      59             :   }
      60         447 :   return (len >= HEX_DIGEST_LEN &&
      61         161 :           strspn(s,HEX_CHARACTERS)==HEX_DIGEST_LEN);
      62             : }

Generated by: LCOV version 1.14