2
* This program is free software; you can redistribute it and/or
3
* modify it under the terms of the GNU General Public License as
4
* published by the Free Software Foundation; either version 3 of the
5
* License, or (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
* General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18
#include "DHTModules.h"
20
#include <libbenc/benc.h>
21
#include <memory/MemAllocator.h>
22
#include <memory/BufferAllocator.h>
23
#include <io/Reader.h>
24
#include <io/ArrayReader.h>
25
#include <io/Writer.h>
26
#include <io/ArrayWriter.h>
30
/* The number of bytes for storing deserialized packets. */
31
#define WORKSPACE_SIZE 4096
33
struct SerializationModule_context {
34
char workspace[WORKSPACE_SIZE];
35
struct MemAllocator* allocator;
36
struct DHTModule module;
39
/*--------------------Prototypes--------------------*/
40
static void freeModule(struct DHTModule* module);
41
static int handleOutgoing(struct DHTMessage* message,
43
static int handleIncoming(struct DHTMessage* message,
46
/*--------------------Interface--------------------*/
49
* Get a new SerializationModule.
51
* @return a new SerializationModule or NULL if there is no space to allocate required memory.
53
struct DHTModule* SerializationModule_new()
55
struct SerializationModule_context* context =
56
malloc(sizeof(struct SerializationModule_context));
58
if (context == NULL) {
62
context->allocator = BufferAllocator_new(context->workspace, WORKSPACE_SIZE);
64
if (context->allocator == NULL) {
68
struct DHTModule module = {
69
.name = "SerializationModule",
75
.handleIncoming = handleIncoming,
76
.handleOutgoing = handleOutgoing
79
memcpy(&context->module, &module, sizeof(struct DHTModule));
81
return &context->module;
84
/*--------------------Internals--------------------*/
86
/** @see DHTModule->freeContext in DHTModules.h */
87
static void freeModule(struct DHTModule* module)
93
struct SerializationModule_context* context =
94
(struct SerializationModule_context*) module->context;
100
* Take an outgoing message and serialize the bencoded message.
102
* @see DHTModule->handleOutgoing in DHTModules.h
104
static int handleOutgoing(struct DHTMessage* message,
107
struct MemAllocator* allocator =
108
((struct SerializationModule_context*) vcontext)->allocator;
110
allocator->free(allocator);
111
struct Writer* writer = ArrayWriter_new(message->bytes, MAX_MESSAGE_SIZE, allocator);
112
if (writer == NULL) {
116
bobj_serialize(writer, message->bencoded);
117
message->length = writer->bytesWritten(writer);
123
* Take an incoming message and deserialize the bencoded message.
125
* @see DHTModule->handleIncoming in DHTModules.h
127
static int handleIncoming(struct DHTMessage* message,
130
struct MemAllocator* allocator =
131
((struct SerializationModule_context*) vcontext)->allocator;
133
allocator->free(allocator);
134
struct Reader* reader = ArrayReader_new(message->bytes, MAX_MESSAGE_SIZE, allocator);
135
if (reader == NULL) {
139
bobj_parse(reader, allocator, &message->bencoded);
144
#undef WORKSPACE_SIZE