diff options
author | Nathan S. Evans <evans@in.tum.de> | 2010-04-05 13:52:41 +0000 |
---|---|---|
committer | Nathan S. Evans <evans@in.tum.de> | 2010-04-05 13:52:41 +0000 |
commit | e4de5bb96aa6560bbd85ebca896e7cb36d426ad5 (patch) | |
tree | 658263e857a93978b669cc90438ed1c5521e188d /src/dht/gnunet-dht-put.c | |
parent | 2b5542569ff904ad595b40aa150823bfbd7bf39b (diff) | |
download | gnunet-e4de5bb96aa6560bbd85ebca896e7cb36d426ad5.tar.gz gnunet-e4de5bb96aa6560bbd85ebca896e7cb36d426ad5.zip |
add gnunet-dht-get and gnunet-dht-put binaries, changes to service
Diffstat (limited to 'src/dht/gnunet-dht-put.c')
-rw-r--r-- | src/dht/gnunet-dht-put.c | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/src/dht/gnunet-dht-put.c b/src/dht/gnunet-dht-put.c new file mode 100644 index 000000000..4d25847fb --- /dev/null +++ b/src/dht/gnunet-dht-put.c | |||
@@ -0,0 +1,217 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 2, 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 dht/gnunet-dht-put.c | ||
22 | * @brief search for data in DHT | ||
23 | * @author Christian Grothoff | ||
24 | * @author Nathan Evans | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_dht_service.h" | ||
28 | |||
29 | /** | ||
30 | * The type of the query | ||
31 | */ | ||
32 | static unsigned int query_type; | ||
33 | |||
34 | /** | ||
35 | * The key for the query | ||
36 | */ | ||
37 | static char *query_key; | ||
38 | |||
39 | /** | ||
40 | * User supplied timeout value | ||
41 | */ | ||
42 | static unsigned long long timeout_request = 5; | ||
43 | |||
44 | /** | ||
45 | * User supplied expiration value | ||
46 | */ | ||
47 | static unsigned long long expiration_seconds = 3600; | ||
48 | |||
49 | /** | ||
50 | * Be verbose | ||
51 | */ | ||
52 | static int verbose; | ||
53 | |||
54 | /** | ||
55 | * Handle to the DHT | ||
56 | */ | ||
57 | static struct GNUNET_DHT_Handle *dht_handle; | ||
58 | |||
59 | /** | ||
60 | * Global handle of the scheduler | ||
61 | */ | ||
62 | static struct GNUNET_SCHEDULER_Handle *sched; | ||
63 | |||
64 | /** | ||
65 | * Global handle of the configuration | ||
66 | */ | ||
67 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
68 | |||
69 | /** | ||
70 | * Global status value | ||
71 | */ | ||
72 | static int ret; | ||
73 | |||
74 | /** | ||
75 | * The data to insert into the dht | ||
76 | */ | ||
77 | static char *data; | ||
78 | |||
79 | static void | ||
80 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
81 | { | ||
82 | |||
83 | if (dht_handle != NULL) | ||
84 | GNUNET_DHT_disconnect (dht_handle); | ||
85 | |||
86 | dht_handle = NULL; | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Signature of the main function of a task. | ||
91 | * | ||
92 | * @param cls closure | ||
93 | * @param tc context information (why was this task triggered now) | ||
94 | */ | ||
95 | void | ||
96 | message_sent_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
97 | { | ||
98 | if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT) | ||
99 | { | ||
100 | if (verbose) | ||
101 | fprintf (stderr, | ||
102 | "Failed to send put request to service, quitting.\n"); | ||
103 | ret = 1; | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | if (verbose) | ||
108 | fprintf (stderr, "PUT request sent!\n"); | ||
109 | } | ||
110 | |||
111 | GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL); | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * Main function that will be run by the scheduler. | ||
116 | * | ||
117 | * @param cls closure | ||
118 | * @param s the scheduler to use | ||
119 | * @param args remaining command-line arguments | ||
120 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
121 | * @param c configuration | ||
122 | */ | ||
123 | static void | ||
124 | run (void *cls, | ||
125 | struct GNUNET_SCHEDULER_Handle *s, | ||
126 | char *const *args, | ||
127 | const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c) | ||
128 | { | ||
129 | struct GNUNET_TIME_Relative timeout; | ||
130 | struct GNUNET_TIME_Absolute expiration; | ||
131 | GNUNET_HashCode key; | ||
132 | sched = s; | ||
133 | cfg = c; | ||
134 | |||
135 | if ((query_key == NULL) || (data == NULL)) | ||
136 | { | ||
137 | if (verbose) | ||
138 | fprintf (stderr, "Must provide KEY and DATA for DHT put!\n"); | ||
139 | ret = 1; | ||
140 | return; | ||
141 | } | ||
142 | |||
143 | dht_handle = GNUNET_DHT_connect (sched, cfg, 1); | ||
144 | |||
145 | if (dht_handle == NULL) | ||
146 | { | ||
147 | if (verbose) | ||
148 | fprintf (stderr, "Couldn't connect to DHT service!\n"); | ||
149 | ret = 1; | ||
150 | return; | ||
151 | } | ||
152 | else if (verbose) | ||
153 | fprintf (stderr, "Connected to DHT service!\n"); | ||
154 | |||
155 | GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key); | ||
156 | |||
157 | timeout = | ||
158 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request); | ||
159 | expiration = | ||
160 | GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply | ||
161 | (GNUNET_TIME_UNIT_SECONDS, | ||
162 | expiration_seconds)); | ||
163 | |||
164 | if (verbose) | ||
165 | fprintf (stderr, "Issuing put request for `%s' with data `%s'!\n", | ||
166 | query_key, data); | ||
167 | |||
168 | GNUNET_DHT_put (dht_handle, &key, query_type, strlen (data), data, | ||
169 | expiration, timeout, &message_sent_cont, NULL); | ||
170 | |||
171 | } | ||
172 | |||
173 | |||
174 | /** | ||
175 | * gnunet-dht-put command line options | ||
176 | */ | ||
177 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
178 | {'k', "key", "KEY", | ||
179 | gettext_noop ("the query key"), | ||
180 | 1, &GNUNET_GETOPT_set_string, &query_key}, | ||
181 | {'d', "data", "DATA", | ||
182 | gettext_noop ("the data to insert under the key"), | ||
183 | 1, &GNUNET_GETOPT_set_string, &data}, | ||
184 | {'t', "type", "TYPE", | ||
185 | gettext_noop ("the type to insert data as"), | ||
186 | 0, &GNUNET_GETOPT_set_uint, &query_type}, | ||
187 | {'T', "timeout", "TIMEOUT", | ||
188 | gettext_noop ("how long to execute this query before giving up?"), | ||
189 | 0, &GNUNET_GETOPT_set_ulong, &timeout_request}, | ||
190 | {'e', "expiration", "EXPIRATION", | ||
191 | gettext_noop ("how long to store this entry in the dht (in seconds)"), | ||
192 | 0, &GNUNET_GETOPT_set_ulong, &expiration_seconds}, | ||
193 | {'V', "verbose", NULL, | ||
194 | gettext_noop ("be verbose (print progress information)"), | ||
195 | 0, &GNUNET_GETOPT_set_one, &verbose}, | ||
196 | GNUNET_GETOPT_OPTION_END | ||
197 | }; | ||
198 | |||
199 | |||
200 | /** | ||
201 | * Entry point for gnunet-dht-put | ||
202 | * | ||
203 | * @param argc number of arguments from the command line | ||
204 | * @param argv command line arguments | ||
205 | * @return 0 ok, 1 on error | ||
206 | */ | ||
207 | int | ||
208 | main (int argc, char *const *argv) | ||
209 | { | ||
210 | return (GNUNET_OK == | ||
211 | GNUNET_PROGRAM_run (argc, | ||
212 | argv, | ||
213 | "gnunet-dht-put", | ||
214 | gettext_noop | ||
215 | ("Issue a PUT request to the GNUnet DHT insert DATA under KEY."), | ||
216 | options, &run, NULL)) ? ret : 1; | ||
217 | } | ||