1 #ifndef LIGHTNING_LIGHTNINGD_LIGHTNINGD_H
2 #define LIGHTNING_LIGHTNINGD_LIGHTNINGD_H
3 #include "config.h"
4 #include <lightningd/htlc_end.h>
5 #include <lightningd/htlc_set.h>
6 #include <signal.h>
7 #include <sys/stat.h>
8 #include <wallet/wallet.h>
9 
10 struct amount_msat;
11 
12 /* Various adjustable things. */
13 struct config {
14 	/* How long do we want them to lock up their funds? (blocks) */
15 	u32 locktime_blocks;
16 
17 	/* How long do we let them lock up our funds? (blocks) */
18 	u32 locktime_max;
19 
20 	/* How many confirms until we consider an anchor "settled". */
21 	u32 anchor_confirms;
22 
23 	/* Minimum CLTV to subtract from incoming HTLCs to outgoing */
24 	u32 cltv_expiry_delta;
25 
26 	/* Minimum CLTV if we're the final hop.*/
27 	u32 cltv_final;
28 
29 	/* Fee rates. */
30 	u32 fee_base;
31 	u32 fee_per_satoshi;
32 
33 	/* htlcs per channel */
34 	u32 max_concurrent_htlcs;
35 
36 	/* Max amount of dust allowed per channel */
37 	struct amount_msat max_dust_htlc_exposure_msat;
38 
39 	/* How long between changing commit and sending COMMIT message. */
40 	u32 commit_time_ms;
41 
42 	/* Do we let the opener set any fee rate they want */
43 	bool ignore_fee_limits;
44 
45 	/* Number of blocks to rescan from the current head, or absolute
46 	 * blockheight if rescan >= 500'000 */
47 	s32 rescan;
48 
49 	/* ipv6 bind disable */
50 	bool no_ipv6_bind;
51 
52 	/* Are we allowed to use DNS lookup for peers. */
53 	bool use_dns;
54 
55 	/* Minimal amount of effective funding_satoshis for accepting channels */
56 	u64 min_capacity_sat;
57 
58 	/* Allow to define the default behavior of tor services calls*/
59 	bool use_v3_autotor;
60 
61 	/* This is the key we use to encrypt `hsm_secret`. */
62 	struct secret *keypass;
63 
64 	/* How long before we give up waiting for INIT msg */
65 	u32 connection_timeout_secs;
66 
67 	/* EXPERIMENTAL: offers support */
68 	bool exp_offers;
69 };
70 
71 typedef STRMAP(const char *) alt_subdaemon_map;
72 
73 enum lightningd_state {
74 	LD_STATE_RUNNING,
75 	LD_STATE_SHUTDOWN,
76 };
77 
78 struct lightningd {
79 	/* The directory to find all the subdaemons. */
80 	const char *daemon_dir;
81 
82 	/* If we told to run in the background, this is our parent fd, otherwise
83 	 * -1. */
84 	int daemon_parent_fd;
85 
86 	/* Our config basedir, network directory, and rpc file */
87 	char *config_basedir, *config_netdir;
88 
89 	/* Location of the RPC socket. */
90 	char *rpc_filename;
91 	/* Mode of the RPC filename. */
92 	mode_t rpc_filemode;
93 
94 	/* The root of the jsonrpc interface. Can be shut down
95 	 * separately from the rest of the daemon to allow a clean
96 	 * shutdown, which frees all pending cmds in a DB
97 	 * transaction. */
98 	struct jsonrpc *jsonrpc;
99 
100 	/* Configuration file name */
101 	char *config_filename;
102 	/* Configuration settings. */
103 	struct config config;
104 
105 	/* This log_book is owned by all the struct logs */
106 	struct log_book *log_book;
107 	/* Log for general stuff. */
108 	struct log *log;
109 	const char *logfile;
110 
111 	/* This is us. */
112 	struct node_id id;
113 
114 	/* The public base for our payer_id keys */
115 	struct point32 bolt12_base;
116 
117 	/* The secret we put in onion message paths to know it's ours. */
118 	struct secret onion_reply_secret;
119 
120 	/* Feature set we offer. */
121 	struct feature_set *our_features;
122 
123 	/* My name is... my favorite color is... */
124 	u8 *alias; /* At least 32 bytes (zero-filled) */
125 	u8 *rgb; /* tal_len() == 3. */
126 
127 	/* Any pending timers. */
128 	struct timers *timers;
129 
130 	/* Port we're listening on */
131 	u16 portnum;
132 
133 	/* Do we want to reconnect to other peers? */
134 	bool reconnect;
135 
136 	/* Do we want to listen for other peers? */
137 	bool listen;
138 
139 	/* Do we want to guess addresses to listen and announce? */
140 	bool autolisten;
141 
142 	/* Setup: Addresses to bind/announce to the network (tal_count()) */
143 	struct wireaddr_internal *proposed_wireaddr;
144 	/* Setup: And the bitset for each, whether to listen, announce or both */
145 	enum addr_listen_announce *proposed_listen_announce;
146 
147 	/* Actual bindings and announcables from gossipd */
148 	struct wireaddr_internal *binding;
149 	struct wireaddr *announcable;
150 
151 	/* Bearer of all my secrets. */
152 	int hsm_fd;
153 	struct subd *hsm;
154 
155 	/* Daemon for routing */
156  	struct subd *gossip;
157 
158 	/* Daemon looking after peers during init / before channel. */
159 	struct subd *connectd;
160 
161 	/* All peers we're tracking. */
162 	struct list_head peers;
163 
164 	/* Outstanding connect commands. */
165 	struct list_head connects;
166 
167 	/* Our chain topology. */
168 	struct chain_topology *topology;
169 
170 	/* HTLCs in flight. */
171 	struct htlc_in_map htlcs_in;
172 	struct htlc_out_map htlcs_out;
173 
174 	/* Sets of HTLCs we are holding onto for MPP. */
175 	struct htlc_set_map htlc_sets;
176 
177 	struct wallet *wallet;
178 
179 	/* Outstanding waitsendpay commands. */
180 	struct list_head waitsendpay_commands;
181 	/* Outstanding sendpay commands. */
182 	struct list_head sendpay_commands;
183 	/* Outstanding close commands. */
184 	struct list_head close_commands;
185 	/* Outstanding ping commands. */
186 	struct list_head ping_commands;
187 
188 	/* Maintained by invoices.c */
189 	struct invoices *invoices;
190 
191 	/* Transaction filter matching what we're interested in */
192 	struct txfilter *owned_txfilter;
193 
194 	/* PID file */
195 	char *pidfile;
196 
197 	/* RPC which asked us to shutdown, if non-NULL */
198 	struct io_conn *stop_conn;
199 	/* RPC response to send once we've shut down. */
200 	const char *stop_response;
201 
202 	/* Used these feerates instead of whatever bcli returns (up to
203 	 * FEERATE_PENALTY). */
204 	u32 *force_feerates;
205 
206 #if DEVELOPER
207 	/* If we want to debug a subdaemon/plugin. */
208 	const char *dev_debug_subprocess;
209 
210 	/* If we have a --dev-disconnect file */
211 	int dev_disconnect_fd;
212 
213 	/* If we have --dev-fail-on-subdaemon-fail */
214 	bool dev_subdaemon_fail;
215 
216 	/* Allow and accept localhost node_announcement addresses */
217 	bool dev_allow_localhost;
218 
219 	/* Timestamp to use for gossipd, iff non-zero */
220 	u32 dev_gossip_time;
221 
222 	/* Speedup gossip propagation, for testing. */
223 	bool dev_fast_gossip;
224 	bool dev_fast_gossip_prune;
225 
226 	/* Things we've marked as not leaking. */
227 	const void **notleaks;
228 
229 	/* This is the forced private key for the node. */
230 	struct privkey *dev_force_privkey;
231 
232 	/* This is the forced bip32 seed for the node. */
233 	struct secret *dev_force_bip32_seed;
234 
235 	/* These are the forced channel secrets for the node. */
236 	struct secrets *dev_force_channel_secrets;
237 	struct sha256 *dev_force_channel_secrets_shaseed;
238 
239 	struct channel_id *dev_force_tmp_channel_id;
240 
241 	/* For slow tests (eg protocol tests) don't die if HTLC not
242 	 * committed in 30 secs */
243 	bool dev_no_htlc_timeout;
244 
245 	bool dev_no_version_checks;
246 
247 	/* Number of blocks we wait for a channel to get funded
248 	 * if we are the fundee. */
249 	u32 dev_max_funding_unconfirmed;
250 
251 	/* Special switches to test onion compatibility */
252 	bool dev_ignore_modern_onion, dev_ignore_obsolete_onion;
253 #endif /* DEVELOPER */
254 
255 	/* tor support */
256 	struct wireaddr *proxyaddr;
257 	bool always_use_proxy;
258 	char *tor_service_password;
259 	bool pure_tor_setup;
260 
261 	struct plugins *plugins;
262 
263 	char *wallet_dsn;
264 
265 	bool encrypted_hsm;
266 
267 	mode_t initial_umask;
268 
269 	/* Outstanding waitblockheight commands.  */
270 	struct list_head waitblockheight_commands;
271 
272 	alt_subdaemon_map alt_subdaemons;
273 
274 	enum lightningd_state state;
275 
276 	/* Total number of coin moves we've seen, since
277 	 * coin move tracking was cool */
278 	s64 coin_moves_count;
279 
280 	/* If non-NULL, contains the exit code to use.  */
281 	int *exit_code;
282 
283 	/* The round-robin list of channels, for use when doing MPP.  */
284 	u64 rr_counter;
285 
286 	/* Should we re-exec ourselves instead of just exiting? */
287 	bool try_reexec;
288 
289 	/* Array of (even) TLV types that we should allow. This is required
290 	 * since we otherwise would outright reject them. */
291 	u64 *accept_extra_tlv_types;
292 
293 	/* EXPERIMENTAL: websocket port if non-zero */
294 	u16 websocket_port;
295 };
296 
297 /* Turning this on allows a tal allocation to return NULL, rather than aborting.
298  * Use only on carefully tested code! */
299 extern bool tal_oom_ok;
300 
301 /* Returns true if called with a recognized subdaemon, eg: "hsmd" */
302 bool is_subdaemon(const char *sdname);
303 
304 /* Returns the path to the subdaemon. Considers alternate subdaemon paths. */
305 const char *subdaemon_path(const tal_t *ctx, const struct lightningd *ld, const char *name);
306 
307 /* Check we can run subdaemons, and check their versions */
308 void test_subdaemons(const struct lightningd *ld);
309 
310 /* Notify lightningd about new blocks. */
311 void notify_new_block(struct lightningd *ld, u32 block_height);
312 
313 /* Signal a clean exit from lightningd.
314  * NOTE! This function **returns**.
315  * This just causes the main loop to exit, so you have to return
316  * all the way to the main loop for `lightningd` to exit.
317  */
318 void lightningd_exit(struct lightningd *ld, int exit_code);
319 
320 #endif /* LIGHTNING_LIGHTNINGD_LIGHTNINGD_H */
321