aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSchanzenbach, Martin <mschanzenbach@posteo.de>2019-06-27 09:08:06 +0200
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2019-06-27 09:08:06 +0200
commit92a2b1758aa56383360603567c17646a1a7fb443 (patch)
tree7652e270ce7f894fe1d76f5dfce62cc6dab1b277
parente0698f82e63b071772217f36aeeb53703f86a19d (diff)
downloadgnunet-92a2b1758aa56383360603567c17646a1a7fb443.tar.gz
gnunet-92a2b1758aa56383360603567c17646a1a7fb443.zip
fix #5782
-rw-r--r--src/gns/gnunet-gns.c36
-rw-r--r--src/gns/nss/nss_gns_query.c99
2 files changed, 79 insertions, 56 deletions
diff --git a/src/gns/gnunet-gns.c b/src/gns/gnunet-gns.c
index 1cda84c59..d795f89fc 100644
--- a/src/gns/gnunet-gns.c
+++ b/src/gns/gnunet-gns.c
@@ -60,6 +60,16 @@ static int raw;
60static uint32_t rtype; 60static uint32_t rtype;
61 61
62/** 62/**
63 * Timeout for lookup
64 */
65static struct GNUNET_TIME_Relative timeout;
66
67/**
68 * Timeout task
69 */
70static struct GNUNET_SCHEDULER_Task *to_task;
71
72/**
63 * Handle to lookup request 73 * Handle to lookup request
64 */ 74 */
65static struct GNUNET_GNS_LookupWithTldRequest *lr; 75static struct GNUNET_GNS_LookupWithTldRequest *lr;
@@ -83,6 +93,11 @@ static void
83do_shutdown (void *cls) 93do_shutdown (void *cls)
84{ 94{
85 (void) cls; 95 (void) cls;
96 if (NULL != to_task)
97 {
98 GNUNET_SCHEDULER_cancel (to_task);
99 to_task = NULL;
100 }
86 if (NULL != lr) 101 if (NULL != lr)
87 { 102 {
88 GNUNET_GNS_lookup_with_tld_cancel (lr); 103 GNUNET_GNS_lookup_with_tld_cancel (lr);
@@ -95,6 +110,18 @@ do_shutdown (void *cls)
95 } 110 }
96} 111}
97 112
113/**
114 * Task to run on timeout
115 *
116 * @param cls unused
117 */
118static void
119do_timeout (void* cls)
120{
121 to_task = NULL;
122 global_ret = 3; //Timeout
123 GNUNET_SCHEDULER_shutdown ();
124}
98 125
99/** 126/**
100 * Function called with the result of a GNS lookup. 127 * Function called with the result of a GNS lookup.
@@ -173,12 +200,14 @@ run (void *cls,
173 (void) cfgfile; 200 (void) cfgfile;
174 201
175 cfg = c; 202 cfg = c;
203 to_task = NULL;
176 if (GNUNET_OK != GNUNET_DNSPARSER_check_name (lookup_name)) 204 if (GNUNET_OK != GNUNET_DNSPARSER_check_name (lookup_name))
177 { 205 {
178 fprintf (stderr, _ ("`%s' is not a valid domain name\n"), lookup_name); 206 fprintf (stderr, _ ("`%s' is not a valid domain name\n"), lookup_name);
179 global_ret = 3; 207 global_ret = 3;
180 return; 208 return;
181 } 209 }
210 to_task = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, NULL);
182 gns = GNUNET_GNS_connect (cfg); 211 gns = GNUNET_GNS_connect (cfg);
183 if (NULL == gns) 212 if (NULL == gns)
184 { 213 {
@@ -221,6 +250,7 @@ run (void *cls,
221int 250int
222main (int argc, char *const *argv) 251main (int argc, char *const *argv)
223{ 252{
253 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
224 struct GNUNET_GETOPT_CommandLineOption options[] = 254 struct GNUNET_GETOPT_CommandLineOption options[] =
225 {GNUNET_GETOPT_option_mandatory ( 255 {GNUNET_GETOPT_option_mandatory (
226 GNUNET_GETOPT_option_string ('u', 256 GNUNET_GETOPT_option_string ('u',
@@ -235,6 +265,12 @@ main (int argc, char *const *argv)
235 gettext_noop ( 265 gettext_noop (
236 "Specify the type of the record to lookup"), 266 "Specify the type of the record to lookup"),
237 &lookup_type), 267 &lookup_type),
268 GNUNET_GETOPT_option_relative_time ('T',
269 "timeout",
270 "TIMEOUT",
271 gettext_noop (
272 "Specify a timeout for the lookup"),
273 &timeout),
238 GNUNET_GETOPT_option_flag ('r', 274 GNUNET_GETOPT_option_flag ('r',
239 "raw", 275 "raw",
240 gettext_noop ("No unneeded output"), 276 gettext_noop ("No unneeded output"),
diff --git a/src/gns/nss/nss_gns_query.c b/src/gns/nss/nss_gns_query.c
index 380788d35..253bb0f02 100644
--- a/src/gns/nss/nss_gns_query.c
+++ b/src/gns/nss/nss_gns_query.c
@@ -30,6 +30,7 @@
30#include <unistd.h> 30#include <unistd.h>
31#include <signal.h> 31#include <signal.h>
32 32
33#define TIMEOUT "5s"
33 34
34static void 35static void
35kwait (pid_t chld) 36kwait (pid_t chld)
@@ -37,9 +38,7 @@ kwait (pid_t chld)
37 int ret; 38 int ret;
38 39
39 kill (chld, SIGKILL); 40 kill (chld, SIGKILL);
40 waitpid (chld, 41 waitpid (chld, &ret, 0);
41 &ret,
42 0);
43} 42}
44 43
45 44
@@ -56,9 +55,7 @@ kwait (pid_t chld)
56 * else 0 55 * else 0
57 */ 56 */
58int 57int
59gns_resolve_name (int af, 58gns_resolve_name (int af, const char *name, struct userdata *u)
60 const char *name,
61 struct userdata *u)
62{ 59{
63 FILE *p; 60 FILE *p;
64 char line[128]; 61 char line[128];
@@ -73,88 +70,78 @@ gns_resolve_name (int af,
73 return -1; 70 return -1;
74 if (0 == pid) 71 if (0 == pid)
75 { 72 {
76 char *argv[] = { 73 char *argv[] = {"gnunet-gns",
77 "gnunet-gns", 74 "-r",
78 "-r", 75 "-t",
79 "-t", 76 (AF_INET6 == af) ? "AAAA" : "A",
80 (AF_INET6 == af) ? "AAAA" : "A", 77 "-u",
81 "-u", 78 (char *) name,
82 (char *) name, 79 "-T",
83 NULL 80 TIMEOUT,
84 }; 81 NULL};
85 82
86 (void) close (STDOUT_FILENO); 83 (void) close (STDOUT_FILENO);
87 if ( (0 != close (out[0])) || 84 if ((0 != close (out[0])) ||
88 (STDOUT_FILENO != dup2 (out[1], STDOUT_FILENO)) ) 85 (STDOUT_FILENO != dup2 (out[1], STDOUT_FILENO)))
89 _exit (1); 86 _exit (1);
90 (void) execvp ("gnunet-gns", 87 (void) execvp ("gnunet-gns", argv);
91 argv);
92 _exit (1); 88 _exit (1);
93 } 89 }
94 (void) close (out[1]); 90 (void) close (out[1]);
95 p = fdopen (out[0], "r"); 91 p = fdopen (out[0], "r");
96 if (NULL == p) 92 if (NULL == p)
97 { 93 {
98 kwait (pid); 94 kwait (pid);
99 return -1; 95 return -1;
100 } 96 }
101 while (NULL != fgets (line, 97 while (NULL != fgets (line, sizeof (line), p))
102 sizeof (line),
103 p))
104 { 98 {
105 if (u->count >= MAX_ENTRIES) 99 if (u->count >= MAX_ENTRIES)
106 break; 100 break;
107 if (line[strlen(line)-1] == '\n') 101 if (line[strlen (line) - 1] == '\n')
108 { 102 {
109 line[strlen(line)-1] = '\0'; 103 line[strlen (line) - 1] = '\0';
110 if (AF_INET == af) 104 if (AF_INET == af)
111 { 105 {
112 if (inet_pton(af, 106 if (inet_pton (af, line, &u->data.ipv4[u->count]))
113 line, 107 {
114 &u->data.ipv4[u->count])) 108 u->count++;
109 u->data_len += sizeof (ipv4_address_t);
110 }
111 else
115 { 112 {
116 u->count++; 113 (void) fclose (p);
117 u->data_len += sizeof(ipv4_address_t);
118 }
119 else
120 {
121 (void) fclose (p);
122 kwait (pid); 114 kwait (pid);
123 errno = EINVAL; 115 errno = EINVAL;
124 return -1; 116 return -1;
125 } 117 }
126 } 118 }
127 else if (AF_INET6 == af) 119 else if (AF_INET6 == af)
128 { 120 {
129 if (inet_pton(af, 121 if (inet_pton (af, line, &u->data.ipv6[u->count]))
130 line,
131 &u->data.ipv6[u->count]))
132 { 122 {
133 u->count++; 123 u->count++;
134 u->data_len += sizeof(ipv6_address_t); 124 u->data_len += sizeof (ipv6_address_t);
135 } 125 }
136 else 126 else
137 { 127 {
138 (void) fclose (p); 128 (void) fclose (p);
139 kwait (pid); 129 kwait (pid);
140 errno = EINVAL; 130 errno = EINVAL;
141 return -1; 131 return -1;
142 } 132 }
143 } 133 }
144 } 134 }
145 } 135 }
146 (void) fclose (p); 136 (void) fclose (p);
147 waitpid (pid, 137 waitpid (pid, &ret, 0);
148 &ret,
149 0);
150 if (! WIFEXITED (ret)) 138 if (! WIFEXITED (ret))
151 return -1; 139 return -1;
152 if (4 == WEXITSTATUS (ret)) 140 if (4 == WEXITSTATUS (ret))
153 return -2; /* not for GNS */ 141 return -2; /* not for GNS */
154 if (3 == ret) 142 if (3 == ret)
155 return -3; /* timeout -> not found */ 143 return -3; /* timeout -> not found */
156 if ( (2 == WEXITSTATUS (ret)) || 144 if ((2 == WEXITSTATUS (ret)) || (1 == WEXITSTATUS (ret)))
157 (1 == WEXITSTATUS (ret)) )
158 return -2; /* launch failure -> service unavailable */ 145 return -2; /* launch failure -> service unavailable */
159 return 0; 146 return 0;
160} 147}