aboutsummaryrefslogtreecommitdiff
path: root/src/services/gnunet-service-gns-dbus.c
blob: 29ac2f86c701e8c43499c18cf10ac581509476b3 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#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_gns_service.h>

#include "gnunet_dbus_lib.h"
#include "gnunet_crypto_dbus_lib.h"
#include "gnunet_gnsrecord_dbus_lib.h"

#define LOG(kind, ...) GNUNET_log_from (kind, "gns-dbus", __VA_ARGS__)

static void
lookup_return (
    void *cls,
    uint32_t rd_count,
    const struct GNUNET_GNSRECORD_Data *rd)
{
  struct GNUNET_DBUS_MethodContext *mc = (struct GNUNET_DBUS_MethodContext *)cls;
  unsigned msg_serial = dbus_message_get_serial (mc->message);
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply from GNS. Method call id %u\n", msg_serial);
  DBusMessage *reply = GNUNET_DBUS_method_context_create_reply (mc);
  DBusMessageIter reply_iter;
  dbus_message_iter_init_append (reply, &reply_iter);

  DBusMessageIter reply_iter_sub;
  GNUNET_DBUS_push_open_array (reply, &reply_iter, &reply_iter_sub, GNUNET_GNSRECORD_DBUS_SIGNATURE_DATA);
  uint32_t i;
  for (i = 0; i < rd_count; i++)
    GNUNET_GNSRECORD_DBUS_push_data (reply, &reply_iter_sub, rd + i);
  GNUNET_DBUS_push_close_array (reply, &reply_iter, &reply_iter_sub);

  GNUNET_DBUS_method_context_send_reply (mc, reply);
  GNUNET_DBUS_method_context_unref (mc);
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarded reply to dbus. Method call id %u\n", msg_serial);
};

#if 0
static void
lookup_timeout (
    void *cls,
    const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_GNS_LookupRequest *lr = (struct GNUNET_GNS_LookupRequest *)cls;
  GNUNET_G
}
#endif

static void
lookup (
    struct GNUNET_DBUS_MethodContext *mc)
{
  const char *name;
  struct GNUNET_CRYPTO_EcdsaPublicKey zone;
  uint32_t type;
  dbus_bool_t only_cached;

  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received lookup request from dbus. Method call id %u\n", dbus_message_get_serial (mc->message));

  DBusMessage *message = mc->message;
  DBusMessageIter message_iter;
  dbus_message_iter_init (message, &message_iter);

  DBusMessage *reply = NULL;
  reply = reply ? reply : GNUNET_DBUS_pop_string (message, &message_iter, "name", &name);
  reply = reply ? reply : GNUNET_CRYPTO_DBUS_pop_ecdsa_public_key (message, &message_iter, "zone", &zone);
  reply = reply ? reply : GNUNET_GNSRECORD_DBUS_pop_type (message, &message_iter, "type", &type);
  reply = reply ? reply : GNUNET_DBUS_pop_boolean (message, &message_iter, "only_cached", &only_cached);
  if (reply)
  {
    GNUNET_DBUS_method_context_send_reply (mc, reply);
    return;
  };

  GNUNET_DBUS_method_context_ref (mc);
  struct GNUNET_GNS_Handle *handle = GNUNET_DBUS_client_get_data (mc->client);
  struct GNUNET_GNS_LookupRequest *lr = GNUNET_GNS_lookup (
      handle,
      name, &zone, (int)type, (int)only_cached, NULL,
      lookup_return, mc);
  (void)lr;

  //GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (500, GNUNET_TIME_UNIT_MILLISECONDS), 
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarded lookup to GNS. Method call id %u\n", dbus_message_get_serial (mc->message));
};

static void
client_connects (
    struct GNUNET_DBUS_Service *service,
    struct GNUNET_DBUS_Client *client)
{
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating GNS client for %s\n", GNUNET_DBUS_client_get_unique_name (client));
  const struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_DBUS_service_get_config (service);
  struct GNUNET_GNS_Handle *handle = GNUNET_GNS_connect (cfg);
  GNUNET_DBUS_client_set_data (client, handle);
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished creating GNS client for %s\n", GNUNET_DBUS_client_get_unique_name (client));
};

static void
client_disconnects (
    struct GNUNET_DBUS_Service *service,
    struct GNUNET_DBUS_Client *client)
{
  (void)service;
  GNUNET_GNS_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_unref (gns_service);

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

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

  struct GNUNET_DBUS_Service *gns_service = GNUNET_DBUS_service_create (cfg, "gns");
  if (! gns_service)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create gns service.\n");
    GNUNET_abort_ ();
  };
  GNUNET_DBUS_service_set_client_handlers (gns_service, client_connects, client_disconnects);

  struct GNUNET_DBUS_Object *gns_object = GNUNET_DBUS_service_get_root_object (gns_service);

  struct GNUNET_DBUS_Interface *gns_interface = GNUNET_DBUS_interface_create ("gnu.gnunet.gns");
  GNUNET_DBUS_object_add_interface (gns_object, GNUNET_DBUS_interface_introspectable ());
  GNUNET_DBUS_object_add_interface (gns_object, gns_interface);

  struct GNUNET_DBUS_Method *gns_method_lookup = GNUNET_DBUS_method_create ("lookup", lookup);
  GNUNET_DBUS_interface_add_method (gns_interface, gns_method_lookup);
  GNUNET_DBUS_method_add_arg (gns_method_lookup, "name", GNUNET_DBUS_SIGNATURE_STRING);
  GNUNET_DBUS_method_add_arg (gns_method_lookup, "zone", GNUNET_CRYPTO_DBUS_SIGNATURE_ECDSA_PUBLIC_KEY);
  GNUNET_DBUS_method_add_arg (gns_method_lookup, "type", GNUNET_GNSRECORD_DBUS_SIGNATURE_TYPE);
  GNUNET_DBUS_method_add_arg (gns_method_lookup, "only_cached", GNUNET_DBUS_SIGNATURE_BOOLEAN);

  GNUNET_DBUS_method_add_return_arg (gns_method_lookup, "records", GNUNET_DBUS_SIGNATURE_ARRAY (GNUNET_GNSRECORD_DBUS_SIGNATURE_DATA));

  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, gns_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-gns-dbus [OPTIONS]";
  static const char bin_help[] = gettext_noop ("DBus proxy for gnunet-service-gns");

  ret = GNUNET_log_setup ("gnunet-service-gns-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)
  {
    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)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to run program. GNUNET_PROGRAM_run returned %d\n", ret);
    return 1;
  };

  GNUNET_free ((void *)argv);
  return 0;
};