chiark / gitweb /
resolve-host: make arg_type an int
[elogind.git] / src / resolve-host / resolve-host.c
index 1b1edaf..4b46cdf 100644 (file)
@@ -37,8 +37,9 @@
 
 static int arg_family = AF_UNSPEC;
 static int arg_ifindex = 0;
-static uint16_t arg_type = 0;
+static int arg_type = 0;
 static uint16_t arg_class = 0;
+static bool arg_legend = true;
 
 static int resolve_host(sd_bus *bus, const char *name) {
 
@@ -104,7 +105,7 @@ static int resolve_host(sd_bus *bus, const char *name) {
                         return bus_log_parse_error(r);
 
                 if (!IN_SET(family, AF_INET, AF_INET6)) {
-                        log_debug("%s: skipping entry with family %hu (%s)", name, family, af_to_name(family) ?: "unknown");
+                        log_debug("%s: skipping entry with family %d (%s)", name, family, af_to_name(family) ?: "unknown");
                         continue;
                 }
 
@@ -315,6 +316,7 @@ static int resolve_record(sd_bus *bus, const char *name) {
         if (r < 0)
                 return bus_log_create_error(r);
 
+        assert((uint16_t) arg_type == arg_type);
         r = sd_bus_message_append(req, "sqq", name, arg_class, arg_type);
         if (r < 0)
                 return bus_log_create_error(r);
@@ -387,6 +389,32 @@ static int resolve_record(sd_bus *bus, const char *name) {
         return 0;
 }
 
+static void help_dns_types(void) {
+        int i;
+        const char *t;
+
+        if (arg_legend)
+                puts("Known dns types:");
+        for (i = 0; i < _DNS_TYPE_MAX; i++) {
+                t = dns_type_to_string(i);
+                if (t)
+                        puts(t);
+        }
+}
+
+static void help_dns_classes(void) {
+        int i;
+        const char *t;
+
+        if (arg_legend)
+                puts("Known dns classes:");
+        for (i = 0; i < _DNS_CLASS_MAX; i++) {
+                t = dns_class_to_string(i);
+                if (t)
+                        puts(t);
+        }
+}
+
 static void help(void) {
         printf("%s [OPTIONS...]\n\n"
                "Resolve IPv4 or IPv6 addresses.\n\n"
@@ -397,12 +425,14 @@ static void help(void) {
                "  -i INTERFACE          Filter by interface\n"
                "  -t --type=TYPE        Query RR with DNS type\n"
                "  -c --class=CLASS      Query RR with DNS class\n"
+               "  --no-legend           Do not print column headers\n"
                , program_invocation_short_name);
 }
 
 static int parse_argv(int argc, char *argv[]) {
         enum {
                 ARG_VERSION = 0x100,
+                ARG_NO_LEGEND,
         };
 
         static const struct option options[] = {
@@ -410,6 +440,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "version",     no_argument,       NULL, ARG_VERSION   },
                 { "type",        no_argument,       NULL, 't'           },
                 { "class",       no_argument,       NULL, 'c'           },
+                { "no-legend",   no_argument,       NULL, ARG_NO_LEGEND },
                 {}
         };
 
@@ -418,7 +449,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "h46i:t:c:", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "h46i:t:c:", options, NULL)) >= 0)
                 switch(c) {
 
                 case 'h':
@@ -447,19 +478,36 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case 't':
-                        r = dns_type_from_string(optarg, &arg_type);
-                        if (r < 0) {
+                        if (streq(optarg, "help")) {
+                                help_dns_types();
+                                return 0;
+                        }
+
+                        arg_type = dns_type_from_string(optarg);
+                        if (arg_type < 0) {
                                 log_error("Failed to parse RR record type %s", optarg);
                                 return r;
                         }
+                        assert(arg_type > 0 && (uint16_t) arg_type == arg_type);
+
                         break;
 
                 case 'c':
+                        if (streq(optarg, "help")) {
+                                help_dns_classes();
+                                return 0;
+                        }
+
                         r = dns_class_from_string(optarg, &arg_class);
                         if (r < 0) {
                                 log_error("Failed to parse RR record class %s", optarg);
                                 return r;
                         }
+
+                        break;
+
+                case ARG_NO_LEGEND:
+                        arg_legend = false;
                         break;
 
                 case '?':
@@ -468,7 +516,6 @@ static int parse_argv(int argc, char *argv[]) {
                 default:
                         assert_not_reached("Unhandled option");
                 }
-        }
 
         if (arg_type == 0 && arg_class != 0) {
                 log_error("--class= may only be used in conjunction with --type=");