summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2015-10-18 02:57:26 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2015-10-18 03:04:02 +0200
commitdf48485aeab897c50fd792a740d1a5aed4378fc9 (patch)
tree2de6c9e4847a54e0798a38a7976dc2a5ac89c7c3
parentd9a5267fceca6f46ee3005f7624fa92786a3c9b1 (diff)
downloadfastd-df48485aeab897c50fd792a740d1a5aed4378fc9.tar
fastd-df48485aeab897c50fd792a740d1a5aed4378fc9.zip
peer_group: move to a new header, add recursive lookup macros
-rw-r--r--src/config.c1
-rw-r--r--src/config.y3
-rw-r--r--src/handshake.c3
-rw-r--r--src/peer.c1
-rw-r--r--src/peer.h32
-rw-r--r--src/peer_group.h92
-rw-r--r--src/protocols/ec25519_fhmqvc/handshake.c3
7 files changed, 100 insertions, 35 deletions
diff --git a/src/config.c b/src/config.c
index 33621c7..034ad75 100644
--- a/src/config.c
+++ b/src/config.c
@@ -40,6 +40,7 @@
#include "lex.h"
#include "method.h"
#include "peer.h"
+#include "peer_group.h"
#include <config.yy.h>
#include <dirent.h>
diff --git a/src/config.y b/src/config.y
index 8ab7b7b..4f55d91 100644
--- a/src/config.y
+++ b/src/config.y
@@ -137,8 +137,9 @@
%code {
- #include <src/peer.h>
#include <src/config.h>
+ #include <src/peer.h>
+ #include <src/peer_group.h>
#include <limits.h>
diff --git a/src/handshake.c b/src/handshake.c
index d82a4b2..2c50c0e 100644
--- a/src/handshake.c
+++ b/src/handshake.c
@@ -33,6 +33,7 @@
#include "handshake.h"
#include "method.h"
#include "peer.h"
+#include "peer_group.h"
#include <fastd_version.h>
@@ -424,7 +425,7 @@ static inline const fastd_method_info_t * get_method_by_name(const fastd_string_
/** Returns the most appropriate method to negotiate with a peer a handshake was received from */
const fastd_method_info_t * fastd_handshake_get_method(const fastd_peer_t *peer, const fastd_handshake_t *handshake) {
- const fastd_string_stack_t *methods = fastd_peer_get_methods(peer);
+ const fastd_string_stack_t *methods = *fastd_peer_group_lookup_peer(peer, methods);
if (handshake->records[RECORD_METHOD_LIST].data && handshake->records[RECORD_METHOD_LIST].length) {
fastd_string_stack_t *method_list = parse_string_list(handshake->records[RECORD_METHOD_LIST].data, handshake->records[RECORD_METHOD_LIST].length);
diff --git a/src/peer.c b/src/peer.c
index 363bdd7..de8846e 100644
--- a/src/peer.c
+++ b/src/peer.c
@@ -30,6 +30,7 @@
*/
#include "peer.h"
+#include "peer_group.h"
#include "peer_hashtable.h"
#include "poll.h"
diff --git a/src/peer.h b/src/peer.h
index ea1b950..298adf4 100644
--- a/src/peer.h
+++ b/src/peer.h
@@ -110,24 +110,6 @@ struct fastd_peer {
};
-/**
- A group of peers
-
- Peer groups may be nested and form a tree
-*/
-struct fastd_peer_group {
- fastd_peer_group_t *next; /**< The next sibling in the group tree */
- fastd_peer_group_t *parent; /**< The group's parent group */
- fastd_peer_group_t *children; /**< The group's first child */
-
- char *name; /**< The group's name; NULL for the root group */
- fastd_string_stack_t *peer_dirs; /**< List of peer directories which belong to this group */
-
- /* constraints */
- int max_connections; /**< The maximum number of connections to allow in this group; -1 for no limit */
- fastd_string_stack_t *methods; /**< The list of configured method names */
-};
-
/** An entry for a MAC address seen at another peer */
struct fastd_peer_eth_addr {
fastd_eth_addr_t addr; /**< The MAC address */
@@ -270,20 +252,6 @@ static inline bool fastd_peer_is_socket_dynamic(const fastd_peer_t *peer) {
return (!peer->sock || !peer->sock->addr);
}
-/** Returns the configured methods for a peer's group */
-static inline const fastd_string_stack_t * fastd_peer_get_methods(const fastd_peer_t *peer) {
- if (!peer)
- return conf.peer_group->methods;
-
- const fastd_peer_group_t *group;
- for (group = peer->group; group; group = group->parent) {
- if (group->methods)
- return group->methods;
- }
-
- return NULL;
-}
-
/** Returns the MTU to use for a peer */
static inline uint16_t fastd_peer_get_mtu(const fastd_peer_t *peer) {
if (conf.mode == MODE_TAP)
diff --git a/src/peer_group.h b/src/peer_group.h
new file mode 100644
index 0000000..ce3a2dc
--- /dev/null
+++ b/src/peer_group.h
@@ -0,0 +1,92 @@
+/*
+ Copyright (c) 2012-2015, Matthias Schiffer <mschiffer@universe-factory.net>
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ \file
+
+ Peer groups
+*/
+
+
+#pragma once
+
+#include "fastd.h"
+
+
+/**
+ A group of peers
+
+ Peer groups may be nested and form a tree
+*/
+struct fastd_peer_group {
+ fastd_peer_group_t *next; /**< The next sibling in the group tree */
+ fastd_peer_group_t *parent; /**< The group's parent group */
+ fastd_peer_group_t *children; /**< The group's first child */
+
+ char *name; /**< The group's name; NULL for the root group */
+ fastd_string_stack_t *peer_dirs; /**< List of peer directories which belong to this group */
+
+ /* constraints */
+ int max_connections; /**< The maximum number of connections to allow in this group; -1 for no limit */
+ fastd_string_stack_t *methods; /**< The list of configured method names */
+};
+
+
+/**
+ Looks up an attribute in the peer group tree
+
+ Returns a pointer to the attribute, going up the group tree to the first group
+ where the attribute is not NULL if such a group exists.
+
+ @param group the peer group
+ @param attr the name of the member
+
+ \hideinitializer
+ */
+#define fastd_peer_group_lookup(group, attr) ({ \
+ const fastd_peer_group_t *_grp = (group); \
+ \
+ while (_grp->parent && !_grp->attr) \
+ _grp = _grp->parent; \
+ \
+ &_grp->attr; \
+ })
+
+/**
+ Looks up an attribute in the peer group tree, for a given peer
+
+ Returns a pointer to the attribute, going up the group tree to the first group
+ where the attribute is not NULL if such a group exists. Uses the default group
+ if no peer is given.
+
+ @param peer the peer
+ @param attr the name of the member
+
+ \hideinitializer
+ */
+#define fastd_peer_group_lookup_peer(peer, attr) ({ \
+ const fastd_peer_t *_peer = (peer); \
+ _peer ? fastd_peer_group_lookup(_peer->group, attr) : &conf.peer_group->attr; \
+ })
diff --git a/src/protocols/ec25519_fhmqvc/handshake.c b/src/protocols/ec25519_fhmqvc/handshake.c
index 0cbe15a..80dbd5c 100644
--- a/src/protocols/ec25519_fhmqvc/handshake.c
+++ b/src/protocols/ec25519_fhmqvc/handshake.c
@@ -33,6 +33,7 @@
#include "../../crypto.h"
#include "../../handshake.h"
#include "../../hkdf_sha256.h"
+#include "../../peer_group.h"
#include "../../verify.h"
@@ -314,7 +315,7 @@ static void respond_handshake(const fastd_socket_t *sock, const fastd_peer_addre
if (!update_shared_handshake_key(peer, handshake_key, peer_handshake_key))
return;
- fastd_handshake_buffer_t buffer = fastd_handshake_new_reply(2, little_endian, fastd_peer_get_mtu(peer), method, fastd_peer_get_methods(peer), 4*(4+PUBLICKEYBYTES) + 2*(4+HASHBYTES));
+ fastd_handshake_buffer_t buffer = fastd_handshake_new_reply(2, little_endian, fastd_peer_get_mtu(peer), method, *fastd_peer_group_lookup_peer(peer, methods), 4*(4+PUBLICKEYBYTES) + 2*(4+HASHBYTES));
fastd_handshake_add(&buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, &conf.protocol_config->key.public);
fastd_handshake_add(&buffer, RECORD_RECIPIENT_KEY, PUBLICKEYBYTES, &peer->key->key);