aboutsummaryrefslogtreecommitdiff
path: root/src/service/core/gnunet_core_underlay_dummy.c
blob: e0ef4c5bba1a719e2fd77e089b80d45091c466ed (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
     This file is part of GNUnet.
     Copyright (C) 2023 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
 */
/**
 * @addtogroup Core
 * @{
 *
 * @author Julius Bünger
 *
 * @file
 * Implementation of the dummy core underlay that uses unix domain sockets
 *
 * @defgroup CORE
 * Secure Communication with other peers
 *
 * @see [Documentation](https://gnunet.org/core-service) TODO
 *
 * @{
 */

#ifdef __cplusplus
extern "C" {
#if 0 /* keep Emacsens' auto-indent happy */
}
#endif
#endif


#include <sys/socket.h>
#include <unistd.h>
#include "gnunet_core_underlay_dummy.h"
#include "gnunet_util_lib.h"

#define LOG(kind, ...) GNUNET_log_from (kind, "core-underlay-dummy", __VA_ARGS__)

#define SERVER_ADDR_BASE "/tmp/gnunet-core-underlay-dummy-server-socket"
#define CLIENT_ADDR_BASE "/tmp/gnunet-core-underlay-dummy-client-socket"
#define BUFF_SIZE 8192
#define BACKLOG 10

/**
 * Opaque handle to the service.
 */
struct GNUNET_CORE_UNDERLAY_DUMMY_Handle
{
  GNUNET_CORE_UNDERLAY_DUMMY_NotifyConnect notify_connect;
  char *recv_addr;
};


/**
 * Connect to the core underlay dummy service.  Note that the connection may
 * complete (or fail) asynchronously.
 *
 * @param cfg configuration to use
 * @param handlers array of message handlers or NULL; note that the closures
 *                 provided will be ignored and replaced with the respective
 *                 return value from @a nc
 * @param cls closure for the @a nc, @a nd and @a na callbacks
 * @param nc function to call on connect events, or NULL
 * @param nd function to call on disconnect events, or NULL
 * @param na function to call on address changes, or NULL
 * @return NULL on error
 */
struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *
GNUNET_CORE_UNDERLAY_DUMMY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
                              const struct GNUNET_MQ_MessageHandler *handlers,
                              void *cls,
                              GNUNET_CORE_UNDERLAY_DUMMY_NotifyConnect nc,
                              GNUNET_CORE_UNDERLAY_DUMMY_NotifyDisconnect nd,
                              GNUNET_CORE_UNDERLAY_DUMMY_NotifyAddressChange na)
{
  // once core connects, we create a socket
  // I guess usually we'd connect to a running transport service that already
  // has or has not its open connections

  struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h;
  int64_t fd, fd_client;
  struct sockaddr_un addr;
  struct sockaddr_un addr_client;
  int64_t len;
  char buff[BUFF_SIZE];
  struct sockaddr_un addr_from;
  socklen_t from_len = sizeof(addr_from);

  h = GNUNET_malloc (sizeof (struct GNUNET_CORE_UNDERLAY_DUMMY_Handle));
  h->notify_connect = nc;

  if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0)
  {
    LOG(GNUNET_ERROR_TYPE_ERROR, "Fd does not open");
    return NULL;
  }

  memset(&addr, 0, sizeof (addr));
  addr.sun_family = AF_UNIX;
  strcpy (addr.sun_path, SERVER_ADDR_BASE);
  unlink (SERVER_ADDR_BASE);
  if (bind (fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
  {
    // TODO use a counter to just use the 'next' free socket
    LOG(GNUNET_ERROR_TYPE_ERROR, "Faild binding to socket");
    close(fd);
    return NULL;
  }
  // TODO we could now use GNUNET_CORE_UNDERLAY_NotifyAddressChange to signal
  // that we actually got the address
  h->recv_addr = GNUNET_malloc (strlen (SERVER_ADDR_BASE));
  h->recv_addr = GNUNET_strdup (SERVER_ADDR_BASE);

  if (listen (fd, BACKLOG) < 0)
  {
    LOG(GNUNET_ERROR_TYPE_ERROR, "Failed listening to socket");
    close(fd);
    return NULL;
  }

  fd_client = accept (fd, (struct sockaddr *) &addr_client, &from_len);
  if (fd_client < 0)
  {
    LOG(GNUNET_ERROR_TYPE_ERROR, "Error accepting incoming connection");
    close (fd);
    close (fd_client);
    return NULL;
  }
  // TODO use GNUNET_CORE_UNDERLAY_NotifyConnect to signal that another 'peer'
  // connected
  //
  // TODO pass received messages to mq
  return h;
}


/**
 * Disconnect from the core underlay dummy service.
 *
 * @param handle handle returned from connect
 */
void
GNUNET_CORE_UNDERLAY_DUMMY_disconnect
(struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *handle)
{
  // TODO delete and close everything
}


/**
 * Notification from the CORE service to the CORE UNDERLAY DUMMY service
 * that the CORE service has finished processing a message from
 * CORE UNDERLAY DUMMY (via the @code{handlers} of
 * #GNUNET_CORE_UNDERLAY_DUMMY_connect()) and that it is thus now OK for CORE
 * UNDERLAY DUMMY to send more messages for the peer with @a mq.
 *
 * Used to provide flow control, this is our equivalent to
 * #GNUNET_SERVICE_client_continue() of an ordinary service.
 *
 * Note that due to the use of a window, CORE UNDERLAY DUMMY may send multiple
 * messages destined for the same peer even without an intermediate
 * call to this function. However, CORE must still call this function
 * once per message received, as otherwise eventually the window will
 * be full and CORE UNDERLAY DUMMY will stop providing messages to CORE on @a
 * mq.
 *
 * @param ch core underlay dummy handle
 * @param mq continue receiving on this message queue
 */
void
GNUNET_CORE_UNDERLAY_DUMMY_receive_continue (
    struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *ch,
    struct GNUNET_MQ_Handle *mq)
{
}


/**
 * Instruct the underlay dummy to try to connect to another peer.
 *
 * Once the connection was successful, the
 * #GNUNET_CORE_UNDERLAY_DUMMY_NotifyConnect
 * will be called with a mq towards the peer.
 *
 * @param ch core underlay dummy handle
 * @param peer_address URI of the peer to connect to
 * @param pp what kind of priority will the application require (can be
 *           #GNUNET_MQ_PRIO_BACKGROUND, we will still try to connect)
 * @param bw desired bandwidth, can be zero (we will still try to connect)
 */
void
GNUNET_CORE_UNDERLAY_DUMMY_connect_to_peer (
    struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *ch,
    const char *peer_address,
    enum GNUNET_MQ_PriorityPreferences pp,
    struct GNUNET_BANDWIDTH_Value32NBO bw)
{
  int64_t fd;
  struct sockaddr_un addr;
  struct sockaddr_un addr_receiver;

  if ((fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0)
  {
    LOG(GNUNET_ERROR_TYPE_ERROR, "Failure opening socket");
    return;
  }

  memset (&addr, 0, sizeof (struct sockaddr_un));
  addr.sun_family = AF_UNIX;
  strcpy (addr.sun_path, CLIENT_ADDR_BASE);

  unlink (CLIENT_ADDR_BASE);
  if (bind (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0)
  {
    LOG(GNUNET_ERROR_TYPE_ERROR, "Failed to bind to socket");
    return;
  }

  addr_receiver.sun_family = AF_UNIX;
  strcpy (addr_receiver.sun_path, peer_address);
  if (connect (fd, (struct sockaddr *) &addr_receiver, sizeof(addr)) < 0)
  {
    LOG(GNUNET_ERROR_TYPE_ERROR, "failed to connect to the socket");
    return;
  }
  //GNUNET_MQ_queue_for_callbacks ()
  //h.notify_connect(cls, 1, peer_address, mq);
  //  FIXME: proper array
  //  FIXME: proper address format
}


#if 0 /* keep Emacsens' auto-indent happy */
{
#endif
#ifdef __cplusplus
}
#endif


/** @} */ /* end of group */

/** @} */ /* end of group addition */

/* end of gnunet_core_underlay_dummy.c */