~timkross/cjdns/cjdns

« back to all changes in this revision

Viewing changes to dns/DNSNetworkModule.c

  • Committer: cjdelisle
  • Date: 2011-02-16 23:03:00 UTC
  • Revision ID: git-v1:d475c9c10adc25590bea5e7dc5383b32f66d5eb8
First commit for cjdns.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <event2/dns.h>
 
2
#include <event2/event.h>
 
3
#include <string.h>
 
4
 
 
5
#include "DNSModules.h"
 
6
 
 
7
 
 
8
struct DNSNetworkModule_context {
 
9
    struct event_base* base;
 
10
    evutil_socket_t socket;
 
11
    struct evdns_server_port* server;
 
12
    struct DNSModuleRegistry* registry;
 
13
};
 
14
 
 
15
static int handleOutgoing(struct DNSMessage* message,
 
16
                          struct DNSModule* module,
 
17
                          const struct DNSModuleRegistry* registry);
 
18
static void handleNetworkEvent(struct evdns_server_request* request, void* module);
 
19
static void shutdownModule(struct DNSModule* module);
 
20
 
 
21
/**
 
22
 * Create a new DNS network module.
 
23
 *
 
24
 * @param base a LibEvent context.
 
25
 * @param socket the socket to bind to.
 
26
 * @param allocator the memory allocator to use for getting memory.
 
27
 */
 
28
struct DNSModule* DNSNetworkModule_new(struct event_base* base,
 
29
                                       evutil_socket_t socket,
 
30
                                       struct MemAllocator* allocator)
 
31
{
 
32
    struct DNSNetworkModule_context* context =
 
33
        allocator->malloc(sizeof(struct DNSNetworkModule_context), allocator);
 
34
 
 
35
    struct DNSModule* module = allocator->malloc(sizeof(struct DNSModule), allocator);
 
36
 
 
37
    if (context == NULL || module == NULL) {
 
38
        return NULL;
 
39
    }
 
40
 
 
41
    context->base = base;
 
42
    context->socket = socket;
 
43
 
 
44
    struct DNSModule localModule = {
 
45
        .name = "DNSNetworkModule",
 
46
        .context = context,
 
47
        .free = shutdownModule,
 
48
        .serialize = NULL,
 
49
        .deserialize = NULL,
 
50
        .handleIncoming = NULL,
 
51
        .handleOutgoing = handleOutgoing
 
52
    };
 
53
    memcpy(module, &localModule, sizeof(struct DNSModule));
 
54
 
 
55
    return module;
 
56
}
 
57
 
 
58
/**
 
59
 * Register the module.
 
60
 *
 
61
 * @param module the module to register.
 
62
 * @param registry the DNSModuleRegistry to call the on an incoming message.
 
63
 * @return whatever is returned by DNSModules_Register().
 
64
 */
 
65
int DNSNetworkModule_register(struct DNSModule* module,
 
66
                              struct DNSModuleRegistry* registry)
 
67
{
 
68
    struct DNSNetworkModule_context* context =
 
69
        (struct DNSNetworkModule_context*) module->context;
 
70
 
 
71
    context->registry = registry;
 
72
 
 
73
    context->server = evdns_add_server_port_with_base(context->base, context->socket,
 
74
                                                      0, handleNetworkEvent, context);
 
75
 
 
76
    return DNSModules_register(module, registry);
 
77
}
 
78
 
 
79
/*--------------------Internal--------------------*/
 
80
 
 
81
static void shutdownModule(struct DNSModule* module)
 
82
{
 
83
    struct DNSNetworkModule_context* context =
 
84
        (struct DNSNetworkModule_context*) module->context;
 
85
 
 
86
    evdns_close_server_port(context->server);
 
87
}
 
88
 
 
89
static void handleNetworkEvent(struct evdns_server_request* request,
 
90
                               void* vcontext)
 
91
{
 
92
    struct DNSNetworkModule_context* context =
 
93
        (struct DNSNetworkModule_context*) vcontext;
 
94
 
 
95
    struct DNSMessage message = {
 
96
        .request = request,
 
97
        .returnCode = DNS_ERR_NONE
 
98
    };
 
99
    DNSModules_handleIncoming(&message, context->registry);
 
100
}
 
101
 
 
102
static int handleOutgoing(struct DNSMessage* message,
 
103
                          struct DNSModule* module,
 
104
                          const struct DNSModuleRegistry* registry)
 
105
{
 
106
    module = module;
 
107
    registry = registry;
 
108
    evdns_server_request_respond(message->request, message->returnCode);
 
109
    return 0;
 
110
}