aboutsummaryrefslogtreecommitdiff
path: root/src/services/gnunet-service-namecache-dbus.c
blob: 7eded44c6deffbd776b3657a6cf590f4c1d76a85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "config.h"

#include <gnunet/platform.h>
#include <gnunet/gnunet_common.h>
#include <gnunet/gnunet_configuration_lib.h>
#include <gnunet/gnunet_getopt_lib.h>
#include <gnunet/gnunet_strings_lib.h>
#include <gnunet/gnunet_program_lib.h>
#include <gnunet/gnunet_namecache_service.h>

#include "gnunet_dbus_lib.h"

static void
block_cache_return (
    void *cls,
    int32_t success,
    const char *emsg)
{
  struct GNUNET_DBUS_MethodContext *mc = (struct GNUNET_DBUS_MethodContext *)cls;

  DBusMessage *message = mc->message;
  DBusMessage *reply = dbus_message_new_method_return (message);
  DBusMessageIter reply_iter;
  dbus_message_iter_init_append (reply, &reply_iter);
  GNUNET_DBUS_message_iter_push_int32 (&reply_iter, success);
  GNUNET_DBUS_message_iter_push_string (&reply_iter, emsg);

  GNUNET_DBUS_method_context_send_reply (mc, reply);
  GNUNET_DBUS_method_context_unref (mc);
};

static void
block_cache (
    struct GNUNET_DBUS_MethodContext *mc)
{
  struct GNUNET_GNSRECORD_Block block;

  DBusMessage *dbus_message = mc->message;
  DBusMessageIter iter;
  dbus_message_iter_init (dbus_message, &iter);
  DBusMessage *reply = GNUNET_DBUS_message_iter_pop_gnsrecord_block (dbus_message, &iter, "block", &block);
  if (reply)
  {
    GNUNET_DBUS_method_context_send_reply (mc, reply);
    return;
  };

  GNUNET_DBUS_method_context_ref (mc);
  struct GNUNET_NAMECACHE_Handle *handle = GNUNET_DBUS_client_get_data (mc->client);
  GNUNET_NAMECACHE_block_cache (handle, &block, block_cache_return, mc);
};

static void
client_connects (
    struct GNUNET_DBUS_Service *service,
    struct GNUNET_DBUS_Client *client)
{
  const struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_DBUS_service_get_config (service);
  struct GNUNET_NAMECACHE_Handle *handle = GNUNET_NAMECACHE_connect (cfg);
  GNUNET_DBUS_client_set_data (client, handle);
};

static void
client_disconnects (
    struct GNUNET_DBUS_Service *service,
    struct GNUNET_DBUS_Client *client)
{
  (void)service;
  GNUNET_NAMECACHE_disconnect (GNUNET_DBUS_client_get_data (client));
};

static void
shutdown_task (
    void *cls,
    const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  (void)tc;

  struct GNUNET_DBUS_Service *gns_service = (struct GNUNET_DBUS_Service *)cls;
  GNUNET_DBUS_service_destroy (gns_service);

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Exiting.\n");
};

static void
run (
    void *cls,
    char *const *args,
    const char *configfile,
    const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running.\n");

  struct GNUNET_DBUS_Service *namecache_service = GNUNET_DBUS_service_create (cfg, "namecache");
  GNUNET_DBUS_service_set_client_handlers (namecache_service, client_connects, client_disconnects);

  struct GNUNET_DBUS_Object *namecache_object = GNUNET_DBUS_object_create ("/namecache", NULL);
  int err = GNUNET_DBUS_service_add_object (namecache_service, namecache_object);
  if (GNUNET_OK != err)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to add /namecache object. GNUNET_DBUS_service_add_object returned %d\n", err);
    GNUNET_abort_ ();
  };

  struct GNUNET_DBUS_Interface *namecache_interface = GNUNET_DBUS_interface_create ("gnu.gnunet.namecache");
  GNUNET_DBUS_object_add_interface (namecache_object, GNUNET_DBUS_interface_introspectable ());
  GNUNET_DBUS_object_add_interface (namecache_object, namecache_interface);
  
  struct GNUNET_DBUS_Method *namecache_method_block_cache = GNUNET_DBUS_method_create ("block_cache", block_cache);
  GNUNET_DBUS_interface_add_method (namecache_interface, namecache_method_block_cache);
  GNUNET_DBUS_method_add_arg (namecache_method_block_cache, "block", GNUNET_DBUS_SIGNATURE_GNSRECORD_BLOCK);

  GNUNET_DBUS_method_add_return_arg (namecache_method_block_cache, "success", GNUNET_DBUS_SIGNATURE_INT32);
  GNUNET_DBUS_method_add_return_arg (namecache_method_block_cache, "error_msg", GNUNET_DBUS_SIGNATURE_STRING);

  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, namecache_service);
};

int
main (
    int argc,
    char *const *argv)
{
  int ret;

  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };

  static const char bin_name[] = "gnunet-service-namecache-dbus [OPTIONS]";
  static const char bin_help[] = "DBus proxy for gnunet-service-namecache";

  ret = GNUNET_log_setup ("gnunet-service-namecache-dbus", "DEBUG", NULL);
  if (GNUNET_OK != ret)
  {
    fprintf (stderr, "ERROR: Failed to setup logging. GNUNET_log_setup returned %d\n", ret);
    return 1;
  };

  ret = GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv);
  if (GNUNET_OK != ret)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to parse command line options. GNUNET_STRINGS_get_utf8_args returned %d\n", ret);
    return 1;
  };

  ret = GNUNET_PROGRAM_run (argc, argv, bin_name, bin_help, options, run, NULL);
  if (GNUNET_OK != ret)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to run program. GNUNET_PROGRAM_run returned %d\n", ret);
    return 1;
  };

  printf ("leaving main()\n");
  GNUNET_free ((void *)argv);
  return 0;
}