2 This file is part of GNUnet
3 Copyright (C) 2012, 2013, 2016 GNUnet e.V.
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.
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.
16 * @file regex/regex_api_announce.c
17 * @brief access regex service to advertise capabilities via regex
18 * @author Maximilian Szengel
19 * @author Christian Grothoff
22 #include "gnunet_protocols.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_regex_service.h"
25 #include "regex_ipc.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "regex-api",__VA_ARGS__)
30 * Handle to store cached data about a regex announce.
32 struct GNUNET_REGEX_Announcement
35 * Connection to the regex service.
37 struct GNUNET_MQ_Handle *mq;
42 const struct GNUNET_CONFIGURATION_Handle *cfg;
45 * Message we're sending to the service.
50 * Frequency of announcements.
52 struct GNUNET_TIME_Relative refresh_delay;
55 * Number of characters per edge.
63 * (Re)connect to the REGEX service with the given announcement @a a.
65 * @param a REGEX to announce.
68 announce_reconnect (struct GNUNET_REGEX_Announcement *a);
72 * We got a disconnect after asking regex to do the announcement.
75 * @param cls the `struct GNUNET_REGEX_Announcement` to retry
76 * @param error error code
79 announce_mq_error_handler (void *cls,
80 enum GNUNET_MQ_Error error)
82 struct GNUNET_REGEX_Announcement *a = cls;
84 GNUNET_MQ_destroy (a->mq);
86 announce_reconnect (a);
91 * (Re)connect to the REGEX service with the given announcement @a a.
93 * @param a REGEX to announce.
96 announce_reconnect (struct GNUNET_REGEX_Announcement *a)
98 struct GNUNET_MQ_Envelope *env;
99 struct AnnounceMessage *am;
102 a->mq = GNUNET_CLIENT_connect (a->cfg,
105 &announce_mq_error_handler,
109 slen = strlen (a->regex) + 1;
110 env = GNUNET_MQ_msg_extra (am,
112 GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE);
113 am->compression = htons (a->compression);
114 am->reserved = htons (0);
115 am->refresh_delay = GNUNET_TIME_relative_hton (a->refresh_delay);
116 GNUNET_memcpy (&am[1],
119 GNUNET_MQ_send (a->mq,
125 * Announce the given peer under the given regular expression.
127 * @param cfg configuration to use
128 * @param regex Regular expression to announce.
129 * @param refresh_delay after what delay should the announcement be repeated?
130 * @param compression How many characters per edge can we squeeze?
131 * @return Handle to reuse o free cached resources.
132 * Must be freed by calling #GNUNET_REGEX_announce_cancel().
134 struct GNUNET_REGEX_Announcement *
135 GNUNET_REGEX_announce (const struct GNUNET_CONFIGURATION_Handle *cfg,
137 struct GNUNET_TIME_Relative refresh_delay,
138 uint16_t compression)
140 struct GNUNET_REGEX_Announcement *a;
143 slen = strlen (regex) + 1;
144 if (slen + sizeof (struct AnnounceMessage) >= GNUNET_MAX_MESSAGE_SIZE)
146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
147 _("Regex `%s' is too long!\n"),
152 a = GNUNET_new (struct GNUNET_REGEX_Announcement);
154 a->refresh_delay = refresh_delay;
155 a->compression = compression;
156 a->regex = GNUNET_strdup (regex);
157 announce_reconnect (a);
160 GNUNET_free (a->regex);
169 * Stop announcing the regex specified by the given handle.
171 * @param a handle returned by a previous #GNUNET_REGEX_announce() call.
174 GNUNET_REGEX_announce_cancel (struct GNUNET_REGEX_Announcement *a)
176 GNUNET_MQ_destroy (a->mq);
177 GNUNET_free (a->regex);
181 /* end of regex_api_announce.c */