2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @brief command line tool to access distributed GNS
23 * @author Christian Grothoff
29 #include <gnunet_util_lib.h>
30 #include <gnunet_dnsparser_lib.h>
31 #include <gnunet_namestore_service.h>
32 #include <gnunet_gns_service.h>
35 * Handle to GNS service.
37 static struct GNUNET_GNS_Handle *gns;
40 * GNS name to shorten. (-s option)
42 static char *shorten_name;
45 * GNS name to lookup. (-u option)
47 static char *lookup_name;
51 * record type to look up (-t option)
53 static char *lookup_type;
56 * name to look up authority for (-a option)
58 static char *auth_name;
60 static enum GNUNET_GNS_RecordType rtype;
63 * Task run on shutdown. Cleans up everything.
66 * @param tc scheduler context
69 do_shutdown (void *cls,
70 const struct GNUNET_SCHEDULER_TaskContext *tc)
74 GNUNET_GNS_disconnect (gns);
81 process_shorten_result(void* cls, const char* nshort)
83 printf("%s shortened to %s\n", (char*) cls, nshort);
84 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
88 process_lookup_result(void* cls, uint32_t rd_count,
89 const struct GNUNET_NAMESTORE_RecordData *rd)
93 char* name = (char*) cls;
96 printf("No results.\n");
98 for (i=0; i<rd_count; i++)
100 if (rd[i].record_type != rtype)
102 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
104 addr = inet_ntoa(*((struct in_addr*)rd[i].data));
105 printf("Got A record for %s: %s\n", name, addr);
107 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
109 printf("Got MX record for %s: %s\n", name, (char*)rd[i].data);
112 //FIXME others? maybe to string method for records?
115 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
119 process_auth_result(void* cls, const char* auth)
121 printf ("%s\n", auth);
122 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
126 * Main function that will be run.
129 * @param args remaining command-line arguments
130 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
131 * @param cfg configuration
134 run (void *cls, char *const *args, const char *cfgfile,
135 const struct GNUNET_CONFIGURATION_Handle *cfg)
137 gns = GNUNET_GNS_connect (cfg);
138 rtype = GNUNET_GNS_RECORD_TYPE_A;
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
143 _("Failed to connect to GNS\n"));
147 if (shorten_name != NULL)
150 GNUNET_GNS_shorten(gns, shorten_name, &process_shorten_result,
154 if (lookup_name != NULL)
156 GNUNET_GNS_lookup(gns, lookup_name, rtype,
157 &process_lookup_result, lookup_name);
160 if (auth_name != NULL)
162 GNUNET_GNS_get_authority(gns, auth_name, &process_auth_result, auth_name);
165 // FIXME: do work here...
166 //GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
171 * The main function for gnunet-gns.
173 * @param argc number of arguments from the command line
174 * @param argv command line arguments
175 * @return 0 ok, 1 on error
178 main (int argc, char *const *argv)
180 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
181 {'s', "shorten", NULL,
182 gettext_noop ("try to shorten a given GNS name"), 1,
183 &GNUNET_GETOPT_set_string, &shorten_name},
184 {'u', "lookup", NULL,
185 gettext_noop ("Lookup a record using GNS (NOT IMPLEMENTED)"), 1,
186 &GNUNET_GETOPT_set_string, &lookup_name},
187 {'a', "authority", NULL,
188 gettext_noop ("Get the authority of a particular name"), 1,
189 &GNUNET_GETOPT_set_string, &auth_name},
191 gettext_noop ("Specify the type of the record lookup"), 1,
192 &GNUNET_GETOPT_set_string, &lookup_type},
193 GNUNET_GETOPT_OPTION_END
198 GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
201 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
202 _("GNUnet GNS access tool"),
204 &run, NULL)) ? 0 : 1;
209 /* end of gnunet-gns.c */