metadata: Add metadata factory and implementation for integer types
Co-authored-by: Thomas Egerer <[email protected]>
This commit is contained in:
co-authored by
Thomas Egerer
parent
85d626e9ae
commit
531335ad20
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Tobias Brunner, codelabs GmbH
|
||||
* Copyright (C) 2021 Thomas Egerer, secunet AG
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup metadata metadata
|
||||
* @ingroup libstrongswan
|
||||
*
|
||||
* @defgroup metadata_t metadata
|
||||
* @{ @ingroup metadata
|
||||
*/
|
||||
|
||||
#ifndef METADATA_H_
|
||||
#define METADATA_H_
|
||||
|
||||
#include <utils/utils.h>
|
||||
|
||||
typedef struct metadata_t metadata_t;
|
||||
|
||||
/**
|
||||
* Pre-defined metadata type for int values. Note that while the constructor and
|
||||
* equals() work with integer values/types smaller than int (such values get
|
||||
* promoted to int when passed via ...), get() does not and expects a pointer to
|
||||
* an int variable.
|
||||
*/
|
||||
#define METADATA_TYPE_INT "int"
|
||||
|
||||
/**
|
||||
* Pre-defined metadata type for uint64_t values. Make sure to pass only
|
||||
* uint64_t values/variables also to the constructor and equals() (or cast them
|
||||
* appropriately there).
|
||||
*/
|
||||
#define METADATA_TYPE_UINT64 "uint64"
|
||||
|
||||
/**
|
||||
* Metadata object to allow storing arbitrary values in an encapsulated
|
||||
* object.
|
||||
*/
|
||||
struct metadata_t {
|
||||
|
||||
/**
|
||||
* Return the type of the metadata object.
|
||||
*
|
||||
* @return string type of the metadata object
|
||||
*/
|
||||
const char *(*get_type)(metadata_t *this);
|
||||
|
||||
/**
|
||||
* Clone this metadata object.
|
||||
*
|
||||
* @return a cloned instance
|
||||
*/
|
||||
metadata_t *(*clone)(metadata_t *this);
|
||||
|
||||
/**
|
||||
* Compare this to another value (or values, depending on the type).
|
||||
*
|
||||
* @param ... value(s) (raw, not metadata_t) to compare this to
|
||||
* @return TRUE if value is equal to metadata value
|
||||
*/
|
||||
bool (*equals)(metadata_t *this, ...);
|
||||
|
||||
/**
|
||||
* Retrieve the values via variadic argument(s).
|
||||
*
|
||||
* @param ... pointer(s) to obtain metadata value(s)
|
||||
*/
|
||||
void (*get)(metadata_t *this, ...);
|
||||
|
||||
/**
|
||||
* Destroy this metadata object.
|
||||
*/
|
||||
void (*destroy)(metadata_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor type for metadata objects.
|
||||
*
|
||||
* @param type type of the metadata object, allows using the same
|
||||
* constructor for different types
|
||||
* @param args type-specific arguments
|
||||
* @return metadata object, NULL on failure
|
||||
*/
|
||||
typedef metadata_t *(*metadata_create_t)(const char *type, va_list args);
|
||||
|
||||
#endif /** METADATA_H_ @}*/
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Tobias Brunner, codelabs GmbH
|
||||
* Copyright (C) 2021 Thomas Egerer, secunet AG
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "metadata_factory.h"
|
||||
#include "metadata_int.h"
|
||||
|
||||
#include <library.h>
|
||||
#include <collections/hashtable.h>
|
||||
|
||||
typedef struct private_metadata_factory_t private_metadata_factory_t;
|
||||
|
||||
/**
|
||||
* Private data
|
||||
*/
|
||||
struct private_metadata_factory_t {
|
||||
|
||||
/**
|
||||
* Public interface
|
||||
*/
|
||||
metadata_factory_t public;
|
||||
|
||||
/**
|
||||
* Registered metadata types (entry_t)
|
||||
*/
|
||||
hashtable_t *types;
|
||||
};
|
||||
|
||||
/**
|
||||
* Registered constructor data
|
||||
*/
|
||||
typedef struct {
|
||||
/** Type name */
|
||||
char *type;
|
||||
/** Constructor */
|
||||
metadata_create_t create;
|
||||
} entry_t;
|
||||
|
||||
/**
|
||||
* Destroy an entry
|
||||
*/
|
||||
static void destroy_entry(entry_t *this)
|
||||
{
|
||||
if (this)
|
||||
{
|
||||
free(this->type);
|
||||
free(this);
|
||||
}
|
||||
}
|
||||
|
||||
METHOD(metadata_factory_t, create, metadata_t*,
|
||||
private_metadata_factory_t *this, const char *type, ...)
|
||||
{
|
||||
metadata_t *metadata = NULL;
|
||||
entry_t *entry;
|
||||
va_list args;
|
||||
|
||||
entry = this->types->get(this->types, type);
|
||||
if (entry)
|
||||
{
|
||||
va_start(args, type);
|
||||
metadata = entry->create(type, args);
|
||||
va_end(args);
|
||||
}
|
||||
return metadata;
|
||||
}
|
||||
|
||||
METHOD(metadata_factory_t, register_type, void,
|
||||
private_metadata_factory_t *this, const char *type,
|
||||
metadata_create_t create)
|
||||
{
|
||||
entry_t *entry;
|
||||
|
||||
INIT(entry,
|
||||
.type = strdup(type),
|
||||
.create = create,
|
||||
);
|
||||
|
||||
destroy_entry(this->types->put(this->types, entry->type, entry));
|
||||
}
|
||||
|
||||
METHOD(metadata_factory_t, destroy, void,
|
||||
private_metadata_factory_t *this)
|
||||
{
|
||||
this->types->destroy_function(this->types, (void*)destroy_entry);
|
||||
free(this);
|
||||
}
|
||||
|
||||
metadata_factory_t *metadata_factory_create()
|
||||
{
|
||||
private_metadata_factory_t *this;
|
||||
|
||||
INIT(this,
|
||||
.public = {
|
||||
.create = _create,
|
||||
.register_type = _register_type,
|
||||
.destroy = _destroy,
|
||||
},
|
||||
.types = hashtable_create(hashtable_hash_str, hashtable_equals_str, 0),
|
||||
);
|
||||
|
||||
/* register pre-defined types */
|
||||
register_type(this, METADATA_TYPE_INT, metadata_create_int);
|
||||
register_type(this, METADATA_TYPE_UINT64, metadata_create_int);
|
||||
|
||||
return &this->public;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Tobias Brunner, codelabs GmbH
|
||||
* Copyright (C) 2021 Thomas Egerer, secunet AG
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup metadata_factory metadata_factory
|
||||
* @{ @ingroup metadata
|
||||
*/
|
||||
|
||||
#ifndef METADATA_FACTORY_H_
|
||||
#define METADATA_FACTORY_H_
|
||||
|
||||
#include "metadata.h"
|
||||
|
||||
typedef struct metadata_factory_t metadata_factory_t;
|
||||
|
||||
/**
|
||||
* Create a factory for dealing with metadata objects.
|
||||
*/
|
||||
struct metadata_factory_t {
|
||||
|
||||
/**
|
||||
* Create a metadata object of the given type.
|
||||
*
|
||||
* @param type type of the desired metadata object
|
||||
* @param ... data to wrap into the metadata object
|
||||
* @return metadata handler on success, NULL otherwise
|
||||
*/
|
||||
metadata_t *(*create)(metadata_factory_t *this, const char *type, ...);
|
||||
|
||||
/**
|
||||
* Register a constructor for a given type.
|
||||
*
|
||||
* @param type type to register
|
||||
* @param create metadata constructor
|
||||
*/
|
||||
void (*register_type)(metadata_factory_t *this, const char *type,
|
||||
metadata_create_t create);
|
||||
|
||||
/**
|
||||
* Destroy a metadata_factory_t instance.
|
||||
*/
|
||||
void (*destroy)(metadata_factory_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a metadata_factory_t instance.
|
||||
*/
|
||||
metadata_factory_t *metadata_factory_create();
|
||||
|
||||
#endif /** METADATA_FACTORY_H_ @}*/
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Tobias Brunner, codelabs GmbH
|
||||
* Copyright (C) 2021 Thomas Egerer, secunet AG
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "metadata_int.h"
|
||||
|
||||
#include <library.h>
|
||||
|
||||
typedef struct private_metadata_t private_metadata_t;
|
||||
|
||||
/**
|
||||
* Private data
|
||||
*/
|
||||
struct private_metadata_t {
|
||||
|
||||
/**
|
||||
* Public interface
|
||||
*/
|
||||
metadata_t public;
|
||||
|
||||
/**
|
||||
* String representation of type.
|
||||
*/
|
||||
const char *type;
|
||||
|
||||
/**
|
||||
* Stored value.
|
||||
*/
|
||||
uint64_t value;
|
||||
};
|
||||
|
||||
/* forward declaration */
|
||||
static metadata_t *create_generic(const char *type, uint64_t value);
|
||||
|
||||
METHOD(metadata_t, get_type, const char*,
|
||||
private_metadata_t *this)
|
||||
{
|
||||
return this->type;
|
||||
}
|
||||
|
||||
METHOD(metadata_t, clone_, metadata_t*,
|
||||
private_metadata_t *this)
|
||||
{
|
||||
return create_generic(this->type, this->value);
|
||||
}
|
||||
|
||||
METHOD(metadata_t, equals, bool,
|
||||
private_metadata_t *this, ...)
|
||||
{
|
||||
/* bool, uint8, uint16 etc. are promoted to int when passed via ... */
|
||||
if (streq(METADATA_TYPE_INT, this->type))
|
||||
{
|
||||
int value;
|
||||
|
||||
VA_ARGS_GET(this, value);
|
||||
return value == (int)this->value;
|
||||
}
|
||||
else if (streq(METADATA_TYPE_UINT64, this->type))
|
||||
{
|
||||
uint64_t value;
|
||||
|
||||
VA_ARGS_GET(this, value);
|
||||
return value == this->value;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
METHOD(metadata_t, get, void,
|
||||
private_metadata_t *this, ...)
|
||||
{
|
||||
/* pointers here have to match exactly, so passing e.g. a bool* or uint16_t
|
||||
* are illegal */
|
||||
if (streq(METADATA_TYPE_INT, this->type))
|
||||
{
|
||||
int *value;
|
||||
|
||||
VA_ARGS_GET(this, value);
|
||||
*value = this->value;
|
||||
}
|
||||
else if (streq(METADATA_TYPE_UINT64, this->type))
|
||||
{
|
||||
uint64_t *value;
|
||||
|
||||
VA_ARGS_GET(this, value);
|
||||
*value = this->value;
|
||||
}
|
||||
}
|
||||
|
||||
METHOD(metadata_t, destroy, void,
|
||||
private_metadata_t *this)
|
||||
{
|
||||
free(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic constructor
|
||||
*/
|
||||
static metadata_t *create_generic(const char *type, uint64_t value)
|
||||
{
|
||||
private_metadata_t *this;
|
||||
|
||||
INIT(this,
|
||||
.public = {
|
||||
.get_type = _get_type,
|
||||
.clone = _clone_,
|
||||
.equals = _equals,
|
||||
.get = _get,
|
||||
.destroy = _destroy,
|
||||
},
|
||||
.type = type,
|
||||
.value = value,
|
||||
);
|
||||
return &this->public;
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header
|
||||
*/
|
||||
metadata_t *metadata_create_int(const char *type, va_list args)
|
||||
{
|
||||
metadata_t *this = NULL;
|
||||
|
||||
/* bool, uint8, uint16 etc. are promoted to int when passed via ... */
|
||||
if (streq(METADATA_TYPE_INT, type))
|
||||
{
|
||||
int int_val;
|
||||
|
||||
VA_ARGS_VGET(args, int_val);
|
||||
this = create_generic(METADATA_TYPE_INT, int_val);
|
||||
}
|
||||
else if (streq(METADATA_TYPE_UINT64, type))
|
||||
{
|
||||
uint64_t uint64_val;
|
||||
|
||||
VA_ARGS_VGET(args, uint64_val);
|
||||
this = create_generic(METADATA_TYPE_UINT64, uint64_val);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Tobias Brunner, codelabs GmbH
|
||||
* Copyright (C) 2021 Thomas Egerer, secunet AG
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup metadata_int metadata_int
|
||||
* @{ @ingroup metadata
|
||||
*/
|
||||
|
||||
#ifndef METADATA_INT_H_
|
||||
#define METADATA_INT_H_
|
||||
|
||||
#include "metadata.h"
|
||||
|
||||
/**
|
||||
* Create a metadata object of an integer type.
|
||||
*
|
||||
* @param type type name
|
||||
* @param args integer of the specified type
|
||||
* @return successfully created object, NULL on failure
|
||||
*/
|
||||
metadata_t *metadata_create_int(const char *type, va_list args);
|
||||
|
||||
#endif /** METADATA_INT_H_ @}*/
|
||||
Reference in New Issue
Block a user