int options_parse(AppContext * const context, int argc, char *argv[]) { int opt_flag; int option_index = 0; options_init_with_default(context); if (argc <= 1) { options_usage(); exit(1); } while ((opt_flag = getopt_long(argc, argv, getopt_options, getopt_long_options, &option_index)) != -1) { switch (opt_flag) { case 'd': context->log_dir = optarg; break; case 'F': context->logfile_max_files = (size_t) strtoul(optarg, NULL, 10); break; case 'h': options_usage(); exit(0); case 'S': context->logfile_max_space = (off_t) strtoul(optarg, NULL, 10); break; case 's': context->logfile_soft_limit = (size_t) strtoul(optarg, NULL, 10); break; case 't': context->logfile_rotate_after = (time_t) strtoul(optarg, NULL, 10); break; case 'V': options_version(); exit(0); case 'z': if (log_set_compression(context, optarg) != 0) { errx(1, _("Unsupported compression method: [%s]"), optarg); } break; default: options_usage(); exit(0); } } options_apply(context); return 0; }
int options_parse(AppContext * const app_context, int argc, char *argv[]) { int opt_flag; int option_index = 0; options_init_with_default(app_context); while ((opt_flag = getopt_long(argc, argv, getopt_options, getopt_long_options, &option_index)) != -1) { switch (opt_flag) { case '6': app_context->want_ipv6 = 1; break; case 'h': options_usage(); exit(0); case 'r': app_context->resolver_ip = optarg; break; case 't': app_context->use_tcp = 1; break; case 'V': options_version(); exit(0); default: options_usage(); exit(1); } } argc -= optind; argv += optind; if (argc != 1 || *argv == NULL) { options_usage(); exit(1); } app_context->host_name = *argv; options_apply(app_context); return 0; }
int options_parse(AppContext * const app_context, ProxyContext * const proxy_context, int argc, char *argv[]) { int opt_flag; int option_index = 0; #ifdef _WIN32 _Bool option_install = 0; #endif options_init_with_default(app_context, proxy_context); while ((opt_flag = getopt_long(argc, argv, getopt_options, getopt_long_options, &option_index)) != -1) { switch (opt_flag) { case 'a': proxy_context->local_ip = optarg; break; case 'd': proxy_context->daemonize = 1; break; case 'e': { char *endptr; const unsigned long edns_payload_size = strtoul(optarg, &endptr, 10); if (*optarg == 0 || *endptr != 0 || edns_payload_size > DNS_MAX_PACKET_SIZE_UDP_RECV) { logger(proxy_context, LOG_ERR, "Invalid EDNS payload size: [%s]", optarg); exit(1); } if (edns_payload_size <= DNS_MAX_PACKET_SIZE_UDP_NO_EDNS_SEND) { proxy_context->edns_payload_size = (size_t) 0U; proxy_context->udp_max_size = DNS_MAX_PACKET_SIZE_UDP_NO_EDNS_SEND; } else { proxy_context->edns_payload_size = (size_t) edns_payload_size; assert(proxy_context->udp_max_size >= DNS_MAX_PACKET_SIZE_UDP_NO_EDNS_SEND); if (proxy_context->edns_payload_size > DNS_MAX_PACKET_SIZE_UDP_NO_EDNS_SEND) { proxy_context->udp_max_size = proxy_context->edns_payload_size; } } break; } case 'E': proxy_context->ephemeral_keys = 1; break; case 'h': options_usage(); exit(0); case 'k': proxy_context->provider_publickey_s = optarg; break; case 'K': proxy_context->client_key_file = optarg; break; case 'l': proxy_context->log_file = optarg; break; case 'L': proxy_context->resolvers_list = optarg; break; case 'R': proxy_context->resolver_name = optarg; break; #ifndef _WIN32 case 'S': proxy_context->syslog = 1; break; case 'Z': proxy_context->syslog = 1; proxy_context->syslog_prefix = optarg; break; #endif case 'm': { char *endptr; const long max_log_level = strtol(optarg, &endptr, 10); if (*optarg == 0 || *endptr != 0 || max_log_level < 0) { logger(proxy_context, LOG_ERR, "Invalid max log level: [%s]", optarg); exit(1); } proxy_context->max_log_level = max_log_level; break; } case 'n': { char *endptr; const unsigned long connections_count_max = strtoul(optarg, &endptr, 10); if (*optarg == 0 || *endptr != 0 || connections_count_max <= 0U || connections_count_max > UINT_MAX) { logger(proxy_context, LOG_ERR, "Invalid max number of active request: [%s]", optarg); exit(1); } proxy_context->connections_count_max = (unsigned int) connections_count_max; break; } case 'p': proxy_context->pid_file = optarg; break; case 'r': proxy_context->resolver_ip = optarg; break; case 't': { char *endptr; const unsigned long margin = strtoul(optarg, &endptr, 10); if (*optarg == 0 || *endptr != 0 || margin > UINT32_MAX / 60U) { logger(proxy_context, LOG_ERR, "Invalid certificate grace period: [%s]", optarg); exit(1); } proxy_context->test_cert_margin = (time_t) margin * (time_t) 60U; proxy_context->test_only = 1; break; } #ifdef HAVE_GETPWNAM case 'u': { const struct passwd * const pw = getpwnam(optarg); if (pw == NULL) { logger(proxy_context, LOG_ERR, "Unknown user: [%s]", optarg); exit(1); } proxy_context->user_id = pw->pw_uid; proxy_context->user_group = pw->pw_gid; proxy_context->user_dir = strdup(pw->pw_dir); break; } #endif case 'N': proxy_context->provider_name = optarg; break; case 'T': proxy_context->tcp_only = 1; break; case 'V': options_version(); exit(0); case 'X': #ifndef PLUGINS logger_noformat(proxy_context, LOG_ERR, "Support for plugins hasn't been compiled in"); exit(1); #else if (plugin_options_parse_str (proxy_context->app_context->dcps_context, optarg) != 0) { logger_noformat(proxy_context, LOG_ERR, "Error while parsing plugin options"); exit(2); } #endif break; #ifdef _WIN32 case WIN_OPTION_INSTALL: case WIN_OPTION_REINSTALL: option_install = 1; break; case WIN_OPTION_UNINSTALL: if (windows_service_uninstall() != 0) { logger_noformat(NULL, LOG_ERR, "Unable to uninstall the service"); exit(1); } else { logger_noformat(NULL, LOG_INFO, "The " WINDOWS_SERVICE_NAME " service has been removed from this system"); exit(0); } break; #endif default: options_usage(); exit(1); } } if (options_apply(proxy_context) != 0) { return -1; } #ifdef _WIN32 if (option_install != 0) { if (windows_service_install(proxy_context) != 0) { logger_noformat(NULL, LOG_ERR, "Unable to install the service"); logger_noformat(NULL, LOG_ERR, "Make sure that you are using an elevated command prompt " "and that the service hasn't been already installed"); exit(1); } logger_noformat(NULL, LOG_INFO, "The " WINDOWS_SERVICE_NAME " service has been installed and started"); logger_noformat(NULL, LOG_INFO, "The registry key used for this " "service is " WINDOWS_SERVICE_REGISTRY_PARAMETERS_KEY); logger(NULL, LOG_INFO, "Now, change your resolver settings to %s", proxy_context->local_ip); exit(0); } #endif return 0; }
int options_parse(AppContext * const app_context, ProxyContext * const proxy_context, int argc, char *argv[]) { int opt_flag; int option_index = 0; options_init_with_default(app_context, proxy_context); while ((opt_flag = getopt_long(argc, argv, getopt_options, getopt_long_options, &option_index)) != -1) { switch (opt_flag) { case 'a': proxy_context->local_ip = optarg; break; case 'd': proxy_context->daemonize = 1; break; case 'e': { char *endptr; const unsigned long edns_payload_size = strtoul(optarg, &endptr, 10); if (*optarg == 0 || *endptr != 0 || edns_payload_size > DNS_MAX_PACKET_SIZE_UDP_RECV) { logger(proxy_context, LOG_ERR, "Invalid EDNS payload size: [%s]", optarg); exit(1); } if (edns_payload_size <= DNS_MAX_PACKET_SIZE_UDP_SEND) { proxy_context->edns_payload_size = (size_t) 0U; } else { proxy_context->edns_payload_size = (size_t) edns_payload_size; } break; } case 'h': options_usage(); exit(0); case 'k': proxy_context->provider_publickey_s = optarg; break; case 'l': proxy_context->log_file = optarg; break; case 'n': { char *endptr; const unsigned long connections_count_max = strtoul(optarg, &endptr, 10); if (*optarg == 0 || *endptr != 0 || connections_count_max <= 0U || connections_count_max > UINT_MAX) { logger(proxy_context, LOG_ERR, "Invalid max number of active request: [%s]", optarg); exit(1); } proxy_context->connections_count_max = (unsigned int) connections_count_max; break; } case 'p': proxy_context->pid_file = optarg; break; case 'r': proxy_context->resolver_ip = optarg; break; #ifdef HAVE_GETPWNAM case 'u': { const struct passwd * const pw = getpwnam(optarg); if (pw == NULL) { logger(proxy_context, LOG_ERR, "Unknown user: [%s]", optarg); exit(1); } proxy_context->user_id = pw->pw_uid; proxy_context->user_group = pw->pw_gid; proxy_context->user_dir = strdup(pw->pw_dir); break; } #endif case 'N': proxy_context->provider_name = optarg; break; case 'T': proxy_context->tcp_only = 1; break; case 'V': options_version(); exit(0); case 'X': #ifndef PLUGINS logger_noformat(proxy_context, LOG_ERR, "Support for plugins hasn't been compiled in"); exit(1); #else if (plugin_options_parse_str (proxy_context->app_context->dcps_context, optarg) != 0) { logger_noformat(proxy_context, LOG_ERR, "Error while parsing plugin options"); exit(2); } #endif break; #ifdef _WIN32 case WIN_OPTION_INSTALL: case WIN_OPTION_UNINSTALL: if (windows_service_option(opt_flag, argc, argv) != 0) { options_usage(); exit(1); } break; #endif default: options_usage(); exit(1); } } if (options_apply(proxy_context) != 0) { return -1; } return 0; }