GNUnet  0.10.x
gnunet-search.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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  */
28 #include "platform.h"
29 #include "gnunet_fs_service.h"
30 
31 static int ret;
32 
33 static const struct GNUNET_CONFIGURATION_Handle *cfg;
34 
35 static struct GNUNET_FS_Handle *ctx;
36 
37 static struct GNUNET_FS_SearchContext *sc;
38 
39 static char *output_filename;
40 
42 
43 static unsigned int anonymity = 1;
44 
49 
50 static unsigned int results_limit;
51 
52 static unsigned int results;
53 
54 static unsigned int verbose;
55 
56 static int local_only;
57 
58 static struct GNUNET_SCHEDULER_Task *tt;
59 
60 
78 static int
79 item_printer(void *cls,
80  const char *plugin_name,
82  enum EXTRACTOR_MetaFormat format,
83  const char *data_mime_type,
84  const char *data,
85  size_t data_size)
86 {
87  if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
89  return 0;
91  return 0;
92 #if HAVE_LIBEXTRACTOR
93  printf("\t%20s: %s\n",
95  EXTRACTOR_metatype_to_string(type)),
96  data);
97 #else
98  printf("\t%20d: %s\n", type, data);
99 #endif
100  return 0;
101 }
102 
103 
104 static void
105 clean_task(void *cls)
106 {
107  size_t dsize;
108  void *ddata;
109 
110  GNUNET_FS_stop(ctx);
111  ctx = NULL;
112  if (output_filename == NULL)
113  return;
114  if (GNUNET_OK != GNUNET_FS_directory_builder_finish(db, &dsize, &ddata))
115  {
116  GNUNET_break(0);
118  return;
119  }
121  ddata,
122  dsize,
125  {
126  fprintf(stderr,
127  _("Failed to write directory with search results to `%s'\n"),
129  }
130  GNUNET_free_non_null(ddata);
132 }
133 
134 
148 static void *
149 progress_cb(void *cls, const struct GNUNET_FS_ProgressInfo *info)
150 {
151  static unsigned int cnt;
152  int is_directory;
153  char *uri;
154  char *filename;
155 
156  switch (info->status)
157  {
159  break;
160 
162  if (db != NULL)
164  info->value.search.specifics.result.uri,
165  info->value.search.specifics.result.meta,
166  NULL);
167  uri = GNUNET_FS_uri_to_string(info->value.search.specifics.result.uri);
168  printf("#%u:\n", ++cnt);
170  info->value.search.specifics.result.meta,
173  info->value.search.specifics.result.meta);
174  if (NULL != filename)
175  {
176  while ((filename[0] != '\0') && ('/' == filename[strlen(filename) - 1]))
177  filename[strlen(filename) - 1] = '\0';
179  if (GNUNET_YES == is_directory)
180  printf("gnunet-download -o \"%s%s\" -R %s\n",
181  filename,
183  uri);
184  else
185  printf("gnunet-download -o \"%s\" %s\n", filename, uri);
186  }
187  else if (GNUNET_YES == is_directory)
188  printf("gnunet-download -o \"collection%s\" -R %s\n",
190  uri);
191  else
192  printf("gnunet-download %s\n", uri);
193  if (verbose)
194  GNUNET_CONTAINER_meta_data_iterate(info->value.search.specifics.result
195  .meta,
196  &item_printer,
197  NULL);
198  printf("\n");
199  fflush(stdout);
200  GNUNET_free_non_null(filename);
201  GNUNET_free(uri);
202  results++;
203  if ((results_limit > 0) && (results >= results_limit))
205  break;
206 
208  break;
209 
211  /* ignore */
212  break;
213 
215  fprintf(stderr,
216  _("Error searching: %s.\n"),
217  info->value.search.specifics.error.message);
219  break;
220 
223  break;
224 
225  default:
226  fprintf(stderr, _("Unexpected status: %d\n"), info->status);
227  break;
228  }
229  return NULL;
230 }
231 
232 
233 static void
234 shutdown_task(void *cls)
235 {
236  if (sc != NULL)
237  {
239  sc = NULL;
240  }
241 }
242 
243 
244 static void
245 timeout_task(void *cls)
246 {
247  tt = NULL;
249 }
250 
251 
260 static void
261 run(void *cls,
262  char *const *args,
263  const char *cfgfile,
264  const struct GNUNET_CONFIGURATION_Handle *c)
265 {
266  struct GNUNET_FS_Uri *uri;
267  unsigned int argc;
269 
270  argc = 0;
271  while (NULL != args[argc])
272  argc++;
273  uri = GNUNET_FS_uri_ksk_create_from_args(argc, (const char **)args);
274  if (NULL == uri)
275  {
276  fprintf(stderr,
277  "%s",
278  _("Could not create keyword URI from arguments.\n"));
279  ret = 1;
280  return;
281  }
282  cfg = c;
283  ctx = GNUNET_FS_start(cfg,
284  "gnunet-search",
285  &progress_cb,
286  NULL,
289  if (NULL == ctx)
290  {
291  fprintf(stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
293  ret = 1;
294  return;
295  }
296  if (output_filename != NULL)
299  if (local_only)
301  sc = GNUNET_FS_search_start(ctx, uri, anonymity, options, NULL);
303  if (NULL == sc)
304  {
305  fprintf(stderr, "%s", _("Could not start searching.\n"));
306  GNUNET_FS_stop(ctx);
307  ret = 1;
308  return;
309  }
310  if (0 != timeout.rel_value_us)
313 }
314 
315 
323 int
324 main(int argc, char *const *argv)
325 {
326  struct GNUNET_GETOPT_CommandLineOption options[] =
328  "anonymity",
329  "LEVEL",
330  gettext_noop(
331  "set the desired LEVEL of receiver-anonymity"),
332  &anonymity),
334  'n',
335  "no-network",
336  gettext_noop("only search the local peer (no P2P network search)"),
337  &local_only),
339  'o',
340  "output",
341  "PREFIX",
342  gettext_noop("write search results to file starting with PREFIX"),
343  &output_filename),
345  't',
346  "timeout",
347  "DELAY",
348  gettext_noop("automatically terminate search after DELAY"),
349  &timeout),
352  "results",
353  "VALUE",
354  gettext_noop("automatically terminate search "
355  "after VALUE results are found"),
356  &results_limit),
358 
359  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
360  return 2;
361 
362  ret =
363  (GNUNET_OK ==
364  GNUNET_PROGRAM_run(argc,
365  argv,
366  "gnunet-search [OPTIONS] KEYWORD",
367  gettext_noop(
368  "Search GNUnet for files that were published on GNUnet"),
369  options,
370  &run,
371  NULL))
372  ? ret
373  : 1;
374  GNUNET_free((void *)argv);
375  return ret;
376 }
377 
378 /* end of gnunet-search.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time)...
int GNUNET_FS_directory_builder_finish(struct GNUNET_FS_DirectoryBuilder *bld, size_t *rsize, void **rdata)
Finish building the directory.
Definition: fs_directory.c:570
No options (use defaults for everything).
EXTRACTOR_MetaFormat
Format in which the extracted meta data is presented.
struct GNUNET_FS_ProgressInfo::@28::@31 search
Values for all "GNUNET_FS_STATUS_SEARCH_*" events.
uint64_t rel_value_us
The actual value.
This search has yielded a result.
Last message from a search; this signals that there will be no further events associated with this se...
struct GNUNET_FS_Uri * GNUNET_FS_uri_ksk_create_from_args(unsigned int argc, const char **argv)
Create an FS URI from a user-supplied command line of keywords.
Definition: fs_uri.c:1152
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
static void clean_task(void *cls)
Event generated for each search result when the respective search is stopped.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
Master context for most FS operations.
Definition: fs_api.h:1056
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose(unsigned int *level)
Define the &#39;-V&#39; verbosity option.
static void shutdown_task(void *cls)
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1439
GNUNET_FS_SearchOptions
Options for searching.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run by the scheduler.
Last option in the VARARG list.
static struct GNUNET_SCHEDULER_Task * tt
Definition: gnunet-search.c:58
First event generated when a client requests a search to begin or when a namespace result automatical...
Internal state of a directory builder.
Definition: fs_directory.c:334
0-terminated, UTF-8 encoded string.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
Definition of a command line option.
char * GNUNET_CONTAINER_meta_data_get_by_type(const struct GNUNET_CONTAINER_MetaData *md, enum EXTRACTOR_MetaType type)
Get the first MD entry of the given type.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:517
EXTRACTOR_MetaType
Enumeration defining various sources of keywords.
void GNUNET_FS_stop(struct GNUNET_FS_Handle *h)
Close our connection with the file-sharing service.
Definition: fs_api.c:3215
ssize_t GNUNET_DISK_fn_write(const char *fn, const void *buffer, size_t n, enum GNUNET_DISK_AccessPermissions mode)
Write a buffer to a file.
Definition: disk.c:877
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static unsigned int verbose
Definition: gnunet-search.c:54
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
static int ret
Definition: gnunet-search.c:31
struct GNUNET_FS_Handle * GNUNET_FS_start(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *client_name, GNUNET_FS_ProgressCallback upcb, void *upcb_cls, enum GNUNET_FS_Flags flags,...)
Setup a connection to the file-sharing service.
Definition: fs_api.c:3140
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
No special flags set.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
char * GNUNET_FS_uri_to_string(const struct GNUNET_FS_Uri *uri)
Convert a URI to a UTF-8 String.
Definition: fs_uri.c:2024
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
void GNUNET_FS_directory_builder_add(struct GNUNET_FS_DirectoryBuilder *bld, const struct GNUNET_FS_Uri *uri, const struct GNUNET_CONTAINER_MetaData *md, const void *data)
Add an entry to a directory.
Definition: fs_directory.c:384
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1237
union GNUNET_FS_ProgressInfo::@28 value
Values that depend on the event type.
static void * progress_cb(void *cls, const struct GNUNET_FS_ProgressInfo *info)
Called by FS client to give information about the progress of an operation.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Definition: gnunet-search.c:33
#define GNUNET_FS_DIRECTORY_EXT
#define dgettext(Domainname, Msgid)
Definition: gettext.h:46
static int item_printer(void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, const char *data_mime_type, const char *data, size_t data_size)
Type of a function that libextractor calls for each meta data item found.
Definition: gnunet-search.c:79
int main(int argc, char *const *argv)
The main function to search GNUnet.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
enum GNUNET_FS_Status status
Specific status code (determines the event type).
struct GNUNET_FS_SearchContext * GNUNET_FS_search_start(struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri, uint32_t anonymity, enum GNUNET_FS_SearchOptions options, void *cctx)
Start search for content.
Definition: fs_search.c:1611
static struct GNUNET_TIME_Relative timeout
Timeout for the search, 0 means to wait for CTRL-C.
Definition: gnunet-search.c:48
static char * filename
static char * output_filename
Definition: gnunet-search.c:39
static int local_only
Definition: gnunet-search.c:56
static unsigned int anonymity
Definition: gnunet-search.c:43
static char * plugin_name
Solver plugin name as string.
static void timeout_task(void *cls)
void GNUNET_FS_uri_destroy(struct GNUNET_FS_Uri *uri)
Free URI.
Definition: fs_uri.c:675
static struct GNUNET_FS_SearchContext * sc
Definition: gnunet-search.c:37
int GNUNET_FS_meta_data_test_for_directory(const struct GNUNET_CONTAINER_MetaData *md)
Does the meta-data claim that this is a directory? Checks if the mime-type is that of a GNUnet direct...
Definition: fs_directory.c:54
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:41
static struct GNUNET_FS_Handle * ctx
Definition: gnunet-search.c:35
configuration data
Definition: configuration.c:83
#define LIBEXTRACTOR_GETTEXT_DOMAIN
Definition: platform.h:182
A Universal Resource Identifier (URI), opaque.
Definition: fs_api.h:162
Handle for controlling a search.
Definition: fs_api.h:1492
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
int GNUNET_CONTAINER_meta_data_iterate(const struct GNUNET_CONTAINER_MetaData *md, EXTRACTOR_MetaDataProcessor iter, void *iter_cls)
Iterate over MD entries.
static unsigned int results
Definition: gnunet-search.c:52
Entry in list of pending tasks.
Definition: scheduler.c:131
Only search the local host, do not search remote systems (no P2P)
Signals a problem with this search.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:367
We have additional data about the quality or availability of a search result.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
#define GNUNET_YES
Definition: gnunet_common.h:77
static unsigned int results_limit
Definition: gnunet-search.c:50
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
uint32_t data
The data value.
struct GNUNET_FS_DirectoryBuilder * GNUNET_FS_directory_builder_create(const struct GNUNET_CONTAINER_MetaData *mdir)
Create a directory builder.
Definition: fs_directory.c:358
static size_t data_size
Number of bytes in data.
void GNUNET_FS_search_stop(struct GNUNET_FS_SearchContext *sc)
Stop search for content.
Definition: fs_search.c:1770
void GNUNET_DISK_filename_canonicalize(char *fn)
Removes special characters as &#39;:&#39; from a filename.
Definition: disk.c:1137
#define GNUNET_free(ptr)
Wrapper around free.
Argument given to the progress callback with information about what is going on.
Time for relative time used by GNUnet, in microseconds.
#define gettext_noop(String)
Definition: gettext.h:69