Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / hostip / options.c
blob5aa85cbe72280c6a142b3acd51d0ae261efad01f
2 #include <config.h>
3 #include <sys/types.h>
5 #include <assert.h>
6 #include <getopt.h>
7 #include <stdio.h>
8 #include <stdlib.h>
10 #include "app.h"
11 #include "options.h"
13 #ifndef DEFAULT_RESOLVER_IP
14 # define DEFAULT_RESOLVER_IP "208.67.220.220"
15 #endif
17 static struct option getopt_long_options[] = {
18 { "ipv6", 0, NULL, '6' },
19 { "help", 0, NULL, 'h' },
20 { "resolver-address", 1, NULL, 'r' },
21 { "version", 0, NULL, 'V' },
22 { NULL, 0, NULL, 0 }
24 static const char *getopt_options = "6hr:V";
26 static void
27 options_version(void)
29 puts("hostip v" PACKAGE_VERSION "\n"
30 "Copyright (C) 2012 OpenDNS, Inc.");
33 static void
34 options_usage(void)
36 puts("Usage: hostip [-6] [-r resolver_ip[:port]] host_name\n"
37 " -6, --ipv6: ask for AAAA records\n"
38 " -h, --help: show usage\n"
39 " -r, --resolver-address=<ip>: the resolver IP address (default: OpenDNS)\n"
40 " -V, --version: show version number\n"
41 "\n"
42 "Example: hostip -r 208.67.222.222 www.example.com\n");
45 static
46 void options_init_with_default(AppContext * const app_context)
48 app_context->host_name = NULL;
49 app_context->resolver_ip = DEFAULT_RESOLVER_IP;
50 app_context->want_ipv6 = 0;
53 static int
54 options_apply(AppContext * const app_context)
56 if (app_context->resolver_ip == NULL) {
57 options_usage();
58 exit(1);
60 return 0;
63 int
64 options_parse(AppContext * const app_context, int argc, char *argv[])
66 int opt_flag;
67 int option_index = 0;
69 options_init_with_default(app_context);
70 while ((opt_flag = getopt_long(argc, argv,
71 getopt_options, getopt_long_options,
72 &option_index)) != -1) {
73 switch (opt_flag) {
74 case '6':
75 app_context->want_ipv6 = 1;
76 break;
77 case 'h':
78 options_usage();
79 exit(0);
80 case 'r':
81 app_context->resolver_ip = optarg;
82 break;
83 case 'V':
84 options_version();
85 exit(0);
86 default:
87 options_usage();
88 exit(1);
91 argc -= optind;
92 argv += optind;
93 if (argc != 1 || *argv == NULL) {
94 options_usage();
95 exit(1);
97 app_context->host_name = *argv;
98 options_apply(app_context);
100 return 0;