Permalink
Browse files

code cleanup, 100% compliant with checkfiles.pl now :D

  • Loading branch information...
1 parent eff7285 commit 92bce3a4ba3509864606efb34d6bfd8d614c8df6 Caleb James DeLisle committed Apr 15, 2012
View
@@ -17,4 +17,4 @@ add_library(cjdadmin
Configurator.c
)
-target_link_libraries(cjdadmin crypto)
+target_link_libraries(cjdadmin crypto cjdbenc_StandardBencSerializer)
View
@@ -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);
View
@@ -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
View
@@ -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.
View
@@ -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
)
View
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+#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
View
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+#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
@@ -11,35 +11,28 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <assert.h>
-#include <string.h>
+#include "dht/DHTModule.h"
+#include "dht/DHTModuleRegistry.h"
#include "memory/Allocator.h"
#include "memory/BufferAllocator.h"
#include "io/Reader.h"
#include "io/Writer.h"
#include "benc/Object.h"
#include "benc/serialization/BencSerializer.h"
#include "benc/serialization/standard/StandardBencSerializer.h"
-#include "DHTModules.h"
+
+#include <assert.h>
+#include <string.h>
#define DEBUG2(x, y)
/* #define DEBUG2(x, y) fprintf(stderr, x, y); fflush(stderr) */
/** 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);
+}
Oops, something went wrong.

0 comments on commit 92bce3a

Please sign in to comment.