GNUnet  0.10.x
regex_api_announce.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 */
26 #include "platform.h"
27 #include "gnunet_protocols.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_regex_service.h"
30 #include "regex_ipc.h"
31 
32 #define LOG(kind,...) GNUNET_log_from (kind, "regex-api",__VA_ARGS__)
33 
38 {
43 
48 
52  char *regex;
53 
58 
62  uint16_t compression;
63 };
64 
65 
66 
72 static void
74 
75 
83 static void
85  enum GNUNET_MQ_Error error)
86 {
87  struct GNUNET_REGEX_Announcement *a = cls;
88 
89  GNUNET_MQ_destroy (a->mq);
90  a->mq = NULL;
92 }
93 
94 
100 static void
102 {
103  struct GNUNET_MQ_Envelope *env;
104  struct AnnounceMessage *am;
105  size_t slen;
106 
107  a->mq = GNUNET_CLIENT_connect (a->cfg,
108  "regex",
109  NULL,
111  a);
112  if (NULL == a->mq)
113  return;
114  slen = strlen (a->regex) + 1;
115  env = GNUNET_MQ_msg_extra (am,
116  slen,
118  am->compression = htons (a->compression);
119  am->reserved = htons (0);
121  GNUNET_memcpy (&am[1],
122  a->regex,
123  slen);
124  GNUNET_MQ_send (a->mq,
125  env);
126 }
127 
128 
141  const char *regex,
143  uint16_t compression)
144 {
145  struct GNUNET_REGEX_Announcement *a;
146  size_t slen;
147 
148  slen = strlen (regex) + 1;
149  if (slen + sizeof (struct AnnounceMessage) >= GNUNET_MAX_MESSAGE_SIZE)
150  {
152  _("Regex `%s' is too long!\n"),
153  regex);
154  GNUNET_break (0);
155  return NULL;
156  }
158  a->cfg = cfg;
161  a->regex = GNUNET_strdup (regex);
162  announce_reconnect (a);
163  if (NULL == a->mq)
164  {
165  GNUNET_free (a->regex);
166  GNUNET_free (a);
167  return NULL;
168  }
169  return a;
170 }
171 
172 
178 void
180 {
181  GNUNET_MQ_destroy (a->mq);
182  GNUNET_free (a->regex);
183  GNUNET_free (a);
184 }
185 
186 /* end of regex_api_announce.c */
uint16_t compression
How many characters can we squeeze per edge?
Definition: regex_ipc.h:45
static void announce_reconnect(struct GNUNET_REGEX_Announcement *a)
(Re)connect to the REGEX service with the given announcement a.
void GNUNET_REGEX_announce_cancel(struct GNUNET_REGEX_Announcement *a)
Stop announcing the regex specified by the given handle.
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
static void announce_mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
We got a disconnect after asking regex to do the announcement.
GNUNET_MQ_Error
Error codes for the queue.
Handle to store cached data about a regex announce.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t compression
Number of characters per edge.
#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 _(String)
GNU gettext support macro.
Definition: platform.h:208
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
Request for regex service to announce capability.
Definition: regex_ipc.h:34
#define GNUNET_memcpy(dst, src, n)
uint16_t reserved
Always zero.
Definition: regex_ipc.h:50
struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton(struct GNUNET_TIME_Relative a)
Convert relative time to network byte order.
Definition: time.c:622
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
char * regex
Message we&#39;re sending to the service.
Handle to a message queue.
Definition: mq.c:85
struct GNUNET_REGEX_Announcement * GNUNET_REGEX_announce(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *regex, struct GNUNET_TIME_Relative refresh_delay, uint16_t compression)
Announce the given peer under the given regular expression.
#define GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE
Advertise regex capability.
configuration data
Definition: configuration.c:85
#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
struct GNUNET_TIME_RelativeNBO refresh_delay
Delay between repeated announcements.
Definition: regex_ipc.h:55
struct GNUNET_MQ_Handle * mq
Connection to the regex service.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
struct GNUNET_TIME_Relative refresh_delay
Frequency of announcements.