peer_group: move to a new header, add recursive lookup macros

This commit is contained in:
Matthias Schiffer 2015-10-18 02:57:26 +02:00
parent d9a5267fce
commit df48485aea
7 changed files with 100 additions and 35 deletions

View file

@ -40,6 +40,7 @@
#include "lex.h" #include "lex.h"
#include "method.h" #include "method.h"
#include "peer.h" #include "peer.h"
#include "peer_group.h"
#include <config.yy.h> #include <config.yy.h>
#include <dirent.h> #include <dirent.h>

View file

@ -137,8 +137,9 @@
%code { %code {
#include <src/peer.h>
#include <src/config.h> #include <src/config.h>
#include <src/peer.h>
#include <src/peer_group.h>
#include <limits.h> #include <limits.h>

View file

@ -33,6 +33,7 @@
#include "handshake.h" #include "handshake.h"
#include "method.h" #include "method.h"
#include "peer.h" #include "peer.h"
#include "peer_group.h"
#include <fastd_version.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 */ /** 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_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) { 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); fastd_string_stack_t *method_list = parse_string_list(handshake->records[RECORD_METHOD_LIST].data, handshake->records[RECORD_METHOD_LIST].length);

View file

@ -30,6 +30,7 @@
*/ */
#include "peer.h" #include "peer.h"
#include "peer_group.h"
#include "peer_hashtable.h" #include "peer_hashtable.h"
#include "poll.h" #include "poll.h"

View file

@ -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 */ /** An entry for a MAC address seen at another peer */
struct fastd_peer_eth_addr { struct fastd_peer_eth_addr {
fastd_eth_addr_t addr; /**< The MAC address */ 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); 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 */ /** Returns the MTU to use for a peer */
static inline uint16_t fastd_peer_get_mtu(const fastd_peer_t *peer) { static inline uint16_t fastd_peer_get_mtu(const fastd_peer_t *peer) {
if (conf.mode == MODE_TAP) if (conf.mode == MODE_TAP)

92
src/peer_group.h Normal file
View file

@ -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; \
})

View file

@ -33,6 +33,7 @@
#include "../../crypto.h" #include "../../crypto.h"
#include "../../handshake.h" #include "../../handshake.h"
#include "../../hkdf_sha256.h" #include "../../hkdf_sha256.h"
#include "../../peer_group.h"
#include "../../verify.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)) if (!update_shared_handshake_key(peer, handshake_key, peer_handshake_key))
return; 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_SENDER_KEY, PUBLICKEYBYTES, &conf.protocol_config->key.public);
fastd_handshake_add(&buffer, RECORD_RECIPIENT_KEY, PUBLICKEYBYTES, &peer->key->key); fastd_handshake_add(&buffer, RECORD_RECIPIENT_KEY, PUBLICKEYBYTES, &peer->key->key);