Skip to content

Commit 5adb0f3

Browse files
lightnind: removeDEFAULT_PORT global definition
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent bc4e6ae commit 5adb0f3

File tree

14 files changed

+51
-22
lines changed

14 files changed

+51
-22
lines changed

bitcoin/chainparams.c

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "config.h"
2+
#include <assert.h>
23
#include <bitcoin/chainparams.h>
34
#include <ccan/array_size/array_size.h>
45
#include <ccan/tal/str/str.h>
@@ -269,3 +270,9 @@ const char *chainparams_get_network_names(const tal_t *ctx)
269270
tal_append_fmt(&networks_string, ", %s", networks[i].network_name);
270271
return networks_string;
271272
}
273+
274+
int chainparams_get_ln_port(const struct chainparams *params)
275+
{
276+
assert(params);
277+
return params->ln_port;
278+
}

bitcoin/chainparams.h

+5
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,9 @@ const struct chainparams *chainparams_by_chainhash(const struct bitcoin_blkid *c
7474
*/
7575
const char *chainparams_get_network_names(const tal_t *ctx);
7676

77+
/**
78+
* chainparams_get_ln_port - Return the lightning network default port by
79+
* network if the chainparams is initialized, otherwise 9735 as mock port
80+
*/
81+
int chainparams_get_ln_port(const struct chainparams *params);
7782
#endif /* LIGHTNING_BITCOIN_CHAINPARAMS_H */

common/test/run-wireaddr.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
#include "config.h"
2-
#include "../wireaddr.c"
2+
#include <bitcoin/chainparams.h>
33
#include <common/amount.h>
44
#include <common/setup.h>
55
#include <stdio.h>
66

7+
int simple_get_ln_port(const struct chainparams *params);
8+
9+
#define chainparams_get_ln_port simple_get_ln_port
10+
11+
#include "../wireaddr.c"
12+
13+
#define DEFAULT_PORT simple_get_ln_port(NULL)
14+
15+
int simple_get_ln_port(const struct chainparams *params UNNEEDED)
16+
{
17+
return 9735;
18+
}
19+
720
/* AUTOGENERATED MOCKS START */
821
/* Generated stub for amount_asset_is_main */
922
bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)

common/wireaddr.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "config.h"
22
#include <arpa/inet.h>
33
#include <assert.h>
4+
#include <bitcoin/chainparams.h>
45
#include <ccan/mem/mem.h>
56
#include <ccan/tal/str/str.h>
67
#include <common/base32.h>
@@ -612,7 +613,7 @@ bool parse_wireaddr_internal(const char *arg, struct wireaddr_internal *addr,
612613
* an onion address. */
613614
if (strstarts(arg, "autotor:")) {
614615
addr->itype = ADDR_INTERNAL_AUTOTOR;
615-
addr->u.torservice.port = DEFAULT_PORT;
616+
addr->u.torservice.port = chainparams_get_ln_port(chainparams);
616617
/* Format is separated by slash. */
617618
char **parts = tal_strsplit(tmpctx, arg, "/", STR_EMPTY_OK);
618619

@@ -644,7 +645,7 @@ bool parse_wireaddr_internal(const char *arg, struct wireaddr_internal *addr,
644645
if (strstarts(arg, "statictor:")) {
645646
bool use_magic_blob = true;
646647
addr->itype = ADDR_INTERNAL_STATICTOR;
647-
addr->u.torservice.port = DEFAULT_PORT;
648+
addr->u.torservice.port = chainparams_get_ln_port(chainparams);
648649
memset(addr->u.torservice.blob, 0, sizeof(addr->u.torservice.blob));
649650

650651
/* Format is separated by slash. */

common/wireaddr.h

-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ struct sockaddr_in6;
1212
struct sockaddr_in;
1313
struct sockaddr_un;
1414

15-
/* BOLT #1:
16-
*
17-
* The default TCP port is 9735. This corresponds to hexadecimal
18-
* `0x2607`: the Unicode code point for LIGHTNING.
19-
*/
20-
#define DEFAULT_PORT 9735
21-
22-
2315
/* BOLT #7:
2416
*
2517
* The following `address descriptor` types are defined:

connectd/connectd.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* it.
99
*/
1010
#include "config.h"
11+
#include <bitcoin/chainparams.h>
1112
#include <ccan/array_size/array_size.h>
1213
#include <ccan/asort/asort.h>
1314
#include <ccan/closefrom/closefrom.h>
@@ -1707,7 +1708,7 @@ static void add_seed_addrs(struct wireaddr_internal **addrs,
17071708

17081709
for (size_t i = 0; i < tal_count(hostnames); i++) {
17091710
status_peer_debug(id, "Resolving %s", hostnames[i]);
1710-
new_addrs = wireaddr_from_hostname(tmpctx, hostnames[i], DEFAULT_PORT,
1711+
new_addrs = wireaddr_from_hostname(tmpctx, hostnames[i], chainparams_get_ln_port(chainparams),
17111712
NULL, broken_reply, NULL);
17121713
if (new_addrs) {
17131714
for (size_t j = 0; j < tal_count(new_addrs); j++) {
@@ -1859,7 +1860,7 @@ static void try_connect_peer(struct daemon *daemon,
18591860
for (size_t i = 0; i < tal_count(hostnames); i++) {
18601861
wireaddr_from_unresolved(&unresolved,
18611862
hostnames[i],
1862-
DEFAULT_PORT);
1863+
chainparams_get_ln_port(chainparams));
18631864
tal_arr_expand(&addrs, unresolved);
18641865
}
18651866
} else if (daemon->use_dns) {

connectd/test/run-netaddress.c

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <stdio.h>
1111
#include <wire/wire.h>
1212

13+
#define DEFAULT_PORT 9735
14+
1315
/* AUTOGENERATED MOCKS START */
1416
/* Generated stub for amount_asset_is_main */
1517
bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)

devtools/gossipwith.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stdio.h>
2222
#include <wire/peer_wire.h>
2323

24+
#define chainparams_get_ln_port simple_get_ln_port
2425
#define io_write_ simple_write
2526
#define io_read_ simple_read
2627
#define io_close simple_close
@@ -45,7 +46,13 @@ static struct io_plan *simple_close(struct io_conn *conn)
4546
return NULL;
4647
}
4748

48-
#include "../connectd/handshake.c"
49+
static int simple_get_ln_port(const struct chainparams *params UNNEEDED)
50+
{
51+
return 9735;
52+
}
53+
54+
#include "../common/wireaddr.h"
55+
#include "../connectd/handshake.c"
4956

5057
/* This makes the handshake prototypes work. */
5158
struct io_conn {
@@ -322,7 +329,7 @@ int main(int argc, char *argv[])
322329
opt_usage_exit_fail("Invalid id %.*s",
323330
(int)(at - argv[1]), argv[1]);
324331

325-
if (!parse_wireaddr_internal(at+1, &addr, DEFAULT_PORT, NULL,
332+
if (!parse_wireaddr_internal(at+1, &addr, simple_get_ln_port(NULL), NULL,
326333
true, false, true, &err_msg))
327334
opt_usage_exit_fail("%s '%s'", err_msg, argv[1]);
328335

@@ -376,4 +383,3 @@ int main(int argc, char *argv[])
376383
handshake_success, argv+2);
377384
exit(0);
378385
}
379-

gossipd/gossipd.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ static void handle_remote_addr(struct daemon *daemon, const u8 *msg)
350350
if (!fromwire_gossipd_remote_addr(msg, &remote_addr))
351351
master_badmsg(WIRE_GOSSIPD_REMOTE_ADDR, msg);
352352

353-
/* current best guess is that we use DEFAULT_PORT on public internet */
354-
remote_addr.port = DEFAULT_PORT;
353+
/* current best guess is that we use default port on public internet */
354+
remote_addr.port = chainparams_get_ln_port(chainparams);
355355

356356
switch (remote_addr.type) {
357357
case ADDR_TYPE_IPV4:

gossipd/test/Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,3 @@ $(GOSSIPD_TEST_PROGRAMS): $(GOSSIPD_TEST_COMMON_OBJS) $(BITCOIN_OBJS)
5353
$(GOSSIPD_TEST_OBJS): $(GOSSIPD_HEADERS) $(GOSSIPD_SRC)
5454

5555
gossipd-tests: $(GOSSIPD_TEST_PROGRAMS:%=unittest/%)
56-

lightningd/connect_control.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static struct command_result *json_connect(struct command *cmd,
141141
/* Is there a port? */
142142
if (!port) {
143143
port = tal(cmd, u32);
144-
*port = chainparams->ln_port;
144+
*port = chainparams_get_ln_port(chainparams);
145145
}
146146
addr = tal(cmd, struct wireaddr_internal);
147147
if (!parse_wireaddr_internal(name, addr, *port, false,

lightningd/lightningd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ int main(int argc, char *argv[])
969969

970970
/*~ Set the default portnum according to the used network
971971
* similarly to what Bitcoin Core does to ports by default. */
972-
ld->portnum = chainparams->ln_port;
972+
ld->portnum = chainparams_get_ln_port(chainparams);
973973

974974
/*~ Initialize all the plugins we just registered, so they can
975975
* do their thing and tell us about themselves (including

wallet/wallet.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
838838

839839
/* This can happen for peers last seen on Torv2! */
840840
addrstr = db_col_strdup(tmpctx, stmt, "address");
841-
if (!parse_wireaddr_internal(addrstr, &addr, DEFAULT_PORT,
841+
if (!parse_wireaddr_internal(addrstr, &addr, chainparams_get_ln_port(chainparams),
842842
false, false, true, true, NULL)) {
843843
log_unusual(w->log, "Unparsable peer address %s: replacing",
844844
addrstr);

wire/test/run-tlvstream.c

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ static const char *reason;
2525
/* Generated stub for chainparams_by_chainhash */
2626
const struct chainparams *chainparams_by_chainhash(const struct bitcoin_blkid *chain_hash UNNEEDED)
2727
{ fprintf(stderr, "chainparams_by_chainhash called!\n"); abort(); }
28+
/* Generate std for chainparams_get_ln_port */
29+
int chainparams_get_ln_port(const struct chainparams *params UNNEEDED)
30+
{ fprintf(stderr, "chainparams_get_ln_port called!\n"); abort(); }
2831
/* Generated stub for fromwire_channel_id */
2932
bool fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
3033
struct channel_id *channel_id UNNEEDED)

0 commit comments

Comments
 (0)