options.c
来自「OpenVPN -- A Secure tunneling daemon」· C语言 代码 · 共 1,267 行 · 第 1/3 页
C
1,267 行
SHOW_INT (tls_timeout); SHOW_INT (renegotiate_bytes); SHOW_INT (renegotiate_packets); SHOW_INT (renegotiate_seconds); SHOW_INT (handshake_window); SHOW_INT (transition_window); SHOW_BOOL (single_session); SHOW_BOOL (disable_occ); SHOW_STR (tls_auth_file);#endif#endif}#undef SHOW_PARM#undef SHOW_STR#undef SHOW_INT#undef SHOW_BOOL#if defined(USE_CRYPTO) && defined(USE_SSL)/* * Build an options string to represent data channel encryption options. * This string must match exactly between peers. The keysize is checked * separately by read_key(). */char *options_string (const struct options *o){ struct buffer out = alloc_buf (256); buf_printf (&out, "V1");#ifdef STRICT_OPTIONS_CHECK buf_printf (&out, " --dev-type %s", dev_type_string (o->dev, o->dev_type)); if (o->udp_mtu_defined) buf_printf (&out, " --udp-mtu %d", o->udp_mtu); if (o->tun_mtu_defined) buf_printf (&out, " --tun-mtu %d", o->tun_mtu); if (o->tun_ipv6) buf_printf (&out, " --tun-ipv6");#endif if (o->ciphername_defined) buf_printf (&out, " --cipher %s", o->ciphername); if (o->authname_defined) buf_printf (&out, " --auth %s", o->authname); if (!o->packet_id) buf_printf (&out, " --no-replay"); if (!o->iv) buf_printf (&out, " --no-iv");#ifdef USE_LZO if (o->comp_lzo) buf_printf (&out, " --comp-lzo");#endif#ifdef FRAGMENT_ENABLE if (o->mtu_dynamic) buf_printf (&out, " --mtu-dynamic");#endif return BSTR (&out);}#endif/* * Compare option strings for equality. * If the first two chars of the strings differ, it means that * we are looking at different versions of the options string, * therefore don't compare them and return true. */bool options_cmp_equal (const char *s1, const char *s2, size_t n){#ifndef STRICT_OPTIONS_CHECK if (strncmp (s1, s2, 2)) return true; else#endif return !strncmp (s1, s2, n);}static char *comma_to_space (const char *src){ char *ret = (char *) gc_malloc (strlen (src) + 1); char *dest = ret; char c; do { c = *src++; if (c == ',') c = ' '; *dest++ = c; } while (c); return ret;}static voidusage (void){ struct options o; FILE *fp = msg_fp(); init_options (&o);#if defined(USE_CRYPTO) && defined(USE_SSL) fprintf (fp, usage_message, title_string, o.local_port, o.remote_port, o.udp_mtu, o.tun_mtu, o.tun_mtu_extra, o.verbosity, o.authname, o.ciphername, o.tls_timeout, o.renegotiate_seconds, o.handshake_window, o.transition_window);#elif defined(USE_CRYPTO) fprintf (fp, usage_message, title_string, o.local_port, o.remote_port, o.udp_mtu, o.tun_mtu, o.tun_mtu_extra, o.verbosity, o.authname, o.ciphername);#else fprintf (fp, usage_message, title_string, o.local_port, o.remote_port, o.udp_mtu, o.tun_mtu, o.tun_mtu_extra, o.verbosity);#endif fflush(fp); exit (OPENVPN_EXIT_STATUS_USAGE); /* exit point */}voidusage_small (void){ msg (M_WARN, "Use --help for more information"); exit (OPENVPN_EXIT_STATUS_USAGE); /* exit point */}static voidusage_version (void){ msg (M_INFO, "%s", title_string); msg (M_INFO, "Copyright (C) 2002-2003 James Yonan <jim@yonan.net>"); exit (OPENVPN_EXIT_STATUS_USAGE); /* exit point */}voidnotnull (const char *arg, const char *description){ if (!arg) { msg (M_WARN, "Options error: You must define %s", description); usage_small (); }}boolstring_defined_equal (const char *s1, const char *s2){ if (s1 && s2) return !strcmp (s1, s2); else return false;}static voidping_rec_err (void){ msg (M_WARN, "Options error: only one of --ping-exit or --ping-restart options may be specified"); usage_small ();}static intpositive (int i){ return i < 0 ? 0 : i;}static boolspace (char c){ return c == '\0' || isspace (c);}static intparse_line (char *line, char *p[], int n, const char *file, int line_num){ int ret = 0; char *c = line; char *start = NULL; /* * Parse states: * 0 -- Initial * 1 -- Reading non-quoted parm * 2 -- Leading quote * 3 -- Reading quoted parm * 4 -- First char after parm */ int state = 0; do { if (state == 0) { if (!space (*c)) { if (*c == ';' || *c == '#') /* comment */ break; if (*c == '\"') state = 2; else { start = c; state = 1; } } } else if (state == 1) { if (space (*c)) state = 4; } else if (state == 2) { start = c; state = 3; } else if (state == 3) { if (*c == '\"') state = 4; } if (state == 4) { const int len = (int) (c - start); ASSERT (len > 0); p[ret] = gc_malloc (len + 1); memcpy (p[ret], start, len); 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; 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 (openvpn_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")) { if (!options->daemon) { options->daemon = true; open_syslog (p2); if (p2) ++i; } } else if (streq (p1, "inetd")) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?