aboutsummaryrefslogtreecommitdiff
path: root/src/ext/gnunet-service-ext.c
blob: 83cbd523987afddba6c628da734cd339cb1dca48 (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
/*
     This file is part of GNUnet.
     Copyright (C) 20xx GNUnet e.V.

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file ext/gnunet-service-ext.c
 * @brief ext service implementation
 * @author Christian Grothoff
 */
#include "gnunet_ext_config.h"
#include <stddef.h>

#if WINDOWS
#define FDTYPE HANDLE
#define SOCKTYPE SOCKET
#else
#define FDTYPE int
#define SOCKTYPE int
#endif

#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

#include <gnunet/gnunet_util_lib.h>
#include "gnunet_protocols_ext.h"

/**
 * Some state we track per client.
 */
struct ClientContext
{
  /**
   * For telling service to continue processing more messages.
   */
  struct GNUNET_SERVICE_Client *c;

  /**
   * For sending messages to the client.
   */
  struct GNUNET_MQ_Handle *mq;

  /**
   * Sample state.
   */
  uint32_t state;
};


/**
 * Our configuration.
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * This structure holds informations about the project.
 */
static const struct GNUNET_OS_ProjectData gnunetext_pd =
{
  .libname = "libgnunetext",
  .project_dirname = "gnunet-ext",
  .binary_name = "gnunet-service-ext",
  .env_varname = "GNUNET_EXT_PREFIX",
  .base_config_varname = "GNUNET_EXT_BASE_CONFIG",
  .bug_email = "gnunet-developers@gnu.org",
  .homepage = "http://www.gnu.org/s/gnunet/",
  .config_file = "gnunet-ext.conf",
  .user_config_file = "~/.config/gnunet-ext.conf",
  .version = "1.0",
  .is_gnu = 1,
  .gettext_domain = PACKAGE,
  .gettext_path = NULL,
};

/**
 * Initialize the project with the data set in the
 * GNUNET_OS_ProjectData structure.  This is defined with
 * __attribute__ ((constructor)) because it has to be called before
 * the main function (implicitly defined by GNUNET_SERVICE_MAIN.)
 * Other "pre-main" initialization can be performed here too.
 */
static void __attribute__ ((constructor))
project_data_initialize (void)
{
  GNUNET_OS_init (&gnunetext_pd);
}

/**
 * Handle EXT-message.
 *
 * @param cls identification of the client
 * @param message the actual message
 */
static void
handle_ext (void *cls,
            const struct GNUNET_MessageHeader *message)
{
  struct ClientContext *cc = cls;
  struct GNUNET_MQ_Envelope *env;
  struct GNUNET_MessageHeader *response;

  /* Send same type of message back... */
  env = GNUNET_MQ_msg (response,
                       GNUNET_MESSAGE_TYPE_EXT);
  GNUNET_MQ_send (cc->mq,
                  env);

  /* Continue processing more messages from client */
  GNUNET_SERVICE_client_continue (cc->c);
}


/**
 * Task run during shutdown.
 *
 * @param cls unused
 */
static void
shutdown_task (void *cls)
{
  /* Clean up whatever #run() setup here. */
}


/**
 * Process statistics requests.
 *
 * @param cls closure
 * @param server the initialized server
 * @param c configuration to use
 */
static void
run (void *cls,
     struct GNUNET_SERVER_Handle *server,
     const struct GNUNET_CONFIGURATION_Handle *c)
{
  cfg = c;
  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
                                 NULL);
}


/**
 * Callback called when a client connects to the service.
 *
 * @param cls closure for the service
 * @param c the new client that connected to the service
 * @param mq the message queue used to send messages to the client
 * @return @a c
 */
static void *
client_connect_cb (void *cls,
		   struct GNUNET_SERVICE_Client *c,
		   struct GNUNET_MQ_Handle *mq)
{
  struct ClientContext *cc;

  cc = GNUNET_new (struct ClientContext);
  cc->c = c;
  cc->mq = mq;
  /* setup more for new client here */
  return cc;
}


/**
 * Callback called when a client disconnected from the service
 *
 * @param cls closure for the service
 * @param c the client that disconnected
 * @param internal_cls our `struct ClientContext`
 */
static void
client_disconnect_cb (void *cls,
		      struct GNUNET_SERVICE_Client *c,
		      void *internal_cls)
{
  struct ClientContext *cc = internal_cls;

  GNUNET_assert (cc->c == c);
  /* Tear down rest of client here */
  GNUNET_free (cc);
}


/**
 * Define "main" method using service macro.
 */
GNUNET_SERVICE_MAIN
("ext",
 GNUNET_SERVICE_OPTION_NONE,
 &run,
 &client_connect_cb,
 &client_disconnect_cb,
 NULL,
 GNUNET_MQ_hd_fixed_size (ext,
			  GNUNET_MESSAGE_TYPE_EXT,
			  struct GNUNET_MessageHeader,
			  NULL),
 GNUNET_MQ_handler_end ());

/* end of gnunet-service-ext.c */