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