📄 options.c
字号:
p[ret][len] = '\0'; state = 0; if (++ret >= n) break; } } while (*c++ != '\0'); if (state == 2 || state == 3) msg (M_FATAL, "No closing quotation (\") in %s:%d", file, line_num); if (state) msg (M_FATAL, "Residual parse state (%d) in %s:%d", state, file, line_num);#if 0 { int i; for (i = 0; i < ret; ++i) { msg (M_INFO, "%s:%d ARG[%d] '%s'", file, line_num, i, p[i]); } }#endif return ret;}static intadd_option (struct options *options, int i, char *p1, char *p2, char *p3, const char* file, int line, int level);static voidread_config_file (struct options *options, const char* file, int level, const char* top_file, int top_line){ const int max_recursive_levels = 10; FILE *fp; int line_num; char line[256]; ++level; if (level > max_recursive_levels) msg (M_FATAL, "In %s:%d: Maximum recursive include levels exceeded in include attempt of file %s -- probably you have a configuration file that tries to include itself.", top_file, top_line, file); fp = fopen (file, "r"); if (!fp) msg (M_ERR, "In %s:%d: Error opening configuration file: %s", top_file, top_line, file); line_num = 0; while (fgets(line, sizeof (line), fp)) { char *p[3]; int nargs; CLEAR (p); ++line_num; nargs = parse_line (line, p, 3, file, line_num); if (nargs) { char *p0 = p[0]; if (strlen (p0) >= 3 && !strncmp (p0, "--", 2)) p0 += 2; add_option (options, 0, p0, p[1], p[2], file, line_num, level); } } fclose (fp);}static intadd_option (struct options *options, int i, char *p1, char *p2, char *p3, const char* file, int line, int level){ if (!file) { file = "[CMD-LINE]"; line = 1; } if (streq (p1, "help")) { usage (); } if (streq (p1, "version")) { usage_version (); } else if (streq (p1, "config") && p2) { ++i; read_config_file (options, p2, level, file, line); } else if (streq (p1, "dev") && p2) { ++i; options->dev = p2; } else if (streq (p1, "dev-type") && p2) { ++i; options->dev_type = p2; } else if (streq (p1, "dev-node") && p2) { ++i; options->dev_node = p2; } else if (streq (p1, "tun-ipv6")) { options->tun_ipv6 = true; } else if (streq (p1, "ifconfig") && p2 && p3) { options->ifconfig_local = p2; options->ifconfig_remote = p3; options->udp_mtu_defined = true; i += 2; } else if (streq (p1, "local") && p2) { ++i; options->local = p2; } else if (streq (p1, "remote") && p2) { ++i; options->remote = p2; } else if (streq (p1, "resolv-retry") && p2) { ++i; options->resolve_retry_seconds = positive (atoi (p2)); } else if (streq (p1, "ipchange") && p2) { ++i; options->ipchange = comma_to_space (p2); } else if (streq (p1, "float")) { options->remote_float = true; } else if (streq (p1, "gremlin")) { options->gremlin = true; } else if (streq (p1, "user") && p2) { ++i; options->username = p2; } else if (streq (p1, "group") && p2) { ++i; options->groupname = p2; } else if (streq (p1, "chroot") && p2) { ++i; options->chroot_dir = p2; } else if (streq (p1, "cd") && p2) { ++i; options->cd_dir = p2; if (chdir (p2)) msg (M_ERR, "cd to '%s' failed", p2); } else if (streq (p1, "writepid") && p2) { ++i; options->writepid = p2; } else if (streq (p1, "up") && p2) { ++i; options->up_script = p2; } else if (streq (p1, "down") && p2) { ++i; options->down_script = p2; } else if (streq (p1, "daemon")) { options->daemon = true; } else if (streq (p1, "inetd")) { if (!options->inetd) { options->inetd = true; become_inetd_server (); } } else if (streq (p1, "mlock")) { options->mlock = true; } else if (streq (p1, "verb") && p2) { ++i; options->verbosity = positive (atoi (p2)); } else if (streq (p1, "mute") && p2) { ++i; options->mute = positive (atoi (p2)); } else if (streq (p1, "udp-mtu") && p2) { ++i; options->udp_mtu = positive (atoi (p2)); options->udp_mtu_defined = true; } else if (streq (p1, "tun-mtu") && p2) { ++i; options->tun_mtu = positive (atoi (p2)); options->tun_mtu_defined = true; } else if (streq (p1, "nice") && p2) { ++i; options->nice = atoi (p2); }#ifdef USE_PTHREAD else if (streq (p1, "nice-work") && p2) { ++i; options->nice_work = atoi (p2); }#endif else if (streq (p1, "shaper") && p2) { ++i; options->shaper = atoi (p2); if (options->shaper < SHAPER_MIN || options->shaper > SHAPER_MAX) { msg (M_WARN, "bad shaper value, must be between %d and %d", SHAPER_MIN, SHAPER_MAX); usage_small (); } } else if (streq (p1, "port") && p2) { ++i; options->local_port = options->remote_port = atoi (p2); if (options->local_port <= 0 || options->remote_port <= 0) { msg (M_WARN, "Bad port number: %s", p2); usage_small (); } } else if (streq (p1, "lport") && p2) { ++i; options->local_port = atoi (p2); if (options->local_port <= 0) { msg (M_WARN, "Bad local port number: %s", p2); usage_small (); } } else if (streq (p1, "rport") && p2) { ++i; options->remote_port = atoi (p2); if (options->remote_port <= 0) { msg (M_WARN, "Bad remote port number: %s", p2); usage_small (); } } else if (streq (p1, "nobind")) { options->bind_local = false; } else if (streq (p1, "inactive") && p2) { ++i; options->inactivity_timeout = positive (atoi (p2)); } else if (streq (p1, "ping") && p2) { ++i; options->ping_send_timeout = positive (atoi (p2)); } else if (streq (p1, "ping-exit") && p2) { ++i; if (options->ping_rec_timeout_action) ping_rec_err(); options->ping_rec_timeout = positive (atoi (p2)); options->ping_rec_timeout_action = PING_EXIT; } else if (streq (p1, "ping-restart") && p2) { ++i; if (options->ping_rec_timeout_action) ping_rec_err(); options->ping_rec_timeout = positive (atoi (p2)); options->ping_rec_timeout_action = PING_RESTART; } else if (streq (p1, "ping-timer-rem")) { options->ping_timer_remote = true; } else if (streq (p1, "persist-tun")) { options->persist_tun = true; } else if (streq (p1, "persist-key")) { options->persist_key = true; } else if (streq (p1, "persist-local-ip")) { options->persist_local_ip = true; } else if (streq (p1, "persist-remote-ip")) { options->persist_remote_ip = true; }#ifdef USE_LZO else if (streq (p1, "comp-lzo")) { options->comp_lzo = true; } else if (streq (p1, "comp-noadapt")) { options->comp_lzo_adaptive = false; }#endif /* USE_LZO */#ifdef USE_CRYPTO else if (streq (p1, "show-ciphers")) { options->show_ciphers = true; } else if (streq (p1, "show-digests")) { options->show_digests = true; } else if (streq (p1, "secret") && p2) { ++i; options->shared_secret_file = p2; } else if (streq (p1, "genkey")) { options->genkey = true; } else if (streq (p1, "auth") && p2) { ++i; options->authname_defined = true; options->authname = p2; if (streq (options->authname, "none")) { options->authname_defined = false; options->authname = NULL; } } else if (streq (p1, "auth")) { options->authname_defined = true; } else if (streq (p1, "cipher") && p2) { ++i; options->ciphername_defined = true; options->ciphername = p2; if (streq (options->ciphername, "none")) { options->ciphername_defined = false; options->ciphername = NULL; } } else if (streq (p1, "cipher")) { options->ciphername_defined = true; } else if (streq (p1, "no-replay")) { options->packet_id = false; } else if (streq (p1, "no-iv")) { options->iv = false; } else if (streq (p1, "test-crypto")) { options->test_crypto = true; }#ifdef HAVE_EVP_CIPHER_CTX_SET_KEY_LENGTH else if (streq (p1, "keysize") && p2) { ++i; options->keysize = atoi (p2) / 8; if (options->keysize < 0 || options->keysize > MAX_CIPHER_KEY_LENGTH) { msg (M_WARN, "Bad keysize: %s", p2); usage_small (); } }#endif#ifdef USE_SSL else if (streq (p1, "show-tls")) { options->show_tls_ciphers = true; } else if (streq (p1, "tls-server")) { options->tls_server = true; } else if (streq (p1, "tls-client")) { options->tls_client = true; } else if (streq (p1, "ca") && p2) { ++i; options->ca_file = p2; } else if (streq (p1, "dh") && p2) { ++i; options->dh_file = p2; } else if (streq (p1, "cert") && p2) { ++i; options->cert_file = p2; } else if (streq (p1, "key") && p2) { ++i; options->priv_key_file = p2; } else if (streq (p1, "askpass")) { options->askpass = true; } else if (streq (p1, "single-session")) { options->single_session = true; } else if (streq (p1, "disable-occ")) { options->disable_occ = true; } else if (streq (p1, "tls-cipher") && p2) { ++i; options->cipher_list = p2; } else if (streq (p1, "tls-verify") && p2) { ++i; options->tls_verify = comma_to_space (p2); } else if (streq (p1, "tls_timeout") && p2) { ++i; options->tls_timeout = positive (atoi (p2)); } else if (streq (p1, "reneg-bytes") && p2) { ++i; options->renegotiate_bytes = positive (atoi (p2)); } else if (streq (p1, "reneg-pkts") && p2) { ++i; options->renegotiate_packets = positive (atoi (p2)); } else if (streq (p1, "reneg-sec") && p2) { ++i; options->renegotiate_seconds = positive (atoi (p2)); } else if (streq (p1, "hand-window") && p2) { ++i; options->handshake_window = positive (atoi (p2)); } else if (streq (p1, "tran-window") && p2) { ++i; options->transition_window = positive (atoi (p2)); } else if (streq (p1, "tls-auth") && p2) { ++i; options->tls_auth_file = p2; }#endif /* USE_SSL */#endif /* USE_CRYPTO */#ifdef TUNSETPERSIST else if (streq (p1, "rmtun")) { options->persist_config = true; options->persist_mode = 0; } else if (streq (p1, "mktun")) { options->persist_config = true; options->persist_mode = 1; }#endif else { if (file) msg (M_WARN, "Unrecognized option or missing parameter(s) in %s:%d: %s", file, line, p1); else msg (M_WARN, "Unrecognized option or missing parameter(s): --%s", p1); usage_small (); } return i;}voidparse_argv (struct options* options, int argc, char *argv[]){ int i; /* usage message */ if (argc <= 1) usage (); /* parse command line */ for (i = 1; i < argc; ++i) { char *p1 = argv[i]; char *p2 = NULL; char *p3 = NULL; if (strncmp(p1, "--", 2)) { msg (M_WARN, "I'm trying to parse \"%s\" as an --option parameter but I don't see a leading '--'", p1); usage_small (); } p1 += 2; if (i + 1 < argc) { p2 = argv[i + 1]; if (!strncmp (p2, "--", 2)) p2 = NULL; } if (i + 2 < argc) { p3 = argv[i + 2]; if (!strncmp (p3, "--", 2)) p3 = NULL; } i = add_option (options, i, p1, p2, p3, NULL, 0, 0); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -