Added -r for recursion in dns command.

This commit is contained in:
Jose Marcial Vieira Bisneto 2016-12-29 20:05:53 -03:00
parent 396dfc6abc
commit 00bf29b0fa

View file

@ -57,27 +57,61 @@ Expect these resolutions:
int ipfs_dns (int argc, char **argv) int ipfs_dns (int argc, char **argv)
{ {
int err; int err, r=0, i;
char **txt, *path; char **txt, *path, *param;
if (argc == 4 && strcmp ("-r", argv[2])==0) {
r = 1;
argc--; argv++;
}
if (argc != 3) { if (argc != 3) {
fprintf (stderr, "usage: ipfs dns dns.name.com\n"); fprintf (stderr, "usage: ipfs dns [-r] dns.name.com\n");
return -1; return -1;
} }
err = ipfs_dnslink_resolv_lookupTXT (&txt, argv[2]); param = malloc (strlen (argv[2]) + 1);
if (err) { if (!param) {
fprintf (stderr, "dns lookupTXT: %s\n", Err[err]); fprintf (stderr, "memory allocation failed.\n");
return err; return 1;
} }
strcpy (param, argv[2]);
err = ipfs_dnslink_parse_txt(&path, *txt); for (i = 0 ; i < DefaultDepthLimit ; i++) {
if (err) { if (memcmp(param, "/ipns/", 6) == 0) {
fprintf (stderr, "dns parse_txt: %s\n", Err[err]); err = ipfs_dnslink_resolv_lookupTXT (&txt, param+6);
return err; } else {
} err = ipfs_dnslink_resolv_lookupTXT (&txt, param);
free (*txt); }
free (txt); if (err) {
fprintf (stderr, "param: %s\n", param);
fprintf (stderr, "dns lookupTXT: %s\n", Err[err]);
return err;
}
err = ipfs_dnslink_parse_txt(&path, *txt);
if (err) {
free (*txt);
free (txt);
fprintf (stderr, "dns parse_txt: %s\n", Err[err]);
return err;
}
free (*txt);
free (txt);
free (param);
if (! r) {
// not recursive.
break;
}
if (memcmp(path, "/ipfs/", 6) == 0) {
break;
}
param = path;
} while (--r);
fprintf (stdout, "%s\n", path); fprintf (stdout, "%s\n", path);
free (path); free (path);