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 dirclient.h
9  * \brief Header file for dirclient.c.
10  **/
11 
12 #ifndef TOR_DIRCLIENT_H
13 #define TOR_DIRCLIENT_H
14 
15 #include "feature/hs/hs_ident.h"
16 
17 void dirclient_dump_total_dls(void);
18 
19 int directories_have_accepted_server_descriptor(void);
20 void directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose,
21                                   dirinfo_type_t type, const char *payload,
22                                   size_t payload_len, size_t extrainfo_len);
23 MOCK_DECL(void, directory_get_from_dirserver, (
24                           uint8_t dir_purpose,
25                           uint8_t router_purpose,
26                           const char *resource,
27                           int pds_flags,
28                           download_want_authority_t want_authority));
29 void directory_get_from_all_authorities(uint8_t dir_purpose,
30                                         uint8_t router_purpose,
31                                         const char *resource);
32 
33 /** Enumeration of ways to connect to a directory server */
34 typedef enum {
35   /** Default: connect over a one-hop Tor circuit. Relays fall back to direct
36    * DirPort connections, clients, onion services, and bridges do not */
37   DIRIND_ONEHOP=0,
38   /** Connect over a multi-hop anonymizing Tor circuit */
39   DIRIND_ANONYMOUS=1,
40   /** Connect to the DirPort directly */
41   DIRIND_DIRECT_CONN,
42   /** Connect over a multi-hop anonymizing Tor circuit to our dirport */
43   DIRIND_ANON_DIRPORT,
44 } dir_indirection_t;
45 
46 /**
47  * A directory_request_t describes the information about a directory request
48  * at the client side.  It describes what we're going to ask for, which
49  * directory we're going to ask for it, how we're going to contact that
50  * directory, and (in some cases) what to do with it when we're done.
51  */
52 typedef struct directory_request_t directory_request_t;
53 directory_request_t *directory_request_new(uint8_t dir_purpose);
54 void directory_request_free_(directory_request_t *req);
55 #define directory_request_free(req) \
56   FREE_AND_NULL(directory_request_t, directory_request_free_, (req))
57 void directory_request_set_or_addr_port(directory_request_t *req,
58                                         const tor_addr_port_t *p);
59 void directory_request_set_dir_addr_port(directory_request_t *req,
60                                          const tor_addr_port_t *p);
61 void directory_request_set_directory_id_digest(directory_request_t *req,
62                                                const char *digest);
63 struct circuit_guard_state_t;
64 void directory_request_set_guard_state(directory_request_t *req,
65                                        struct circuit_guard_state_t *state);
66 void directory_request_set_router_purpose(directory_request_t *req,
67                                           uint8_t router_purpose);
68 void directory_request_set_indirection(directory_request_t *req,
69                                        dir_indirection_t indirection);
70 void directory_request_set_resource(directory_request_t *req,
71                                     const char *resource);
72 void directory_request_set_payload(directory_request_t *req,
73                                    const char *payload,
74                                    size_t payload_len);
75 void directory_request_set_if_modified_since(directory_request_t *req,
76                                              time_t if_modified_since);
77 void directory_request_upload_set_hs_ident(directory_request_t *req,
78                                            const hs_ident_dir_conn_t *ident);
79 void directory_request_fetch_set_hs_ident(directory_request_t *req,
80                                           const hs_ident_dir_conn_t *ident);
81 
82 void directory_request_set_routerstatus(directory_request_t *req,
83                                         const routerstatus_t *rs);
84 void directory_request_add_header(directory_request_t *req,
85                                   const char *key,
86                                   const char *val);
87 MOCK_DECL(void, directory_initiate_request, (directory_request_t *request));
88 
89 int router_supports_extrainfo(const char *identity_digest, int is_authority);
90 
91 void connection_dir_client_request_failed(dir_connection_t *conn);
92 void connection_dir_client_refetch_hsdesc_if_needed(
93                                           dir_connection_t *dir_conn);
94 
95 #ifdef DIRCLIENT_PRIVATE
96 struct directory_request_t {
97   /**
98    * These fields specify which directory we're contacting.  Routerstatus,
99    * if present, overrides the other fields.
100    *
101    * @{ */
102   tor_addr_port_t or_addr_port;
103   tor_addr_port_t dir_addr_port;
104   char digest[DIGEST_LEN];
105 
106   const routerstatus_t *routerstatus;
107   /** @} */
108   /** One of DIR_PURPOSE_* other than DIR_PURPOSE_SERVER. Describes what
109    * kind of operation we'll be doing (upload/download), and of what kind
110    * of document. */
111   uint8_t dir_purpose;
112   /** One of ROUTER_PURPOSE_*; used for uploads and downloads of routerinfo
113    * and extrainfo docs.  */
114   uint8_t router_purpose;
115   /** Enum: determines whether to anonymize, and whether to use dirport or
116    * orport. */
117   dir_indirection_t indirection;
118   /** Alias to the variable part of the URL for this request */
119   const char *resource;
120   /** Alias to the payload to upload (if any) */
121   const char *payload;
122   /** Number of bytes to upload from payload</b> */
123   size_t payload_len;
124   /** Value to send in an if-modified-since header, or 0 for none. */
125   time_t if_modified_since;
126   /** Extra headers to append to the request */
127   struct config_line_t *additional_headers;
128   /** Hidden-service-specific information for v3+. */
129   const hs_ident_dir_conn_t *hs_ident;
130   /** Used internally to directory.c: gets informed when the attempt to
131    * connect to the directory succeeds or fails, if that attempt bears on the
132    * directory's usability as a directory guard. */
133   struct circuit_guard_state_t *guard_state;
134 };
135 
136 /** A structure to hold arguments passed into each directory response
137  * handler */
138 typedef struct response_handler_args_t {
139   int status_code;
140   const char *reason;
141   const char *body;
142   size_t body_len;
143   const char *headers;
144 } response_handler_args_t;
145 
146 enum compress_method_t;
147 STATIC int allowed_anonymous_connection_compression_method(
148                                                enum compress_method_t);
149 STATIC void warn_disallowed_anonymous_compression_method(
150                                                enum compress_method_t);
151 
152 STATIC int should_use_directory_guards(const or_options_t *options);
153 STATIC char *accept_encoding_header(void);
154 STATIC const char *dir_conn_purpose_to_string(int purpose);
155 
156 STATIC int handle_response_fetch_hsdesc_v3(dir_connection_t *conn,
157                                           const response_handler_args_t *args);
158 STATIC int handle_response_fetch_microdesc(dir_connection_t *conn,
159                                  const response_handler_args_t *args);
160 
161 STATIC int handle_response_fetch_consensus(dir_connection_t *conn,
162                                          const response_handler_args_t *args);
163 
164 STATIC dirinfo_type_t dir_fetch_type(int dir_purpose, int router_purpose,
165                                      const char *resource);
166 #endif /* defined(DIRCLIENT_PRIVATE) */
167 
168 #endif /* !defined(TOR_DIRCLIENT_H) */
169