- adding assertion for name
[oweals/gnunet.git] / src / gns / gnunet-gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file gnunet-gns.c
22  * @brief command line tool to access distributed GNS
23  * @author Christian Grothoff
24  *
25  * TODO:
26  * - everything
27  */
28 #include "platform.h"
29 #include <gnunet_util_lib.h>
30 #include <gnunet_dnsparser_lib.h>
31 #include <gnunet_namestore_service.h>
32 #include <gnunet_gns_service.h>
33
34 /**
35  * Handle to GNS service.
36  */
37 static struct GNUNET_GNS_Handle *gns;
38
39 /**
40  * GNS name to shorten. (-s option)
41  */
42 static char *name;
43
44 /**
45  * Task run on shutdown.  Cleans up everything.
46  *
47  * @param cls unused
48  * @param tc scheduler context
49  */
50 static void
51 do_shutdown (void *cls,
52              const struct GNUNET_SCHEDULER_TaskContext *tc)
53 {
54   if (NULL != gns)
55   {
56     GNUNET_GNS_disconnect (gns);
57     gns = NULL;
58   }
59 }
60
61
62 static void
63 process_shorten_result(void* cls, const char* nshort)
64 {
65   printf("%s shortened to %s\n", (char*) cls, nshort);
66 }
67
68 /**
69  * Main function that will be run.
70  *
71  * @param cls closure
72  * @param args remaining command-line arguments
73  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
74  * @param cfg configuration
75  */
76 static void
77 run (void *cls, char *const *args, const char *cfgfile,
78      const struct GNUNET_CONFIGURATION_Handle *cfg)
79 {
80   gns = GNUNET_GNS_connect (cfg);
81   if (NULL == gns)
82   {
83     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
84                 _("Failed to connect to GNS\n"));
85     return;
86   }
87   
88   if (name != NULL)
89   {
90     /** shorten name */
91     GNUNET_GNS_shorten(gns, name, &process_shorten_result, name);
92   }
93
94   // FIXME: do work here...
95   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
96 }
97
98
99 /**
100  * The main function for gnunet-gns.
101  *
102  * @param argc number of arguments from the command line
103  * @param argv command line arguments
104  * @return 0 ok, 1 on error
105  */
106 int
107 main (int argc, char *const *argv)
108 {
109   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
110     {'s', "shorten", NULL,
111      gettext_noop ("try to shorten a given GNS name"), 1,
112      &GNUNET_GETOPT_set_string, &name},   
113     GNUNET_GETOPT_OPTION_END
114   };
115
116   int ret;
117
118   GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
119   ret =
120       (GNUNET_OK ==
121        GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
122                            _("GNUnet GNS access tool"), 
123                            options,
124                            &run, NULL)) ? 0 : 1;
125
126   return ret;
127 }
128
129 /* end of gnunet-gns.c */