GNUnet  0.10.x
regex_api_search.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2012, 2013, 2016 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
27 #include "platform.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_regex_service.h"
31 #include "regex_ipc.h"
32 
33 #define LOG(kind,...) GNUNET_log_from (kind, "regex-api",__VA_ARGS__)
34 
35 
40 {
45 
50 
55 
59  void *callback_cls;
60 
64  char *string;
65 };
66 
67 
73 static void
75 
76 
85 static int
87  const struct ResultMessage *result)
88 {
89  uint16_t size = ntohs (result->header.size) - sizeof (*result);
90  uint16_t gpl = ntohs (result->get_path_length);
91  uint16_t ppl = ntohs (result->put_path_length);
92 
93  if (size != (gpl + ppl) * sizeof (struct GNUNET_PeerIdentity))
94  {
95  GNUNET_break (0);
96  return GNUNET_SYSERR;
97  }
98  return GNUNET_OK;
99 }
100 
101 
109 static void
111  const struct ResultMessage *result)
112 {
113  struct GNUNET_REGEX_Search *s = cls;
114  uint16_t gpl = ntohs (result->get_path_length);
115  uint16_t ppl = ntohs (result->put_path_length);
116  const struct GNUNET_PeerIdentity *pid;
117 
118  pid = &result->id;
120  "Got regex result %s\n",
121  GNUNET_i2s (pid));
122  s->callback (s->callback_cls,
123  pid,
124  &pid[1],
125  gpl,
126  &pid[1 + gpl],
127  ppl);
128 }
129 
130 
138 static void
139 mq_error_handler (void *cls,
140  enum GNUNET_MQ_Error error)
141 {
142  struct GNUNET_REGEX_Search *s = cls;
143 
144  GNUNET_MQ_destroy (s->mq);
145  s->mq = NULL;
146  search_reconnect (s);
147 }
148 
149 
155 static void
157 {
158  struct GNUNET_MQ_MessageHandler handlers[] = {
159  GNUNET_MQ_hd_var_size (search_response,
161  struct ResultMessage,
162  s),
164  };
165  size_t slen = strlen (s->string) + 1;
166  struct GNUNET_MQ_Envelope *env;
167  struct RegexSearchMessage *rsm;
168 
169  GNUNET_assert (NULL == s->mq);
170  s->mq = GNUNET_CLIENT_connect (s->cfg,
171  "regex",
172  handlers,
174  s);
175  if (NULL == s->mq)
176  return;
177  env = GNUNET_MQ_msg_extra (rsm,
178  slen,
180  GNUNET_memcpy (&rsm[1],
181  s->string,
182  slen);
183  GNUNET_MQ_send (s->mq,
184  env);
185 }
186 
187 
200 struct GNUNET_REGEX_Search *
202  const char *string,
204  void *callback_cls)
205 {
206  struct GNUNET_REGEX_Search *s;
207  size_t slen = strlen (string) + 1;
208 
209  if (slen + sizeof (struct RegexSearchMessage) >= GNUNET_MAX_MESSAGE_SIZE)
210  {
212  _("Search string `%s' is too long!\n"),
213  string);
214  GNUNET_break (0);
215  return NULL;
216  }
218  "Starting regex search for %s\n",
219  string);
220  s = GNUNET_new (struct GNUNET_REGEX_Search);
221  s->cfg = cfg;
222  s->string = GNUNET_strdup (string);
223  s->callback = callback;
225  search_reconnect (s);
226  if (NULL == s->mq)
227  {
228  GNUNET_free (s->string);
229  GNUNET_free (s);
230  return NULL;
231  }
232  return s;
233 }
234 
235 
241 void
243 {
244  GNUNET_MQ_destroy (s->mq);
245  GNUNET_free (s->string);
246  GNUNET_free (s);
247 }
248 
249 
250 /* end of regex_api_search.c */
char * string
Search string to transmit to the service.
static void search_reconnect(struct GNUNET_REGEX_Search *s)
(Re)connect to the REGEX service for the given search s.
Message to initiate regex search.
Definition: regex_ipc.h:64
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:901
#define GNUNET_MESSAGE_TYPE_REGEX_SEARCH
Search for peer with matching capability.
Result from regex search.
Definition: regex_ipc.h:79
GNUNET_MQ_Error
Error codes for the queue.
#define LOG(kind,...)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_MESSAGE_TYPE_REGEX_RESULT
Result in response to regex search.
struct GNUNET_REGEX_Search * GNUNET_REGEX_search(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *string, GNUNET_REGEX_Found callback, void *callback_cls)
Search for a peer offering a regex matching certain string in the DHT.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
struct GNUNET_MQ_Handle * mq
Connection to the regex service.
regex IPC messages (not called &#39;regex.h&#39; due to conflict with system headers)
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
uint16_t put_path_length
Number of entries in the PUT path.
Definition: regex_ipc.h:94
GNUNET_REGEX_Found callback
Function to call with results.
#define GNUNET_memcpy(dst, src, n)
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
void * callback_cls
Closure for callback.
static int result
Global testing status.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
void GNUNET_REGEX_search_cancel(struct GNUNET_REGEX_Search *s)
Stop search and free all data used by a GNUNET_REGEX_search() call.
static int check_search_response(void *cls, const struct ResultMessage *result)
We got a response or disconnect after asking regex to do the search.
Message handler for a specific message type.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_REGEX_RESULT.
Definition: regex_ipc.h:84
void(* GNUNET_REGEX_Found)(void *cls, const struct GNUNET_PeerIdentity *id, const struct GNUNET_PeerIdentity *get_path, unsigned int get_path_length, const struct GNUNET_PeerIdentity *put_path, unsigned int put_path_length)
Search callback function, invoked for every result that was found.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
We got a disconnect after asking regex to do the announcement.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
struct GNUNET_PeerIdentity id
Identity of the peer that was found.
Definition: regex_ipc.h:99
Handle to a message queue.
Definition: mq.c:85
The identity of the host (wraps the signing key of the peer).
static void handle_search_response(void *cls, const struct ResultMessage *result)
We got a response or disconnect after asking regex to do the search.
configuration data
Definition: configuration.c:85
uint16_t get_path_length
Number of entries in the GET path.
Definition: regex_ipc.h:89
#define GNUNET_log(kind,...)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:353
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
Handle to store data about a regex search.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.