From 83e76e3ac3e7b3eccd948c13d94be7cfe9a20de3 Mon Sep 17 00:00:00 2001 From: ZmnSCPxj Date: Fri, 12 Jan 2018 13:35:52 +0000 Subject: [PATCH] features: Move feature-handling code to a common/features.c source. --- common/Makefile | 1 + common/features.c | 62 +++++++++++++++++++++++++++++++++ common/features.h | 14 ++++++++ lightningd/Makefile | 1 + lightningd/gossip_control.c | 1 + lightningd/peer_control.c | 69 ++++--------------------------------- lightningd/peer_control.h | 7 ---- 7 files changed, 86 insertions(+), 69 deletions(-) create mode 100644 common/features.c create mode 100644 common/features.h diff --git a/common/Makefile b/common/Makefile index b254a43de..70eefc16c 100644 --- a/common/Makefile +++ b/common/Makefile @@ -11,6 +11,7 @@ COMMON_SRC := \ common/daemon_conn.c \ common/derive_basepoints.c \ common/dev_disconnect.c \ + common/features.c \ common/funding_tx.c \ common/hash_u5.c \ common/htlc_state.c \ diff --git a/common/features.c b/common/features.c new file mode 100644 index 000000000..a710ee458 --- /dev/null +++ b/common/features.c @@ -0,0 +1,62 @@ +#include "features.h" +#include + +static const u8 supported_local_features[] += {LOCALFEATURES_INITIAL_ROUTING_SYNC}; +static const u8 supported_global_features[] += {}; + +u8 *get_supported_global_features(const tal_t *ctx) +{ + return tal_dup_arr(ctx, u8, supported_global_features, + sizeof(supported_global_features), 0); +} + +u8 *get_supported_local_features(const tal_t *ctx) +{ + return tal_dup_arr(ctx, u8, supported_local_features, + sizeof(supported_local_features), 0); +} + +/** + * requires_unsupported_features - Check if we support what's being asked + * + * Given the features vector that the remote connection is expecting + * from us, we check to see if we support all even bit features, i.e., + * the required features. We do so by subtracting our own features in + * the provided positions and see if even bits remain. + * + * @bitmap: the features bitmap the peer is asking for + * @supportmap: what do we support + * @smlen: how long is our supportmap + */ +static bool requires_unsupported_features(const u8 *bitmap, + const u8 *supportmap, + size_t smlen) +{ + size_t len = tal_count(bitmap); + u8 support; + for (size_t i=0; i smlen) { + support = 0x00; + } else { + support = supportmap[smlen-1]; + } + + /* Cancel out supported bits, check for even bits */ + if ((~support & bitmap[i]) & 0x55) + return true; + } + return false; +} + +bool unsupported_features(const u8 *gfeatures, const u8 *lfeatures) +{ + return requires_unsupported_features(gfeatures, + supported_global_features, + sizeof(supported_global_features)) + || requires_unsupported_features(lfeatures, + supported_local_features, + sizeof(supported_local_features)); +} diff --git a/common/features.h b/common/features.h new file mode 100644 index 000000000..5a5c268e0 --- /dev/null +++ b/common/features.h @@ -0,0 +1,14 @@ +#ifndef LIGHTNING_COMMON_FEATURES_H +#define LIGHTNING_COMMON_FEATURES_H +#include "config.h" +#include +#include + +/* Returns true if these contain any unsupported features. */ +bool unsupported_features(const u8 *gfeatures, const u8 *lfeatures); + +/* For sending our features: tal_len() returns length. */ +u8 *get_supported_global_features(const tal_t *ctx); +u8 *get_supported_local_features(const tal_t *ctx); + +#endif /* LIGHTNING_COMMON_FEATURES_H */ diff --git a/lightningd/Makefile b/lightningd/Makefile index 8c5fde517..3ce06d2f7 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -21,6 +21,7 @@ LIGHTNINGD_COMMON_OBJS := \ common/configdir.o \ common/crypto_state.o \ common/derive_basepoints.o \ + common/features.o \ common/funding_tx.o \ common/hash_u5.o \ common/htlc_state.o \ diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 2dbf23f27..0e0910a17 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 5c507b196..ee85da6d6 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -44,11 +45,6 @@ #include #include -static const u8 supported_local_features[] -= {LOCALFEATURES_INITIAL_ROUTING_SYNC}; -static const u8 supported_global_features[] -= {}; - struct connect { struct list_node list; struct pubkey id; @@ -126,18 +122,6 @@ static void destroy_peer(struct peer *peer) list_del_from(&peer->ld->peers, &peer->list); } -u8 *get_supported_global_features(const tal_t *ctx) -{ - return tal_dup_arr(ctx, u8, supported_global_features, - sizeof(supported_global_features), 0); -} - -u8 *get_supported_local_features(const tal_t *ctx) -{ - return tal_dup_arr(ctx, u8, supported_local_features, - sizeof(supported_local_features), 0); -} - static void sign_last_tx(struct peer *peer) { const tal_t *tmpctx = tal_tmpctx(peer); @@ -402,49 +386,6 @@ static struct peer *new_peer(struct lightningd *ld, return peer; } -/** - * requires_unsupported_features - Check if we support what's being asked - * - * Given the features vector that the remote connection is expecting - * from us, we check to see if we support all even bit features, i.e., - * the required features. We do so by subtracting our own features in - * the provided positions and see if even bits remain. - * - * @bitmap: the features bitmap the peer is asking for - * @supportmap: what do we support - * @smlen: how long is our supportmap - */ -static bool requires_unsupported_features(const u8 *bitmap, - const u8 *supportmap, - size_t smlen) -{ - size_t len = tal_count(bitmap); - u8 support; - for (size_t i=0; i smlen) { - support = 0x00; - } else { - support = supportmap[smlen-1]; - } - - /* Cancel out supported bits, check for even bits */ - if ((~support & bitmap[i]) & 0x55) - return true; - } - return false; -} - -bool unsupported_features(const u8 *gfeatures, const u8 *lfeatures) -{ - return requires_unsupported_features(gfeatures, - supported_global_features, - sizeof(supported_global_features)) - || requires_unsupported_features(lfeatures, - supported_local_features, - sizeof(supported_local_features)); -} - /** * peer_channel_new -- Instantiate a new channel for the given peer and save it * @@ -524,6 +465,8 @@ void peer_connected(struct lightningd *ld, const u8 *msg, struct crypto_state cs; u8 *gfeatures, *lfeatures; u8 *error; + u8 *supported_global_features; + u8 *supported_local_features; struct peer *peer; struct wireaddr addr; u64 gossip_index; @@ -539,15 +482,17 @@ void peer_connected(struct lightningd *ld, const u8 *msg, type_to_string(msg, struct pubkey, &id), tal_hex(msg, gfeatures), tal_hex(msg, lfeatures)); + supported_global_features = get_supported_global_features(msg); + supported_local_features = get_supported_local_features(msg); error = towire_errorfmt(msg, NULL, "We only support globalfeatures %s" " and localfeatures %s", tal_hexstr(msg, supported_global_features, - sizeof(supported_global_features)), + tal_len(supported_global_features)), tal_hexstr(msg, supported_local_features, - sizeof(supported_local_features))); + tal_len(supported_local_features))); goto send_error; } diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index 7b58cfc5c..31d8640b2 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -198,13 +198,6 @@ void peer_sent_nongossip(struct lightningd *ld, */ void populate_peer(struct lightningd *ld, struct peer *peer); -/* Returns true if these contain any unsupported features. */ -bool unsupported_features(const u8 *gfeatures, const u8 *lfeatures); - -/* For sending our features: tal_len() returns length. */ -u8 *get_supported_global_features(const tal_t *ctx); -u8 *get_supported_local_features(const tal_t *ctx); - /* Could be configurable. */ #define OUR_CHANNEL_FLAGS CHANNEL_FLAGS_ANNOUNCE_CHANNEL