From 92bce3a4ba3509864606efb34d6bfd8d614c8df6 Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Sun, 15 Apr 2012 03:47:19 -0400 Subject: [PATCH] code cleanup, 100% compliant with checkfiles.pl now :D --- admin/CMakeLists.txt | 2 +- admin/http/HttpServer.c | 4 +- cjdroute.c | 2 +- crypto/CryptoAuth.h | 2 +- dht/CMakeLists.txt | 3 +- dht/DHTMessage.h | 58 ++++++ dht/DHTModule.h | 57 ++++++ dht/{DHTModules.c => DHTModuleRegistry.c} | 108 ++--------- dht/DHTModuleRegistry.h | 80 ++++++++ dht/DHTModules.h | 211 ---------------------- dht/Ducttape.c | 13 +- dht/Ducttape.h | 2 +- dht/ReplyModule.c | 11 +- dht/ReplyModule.h | 2 +- dht/SerializationModule.c | 26 +-- dht/SerializationModule.h | 8 +- dht/dhtcore/Janitor.c | 2 +- dht/dhtcore/RouterModule.c | 12 +- dht/dhtcore/RouterModule.h | 2 +- dht/test/CMakeLists.txt | 2 + dht/test/DHTModules_handleIncoming_test.c | 14 +- dht/test/DHTModules_handleOutgoing_test.c | 13 +- dht/test/DHTModules_serialization_test.c | 114 ------------ interface/test/InterfaceController_test.c | 2 +- scripts/checkfiles.pl | 10 +- test/CMakeLists.txt | 1 + test/TestFramework.c | 2 +- 27 files changed, 292 insertions(+), 471 deletions(-) create mode 100644 dht/DHTMessage.h create mode 100644 dht/DHTModule.h rename dht/{DHTModules.c => DHTModuleRegistry.c} (51%) create mode 100644 dht/DHTModuleRegistry.h delete mode 100644 dht/DHTModules.h delete mode 100644 dht/test/DHTModules_serialization_test.c diff --git a/admin/CMakeLists.txt b/admin/CMakeLists.txt index 4d63404cb..8af33bc30 100644 --- a/admin/CMakeLists.txt +++ b/admin/CMakeLists.txt @@ -17,4 +17,4 @@ add_library(cjdadmin Configurator.c ) -target_link_libraries(cjdadmin crypto) +target_link_libraries(cjdadmin crypto cjdbenc_StandardBencSerializer) diff --git a/admin/http/HttpServer.c b/admin/http/HttpServer.c index 68de61298..17cdee66a 100644 --- a/admin/http/HttpServer.c +++ b/admin/http/HttpServer.c @@ -239,7 +239,7 @@ static void handleApiEvent(evutil_socket_t socket, short eventType, void* vconte ssize_t length = read(socket, context->messageBuffer, MAX_API_REPLY_SIZE); if (0 >= length) { - if(0 == length) { + if (0 == length) { fprintf(stderr, "API server shutting down\n"); } else { perror("error reading from API server"); @@ -294,7 +294,7 @@ static void setupApi(struct Context* context) evutil_socket_t sockfd = socket(addr.ss_family, SOCK_STREAM, 0); - while(connect(sockfd, (struct sockaddr*) &addr, addrLen) < 0) { + while (connect(sockfd, (struct sockaddr*) &addr, addrLen) < 0) { perror("error connecting to API server"); fprintf(stderr, "retrying in " STR(API_RETRY_DELAY) " seconds\n"); sleep(API_RETRY_DELAY); diff --git a/cjdroute.c b/cjdroute.c index 8fbed277c..94a0508a0 100644 --- a/cjdroute.c +++ b/cjdroute.c @@ -576,7 +576,7 @@ int main(int argc, char** argv) context.eHandler = AbortHandler_INSTANCE; context.switchCore = SwitchCore_new(context.logger, context.allocator); - context.registry = DHTModules_new(context.allocator); + context.registry = DHTModuleRegistry_new(context.allocator); ReplyModule_register(context.registry, context.allocator); // Router diff --git a/crypto/CryptoAuth.h b/crypto/CryptoAuth.h index 6040dc6ce..4f07fd9e2 100644 --- a/crypto/CryptoAuth.h +++ b/crypto/CryptoAuth.h @@ -38,7 +38,7 @@ struct CryptoAuth; * @param user The thing to associate with this user, will be returned by CryptoAuth_getUser(). * If this is NULL and requireAuthentication is enabled, authentication will fail. * @param context The CryptoAuth context. - * @return 0 if all goes well, + * @return 0 if all goes well, * CryptoAuth_addUser_INVALID_AUTHTYPE if the authentication method is not supported, * CryptoAuth_addUser_OUT_OF_SPACE if there is not enough space to store the entry, * CryptoAuth_addUser_DUPLICATE if the entry already exists. diff --git a/dht/CMakeLists.txt b/dht/CMakeLists.txt index 342ebc519..6396d0097 100644 --- a/dht/CMakeLists.txt +++ b/dht/CMakeLists.txt @@ -19,10 +19,9 @@ include_directories(${NACL_INCLUDE_DIRS}) add_library(dht CJDHTConstants.c - DHTModules.c + DHTModuleRegistry.c SerializationModule.c ReplyModule.c - #RateLimitModule.c Ducttape.c ) diff --git a/dht/DHTMessage.h b/dht/DHTMessage.h new file mode 100644 index 000000000..5d86b24a1 --- /dev/null +++ b/dht/DHTMessage.h @@ -0,0 +1,58 @@ +/* + * You may redistribute this program and/or modify it under the terms of + * the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef DHTMessage_H +#define DHTMessage_H + +#include "benc/Dict.h" + +/** + * Maximum number of bytes in a message. + * Ethernet MTU is 1500 so it's hard to imagine much more. + */ +#define DHTMessage_MAX_SIZE 1536 + + +/** + * This struct represents a DHT message which will be passed to the + * modules. The only part of the message which will be available to + * all modules is Message.peer. Incoming modules will have Message.bytes + * and Message.length when they come from the network module. + */ +struct DHTMessage; +struct DHTMessage +{ + struct Address* address; + + char padding[512]; + + /** The message in binary format. */ + char bytes[DHTMessage_MAX_SIZE]; + + /** The length of the binary message. */ + unsigned short length; + + /** The message as a bencoded dictionary. */ + Dict* asDict; + + /** + * If this message is an outgoing reply, replyTo is the original query. + * For incoming replies or any queries, it is NULL. + */ + struct DHTMessage* replyTo; + + /** A memory allocator which will be freed after this message is sent. */ + const struct Allocator* allocator; +}; + +#endif diff --git a/dht/DHTModule.h b/dht/DHTModule.h new file mode 100644 index 000000000..79135c199 --- /dev/null +++ b/dht/DHTModule.h @@ -0,0 +1,57 @@ +/* + * You may redistribute this program and/or modify it under the terms of + * the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef DHTModule_H +#define DHTModule_H + +#include "dht/DHTMessage.h" + +/** + * This represents a DHT module. + * Pass one of these to DHTModule_register() and it + * will handle dht requests and responses. + */ +struct DHTModule; +struct DHTModule { + /** + * A user friendly null terminated string which will be used to + * manipulate the module using the DHTModules API. + */ + const char* const name; + + /** + * The module's state. + */ + void* const context; + + /** + * @param the message which came in from a peer. + * @param context the module's state. + * @return 1 if a response should be sent for this message. + * -1 if the message is known invalid and should not be passed + * to any more handlers. + */ + int (* const handleIncoming)(struct DHTMessage* message, + void* context); + + /** + * @param message the message which will be sent to the peer. + * @param context the module's state. + * @return -1 if the message should not be propigated to any more modules. + * use with caution as it may be interpreted as network loss. + */ + int (* const handleOutgoing)(struct DHTMessage* message, + void* context); +}; + +#endif diff --git a/dht/DHTModules.c b/dht/DHTModuleRegistry.c similarity index 51% rename from dht/DHTModules.c rename to dht/DHTModuleRegistry.c index df5a07356..1e3a8db22 100644 --- a/dht/DHTModules.c +++ b/dht/DHTModuleRegistry.c @@ -11,9 +11,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include -#include +#include "dht/DHTModule.h" +#include "dht/DHTModuleRegistry.h" #include "memory/Allocator.h" #include "memory/BufferAllocator.h" #include "io/Reader.h" @@ -21,7 +21,9 @@ #include "benc/Object.h" #include "benc/serialization/BencSerializer.h" #include "benc/serialization/standard/StandardBencSerializer.h" -#include "DHTModules.h" + +#include +#include #define DEBUG2(x, y) /* #define DEBUG2(x, y) fprintf(stderr, x, y); fflush(stderr) */ @@ -29,17 +31,8 @@ /** This defines what format the registry will be serialized in. */ #define SERIALIZER StandardBencSerializer_get() -/*--------------------Prototypes--------------------*/ -static void forEachModule(int (*doThis)(struct DHTModule* module, struct DHTMessage* message), - struct DHTMessage* message, - const struct DHTModuleRegistry* registry); -static inline void deserializeContext(struct DHTModule* module, - struct DHTModuleRegistry* registry); - -/*--------------------Interface--------------------*/ -/** @see DHTModules.h */ -struct DHTModuleRegistry* DHTModules_new(struct Allocator* allocator) +struct DHTModuleRegistry* DHTModuleRegistry_new(struct Allocator* allocator) { struct DHTModuleRegistry* reg = allocator->calloc(sizeof(struct DHTModuleRegistry), 1, allocator); @@ -48,12 +41,9 @@ struct DHTModuleRegistry* DHTModules_new(struct Allocator* allocator) return reg; } -/** @see DHTModules.h */ -int DHTModules_register(struct DHTModule* module, - struct DHTModuleRegistry* registry) +int DHTModuleRegistry_register(struct DHTModule* module, + struct DHTModuleRegistry* registry) { - deserializeContext(module, registry); - registry->members = registry->allocator->realloc(registry->members, sizeof(char*) * (registry->memberCount + 2), registry->allocator); @@ -65,9 +55,8 @@ int DHTModules_register(struct DHTModule* module, return 0; } -/** @see DHTModules.h */ -void DHTModules_handleIncoming(struct DHTMessage* message, - const struct DHTModuleRegistry* registry) +void DHTModuleRegistry_handleIncoming(struct DHTMessage* message, + const struct DHTModuleRegistry* registry) { if (!(message && registry && registry->members && registry->memberCount)) { return; @@ -104,77 +93,6 @@ static int dhtModuleHandleOutgoing(struct DHTModule* module, struct DHTMessage* return 0; } -/** @see DHTModules.h */ -void DHTModules_handleOutgoing(struct DHTMessage* message, - const struct DHTModuleRegistry* registry) -{ - forEachModule(dhtModuleHandleOutgoing, message, registry); -} - -/** @see DHTModules.h */ -void DHTModules_serialize(const struct DHTModuleRegistry* registry, - const struct Writer* writer) -{ - char buffer[1024]; - struct Allocator* allocator = BufferAllocator_new(buffer, 1024); - Dict* dictionary = Dict_new(allocator); - - struct DHTModule** modulePtr = registry->members; - struct DHTModule* module = *modulePtr; - while (module) { - if (module->serialize != NULL) { - Dict_putDict(dictionary, - String_new(module->name, allocator), - module->serialize(module->context), - allocator); - } - modulePtr++; - module = *modulePtr; - } - SERIALIZER->serializeDictionary(writer, dictionary); -} - -/** @see DHTModules.h */ -struct DHTModuleRegistry* DHTModules_deserialize(const struct Reader* reader, - struct Allocator* allocator) -{ - Dict* dictionary = Dict_new(allocator); - if (SERIALIZER->parseDictionary(reader, allocator, dictionary) != 0) { - return NULL; - } - - struct DHTModuleRegistry* reg = DHTModules_new(allocator); - reg->serializedContexts = dictionary; - return reg; - - return NULL; -} - -/*----------------------Internals----------------------*/ - -/** - * Deserialize the context for this module. - * First the registry is deserialized then the modules are registered. - * When the modules are registered, if they have serialized contexts, - * those are deserialized by this function which calls their own - * deserialization functions. - * - * @param module the module to deserialize the context for. - * @param registry the DHT module registry. - */ -static inline void deserializeContext(struct DHTModule* module, - struct DHTModuleRegistry* registry) -{ - char* name = (char*) module->name; - if (module && registry && registry->serializedContexts) { - Dict* serContext = Dict_getDict(registry->serializedContexts, - &(String) { .len = strlen(name), .bytes = name } ); - if (module->deserialize && module->context && serContext) { - module->deserialize(serContext, module->context); - } - } -} - /** * Do something to every module which is registered. * @param doThis the callback. @@ -194,3 +112,9 @@ static void forEachModule(int (*doThis)(struct DHTModule* module, struct DHTMess module = *modulePtr; } } + +void DHTModuleRegistry_handleOutgoing(struct DHTMessage* message, + const struct DHTModuleRegistry* registry) +{ + forEachModule(dhtModuleHandleOutgoing, message, registry); +} diff --git a/dht/DHTModuleRegistry.h b/dht/DHTModuleRegistry.h new file mode 100644 index 000000000..468129012 --- /dev/null +++ b/dht/DHTModuleRegistry.h @@ -0,0 +1,80 @@ +/* + * You may redistribute this program and/or modify it under the terms of + * the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef DHTModuleRegistry_H +#define DHTModuleRegistry_H + +#include "benc/Dict.h" +#include "dht/DHTMessage.h" +#include "memory/Allocator.h" +#include "io/Reader.h" +#include "io/Writer.h" +#include "benc/Object.h" + + +/** State of the registry. */ +struct DHTModuleRegistry { + + /** Number of members. */ + int memberCount; + + /** A null terminated list of pointers to members. */ + struct DHTModule** members; + + /** + * A list of serialized contexts by module name to be + * deserialized when the modules are loaded. + */ + Dict* serializedContexts; + + /** Means of getting memory for the registry. */ + struct Allocator* allocator; +}; + +/** + * Create a new registry. + * + * @param allocator the means of getting memory to store the registry. + * @return a new (empty) registry. + */ +struct DHTModuleRegistry* DHTModuleRegistry_new(struct Allocator* allocator); + +/** + * Register an event handler. + * + * @param module the module to register. + * @return 0 if everything went well. + */ +int DHTModuleRegistry_register(struct DHTModule* module, + struct DHTModuleRegistry* registry); + +/** + * handleIncoming starts by running the last module registered + * and working back. It is assumed that the core modules will + * be registered first and will be the ones to initiate the + * response. + * The last module registered must be the one with access to + * the network. + * + * @see DHTModule->handleIncoming() + */ +void DHTModuleRegistry_handleIncoming(struct DHTMessage* message, + const struct DHTModuleRegistry* registry); + +/** + * @see DHTModule->handleOutgoing() + */ +void DHTModuleRegistry_handleOutgoing(struct DHTMessage* message, + const struct DHTModuleRegistry* registry); + +#endif diff --git a/dht/DHTModules.h b/dht/DHTModules.h deleted file mode 100644 index 87cfaaa6e..000000000 --- a/dht/DHTModules.h +++ /dev/null @@ -1,211 +0,0 @@ -/* - * You may redistribute this program and/or modify it under the terms of - * the GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#ifndef DHTModules_H -#define DHTModules_H - -#include "memory/Allocator.h" -#include "io/Reader.h" -#include "io/Writer.h" -#include "benc/Object.h" - -/** - * Maximum number of bytes in a message. - * Ethernet MTU is 1500 so it's hard to imagine much more. - */ -#define DHTModules_MAX_MESSAGE_SIZE 1536 - -/** - * This struct represents a DHT message which will be passed to the - * modules. The only part of the message which will be available to - * all modules is Message.peer. Incoming modules will have Message.bytes - * and Message.length when they come from the network module. - */ -struct DHTMessage; -struct DHTMessage -{ - struct Address* address; - - char padding[512]; - - /** The message in binary format. */ - char bytes[DHTModules_MAX_MESSAGE_SIZE]; - - /** The length of the binary message. */ - unsigned short length; - - /** The message as a bencoded dictionary. */ - Dict* asDict; - - /** - * If this message is an outgoing reply, replyTo is the original query. - * For incoming replies or any queries, it is NULL. - */ - struct DHTMessage* replyTo; - - /** A memory allocator which will be freed after this message is sent. */ - const struct Allocator* allocator; -}; - -/** - * This represents a DHT module. - * Pass one of these to DHTModule_register() and it - * will handle dht requests and responses. - */ -struct DHTModule; -struct DHTModule { - /** - * A user friendly null terminated string which will be used to - * manipulate the module using the DHTModules API. - */ - const char* const name; - - /** - * The module's state. - */ - void* const context; - - /** - * Serialize the content of the context into a bencoded object. - * - * @param context the module's state. - * @return a serialized form of context. - */ - Dict* (* const serialize)(void* context); - - /** - * Deserialize the context from a bencoded object. - * - * @param serialData the same data which was output by - * moduleContext_serialize. - * - * @param context the existing context to add content to. - */ - void (* const deserialize)(const Dict* serialData, - void* context); - - /** - * When a node must be evicted from the table, this will allow the - * module to decide which one it should be. Thus other node with more - * capabilities will be favored. - * - * @param one node's ID. - * @param another node's ID. - * @param context the module's state. - * @return less than 0 if the first node is favorable, greater than 0 - * if the second node is more favorable, - * 0 if they are considered equal. - */ - int (* const compareNodes)(const char nodeId[20], - const char otherNodeId[20], - void* context); - - /** - * @param the message which came in from a peer. - * @param context the module's state. - * @return 1 if a response should be sent for this message. - * -1 if the message is known invalid and should not be passed - * to any more handlers. - */ - int (* const handleIncoming)(struct DHTMessage* message, - void* context); - - /** - * @param message the message which will be sent to the peer. - * @param context the module's state. - * @return -1 if the message should not be propigated to any more modules. - * use with caution as it may be interpreted as network loss. - */ - int (* const handleOutgoing)(struct DHTMessage* message, - void* context); -}; - -/** State of the registry. */ -struct DHTModuleRegistry { - - /** Number of members. */ - int memberCount; - - /** A null terminated list of pointers to members. */ - struct DHTModule** members; - - /** - * A list of serialized contexts by module name to be - * deserialized when the modules are loaded. - */ - Dict* serializedContexts; - - /** Means of getting memory for the registry. */ - struct Allocator* allocator; -}; - -/** - * Create a new registry. - * - * @param allocator the means of getting memory to store the registry. - * @return a new (empty) registry. - */ -struct DHTModuleRegistry* DHTModules_new(struct Allocator* allocator); - -/** - * Register an event handler. - * - * @param module the module to register. - * @return 0 if everything went well. - */ -int DHTModules_register(struct DHTModule* module, - struct DHTModuleRegistry* registry); - -/** - * handleIncoming starts by running the last module registered - * and working back. It is assumed that the core modules will - * be registered first and will be the ones to initiate the - * response. - * The last module registered must be the one with access to - * the network. - * - * @see DHTModule->handleIncoming() - */ -void DHTModules_handleIncoming(struct DHTMessage* message, - const struct DHTModuleRegistry* registry); - -/** - * @see DHTModule->handleOutgoing() - */ -void DHTModules_handleOutgoing(struct DHTMessage* message, - const struct DHTModuleRegistry* registry); - -/** - * Serialize the state of each member in the registry. - * - * @param registry the module registry to serialize. - * @param writer a writer to which the output will be written. - * @see DHTModule->serialize - */ -void DHTModules_serialize(const struct DHTModuleRegistry* registry, - const struct Writer* writer); - -/** - * Deserialize a new registry from binary. - * NOTE: After deserialization, the modules still need to be registered. - * - * @param reader a reader which reads a stream of data prepared by a former call to DHTModules_serialize() - * @param allocator a means of acquiring memory. - * @return a new registry, modules will still need to be registered although they will regain their - * former state afterwords. - * @see DHTModule->deserialize - */ -struct DHTModuleRegistry* DHTModules_deserialize(const struct Reader* reader, - struct Allocator* allocator); - -#endif diff --git a/dht/Ducttape.c b/dht/Ducttape.c index 2ad51cfa7..c34eee2b2 100644 --- a/dht/Ducttape.c +++ b/dht/Ducttape.c @@ -11,11 +11,14 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + #include "crypto/CryptoAuth.h" #include "util/Log.h" #include "dht/Address.h" #include "dht/AddressMapper.h" -#include "dht/DHTModules.h" +#include "dht/DHTMessage.h" +#include "dht/DHTModule.h" +#include "dht/DHTModuleRegistry.h" #include "dht/dhtcore/Node.h" #include "dht/dhtcore/RouterModule.h" #include "dht/Ducttape.h" @@ -128,9 +131,9 @@ static inline uint8_t incomingDHT(struct Message* message, memset(&dht, 0, sizeof(struct DHTMessage)); // TODO: These copies are not necessary at all. - const uint32_t length = (message->length < DHTModules_MAX_MESSAGE_SIZE) + const uint32_t length = (message->length < DHTMessage_MAX_SIZE) ? message->length - : DHTModules_MAX_MESSAGE_SIZE; + : DHTMessage_MAX_SIZE; memcpy(dht.bytes, message->bytes, length); dht.address = addr; @@ -142,7 +145,7 @@ static inline uint8_t incomingDHT(struct Message* message, if (!setjmp(towel)) { BufferAllocator_onOOM(dht.allocator, outOfMemory, &towel); - DHTModules_handleIncoming(&dht, context->registry); + DHTModuleRegistry_handleIncoming(&dht, context->registry); } // TODO: return something meaningful. return Error_NONE; @@ -680,7 +683,7 @@ struct Ducttape* Ducttape_register(Dict* config, .allocator = allocator }), sizeof(struct Interface)); - if (DHTModules_register(&context->module, context->registry) + if (DHTModuleRegistry_register(&context->module, context->registry) || SwitchCore_setRouterInterface(&context->switchInterface, switchCore)) { return NULL; diff --git a/dht/Ducttape.h b/dht/Ducttape.h index 441b601ed..d1aeaf119 100644 --- a/dht/Ducttape.h +++ b/dht/Ducttape.h @@ -14,7 +14,7 @@ #ifndef Ducttape_H #define Ducttape_H -#include "dht/DHTModules.h" +#include "dht/DHTModuleRegistry.h" #include "dht/dhtcore/RouterModule.h" #include "switch/SwitchCore.h" #include "memory/Allocator.h" diff --git a/dht/ReplyModule.c b/dht/ReplyModule.c index 12c1eddbd..17de901e9 100644 --- a/dht/ReplyModule.c +++ b/dht/ReplyModule.c @@ -14,7 +14,9 @@ #include #include "dht/CJDHTConstants.h" -#include "dht/DHTModules.h" +#include "dht/DHTMessage.h" +#include "dht/DHTModule.h" +#include "dht/DHTModuleRegistry.h" #include "benc/Object.h" /** @@ -39,7 +41,10 @@ static int handleOutgoing(struct DHTMessage* message, void* vcontext); */ void ReplyModule_register(struct DHTModuleRegistry* registry, const struct Allocator* allocator) { - DHTModules_register(allocator->clone(sizeof(struct DHTModule), allocator, &(struct DHTModule) { + DHTModuleRegistry_register(allocator->clone(sizeof(struct DHTModule), + allocator, + &(struct DHTModule) + { .name = "ReplyModule", // We use the registry itself as the context .context = registry, @@ -63,7 +68,7 @@ static int handleIncoming(struct DHTMessage* message, void* vcontext) .allocator = message->allocator }); - DHTModules_handleOutgoing(reply, registry); + DHTModuleRegistry_handleOutgoing(reply, registry); return 0; } diff --git a/dht/ReplyModule.h b/dht/ReplyModule.h index 8fb2bda1e..13007a41b 100644 --- a/dht/ReplyModule.h +++ b/dht/ReplyModule.h @@ -14,7 +14,7 @@ #ifndef ReplyModule_H #define ReplyModule_H -#include "dht/DHTModules.h" +#include "dht/DHTModuleRegistry.h" /** * The reply module replies to all incoming queries. diff --git a/dht/SerializationModule.c b/dht/SerializationModule.c index 1e2369d8d..954ef6957 100644 --- a/dht/SerializationModule.c +++ b/dht/SerializationModule.c @@ -11,15 +11,17 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include "DHTModules.h" - -#include -#include -#include -#include -#include -#include -#include + +#include "benc/Object.h" +#include "dht/DHTMessage.h" +#include "dht/DHTModule.h" +#include "dht/DHTModuleRegistry.h" +#include "memory/Allocator.h" +#include "memory/BufferAllocator.h" +#include "io/Reader.h" +#include "io/ArrayReader.h" +#include "io/Writer.h" +#include "io/ArrayWriter.h" #include "benc/serialization/BencSerializer.h" #include "benc/serialization/standard/StandardBencSerializer.h" @@ -53,7 +55,7 @@ void SerializationModule_register(struct DHTModuleRegistry* registry, } }), sizeof(struct SerializationModule_context)); - DHTModules_register(&(context->module), registry); + DHTModuleRegistry_register(&(context->module), registry); } /*--------------------Internals--------------------*/ @@ -67,7 +69,7 @@ static int handleOutgoing(struct DHTMessage* message, void* vcontext) { struct Writer* writer = - ArrayWriter_new(message->bytes, DHTModules_MAX_MESSAGE_SIZE, message->allocator); + ArrayWriter_new(message->bytes, DHTMessage_MAX_SIZE, message->allocator); SERIALIZER->serializeDictionary(writer, message->asDict); message->length = writer->bytesWritten(writer); @@ -86,7 +88,7 @@ static int handleIncoming(struct DHTMessage* message, message->asDict = message->allocator->malloc(sizeof(Dict), message->allocator); struct Reader* reader = - ArrayReader_new(message->bytes, DHTModules_MAX_MESSAGE_SIZE, message->allocator); + ArrayReader_new(message->bytes, DHTMessage_MAX_SIZE, message->allocator); if (SERIALIZER->parseDictionary(reader, message->allocator, message->asDict) != 0) { return -2; diff --git a/dht/SerializationModule.h b/dht/SerializationModule.h index dd88e8cb8..9b61f69ba 100644 --- a/dht/SerializationModule.h +++ b/dht/SerializationModule.h @@ -14,7 +14,7 @@ #ifndef SerializationModule_H #define SerializationModule_H -#include "DHTModules.h" +#include "DHTModuleRegistry.h" /** * Register a new SerializationModule. @@ -22,9 +22,7 @@ * @param registry the module registry to register with. * @param allocator the means of aquiring memory for the serialization module. */ -#ifdef __GNUC__ -__attribute__((nonnull)) -#endif -void SerializationModule_register(struct DHTModuleRegistry* registry, const struct Allocator* allocator); +void SerializationModule_register(struct DHTModuleRegistry* registry, + const struct Allocator* allocator); #endif diff --git a/dht/dhtcore/Janitor.c b/dht/dhtcore/Janitor.c index 8875ca168..96fc66431 100644 --- a/dht/dhtcore/Janitor.c +++ b/dht/dhtcore/Janitor.c @@ -11,9 +11,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + #include "crypto/Crypto.h" #include "dht/Address.h" -#include "dht/DHTModules.h" #include "dht/dhtcore/Janitor.h" #include "dht/dhtcore/Node.h" #include "dht/dhtcore/NodeList.h" diff --git a/dht/dhtcore/RouterModule.c b/dht/dhtcore/RouterModule.c index 98c7f107f..26725512c 100644 --- a/dht/dhtcore/RouterModule.c +++ b/dht/dhtcore/RouterModule.c @@ -11,6 +11,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + #include "dht/Address.h" #include "dht/dhtcore/Janitor.h" #include "dht/dhtcore/RouterModule.h" @@ -20,7 +21,9 @@ #include "dht/dhtcore/NodeStore.h" #include "dht/dhtcore/SearchStore.h" #include "dht/CJDHTConstants.h" -#include "dht/DHTModules.h" +#include "dht/DHTMessage.h" +#include "dht/DHTModule.h" +#include "dht/DHTModuleRegistry.h" #include "benc/Object.h" #include "util/Log.h" #include "memory/Allocator.h" @@ -216,7 +219,10 @@ struct RouterModule* RouterModule_register(struct DHTModuleRegistry* registry, { struct RouterModule* const out = allocator->calloc(sizeof(struct RouterModule), 1, allocator); - DHTModules_register(allocator->clone(sizeof(struct DHTModule), allocator, &(struct DHTModule) { + DHTModuleRegistry_register(allocator->clone(sizeof(struct DHTModule), + allocator, + &(struct DHTModule) + { .name = "RouterModule", .context = out, .handleIncoming = handleIncoming, @@ -422,7 +428,7 @@ static inline void sendRequest(struct Address* address, message.address = address; - DHTModules_handleOutgoing(&message, registry); + DHTModuleRegistry_handleOutgoing(&message, registry); } /** diff --git a/dht/dhtcore/RouterModule.h b/dht/dhtcore/RouterModule.h index 4f820e75f..b6e9a605e 100644 --- a/dht/dhtcore/RouterModule.h +++ b/dht/dhtcore/RouterModule.h @@ -20,7 +20,7 @@ #include "admin/Admin.h" #include "dht/Address.h" -#include "dht/DHTModules.h" +#include "dht/DHTModuleRegistry.h" #include "dht/dhtcore/Node.h" #include "benc/Object.h" #include "util/Log.h" diff --git a/dht/test/CMakeLists.txt b/dht/test/CMakeLists.txt index b651f1e0a..02ff556ff 100644 --- a/dht/test/CMakeLists.txt +++ b/dht/test/CMakeLists.txt @@ -16,6 +16,8 @@ string(REPLACE ${CMAKE_SOURCE_DIR} ${main_dir_name} this_dir ${CMAKE_CURRENT_SOU message("-- Tests to run for " ${this_dir}) add_definitions(-g) +# don't ask + # Anything in this dir which ends with "test.c" is considered a test. file(GLOB tests "*test.c") foreach(test_source_fullpath ${tests}) diff --git a/dht/test/DHTModules_handleIncoming_test.c b/dht/test/DHTModules_handleIncoming_test.c index 2b5161729..38bbf4377 100644 --- a/dht/test/DHTModules_handleIncoming_test.c +++ b/dht/test/DHTModules_handleIncoming_test.c @@ -11,7 +11,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include "dht/DHTModules.h" + +#include "dht/DHTModule.h" +#include "dht/DHTModuleRegistry.h" #include "memory/Allocator.h" #include "memory/MallocAllocator.h" @@ -63,14 +65,14 @@ int testInputHandler() struct Allocator* allocator = MallocAllocator_new(2048); - struct DHTModuleRegistry* reg = DHTModules_new(allocator); - DHTModules_register(&module, reg); - DHTModules_register(&module2, reg); + struct DHTModuleRegistry* reg = DHTModuleRegistry_new(allocator); + DHTModuleRegistry_register(&module, reg); + DHTModuleRegistry_register(&module2, reg); - DHTModules_handleIncoming(&theMessage, reg); + DHTModuleRegistry_handleIncoming(&theMessage, reg); /* This should be ignored. */ - DHTModules_handleOutgoing(&theMessage, reg); + DHTModuleRegistry_handleOutgoing(&theMessage, reg); if (context.ret == -1) { printf("message not received"); diff --git a/dht/test/DHTModules_handleOutgoing_test.c b/dht/test/DHTModules_handleOutgoing_test.c index 1e90fd44f..6c4eee011 100644 --- a/dht/test/DHTModules_handleOutgoing_test.c +++ b/dht/test/DHTModules_handleOutgoing_test.c @@ -11,7 +11,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include "dht/DHTModules.h" + +#include "dht/DHTMessage.h" +#include "dht/DHTModule.h" +#include "dht/DHTModuleRegistry.h" #include "memory/Allocator.h" #include "memory/MallocAllocator.h" @@ -52,13 +55,13 @@ int testOutputHandler() struct Allocator* allocator = MallocAllocator_new(2048); - struct DHTModuleRegistry* reg = DHTModules_new(allocator); - DHTModules_register(&module, reg); + struct DHTModuleRegistry* reg = DHTModuleRegistry_new(allocator); + DHTModuleRegistry_register(&module, reg); - DHTModules_handleOutgoing(&theMessage, reg); + DHTModuleRegistry_handleOutgoing(&theMessage, reg); /* These should be ignored. */ - DHTModules_handleIncoming(&theMessage, reg); + DHTModuleRegistry_handleIncoming(&theMessage, reg); if (context.ret == -1) { printf("message not received"); diff --git a/dht/test/DHTModules_serialization_test.c b/dht/test/DHTModules_serialization_test.c deleted file mode 100644 index 62678cb3b..000000000 --- a/dht/test/DHTModules_serialization_test.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * You may redistribute this program and/or modify it under the terms of - * the GNU General Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -/* - * This test tests the ability of DHTModules.c to serialize the contexts - * of the modules. For (de)serialization of the messages go in and out, - * see SerializationModule_test.c - */ - -#include -#include - -#include "dht/DHTModules.h" -#include "memory/Allocator.h" -#include "memory/BufferAllocator.h" -#include "io/Writer.h" -#include "io/ArrayWriter.h" -#include "io/Reader.h" -#include "io/ArrayReader.h" - -static const char* control = "d10:TestModuled2:hi11:Hello Worldee"; - -static const String hello = { .bytes = "Hello World!", .len = 12 }; -static const String hi = { .bytes = "hi", .len = 2 }; - -static Dict* serialize(void* vcontext) -{ - char* buffer = calloc(2048, 1); - struct Allocator* allocator = BufferAllocator_new(buffer, 2048); - Dict* out = Dict_new(allocator); - Dict_putString(out, &hi, String_new((char*) vcontext, allocator), allocator); - return out; -} - -static int testSerialization() -{ - char* context = "Hello World"; - - struct DHTModule module = { - .name = "TestModule", - .context = context, - .serialize = serialize - }; - - char buffer[256]; - struct Allocator* allocator = BufferAllocator_new(buffer, 256); - char writeBuffer[64]; - memset(writeBuffer, 0, 64); - struct Writer* writer = ArrayWriter_new(writeBuffer, 64, allocator); - - struct DHTModuleRegistry* reg = DHTModules_new(allocator); - DHTModules_register(&module, reg); - - DHTModules_serialize(reg, writer); - - /* This is ok because the output is null padded at the end. */ - printf("The content is: %s\nand the length is: %d\n", - writeBuffer, - (int) writer->bytesWritten(writer)); - - if (writer->bytesWritten(writer) != strlen(control)) { - printf("Length mismatch! expected: %d", (int) strlen(control)); - } - - return memcmp(writeBuffer, control, writer->bytesWritten(writer)); -} - -static void deserialize(const Dict* serialData, void* vcontext) -{ - char* context = (char*) vcontext; - String* out = Dict_getString(serialData, &hi); - memcpy(context, out->bytes, out->len); -} - -int testDeserialization() -{ - char context[12]; - - struct DHTModule module = { - .name = "TestModule", - .context = context, - .deserialize = deserialize - }; - - char buffer[512]; - struct Allocator* allocator = BufferAllocator_new(buffer, 512); - struct Reader* reader = ArrayReader_new(control, strlen(control), allocator); - - struct DHTModuleRegistry* reg = DHTModules_deserialize(reader, allocator); - DHTModules_register(&module, reg); - - context[11] = '\0'; - - printf("Deserialization output is: %s\n", context); - - return memcmp(context, "Hello World", 11); -} - -int main() -{ - return - testSerialization() - | testDeserialization(); -} diff --git a/interface/test/InterfaceController_test.c b/interface/test/InterfaceController_test.c index 4bacd9303..9ee618953 100644 --- a/interface/test/InterfaceController_test.c +++ b/interface/test/InterfaceController_test.c @@ -150,7 +150,7 @@ int main() SwitchCore_setRouterInterface(&iface, switchCore); // These are unused. - struct DHTModuleRegistry* registry = DHTModules_new(alloc); + struct DHTModuleRegistry* registry = DHTModuleRegistry_new(alloc); struct RouterModule* rm = RouterModule_register(registry, alloc, publicKey, eventBase, logger, NULL); diff --git a/scripts/checkfiles.pl b/scripts/checkfiles.pl index 87789d86f..90eff2a75 100644 --- a/scripts/checkfiles.pl +++ b/scripts/checkfiles.pl @@ -86,11 +86,17 @@ sub error { $lineInfo = "$fileName:$lineNum"; if ($fileName =~ /\.h$/) { - if ($line =~ /^struct / && !($line =~ /^struct ${name}/) && !($line =~ /\(/)) { + my $n = $name; + # If the name is CryptoAuth_struct.h, it's ok to make a structure called CryptoAuth + if ($name =~ /^(.*)_struct$/) { + $n = $1; + } + + if ($line =~ /^struct / && !($line =~ /^struct ${n}/) && !($line =~ /\(/)) { error("all structures must begin with the name of the file."); } - if ($line =~ /#define / && $line !~ /#define $name/) { + if ($line =~ /#define / && $line !~ /#define $n/) { error("all defines must begin with the name of the file."); } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8bd01a46f..853364f06 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,6 +25,7 @@ target_link_libraries(TestFramework dhtcore cjdbenc cjdbenc_JsonBencSerializer + cjdbenc_StandardBencSerializer cjdmemory cjdadmin ${LIBEVENT2_LIBRARIES} diff --git a/test/TestFramework.c b/test/TestFramework.c index 73468071b..b7ffd8f44 100644 --- a/test/TestFramework.c +++ b/test/TestFramework.c @@ -43,7 +43,7 @@ struct Ducttape* TestFramework_setUp() struct SwitchCore* switchCore = SwitchCore_new(logger, allocator); //struct CryptoAuth* ca = CryptoAuth_new(NULL, allocator, privateKey, base, logger); - struct DHTModuleRegistry* registry = DHTModules_new(allocator); + struct DHTModuleRegistry* registry = DHTModuleRegistry_new(allocator); ReplyModule_register(registry, allocator); struct RouterModule* routerModule =