GNUnet  0.11.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 
71 static void
73 
74 
82 static void
84  enum GNUNET_MQ_Error error)
85 {
86  struct GNUNET_REGEX_Announcement *a = cls;
87 
88  GNUNET_MQ_destroy (a->mq);
89  a->mq = NULL;
91 }
92 
93 
99 static void
101 {
102  struct GNUNET_MQ_Envelope *env;
103  struct AnnounceMessage *am;
104  size_t slen;
105 
106  a->mq = GNUNET_CLIENT_connect (a->cfg,
107  "regex",
108  NULL,
110  a);
111  if (NULL == a->mq)
112  return;
113  slen = strlen (a->regex) + 1;
114  env = GNUNET_MQ_msg_extra (am,
115  slen,
117  am->compression = htons (a->compression);
118  am->reserved = htons (0);
120  GNUNET_memcpy (&am[1],
121  a->regex,
122  slen);
123  GNUNET_MQ_send (a->mq,
124  env);
125 }
126 
127 
140  const char *regex,
142  uint16_t compression)
143 {
144  struct GNUNET_REGEX_Announcement *a;
145  size_t slen;
146 
147  slen = strlen (regex) + 1;
148  if (slen + sizeof(struct AnnounceMessage) >= GNUNET_MAX_MESSAGE_SIZE)
149  {
151  _ ("Regex `%s' is too long!\n"),
152  regex);
153  GNUNET_break (0);
154  return NULL;
155  }
157  a->cfg = cfg;
160  a->regex = GNUNET_strdup (regex);
161  announce_reconnect (a);
162  if (NULL == a->mq)
163  {
164  GNUNET_free (a->regex);
165  GNUNET_free (a);
166  return NULL;
167  }
168  return a;
169 }
170 
171 
177 void
179 {
180  GNUNET_MQ_destroy (a->mq);
181  GNUNET_free (a->regex);
182  GNUNET_free (a);
183 }
184 
185 
186 /* end of regex_api_announce.c */
uint16_t compression
How many characters can we squeeze per edge?
Definition: regex_ipc.h:44
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:1057
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_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#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:181
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
uint16_t reserved
Always zero.
Definition: regex_ipc.h:49
struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton(struct GNUNET_TIME_Relative a)
Convert relative time to network byte order.
Definition: time.c:625
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:54
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.