-needed for nss plugin
[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 *shorten_name;
43
44 /**
45  * GNS name to lookup. (-u option)
46  */
47 static char *lookup_name;
48
49
50 /**
51  * record type to look up (-t option)
52  */
53 static char *lookup_type;
54
55 /**
56  * name to look up authority for (-a option)
57  */
58 static char *auth_name;
59
60 /**
61  * raw output
62  */
63 static int raw;
64
65 static enum GNUNET_GNS_RecordType rtype;
66
67 /**
68  * Task run on shutdown.  Cleans up everything.
69  *
70  * @param cls unused
71  * @param tc scheduler context
72  */
73 static void
74 do_shutdown (void *cls,
75              const struct GNUNET_SCHEDULER_TaskContext *tc)
76 {
77   if (NULL != gns)
78   {
79     GNUNET_GNS_disconnect (gns);
80     gns = NULL;
81   }
82 }
83
84
85 static void
86 process_shorten_result(void* cls, const char* nshort)
87 {
88   printf("%s shortened to %s\n", (char*) cls, nshort);
89   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
90 }
91
92 static void
93 process_lookup_result(void* cls, uint32_t rd_count,
94                       const struct GNUNET_NAMESTORE_RecordData *rd)
95 {
96   int i;
97   char* name = (char*) cls;
98   const char* typename;
99   char* string_val;
100   
101   if (!raw) {
102     if (rd_count == 0)
103       printf("No results.\n");
104     else
105       printf("%s:\n", name);
106   }
107
108
109
110   for (i=0; i<rd_count; i++)
111   {
112     typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
113     string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type,
114                                                   rd[i].data,
115                                                   rd[i].data_size);
116     if (raw)
117       printf("%s\n", string_val);
118     else
119       printf("Got %s record: %s\n", typename, string_val);
120
121   }
122
123   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
124 }
125
126 static void
127 process_auth_result(void* cls, const char* auth)
128 {
129   printf ("%s\n", auth);
130   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
131 }
132
133 /**
134  * Main function that will be run.
135  *
136  * @param cls closure
137  * @param args remaining command-line arguments
138  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
139  * @param cfg configuration
140  */
141 static void
142 run (void *cls, char *const *args, const char *cfgfile,
143      const struct GNUNET_CONFIGURATION_Handle *cfg)
144 {
145   gns = GNUNET_GNS_connect (cfg);
146   if (lookup_type != NULL)
147     rtype = GNUNET_NAMESTORE_typename_to_number(lookup_type);
148   else
149     rtype = GNUNET_GNS_RECORD_TYPE_A;
150
151   if (NULL == gns)
152   {
153     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154                 _("Failed to connect to GNS\n"));
155     return;
156   }
157   
158   if (shorten_name != NULL)
159   {
160     /** shorten name */
161     GNUNET_GNS_shorten(gns, shorten_name, &process_shorten_result,
162                        shorten_name);
163   }
164
165   if (lookup_name != NULL)
166   {
167     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168                 "Lookup\n");
169     GNUNET_GNS_lookup(gns, lookup_name, rtype,
170                       &process_lookup_result, lookup_name);
171   }
172
173   if (auth_name != NULL)
174   {
175     GNUNET_GNS_get_authority(gns, auth_name, &process_auth_result, auth_name);
176   }
177   
178   // FIXME: do work here...
179   //GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
180 }
181
182
183 /**
184  * The main function for gnunet-gns.
185  *
186  * @param argc number of arguments from the command line
187  * @param argv command line arguments
188  * @return 0 ok, 1 on error
189  */
190 int
191 main (int argc, char *const *argv)
192 {
193   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
194     {'s', "shorten", NULL,
195      gettext_noop ("try to shorten a given GNS name"), 1,
196      &GNUNET_GETOPT_set_string, &shorten_name},
197     {'u', "lookup", NULL,
198       gettext_noop ("Lookup a record using GNS (NOT IMPLEMENTED)"), 1,
199       &GNUNET_GETOPT_set_string, &lookup_name},
200     {'a', "authority", NULL,
201       gettext_noop ("Get the authority of a particular name"), 1,
202       &GNUNET_GETOPT_set_string, &auth_name},
203     {'t', "type", NULL,
204       gettext_noop ("Specify the type of the record lookup"), 1,
205       &GNUNET_GETOPT_set_string, &lookup_type},
206     {'r', "raw", NULL,
207       gettext_noop ("No unneeded output"), 0,
208       &GNUNET_GETOPT_set_one, &raw},
209     GNUNET_GETOPT_OPTION_END
210   };
211
212   int ret;
213
214   GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
215   ret =
216       (GNUNET_OK ==
217        GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
218                            _("GNUnet GNS access tool"), 
219                            options,
220                            &run, NULL)) ? 0 : 1;
221
222   return ret;
223 }
224
225 /* end of gnunet-gns.c */