aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/messenger_api_handle.c
blob: 20ef77254f8028e155d2cf3fdbea447d09d32898 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
   This file is part of GNUnet.
   Copyright (C) 2020 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   GNUnet 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
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */
/**
 * @author Tobias Frisch
 * @file src/messenger/messenger_api_handle.c
 * @brief messenger api: client implementation of GNUnet MESSENGER service
 */

#include "messenger_api_handle.h"

struct GNUNET_MESSENGER_Handle*
create_handle (const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_MESSENGER_IdentityCallback identity_callback,
               void *identity_cls, GNUNET_MESSENGER_MessageCallback msg_callback, void *msg_cls)
{
  struct GNUNET_MESSENGER_Handle *handle = GNUNET_new(struct GNUNET_MESSENGER_Handle);

  handle->cfg = cfg;
  handle->mq = NULL;

  handle->identity_callback = identity_callback;
  handle->identity_cls = identity_cls;

  handle->msg_callback = msg_callback;
  handle->msg_cls = msg_cls;

  handle->name = NULL;
  handle->pubkey = NULL;

  handle->reconnect_time = GNUNET_TIME_relative_get_zero_ ();
  handle->reconnect_task = NULL;

  handle->rooms = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
  handle->contacts = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);

  return handle;
}

static int
iterate_destroy_room (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  struct GNUNET_MESSENGER_Room *room = value;

  destroy_room (room);

  return GNUNET_YES;
}

static int
iterate_destroy_contact (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  struct GNUNET_MESSENGER_Contact *contact = value;

  destroy_contact (contact);

  return GNUNET_YES;
}

void
destroy_handle (struct GNUNET_MESSENGER_Handle *handle)
{
  if (handle->reconnect_task)
    GNUNET_SCHEDULER_cancel (handle->reconnect_task);

  if (handle->mq)
    GNUNET_MQ_destroy (handle->mq);

  if (handle->name)
    GNUNET_free(handle->name);

  if (handle->pubkey)
    GNUNET_free(handle->pubkey);

  if (handle->rooms)
  {
    GNUNET_CONTAINER_multihashmap_iterate (handle->rooms, iterate_destroy_room, NULL);

    GNUNET_CONTAINER_multihashmap_destroy (handle->rooms);
  }

  if (handle->contacts)
  {
    GNUNET_CONTAINER_multihashmap_iterate (handle->contacts, iterate_destroy_contact, NULL);

    GNUNET_CONTAINER_multihashmap_destroy (handle->contacts);
  }

  GNUNET_free(handle->name);
}

void
set_handle_name (struct GNUNET_MESSENGER_Handle *handle, const char *name)
{
  if (handle->name)
    GNUNET_free(handle->name);

  handle->name = name? GNUNET_strdup(name) : NULL;
}

const char*
get_handle_name (const struct GNUNET_MESSENGER_Handle *handle)
{
  return handle->name;
}

void
set_handle_key (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_IDENTITY_PublicKey *pubkey)
{
  if (!handle->pubkey)
    handle->pubkey = GNUNET_new(struct GNUNET_IDENTITY_PublicKey);

  GNUNET_memcpy(handle->pubkey, pubkey, sizeof(*pubkey));
}

const struct GNUNET_IDENTITY_PublicKey*
get_handle_key (const struct GNUNET_MESSENGER_Handle *handle)
{
  if (!handle->pubkey)
  {
    struct GNUNET_IDENTITY_Ego *anonymous = GNUNET_IDENTITY_ego_get_anonymous ();
    static struct GNUNET_IDENTITY_PublicKey pubkey;

    GNUNET_IDENTITY_ego_get_public_key (anonymous, &pubkey);

    return &pubkey;
  }

  return handle->pubkey;
}

struct GNUNET_MESSENGER_Contact*
get_handle_contact_by_pubkey (const struct GNUNET_MESSENGER_Handle *handle,
                              const struct GNUNET_IDENTITY_PublicKey *pubkey)
{
  struct GNUNET_HashCode hash;

  GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash);

  struct GNUNET_MESSENGER_Contact *contact = GNUNET_CONTAINER_multihashmap_get (handle->contacts, &hash);

  if (contact)
    return contact;

  contact = create_contact (pubkey);

  if (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (handle->contacts, &hash, contact,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
    return contact;

  destroy_contact (contact);
  return NULL;
}

void
swap_handle_contact_by_pubkey (struct GNUNET_MESSENGER_Handle *handle, struct GNUNET_MESSENGER_Contact *contact,
                               const struct GNUNET_IDENTITY_PublicKey *pubkey)
{
  const struct GNUNET_HashCode *hash = get_contact_id_from_key (contact);

  if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (handle->contacts, hash, contact))
  {
    GNUNET_memcpy(&(contact->public_key), pubkey, sizeof(*pubkey));

    hash = get_contact_id_from_key (contact);

    GNUNET_CONTAINER_multihashmap_put (handle->contacts, hash, contact,
                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  }
}

void
open_handle_room (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key)
{
  struct GNUNET_MESSENGER_Room *room = GNUNET_CONTAINER_multihashmap_get (handle->rooms, key);

  if (room)
    room->opened = GNUNET_YES;
}

void
entry_handle_room_at (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_PeerIdentity *door,
                      const struct GNUNET_HashCode *key)
{
  struct GNUNET_MESSENGER_Room *room = GNUNET_CONTAINER_multihashmap_get (handle->rooms, key);

  if (room)
    add_to_list_tunnels (&(room->entries), door);
}

void
close_handle_room (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key)
{
  struct GNUNET_MESSENGER_Room *room = GNUNET_CONTAINER_multihashmap_get (handle->rooms, key);

  if ((room) && (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (handle->rooms, key, room)))
    destroy_room (room);
}