diff --git a/.uncrustify.cfg b/.uncrustify.cfg index 233e06d2..fa59765c 100644 --- a/.uncrustify.cfg +++ b/.uncrustify.cfg @@ -1,80 +1,1376 @@ # -# based on uncrustify config file for the linux kernel +# General options # -code_width = 120 -indent_case_brace = 4 -indent_columns = 4 -indent_label = 2 # pos: absolute col, neg: relative column -indent_with_tabs = 0 +# The type of line endings +newlines = lf # auto/lf/crlf/cr + +# The original size of tabs in the input +input_tab_size = 8 # number + +# The size of tabs in the output (only used if align_with_tabs=true) +output_tab_size = 8 # number + +# The ASCII value of the string escape char, usually 92 (\) or 94 (^). (Pawn) +string_escape_char = 92 # number + +# Alternate string escape char for Pawn. Only works right before the quote char. +string_escape_char2 = 0 # number # -# inter-symbol newlines +# Indenting # -nl_brace_else = remove # "} else" vs "} \n else" - cuddle else -nl_brace_while = remove # "} while" vs "} \n while" - cuddle while -nl_do_brace = remove # "do {" vs "do \n {" -nl_else_brace = remove # "else {" vs "else \n {" -nl_enum_brace = remove # "enum {" vs "enum \n {" -nl_fcall_brace = remove # "list_for_each() {" vs "list_for_each()\n{" -nl_fdef_brace = force # "int foo() {" vs "int foo()\n{" -nl_for_brace = remove # "for () {" vs "for () \n {" -nl_func_var_def_blk = 0 # don't add newlines after a block of var declarations -nl_if_brace = remove # "if () {" vs "if () \n {" -nl_multi_line_define = true -nl_struct_brace = remove # "struct {" vs "struct \n {" -nl_switch_brace = remove # "switch () {" vs "switch () \n {" -nl_union_brace = remove # "union {" vs "union \n {" -nl_while_brace = remove # "while () {" vs "while () \n {" +# The number of columns to indent per level. +# Usually 2, 3, 4, or 8. +indent_columns = 4 # number + +# The continuation indent. If non-zero, this overrides the indent of '(' and '=' continuation indents. +# For FreeBSD, this is set to 4. +indent_continue = 0 # number + +# How to use tabs when indenting code +# 0=spaces only +# 1=indent with tabs to brace level, align with spaces +# 2=indent and align with tabs, using spaces when not on a tabstop +indent_with_tabs = 0 # number + +# Comments that are not a brace level are indented with tabs on a tabstop. +# Requires indent_with_tabs=2. If false, will use spaces. +indent_cmt_with_tabs = false # false/true + +# Whether to indent strings broken by '\' so that they line up +indent_align_string = true # false/true + +# The number of spaces to indent multi-line XML strings. +# Requires indent_align_string=True +indent_xml_string = 0 # number + +# Spaces to indent '{' from level +indent_brace = 0 # number + +# Whether braces are indented to the body level +indent_braces = false # false/true + +# Disabled indenting function braces if indent_braces is true +indent_braces_no_func = false # false/true + +# Disabled indenting class braces if indent_braces is true +indent_braces_no_class = false # false/true + +# Disabled indenting struct braces if indent_braces is true +indent_braces_no_struct = false # false/true + +# Indent based on the size of the brace parent, i.e. 'if' => 3 spaces, 'for' => 4 spaces, etc. +indent_brace_parent = false # false/true + +# Whether the 'namespace' body is indented +indent_namespace = false # false/true + +# The number of spaces to indent a namespace block +indent_namespace_level = 0 # number + +# If the body of the namespace is longer than this number, it won't be indented. +# Requires indent_namespace=true. Default=0 (no limit) +indent_namespace_limit = 0 # number + +# Whether the 'extern "C"' body is indented +indent_extern = false # false/true + +# Whether the 'class' body is indented +indent_class = false # false/true + +# Whether to indent the stuff after a leading class colon +indent_class_colon = false # false/true + +# False=treat 'else\nif' as 'else if' for indenting purposes +# True=indent the 'if' one level +indent_else_if = false # false/true + +# Amount to indent variable declarations after a open brace. neg=relative, pos=absolute +indent_var_def_blk = 0 # number + +# Indent continued variable declarations instead of aligning. +indent_var_def_cont = false # false/true + +# True: indent continued function call parameters one indent level +# False: align parameters under the open paren +indent_func_call_param = false # false/true + +# Same as indent_func_call_param, but for function defs +indent_func_def_param = false # false/true + +# Same as indent_func_call_param, but for function protos +indent_func_proto_param = false # false/true + +# Same as indent_func_call_param, but for class declarations +indent_func_class_param = false # false/true + +# Same as indent_func_call_param, but for class variable constructors +indent_func_ctor_var_param = false # false/true + +# Same as indent_func_call_param, but for templates +indent_template_param = false # false/true + +# Double the indent for indent_func_xxx_param options +indent_func_param_double = false # false/true + +# Indentation column for standalone 'const' function decl/proto qualifier +indent_func_const = 0 # number + +# Indentation column for standalone 'throw' function decl/proto qualifier +indent_func_throw = 0 # number + +# The number of spaces to indent a continued '->' or '.' +# Usually set to 0, 1, or indent_columns. +indent_member = 0 # number + +# Spaces to indent single line ('//') comments on lines before code +indent_sing_line_comments = 0 # number + +# If set, will indent trailing single line ('//') comments relative +# to the code instead of trying to keep the same absolute column +indent_relative_single_line_comments = false # false/true + +# Spaces to indent 'case' from 'switch' +# Usually 0 or indent_columns. +indent_switch_case = 0 # number + +# Spaces to shift the 'case' line, without affecting any other lines +# Usually 0. +indent_case_shift = 0 # number + +# Spaces to indent '{' from 'case'. +# By default, the brace will appear under the 'c' in case. +# Usually set to 0 or indent_columns. +indent_case_brace = 0 # number + +# Whether to indent comments found in first column +indent_col1_comment = false # false/true + +# How to indent goto labels +# >0 : absolute column where 1 is the leftmost column +# <=0 : subtract from brace indent +indent_label = 1 # number + +# Same as indent_label, but for access specifiers that are followed by a colon +indent_access_spec = 1 # number + +# Indent the code after an access specifier by one level. +# If set, this option forces 'indent_access_spec=0' +indent_access_spec_body = false # false/true + +# If an open paren is followed by a newline, indent the next line so that it lines up after the open paren (not recommended) +indent_paren_nl = false # false/true + +# Controls the indent of a close paren after a newline. +# 0: Indent to body level +# 1: Align under the open paren +# 2: Indent to the brace level +indent_paren_close = 0 # number + +# Controls the indent of a comma when inside a paren.If TRUE, aligns under the open paren +indent_comma_paren = false # false/true + +# Controls the indent of a BOOL operator when inside a paren.If TRUE, aligns under the open paren +indent_bool_paren = false # false/true + +# If 'indent_bool_paren' is true, controls the indent of the first expression. If TRUE, aligns the first expression to the following ones +indent_first_bool_expr = false # false/true + +# If an open square is followed by a newline, indent the next line so that it lines up after the open square (not recommended) +indent_square_nl = false # false/true + +# Don't change the relative indent of ESQL/C 'EXEC SQL' bodies +indent_preserve_sql = false # false/true + +# Align continued statements at the '='. Default=True +# If FALSE or the '=' is followed by a newline, the next line is indent one tab. +indent_align_assign = true # false/true # -# Source code modifications +# Spacing options # -mod_full_brace_do = force # "do a--; while ();" vs "do { a--; } while ();" -mod_full_brace_for = force # "for () a--;" vs "for () { a--; }" -mod_full_brace_if = force # "if (a) a--;" vs "if (a) { a--; }" -mod_full_brace_nl = 3 # don't remove if more than 3 newlines -mod_full_brace_while = force # "while (a) a--;" vs "while (a) { a--; }" -mod_paren_on_return = remove # "return 1;" vs "return (1);" +# Add or remove space around arithmetic operator '+', '-', '/', '*', etc +sp_arith = force # ignore/add/remove/force + +# Add or remove space around assignment operator '=', '+=', etc +sp_assign = force # ignore/add/remove/force + +# Add or remove space around assignment operator '=' in a prototype +sp_assign_default = ignore # ignore/add/remove/force + +# Add or remove space before assignment operator '=', '+=', etc. Overrides sp_assign. +sp_before_assign = ignore # ignore/add/remove/force + +# Add or remove space after assignment operator '=', '+=', etc. Overrides sp_assign. +sp_after_assign = ignore # ignore/add/remove/force + +# Add or remove space around assignment '=' in enum +sp_enum_assign = force # ignore/add/remove/force + +# Add or remove space before assignment '=' in enum. Overrides sp_enum_assign. +sp_enum_before_assign = ignore # ignore/add/remove/force + +# Add or remove space after assignment '=' in enum. Overrides sp_enum_assign. +sp_enum_after_assign = ignore # ignore/add/remove/force + +# Add or remove space around preprocessor '##' concatenation operator. Default=Add +sp_pp_concat = add # ignore/add/remove/force + +# Add or remove space after preprocessor '#' stringify operator. Also affects the '#@' charizing operator. Default=Add +sp_pp_stringify = add # ignore/add/remove/force + +# Add or remove space around boolean operators '&&' and '||' +sp_bool = force # ignore/add/remove/force + +# Add or remove space around compare operator '<', '>', '==', etc +sp_compare = force # ignore/add/remove/force + +# Add or remove space inside '(' and ')' +sp_inside_paren = remove # ignore/add/remove/force + +# Add or remove space between nested parens +sp_paren_paren = remove # ignore/add/remove/force + +# Whether to balance spaces inside nested parens +sp_balance_nested_parens = false # false/true + +# Add or remove space between ')' and '{' +sp_paren_brace = force # ignore/add/remove/force + +# Add or remove space before pointer star '*' +sp_before_ptr_star = add # ignore/add/remove/force + +# Add or remove space before pointer star '*' that isn't followed by a variable name +# If set to 'ignore', sp_before_ptr_star is used instead. +sp_before_unnamed_ptr_star = add # ignore/add/remove/force + +# Add or remove space between pointer stars '*' +sp_between_ptr_star = remove # ignore/add/remove/force + +# Add or remove space after pointer star '*', if followed by a word. +sp_after_ptr_star = remove # ignore/add/remove/force + +# Add or remove space after a pointer star '*', if followed by a func proto/def. +sp_after_ptr_star_func = remove # ignore/add/remove/force + +# Add or remove space before a pointer star '*', if followed by a func proto/def. +sp_before_ptr_star_func = ignore # ignore/add/remove/force + +# Add or remove space before a reference sign '&' +sp_before_byref = force # ignore/add/remove/force + +# Add or remove space before a reference sign '&' that isn't followed by a variable name +# If set to 'ignore', sp_before_byref is used instead. +sp_before_unnamed_byref = force # ignore/add/remove/force + +# Add or remove space after reference sign '&', if followed by a word. +sp_after_byref = ignore # ignore/add/remove/force + +# Add or remove space after a reference sign '&', if followed by a func proto/def. +sp_after_byref_func = ignore # ignore/add/remove/force + +# Add or remove space before a reference sign '&', if followed by a func proto/def. +sp_before_byref_func = ignore # ignore/add/remove/force + +# Add or remove space between type and word. Default=Force +sp_after_type = force # ignore/add/remove/force + +# Add or remove space in 'template <' vs 'template<'. +# If set to ignore, sp_before_angle is used. +sp_template_angle = ignore # ignore/add/remove/force + +# Add or remove space before '<>' +sp_before_angle = ignore # ignore/add/remove/force + +# Add or remove space inside '<' and '>' +sp_inside_angle = ignore # ignore/add/remove/force + +# Add or remove space after '<>' +sp_after_angle = ignore # ignore/add/remove/force + +# Add or remove space between '<>' and '(' as found in 'new List();' +sp_angle_paren = ignore # ignore/add/remove/force + +# Add or remove space between '<>' and a word as in 'List m;' +sp_angle_word = ignore # ignore/add/remove/force + +# Add or remove space between '>' and '>' in '>>' (template stuff C++/C# only). Default=Add +sp_angle_shift = add # ignore/add/remove/force + +# Add or remove space before '(' of 'if', 'for', 'switch', and 'while' +sp_before_sparen = force # ignore/add/remove/force + +# Add or remove space inside if-condition '(' and ')' +sp_inside_sparen = remove # ignore/add/remove/force + +# Add or remove space before if-condition ')'. Overrides sp_inside_sparen. +sp_inside_sparen_close = ignore # ignore/add/remove/force + +# Add or remove space after ')' of 'if', 'for', 'switch', and 'while' +sp_after_sparen = ignore # ignore/add/remove/force + +# Add or remove space between ')' and '{' of 'if', 'for', 'switch', and 'while' +sp_sparen_brace = force # ignore/add/remove/force + +# Add or remove space between 'invariant' and '(' in the D language. +sp_invariant_paren = ignore # ignore/add/remove/force + +# Add or remove space after the ')' in 'invariant (C) c' in the D language. +sp_after_invariant_paren = ignore # ignore/add/remove/force + +# Add or remove space before empty statement ';' on 'if', 'for' and 'while' +sp_special_semi = ignore # ignore/add/remove/force + +# Add or remove space before ';'. Default=Remove +sp_before_semi = remove # ignore/add/remove/force + +# Add or remove space before ';' in non-empty 'for' statements +sp_before_semi_for = ignore # ignore/add/remove/force + +# Add or remove space before a semicolon of an empty part of a for statement. +sp_before_semi_for_empty = ignore # ignore/add/remove/force + +# Add or remove space after ';', except when followed by a comment. Default=Add +sp_after_semi = force # ignore/add/remove/force + +# Add or remove space after ';' in non-empty 'for' statements. Default=Force +sp_after_semi_for = force # ignore/add/remove/force + +# Add or remove space after the final semicolon of an empty part of a for statement: for ( ; ; ). +sp_after_semi_for_empty = remove # ignore/add/remove/force + +# Add or remove space before '[' (except '[]') +sp_before_square = remove # ignore/add/remove/force + +# Add or remove space before '[]' +sp_before_squares = ignore # ignore/add/remove/force + +# Add or remove space inside '[' and ']' +sp_inside_square = remove # ignore/add/remove/force + +# Add or remove space after ',' +sp_after_comma = force # ignore/add/remove/force + +# Add or remove space before ',' +sp_before_comma = remove # ignore/add/remove/force + +# Add or remove space between an open paren and comma: '(,' vs '( ,' +sp_paren_comma = force # ignore/add/remove/force + +# Add or remove space before the variadic '...' when preceded by a non-punctuator +sp_before_ellipsis = ignore # ignore/add/remove/force + +# Add or remove space after class ':' +sp_after_class_colon = ignore # ignore/add/remove/force + +# Add or remove space before class ':' +sp_before_class_colon = ignore # ignore/add/remove/force + +# Add or remove space before case ':'. Default=Remove +sp_before_case_colon = remove # ignore/add/remove/force + +# Add or remove space between 'operator' and operator sign +sp_after_operator = ignore # ignore/add/remove/force + +# Add or remove space between the operator symbol and the open paren, as in 'operator ++(' +sp_after_operator_sym = ignore # ignore/add/remove/force + +# Add or remove space after C/D cast, i.e. 'cast(int)a' vs 'cast(int) a' or '(int)a' vs '(int) a' +sp_after_cast = remove # ignore/add/remove/force + +# Add or remove spaces inside cast parens +sp_inside_paren_cast = remove # ignore/add/remove/force + +# Add or remove space between the type and open paren in a C++ cast, i.e. 'int(exp)' vs 'int (exp)' +sp_cpp_cast_paren = ignore # ignore/add/remove/force + +# Add or remove space between 'sizeof' and '(' +sp_sizeof_paren = remove # ignore/add/remove/force + +# Add or remove space after the tag keyword (Pawn) +sp_after_tag = ignore # ignore/add/remove/force + +# Add or remove space inside enum '{' and '}' +sp_inside_braces_enum = remove # ignore/add/remove/force + +# Add or remove space inside struct/union '{' and '}' +sp_inside_braces_struct = force # ignore/add/remove/force + +# Add or remove space inside '{' and '}' +sp_inside_braces = force # ignore/add/remove/force + +# Add or remove space inside '{}' +sp_inside_braces_empty = remove # ignore/add/remove/force + +# Add or remove space between return type and function name +# A minimum of 1 is forced except for pointer return types. +sp_type_func = remove # ignore/add/remove/force + +# Add or remove space between function name and '(' on function declaration +sp_func_proto_paren = remove # ignore/add/remove/force + +# Add or remove space between function name and '(' on function definition +sp_func_def_paren = remove # ignore/add/remove/force + +# Add or remove space inside empty function '()' +sp_inside_fparens = remove # ignore/add/remove/force + +# Add or remove space inside function '(' and ')' +sp_inside_fparen = remove # ignore/add/remove/force + +# Add or remove space between ']' and '(' when part of a function call. +sp_square_fparen = ignore # ignore/add/remove/force + +# Add or remove space between ')' and '{' of function +sp_fparen_brace = ignore # ignore/add/remove/force + +# Add or remove space between function name and '(' on function calls +sp_func_call_paren = remove # ignore/add/remove/force + +# Add or remove space between function name and '()' on function calls without parameters. +# If set to 'ignore' (the default), sp_func_call_paren is used. +sp_func_call_paren_empty = ignore # ignore/add/remove/force + +# Add or remove space between the user function name and '(' on function calls +# You need to set a keyword to be a user function, like this: 'set func_call_user _' in the config file. +sp_func_call_user_paren = ignore # ignore/add/remove/force + +# Add or remove space between a constructor/destructor and the open paren +sp_func_class_paren = ignore # ignore/add/remove/force + +# Add or remove space between 'return' and '(' +sp_return_paren = ignore # ignore/add/remove/force + +# Add or remove space between '__attribute__' and '(' +sp_attribute_paren = remove # ignore/add/remove/force + +# Add or remove space between 'defined' and '(' in '#if defined (FOO)' +sp_defined_paren = ignore # ignore/add/remove/force + +# Add or remove space between 'throw' and '(' in 'throw (something)' +sp_throw_paren = ignore # ignore/add/remove/force + +# Add or remove space between macro and value +sp_macro = ignore # ignore/add/remove/force + +# Add or remove space between macro function ')' and value +sp_macro_func = ignore # ignore/add/remove/force + +# Add or remove space between 'else' and '{' if on the same line +sp_else_brace = force # ignore/add/remove/force + +# Add or remove space between '}' and 'else' if on the same line +sp_brace_else = force # ignore/add/remove/force + +# Add or remove space between '}' and the name of a typedef on the same line +sp_brace_typedef = force # ignore/add/remove/force + +# Add or remove space between 'catch' and '{' if on the same line +sp_catch_brace = ignore # ignore/add/remove/force + +# Add or remove space between '}' and 'catch' if on the same line +sp_brace_catch = ignore # ignore/add/remove/force + +# Add or remove space between 'finally' and '{' if on the same line +sp_finally_brace = ignore # ignore/add/remove/force + +# Add or remove space between '}' and 'finally' if on the same line +sp_brace_finally = ignore # ignore/add/remove/force + +# Add or remove space between 'try' and '{' if on the same line +sp_try_brace = ignore # ignore/add/remove/force + +# Add or remove space between get/set and '{' if on the same line +sp_getset_brace = ignore # ignore/add/remove/force + +# Add or remove space before the '::' operator +sp_before_dc = ignore # ignore/add/remove/force + +# Add or remove space after the '::' operator +sp_after_dc = ignore # ignore/add/remove/force + +# Add or remove around the D named array initializer ':' operator +sp_d_array_colon = ignore # ignore/add/remove/force + +# Add or remove space after the '!' (not) operator. Default=Remove +sp_not = remove # ignore/add/remove/force + +# Add or remove space after the '~' (invert) operator. Default=Remove +sp_inv = remove # ignore/add/remove/force + +# Add or remove space after the '&' (address-of) operator. Default=Remove +# This does not affect the spacing after a '&' that is part of a type. +sp_addr = remove # ignore/add/remove/force + +# Add or remove space around the '.' or '->' operators. Default=Remove +sp_member = remove # ignore/add/remove/force + +# Add or remove space after the '*' (dereference) operator. Default=Remove +# This does not affect the spacing after a '*' that is part of a type. +sp_deref = remove # ignore/add/remove/force + +# Add or remove space after '+' or '-', as in 'x = -5' or 'y = +7'. Default=Remove +sp_sign = remove # ignore/add/remove/force + +# Add or remove space before or after '++' and '--', as in '(--x)' or 'y++;'. Default=Remove +sp_incdec = remove # ignore/add/remove/force + +# Add or remove space before a backslash-newline at the end of a line. Default=Add +sp_before_nl_cont = add # ignore/add/remove/force + +# Add or remove space after the scope '+' or '-', as in '-(void) foo;' or '+(int) bar;' +sp_after_oc_scope = ignore # ignore/add/remove/force + +# Add or remove space after the colon in message specs +# '-(int) f:(int) x;' vs '-(int) f: (int) x;' +sp_after_oc_colon = ignore # ignore/add/remove/force + +# Add or remove space before the colon in message specs +# '-(int) f: (int) x;' vs '-(int) f : (int) x;' +sp_before_oc_colon = ignore # ignore/add/remove/force + +# Add or remove space after the colon in message specs +# '[object setValue:1];' vs '[object setValue: 1];' +sp_after_send_oc_colon = ignore # ignore/add/remove/force + +# Add or remove space before the colon in message specs +# '[object setValue:1];' vs '[object setValue :1];' +sp_before_send_oc_colon = ignore # ignore/add/remove/force + +# Add or remove space after the (type) in message specs +# '-(int)f: (int) x;' vs '-(int)f: (int)x;' +sp_after_oc_type = ignore # ignore/add/remove/force + +# Add or remove space after the first (type) in message specs +# '-(int) f:(int)x;' vs '-(int)f:(int)x;' +sp_after_oc_return_type = ignore # ignore/add/remove/force + +# Add or remove space between '@selector' and '(' +# '@selector(msgName)' vs '@selector (msgName)' +# Also applies to @protocol() constructs +sp_after_oc_at_sel = ignore # ignore/add/remove/force + +# Add or remove space between '@selector(x)' and the following word +# '@selector(foo) a:' vs '@selector(foo)a:' +sp_after_oc_at_sel_parens = ignore # ignore/add/remove/force + +# Add or remove space inside '@selector' parens +# '@selector(foo)' vs '@selector( foo )' +# Also applies to @protocol() constructs +sp_inside_oc_at_sel_parens = ignore # ignore/add/remove/force + +# Add or remove space before a block pointer caret +# '^int (int arg){...}' vs. ' ^int (int arg){...}' +sp_before_oc_block_caret = ignore # ignore/add/remove/force + +# Add or remove space after a block pointer caret +# '^int (int arg){...}' vs. '^ int (int arg){...}' +sp_after_oc_block_caret = ignore # ignore/add/remove/force + +# Add or remove space around the ':' in 'b ? t : f' +sp_cond_colon = force # ignore/add/remove/force + +# Add or remove space around the '?' in 'b ? t : f' +sp_cond_question = force # ignore/add/remove/force + +# Fix the spacing between 'case' and the label. Only 'ignore' and 'force' make sense here. +sp_case_label = ignore # ignore/add/remove/force + +# Control the space around the D '..' operator. +sp_range = ignore # ignore/add/remove/force + +# Control the space after the opening of a C++ comment '// A' vs '//A' +# MPlayer devs note: we ignore the following setting because it breaks Doxygen comments +#sp_cmt_cpp_start = ignore # ignore/add/remove/force +sp_cmt_cpp_start = add # ignore/add/remove/force + +# Controls the spaces between #else or #endif and a trailing comment +sp_endif_cmt = ignore # ignore/add/remove/force # -# inter-character spacing options +# Code alignment (not left column spaces/tabs) # -sp_after_cast = remove # "(int) a" vs "(int)a" -sp_after_comma = force -sp_after_sparen = force # "if () {" vs "if (){" -sp_arith = force -sp_assign = force -sp_assign = force -sp_before_comma = remove -sp_before_ptr_star = force # "char *foo" vs "char* foo -sp_before_sparen = force # "if (" vs "if(" -sp_between_ptr_star = remove # "char * *foo" vs "char **foo" -sp_bool = force -sp_compare = force -sp_func_call_paren = remove # "foo (" vs "foo(" -sp_func_def_paren = remove # "int foo (){" vs "int foo(){" -sp_func_proto_paren = remove # "int foo ();" vs "int foo();" -sp_inside_braces = force # "{ 1 }" vs "{1}" -sp_inside_braces_enum = force # "{ 1 }" vs "{1}" -sp_inside_braces_struct = force # "{ 1 }" vs "{1}" -sp_inside_sparen = remove -sp_paren_brace = force -sp_sizeof_paren = remove # "sizeof (int)" vs "sizeof(int)" -sp_else_brace = force -sp_brace_else = force + +# Whether to keep non-indenting tabs +align_keep_tabs = false # false/true + +# Whether to use tabs for aligning +align_with_tabs = false # false/true + +# Whether to bump out to the next tab when aligning +align_on_tabstop = false # false/true + +# Whether to left-align numbers +align_number_left = true # false/true + +# Align variable definitions in prototypes and functions +align_func_params = false # false/true + +# Align parameters in single-line functions that have the same name. +# The function names must already be aligned with each other. +align_same_func_call_params = false # false/true + +# The span for aligning variable definitions (0=don't align) +align_var_def_span = 0 # number + +# How to align the star in variable definitions. +# 0=Part of the type 'void * foo;' +# 1=Part of the variable 'void *foo;' +# 2=Dangling 'void *foo;' +align_var_def_star_style = 2 # number + +# How to align the '&' in variable definitions. +# 0=Part of the type +# 1=Part of the variable +# 2=Dangling +align_var_def_amp_style = 0 # number + +# The threshold for aligning variable definitions (0=no limit) +align_var_def_thresh = 0 # number + +# The gap for aligning variable definitions +align_var_def_gap = 0 # number + +# Whether to align the colon in struct bit fields +align_var_def_colon = false # false/true + +# Whether to align any attribute after the variable name +align_var_def_attribute = false # false/true + +# Whether to align inline struct/enum/union variable definitions +align_var_def_inline = false # false/true + +# The span for aligning on '=' in assignments (0=don't align) +align_assign_span = 1 # number + +# The threshold for aligning on '=' in assignments (0=no limit) +align_assign_thresh = 0 # number + +# The span for aligning on '=' in enums (0=don't align) +align_enum_equ_span = 1 # number + +# The threshold for aligning on '=' in enums (0=no limit) +align_enum_equ_thresh = 0 # number + +# The span for aligning struct/union (0=don't align) +align_var_struct_span = 0 # number + +# The threshold for aligning struct/union member definitions (0=no limit) +align_var_struct_thresh = 0 # number + +# The gap for aligning struct/union member definitions +align_var_struct_gap = 0 # number + +# The span for aligning struct initializer values (0=don't align) +align_struct_init_span = 1 # number + +# The minimum space between the type and the synonym of a typedef +align_typedef_gap = 0 # number + +# The span for aligning single-line typedefs (0=don't align) +align_typedef_span = 0 # number + +# How to align typedef'd functions with other typedefs +# 0: Don't mix them at all +# 1: align the open paren with the types +# 2: align the function type name with the other type names +align_typedef_func = 0 # number + +# Controls the positioning of the '*' in typedefs. Just try it. +# 0: Align on typedef type, ignore '*' +# 1: The '*' is part of type name: typedef int *pint; +# 2: The '*' is part of the type, but dangling: typedef int *pint; +align_typedef_star_style = 0 # number + +# Controls the positioning of the '&' in typedefs. Just try it. +# 0: Align on typedef type, ignore '&' +# 1: The '&' is part of type name: typedef int &pint; +# 2: The '&' is part of the type, but dangling: typedef int &pint; +align_typedef_amp_style = 0 # number + +# The span for aligning comments that end lines (0=don't align) +align_right_cmt_span = 0 # number + +# If aligning comments, mix with comments after '}' and #endif with less than 3 spaces before the comment +align_right_cmt_mix = false # false/true + +# If a trailing comment is more than this number of columns away from the text it follows, +# it will qualify for being aligned. This has to be > 0 to do anything. +align_right_cmt_gap = 0 # number + +# Align trailing comment at or beyond column N; 'pulls in' comments as a bonus side effect (0=ignore) +align_right_cmt_at_col = 0 # number + +# The span for aligning function prototypes (0=don't align) +align_func_proto_span = 0 # number + +# Minimum gap between the return type and the function name. +align_func_proto_gap = 0 # number + +# Align function protos on the 'operator' keyword instead of what follows +align_on_operator = false # false/true + +# Whether to mix aligning prototype and variable declarations. +# If true, align_var_def_XXX options are used instead of align_func_proto_XXX options. +align_mix_var_proto = false # false/true + +# Align single-line functions with function prototypes, uses align_func_proto_span +align_single_line_func = false # false/true + +# Aligning the open brace of single-line functions. +# Requires align_single_line_func=true, uses align_func_proto_span +align_single_line_brace = false # false/true + +# Gap for align_single_line_brace. +align_single_line_brace_gap = 0 # number + +# The span for aligning ObjC msg spec (0=don't align) +align_oc_msg_spec_span = 0 # number + +# Whether to align macros wrapped with a backslash and a newline. +# This will not work right if the macro contains a multi-line comment. +align_nl_cont = false # false/true + +# The minimum space between label and value of a preprocessor define +align_pp_define_gap = 0 # number + +# The span for aligning on '#define' bodies (0=don't align) +align_pp_define_span = 0 # number + +# Align lines that start with '<<' with previous '<<'. Default=true +align_left_shift = true # false/true + +# Span for aligning parameters in an Obj-C message call on the ':' (0=don't align) +align_oc_msg_colon_span = 0 # number + +# Aligning parameters in an Obj-C '+' or '-' declaration on the ':' +align_oc_decl_colon = false # false/true # -# Aligning stuff +# Newline adding and removing options +# + +# Whether to collapse empty blocks between '{' and '}' +nl_collapse_empty_body = false # false/true + +# Don't split one-line braced assignments - 'foo_t f = { 1, 2 };' +nl_assign_leave_one_liners = false # false/true + +# Don't split one-line braced statements inside a class xx { } body +nl_class_leave_one_liners = false # false/true + +# Don't split one-line enums: 'enum foo { BAR = 15 };' +nl_enum_leave_one_liners = false # false/true + +# Don't split one-line get or set functions +nl_getset_leave_one_liners = false # false/true + +# Don't split one-line function definitions - 'int foo() { return 0; }' +nl_func_leave_one_liners = false # false/true + +# Don't split one-line if/else statements - 'if(a) b++;' +nl_if_leave_one_liners = false # false/true + +# Add or remove newlines at the start of the file +nl_start_of_file = remove # ignore/add/remove/force + +# The number of newlines at the start of the file (only used if nl_start_of_file is 'add' or 'force' +nl_start_of_file_min = 0 # number + +# Add or remove newline at the end of the file +nl_end_of_file = force # ignore/add/remove/force + +# The number of newlines at the end of the file (only used if nl_end_of_file is 'add' or 'force') +nl_end_of_file_min = 1 # number + +# Add or remove newline between '=' and '{' +nl_assign_brace = remove # ignore/add/remove/force + +# Add or remove newline between '=' and '[' (D only) +nl_assign_square = ignore # ignore/add/remove/force + +# Add or remove newline after '= [' (D only). Will also affect the newline before the ']' +nl_after_square_assign = ignore # ignore/add/remove/force + +# The number of blank lines after a block of variable definitions +nl_func_var_def_blk = 0 # number + +# Add or remove newline between a function call's ')' and '{', as in: +# list_for_each(item, &list) { } +nl_fcall_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'enum' and '{' +nl_enum_brace = remove # ignore/add/remove/force + +# Add or remove newline between 'struct and '{' +nl_struct_brace = remove # ignore/add/remove/force + +# Add or remove newline between 'union' and '{' +nl_union_brace = remove # ignore/add/remove/force + +# Add or remove newline between 'if' and '{' +nl_if_brace = remove # ignore/add/remove/force + +# Add or remove newline between '}' and 'else' +nl_brace_else = remove # ignore/add/remove/force + +# Add or remove newline between 'else if' and '{' +# If set to ignore, nl_if_brace is used instead +nl_elseif_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'else' and '{' +nl_else_brace = remove # ignore/add/remove/force + +# Add or remove newline between 'else' and 'if' +nl_else_if = remove # ignore/add/remove/force + +# Add or remove newline between '}' and 'finally' +nl_brace_finally = ignore # ignore/add/remove/force + +# Add or remove newline between 'finally' and '{' +nl_finally_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'try' and '{' +nl_try_brace = ignore # ignore/add/remove/force + +# Add or remove newline between get/set and '{' +nl_getset_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'for' and '{' +nl_for_brace = remove # ignore/add/remove/force + +# Add or remove newline between 'catch' and '{' +nl_catch_brace = ignore # ignore/add/remove/force + +# Add or remove newline between '}' and 'catch' +nl_brace_catch = ignore # ignore/add/remove/force + +# Add or remove newline between 'while' and '{' +nl_while_brace = remove # ignore/add/remove/force + +# Add or remove newline between 'using' and '{' +nl_using_brace = ignore # ignore/add/remove/force + +# Add or remove newline between two open or close braces. +# Due to general newline/brace handling, REMOVE may not work. +nl_brace_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'do' and '{' +nl_do_brace = remove # ignore/add/remove/force + +# Add or remove newline between '}' and 'while' of 'do' statement +nl_brace_while = remove # ignore/add/remove/force + +# Add or remove newline between 'switch' and '{' +nl_switch_brace = remove # ignore/add/remove/force + +# Add a newline between ')' and '{' if the ')' is on a different line than the if/for/etc. +# Overrides nl_for_brace, nl_if_brace, nl_switch_brace, nl_while_switch, and nl_catch_brace. +nl_multi_line_cond = false # false/true + +# Force a newline in a define after the macro name for multi-line defines. +nl_multi_line_define = false # false/true + +# Whether to put a newline before 'case' statement +nl_before_case = false # false/true + +# Add or remove newline between ')' and 'throw' +nl_before_throw = ignore # ignore/add/remove/force + +# Whether to put a newline after 'case' statement +nl_after_case = true # false/true + +# Newline between namespace and { +nl_namespace_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'template<>' and whatever follows. +nl_template_class = ignore # ignore/add/remove/force + +# Add or remove newline between 'class' and '{' +nl_class_brace = ignore # ignore/add/remove/force + +# Add or remove newline after each ',' in the constructor member initialization +nl_class_init_args = ignore # ignore/add/remove/force + +# Add or remove newline between return type and function name in a function definition +nl_func_type_name = remove # ignore/add/remove/force + +# Add or remove newline between return type and function name inside a class {} +# Uses nl_func_type_name or nl_func_proto_type_name if set to ignore. +nl_func_type_name_class = ignore # ignore/add/remove/force + +# Add or remove newline between function scope and name in a definition +# Controls the newline after '::' in 'void A::f() { }' +nl_func_scope_name = ignore # ignore/add/remove/force + +# Add or remove newline between return type and function name in a prototype +nl_func_proto_type_name = remove # ignore/add/remove/force + +# Add or remove newline between a function name and the opening '(' +nl_func_paren = remove # ignore/add/remove/force + +# Add or remove newline between a function name and the opening '(' in the definition +nl_func_def_paren = remove # ignore/add/remove/force + +# Add or remove newline after '(' in a function declaration +nl_func_decl_start = remove # ignore/add/remove/force + +# Add or remove newline after '(' in a function definition +nl_func_def_start = ignore # ignore/add/remove/force + +# Overrides nl_func_decl_start when there is only one parameter. +nl_func_decl_start_single = ignore # ignore/add/remove/force + +# Overrides nl_func_def_start when there is only one parameter. +nl_func_def_start_single = ignore # ignore/add/remove/force + +# Add or remove newline after each ',' in a function declaration +nl_func_decl_args = ignore # ignore/add/remove/force + +# Add or remove newline after each ',' in a function definition +nl_func_def_args = ignore # ignore/add/remove/force + +# Add or remove newline before the ')' in a function declaration +nl_func_decl_end = remove # ignore/add/remove/force + +# Add or remove newline before the ')' in a function definition +nl_func_def_end = ignore # ignore/add/remove/force + +# Overrides nl_func_decl_end when there is only one parameter. +nl_func_decl_end_single = ignore # ignore/add/remove/force + +# Overrides nl_func_def_end when there is only one parameter. +nl_func_def_end_single = ignore # ignore/add/remove/force + +# Add or remove newline between '()' in a function declaration. +nl_func_decl_empty = ignore # ignore/add/remove/force + +# Add or remove newline between '()' in a function definition. +nl_func_def_empty = ignore # ignore/add/remove/force + +# Add or remove newline between function signature and '{' +nl_fdef_brace = force # ignore/add/remove/force + +# Whether to put a newline after 'return' statement +nl_after_return = false # false/true + +# Add or remove a newline between the return keyword and return expression. +nl_return_expr = ignore # ignore/add/remove/force + +# Whether to put a newline after semicolons, except in 'for' statements +nl_after_semicolon = true # false/true + +# Whether to put a newline after brace open. +# This also adds a newline before the matching brace close. +nl_after_brace_open = false # false/true + +# If nl_after_brace_open and nl_after_brace_open_cmt are true, a newline is +# placed between the open brace and a trailing single-line comment. +nl_after_brace_open_cmt = false # false/true + +# Whether to put a newline after a virtual brace open with a non-empty body. +# These occur in un-braced if/while/do/for statement bodies. +nl_after_vbrace_open = true # false/true + +# Whether to put a newline after a virtual brace open with an empty body. +# These occur in un-braced if/while/do/for statement bodies. +nl_after_vbrace_open_empty = false # false/true + +# Whether to put a newline after a brace close. +# Does not apply if followed by a necessary ';'. +nl_after_brace_close = false # false/true + +# Whether to put a newline after a virtual brace close. +# Would add a newline before return in: 'if (foo) a++; return;' +nl_after_vbrace_close = false # false/true + +# Whether to alter newlines in '#define' macros +nl_define_macro = false # false/true + +# Whether to not put blanks after '#ifxx', '#elxx', or before '#endif' +nl_squeeze_ifdef = false # false/true + +# Add or remove blank line before 'if' +nl_before_if = ignore # ignore/add/remove/force + +# Add or remove blank line after 'if' statement +nl_after_if = ignore # ignore/add/remove/force + +# Add or remove blank line before 'for' +nl_before_for = ignore # ignore/add/remove/force + +# Add or remove blank line after 'for' statement +nl_after_for = ignore # ignore/add/remove/force + +# Add or remove blank line before 'while' +nl_before_while = ignore # ignore/add/remove/force + +# Add or remove blank line after 'while' statement +nl_after_while = ignore # ignore/add/remove/force + +# Add or remove blank line before 'switch' +nl_before_switch = ignore # ignore/add/remove/force + +# Add or remove blank line after 'switch' statement +nl_after_switch = ignore # ignore/add/remove/force + +# Add or remove blank line before 'do' +nl_before_do = ignore # ignore/add/remove/force + +# Add or remove blank line after 'do/while' statement +nl_after_do = ignore # ignore/add/remove/force + +# Whether to double-space commented-entries in struct/enum +nl_ds_struct_enum_cmt = false # false/true + +# Whether to double-space before the close brace of a struct/union/enum +# (lower priority than 'eat_blanks_before_close_brace') +nl_ds_struct_enum_close_brace = false # false/true + +# Add or remove a newline around a class colon. +# Related to pos_class_colon, nl_class_init_args, and pos_comma. +nl_class_colon = ignore # ignore/add/remove/force + +# Change simple unbraced if statements into a one-liner +# 'if(b)\n i++;' => 'if(b) i++;' +nl_create_if_one_liner = false # false/true + +# Change simple unbraced for statements into a one-liner +# 'for (i=0;i<5;i++)\n foo(i);' => 'for (i=0;i<5;i++) foo(i);' +nl_create_for_one_liner = false # false/true + +# Change simple unbraced while statements into a one-liner +# 'while (i<5)\n foo(i++);' => 'while (i<5) foo(i++);' +nl_create_while_one_liner = false # false/true + +# +# Positioning options +# + +# The position of arithmetic operators in wrapped expressions +pos_arith = ignore # ignore/lead/lead_break/lead_force/trail/trail_break/trail_force + +# The position of assignment in wrapped expressions. +# Do not affect '=' followed by '{' +pos_assign = ignore # ignore/lead/lead_break/lead_force/trail/trail_break/trail_force + +# The position of boolean operators in wrapped expressions +pos_bool = ignore # ignore/lead/lead_break/lead_force/trail/trail_break/trail_force + +# The position of comparison operators in wrapped expressions +pos_compare = ignore # ignore/lead/lead_break/lead_force/trail/trail_break/trail_force + +# The position of conditional (b ? t : f) operators in wrapped expressions +pos_conditional = ignore # ignore/lead/lead_break/lead_force/trail/trail_break/trail_force + +# The position of the comma in wrapped expressions +pos_comma = ignore # ignore/lead/lead_break/lead_force/trail/trail_break/trail_force + +# The position of the comma in the constructor initialization list +pos_class_comma = ignore # ignore/lead/lead_break/lead_force/trail/trail_break/trail_force + +# The position of colons between constructor and member initialization +pos_class_colon = ignore # ignore/lead/lead_break/lead_force/trail/trail_break/trail_force + +# +# Line Splitting options +# + +# Try to limit code width to N number of columns +code_width = 120 # number + +# Whether to fully split long 'for' statements at semi-colons +ls_for_split_full = false # false/true + +# Whether to fully split long function protos/calls at commas +ls_func_split_full = false # false/true + +# +# Blank line options +# + +# The maximum consecutive newlines +nl_max = 2 # number + +# The number of newlines after a function prototype, if followed by another function prototype +nl_after_func_proto = 0 # number + +# The number of newlines after a function prototype, if not followed by another function prototype +nl_after_func_proto_group = 0 # number + +# The number of newlines after '}' of a multi-line function body +nl_after_func_body = 2 # number + +# The number of newlines after '}' of a single line function body +nl_after_func_body_one_liner = 2 # number + +# The minimum number of newlines before a multi-line comment. +# Doesn't apply if after a brace open or another multi-line comment. +nl_before_block_comment = 0 # number + +# The minimum number of newlines before a single-line C comment. +# Doesn't apply if after a brace open or other single-line C comments. +nl_before_c_comment = 0 # number + +# The minimum number of newlines before a CPP comment. +# Doesn't apply if after a brace open or other CPP comments. +nl_before_cpp_comment = 0 # number + +# Whether to force a newline after a multi-line comment. +nl_after_multiline_comment = false # false/true + +# The number of newlines before a 'private:', 'public:', 'protected:', 'signals:', or 'slots:' label. +# Will not change the newline count if after a brace open. +# 0 = No change. +nl_before_access_spec = 0 # number + +# The number of newlines after a 'private:', 'public:', 'protected:', 'signals:', or 'slots:' label. +# 0 = No change. +nl_after_access_spec = 0 # number + +# The number of newlines between a function def and the function comment. +# 0 = No change. +nl_comment_func_def = 0 # number + +# The number of newlines after a try-catch-finally block that isn't followed by a brace close. +# 0 = No change. +nl_after_try_catch_finally = 0 # number + +# The number of newlines before and after a property, indexer or event decl. +# 0 = No change. +nl_around_cs_property = 0 # number + +# The number of newlines between the get/set/add/remove handlers in C#. +# 0 = No change. +nl_between_get_set = 0 # number + +# Whether to remove blank lines after '{' +eat_blanks_after_open_brace = true # false/true + +# Whether to remove blank lines before '}' +eat_blanks_before_close_brace = true # false/true + +# +# Code modifying options (non-whitespace) +# + +# Add or remove braces on single-line 'do' statement +mod_full_brace_do = remove # ignore/add/remove/force + +# Add or remove braces on single-line 'for' statement +mod_full_brace_for = remove # ignore/add/remove/force + +# Add or remove braces on single-line function definitions. (Pawn) +mod_full_brace_function = ignore # ignore/add/remove/force + +# Add or remove braces on single-line 'if' statement. Will not remove the braces if they contain an 'else'. +mod_full_brace_if = ignore # ignore/add/remove/force + +# Make all if/elseif/else statements in a chain be braced or not. Overrides mod_full_brace_if. +# If any must be braced, they are all braced. If all can be unbraced, then the braces are removed. +mod_full_brace_if_chain = false # false/true + +# Don't remove braces around statements that span N newlines +mod_full_brace_nl = 0 # number + +# Add or remove braces on single-line 'while' statement +mod_full_brace_while = remove # ignore/add/remove/force + +# Add or remove braces on single-line 'using ()' statement +mod_full_brace_using = remove # ignore/add/remove/force + +# Add or remove unnecessary paren on 'return' statement +mod_paren_on_return = remove # ignore/add/remove/force + +# Whether to change optional semicolons to real semicolons +mod_pawn_semicolon = false # false/true + +# Add parens on 'while' and 'if' statement around bools +mod_full_paren_if_bool = false # false/true + +# Whether to remove superfluous semicolons +mod_remove_extra_semicolon = true # false/true + +# If a function body exceeds the specified number of newlines and doesn't have a comment after +# the close brace, a comment will be added. +mod_add_long_function_closebrace_comment = 0 # number + +# If a switch body exceeds the specified number of newlines and doesn't have a comment after +# the close brace, a comment will be added. +mod_add_long_switch_closebrace_comment = 0 # number + +# If an #ifdef body exceeds the specified number of newlines and doesn't have a comment after +# the #else, a comment will be added. +mod_add_long_ifdef_endif_comment = 0 # number + +# If an #ifdef or #else body exceeds the specified number of newlines and doesn't have a comment after +# the #endif, a comment will be added. +mod_add_long_ifdef_else_comment = 0 # number + +# If TRUE, will sort consecutive single-line 'import' statements [Java, D] +mod_sort_import = false # false/true + +# If TRUE, will sort consecutive single-line 'using' statements [C#] +mod_sort_using = false # false/true + +# If TRUE, will sort consecutive single-line '#include' statements [C/C++] and '#import' statements [Obj-C] +# This is generally a bad idea, as it may break your code. +mod_sort_include = false # false/true + +# If TRUE, it will move a 'break' that appears after a fully braced 'case' before the close brace. +mod_move_case_break = false # false/true + +# Will add or remove the braces around a fully braced case statement. +# Will only remove the braces if there are no variable declarations in the block. +mod_case_brace = ignore # ignore/add/remove/force + +# If TRUE, it will remove a void 'return;' that appears as the last statement in a function. +mod_remove_empty_return = false # false/true + +# +# Comment modifications +# + +# Try to wrap comments at cmt_width columns +cmt_width = 0 # number + +# Set the comment reflow mode (default: 0) +# 0: no reflowing (apart from the line wrapping due to cmt_width) +# 1: no touching at all +# 2: full reflow +cmt_reflow_mode = 0 # number + +# If false, disable all multi-line comment changes, including cmt_width. keyword substitution, and leading chars. +# Default is true. +cmt_indent_multi = true # false/true + +# Whether to group c-comments that look like they are in a block +cmt_c_group = false # false/true + +# Whether to put an empty '/*' on the first line of the combined c-comment +cmt_c_nl_start = false # false/true + +# Whether to put a newline before the closing '*/' of the combined c-comment +cmt_c_nl_end = false # false/true + +# Whether to group cpp-comments that look like they are in a block +cmt_cpp_group = false # false/true + +# Whether to put an empty '/*' on the first line of the combined cpp-comment +cmt_cpp_nl_start = false # false/true + +# Whether to put a newline before the closing '*/' of the combined cpp-comment +cmt_cpp_nl_end = false # false/true + +# Whether to change cpp-comments into c-comments +cmt_cpp_to_c = false # false/true + +# Whether to put a star on subsequent comment lines +cmt_star_cont = true # false/true + +# The number of spaces to insert at the start of subsequent comment lines +cmt_sp_before_star_cont = 0 # number + +# The number of spaces to insert after the star on subsequent comment lines +cmt_sp_after_star_cont = 0 # number + +# For multi-line comments with a '*' lead, remove leading spaces if the first and last lines of +# the comment are the same length. Default=True +cmt_multi_check_last = false # false/true + +# The filename that contains text to insert at the head of a file if the file doesn't start with a C/C++ comment. +# Will substitute $(filename) with the current file's name. +cmt_insert_file_header = "" # string + +# The filename that contains text to insert at the end of a file if the file doesn't end with a C/C++ comment. +# Will substitute $(filename) with the current file's name. +cmt_insert_file_footer = "" # string + +# The filename that contains text to insert before a function implementation if the function isn't preceded with a C/C++ comment. +# Will substitute $(function) with the function name and $(javaparam) with the javadoc @param and @return stuff. +# Will also substitute $(fclass) with the class name: void CFoo::Bar() { ... } +cmt_insert_func_header = "" # string + +# The filename that contains text to insert before a class if the class isn't preceded with a C/C++ comment. +# Will substitute $(class) with the class name. +cmt_insert_class_header = "" # string + +# If a preprocessor is encountered when stepping backwards from a function name, then +# this option decides whether the comment should be inserted. +# Affects cmt_insert_func_header and cmt_insert_class_header. +cmt_insert_before_preproc = false # false/true + +# +# Preprocessor options +# + +# Control indent of preprocessors inside #if blocks at brace level 0 +pp_indent = remove # ignore/add/remove/force + +# Whether to indent #if/#else/#endif at the brace level (true) or from column 1 (false) +pp_indent_at_level = false # false/true + +# If pp_indent_at_level=false, specifies the number of columns to indent per level. Default=1. +pp_indent_count = 1 # number + +# Add or remove space after # based on pp_level of #if blocks +pp_space = remove # ignore/add/remove/force + +# Sets the number of spaces added with pp_space +pp_space_count = 0 # number + +# The indent for #region and #endregion in C# and '#pragma region' in C/C++ +pp_indent_region = 0 # number + +# Whether to indent the code between #region and #endregion +pp_region_indent_code = false # false/true + +# If pp_indent_at_level=true, sets the indent for #if, #else, and #endif when not at file-level +pp_indent_if = 0 # number + +# Control whether to indent the code between #if, #else and #endif when not at file-level +pp_if_indent_code = false # false/true + +# Whether to indent '#define' at the brace level (true) or from column 1 (false) +pp_define_at_level = false # false/true + +# You can force a token to be a type with the 'type' option. +# Example: +# type myfoo1 myfoo2 +# +# You can create custom macro-based indentation using macro-open, +# macro-else and macro-close. +# Example: +# macro-open BEGIN_TEMPLATE_MESSAGE_MAP +# macro-open BEGIN_MESSAGE_MAP +# macro-close END_MESSAGE_MAP +# +# You can assign any keyword to any type with the set option. +# set func_call_user _ N_ +# +# The full syntax description of all custom definition config entries +# is shown below: +# +# define custom tokens as: +# - embed whitespace in token using '' escape character, or +# put token in quotes +# - these: ' " and ` are recognized as quote delimiters +# +# type token1 token2 token3 ... +# ^ optionally specify multiple tokens on a single line +# define def_token output_token +# ^ output_token is optional, then NULL is assumed +# macro-open token +# macro-close token +# macro-else token +# set id token1 token2 ... +# ^ optionally specify multiple tokens on a single line +# ^ id is one of the names in token_enum.h sans the CT_ prefix, +# e.g. PP_PRAGMA +# +# all tokens are separated by any mix of ',' commas, '=' equal signs +# and whitespace (space, tab) # -align_enum_equ_span = 4 # '=' in enum definition -align_nl_cont = true -align_on_tabstop = FALSE # align on tabstops -align_right_cmt_span = 3 -align_struct_init_span = 1 -align_struct_init_span = 3 # align stuff in a structure init '= { }' -align_var_def_star_style = 2 # void *foo; -align_var_struct_span = 0 -align_with_tabs = FALSE # use tabs to align diff --git a/src/acl.c b/src/acl.c index 3a9c7577..e6f31dba 100644 --- a/src/acl.c +++ b/src/acl.c @@ -44,7 +44,7 @@ static void parse_addr_cidr(const char *str, char *host, int *cidr) } else { memcpy(host, str, ret); host[ret] = '\0'; - *cidr = atoi(str + ret + 1); + *cidr = atoi(str + ret + 1); } } @@ -62,7 +62,7 @@ int init_acl(const char *path) } char line[256]; - while (!feof(f)) { + while (!feof(f)) if (fgets(line, 256, f)) { // Trim the newline int len = strlen(line); @@ -92,7 +92,6 @@ int init_acl(const char *path) } } } - } fclose(f); @@ -105,7 +104,7 @@ void free_acl(void) ipset_done(&acl_ipv6_set); } -int acl_contains_ip(const char * host) +int acl_contains_ip(const char *host) { struct cork_ip addr; int err = cork_ip_init(&addr, host); diff --git a/src/acl.h b/src/acl.h index 8643f510..52ada9c0 100644 --- a/src/acl.h +++ b/src/acl.h @@ -26,6 +26,6 @@ int init_acl(const char *path); void free_acl(void); -int acl_contains_ip(const char * ip); +int acl_contains_ip(const char *ip); #endif // _ACL_H diff --git a/src/android.c b/src/android.c index 458246be..81d914bf 100644 --- a/src/android.c +++ b/src/android.c @@ -50,14 +50,14 @@ int protect_socket(int fd) int sock; struct sockaddr_un addr; - if ( (sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { LOGE("[android] socket() failed: %s (socket fd = %d)\n", strerror(errno), sock); return -1; } // Set timeout to 1s struct timeval tv; - tv.tv_sec = 1; + tv.tv_sec = 1; tv.tv_usec = 0; setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)); @@ -97,14 +97,14 @@ int send_traffic_stat(uint64_t tx, uint64_t rx) int sock; struct sockaddr_un addr; - if ( (sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { LOGE("[android] socket() failed: %s (socket fd = %d)\n", strerror(errno), sock); return -1; } // Set timeout to 1s struct timeval tv; - tv.tv_sec = 1; + tv.tv_sec = 1; tv.tv_usec = 0; setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)); @@ -121,7 +121,7 @@ int send_traffic_stat(uint64_t tx, uint64_t rx) return -1; } - char stat[256] = {0}; + char stat[256] = { 0 }; snprintf(stat, 256, "%llu|%llu", tx, rx); size_t len = strlen(stat); diff --git a/src/cache.c b/src/cache.c index 4808ca98..18dfadf2 100644 --- a/src/cache.c +++ b/src/cache.c @@ -34,14 +34,14 @@ #endif /** Creates a new cache object - - @param dst - Where the newly allocated cache object will be stored in - - @param capacity - The maximum number of elements this cache object can hold - - @return EINVAL if dst is NULL, ENOMEM if malloc fails, 0 otherwise + * + * @param dst + * Where the newly allocated cache object will be stored in + * + * @param capacity + * The maximum number of elements this cache object can hold + * + * @return EINVAL if dst is NULL, ENOMEM if malloc fails, 0 otherwise */ int cache_create(struct cache **dst, const size_t capacity, void (*free_cb)(void *element)) @@ -57,22 +57,21 @@ int cache_create(struct cache **dst, const size_t capacity, } new->max_entries = capacity; - new->entries = NULL; - new->free_cb = free_cb; - *dst = new; + new->entries = NULL; + new->free_cb = free_cb; + *dst = new; return 0; - } /** Frees an allocated cache object - - @param cache - The cache object to free - - @param keep_data - Whether to free contained data or just delete references to it - - @return EINVAL if cache is NULL, 0 otherwise + * + * @param cache + * The cache object to free + * + * @param keep_data + * Whether to free contained data or just delete references to it + * + * @return EINVAL if cache is NULL, 0 otherwise */ int cache_delete(struct cache *cache, int keep_data) { @@ -103,17 +102,17 @@ int cache_delete(struct cache *cache, int keep_data) } /** Removes a cache entry - - @param cache - The cache object - - @param key - The key of the entry to remove - - @param key_len - The length of key - - @return EINVAL if cache is NULL, 0 otherwise + * + * @param cache + * The cache object + * + * @param key + * The key of the entry to remove + * + * @param key_len + * The length of key + * + * @return EINVAL if cache is NULL, 0 otherwise */ int cache_remove(struct cache *cache, char *key, size_t key_len) { @@ -142,29 +141,29 @@ int cache_remove(struct cache *cache, char *key, size_t key_len) } /** Checks if a given key is in the cache - - @param cache - The cache object - - @param key - The key to look-up - - @param key_len - The length of key - - @param result - Where to store the result if key is found. - - A warning: Even though result is just a pointer, - you have to call this function with a **ptr, - otherwise this will blow up in your face. - - @return EINVAL if cache is NULL, 0 otherwise + * + * @param cache + * The cache object + * + * @param key + * The key to look-up + * + * @param key_len + * The length of key + * + * @param result + * Where to store the result if key is found. + * + * A warning: Even though result is just a pointer, + * you have to call this function with a **ptr, + * otherwise this will blow up in your face. + * + * @return EINVAL if cache is NULL, 0 otherwise */ int cache_lookup(struct cache *cache, char *key, size_t key_len, void *result) { struct cache_entry *tmp = NULL; - char **dirty_hack = result; + char **dirty_hack = result; if (!cache || !key || !result) { return EINVAL; @@ -203,24 +202,24 @@ int cache_key_exist(struct cache *cache, char *key, size_t key_len) } /** Inserts a given pair into the cache - - @param cache - The cache object - - @param key - The key that identifies - - @param key_len - The length of key - - @param data - Data associated with - - @return EINVAL if cache is NULL, ENOMEM if malloc fails, 0 otherwise + * + * @param cache + * The cache object + * + * @param key + * The key that identifies + * + * @param key_len + * The length of key + * + * @param data + * Data associated with + * + * @return EINVAL if cache is NULL, ENOMEM if malloc fails, 0 otherwise */ int cache_insert(struct cache *cache, char *key, size_t key_len, void *data) { - struct cache_entry *entry = NULL; + struct cache_entry *entry = NULL; struct cache_entry *tmp_entry = NULL; if (!cache) { diff --git a/src/cache.h b/src/cache.h index 5f24b993..e00be960 100644 --- a/src/cache.h +++ b/src/cache.h @@ -48,7 +48,6 @@ struct cache { void (*free_cb) (void *element); /**array = malloc(capacity); + memset(ptr, 0, sizeof(buffer_t)); + ptr->array = malloc(capacity); ptr->capacity = capacity; return capacity; } @@ -221,8 +215,8 @@ int brealloc(buffer_t *ptr, size_t len, size_t capacity) void bfree(buffer_t *ptr) { - ptr->idx = 0; - ptr->len = 0; + ptr->idx = 0; + ptr->len = 0; ptr->capacity = 0; if (ptr->array != NULL) { free(ptr->array); @@ -314,7 +308,7 @@ static void merge_sort(uint8_t array[], int length, llength = length - middle; - left = array; + left = array; right = array + llength; merge_sort(left, llength, salt, key); @@ -359,20 +353,16 @@ void enc_table_init(const char *pass) digest = enc_md5((const uint8_t *)pass, strlen(pass), NULL); - for (i = 0; i < 8; i++) { + for (i = 0; i < 8; i++) key += OFFSET_ROL(digest, i); - } - for (i = 0; i < 256; ++i) { + for (i = 0; i < 256; ++i) enc_table[i] = i; - } - for (i = 1; i < 1024; ++i) { + for (i = 1; i < 1024; ++i) merge_sort(enc_table, 256, i, key); - } - for (i = 0; i < 256; ++i) { + for (i = 0; i < 256; ++i) // gen decrypt table from encrypt table dec_table[enc_table[i]] = i; - } } int cipher_iv_size(const cipher_kt_t *cipher) @@ -436,8 +426,8 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md, int rv; nkey = cipher_key_size(cipher); - niv = cipher_iv_size(cipher); - rv = nkey; + niv = cipher_iv_size(cipher); + rv = nkey; if (pass == NULL) { return nkey; } @@ -447,8 +437,8 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md, return 0; } addmd = 0; - mds = md_get_size(md); - for (;; ) { + mds = md_get_size(md); + for (;;) { int error; do { error = 1; @@ -478,7 +468,7 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md, i = 0; if (nkey) { - for (;; ) { + for (;;) { if (nkey == 0) { break; } @@ -493,7 +483,7 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md, } } if (niv && (i != mds)) { - for (;; ) { + for (;;) { if (niv == 0) { break; } @@ -518,26 +508,26 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md, /* * * Generic message digest context. - - typedef struct { - Information about the associated message digest - const mbedtls_md_info_t *md_info; - - Digest-specific context - void *md_ctx; - - HMAC part of the context - void *hmac_ctx; - } mbedtls_md_context_t; // mbedtls 2.0.0 - - typedef struct { - Information about the associated message digest - const md_info_t *md_info; - - Digest-specific context - void *md_ctx; - } md_context_t; //polarssl 1.3 - + * + * typedef struct { + * Information about the associated message digest + * const mbedtls_md_info_t *md_info; + * + * Digest-specific context + * void *md_ctx; + * + * HMAC part of the context + * void *hmac_ctx; + * } mbedtls_md_context_t; // mbedtls 2.0.0 + * + * typedef struct { + * Information about the associated message digest + * const md_info_t *md_info; + * + * Digest-specific context + * void *md_ctx; + * } md_context_t; //polarssl 1.3 + * */ // NOTE: different struct body, initialize new param hmac 0 to disable HMAC mbedtls_md_context_t c; @@ -550,8 +540,8 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md, int rv; nkey = cipher_key_size(cipher); - niv = cipher_iv_size(cipher); - rv = nkey; + niv = cipher_iv_size(cipher); + rv = nkey; if (pass == NULL) { return nkey; } @@ -564,8 +554,8 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md, return 0; } addmd = 0; - mds = mbedtls_md_get_size(md); - for (;; ) { + mds = mbedtls_md_get_size(md); + for (;;) { int error; do { error = 1; @@ -588,14 +578,14 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md, error = 0; } while (0); if (error) { - mbedtls_md_free(&c); //md_free_ctx deprecated, Use mbedtls_md_free() instead + mbedtls_md_free(&c); // md_free_ctx deprecated, Use mbedtls_md_free() instead memset(md_buf, 0, MAX_MD_SIZE); return 0; } i = 0; if (nkey) { - for (;; ) { + for (;;) { if (nkey == 0) { break; } @@ -610,7 +600,7 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md, } } if (niv && (i != mds)) { - for (;; ) { + for (;;) { if (niv == 0) { break; } @@ -628,7 +618,7 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md, break; } } - mbedtls_md_free(&c); //NOTE: md_free_ctx deprecated, Use mbedtls_md_free() instead + mbedtls_md_free(&c); // NOTE: md_free_ctx deprecated, Use mbedtls_md_free() instead memset(md_buf, 0, MAX_MD_SIZE); return rv; #endif @@ -642,7 +632,7 @@ int rand_bytes(uint8_t *output, int len) static entropy_context ec = {}; static ctr_drbg_context cd_ctx = {}; static unsigned char rand_initialised = 0; - const size_t blen = min(len, CTR_DRBG_MAX_REQUEST); + const size_t blen = min(len, CTR_DRBG_MAX_REQUEST); if (!rand_initialised) { #ifdef _WIN32 @@ -694,7 +684,7 @@ int rand_bytes(uint8_t *output, int len) return 0; } output += blen; - len -= blen; + len -= blen; } return 1; #elif defined(USE_CRYPTO_MBEDTLS) @@ -702,7 +692,7 @@ int rand_bytes(uint8_t *output, int len) // XXX: ctr_drbg_context changed, [if defined(MBEDTLS_THREADING_C) mbedtls_threading_mutex_t mutex;] static mbedtls_ctr_drbg_context cd_ctx = {}; static unsigned char rand_initialised = 0; - const size_t blen = min(len, MBEDTLS_CTR_DRBG_MAX_REQUEST); + const size_t blen = min(len, MBEDTLS_CTR_DRBG_MAX_REQUEST); if (!rand_initialised) { #ifdef _WIN32 @@ -754,7 +744,7 @@ int rand_bytes(uint8_t *output, int len) return 0; } output += blen; - len -= blen; + len -= blen; } return 1; #endif @@ -829,23 +819,23 @@ void cipher_context_init(cipher_ctx_t *ctx, int method, int enc) #if defined(USE_CRYPTO_APPLECC) cipher_cc_t *cc = &ctx->cc; cc->cryptor = NULL; - cc->cipher = supported_ciphers_applecc[method]; + cc->cipher = supported_ciphers_applecc[method]; if (cc->cipher == kCCAlgorithmInvalid) { cc->valid = kCCContextInvalid; } else { cc->valid = kCCContextValid; if (cc->cipher == kCCAlgorithmRC4) { - cc->mode = kCCModeRC4; + cc->mode = kCCModeRC4; cc->padding = ccNoPadding; } else { - cc->mode = kCCModeCFB; + cc->mode = kCCModeCFB; cc->padding = ccPKCS7Padding; } return; } #endif - cipher_evp_t *evp = &ctx->evp; + cipher_evp_t *evp = &ctx->evp; const cipher_kt_t *cipher = get_cipher_type(method); #if defined(USE_CRYPTO_OPENSSL) if (cipher == NULL) { @@ -912,7 +902,7 @@ void cipher_context_set_iv(cipher_ctx_t *ctx, uint8_t *iv, size_t iv_len, memcpy(key_iv, enc_key, 16); memcpy(key_iv + 16, iv, 16); true_key = enc_md5(key_iv, 32, NULL); - iv_len = 0; + iv_len = 0; } else { true_key = enc_key; } @@ -922,7 +912,7 @@ void cipher_context_set_iv(cipher_ctx_t *ctx, uint8_t *iv, size_t iv_len, if (cc->valid == kCCContextValid) { memcpy(cc->iv, iv, iv_len); memcpy(cc->key, true_key, enc_key_len); - cc->iv_len = iv_len; + cc->iv_len = iv_len; cc->key_len = enc_key_len; cc->encrypt = enc ? kCCEncrypt : kCCDecrypt; if (cc->cryptor != NULL) { @@ -1023,10 +1013,10 @@ void cipher_context_release(cipher_ctx_t *ctx) #if defined(USE_CRYPTO_OPENSSL) EVP_CIPHER_CTX_cleanup(evp); #elif defined(USE_CRYPTO_POLARSSL) -//NOTE: cipher_free_ctx deprecated in PolarSSL 1.3.11 +// NOTE: cipher_free_ctx deprecated in PolarSSL 1.3.11 cipher_free_ctx(evp); #elif defined(USE_CRYPTO_MBEDTLS) -//NOTE: cipher_free_ctx deprecated +// NOTE: cipher_free_ctx deprecated mbedtls_cipher_free(evp); #endif } @@ -1086,14 +1076,15 @@ int ss_onetimeauth_verify(buffer_t *buf, uint8_t *iv) uint8_t auth_key[MAX_IV_LENGTH + MAX_KEY_LENGTH]; memcpy(auth_key, iv, enc_iv_len); memcpy(auth_key + enc_iv_len, enc_key, enc_key_len); + size_t len = buf->len - ONETIMEAUTH_BYTES; #if defined(USE_CRYPTO_OPENSSL) - HMAC(EVP_sha1(), auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, buf->len, hash, NULL); + HMAC(EVP_sha1(), auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, len, hash, NULL); #else - ss_sha1_hmac(auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, buf->len, hash); + ss_sha1_hmac(auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, len, hash); #endif - return memcmp(buf->array + buf->len - ONETIMEAUTH_BYTES, hash, ONETIMEAUTH_BYTES); + return memcmp(buf->array + len, hash, ONETIMEAUTH_BYTES); } int ss_encrypt_all(buffer_t *plain, int method, int auth) @@ -1102,34 +1093,34 @@ int ss_encrypt_all(buffer_t *plain, int method, int auth) cipher_ctx_t evp; cipher_context_init(&evp, method, 1); - size_t p_len = plain->len, c_len = plain->len; size_t iv_len = enc_iv_len; - int err = 1; + int err = 1; - static buffer_t tmp = {0}; - brealloc(&tmp, iv_len + c_len, plain->capacity); + static buffer_t tmp = { 0 }; + brealloc(&tmp, iv_len + plain->len, plain->capacity); buffer_t *cipher = &tmp; + cipher->len = plain->len; uint8_t iv[MAX_IV_LENGTH]; rand_bytes(iv, iv_len); cipher_context_set_iv(&evp, iv, iv_len, 1); - memcpy(cipher, iv, iv_len); + memcpy(cipher->array, iv, iv_len); if (auth) { ss_onetimeauth(plain, iv); - p_len = c_len = p_len + ONETIMEAUTH_BYTES; + cipher->len = plain->len; } if (method >= SALSA20) { - crypto_stream_xor_ic((uint8_t *)(cipher + iv_len), - (const uint8_t *)plain->array, (uint64_t)(p_len), + crypto_stream_xor_ic((uint8_t *)(cipher->array + iv_len), + (const uint8_t *)plain->array, (uint64_t)(plain->len), (const uint8_t *)iv, 0, enc_key, method); } else { - err = cipher_context_update(&evp, (uint8_t *)(cipher + iv_len), - &c_len, (const uint8_t *)plain->array, - p_len); + err = cipher_context_update(&evp, (uint8_t *)(cipher->array + iv_len), + &cipher->len, (const uint8_t *)plain->array, + plain->len); } if (!err) { @@ -1139,20 +1130,20 @@ int ss_encrypt_all(buffer_t *plain, int method, int auth) } #ifdef DEBUG - dump("PLAIN", plain->array, *len); - dump("CIPHER", cipher->array + iv_len, c_len); + dump("PLAIN", plain->array, plain->len); + dump("CIPHER", cipher->array + iv_len, cipher->len); #endif cipher_context_release(&evp); - brealloc(plain, iv_len + c_len, plain->capacity); - memcpy(plain, cipher, iv_len + c_len); - plain->len = iv_len + c_len; + brealloc(plain, iv_len + cipher->len, plain->capacity); + memcpy(plain->array, cipher->array, iv_len + cipher->len); + plain->len = iv_len + cipher->len; return 0; } else { char *begin = plain->array; - char *ptr = plain->array; + char *ptr = plain->array; while (ptr < begin + plain->len) { *ptr = (char)enc_table[(uint8_t)*ptr]; ptr++; @@ -1164,69 +1155,68 @@ int ss_encrypt_all(buffer_t *plain, int method, int auth) int ss_encrypt(buffer_t *plain, enc_ctx_t *ctx) { if (ctx != NULL) { - static buffer_t tmp = {0}; + static buffer_t tmp = { 0 }; - int err = 1; + int err = 1; size_t iv_len = 0; - size_t p_len = plain->len, c_len = plain->len; if (!ctx->init) { iv_len = enc_iv_len; } - brealloc(&tmp, iv_len + c_len, plain->capacity); - + brealloc(&tmp, iv_len + plain->len, plain->capacity); buffer_t *cipher = &tmp; + cipher->len = plain->len; if (!ctx->init) { cipher_context_set_iv(&ctx->evp, ctx->evp.iv, iv_len, 1); memcpy(cipher->array, ctx->evp.iv, iv_len); ctx->counter = 0; - ctx->init = 1; + ctx->init = 1; } if (enc_method >= SALSA20) { int padding = ctx->counter % SODIUM_BLOCK_SIZE; - brealloc(cipher, iv_len + (padding + c_len) * 2, cipher->capacity); + brealloc(cipher, iv_len + (padding + cipher->len) * 2, cipher->capacity); if (padding) { - brealloc(plain, p_len + padding, plain->capacity); - memmove(plain->array + padding, plain->array, p_len); + brealloc(plain, plain->len + padding, plain->capacity); + memmove(plain->array + padding, plain->array, plain->len); memset(plain->array, 0, padding); } crypto_stream_xor_ic((uint8_t *)(cipher->array + iv_len), (const uint8_t *)plain->array, - (uint64_t)(p_len + padding), + (uint64_t)(plain->len + padding), (const uint8_t *)ctx->evp.iv, ctx->counter / SODIUM_BLOCK_SIZE, enc_key, enc_method); - ctx->counter += p_len; + ctx->counter += plain->len; if (padding) { memmove(cipher->array + iv_len, - cipher->array + iv_len + padding, c_len); + cipher->array + iv_len + padding, cipher->len); } } else { err = cipher_context_update(&ctx->evp, (uint8_t *)(cipher->array + iv_len), - &c_len, (const uint8_t *)plain->array, - p_len); + &cipher->len, (const uint8_t *)plain->array, + plain->len); if (!err) { return -1; } } #ifdef DEBUG - dump("PLAIN", plain->array, p_len); - dump("CIPHER", cipher->array + iv_len, c_len); + dump("PLAIN", plain->array, plain->len); + dump("CIPHER", cipher->array + iv_len, cipher->len); #endif - brealloc(plain, iv_len + c_len, plain->capacity); - memcpy(plain->array, cipher->array, iv_len + c_len); - plain->len = iv_len + c_len; + brealloc(plain, iv_len + cipher->len, plain->capacity); + memcpy(plain->array, cipher->array, iv_len + cipher->len); + plain->len = iv_len + cipher->len; return 0; } else { char *begin = plain->array; - char *ptr = plain->array; + char *ptr = plain->array; while (ptr < begin + plain->len) { *ptr = (char)enc_table[(uint8_t)*ptr]; ptr++; @@ -1239,41 +1229,40 @@ int ss_decrypt_all(buffer_t *cipher, int method, int auth) { if (method > TABLE) { size_t iv_len = enc_iv_len; - size_t c_len = cipher->len; - size_t p_len = cipher->len - iv_len; - int ret = 1; + int ret = 1; - if (c_len <= iv_len) { + if (cipher->len <= iv_len) { return -1; } cipher_ctx_t evp; cipher_context_init(&evp, method, 0); - static buffer_t tmp = {0}; + static buffer_t tmp = { 0 }; brealloc(&tmp, cipher->len, cipher->capacity); buffer_t *plain = &tmp; + plain->len = cipher->len; uint8_t iv[MAX_IV_LENGTH]; - memcpy(iv, cipher, iv_len); + memcpy(iv, cipher->array, iv_len); cipher_context_set_iv(&evp, iv, iv_len, 0); if (method >= SALSA20) { crypto_stream_xor_ic((uint8_t *)plain->array, (const uint8_t *)(cipher->array + iv_len), - (uint64_t)(c_len - iv_len), + (uint64_t)(cipher->len - iv_len), (const uint8_t *)iv, 0, enc_key, method); } else { - ret = cipher_context_update(&evp, (uint8_t *)plain->array, &p_len, + ret = cipher_context_update(&evp, (uint8_t *)plain->array, &plain->len, (const uint8_t *)(cipher->array + iv_len), - c_len - iv_len); + cipher->len - iv_len); } if (auth || (plain->array[0] & ONETIMEAUTH_FLAG)) { - if (p_len > ONETIMEAUTH_BYTES) { + if (plain->len > ONETIMEAUTH_BYTES) { ret = !ss_onetimeauth_verify(plain, iv); if (ret) { - p_len -= ONETIMEAUTH_BYTES; + plain->len -= ONETIMEAUTH_BYTES; } } else { ret = 0; @@ -1287,20 +1276,20 @@ int ss_decrypt_all(buffer_t *cipher, int method, int auth) } #ifdef DEBUG - dump("PLAIN", plain->array, p_len); - dump("CIPHER", cipher->array + iv_len, c_len - iv_len); + dump("PLAIN", plain->array, plain->len); + dump("CIPHER", cipher->array + iv_len, cipher->len - iv_len); #endif cipher_context_release(&evp); - brealloc(cipher, p_len, plain->capacity); - memcpy(cipher, plain, p_len); - cipher->len = p_len; + brealloc(cipher, plain->len, plain->capacity); + memcpy(cipher->array, plain->array, plain->len); + cipher->len = plain->len; return 0; } else { char *begin = cipher->array; - char *ptr = cipher->array; + char *ptr = cipher->array; while (ptr < begin + cipher->len) { *ptr = (char)dec_table[(uint8_t)*ptr]; ptr++; @@ -1312,25 +1301,24 @@ int ss_decrypt_all(buffer_t *cipher, int method, int auth) int ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx) { if (ctx != NULL) { - static buffer_t tmp = {0}; + static buffer_t tmp = { 0 }; - size_t c_len = cipher->len; - size_t p_len = cipher->len; size_t iv_len = 0; - int err = 1; + int err = 1; - brealloc(&tmp, p_len, cipher->capacity); + brealloc(&tmp, cipher->len, cipher->capacity); buffer_t *plain = &tmp; + plain->len = cipher->len; if (!ctx->init) { uint8_t iv[MAX_IV_LENGTH]; - iv_len = enc_iv_len; - p_len -= iv_len; + iv_len = enc_iv_len; + plain->len -= iv_len; memcpy(iv, cipher->array, iv_len); cipher_context_set_iv(&ctx->evp, iv, iv_len, 0); ctx->counter = 0; - ctx->init = 1; + ctx->init = 1; if (enc_method >= RC4_MD5) { if (cache_key_exist(iv_cache, (char *)iv, iv_len)) { @@ -1344,28 +1332,28 @@ int ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx) if (enc_method >= SALSA20) { int padding = ctx->counter % SODIUM_BLOCK_SIZE; - brealloc(plain, (p_len + padding) * 2, plain->capacity); + brealloc(plain, (plain->len + padding) * 2, plain->capacity); if (padding) { - brealloc(cipher, c_len + padding, cipher->capacity); + brealloc(cipher, cipher->len + padding, cipher->capacity); memmove(cipher->array + iv_len + padding, cipher->array + iv_len, - c_len - iv_len); + cipher->len - iv_len); memset(cipher->array + iv_len, 0, padding); } crypto_stream_xor_ic((uint8_t *)plain->array, (const uint8_t *)(cipher->array + iv_len), - (uint64_t)(c_len - iv_len + padding), + (uint64_t)(cipher->len - iv_len + padding), (const uint8_t *)ctx->evp.iv, ctx->counter / SODIUM_BLOCK_SIZE, enc_key, enc_method); - ctx->counter += c_len - iv_len; + ctx->counter += cipher->len - iv_len; if (padding) { - memmove(plain->array, plain->array + padding, p_len); + memmove(plain->array, plain->array + padding, plain->len); } } else { - err = cipher_context_update(&ctx->evp, (uint8_t *)plain->array, &p_len, - (const uint8_t *)(cipher + iv_len), - c_len - iv_len); + err = cipher_context_update(&ctx->evp, (uint8_t *)plain->array, &plain->len, + (const uint8_t *)(cipher->array + iv_len), + cipher->len - iv_len); } if (!err) { @@ -1374,18 +1362,18 @@ int ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx) } #ifdef DEBUG - dump("PLAIN", plain->array, p_len); - dump("CIPHER", cipher->array + iv_len, c_len - iv_len); + dump("PLAIN", plain->array, plain->len); + dump("CIPHER", cipher->array + iv_len, cipher->len - iv_len); #endif - brealloc(cipher, p_len, cipher->capacity); - memcpy(cipher->array, plain->array, p_len); - cipher->len = p_len; + brealloc(cipher, plain->len, cipher->capacity); + memcpy(cipher->array, plain->array, plain->len); + cipher->len = plain->len; return 0; } else { char *begin = cipher->array; - char *ptr = cipher->array; + char *ptr = cipher->array; while (ptr < begin + cipher->len) { *ptr = (char)dec_table[(uint8_t)*ptr]; ptr++; @@ -1431,18 +1419,18 @@ void enc_key_init(int method, const char *pass) cipher = (cipher_kt_t *)&cipher_info; #if defined(USE_CRYPTO_OPENSSL) cipher->key_len = supported_ciphers_key_size[method]; - cipher->iv_len = supported_ciphers_iv_size[method]; + cipher->iv_len = supported_ciphers_iv_size[method]; #endif #if defined(USE_CRYPTO_POLARSSL) - cipher->base = NULL; + cipher->base = NULL; cipher->key_length = supported_ciphers_key_size[method] * 8; - cipher->iv_size = supported_ciphers_iv_size[method]; + cipher->iv_size = supported_ciphers_iv_size[method]; #endif #if defined(USE_CRYPTO_MBEDTLS) // XXX: key_length changed to key_bitlen in mbed TLS 2.0.0 - cipher->base = NULL; + cipher->base = NULL; cipher->key_bitlen = supported_ciphers_key_size[method] * 8; - cipher->iv_size = supported_ciphers_iv_size[method]; + cipher->iv_size = supported_ciphers_iv_size[method]; #endif } else { cipher = (cipher_kt_t *)get_cipher_type(method); @@ -1452,20 +1440,20 @@ void enc_key_init(int method, const char *pass) do { #if defined(USE_CRYPTO_POLARSSL) && defined(USE_CRYPTO_APPLECC) if (supported_ciphers_applecc[method] != kCCAlgorithmInvalid) { - cipher_info.base = NULL; + cipher_info.base = NULL; cipher_info.key_length = supported_ciphers_key_size[method] * 8; - cipher_info.iv_size = supported_ciphers_iv_size[method]; - cipher = (cipher_kt_t *)&cipher_info; + cipher_info.iv_size = supported_ciphers_iv_size[method]; + cipher = (cipher_kt_t *)&cipher_info; break; } #endif #if defined(USE_CRYPTO_MBEDTLS) && defined(USE_CRYPTO_APPLECC) // XXX: key_length changed to key_bitlen in mbed TLS 2.0.0 if (supported_ciphers_applecc[method] != kCCAlgorithmInvalid) { - cipher_info.base = NULL; + cipher_info.base = NULL; cipher_info.key_bitlen = supported_ciphers_key_size[method] * 8; - cipher_info.iv_size = supported_ciphers_iv_size[method]; - cipher = (cipher_kt_t *)&cipher_info; + cipher_info.iv_size = supported_ciphers_iv_size[method]; + cipher = (cipher_kt_t *)&cipher_info; break; } #endif @@ -1496,11 +1484,10 @@ int enc_init(const char *pass, const char *method) { int m = TABLE; if (method != NULL) { - for (m = TABLE; m < CIPHER_NUM; m++) { + for (m = TABLE; m < CIPHER_NUM; m++) if (strcmp(method, supported_ciphers[m]) == 0) { break; } - } if (m >= CIPHER_NUM) { LOGE("Invalid cipher name: %s, use table instead", method); m = TABLE; @@ -1517,14 +1504,13 @@ int enc_init(const char *pass, const char *method) int ss_check_hash(buffer_t *buf, chunk_t *chunk, enc_ctx_t *ctx) { int i, j, k; - ssize_t blen = buf->len; + ssize_t blen = buf->len; uint32_t cidx = chunk->idx; - brealloc(chunk->buf, blen, buf->capacity); + brealloc(chunk->buf, chunk->len + blen, buf->capacity); brealloc(buf, chunk->len + blen, buf->capacity); for (i = 0, j = 0, k = 0; i < blen; i++) { - chunk->buf->array[cidx++] = buf->array[k++]; if (cidx == CLEN_BYTES) { @@ -1558,21 +1544,21 @@ int ss_check_hash(buffer_t *buf, chunk_t *chunk, enc_ctx_t *ctx) memcpy(buf->array + j, chunk->buf->array + AUTH_BYTES, chunk->len); // Reset the base offset - j += chunk->len; - k = j; + j += chunk->len; + k = j; cidx = 0; chunk->counter++; } } - buf->len = j; + buf->len = j; chunk->idx = cidx; return 1; } int ss_gen_hash(buffer_t *buf, uint32_t *counter, enc_ctx_t *ctx) { - ssize_t blen = buf->len; + ssize_t blen = buf->len; uint16_t chunk_len = htons((uint16_t)blen); uint8_t hash[ONETIMEAUTH_BYTES * 2]; uint8_t key[MAX_IV_LENGTH + sizeof(uint32_t)]; diff --git a/src/encrypt.h b/src/encrypt.h index 456b1fc0..63f8c3c1 100644 --- a/src/encrypt.h +++ b/src/encrypt.h @@ -140,7 +140,6 @@ typedef struct { #define SALSA20 15 #define CHACHA20 16 - #define ONETIMEAUTH_FLAG 0x10 #define ADDRTYPE_MASK 0xF diff --git a/src/hmac-sha1.c b/src/hmac-sha1.c index 153b2e63..71d169fc 100644 --- a/src/hmac-sha1.c +++ b/src/hmac-sha1.c @@ -1,4 +1,3 @@ - /** * \file sha1.c * @@ -29,11 +28,10 @@ #include #include "hmac-sha1.h" - /* * SHA-1 context setup */ -void ss_sha1_starts( ss_sha1_context *ctx ) +void ss_sha1_starts(ss_sha1_context *ctx) { ctx->total[0] = 0; ctx->total[1] = 0; @@ -45,34 +43,34 @@ void ss_sha1_starts( ss_sha1_context *ctx ) ctx->state[4] = 0xC3D2E1F0; } -void ss_sha1_process( ss_sha1_context *ctx, const unsigned char data[64] ) +void ss_sha1_process(ss_sha1_context *ctx, const unsigned char data[64]) { uint32_t temp, W[16], A, B, C, D, E; - GET_UINT32_BE( W[ 0], data, 0 ); - GET_UINT32_BE( W[ 1], data, 4 ); - GET_UINT32_BE( W[ 2], data, 8 ); - GET_UINT32_BE( W[ 3], data, 12 ); - GET_UINT32_BE( W[ 4], data, 16 ); - GET_UINT32_BE( W[ 5], data, 20 ); - GET_UINT32_BE( W[ 6], data, 24 ); - GET_UINT32_BE( W[ 7], data, 28 ); - GET_UINT32_BE( W[ 8], data, 32 ); - GET_UINT32_BE( W[ 9], data, 36 ); - GET_UINT32_BE( W[10], data, 40 ); - GET_UINT32_BE( W[11], data, 44 ); - GET_UINT32_BE( W[12], data, 48 ); - GET_UINT32_BE( W[13], data, 52 ); - GET_UINT32_BE( W[14], data, 56 ); - GET_UINT32_BE( W[15], data, 60 ); + GET_UINT32_BE(W[0], data, 0); + GET_UINT32_BE(W[1], data, 4); + GET_UINT32_BE(W[2], data, 8); + GET_UINT32_BE(W[3], data, 12); + GET_UINT32_BE(W[4], data, 16); + GET_UINT32_BE(W[5], data, 20); + GET_UINT32_BE(W[6], data, 24); + GET_UINT32_BE(W[7], data, 28); + GET_UINT32_BE(W[8], data, 32); + GET_UINT32_BE(W[9], data, 36); + GET_UINT32_BE(W[10], data, 40); + GET_UINT32_BE(W[11], data, 44); + GET_UINT32_BE(W[12], data, 48); + GET_UINT32_BE(W[13], data, 52); + GET_UINT32_BE(W[14], data, 56); + GET_UINT32_BE(W[15], data, 60); #define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) #define R(t) \ ( \ temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \ - W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \ - ( W[t & 0x0F] = S(temp, 1) ) \ + W[(t - 14) & 0x0F] ^ W[t & 0x0F], \ + (W[t & 0x0F] = S(temp, 1)) \ ) #define P(a, b, c, d, e, x) \ @@ -89,26 +87,26 @@ void ss_sha1_process( ss_sha1_context *ctx, const unsigned char data[64] ) #define F(x, y, z) (z ^ (x & (y ^ z))) #define K 0x5A827999 - P( A, B, C, D, E, W[0] ); - P( E, A, B, C, D, W[1] ); - P( D, E, A, B, C, W[2] ); - P( C, D, E, A, B, W[3] ); - P( B, C, D, E, A, W[4] ); - P( A, B, C, D, E, W[5] ); - P( E, A, B, C, D, W[6] ); - P( D, E, A, B, C, W[7] ); - P( C, D, E, A, B, W[8] ); - P( B, C, D, E, A, W[9] ); - P( A, B, C, D, E, W[10] ); - P( E, A, B, C, D, W[11] ); - P( D, E, A, B, C, W[12] ); - P( C, D, E, A, B, W[13] ); - P( B, C, D, E, A, W[14] ); - P( A, B, C, D, E, W[15] ); - P( E, A, B, C, D, R(16) ); - P( D, E, A, B, C, R(17) ); - P( C, D, E, A, B, R(18) ); - P( B, C, D, E, A, R(19) ); + P(A, B, C, D, E, W[0]); + P(E, A, B, C, D, W[1]); + P(D, E, A, B, C, W[2]); + P(C, D, E, A, B, W[3]); + P(B, C, D, E, A, W[4]); + P(A, B, C, D, E, W[5]); + P(E, A, B, C, D, W[6]); + P(D, E, A, B, C, W[7]); + P(C, D, E, A, B, W[8]); + P(B, C, D, E, A, W[9]); + P(A, B, C, D, E, W[10]); + P(E, A, B, C, D, W[11]); + P(D, E, A, B, C, W[12]); + P(C, D, E, A, B, W[13]); + P(B, C, D, E, A, W[14]); + P(A, B, C, D, E, W[15]); + P(E, A, B, C, D, R(16)); + P(D, E, A, B, C, R(17)); + P(C, D, E, A, B, R(18)); + P(B, C, D, E, A, R(19)); #undef K #undef F @@ -116,26 +114,26 @@ void ss_sha1_process( ss_sha1_context *ctx, const unsigned char data[64] ) #define F(x, y, z) (x ^ y ^ z) #define K 0x6ED9EBA1 - P( A, B, C, D, E, R(20) ); - P( E, A, B, C, D, R(21) ); - P( D, E, A, B, C, R(22) ); - P( C, D, E, A, B, R(23) ); - P( B, C, D, E, A, R(24) ); - P( A, B, C, D, E, R(25) ); - P( E, A, B, C, D, R(26) ); - P( D, E, A, B, C, R(27) ); - P( C, D, E, A, B, R(28) ); - P( B, C, D, E, A, R(29) ); - P( A, B, C, D, E, R(30) ); - P( E, A, B, C, D, R(31) ); - P( D, E, A, B, C, R(32) ); - P( C, D, E, A, B, R(33) ); - P( B, C, D, E, A, R(34) ); - P( A, B, C, D, E, R(35) ); - P( E, A, B, C, D, R(36) ); - P( D, E, A, B, C, R(37) ); - P( C, D, E, A, B, R(38) ); - P( B, C, D, E, A, R(39) ); + P(A, B, C, D, E, R(20)); + P(E, A, B, C, D, R(21)); + P(D, E, A, B, C, R(22)); + P(C, D, E, A, B, R(23)); + P(B, C, D, E, A, R(24)); + P(A, B, C, D, E, R(25)); + P(E, A, B, C, D, R(26)); + P(D, E, A, B, C, R(27)); + P(C, D, E, A, B, R(28)); + P(B, C, D, E, A, R(29)); + P(A, B, C, D, E, R(30)); + P(E, A, B, C, D, R(31)); + P(D, E, A, B, C, R(32)); + P(C, D, E, A, B, R(33)); + P(B, C, D, E, A, R(34)); + P(A, B, C, D, E, R(35)); + P(E, A, B, C, D, R(36)); + P(D, E, A, B, C, R(37)); + P(C, D, E, A, B, R(38)); + P(B, C, D, E, A, R(39)); #undef K #undef F @@ -143,26 +141,26 @@ void ss_sha1_process( ss_sha1_context *ctx, const unsigned char data[64] ) #define F(x, y, z) ((x & y) | (z & (x | y))) #define K 0x8F1BBCDC - P( A, B, C, D, E, R(40) ); - P( E, A, B, C, D, R(41) ); - P( D, E, A, B, C, R(42) ); - P( C, D, E, A, B, R(43) ); - P( B, C, D, E, A, R(44) ); - P( A, B, C, D, E, R(45) ); - P( E, A, B, C, D, R(46) ); - P( D, E, A, B, C, R(47) ); - P( C, D, E, A, B, R(48) ); - P( B, C, D, E, A, R(49) ); - P( A, B, C, D, E, R(50) ); - P( E, A, B, C, D, R(51) ); - P( D, E, A, B, C, R(52) ); - P( C, D, E, A, B, R(53) ); - P( B, C, D, E, A, R(54) ); - P( A, B, C, D, E, R(55) ); - P( E, A, B, C, D, R(56) ); - P( D, E, A, B, C, R(57) ); - P( C, D, E, A, B, R(58) ); - P( B, C, D, E, A, R(59) ); + P(A, B, C, D, E, R(40)); + P(E, A, B, C, D, R(41)); + P(D, E, A, B, C, R(42)); + P(C, D, E, A, B, R(43)); + P(B, C, D, E, A, R(44)); + P(A, B, C, D, E, R(45)); + P(E, A, B, C, D, R(46)); + P(D, E, A, B, C, R(47)); + P(C, D, E, A, B, R(48)); + P(B, C, D, E, A, R(49)); + P(A, B, C, D, E, R(50)); + P(E, A, B, C, D, R(51)); + P(D, E, A, B, C, R(52)); + P(C, D, E, A, B, R(53)); + P(B, C, D, E, A, R(54)); + P(A, B, C, D, E, R(55)); + P(E, A, B, C, D, R(56)); + P(D, E, A, B, C, R(57)); + P(C, D, E, A, B, R(58)); + P(B, C, D, E, A, R(59)); #undef K #undef F @@ -170,26 +168,26 @@ void ss_sha1_process( ss_sha1_context *ctx, const unsigned char data[64] ) #define F(x, y, z) (x ^ y ^ z) #define K 0xCA62C1D6 - P( A, B, C, D, E, R(60) ); - P( E, A, B, C, D, R(61) ); - P( D, E, A, B, C, R(62) ); - P( C, D, E, A, B, R(63) ); - P( B, C, D, E, A, R(64) ); - P( A, B, C, D, E, R(65) ); - P( E, A, B, C, D, R(66) ); - P( D, E, A, B, C, R(67) ); - P( C, D, E, A, B, R(68) ); - P( B, C, D, E, A, R(69) ); - P( A, B, C, D, E, R(70) ); - P( E, A, B, C, D, R(71) ); - P( D, E, A, B, C, R(72) ); - P( C, D, E, A, B, R(73) ); - P( B, C, D, E, A, R(74) ); - P( A, B, C, D, E, R(75) ); - P( E, A, B, C, D, R(76) ); - P( D, E, A, B, C, R(77) ); - P( C, D, E, A, B, R(78) ); - P( B, C, D, E, A, R(79) ); + P(A, B, C, D, E, R(60)); + P(E, A, B, C, D, R(61)); + P(D, E, A, B, C, R(62)); + P(C, D, E, A, B, R(63)); + P(B, C, D, E, A, R(64)); + P(A, B, C, D, E, R(65)); + P(E, A, B, C, D, R(66)); + P(D, E, A, B, C, R(67)); + P(C, D, E, A, B, R(68)); + P(B, C, D, E, A, R(69)); + P(A, B, C, D, E, R(70)); + P(E, A, B, C, D, R(71)); + P(D, E, A, B, C, R(72)); + P(C, D, E, A, B, R(73)); + P(B, C, D, E, A, R(74)); + P(A, B, C, D, E, R(75)); + P(E, A, B, C, D, R(76)); + P(D, E, A, B, C, R(77)); + P(C, D, E, A, B, R(78)); + P(B, C, D, E, A, R(79)); #undef K #undef F @@ -204,7 +202,7 @@ void ss_sha1_process( ss_sha1_context *ctx, const unsigned char data[64] ) /* * SHA-1 process buffer */ -void ss_sha1_update( ss_sha1_context *ctx, const unsigned char *input, size_t ilen ) +void ss_sha1_update(ss_sha1_context *ctx, const unsigned char *input, size_t ilen) { size_t fill; uint32_t left; @@ -224,26 +222,25 @@ void ss_sha1_update( ss_sha1_context *ctx, const unsigned char *input, size_t il } if (left && ilen >= fill) { - memcpy( (void *)(ctx->buffer + left), input, fill ); - ss_sha1_process( ctx, ctx->buffer ); + memcpy((void *)(ctx->buffer + left), input, fill); + ss_sha1_process(ctx, ctx->buffer); input += fill; - ilen -= fill; - left = 0; + ilen -= fill; + left = 0; } while (ilen >= 64) { - ss_sha1_process( ctx, input ); + ss_sha1_process(ctx, input); input += 64; - ilen -= 64; + ilen -= 64; } if (ilen > 0) { - memcpy( (void *)(ctx->buffer + left), input, ilen ); + memcpy((void *)(ctx->buffer + left), input, ilen); } } -static const unsigned char ss_sha1_padding[64] = -{ +static const unsigned char ss_sha1_padding[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -253,120 +250,119 @@ static const unsigned char ss_sha1_padding[64] = /* * SHA-1 final digest */ -void ss_sha1_finish( ss_sha1_context *ctx, unsigned char output[20] ) +void ss_sha1_finish(ss_sha1_context *ctx, unsigned char output[20]) { uint32_t last, padn; uint32_t high, low; unsigned char msglen[8]; - high = ( ctx->total[0] >> 29 ) - | ( ctx->total[1] << 3 ); - low = ( ctx->total[0] << 3 ); + high = (ctx->total[0] >> 29) + | (ctx->total[1] << 3); + low = (ctx->total[0] << 3); - PUT_UINT32_BE( high, msglen, 0 ); - PUT_UINT32_BE( low, msglen, 4 ); + PUT_UINT32_BE(high, msglen, 0); + PUT_UINT32_BE(low, msglen, 4); last = ctx->total[0] & 0x3F; - padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); + padn = (last < 56) ? (56 - last) : (120 - last); - ss_sha1_update( ctx, ss_sha1_padding, padn ); - ss_sha1_update( ctx, msglen, 8 ); + ss_sha1_update(ctx, ss_sha1_padding, padn); + ss_sha1_update(ctx, msglen, 8); - PUT_UINT32_BE( ctx->state[0], output, 0 ); - PUT_UINT32_BE( ctx->state[1], output, 4 ); - PUT_UINT32_BE( ctx->state[2], output, 8 ); - PUT_UINT32_BE( ctx->state[3], output, 12 ); - PUT_UINT32_BE( ctx->state[4], output, 16 ); + PUT_UINT32_BE(ctx->state[0], output, 0); + PUT_UINT32_BE(ctx->state[1], output, 4); + PUT_UINT32_BE(ctx->state[2], output, 8); + PUT_UINT32_BE(ctx->state[3], output, 12); + PUT_UINT32_BE(ctx->state[4], output, 16); } + /* * output = SHA-1( input buffer ) */ -void ss_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ) +void ss_sha1(const unsigned char *input, size_t ilen, unsigned char output[20]) { ss_sha1_context ctx; - ss_sha1_starts( &ctx ); - ss_sha1_update( &ctx, input, ilen ); - ss_sha1_finish( &ctx, output ); + ss_sha1_starts(&ctx); + ss_sha1_update(&ctx, input, ilen); + ss_sha1_finish(&ctx, output); - memset( &ctx, 0, sizeof( ss_sha1_context ) ); + memset(&ctx, 0, sizeof(ss_sha1_context)); } /* * SHA-1 HMAC context setup */ -void ss_sha1_hmac_starts( ss_sha1_context *ctx, const unsigned char *key, size_t keylen ) +void ss_sha1_hmac_starts(ss_sha1_context *ctx, const unsigned char *key, size_t keylen) { size_t i; unsigned char sum[20]; if (keylen > 64) { - ss_sha1( key, keylen, sum ); + ss_sha1(key, keylen, sum); keylen = 20; - key = sum; + key = sum; } - memset( ctx->ipad, 0x36, 64 ); - memset( ctx->opad, 0x5C, 64 ); + memset(ctx->ipad, 0x36, 64); + memset(ctx->opad, 0x5C, 64); for (i = 0; i < keylen; i++) { - ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); - ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); + ctx->ipad[i] = (unsigned char)(ctx->ipad[i] ^ key[i]); + ctx->opad[i] = (unsigned char)(ctx->opad[i] ^ key[i]); } - ss_sha1_starts( ctx ); - ss_sha1_update( ctx, ctx->ipad, 64 ); + ss_sha1_starts(ctx); + ss_sha1_update(ctx, ctx->ipad, 64); - memset( sum, 0, sizeof( sum ) ); + memset(sum, 0, sizeof(sum)); } /* * SHA-1 HMAC process buffer */ -void ss_sha1_hmac_update( ss_sha1_context *ctx, const unsigned char *input, size_t ilen ) +void ss_sha1_hmac_update(ss_sha1_context *ctx, const unsigned char *input, size_t ilen) { - ss_sha1_update( ctx, input, ilen ); + ss_sha1_update(ctx, input, ilen); } /* * SHA-1 HMAC final digest */ -void ss_sha1_hmac_finish( ss_sha1_context *ctx, unsigned char output[20] ) +void ss_sha1_hmac_finish(ss_sha1_context *ctx, unsigned char output[20]) { unsigned char tmpbuf[20]; - ss_sha1_finish( ctx, tmpbuf ); - ss_sha1_starts( ctx ); - ss_sha1_update( ctx, ctx->opad, 64 ); - ss_sha1_update( ctx, tmpbuf, 20 ); - ss_sha1_finish( ctx, output ); + ss_sha1_finish(ctx, tmpbuf); + ss_sha1_starts(ctx); + ss_sha1_update(ctx, ctx->opad, 64); + ss_sha1_update(ctx, tmpbuf, 20); + ss_sha1_finish(ctx, output); - memset( tmpbuf, 0, sizeof( tmpbuf ) ); + memset(tmpbuf, 0, sizeof(tmpbuf)); } /* * SHA1 HMAC context reset */ -void ss_sha1_hmac_reset( ss_sha1_context *ctx ) +void ss_sha1_hmac_reset(ss_sha1_context *ctx) { - ss_sha1_starts( ctx ); - ss_sha1_update( ctx, ctx->ipad, 64 ); + ss_sha1_starts(ctx); + ss_sha1_update(ctx, ctx->ipad, 64); } /* * output = HMAC-SHA-1( hmac key, input buffer ) */ -void ss_sha1_hmac( const unsigned char *key, size_t keylen, - const unsigned char *input, size_t ilen, - unsigned char output[20] ) +void ss_sha1_hmac(const unsigned char *key, size_t keylen, + const unsigned char *input, size_t ilen, + unsigned char output[20]) { ss_sha1_context ctx; - ss_sha1_hmac_starts( &ctx, key, keylen ); - ss_sha1_hmac_update( &ctx, input, ilen ); - ss_sha1_hmac_finish( &ctx, output ); + ss_sha1_hmac_starts(&ctx, key, keylen); + ss_sha1_hmac_update(&ctx, input, ilen); + ss_sha1_hmac_finish(&ctx, output); - memset( &ctx, 0, sizeof( ss_sha1_context ) ); + memset(&ctx, 0, sizeof(ss_sha1_context)); } - - diff --git a/src/hmac-sha1.h b/src/hmac-sha1.h index 92053060..96a8fe23 100644 --- a/src/hmac-sha1.h +++ b/src/hmac-sha1.h @@ -31,34 +31,30 @@ #include #include - /* * 32-bit integer manipulation macros (big endian) */ -#define GET_UINT32_BE(n,b,i) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ - | ( (uint32_t) (b)[(i) + 1] << 16 ) \ - | ( (uint32_t) (b)[(i) + 2] << 8 ) \ - | ( (uint32_t) (b)[(i) + 3] ); \ -} - -#define PUT_UINT32_BE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} - +#define GET_UINT32_BE(n, b, i) \ + { \ + (n) = ((uint32_t)(b)[(i)] << 24) \ + | ((uint32_t)(b)[(i) + 1] << 16) \ + | ((uint32_t)(b)[(i) + 2] << 8) \ + | ((uint32_t)(b)[(i) + 3]); \ + } +#define PUT_UINT32_BE(n, b, i) \ + { \ + (b)[(i)] = (unsigned char)((n) >> 24); \ + (b)[(i) + 1] = (unsigned char)((n) >> 16); \ + (b)[(i) + 2] = (unsigned char)((n) >> 8); \ + (b)[(i) + 3] = (unsigned char)((n)); \ + } /** * \brief SHA-1 context structure */ -typedef struct -{ +typedef struct { uint32_t total[2]; /*!< number of bytes processed */ uint32_t state[5]; /*!< intermediate digest state */ unsigned char buffer[64]; /*!< data block being processed */ @@ -73,7 +69,7 @@ ss_sha1_context; * * \param ctx context to be initialized */ -void ss_sha1_starts( ss_sha1_context *ctx ); +void ss_sha1_starts(ss_sha1_context *ctx); /** * \brief SHA-1 process buffer @@ -82,7 +78,7 @@ void ss_sha1_starts( ss_sha1_context *ctx ); * \param input buffer holding the data * \param ilen length of the input data */ -void ss_sha1_update( ss_sha1_context *ctx, const unsigned char *input, size_t ilen ); +void ss_sha1_update(ss_sha1_context *ctx, const unsigned char *input, size_t ilen); /** * \brief SHA-1 final digest @@ -90,11 +86,10 @@ void ss_sha1_update( ss_sha1_context *ctx, const unsigned char *input, size_t il * \param ctx SHA-1 context * \param output SHA-1 checksum result */ -void ss_sha1_finish( ss_sha1_context *ctx, unsigned char output[20] ); +void ss_sha1_finish(ss_sha1_context *ctx, unsigned char output[20]); /* Internal use */ -void ss_sha1_process( ss_sha1_context *ctx, const unsigned char data[64] ); - +void ss_sha1_process(ss_sha1_context *ctx, const unsigned char data[64]); /** * \brief Output = SHA-1( input buffer ) @@ -103,8 +98,7 @@ void ss_sha1_process( ss_sha1_context *ctx, const unsigned char data[64] ); * \param ilen length of the input data * \param output SHA-1 checksum result */ -void ss_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ); - +void ss_sha1(const unsigned char *input, size_t ilen, unsigned char output[20]); /** * \brief SHA-1 HMAC context setup @@ -113,7 +107,7 @@ void ss_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] * \param key HMAC secret key * \param keylen length of the HMAC key */ -void ss_sha1_hmac_starts( ss_sha1_context *ctx, const unsigned char *key, size_t keylen ); +void ss_sha1_hmac_starts(ss_sha1_context *ctx, const unsigned char *key, size_t keylen); /** * \brief SHA-1 HMAC process buffer @@ -122,7 +116,7 @@ void ss_sha1_hmac_starts( ss_sha1_context *ctx, const unsigned char *key, size_t * \param input buffer holding the data * \param ilen length of the input data */ -void ss_sha1_hmac_update( ss_sha1_context *ctx, const unsigned char *input, size_t ilen ); +void ss_sha1_hmac_update(ss_sha1_context *ctx, const unsigned char *input, size_t ilen); /** * \brief SHA-1 HMAC final digest @@ -130,14 +124,14 @@ void ss_sha1_hmac_update( ss_sha1_context *ctx, const unsigned char *input, size * \param ctx HMAC context * \param output SHA-1 HMAC checksum result */ -void ss_sha1_hmac_finish( ss_sha1_context *ctx, unsigned char output[20] ); +void ss_sha1_hmac_finish(ss_sha1_context *ctx, unsigned char output[20]); /** * \brief SHA-1 HMAC context reset * * \param ctx HMAC context to be reset */ -void ss_sha1_hmac_reset( ss_sha1_context *ctx ); +void ss_sha1_hmac_reset(ss_sha1_context *ctx); /** * \brief Output = HMAC-SHA-1( hmac key, input buffer ) @@ -148,10 +142,8 @@ void ss_sha1_hmac_reset( ss_sha1_context *ctx ); * \param ilen length of the input data * \param output HMAC-SHA-1 result */ -void ss_sha1_hmac( const unsigned char *key, size_t keylen, - const unsigned char *input, size_t ilen, - unsigned char output[20] ); - - +void ss_sha1_hmac(const unsigned char *key, size_t keylen, + const unsigned char *input, size_t ilen, + unsigned char output[20]); #endif /* HMAC_SHA1_H_ */ diff --git a/src/jconf.c b/src/jconf.c index ec937568..2ddaedc3 100644 --- a/src/jconf.c +++ b/src/jconf.c @@ -97,9 +97,8 @@ void parse_addr(const char *str, ss_addr_t *addr) } } -jconf_t *read_jconf(const char * file) +jconf_t *read_jconf(const char *file) { - static jconf_t conf; char *buf; @@ -142,7 +141,7 @@ jconf_t *read_jconf(const char * file) if (obj->type == json_object) { int i, j; for (i = 0; i < obj->u.object.length; i++) { - char *name = obj->u.object.values[i].name; + char *name = obj->u.object.values[i].name; json_value *value = obj->u.object.values[i].value; if (strcmp(name, "server") == 0) { if (value->type == json_array) { @@ -157,7 +156,7 @@ jconf_t *read_jconf(const char * file) } else if (value->type == json_string) { conf.remote_addr[0].host = to_string(value); conf.remote_addr[0].port = NULL; - conf.remote_num = 1; + conf.remote_num = 1; } } else if (strcmp(name, "port_password") == 0) { if (value->type == json_object) { @@ -170,7 +169,7 @@ jconf_t *read_jconf(const char * file) conf.port_password[j].port = ss_strndup(value->u.object.values[j].name, value->u.object.values[j].name_length); conf.port_password[j].password = to_string(v); - conf.port_password_num = j + 1; + conf.port_password_num = j + 1; } } } @@ -203,5 +202,4 @@ jconf_t *read_jconf(const char * file) free(buf); json_value_free(obj); return &conf; - } diff --git a/src/jconf.h b/src/jconf.h index ceac61f7..752caf41 100644 --- a/src/jconf.h +++ b/src/jconf.h @@ -56,7 +56,7 @@ typedef struct { char *nameserver; } jconf_t; -jconf_t *read_jconf(const char * file); +jconf_t *read_jconf(const char *file); void parse_addr(const char *str, ss_addr_t *addr); void free_addr(ss_addr_t *addr); diff --git a/src/json.c b/src/json.c index f831c788..62929606 100644 --- a/src/json.c +++ b/src/json.c @@ -86,20 +86,19 @@ typedef struct { json_settings settings; int first_pass; - } json_state; -static void * default_alloc(size_t size, int zero, void * user_data) +static void *default_alloc(size_t size, int zero, void *user_data) { return zero ? calloc(1, size) : malloc(size); } -static void default_free(void * ptr, void * user_data) +static void default_free(void *ptr, void *user_data) { free(ptr); } -static void * json_alloc(json_state * state, unsigned long size, int zero) +static void *json_alloc(json_state *state, unsigned long size, int zero) { if ((state->ulong_max - state->used_memory) < size) { return 0; @@ -113,15 +112,14 @@ static void * json_alloc(json_state * state, unsigned long size, int zero) return state->settings.mem_alloc(size, zero, state->settings.user_data); } -static int new_value - (json_state * state, json_value ** top, json_value ** root, - json_value ** alloc, json_type type) +static int new_value(json_state *state, json_value **top, json_value **root, + json_value **alloc, json_type type) { - json_value * value; + json_value *value; int values_size; if (!state->first_pass) { - value = *top = *alloc; + value = *top = *alloc; *alloc = (*alloc)->_reserved.next_alloc; if (!*root) { @@ -133,7 +131,7 @@ static int new_value if (!(value->u.array.values = (json_value **)json_alloc (state, value->u.array.length * - sizeof(json_value *), 0)) ) { + sizeof(json_value *), 0))) { return 0; } @@ -150,7 +148,7 @@ static int new_value values_size + ((size_t)value->u. object.values), - 0)) ) { + 0))) { return 0; } @@ -165,7 +163,7 @@ static int new_value if (!(value->u.string.ptr = (json_char *)json_alloc (state, (value->u.string.length + - 1) * sizeof(json_char), 0)) ) { + 1) * sizeof(json_char), 0))) { return 0; } @@ -175,7 +173,6 @@ static int new_value default: break; } - ; return 1; } @@ -190,7 +187,7 @@ static int new_value *root = value; } - value->type = type; + value->type = type; value->parent = *top; if (*alloc) { @@ -206,55 +203,58 @@ static int new_value ((int)(i - cur_line_begin)) #define whitespace \ -case '\n': ++cur_line; cur_line_begin = i; \ -case ' ': case '\t': case '\r' +case '\n': \ + ++cur_line; cur_line_begin = i; \ +case ' ': \ +case '\t': \ +case '\r' #define string_add(b) \ - do { if (!state.first_pass) { string [string_length] = b; \ + do { if (!state.first_pass) { string[string_length] = b; \ } ++string_length; } while (0) static const long - flag_next = 1 << 0, - flag_reproc = 1 << 1, - flag_need_comma = 1 << 2, - flag_seek_value = 1 << 3, - flag_escaped = 1 << 4, - flag_string = 1 << 5, - flag_need_colon = 1 << 6, - flag_done = 1 << 7, - flag_num_negative = 1 << 8, - flag_num_zero = 1 << 9, - flag_num_e = 1 << 10, + flag_next = 1 << 0, + flag_reproc = 1 << 1, + flag_need_comma = 1 << 2, + flag_seek_value = 1 << 3, + flag_escaped = 1 << 4, + flag_string = 1 << 5, + flag_need_colon = 1 << 6, + flag_done = 1 << 7, + flag_num_negative = 1 << 8, + flag_num_zero = 1 << 9, + flag_num_e = 1 << 10, flag_num_e_got_sign = 1 << 11, flag_num_e_negative = 1 << 12, - flag_line_comment = 1 << 13, - flag_block_comment = 1 << 14; + flag_line_comment = 1 << 13, + flag_block_comment = 1 << 14; -json_value * json_parse_ex(json_settings * settings, - const json_char * json, - size_t length, - char * error_buf) +json_value *json_parse_ex(json_settings *settings, + const json_char *json, + size_t length, + char *error_buf) { - json_char error [json_error_max]; + json_char error[json_error_max]; unsigned int cur_line; - const json_char * cur_line_begin, * i, * end; - json_value * top, * root, * alloc = 0; + const json_char *cur_line_begin, *i, *end; + json_value *top, *root, *alloc = 0; json_state state = { 0 }; long flags; - long num_digits = 0, num_e = 0; + long num_digits = 0, num_e = 0; json_int_t num_fraction = 0; /* Skip UTF-8 BOM */ - if (length >= 3 && ((unsigned char)json [0]) == 0xEF - && ((unsigned char)json [1]) == 0xBB - && ((unsigned char)json [2]) == 0xBF) { - json += 3; + if (length >= 3 && ((unsigned char)json[0]) == 0xEF + && ((unsigned char)json[1]) == 0xBB + && ((unsigned char)json[2]) == 0xBF) { + json += 3; length -= 3; } error[0] = '\0'; - end = (json + length); + end = (json + length); memcpy(&state.settings, settings, sizeof(json_settings)); @@ -269,19 +269,19 @@ json_value * json_parse_ex(json_settings * settings, memset(&state.uint_max, 0xFF, sizeof(state.uint_max)); memset(&state.ulong_max, 0xFF, sizeof(state.ulong_max)); - state.uint_max -= 8; /* limit of how much can be added before next check */ + state.uint_max -= 8; /* limit of how much can be added before next check */ state.ulong_max -= 8; for (state.first_pass = 1; state.first_pass >= 0; --state.first_pass) { json_uchar uchar; unsigned char uc_b1, uc_b2, uc_b3, uc_b4; - json_char * string = 0; + json_char *string = 0; unsigned int string_length = 0; - top = root = 0; + top = root = 0; flags = flag_seek_value; - cur_line = 1; + cur_line = 1; cur_line_begin = json; for (i = json;; ++i) { @@ -345,13 +345,13 @@ json_value * json_parse_ex(json_settings * settings, if (state.first_pass) { string_length += 2; } else { - string [string_length++] = 0xC0 | - ((uc_b2 & - 0xC0) >> - 6) | - ((uc_b1 & 0x7) << 2); - string [string_length++] = 0x80 | - (uc_b2 & 0x3F); + string[string_length++] = 0xC0 | + ((uc_b2 & + 0xC0) >> + 6) | + ((uc_b1 & 0x7) << 2); + string[string_length++] = 0x80 | + (uc_b2 & 0x3F); } break; @@ -360,14 +360,14 @@ json_value * json_parse_ex(json_settings * settings, if (state.first_pass) { string_length += 3; } else { - string [string_length++] = 0xE0 | - ((uc_b1 & 0xF0) >> 4); - string [string_length++] = 0x80 | - ((uc_b1 & - 0xF) << - 2) | - ((uc_b2 & 0xC0) >> 6); - string [string_length++] = 0x80 | (uc_b2 & 0x3F); + string[string_length++] = 0xE0 | + ((uc_b1 & 0xF0) >> 4); + string[string_length++] = 0x80 | + ((uc_b1 & + 0xF) << + 2) | + ((uc_b2 & 0xC0) >> 6); + string[string_length++] = 0x80 | (uc_b2 & 0x3F); } break; @@ -375,7 +375,6 @@ json_value * json_parse_ex(json_settings * settings, default: string_add(b); } - ; continue; } @@ -387,7 +386,7 @@ json_value * json_parse_ex(json_settings * settings, if (b == '"') { if (!state.first_pass) { - string [string_length] = 0; + string[string_length] = 0; } flags &= ~flag_string; @@ -397,7 +396,7 @@ json_value * json_parse_ex(json_settings * settings, case json_string: top->u.string.length = string_length; - flags |= flag_next; + flags |= flag_next; break; @@ -407,10 +406,10 @@ json_value * json_parse_ex(json_settings * settings, (*(json_char **)&top->u.object.values) += string_length + 1; } else { - top->u.object.values [top->u.object.length].name + top->u.object.values[top->u.object.length].name = (json_char *)top->_reserved.object_mem; - top->u.object.values [top->u.object.length]. + top->u.object.values[top->u.object.length]. name_length = string_length; @@ -424,7 +423,6 @@ json_value * json_parse_ex(json_settings * settings, default: break; } - ; } else { string_add(b); continue; @@ -450,7 +448,7 @@ json_value * json_parse_ex(json_settings * settings, goto e_failed; } - if (b == '*' && i < (end - 1) && i [1] == '/') { + if (b == '*' && i < (end - 1) && i[1] == '/') { flags &= ~flag_block_comment; ++i; /* skip closing sequence */ } @@ -486,7 +484,6 @@ json_value * json_parse_ex(json_settings * settings, b); goto e_failed; } - ; } } @@ -496,7 +493,7 @@ json_value * json_parse_ex(json_settings * settings, } switch (b) { - whitespace: +whitespace: continue; default: @@ -504,12 +501,11 @@ json_value * json_parse_ex(json_settings * settings, e_off, b); goto e_failed; } - ; } if (flags & flag_seek_value) { switch (b) { - whitespace: +whitespace: continue; case ']': @@ -580,7 +576,7 @@ json_value * json_parse_ex(json_settings * settings, flags |= flag_string; - string = top->u.string.ptr; + string = top->u.string.ptr; string_length = 0; continue; @@ -644,7 +640,7 @@ json_value * json_parse_ex(json_settings * settings, while (isdigit((uint8_t)b) || b == '+' || b == '-' || b == 'e' || b == 'E' || b == '.') { - if ( (++i) == end) { + if ((++i) == end) { b = 0; break; } @@ -661,9 +657,9 @@ json_value * json_parse_ex(json_settings * settings, flag_num_e_negative | flag_num_zero); - num_digits = 0; + num_digits = 0; num_fraction = 0; - num_e = 0; + num_e = 0; if (b != '-') { flags |= flag_reproc; @@ -679,15 +675,13 @@ json_value * json_parse_ex(json_settings * settings, goto e_failed; } } - ; } - ; } else { switch (top->type) { case json_object: switch (b) { - whitespace: +whitespace: continue; case '"': @@ -700,7 +694,7 @@ json_value * json_parse_ex(json_settings * settings, flags |= flag_string; - string = (json_char *)top->_reserved.object_mem; + string = (json_char *)top->_reserved.object_mem; string_length = 0; break; @@ -723,7 +717,6 @@ json_value * json_parse_ex(json_settings * settings, cur_line, e_off, b); goto e_failed; } - ; break; @@ -747,7 +740,7 @@ json_value * json_parse_ex(json_settings * settings, } } else { flags |= flag_num_e_got_sign; - num_e = (num_e * 10) + (b - '0'); + num_e = (num_e * 10) + (b - '0'); continue; } @@ -760,8 +753,8 @@ json_value * json_parse_ex(json_settings * settings, } if (b == '+' || b == '-') { - if ( (flags & flag_num_e) && - !(flags & flag_num_e_got_sign)) { + if ((flags & flag_num_e) && + !(flags & flag_num_e_got_sign)) { flags |= flag_num_e_got_sign; if (b == '-') { @@ -777,7 +770,7 @@ json_value * json_parse_ex(json_settings * settings, goto e_failed; } - top->type = json_double; + top->type = json_double; top->u.dbl = (double)top->u.integer; num_digits = 0; @@ -801,12 +794,12 @@ json_value * json_parse_ex(json_settings * settings, flags |= flag_num_e; if (top->type == json_integer) { - top->type = json_double; + top->type = json_double; top->u.dbl = (double)top->u.integer; } num_digits = 0; - flags &= ~flag_num_zero; + flags &= ~flag_num_zero; continue; } @@ -837,7 +830,6 @@ json_value * json_parse_ex(json_settings * settings, default: break; } - ; } if (flags & flag_reproc) { @@ -860,7 +852,7 @@ json_value * json_parse_ex(json_settings * settings, } if (!state.first_pass) { - json_value * parent = top->parent; + json_value *parent = top->parent; switch (parent->type) { case json_object: @@ -880,10 +872,9 @@ json_value * json_parse_ex(json_settings * settings, default: break; } - ; } - if ( (++top->parent->u.array.length) > state.uint_max) { + if ((++top->parent->u.array.length) > state.uint_max) { goto e_overflow; } @@ -898,22 +889,22 @@ json_value * json_parse_ex(json_settings * settings, return root; - e_unknown_value: +e_unknown_value: sprintf(error, "%d:%d: Unknown value", cur_line, e_off); goto e_failed; - e_alloc_failure: +e_alloc_failure: strcpy(error, "Memory allocation failure"); goto e_failed; - e_overflow: +e_overflow: sprintf(error, "%d:%d: Too long (caught overflow)", cur_line, e_off); goto e_failed; - e_failed: +e_failed: if (error_buf) { if (*error) { @@ -940,15 +931,15 @@ json_value * json_parse_ex(json_settings * settings, return 0; } -json_value * json_parse(const json_char * json, size_t length) +json_value *json_parse(const json_char *json, size_t length) { json_settings settings = { 0 }; return json_parse_ex(&settings, json, length, 0); } -void json_value_free_ex(json_settings * settings, json_value * value) +void json_value_free_ex(json_settings *settings, json_value *value) { - json_value * cur_value; + json_value *cur_value; if (!value) { return; @@ -965,7 +956,7 @@ void json_value_free_ex(json_settings * settings, json_value * value) break; } - value = value->u.array.values [--value->u.array.length]; + value = value->u.array.values[--value->u.array.length]; continue; case json_object: @@ -975,7 +966,7 @@ void json_value_free_ex(json_settings * settings, json_value * value) break; } - value = value->u.object.values [--value->u.object.length].value; + value = value->u.object.values[--value->u.object.length].value; continue; case json_string: @@ -986,18 +977,16 @@ void json_value_free_ex(json_settings * settings, json_value * value) default: break; } - ; cur_value = value; - value = value->parent; + value = value->parent; settings->mem_free(cur_value, settings->user_data); } } -void json_value_free(json_value * value) +void json_value_free(json_value *value) { json_settings settings = { 0 }; settings.mem_free = default_free; json_value_free_ex(&settings, value); } - diff --git a/src/json.h b/src/json.h index bd3d2561..7b383b30 100644 --- a/src/json.h +++ b/src/json.h @@ -51,7 +51,6 @@ extern "C" { - #endif typedef struct { @@ -61,11 +60,10 @@ typedef struct { /* Custom allocator support (leave null to use malloc/free) */ - void * (*mem_alloc)(size_t, int zero, void * user_data); - void (* mem_free)(void *, void * user_data); - - void * user_data; /* will be passed to mem_alloc and mem_free */ + void * (*mem_alloc)(size_t, int zero, void *user_data); + void (*mem_free)(void *, void *user_data); + void *user_data; /* will be passed to mem_alloc and mem_free */ } json_settings; #define json_enable_comments 0x01 @@ -79,13 +77,12 @@ typedef enum { json_string, json_boolean, json_null - } json_type; extern const struct _json_value json_value_none; typedef struct _json_value { - struct _json_value * parent; + struct _json_value *parent; json_type type; @@ -96,20 +93,18 @@ typedef struct _json_value { struct { unsigned int length; - json_char * ptr; /* null terminated */ - + json_char *ptr; /* null terminated */ } string; struct { unsigned int length; struct { - json_char * name; + json_char *name; unsigned int name_length; - struct _json_value * value; - - } * values; + struct _json_value *value; + } *values; #if defined(__cplusplus) && __cplusplus >= 201103L decltype(values) begin() const @@ -121,12 +116,11 @@ typedef struct _json_value { return values + length; } #endif - } object; struct { unsigned int length; - struct _json_value ** values; + struct _json_value **values; #if defined(__cplusplus) && __cplusplus >= 201103L decltype(values) begin() const @@ -138,18 +132,14 @@ typedef struct _json_value { return values + length; } #endif - } array; - } u; union { - struct _json_value * next_alloc; - void * object_mem; - + struct _json_value *next_alloc; + void *object_mem; } _reserved; - /* Some C++ operator sugar */ #ifdef __cplusplus @@ -166,19 +156,18 @@ public: return json_value_none; } - return *u.array.values [index]; + return *u.array.values[index]; } - inline const struct _json_value &operator [] (const char * index) const { + inline const struct _json_value &operator [] (const char *index) const { if (type != json_object) { return json_value_none; } - for (unsigned int i = 0; i < u.object.length; ++i) { - if (!strcmp(u.object.values [i].name, index)) { - return *u.object.values [i].value; + for (unsigned int i = 0; i < u.object.length; ++i) + if (!strcmp(u.object.values[i].name, index)) { + return *u.object.values[i].value; } - } return json_value_none; } @@ -192,7 +181,6 @@ public: default: return ""; } - ; } inline operator json_int_t() const @@ -207,7 +195,6 @@ public: default: return 0; } - ; } inline operator bool() const @@ -231,36 +218,30 @@ public: default: return 0; } - ; } #endif - } json_value; -json_value * json_parse(const json_char * json, - size_t length); +json_value *json_parse(const json_char *json, + size_t length); #define json_error_max 128 -json_value * json_parse_ex(json_settings * settings, - const json_char * json, - size_t length, - char * error); +json_value *json_parse_ex(json_settings *settings, + const json_char *json, + size_t length, + char *error); void json_value_free(json_value *); - /* Not usually necessary, unless you used a custom mem_alloc and now want to * use a custom mem_free. */ -void json_value_free_ex(json_settings * settings, +void json_value_free_ex(json_settings *settings, json_value *); - #ifdef __cplusplus } /* extern "C" */ #endif #endif - - diff --git a/src/local.c b/src/local.c index eb61e8f7..d637288f 100644 --- a/src/local.c +++ b/src/local.c @@ -81,13 +81,13 @@ int verbose = 0; #ifdef ANDROID -int vpn = 0; -uint64_t tx = 0; -uint64_t rx = 0; +int vpn = 0; +uint64_t tx = 0; +uint64_t rx = 0; ev_tstamp last = 0; #endif -static int acl = 0; +static int acl = 0; static int mode = TCP_ONLY; static int fast_open = 0; @@ -107,14 +107,14 @@ static void accept_cb(EV_P_ ev_io *w, int revents); static void signal_cb(EV_P_ ev_signal *w, int revents); static int create_and_bind(const char *addr, const char *port); -static remote_t * create_remote(listen_ctx_t *listener, struct sockaddr *addr); +static remote_t *create_remote(listen_ctx_t *listener, struct sockaddr *addr); static void free_remote(remote_t *remote); static void close_and_free_remote(EV_P_ remote_t *remote); static void free_server(server_t *server); static void close_and_free_server(EV_P_ server_t *server); -static remote_t * new_remote(int fd, int timeout); -static server_t * new_server(int fd, int method); +static remote_t *new_remote(int fd, int timeout); +static server_t *new_server(int fd, int method); static struct cork_dllist connections; @@ -127,10 +127,11 @@ int setnonblocking(int fd) } return fcntl(fd, F_SETFL, flags | O_NONBLOCK); } + #endif #ifdef SET_INTERFACE -int setinterface(int socket_fd, const char * interface_name) +int setinterface(int socket_fd, const char *interface_name) { struct ifreq interface; memset(&interface, 0, sizeof(interface)); @@ -139,6 +140,7 @@ int setinterface(int socket_fd, const char * interface_name) sizeof(struct ifreq)); return res; } + #endif int create_and_bind(const char *addr, const char *port) @@ -148,7 +150,7 @@ int create_and_bind(const char *addr, const char *port) int s, listen_sock; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ + hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */ s = getaddrinfo(addr, port, &hints, &result); @@ -206,8 +208,8 @@ static void free_connections(struct ev_loop *loop) static void server_recv_cb(EV_P_ ev_io *w, int revents) { server_ctx_t *server_recv_ctx = (server_ctx_t *)w; - server_t *server = server_recv_ctx->server; - remote_t *remote = server->remote; + server_t *server = server_recv_ctx->server; + remote_t *remote = server->remote; buffer_t *buf; if (remote == NULL) { @@ -269,7 +271,6 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } if (!remote->send_ctx->connected) { - #ifdef ANDROID if (vpn) { if (protect_socket(remote->fd) == -1) { @@ -316,7 +317,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } } else if (s < remote->buf->len) { remote->buf->len -= s; - remote->buf->idx = s; + remote->buf->idx = s; } // Just connected @@ -346,7 +347,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } } else if (s < remote->buf->len) { remote->buf->len -= s; - remote->buf->idx = s; + remote->buf->idx = s; ev_io_stop(EV_A_ & server_recv_ctx->io); ev_io_start(EV_A_ & remote->send_ctx->io); return; @@ -357,7 +358,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) return; } else if (server->stage == 0) { struct method_select_response response; - response.ver = SVERSION; + response.ver = SVERSION; response.method = 0; char *send_buf = (char *)&response; send(server->fd, send_buf, sizeof(response), 0); @@ -390,9 +391,9 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } else if (request->cmd != 1) { LOGE("unsupported cmd: %d", request->cmd); struct socks5_response response; - response.ver = SVERSION; - response.rep = CMD_NOT_SUPPORTED; - response.rsv = 0; + response.ver = SVERSION; + response.rep = CMD_NOT_SUPPORTED; + response.rsv = 0; response.atyp = 1; char *send_buf = (char *)&response; send(server->fd, send_buf, 4, 0); @@ -416,34 +417,34 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) abuf->len += in_addr_len + 2; if (acl || verbose) { - uint16_t p = ntohs(*(uint16_t *)(buf + 4 + in_addr_len)); - dns_ntop(AF_INET, (const void *)(buf + 4), + uint16_t p = ntohs(*(uint16_t *)(buf->array + 4 + in_addr_len)); + dns_ntop(AF_INET, (const void *)(buf->array + 4), host, INET_ADDRSTRLEN); sprintf(port, "%d", p); } } else if (request->atyp == 3) { // Domain name - uint8_t name_len = *(uint8_t *)(buf + 4); + uint8_t name_len = *(uint8_t *)(buf->array + 4); abuf->array[abuf->len++] = name_len; - memcpy(abuf->array + abuf->len, buf + 4 + 1, name_len + 2); + memcpy(abuf->array + abuf->len, buf->array + 4 + 1, name_len + 2); abuf->len += name_len + 2; if (acl || verbose) { uint16_t p = - ntohs(*(uint16_t *)(buf + 4 + 1 + name_len)); - memcpy(host, buf + 4 + 1, name_len); + ntohs(*(uint16_t *)(buf->array + 4 + 1 + name_len)); + memcpy(host, buf->array + 4 + 1, name_len); host[name_len] = '\0'; sprintf(port, "%d", p); } } else if (request->atyp == 4) { // IP V6 size_t in6_addr_len = sizeof(struct in6_addr); - memcpy(abuf->array + abuf->len, buf + 4, in6_addr_len + 2); + memcpy(abuf->array + abuf->len, buf->array + 4, in6_addr_len + 2); abuf->len += in6_addr_len + 2; if (acl || verbose) { - uint16_t p = ntohs(*(uint16_t *)(buf + 4 + in6_addr_len)); - dns_ntop(AF_INET6, (const void *)(buf + 4), + uint16_t p = ntohs(*(uint16_t *)(buf->array + 4 + in6_addr_len)); + dns_ntop(AF_INET6, (const void *)(buf->array + 4), host, INET6_ADDRSTRLEN); sprintf(port, "%d", p); } @@ -473,7 +474,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) struct sockaddr_storage storage; memset(&storage, 0, sizeof(struct sockaddr_storage)); if (get_sockaddr(host, port, &storage, 0) != -1) { - remote = create_remote(server->listener, (struct sockaddr *)&storage); + remote = create_remote(server->listener, (struct sockaddr *)&storage); remote->direct = 1; } } else { @@ -518,9 +519,9 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) // Fake reply struct socks5_response response; - response.ver = SVERSION; - response.rep = 0; - response.rsv = 0; + response.ver = SVERSION; + response.rep = 0; + response.rsv = 0; response.atyp = 1; memcpy(server->buf->array, &response, sizeof(struct socks5_response)); @@ -553,8 +554,8 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) static void server_send_cb(EV_P_ ev_io *w, int revents) { server_ctx_t *server_send_ctx = (server_ctx_t *)w; - server_t *server = server_send_ctx->server; - remote_t *remote = server->remote; + server_t *server = server_send_ctx->server; + remote_t *remote = server->remote; if (server->buf->len == 0) { // close and free close_and_free_remote(EV_A_ remote); @@ -585,7 +586,6 @@ static void server_send_cb(EV_P_ ev_io *w, int revents) return; } } - } #ifdef ANDROID @@ -597,12 +597,13 @@ static void stat_update_cb(struct ev_loop *loop) last = now; } } + #endif static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) { remote_ctx_t *remote_ctx = (remote_ctx_t *)(((void *)watcher) - - sizeof(ev_io)); + - sizeof(ev_io)); remote_t *remote = remote_ctx->remote; server_t *server = remote->server; @@ -617,8 +618,8 @@ static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) static void remote_recv_cb(EV_P_ ev_io *w, int revents) { remote_ctx_t *remote_recv_ctx = (remote_ctx_t *)w; - remote_t *remote = remote_recv_ctx->remote; - server_t *server = remote->server; + remote_t *remote = remote_recv_ctx->remote; + server_t *server = remote->server; ev_timer_again(EV_A_ & remote->recv_ctx->watcher); @@ -678,7 +679,7 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) } } else if (s < server->buf->len) { server->buf->len -= s; - server->buf->idx = s; + server->buf->idx = s; ev_io_stop(EV_A_ & remote_recv_ctx->io); ev_io_start(EV_A_ & server->send_ctx->io); return; @@ -688,13 +689,13 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) static void remote_send_cb(EV_P_ ev_io *w, int revents) { remote_ctx_t *remote_send_ctx = (remote_ctx_t *)w; - remote_t *remote = remote_send_ctx->remote; - server_t *server = remote->server; + remote_t *remote = remote_send_ctx->remote; + server_t *server = remote->server; if (!remote_send_ctx->connected) { struct sockaddr_storage addr; socklen_t len = sizeof addr; - int r = getpeername(remote->fd, (struct sockaddr *)&addr, &len); + int r = getpeername(remote->fd, (struct sockaddr *)&addr, &len); if (r == 0) { remote_send_ctx->connected = 1; ev_timer_stop(EV_A_ & remote_send_ctx->watcher); @@ -748,29 +749,31 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents) } } -static remote_t * new_remote(int fd, int timeout) +static remote_t *new_remote(int fd, int timeout) { remote_t *remote; remote = malloc(sizeof(remote_t)); memset(remote, 0, sizeof(remote_t)); - remote->buf = malloc(sizeof(buffer_t)); - balloc(remote->buf, BUF_SIZE); - - remote->recv_ctx = malloc(sizeof(remote_ctx_t)); - remote->send_ctx = malloc(sizeof(remote_ctx_t)); + remote->buf = malloc(sizeof(buffer_t)); + remote->recv_ctx = malloc(sizeof(remote_ctx_t)); + remote->send_ctx = malloc(sizeof(remote_ctx_t)); remote->recv_ctx->connected = 0; remote->send_ctx->connected = 0; - remote->fd = fd; + remote->fd = fd; + remote->recv_ctx->remote = remote; + remote->send_ctx->remote = remote; + ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ); ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE); ev_timer_init(&remote->send_ctx->watcher, remote_timeout_cb, min(MAX_CONNECT_TIMEOUT, timeout), 0); ev_timer_init(&remote->recv_ctx->watcher, remote_timeout_cb, min(MAX_CONNECT_TIMEOUT, timeout), timeout); - remote->recv_ctx->remote = remote; - remote->send_ctx->remote = remote; + + balloc(remote->buf, BUF_SIZE); + return remote; } @@ -800,25 +803,22 @@ static void close_and_free_remote(EV_P_ remote_t *remote) } } -static server_t * new_server(int fd, int method) +static server_t *new_server(int fd, int method) { server_t *server; server = malloc(sizeof(server_t)); memset(server, 0, sizeof(server_t)); - server->buf = malloc(sizeof(buffer_t)); - balloc(server->buf, BUF_SIZE); - - server->recv_ctx = malloc(sizeof(server_ctx_t)); - server->send_ctx = malloc(sizeof(server_ctx_t)); + server->recv_ctx = malloc(sizeof(server_ctx_t)); + server->send_ctx = malloc(sizeof(server_ctx_t)); + server->buf = malloc(sizeof(buffer_t)); server->recv_ctx->connected = 0; server->send_ctx->connected = 0; - server->fd = fd; - ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ); - ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE); - server->recv_ctx->server = server; - server->send_ctx->server = server; + server->fd = fd; + server->recv_ctx->server = server; + server->send_ctx->server = server; + if (method) { server->e_ctx = malloc(sizeof(struct enc_ctx)); server->d_ctx = malloc(sizeof(struct enc_ctx)); @@ -829,6 +829,11 @@ static server_t * new_server(int fd, int method) server->d_ctx = NULL; } + ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ); + ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE); + + balloc(server->buf, BUF_SIZE); + cork_dllist_add(&connections, &server->entries); return server; @@ -868,8 +873,8 @@ static void close_and_free_server(EV_P_ server_t *server) } } -static remote_t * create_remote(listen_ctx_t *listener, - struct sockaddr *addr) +static remote_t *create_remote(listen_ctx_t *listener, + struct sockaddr *addr) { struct sockaddr *remote_addr; @@ -922,7 +927,7 @@ static void signal_cb(EV_P_ ev_signal *w, int revents) void accept_cb(EV_P_ ev_io *w, int revents) { listen_ctx_t *listener = (listen_ctx_t *)w; - int serverfd = accept(listener->fd, NULL, NULL); + int serverfd = accept(listener->fd, NULL, NULL); if (serverfd == -1) { ERROR("accept"); return; @@ -943,18 +948,17 @@ void accept_cb(EV_P_ ev_io *w, int revents) #ifndef LIB_ONLY int main(int argc, char **argv) { - int i, c; - int pid_flags = 0; - char *user = NULL; + int pid_flags = 0; + char *user = NULL; char *local_port = NULL; char *local_addr = NULL; - char *password = NULL; - char *timeout = NULL; - char *method = NULL; - char *pid_path = NULL; - char *conf_path = NULL; - char *iface = NULL; + char *password = NULL; + char *timeout = NULL; + char *method = NULL; + char *pid_path = NULL; + char *conf_path = NULL; + char *iface = NULL; srand(time(NULL)); @@ -962,12 +966,11 @@ int main(int argc, char **argv) ss_addr_t remote_addr[MAX_REMOTE_NUM]; char *remote_port = NULL; - int option_index = 0; - static struct option long_options[] = - { + int option_index = 0; + static struct option long_options[] = { { "fast-open", no_argument, 0, 0 }, { "acl", required_argument, 0, 0 }, - { 0, 0, 0, 0 } + { 0, 0, 0, 0 } }; opterr = 0; @@ -992,7 +995,7 @@ int main(int argc, char **argv) break; case 's': if (remote_num < MAX_REMOTE_NUM) { - remote_addr[remote_num].host = optarg; + remote_addr[remote_num].host = optarg; remote_addr[remote_num++].port = NULL; } break; @@ -1007,7 +1010,7 @@ int main(int argc, char **argv) break; case 'f': pid_flags = 1; - pid_path = optarg; + pid_path = optarg; break; case 't': timeout = optarg; @@ -1058,9 +1061,8 @@ int main(int argc, char **argv) jconf_t *conf = read_jconf(conf_path); if (remote_num == 0) { remote_num = conf->remote_num; - for (i = 0; i < remote_num; i++) { + for (i = 0; i < remote_num; i++) remote_addr[i] = conf->remote_addr[i]; - } } if (remote_port == NULL) { remote_port = conf->remote_port; @@ -1155,7 +1157,7 @@ int main(int argc, char **argv) // Setup proxy context listen_ctx_t listen_ctx; - listen_ctx.remote_num = remote_num; + listen_ctx.remote_num = remote_num; listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *) * remote_num); for (i = 0; i < remote_num; i++) { char *host = remote_addr[i].host; @@ -1169,8 +1171,8 @@ int main(int argc, char **argv) listen_ctx.remote_addr[i] = (struct sockaddr *)storage; } listen_ctx.timeout = atoi(timeout); - listen_ctx.iface = iface; - listen_ctx.method = m; + listen_ctx.iface = iface; + listen_ctx.method = m; struct ev_loop *loop = EV_DEFAULT; @@ -1223,9 +1225,8 @@ int main(int argc, char **argv) free_udprelay(); } - for (i = 0; i < remote_num; i++) { + for (i = 0; i < remote_num; i++) free(listen_ctx.remote_addr[i]); - } free(listen_ctx.remote_addr); #ifdef __MINGW32__ @@ -1245,17 +1246,17 @@ int start_ss_local_server(profile_t profile) srand(time(NULL)); char *remote_host = profile.remote_host; - char *local_addr = profile.local_addr; - char *method = profile.method; - char *password = profile.password; - char *log = profile.log; - int remote_port = profile.remote_port; - int local_port = profile.local_port; - int timeout = profile.timeout; + char *local_addr = profile.local_addr; + char *method = profile.method; + char *password = profile.password; + char *log = profile.log; + int remote_port = profile.remote_port; + int local_port = profile.local_port; + int timeout = profile.timeout; - mode = profile.mode; + mode = profile.mode; fast_open = profile.fast_open; - verbose = profile.verbose; + verbose = profile.verbose; char local_port_str[16]; char remote_port_str[16]; @@ -1301,12 +1302,12 @@ int start_ss_local_server(profile_t profile) struct ev_loop *loop = EV_DEFAULT; listen_ctx_t listen_ctx; - listen_ctx.remote_num = 1; - listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *)); + listen_ctx.remote_num = 1; + listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *)); listen_ctx.remote_addr[0] = (struct sockaddr *)storage; - listen_ctx.timeout = timeout; - listen_ctx.method = m; - listen_ctx.iface = NULL; + listen_ctx.timeout = timeout; + listen_ctx.method = m; + listen_ctx.iface = NULL; // Setup socket int listenfd; @@ -1369,4 +1370,3 @@ int start_ss_local_server(profile_t profile) } #endif - diff --git a/src/manager.c b/src/manager.c index b1bd07eb..36072cee 100644 --- a/src/manager.c +++ b/src/manager.c @@ -71,7 +71,7 @@ #define BUF_SIZE 65535 #endif -int verbose = 0; +int verbose = 0; char *executable = "ss-server"; char working_dir[PATH_MAX]; @@ -86,6 +86,7 @@ static int setnonblocking(int fd) } return fcntl(fd, F_SETFL, flags | O_NONBLOCK); } + #endif static void build_config(char *prefix, struct server *server) @@ -116,9 +117,9 @@ static char *construct_command_line(struct manager_ctx *manager, struct server * memset(cmd, 0, BUF_SIZE); snprintf(cmd, BUF_SIZE, - "%s -m %s --manager-address %s -f %s/.shadowsocks_%s.pid -c %s/.shadowsocks_%s.conf", - executable, manager->method, manager->manager_address, - working_dir, server->port, working_dir, server->port); + "%s -m %s --manager-address %s -f %s/.shadowsocks_%s.pid -c %s/.shadowsocks_%s.conf", + executable, manager->method, manager->manager_address, + working_dir, server->port, working_dir, server->port); if (manager->acl != NULL) { int len = strlen(cmd); @@ -172,15 +173,13 @@ static char *construct_command_line(struct manager_ctx *manager, struct server * return cmd; } - static char *get_data(char *buf, int len) { char *data; int pos = 0; - while (buf[pos] != '{' && pos < len) { + while (buf[pos] != '{' && pos < len) pos++; - } if (pos == len) { return NULL; } @@ -194,17 +193,15 @@ static char *get_action(char *buf, int len) char *action; int pos = 0; - while (isspace((unsigned char)buf[pos]) && pos < len) { + while (isspace((unsigned char)buf[pos]) && pos < len) pos++; - } if (pos == len) { return NULL; } action = buf + pos; - while ((!isspace((unsigned char)buf[pos]) && buf[pos] != ':') && pos < len) { + while ((!isspace((unsigned char)buf[pos]) && buf[pos] != ':') && pos < len) pos++; - } buf[pos] = '\0'; return action; @@ -223,7 +220,7 @@ static struct server *get_server(char *buf, int len) memset(server, 0, sizeof(struct server)); json_settings settings = { 0 }; - json_value *obj = json_parse_ex(&settings, data, strlen(data), error_buf); + json_value *obj = json_parse_ex(&settings, data, strlen(data), error_buf); if (obj == NULL) { LOGE("%s", error_buf); @@ -233,7 +230,7 @@ static struct server *get_server(char *buf, int len) if (obj->type == json_object) { int i = 0; for (i = 0; i < obj->u.object.length; i++) { - char *name = obj->u.object.values[i].name; + char *name = obj->u.object.values[i].name; json_value *value = obj->u.object.values[i].value; if (strcmp(name, "server_port") == 0) { if (value->type == json_string) { @@ -277,7 +274,7 @@ static int parse_traffic(char *buf, int len, char *port, uint64_t *traffic) if (obj->type == json_object) { int i = 0; for (i = 0; i < obj->u.object.length; i++) { - char *name = obj->u.object.values[i].name; + char *name = obj->u.object.values[i].name; json_value *value = obj->u.object.values[i].value; if (value->type == json_integer) { strncpy(port, name, 8); @@ -321,7 +318,7 @@ static void stop_server(char *prefix, char *port) static void remove_server(char *prefix, char *port) { - char *old_port = NULL; + char *old_port = NULL; struct server *old_server = NULL; cork_hash_table_delete(server_table, (void *)port, (void **)&old_port, (void **)&old_server); @@ -353,7 +350,7 @@ static void manager_recv_cb(EV_P_ ev_io *w, int revents) memset(buf, 0, BUF_SIZE); len = sizeof(struct sockaddr_un); - r = recvfrom(manager->fd, buf, BUF_SIZE, 0, (struct sockaddr *)&claddr, &len); + r = recvfrom(manager->fd, buf, BUF_SIZE, 0, (struct sockaddr *)&claddr, &len); if (r == -1) { ERROR("manager_recvfrom"); return; @@ -384,7 +381,6 @@ static void manager_recv_cb(EV_P_ ev_io *w, int revents) if (sendto(manager->fd, msg, 3, 0, (struct sockaddr *)&claddr, len) != 3) { ERROR("add_sendto"); } - } else if (strcmp(action, "remove") == 0) { struct server *server = get_server(buf, r); @@ -403,7 +399,6 @@ static void manager_recv_cb(EV_P_ ev_io *w, int revents) if (sendto(manager->fd, msg, 3, 0, (struct sockaddr *)&claddr, len) != 3) { ERROR("remove_sendto"); } - } else if (strcmp(action, "stat") == 0) { char port[8]; uint64_t traffic = 0; @@ -414,9 +409,7 @@ static void manager_recv_cb(EV_P_ ev_io *w, int revents) } update_stat(port, traffic); - } else if (strcmp(action, "ping") == 0) { - struct cork_hash_table_entry *entry; struct cork_hash_table_iterator server_iter; @@ -429,7 +422,7 @@ static void manager_recv_cb(EV_P_ ev_io *w, int revents) while ((entry = cork_hash_table_iterator_next(&server_iter)) != NULL) { struct server *server = (struct server *)entry->value; - size_t pos = strlen(buf); + size_t pos = strlen(buf); if (pos > BUF_SIZE / 2) { buf[pos - 1] = '}'; if (sendto(manager->fd, buf, pos + 1, 0, (struct sockaddr *)&claddr, len) @@ -457,7 +450,7 @@ static void manager_recv_cb(EV_P_ ev_io *w, int revents) return; - ERROR_MSG: +ERROR_MSG: strcpy(buf, "err"); if (sendto(manager->fd, buf, 4, 0, (struct sockaddr *)&claddr, len) != 4) { ERROR("error_sendto"); @@ -482,9 +475,9 @@ int create_server_socket(const char *host, const char *port) int s, server_sock; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ + hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */ - hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */ + hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */ hints.ai_protocol = IPPROTO_UDP; s = getaddrinfo(host, port, &hints, &result); @@ -496,10 +489,10 @@ int create_server_socket(const char *host, const char *port) rp = result; /* - On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with - AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to - return a list of addresses to listen on, but it is impossible to listen on - 0.0.0.0 and :: at the same time, if :: implies dualstack mode. + * On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with + * AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to + * return a list of addresses to listen on, but it is impossible to listen on + * 0.0.0.0 and :: at the same time, if :: implies dualstack mode. */ if (!host) { ipv4v6bindall = result; @@ -549,42 +542,39 @@ int create_server_socket(const char *host, const char *port) return server_sock; } - int main(int argc, char **argv) { - int i, c; - int pid_flags = 0; - char *acl = NULL; - char *user = NULL; - char *password = NULL; - char *timeout = NULL; - char *method = NULL; - char *pid_path = NULL; - char *conf_path = NULL; - char *iface = NULL; + int pid_flags = 0; + char *acl = NULL; + char *user = NULL; + char *password = NULL; + char *timeout = NULL; + char *method = NULL; + char *pid_path = NULL; + char *conf_path = NULL; + char *iface = NULL; char *manager_address = NULL; - int auth = 0; + int auth = 0; int fast_open = 0; - int mode = TCP_ONLY; + int mode = TCP_ONLY; int server_num = 0; char *server_host[MAX_REMOTE_NUM]; - char * nameservers[MAX_DNS_NUM + 1]; + char *nameservers[MAX_DNS_NUM + 1]; int nameserver_num = 0; jconf_t *conf = NULL; - int option_index = 0; - static struct option long_options[] = - { + int option_index = 0; + static struct option long_options[] = { { "fast-open", no_argument, 0, 0 }, { "acl", required_argument, 0, 0 }, { "manager-address", required_argument, 0, 0 }, { "executable", required_argument, 0, 0 }, - { 0, 0, 0, 0 } + { 0, 0, 0, 0 } }; opterr = 0; @@ -592,7 +582,7 @@ int main(int argc, char **argv) USE_TTY(); while ((c = getopt_long(argc, argv, "f:s:l:k:t:m:c:i:d:a:uUvA", - long_options, &option_index)) != -1) { + long_options, &option_index)) != -1) switch (c) { case 0: if (option_index == 0) { @@ -615,7 +605,7 @@ int main(int argc, char **argv) break; case 'f': pid_flags = 1; - pid_path = optarg; + pid_path = optarg; break; case 't': timeout = optarg; @@ -650,7 +640,6 @@ int main(int argc, char **argv) auth = 1; break; } - } if (opterr) { usage(); @@ -661,9 +650,8 @@ int main(int argc, char **argv) conf = read_jconf(conf_path); if (server_num == 0) { server_num = conf->remote_num; - for (i = 0; i < server_num; i++) { + for (i = 0; i < server_num; i++) server_host[i] = conf->remote_addr[i].host; - } } if (password == NULL) { password = conf->password; @@ -740,21 +728,21 @@ int main(int argc, char **argv) struct manager_ctx manager; memset(&manager, 0, sizeof(struct manager_ctx)); - manager.fast_open = fast_open; - manager.verbose = verbose; - manager.mode = mode; - manager.auth = auth; - manager.password = password; - manager.timeout = timeout; - manager.method = method; - manager.iface = iface; - manager.acl = acl; - manager.user = user; + manager.fast_open = fast_open; + manager.verbose = verbose; + manager.mode = mode; + manager.auth = auth; + manager.password = password; + manager.timeout = timeout; + manager.method = method; + manager.iface = iface; + manager.acl = acl; + manager.user = user; manager.manager_address = manager_address; - manager.hosts = server_host; - manager.host_num = server_num; - manager.nameservers = nameservers; - manager.nameserver_num = nameserver_num; + manager.hosts = server_host; + manager.host_num = server_num; + manager.nameservers = nameservers; + manager.nameserver_num = nameserver_num; // inilitialize ev loop struct ev_loop *loop = EV_DEFAULT; @@ -764,7 +752,7 @@ int main(int argc, char **argv) run_as(user); } - struct passwd *pw = getpwuid(getuid()); + struct passwd *pw = getpwuid(getuid()); const char *homedir = pw->pw_dir; snprintf(working_dir, PATH_MAX, "%s/.shadowsocks", homedir); diff --git a/src/netutils.c b/src/netutils.c index 17d13c93..b4227a6c 100644 --- a/src/netutils.c +++ b/src/netutils.c @@ -77,7 +77,7 @@ size_t get_sockaddr(char *host, char *port, struct sockaddr_storage *storage, in struct addrinfo *result, *rp; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ + hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */ int err, i; @@ -97,20 +97,18 @@ size_t get_sockaddr(char *host, char *port, struct sockaddr_storage *storage, in return -1; } - for (rp = result; rp != NULL; rp = rp->ai_next) { + for (rp = result; rp != NULL; rp = rp->ai_next) if (rp->ai_family == AF_INET) { memcpy(storage, rp->ai_addr, sizeof(struct sockaddr_in)); break; } - } if (rp == NULL) { - for (rp = result; rp != NULL; rp = rp->ai_next) { + for (rp = result; rp != NULL; rp = rp->ai_next) if (rp->ai_family == AF_INET6) { memcpy(storage, rp->ai_addr, sizeof(struct sockaddr_in6)); break; } - } } if (rp == NULL) { diff --git a/src/redir.c b/src/redir.c index a52c4f74..bac2492e 100644 --- a/src/redir.c +++ b/src/redir.c @@ -72,8 +72,8 @@ static void server_send_cb(EV_P_ ev_io *w, int revents); static void remote_recv_cb(EV_P_ ev_io *w, int revents); static void remote_send_cb(EV_P_ ev_io *w, int revents); -static remote_t * new_remote(int fd, int timeout); -static server_t * new_server(int fd, int method); +static remote_t *new_remote(int fd, int timeout); +static server_t *new_server(int fd, int method); static void free_remote(remote_t *remote); static void close_and_free_remote(EV_P_ remote_t *remote); @@ -88,7 +88,7 @@ static int auth = 0; int getdestaddr(int fd, struct sockaddr_storage *destaddr) { socklen_t socklen = sizeof(*destaddr); - int error = 0; + int error = 0; error = getsockopt(fd, SOL_IPV6, IP6T_SO_ORIGINAL_DST, destaddr, &socklen); if (error) { // Didn't find a proper way to detect IP version. @@ -116,7 +116,7 @@ int create_and_bind(const char *addr, const char *port) int s, listen_sock; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ + hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */ s = getaddrinfo(addr, port, &hints, &result); @@ -161,8 +161,8 @@ int create_and_bind(const char *addr, const char *port) static void server_recv_cb(EV_P_ ev_io *w, int revents) { server_ctx_t *server_recv_ctx = (server_ctx_t *)w; - server_t *server = server_recv_ctx->server; - remote_t *remote = server->remote; + server_t *server = server_recv_ctx->server; + remote_t *remote = server->remote; ssize_t r = recv(server->fd, remote->buf->array, BUF_SIZE, 0); @@ -216,19 +216,18 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } } else if (s < remote->buf->len) { remote->buf->len -= s; - remote->buf->idx = s; + remote->buf->idx = s; ev_io_stop(EV_A_ & server_recv_ctx->io); ev_io_start(EV_A_ & remote->send_ctx->io); return; } - } static void server_send_cb(EV_P_ ev_io *w, int revents) { server_ctx_t *server_send_ctx = (server_ctx_t *)w; - server_t *server = server_send_ctx->server; - remote_t *remote = server->remote; + server_t *server = server_send_ctx->server; + remote_t *remote = server->remote; if (server->buf->len == 0) { // close and free close_and_free_remote(EV_A_ remote); @@ -258,13 +257,12 @@ static void server_send_cb(EV_P_ ev_io *w, int revents) ev_io_start(EV_A_ & remote->recv_ctx->io); } } - } static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) { remote_ctx_t *remote_ctx = (remote_ctx_t *)(((void *)watcher) - - sizeof(ev_io)); + - sizeof(ev_io)); remote_t *remote = remote_ctx->remote; server_t *server = remote->server; @@ -277,8 +275,8 @@ static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) static void remote_recv_cb(EV_P_ ev_io *w, int revents) { remote_ctx_t *remote_recv_ctx = (remote_ctx_t *)w; - remote_t *remote = remote_recv_ctx->remote; - server_t *server = remote->server; + remote_t *remote = remote_recv_ctx->remote; + server_t *server = remote->server; ssize_t r = recv(remote->fd, server->buf->array, BUF_SIZE, 0); @@ -326,7 +324,7 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) } } else if (s < server->buf->len) { server->buf->len -= s; - server->buf->idx = s; + server->buf->idx = s; ev_io_stop(EV_A_ & remote_recv_ctx->io); ev_io_start(EV_A_ & server->send_ctx->io); return; @@ -336,14 +334,13 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) static void remote_send_cb(EV_P_ ev_io *w, int revents) { remote_ctx_t *remote_send_ctx = (remote_ctx_t *)w; - remote_t *remote = remote_send_ctx->remote; - server_t *server = remote->server; + remote_t *remote = remote_send_ctx->remote; + server_t *server = remote->server; if (!remote_send_ctx->connected) { - struct sockaddr_storage addr; socklen_t len = sizeof addr; - int r = getpeername(remote->fd, (struct sockaddr *)&addr, &len); + int r = getpeername(remote->fd, (struct sockaddr *)&addr, &len); if (r == 0) { remote_send_ctx->connected = 1; ev_io_stop(EV_A_ & remote_send_ctx->io); @@ -355,7 +352,7 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents) balloc(abuf, BUF_SIZE); if (AF_INET6 == server->destaddr.ss_family) { // IPv6 - abuf->array[abuf->len++] = 4; //Type 4 is IPv6 address + abuf->array[abuf->len++] = 4; // Type 4 is IPv6 address size_t in6_addr_len = sizeof(struct in6_addr); memcpy(abuf->array + abuf->len, @@ -365,8 +362,8 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents) memcpy(abuf->array + abuf->len, &(((struct sockaddr_in6 *)&(server->destaddr))->sin6_port), 2); - } else { //IPv4 - abuf->array[abuf->len++] = 1; //Type 1 is IPv4 address + } else { // IPv4 + abuf->array[abuf->len++] = 1; // Type 1 is IPv4 address size_t in_addr_len = sizeof(struct in_addr); memcpy(abuf->array + abuf->len, @@ -444,30 +441,30 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents) ev_io_start(EV_A_ & server->recv_ctx->io); } } - } } -static remote_t * new_remote(int fd, int timeout) +static remote_t *new_remote(int fd, int timeout) { remote_t *remote; remote = malloc(sizeof(remote_t)); memset(remote, 0, sizeof(remote_t)); - remote->recv_ctx = malloc(sizeof(remote_ctx_t)); - remote->send_ctx = malloc(sizeof(remote_ctx_t)); - remote->fd = fd; + remote->recv_ctx = malloc(sizeof(remote_ctx_t)); + remote->send_ctx = malloc(sizeof(remote_ctx_t)); + remote->buf = malloc(sizeof(buffer_t)); + remote->fd = fd; + remote->recv_ctx->remote = remote; + remote->recv_ctx->connected = 0; + remote->send_ctx->remote = remote; + remote->send_ctx->connected = 0; + ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ); ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE); ev_timer_init(&remote->send_ctx->watcher, remote_timeout_cb, min(MAX_CONNECT_TIMEOUT, timeout), 0); - remote->recv_ctx->remote = remote; - remote->recv_ctx->connected = 0; - remote->send_ctx->remote = remote; - remote->send_ctx->connected = 0; - remote->buf = malloc(sizeof(buffer_t)); balloc(remote->buf, BUF_SIZE); return remote; @@ -500,20 +497,20 @@ static void close_and_free_remote(EV_P_ remote_t *remote) } } -static server_t * new_server(int fd, int method) +static server_t *new_server(int fd, int method) { server_t *server; server = malloc(sizeof(server_t)); - server->recv_ctx = malloc(sizeof(server_ctx_t)); - server->send_ctx = malloc(sizeof(server_ctx_t)); - server->fd = fd; - ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ); - ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE); - server->recv_ctx->server = server; + server->recv_ctx = malloc(sizeof(server_ctx_t)); + server->send_ctx = malloc(sizeof(server_ctx_t)); + server->buf = malloc(sizeof(buffer_t)); + server->fd = fd; + server->recv_ctx->server = server; server->recv_ctx->connected = 0; - server->send_ctx->server = server; + server->send_ctx->server = server; server->send_ctx->connected = 0; + if (method) { server->e_ctx = malloc(sizeof(enc_ctx_t)); server->d_ctx = malloc(sizeof(enc_ctx_t)); @@ -524,7 +521,9 @@ static server_t * new_server(int fd, int method) server->d_ctx = NULL; } - server->buf = malloc(sizeof(buffer_t)); + ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ); + ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE); + balloc(server->buf, BUF_SIZE); return server; @@ -589,7 +588,7 @@ static void accept_cb(EV_P_ ev_io *w, int revents) setsockopt(serverfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)); #endif - int index = rand() % listener->remote_num; + int index = rand() % listener->remote_num; struct sockaddr *remote_addr = listener->remote_addr[index]; int remotefd = socket(remote_addr->sa_family, SOCK_STREAM, IPPROTO_TCP); @@ -608,8 +607,8 @@ static void accept_cb(EV_P_ ev_io *w, int revents) server_t *server = new_server(serverfd, listener->method); remote_t *remote = new_remote(remotefd, listener->timeout); - server->remote = remote; - remote->server = server; + server->remote = remote; + remote->server = server; server->destaddr = destaddr; connect(remotefd, remote_addr, get_sockaddr_len(remote_addr)); @@ -620,17 +619,16 @@ static void accept_cb(EV_P_ ev_io *w, int revents) int main(int argc, char **argv) { - int i, c; - int pid_flags = 0; - char *user = NULL; + int pid_flags = 0; + char *user = NULL; char *local_port = NULL; char *local_addr = NULL; - char *password = NULL; - char *timeout = NULL; - char *method = NULL; - char *pid_path = NULL; - char *conf_path = NULL; + char *password = NULL; + char *timeout = NULL; + char *method = NULL; + char *pid_path = NULL; + char *conf_path = NULL; int remote_num = 0; ss_addr_t remote_addr[MAX_REMOTE_NUM]; @@ -638,11 +636,11 @@ int main(int argc, char **argv) opterr = 0; - while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:c:b:a:uUvA")) != -1) { + while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:c:b:a:uUvA")) != -1) switch (c) { case 's': if (remote_num < MAX_REMOTE_NUM) { - remote_addr[remote_num].host = optarg; + remote_addr[remote_num].host = optarg; remote_addr[remote_num++].port = NULL; } break; @@ -657,7 +655,7 @@ int main(int argc, char **argv) break; case 'f': pid_flags = 1; - pid_path = optarg; + pid_path = optarg; break; case 't': timeout = optarg; @@ -687,7 +685,6 @@ int main(int argc, char **argv) auth = 1; break; } - } if (opterr) { usage(); @@ -704,9 +701,8 @@ int main(int argc, char **argv) jconf_t *conf = read_jconf(conf_path); if (remote_num == 0) { remote_num = conf->remote_num; - for (i = 0; i < remote_num; i++) { + for (i = 0; i < remote_num; i++) remote_addr[i] = conf->remote_addr[i]; - } } if (remote_port == NULL) { remote_port = conf->remote_port; @@ -764,7 +760,7 @@ int main(int argc, char **argv) // Setup proxy context listen_ctx_t listen_ctx; - listen_ctx.remote_num = remote_num; + listen_ctx.remote_num = remote_num; listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *) * remote_num); for (int i = 0; i < remote_num; i++) { char *host = remote_addr[i].host; @@ -778,7 +774,7 @@ int main(int argc, char **argv) listen_ctx.remote_addr[i] = (struct sockaddr *)storage; } listen_ctx.timeout = atoi(timeout); - listen_ctx.method = m; + listen_ctx.method = m; struct ev_loop *loop = EV_DEFAULT; @@ -822,4 +818,3 @@ int main(int argc, char **argv) return 0; } - diff --git a/src/resolv.c b/src/resolv.c index ea777e13..7cb79297 100644 --- a/src/resolv.c +++ b/src/resolv.c @@ -44,11 +44,9 @@ #include #endif - #include "resolv.h" #include "utils.h" - /* * Implement DNS resolution interface using libudns */ @@ -67,11 +65,11 @@ extern int verbose; static struct ev_io resolv_io_watcher; static struct ev_timer resolv_timeout_watcher; -static const int MODE_IPV4_ONLY = 0; -static const int MODE_IPV6_ONLY = 1; +static const int MODE_IPV4_ONLY = 0; +static const int MODE_IPV6_ONLY = 1; static const int MODE_IPV4_FIRST = 2; static const int MODE_IPV6_FIRST = 3; -static int resolv_mode = 0; +static int resolv_mode = 0; static void resolv_sock_cb(struct ev_loop *, struct ev_io *, int); static void resolv_timeout_cb(struct ev_loop *, struct ev_timer *, int); @@ -84,8 +82,7 @@ static struct sockaddr *choose_ipv4_first(struct ResolvQuery *); static struct sockaddr *choose_ipv6_first(struct ResolvQuery *); static struct sockaddr *choose_any(struct ResolvQuery *); -int -resolv_init(struct ev_loop *loop, char **nameservers, int nameserver_num) +int resolv_init(struct ev_loop *loop, char **nameservers, int nameserver_num) { resolv_mode = MODE_IPV4_FIRST; @@ -119,7 +116,6 @@ resolv_init(struct ev_loop *loop, char **nameservers, int nameserver_num) ev_io_start(loop, &resolv_io_watcher); - ev_timer_init(&resolv_timeout_watcher, resolv_timeout_cb, 0.0, 0.0); resolv_timeout_watcher.data = ctx; @@ -128,8 +124,7 @@ resolv_init(struct ev_loop *loop, char **nameservers, int nameserver_num) return sockfd; } -void -resolv_shutdown(struct ev_loop * loop) +void resolv_shutdown(struct ev_loop *loop) { struct dns_ctx *ctx = (struct dns_ctx *)resolv_io_watcher.data; @@ -142,10 +137,9 @@ resolv_shutdown(struct ev_loop * loop) dns_close(ctx); } -struct ResolvQuery * -resolv_query(const char *hostname, void (*client_cb)(struct sockaddr *, void *), - void (*client_free_cb)(void *), void *client_cb_data, - uint16_t port) +struct ResolvQuery *resolv_query(const char *hostname, void (*client_cb)(struct sockaddr *, void *), + void (*client_free_cb)(void *), void *client_cb_data, + uint16_t port) { struct dns_ctx *ctx = (struct dns_ctx *)resolv_io_watcher.data; @@ -157,13 +151,13 @@ resolv_query(const char *hostname, void (*client_cb)(struct sockaddr *, void *), LOGE("Failed to allocate memory for DNS query callback data."); return NULL; } - cb_data->client_cb = client_cb; + cb_data->client_cb = client_cb; cb_data->client_free_cb = client_free_cb; cb_data->client_cb_data = client_cb_data; memset(cb_data->queries, 0, sizeof(cb_data->queries)); cb_data->response_count = 0; - cb_data->responses = NULL; - cb_data->port = port; + cb_data->responses = NULL; + cb_data->port = port; /* Submit A and AAAA queries */ if (resolv_mode != MODE_IPV6_ONLY) { @@ -175,7 +169,6 @@ resolv_query(const char *hostname, void (*client_cb)(struct sockaddr *, void *), dns_strerror(dns_status(ctx))); } } - ; if (resolv_mode != MODE_IPV4_ONLY) { cb_data->queries[1] = dns_submit_a6(ctx, @@ -198,20 +191,18 @@ resolv_query(const char *hostname, void (*client_cb)(struct sockaddr *, void *), return cb_data; } -void -resolv_cancel(struct ResolvQuery *query_handle) +void resolv_cancel(struct ResolvQuery *query_handle) { struct ResolvQuery *cb_data = (struct ResolvQuery *)query_handle; - struct dns_ctx *ctx = (struct dns_ctx *)resolv_io_watcher.data; + struct dns_ctx *ctx = (struct dns_ctx *)resolv_io_watcher.data; for (int i = 0; i < sizeof(cb_data->queries) / sizeof(cb_data->queries[0]); - i++) { + i++) if (cb_data->queries[i] != NULL) { dns_cancel(ctx, cb_data->queries[i]); free(cb_data->queries[i]); cb_data->queries[i] = NULL; } - } if (cb_data->client_free_cb != NULL) { cb_data->client_free_cb(cb_data->client_cb_data); @@ -223,8 +214,7 @@ resolv_cancel(struct ResolvQuery *query_handle) /* * DNS UDP socket activity callback */ -static void -resolv_sock_cb(struct ev_loop *loop, struct ev_io *w, int revents) +static void resolv_sock_cb(struct ev_loop *loop, struct ev_io *w, int revents) { struct dns_ctx *ctx = (struct dns_ctx *)w->data; @@ -236,8 +226,7 @@ resolv_sock_cb(struct ev_loop *loop, struct ev_io *w, int revents) /* * Wrapper for client callback we provide to udns */ -static void -dns_query_v4_cb(struct dns_ctx *ctx, struct dns_rr_a4 *result, void *data) +static void dns_query_v4_cb(struct dns_ctx *ctx, struct dns_rr_a4 *result, void *data) { struct ResolvQuery *cb_data = (struct ResolvQuery *)data; @@ -259,8 +248,8 @@ dns_query_v4_cb(struct dns_ctx *ctx, struct dns_rr_a4 *result, void *data) struct sockaddr_in *sa = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in)); sa->sin_family = AF_INET; - sa->sin_port = cb_data->port; - sa->sin_addr = result->dnsa4_addr[i]; + sa->sin_port = cb_data->port; + sa->sin_addr = result->dnsa4_addr[i]; cb_data->responses[cb_data->response_count] = (struct sockaddr *)sa; @@ -283,8 +272,7 @@ dns_query_v4_cb(struct dns_ctx *ctx, struct dns_rr_a4 *result, void *data) } } -static void -dns_query_v6_cb(struct dns_ctx *ctx, struct dns_rr_a6 *result, void *data) +static void dns_query_v6_cb(struct dns_ctx *ctx, struct dns_rr_a6 *result, void *data) { struct ResolvQuery *cb_data = (struct ResolvQuery *)data; @@ -306,8 +294,8 @@ dns_query_v6_cb(struct dns_ctx *ctx, struct dns_rr_a6 *result, void *data) struct sockaddr_in6 *sa = (struct sockaddr_in6 *)malloc(sizeof(struct sockaddr_in6)); sa->sin6_family = AF_INET6; - sa->sin6_port = cb_data->port; - sa->sin6_addr = result->dnsa6_addr[i]; + sa->sin6_port = cb_data->port; + sa->sin6_addr = result->dnsa6_addr[i]; cb_data->responses[cb_data->response_count] = (struct sockaddr *)sa; @@ -333,8 +321,7 @@ dns_query_v6_cb(struct dns_ctx *ctx, struct dns_rr_a6 *result, void *data) /* * Called once all queries have been completed */ -static void -process_client_callback(struct ResolvQuery *cb_data) +static void process_client_callback(struct ResolvQuery *cb_data) { struct sockaddr *best_address = NULL; @@ -348,9 +335,8 @@ process_client_callback(struct ResolvQuery *cb_data) cb_data->client_cb(best_address, cb_data->client_cb_data); - for (int i = 0; i < cb_data->response_count; i++) { + for (int i = 0; i < cb_data->response_count; i++) free(cb_data->responses[i]); - } free(cb_data->responses); if (cb_data->client_free_cb != NULL) { @@ -359,32 +345,27 @@ process_client_callback(struct ResolvQuery *cb_data) free(cb_data); } -static struct sockaddr * -choose_ipv4_first(struct ResolvQuery *cb_data) +static struct sockaddr *choose_ipv4_first(struct ResolvQuery *cb_data) { - for (int i = 0; i < cb_data->response_count; i++) { + for (int i = 0; i < cb_data->response_count; i++) if (cb_data->responses[i]->sa_family == AF_INET) { return cb_data->responses[i]; } - } - return choose_any(cb_data);; + return choose_any(cb_data); } -static struct sockaddr * -choose_ipv6_first(struct ResolvQuery *cb_data) +static struct sockaddr *choose_ipv6_first(struct ResolvQuery *cb_data) { - for (int i = 0; i < cb_data->response_count; i++) { + for (int i = 0; i < cb_data->response_count; i++) if (cb_data->responses[i]->sa_family == AF_INET6) { return cb_data->responses[i]; } - } - return choose_any(cb_data);; + return choose_any(cb_data); } -static struct sockaddr * -choose_any(struct ResolvQuery *cb_data) +static struct sockaddr *choose_any(struct ResolvQuery *cb_data) { if (cb_data->response_count >= 1) { return cb_data->responses[0]; @@ -396,8 +377,7 @@ choose_any(struct ResolvQuery *cb_data) /* * DNS timeout callback */ -static void -resolv_timeout_cb(struct ev_loop *loop, struct ev_timer *w, int revents) +static void resolv_timeout_cb(struct ev_loop *loop, struct ev_timer *w, int revents) { struct dns_ctx *ctx = (struct dns_ctx *)w->data; @@ -409,8 +389,7 @@ resolv_timeout_cb(struct ev_loop *loop, struct ev_timer *w, int revents) /* * Callback to setup DNS timeout callback */ -static void -dns_timer_setup_cb(struct dns_ctx *ctx, int timeout, void *data) +static void dns_timer_setup_cb(struct dns_ctx *ctx, int timeout, void *data) { struct ev_loop *loop = (struct ev_loop *)data; @@ -424,15 +403,13 @@ dns_timer_setup_cb(struct dns_ctx *ctx, int timeout, void *data) } } -static inline int -all_queries_are_null(struct ResolvQuery *cb_data) +static inline int all_queries_are_null(struct ResolvQuery *cb_data) { int result = 1; for (int i = 0; i < sizeof(cb_data->queries) / sizeof(cb_data->queries[0]); - i++) { + i++) result = result && cb_data->queries[i] == NULL; - } return result; } diff --git a/src/server.c b/src/server.c index 293f644f..0ece5b39 100644 --- a/src/server.c +++ b/src/server.c @@ -93,10 +93,10 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents); static void remote_send_cb(EV_P_ ev_io *w, int revents); static void server_timeout_cb(EV_P_ ev_timer *watcher, int revents); -static remote_t * new_remote(int fd); -static server_t * new_server(int fd, listen_ctx_t *listener); +static remote_t *new_remote(int fd); +static server_t *new_server(int fd, listen_ctx_t *listener); static remote_t *connect_to_remote(struct addrinfo *res, - server_t *server); + server_t *server); static void free_remote(remote_t *remote); static void close_and_free_remote(EV_P_ remote_t *remote); @@ -107,7 +107,7 @@ static void server_resolve_cb(struct sockaddr *addr, void *data); int verbose = 0; -static int acl = 0; +static int acl = 0; static int mode = TCP_ONLY; static int auth = 0; @@ -118,10 +118,10 @@ static int nofile = 0; static int remote_conn = 0; static int server_conn = 0; -static char *server_port = NULL; +static char *server_port = NULL; static char *manager_address = NULL; -uint64_t tx = 0; -uint64_t rx = 0; +uint64_t tx = 0; +uint64_t rx = 0; ev_timer stat_update_watcher; static struct cork_dllist connections; @@ -174,7 +174,6 @@ static void stat_update_cb(EV_P_ ev_timer *watcher, int revents) } unlink(claddr.sun_path); - } else { struct sockaddr_storage storage; memset(&storage, 0, sizeof(struct sockaddr_storage)); @@ -243,10 +242,11 @@ int setnonblocking(int fd) } return fcntl(fd, F_SETFL, flags | O_NONBLOCK); } + #endif #ifdef SET_INTERFACE -int setinterface(int socket_fd, const char * interface_name) +int setinterface(int socket_fd, const char *interface_name) { struct ifreq interface; memset(&interface, 0, sizeof(interface)); @@ -255,6 +255,7 @@ int setinterface(int socket_fd, const char * interface_name) sizeof(struct ifreq)); return res; } + #endif int create_and_bind(const char *host, const char *port) @@ -264,9 +265,9 @@ int create_and_bind(const char *host, const char *port) int s, listen_sock; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ + hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */ - hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */ + hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */ hints.ai_protocol = IPPROTO_TCP; for (int i = 1; i < 8; i++) { @@ -287,10 +288,10 @@ int create_and_bind(const char *host, const char *port) rp = result; /* - On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with - AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to - return a list of addresses to listen on, but it is impossible to listen on - 0.0.0.0 and :: at the same time, if :: implies dualstack mode. + * On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with + * AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to + * return a list of addresses to listen on, but it is impossible to listen on + * 0.0.0.0 and :: at the same time, if :: implies dualstack mode. */ if (!host) { ipv4v6bindall = result; @@ -358,7 +359,7 @@ int create_and_bind(const char *host, const char *port) } static remote_t *connect_to_remote(struct addrinfo *res, - server_t *server) + server_t *server) { int sockfd; #ifdef SET_INTERFACE @@ -425,18 +426,18 @@ static remote_t *connect_to_remote(struct addrinfo *res, static void server_recv_cb(EV_P_ ev_io *w, int revents) { server_ctx_t *server_recv_ctx = (server_ctx_t *)w; - server_t *server = server_recv_ctx->server; - remote_t *remote = NULL; + server_t *server = server_recv_ctx->server; + remote_t *remote = NULL; - int len = server->buf->len; + int len = server->buf->len; buffer_t *buf = server->buf; ev_timer_again(EV_A_ & server->recv_ctx->watcher); if (server->stage != 0) { remote = server->remote; - buf = remote->buf; - len = 0; + buf = remote->buf; + len = 0; } ssize_t r = recv(server->fd, buf->array + len, BUF_SIZE - len, 0); @@ -515,14 +516,12 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } } else if (s < remote->buf->len) { remote->buf->len -= s; - remote->buf->idx = s; + remote->buf->idx = s; ev_io_stop(EV_A_ & server_recv_ctx->io); ev_io_start(EV_A_ & remote->send_ctx->io); } return; - } else if (server->stage == 0) { - /* * Shadowsocks TCP Relay Header: * @@ -551,11 +550,11 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) * The output of HMAC-SHA is truncated to 10 bytes (leftmost bits). */ - int offset = 0; + int offset = 0; int need_query = 0; - char atyp = server->buf->array[offset++]; + char atyp = server->buf->array[offset++]; char host[256] = { 0 }; - uint16_t port = 0; + uint16_t port = 0; struct addrinfo info; struct sockaddr_storage storage; memset(&info, 0, sizeof(struct addrinfo)); @@ -565,7 +564,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) if ((atyp & ADDRTYPE_MASK) == 1) { // IP V4 struct sockaddr_in *addr = (struct sockaddr_in *)&storage; - size_t in_addr_len = sizeof(struct in_addr); + size_t in_addr_len = sizeof(struct in_addr); addr->sin_family = AF_INET; if (server->buf->len >= in_addr_len + 3) { addr->sin_addr = *(struct in_addr *)(server->buf->array + offset); @@ -578,12 +577,12 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) close_and_free_server(EV_A_ server); return; } - addr->sin_port = *(uint16_t *)(server->buf->array + offset); - info.ai_family = AF_INET; + addr->sin_port = *(uint16_t *)(server->buf->array + offset); + info.ai_family = AF_INET; info.ai_socktype = SOCK_STREAM; info.ai_protocol = IPPROTO_TCP; - info.ai_addrlen = sizeof(struct sockaddr_in); - info.ai_addr = (struct sockaddr *)addr; + info.ai_addrlen = sizeof(struct sockaddr_in); + info.ai_addr = (struct sockaddr *)addr; } else if ((atyp & ADDRTYPE_MASK) == 3) { // Domain name uint8_t name_len = *(uint8_t *)(server->buf->array + offset); @@ -603,19 +602,19 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) if (ip.version == 4) { struct sockaddr_in *addr = (struct sockaddr_in *)&storage; dns_pton(AF_INET, host, &(addr->sin_addr)); - addr->sin_port = *(uint16_t *)(server->buf->array + offset); + addr->sin_port = *(uint16_t *)(server->buf->array + offset); addr->sin_family = AF_INET; - info.ai_family = AF_INET; - info.ai_addrlen = sizeof(struct sockaddr_in); - info.ai_addr = (struct sockaddr *)addr; + info.ai_family = AF_INET; + info.ai_addrlen = sizeof(struct sockaddr_in); + info.ai_addr = (struct sockaddr *)addr; } else if (ip.version == 6) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)&storage; dns_pton(AF_INET6, host, &(addr->sin6_addr)); - addr->sin6_port = *(uint16_t *)(server->buf->array + offset); + addr->sin6_port = *(uint16_t *)(server->buf->array + offset); addr->sin6_family = AF_INET6; - info.ai_family = AF_INET6; - info.ai_addrlen = sizeof(struct sockaddr_in6); - info.ai_addr = (struct sockaddr *)addr; + info.ai_family = AF_INET6; + info.ai_addrlen = sizeof(struct sockaddr_in6); + info.ai_addr = (struct sockaddr *)addr; } } else { need_query = 1; @@ -623,7 +622,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } else if ((atyp & ADDRTYPE_MASK) == 4) { // IP V6 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)&storage; - size_t in6_addr_len = sizeof(struct in6_addr); + size_t in6_addr_len = sizeof(struct in6_addr); addr->sin6_family = AF_INET6; if (server->buf->len >= in6_addr_len + 3) { addr->sin6_addr = *(struct in6_addr *)(server->buf->array + offset); @@ -636,12 +635,12 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) close_and_free_server(EV_A_ server); return; } - addr->sin6_port = *(uint16_t *)(server->buf->array + offset); - info.ai_family = AF_INET6; + addr->sin6_port = *(uint16_t *)(server->buf->array + offset); + info.ai_family = AF_INET6; info.ai_socktype = SOCK_STREAM; info.ai_protocol = IPPROTO_TCP; - info.ai_addrlen = sizeof(struct sockaddr_in6); - info.ai_addr = (struct sockaddr *)addr; + info.ai_addrlen = sizeof(struct sockaddr_in6); + info.ai_addr = (struct sockaddr *)addr; } if (offset == 1) { @@ -664,27 +663,38 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) offset += 2; if (auth || (atyp & ONETIMEAUTH_FLAG)) { + if (server->buf->len < offset + ONETIMEAUTH_BYTES) { + report_addr(server->fd); + close_and_free_server(EV_A_ server); + return; + } + size_t len = server->buf->len; + server->buf->len = offset + ONETIMEAUTH_BYTES; if (ss_onetimeauth_verify(server->buf, server->d_ctx->evp.iv)) { LOGE("authentication error %d", atyp); report_addr(server->fd); close_and_free_server(EV_A_ server); return; } - ; - offset += ONETIMEAUTH_BYTES; - server->auth = 1; + server->buf->len = len; + offset += ONETIMEAUTH_BYTES; + server->auth = 1; + } + + server->buf->len -= offset; + + if (server->buf->len < 0) { + report_addr(server->fd); + close_and_free_server(EV_A_ server); + return; + } else { + memmove(server->buf->array, server->buf->array + offset, server->buf->len); } if (verbose) { LOGI("connect to: %s:%d", host, ntohs(port)); } - // XXX: should handle buffer carefully - if (server->buf->len > offset) { - server->buf->len -= offset; - memmove(server->buf->array, server->buf->array + offset, server->buf->len); - } - if (server->auth && !ss_check_hash(server->buf, server->chunk, server->d_ctx)) { LOGE("hash error"); report_addr(server->fd); @@ -735,8 +745,8 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) static void server_send_cb(EV_P_ ev_io *w, int revents) { server_ctx_t *server_send_ctx = (server_ctx_t *)w; - server_t *server = server_send_ctx->server; - remote_t *remote = server->remote; + server_t *server = server_send_ctx->server; + remote_t *remote = server->remote; if (remote == NULL) { LOGE("invalid server"); @@ -789,7 +799,7 @@ static void server_send_cb(EV_P_ ev_io *w, int revents) static void server_timeout_cb(EV_P_ ev_timer *watcher, int revents) { server_ctx_t *server_ctx = (server_ctx_t *)(((void *)watcher) - - sizeof(ev_io)); + - sizeof(ev_io)); server_t *server = server_ctx->server; remote_t *remote = server->remote; @@ -803,7 +813,7 @@ static void server_timeout_cb(EV_P_ ev_timer *watcher, int revents) static void server_resolve_cb(struct sockaddr *addr, void *data) { - server_t *server = (server_t *)data; + server_t *server = (server_t *)data; struct ev_loop *loop = server->listen_ctx->loop; server->query = NULL; @@ -839,13 +849,13 @@ static void server_resolve_cb(struct sockaddr *addr, void *data) memset(&info, 0, sizeof(struct addrinfo)); info.ai_socktype = SOCK_STREAM; info.ai_protocol = IPPROTO_TCP; - info.ai_addr = addr; + info.ai_addr = addr; if (addr->sa_family == AF_INET) { - info.ai_family = AF_INET; + info.ai_family = AF_INET; info.ai_addrlen = sizeof(struct sockaddr_in); } else if (addr->sa_family == AF_INET6) { - info.ai_family = AF_INET6; + info.ai_family = AF_INET6; info.ai_addrlen = sizeof(struct sockaddr_in6); } @@ -877,8 +887,8 @@ static void server_resolve_cb(struct sockaddr *addr, void *data) static void remote_recv_cb(EV_P_ ev_io *w, int revents) { remote_ctx_t *remote_recv_ctx = (remote_ctx_t *)w; - remote_t *remote = remote_recv_ctx->remote; - server_t *server = remote->server; + remote_t *remote = remote_recv_ctx->remote; + server_t *server = remote->server; if (server == NULL) { LOGE("invalid server"); @@ -913,7 +923,6 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) rx += r; - server->buf->len = r; int err = ss_encrypt(server->buf, server->e_ctx); @@ -940,7 +949,7 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) return; } else if (s < server->buf->len) { server->buf->len -= s; - server->buf->idx = s; + server->buf->idx = s; ev_io_stop(EV_A_ & remote_recv_ctx->io); ev_io_start(EV_A_ & server->send_ctx->io); return; @@ -950,8 +959,8 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) static void remote_send_cb(EV_P_ ev_io *w, int revents) { remote_ctx_t *remote_send_ctx = (remote_ctx_t *)w; - remote_t *remote = remote_send_ctx->remote; - server_t *server = remote->server; + remote_t *remote = remote_send_ctx->remote; + server_t *server = remote->server; if (server == NULL) { LOGE("invalid server"); @@ -960,7 +969,6 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents) } if (!remote_send_ctx->connected) { - struct sockaddr_storage addr; socklen_t len = sizeof addr; memset(&addr, 0, len); @@ -978,7 +986,6 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents) ev_io_start(EV_A_ & remote->recv_ctx->io); return; } - } else { ERROR("getpeername"); // not connected @@ -1034,28 +1041,30 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents) } } -static remote_t * new_remote(int fd) +static remote_t *new_remote(int fd) { if (verbose) { remote_conn++; } remote_t *remote; - remote = malloc(sizeof(remote_t)); - remote->recv_ctx = malloc(sizeof(remote_ctx_t)); - remote->send_ctx = malloc(sizeof(remote_ctx_t)); - remote->buf = malloc(sizeof(buffer_t)); - balloc(remote->buf, BUF_SIZE); + remote = malloc(sizeof(remote_t)); + remote->recv_ctx = malloc(sizeof(remote_ctx_t)); + remote->send_ctx = malloc(sizeof(remote_ctx_t)); + remote->buf = malloc(sizeof(buffer_t)); + remote->fd = fd; + remote->recv_ctx->remote = remote; + remote->recv_ctx->connected = 0; + remote->send_ctx->remote = remote; + remote->send_ctx->connected = 0; + remote->server = NULL; - remote->fd = fd; ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ); ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE); - remote->recv_ctx->remote = remote; - remote->recv_ctx->connected = 0; - remote->send_ctx->remote = remote; - remote->send_ctx->connected = 0; - remote->server = NULL; + + balloc(remote->buf, BUF_SIZE); + return remote; } @@ -1087,7 +1096,7 @@ static void close_and_free_remote(EV_P_ remote_t *remote) } } -static server_t * new_server(int fd, listen_ctx_t *listener) +static server_t *new_server(int fd, listen_ctx_t *listener) { if (verbose) { server_conn++; @@ -1098,23 +1107,19 @@ static server_t * new_server(int fd, listen_ctx_t *listener) memset(server, 0, sizeof(server_t)); - server->buf = malloc(sizeof(buffer_t)); - balloc(server->buf, BUF_SIZE); - - server->recv_ctx = malloc(sizeof(server_ctx_t)); - server->send_ctx = malloc(sizeof(server_ctx_t)); - server->fd = fd; - ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ); - ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE); - ev_timer_init(&server->recv_ctx->watcher, server_timeout_cb, - min(MAX_CONNECT_TIMEOUT, listener->timeout), listener->timeout); - server->recv_ctx->server = server; + server->recv_ctx = malloc(sizeof(server_ctx_t)); + server->send_ctx = malloc(sizeof(server_ctx_t)); + server->buf = malloc(sizeof(buffer_t)); + server->fd = fd; + server->recv_ctx->server = server; server->recv_ctx->connected = 0; - server->send_ctx->server = server; + server->send_ctx->server = server; server->send_ctx->connected = 0; - server->stage = 0; - server->query = NULL; - server->listen_ctx = listener; + server->stage = 0; + server->query = NULL; + server->listen_ctx = listener; + server->remote = NULL; + if (listener->method) { server->e_ctx = malloc(sizeof(enc_ctx_t)); server->d_ctx = malloc(sizeof(enc_ctx_t)); @@ -1124,11 +1129,18 @@ static server_t * new_server(int fd, listen_ctx_t *listener) server->e_ctx = NULL; server->d_ctx = NULL; } - server->remote = NULL; + + ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ); + ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE); + ev_timer_init(&server->recv_ctx->watcher, server_timeout_cb, + min(MAX_CONNECT_TIMEOUT, listener->timeout), listener->timeout); + + balloc(server->buf, BUF_SIZE); server->chunk = (chunk_t *)malloc(sizeof(chunk_t)); memset(server->chunk, 0, sizeof(chunk_t)); server->chunk->buf = malloc(sizeof(buffer_t)); + balloc(server->chunk->buf, BUF_SIZE); cork_dllist_add(&connections, &server->entries); @@ -1140,8 +1152,11 @@ static void free_server(server_t *server) cork_dllist_remove(&server->entries); if (server->chunk != NULL) { - bfree(server->chunk->buf); - free(server->chunk->buf); + if (server->chunk->buf != NULL) { + bfree(server->chunk->buf); + free(server->chunk->buf); + server->chunk->buf = NULL; + } free(server->chunk); server->chunk = NULL; } @@ -1199,7 +1214,7 @@ static void signal_cb(EV_P_ ev_signal *w, int revents) static void accept_cb(EV_P_ ev_io *w, int revents) { listen_ctx_t *listener = (listen_ctx_t *)w; - int serverfd = accept(listener->fd, NULL, NULL); + int serverfd = accept(listener->fd, NULL, NULL); if (serverfd == -1) { ERROR("accept"); return; @@ -1223,30 +1238,28 @@ static void accept_cb(EV_P_ ev_io *w, int revents) int main(int argc, char **argv) { - int i, c; - int pid_flags = 0; - char *user = NULL; - char *password = NULL; - char *timeout = NULL; - char *method = NULL; - char *pid_path = NULL; + int pid_flags = 0; + char *user = NULL; + char *password = NULL; + char *timeout = NULL; + char *method = NULL; + char *pid_path = NULL; char *conf_path = NULL; - char *iface = NULL; + char *iface = NULL; int server_num = 0; const char *server_host[MAX_REMOTE_NUM]; - char * nameservers[MAX_DNS_NUM + 1]; + char *nameservers[MAX_DNS_NUM + 1]; int nameserver_num = 0; - int option_index = 0; - static struct option long_options[] = - { + int option_index = 0; + static struct option long_options[] = { { "fast-open", no_argument, 0, 0 }, { "acl", required_argument, 0, 0 }, { "manager-address", required_argument, 0, 0 }, - { 0, 0, 0, 0 } + { 0, 0, 0, 0 } }; opterr = 0; @@ -1254,7 +1267,7 @@ int main(int argc, char **argv) USE_TTY(); while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:c:i:d:a:uUvA", - long_options, &option_index)) != -1) { + long_options, &option_index)) != -1) switch (c) { case 0: if (option_index == 0) { @@ -1279,7 +1292,7 @@ int main(int argc, char **argv) break; case 'f': pid_flags = 1; - pid_path = optarg; + pid_path = optarg; break; case 't': timeout = optarg; @@ -1314,7 +1327,6 @@ int main(int argc, char **argv) auth = 1; break; } - } if (opterr) { usage(); @@ -1331,9 +1343,8 @@ int main(int argc, char **argv) jconf_t *conf = read_jconf(conf_path); if (server_num == 0) { server_num = conf->remote_num; - for (i = 0; i < server_num; i++) { + for (i = 0; i < server_num; i++) server_host[i] = conf->remote_addr[i].host; - } } if (server_port == NULL) { server_port = conf->remote_port; @@ -1444,17 +1455,16 @@ int main(int argc, char **argv) resolv_init(loop, nameservers, nameserver_num); } - for (int i = 0; i < nameserver_num; i++) { + for (int i = 0; i < nameserver_num; i++) LOGI("using nameserver: %s", nameservers[i]); - } // inilitialize listen context listen_ctx_t listen_ctx_list[server_num]; // bind to each interface while (server_num > 0) { - int index = --server_num; - const char * host = server_host[index]; + int index = --server_num; + const char *host = server_host[index]; if (mode != UDP_ONLY) { // Bind to port @@ -1471,10 +1481,10 @@ int main(int argc, char **argv) // Setup proxy context listen_ctx->timeout = atoi(timeout); - listen_ctx->fd = listenfd; - listen_ctx->method = m; - listen_ctx->iface = iface; - listen_ctx->loop = loop; + listen_ctx->fd = listenfd; + listen_ctx->method = m; + listen_ctx->iface = iface; + listen_ctx->loop = loop; ev_io_init(&listen_ctx->io, accept_cb, listenfd, EV_READ); ev_io_start(loop, &listen_ctx->io); @@ -1487,7 +1497,6 @@ int main(int argc, char **argv) } LOGI("listening at %s:%s", host ? host : "*", server_port); - } if (manager_address != NULL) { diff --git a/src/shadowsocks.h b/src/shadowsocks.h index 2a2bc780..45143d26 100644 --- a/src/shadowsocks.h +++ b/src/shadowsocks.h @@ -41,21 +41,21 @@ typedef struct { } profile_t; /* An example profile - - const profile_t EXAMPLE_PROFILE = { - .remote_host = "example.com", - .local_addr = "127.0.0.1", - .method = "bf-cfb", - .password = "barfoo!", - .remote_port = 8338, - .local_port = 1080, - .timeout = 600; - .acl = NULL, - .log = NULL, - .fast_open = 0, - .mode = 0, - .verbose = 0 - }; + * + * const profile_t EXAMPLE_PROFILE = { + * .remote_host = "example.com", + * .local_addr = "127.0.0.1", + * .method = "bf-cfb", + * .password = "barfoo!", + * .remote_port = 8338, + * .local_port = 1080, + * .timeout = 600; + * .acl = NULL, + * .log = NULL, + * .fast_open = 0, + * .mode = 0, + * .verbose = 0 + * }; */ #ifdef __cplusplus diff --git a/src/socks5.h b/src/socks5.h index c119c457..901c685e 100644 --- a/src/socks5.h +++ b/src/socks5.h @@ -60,5 +60,4 @@ struct socks5_response { #pragma pack(pop) -#endif //_SOCKS5_H - +#endif // _SOCKS5_H diff --git a/src/tunnel.c b/src/tunnel.c index 21106435..6d16883e 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -77,8 +77,8 @@ static void server_send_cb(EV_P_ ev_io *w, int revents); static void remote_recv_cb(EV_P_ ev_io *w, int revents); static void remote_send_cb(EV_P_ ev_io *w, int revents); -static remote_t * new_remote(int fd, int timeout); -static server_t * new_server(int fd, int method); +static remote_t *new_remote(int fd, int timeout); +static server_t *new_server(int fd, int method); static void free_remote(remote_t *remote); static void close_and_free_remote(EV_P_ remote_t *remote); @@ -102,10 +102,11 @@ static int setnonblocking(int fd) } return fcntl(fd, F_SETFL, flags | O_NONBLOCK); } + #endif #ifdef SET_INTERFACE -int setinterface(int socket_fd, const char * interface_name) +int setinterface(int socket_fd, const char *interface_name) { struct ifreq interface; memset(&interface, 0, sizeof(interface)); @@ -114,6 +115,7 @@ int setinterface(int socket_fd, const char * interface_name) sizeof(struct ifreq)); return res; } + #endif int create_and_bind(const char *addr, const char *port) @@ -123,7 +125,7 @@ int create_and_bind(const char *addr, const char *port) int s, listen_sock; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ + hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */ s = getaddrinfo(addr, port, &hints, &result); @@ -168,8 +170,8 @@ int create_and_bind(const char *addr, const char *port) static void server_recv_cb(EV_P_ ev_io *w, int revents) { server_ctx_t *server_recv_ctx = (server_ctx_t *)w; - server_t *server = server_recv_ctx->server; - remote_t *remote = server->remote; + server_t *server = server_recv_ctx->server; + remote_t *remote = server->remote; if (remote == NULL) { close_and_free_server(EV_A_ server); @@ -228,7 +230,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } } else if (s < remote->buf->len) { remote->buf->len -= s; - remote->buf->idx = s; + remote->buf->idx = s; ev_io_stop(EV_A_ & server_recv_ctx->io); ev_io_start(EV_A_ & remote->send_ctx->io); return; @@ -238,8 +240,8 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) static void server_send_cb(EV_P_ ev_io *w, int revents) { server_ctx_t *server_send_ctx = (server_ctx_t *)w; - server_t *server = server_send_ctx->server; - remote_t *remote = server->remote; + server_t *server = server_send_ctx->server; + remote_t *remote = server->remote; if (server->buf->len == 0) { // close and free close_and_free_remote(EV_A_ remote); @@ -275,13 +277,12 @@ static void server_send_cb(EV_P_ ev_io *w, int revents) } } } - } static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) { remote_ctx_t *remote_ctx = (remote_ctx_t *)(((void *)watcher) - - sizeof(ev_io)); + - sizeof(ev_io)); remote_t *remote = remote_ctx->remote; server_t *server = remote->server; @@ -298,8 +299,8 @@ static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) static void remote_recv_cb(EV_P_ ev_io *w, int revents) { remote_ctx_t *remote_recv_ctx = (remote_ctx_t *)w; - remote_t *remote = remote_recv_ctx->remote; - server_t *server = remote->server; + remote_t *remote = remote_recv_ctx->remote; + server_t *server = remote->server; ssize_t r = recv(remote->fd, server->buf->array, BUF_SIZE, 0); @@ -349,7 +350,7 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) } } else if (s < server->buf->len) { server->buf->len -= s; - server->buf->idx = s; + server->buf->idx = s; ev_io_stop(EV_A_ & remote_recv_ctx->io); ev_io_start(EV_A_ & server->send_ctx->io); return; @@ -359,8 +360,8 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) static void remote_send_cb(EV_P_ ev_io *w, int revents) { remote_ctx_t *remote_send_ctx = (remote_ctx_t *)w; - remote_t *remote = remote_send_ctx->remote; - server_t *server = remote->server; + remote_t *remote = remote_send_ctx->remote; + server_t *server = remote->server; if (!remote_send_ctx->connected) { struct sockaddr_storage addr; @@ -485,31 +486,32 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents) ev_io_start(EV_A_ & server->recv_ctx->io); } } - } } -static remote_t * new_remote(int fd, int timeout) +static remote_t *new_remote(int fd, int timeout) { remote_t *remote; remote = malloc(sizeof(remote_t)); memset(remote, 0, sizeof(remote_t)); - remote->buf = malloc(BUF_SIZE); - remote->recv_ctx = malloc(sizeof(remote_ctx_t)); - remote->send_ctx = malloc(sizeof(remote_ctx_t)); - remote->fd = fd; + remote->buf = malloc(sizeof(buffer_t)); + remote->recv_ctx = malloc(sizeof(remote_ctx_t)); + remote->send_ctx = malloc(sizeof(remote_ctx_t)); + remote->fd = fd; + remote->recv_ctx->remote = remote; + remote->recv_ctx->connected = 0; + remote->send_ctx->remote = remote; + remote->send_ctx->connected = 0; + ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ); ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE); ev_timer_init(&remote->send_ctx->watcher, remote_timeout_cb, min(MAX_CONNECT_TIMEOUT, timeout), 0); - remote->recv_ctx->remote = remote; - remote->recv_ctx->connected = 0; - remote->send_ctx->remote = remote; - remote->send_ctx->connected = 0; - remote->buf->len = 0; - remote->buf->idx = 0; + + balloc(remote->buf, BUF_SIZE); + return remote; } @@ -540,20 +542,20 @@ static void close_and_free_remote(EV_P_ remote_t *remote) } } -static server_t * new_server(int fd, int method) +static server_t *new_server(int fd, int method) { server_t *server; - server = malloc(sizeof(server_t)); - server->buf = malloc(BUF_SIZE); - server->recv_ctx = malloc(sizeof(server_ctx_t)); - server->send_ctx = malloc(sizeof(server_ctx_t)); - server->fd = fd; - ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ); - ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE); - server->recv_ctx->server = server; + + server = malloc(sizeof(server_t)); + server->buf = malloc(sizeof(buffer_t)); + server->recv_ctx = malloc(sizeof(server_ctx_t)); + server->send_ctx = malloc(sizeof(server_ctx_t)); + server->fd = fd; + server->recv_ctx->server = server; server->recv_ctx->connected = 0; - server->send_ctx->server = server; + server->send_ctx->server = server; server->send_ctx->connected = 0; + if (method) { server->e_ctx = malloc(sizeof(struct enc_ctx)); server->d_ctx = malloc(sizeof(struct enc_ctx)); @@ -563,8 +565,12 @@ static server_t * new_server(int fd, int method) server->e_ctx = NULL; server->d_ctx = NULL; } - server->buf->len = 0; - server->buf->idx = 0; + + balloc(server->buf, BUF_SIZE); + + ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ); + ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE); + return server; } @@ -605,7 +611,7 @@ static void close_and_free_server(EV_P_ server_t *server) static void accept_cb(EV_P_ ev_io *w, int revents) { struct listen_ctx *listener = (struct listen_ctx *)w; - int serverfd = accept(listener->fd, NULL, NULL); + int serverfd = accept(listener->fd, NULL, NULL); if (serverfd == -1) { ERROR("accept"); return; @@ -617,7 +623,7 @@ static void accept_cb(EV_P_ ev_io *w, int revents) setsockopt(serverfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)); #endif - int index = rand() % listener->remote_num; + int index = rand() % listener->remote_num; struct sockaddr *remote_addr = listener->remote_addr[index]; int remotefd = socket(remote_addr->sa_family, SOCK_STREAM, IPPROTO_TCP); @@ -652,8 +658,8 @@ static void accept_cb(EV_P_ ev_io *w, int revents) server_t *server = new_server(serverfd, listener->method); remote_t *remote = new_remote(remotefd, listener->timeout); server->destaddr = listener->tunnel_addr; - server->remote = remote; - remote->server = server; + server->remote = remote; + remote->server = server; connect(remotefd, remote_addr, get_sockaddr_len(remote_addr)); // listen to remote connected event @@ -663,18 +669,17 @@ static void accept_cb(EV_P_ ev_io *w, int revents) int main(int argc, char **argv) { - int i, c; - int pid_flags = 0; - char *user = NULL; + int pid_flags = 0; + char *user = NULL; char *local_port = NULL; char *local_addr = NULL; - char *password = NULL; - char *timeout = NULL; - char *method = NULL; - char *pid_path = NULL; - char *conf_path = NULL; - char *iface = NULL; + char *password = NULL; + char *timeout = NULL; + char *method = NULL; + char *pid_path = NULL; + char *conf_path = NULL; + char *iface = NULL; int remote_num = 0; ss_addr_t remote_addr[MAX_REMOTE_NUM]; @@ -695,7 +700,7 @@ int main(int argc, char **argv) switch (c) { case 's': if (remote_num < MAX_REMOTE_NUM) { - remote_addr[remote_num].host = optarg; + remote_addr[remote_num].host = optarg; remote_addr[remote_num++].port = NULL; } break; @@ -710,7 +715,7 @@ int main(int argc, char **argv) break; case 'f': pid_flags = 1; - pid_path = optarg; + pid_path = optarg; break; case 't': timeout = optarg; @@ -768,9 +773,8 @@ int main(int argc, char **argv) jconf_t *conf = read_jconf(conf_path); if (remote_num == 0) { remote_num = conf->remote_num; - for (i = 0; i < remote_num; i++) { + for (i = 0; i < remote_num; i++) remote_addr[i] = conf->remote_addr[i]; - } } if (remote_port == NULL) { remote_port = conf->remote_port; @@ -840,7 +844,7 @@ int main(int argc, char **argv) // Setup proxy context struct listen_ctx listen_ctx; listen_ctx.tunnel_addr = tunnel_addr; - listen_ctx.remote_num = remote_num; + listen_ctx.remote_num = remote_num; listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *) * remote_num); for (i = 0; i < remote_num; i++) { char *host = remote_addr[i].host; @@ -854,8 +858,8 @@ int main(int argc, char **argv) listen_ctx.remote_addr[i] = (struct sockaddr *)storage; } listen_ctx.timeout = atoi(timeout); - listen_ctx.iface = iface; - listen_ctx.method = m; + listen_ctx.iface = iface; + listen_ctx.method = m; struct ev_loop *loop = EV_DEFAULT; @@ -904,4 +908,3 @@ int main(int argc, char **argv) return 0; } - diff --git a/src/udprelay.c b/src/udprelay.c index c25dc7e6..52ef8637 100644 --- a/src/udprelay.c +++ b/src/udprelay.c @@ -92,7 +92,7 @@ static char *hash_key(const int af, const struct sockaddr_storage *addr); static void query_resolve_cb(struct sockaddr *addr, void *data); #endif static void close_and_free_remote(EV_P_ remote_ctx_t *ctx); -static remote_ctx_t * new_remote(int fd, server_ctx_t * server_ctx); +static remote_ctx_t *new_remote(int fd, server_ctx_t *server_ctx); extern int verbose; extern int vpn; @@ -101,7 +101,7 @@ extern uint64_t tx; extern uint64_t rx; #endif -static int server_num = 0; +static int server_num = 0; static server_ctx_t *server_ctx_list[MAX_REMOTE_NUM] = { NULL }; #ifndef __MINGW32__ @@ -113,10 +113,11 @@ static int setnonblocking(int fd) } return fcntl(fd, F_SETFL, flags | O_NONBLOCK); } + #endif #ifdef SET_INTERFACE -static int setinterface(int socket_fd, const char * interface_name) +static int setinterface(int socket_fd, const char *interface_name) { struct ifreq interface; memset(&interface, 0, sizeof(interface)); @@ -125,6 +126,7 @@ static int setinterface(int socket_fd, const char * interface_name) sizeof(struct ifreq)); return res; } + #endif #if defined(UDPRELAY_REMOTE) && defined(SO_BROADCAST) @@ -133,6 +135,7 @@ static int set_broadcast(int socket_fd) int opt = 1; return setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt)); } + #endif #ifdef SO_NOSIGPIPE @@ -141,6 +144,7 @@ static int set_nosigpipe(int socket_fd) int opt = 1; return setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)); } + #endif #ifdef UDPRELAY_REDIR @@ -162,7 +166,6 @@ static int get_dstaddr(struct msghdr *msg, struct sockaddr_storage *dstaddr) memcpy(dstaddr, CMSG_DATA(cmsg), sizeof(struct sockaddr_in)); dstaddr->ss_family = AF_INET; return 0; - } else if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IP_RECVORIGDSTADDR) { memcpy(dstaddr, CMSG_DATA(cmsg), sizeof(struct sockaddr_in6)); dstaddr->ss_family = AF_INET6; @@ -172,6 +175,7 @@ static int get_dstaddr(struct msghdr *msg, struct sockaddr_storage *dstaddr) return 1; } + #endif #define HASH_KEY_LEN sizeof(struct sockaddr_storage) + sizeof(int) @@ -191,11 +195,10 @@ static char *hash_key(const int af, const struct sockaddr_storage *addr) static int construct_udprealy_header(const struct sockaddr_storage *in_addr, char *addr_header) { - int addr_header_len = 0; if (in_addr->ss_family == AF_INET) { struct sockaddr_in *addr = (struct sockaddr_in *)in_addr; - size_t addr_len = sizeof(struct in_addr); + size_t addr_len = sizeof(struct in_addr); addr_header[addr_header_len++] = 1; memcpy(addr_header + addr_header_len, &addr->sin_addr, addr_len); addr_header_len += addr_len; @@ -203,7 +206,7 @@ static int construct_udprealy_header(const struct sockaddr_storage *in_addr, addr_header_len += 2; } else if (in_addr->ss_family == AF_INET6) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)in_addr; - size_t addr_len = sizeof(struct in6_addr); + size_t addr_len = sizeof(struct in6_addr); addr_header[addr_header_len++] = 4; memcpy(addr_header + addr_header_len, &addr->sin6_addr, addr_len); addr_header_len += addr_len; @@ -214,15 +217,15 @@ static int construct_udprealy_header(const struct sockaddr_storage *in_addr, } return addr_header_len; } + #endif -static int parse_udprealy_header(const char * buf, const int buf_len, +static int parse_udprealy_header(const char *buf, const int buf_len, int *auth, char *host, char *port, struct sockaddr_storage *storage) { - const uint8_t atyp = *(uint8_t *)buf; - int offset = 1; + int offset = 1; if (auth != NULL) { *auth |= (atyp & ONETIMEAUTH_FLAG); @@ -236,8 +239,8 @@ static int parse_udprealy_header(const char * buf, const int buf_len, if (storage != NULL) { struct sockaddr_in *addr = (struct sockaddr_in *)storage; addr->sin_family = AF_INET; - addr->sin_addr = *(struct in_addr *)(buf + offset); - addr->sin_port = *(uint16_t *)(buf + offset + in_addr_len); + addr->sin_addr = *(struct in_addr *)(buf + offset); + addr->sin_port = *(uint16_t *)(buf + offset + in_addr_len); } if (host != NULL) { dns_ntop(AF_INET, (const void *)(buf + offset), @@ -257,12 +260,12 @@ static int parse_udprealy_header(const char * buf, const int buf_len, if (ip.version == 4) { struct sockaddr_in *addr = (struct sockaddr_in *)storage; dns_pton(AF_INET, tmp, &(addr->sin_addr)); - addr->sin_port = *(uint16_t *)(buf + offset + 1 + name_len); + addr->sin_port = *(uint16_t *)(buf + offset + 1 + name_len); addr->sin_family = AF_INET; } else if (ip.version == 6) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)storage; dns_pton(AF_INET, tmp, &(addr->sin6_addr)); - addr->sin6_port = *(uint16_t *)(buf + offset + 1 + name_len); + addr->sin6_port = *(uint16_t *)(buf + offset + 1 + name_len); addr->sin6_family = AF_INET6; } } @@ -279,8 +282,8 @@ static int parse_udprealy_header(const char * buf, const int buf_len, if (storage != NULL) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)storage; addr->sin6_family = AF_INET6; - addr->sin6_addr = *(struct in6_addr *)(buf + offset); - addr->sin6_port = *(uint16_t *)(buf + offset + in6_addr_len); + addr->sin6_addr = *(struct in6_addr *)(buf + offset); + addr->sin6_port = *(uint16_t *)(buf + offset + in6_addr_len); } if (host != NULL) { dns_ntop(AF_INET6, (const void *)(buf + offset), @@ -308,7 +311,7 @@ static char *get_addr_str(const struct sockaddr *sa) static char s[SS_ADDRSTRLEN]; memset(s, 0, SS_ADDRSTRLEN); char addr[INET6_ADDRSTRLEN] = { 0 }; - char port[PORTSTRLEN] = { 0 }; + char port[PORTSTRLEN] = { 0 }; uint16_t p; switch (sa->sa_family) { @@ -339,7 +342,6 @@ static char *get_addr_str(const struct sockaddr *sa) return s; } - int create_remote_socket(int ipv6) { int remote_sock; @@ -349,9 +351,9 @@ int create_remote_socket(int ipv6) struct sockaddr_in6 addr; memset(&addr, 0, sizeof(addr)); addr.sin6_family = AF_INET6; - addr.sin6_addr = in6addr_any; - addr.sin6_port = 0; - remote_sock = socket(AF_INET6, SOCK_DGRAM, 0); + addr.sin6_addr = in6addr_any; + addr.sin6_port = 0; + remote_sock = socket(AF_INET6, SOCK_DGRAM, 0); if (remote_sock == -1) { ERROR("[udp] cannot create socket"); return -1; @@ -364,10 +366,10 @@ int create_remote_socket(int ipv6) // Or else bind to IPv4 struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; + addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_port = 0; - remote_sock = socket(AF_INET, SOCK_DGRAM, 0); + addr.sin_port = 0; + remote_sock = socket(AF_INET, SOCK_DGRAM, 0); if (remote_sock == -1) { ERROR("[udp] cannot create socket"); return -1; @@ -388,9 +390,9 @@ int create_server_socket(const char *host, const char *port) int s, server_sock; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ + hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */ - hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */ + hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */ hints.ai_protocol = IPPROTO_UDP; s = getaddrinfo(host, port, &hints, &result); @@ -402,10 +404,10 @@ int create_server_socket(const char *host, const char *port) rp = result; /* - On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with - AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to - return a list of addresses to listen on, but it is impossible to listen on - 0.0.0.0 and :: at the same time, if :: implies dualstack mode. + * On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with + * AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to + * return a list of addresses to listen on, but it is impossible to listen on + * 0.0.0.0 and :: at the same time, if :: implies dualstack mode. */ if (!host) { ipv4v6bindall = result; @@ -472,20 +474,25 @@ remote_ctx_t *new_remote(int fd, server_ctx_t *server_ctx) remote_ctx_t *ctx = malloc(sizeof(remote_ctx_t)); memset(ctx, 0, sizeof(remote_ctx_t)); - ctx->fd = fd; + ctx->fd = fd; ctx->server_ctx = server_ctx; + ev_io_init(&ctx->io, remote_recv_cb, fd, EV_READ); ev_timer_init(&ctx->watcher, remote_timeout_cb, server_ctx->timeout, server_ctx->timeout); + return ctx; } -server_ctx_t * new_server_ctx(int fd) +server_ctx_t *new_server_ctx(int fd) { server_ctx_t *ctx = malloc(sizeof(server_ctx_t)); memset(ctx, 0, sizeof(server_ctx_t)); + ctx->fd = fd; + ev_io_init(&ctx->io, server_recv_cb, fd, EV_READ); + return ctx; } @@ -497,6 +504,7 @@ struct query_ctx *new_query_ctx(char *buf, size_t len) ctx->buf = malloc(sizeof(buffer_t)); balloc(ctx->buf, len); memcpy(ctx->buf->array, buf, len); + ctx->buf->len = len; return ctx; } @@ -530,7 +538,7 @@ void close_and_free_remote(EV_P_ remote_ctx_t *ctx) static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) { remote_ctx_t *remote_ctx = (remote_ctx_t *)(((void *)watcher) - - sizeof(ev_io)); + - sizeof(ev_io)); if (verbose) { LOGI("[udp] connection timeout"); @@ -544,7 +552,7 @@ static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) static void query_resolve_cb(struct sockaddr *addr, void *data) { struct query_ctx *query_ctx = (struct query_ctx *)data; - struct ev_loop *loop = query_ctx->server_ctx->loop; + struct ev_loop *loop = query_ctx->server_ctx->loop; if (verbose) { LOGI("[udp] udns resolved"); @@ -556,7 +564,7 @@ static void query_resolve_cb(struct sockaddr *addr, void *data) LOGE("[udp] udns returned an error"); } else { remote_ctx_t *remote_ctx = query_ctx->remote_ctx; - int cache_hit = 0; + int cache_hit = 0; // Lookup in the conn cache if (remote_ctx == NULL) { @@ -579,9 +587,9 @@ static void query_resolve_cb(struct sockaddr *addr, void *data) setinterface(remotefd, query_ctx->server_ctx->iface); } #endif - remote_ctx = new_remote(remotefd, query_ctx->server_ctx); - remote_ctx->src_addr = query_ctx->src_addr; - remote_ctx->server_ctx = query_ctx->server_ctx; + remote_ctx = new_remote(remotefd, query_ctx->server_ctx); + remote_ctx->src_addr = query_ctx->src_addr; + remote_ctx->server_ctx = query_ctx->server_ctx; remote_ctx->addr_header_len = query_ctx->addr_header_len; memcpy(remote_ctx->addr_header, query_ctx->addr_header, query_ctx->addr_header_len); @@ -594,8 +602,8 @@ static void query_resolve_cb(struct sockaddr *addr, void *data) if (remote_ctx != NULL) { size_t addr_len = get_sockaddr_len(addr); - int s = sendto(remote_ctx->fd, query_ctx->buf->array, query_ctx->buf->len, - 0, addr, addr_len); + int s = sendto(remote_ctx->fd, query_ctx->buf->array, query_ctx->buf->len, + 0, addr, addr_len); if (s == -1) { ERROR("[udp] sendto_remote"); @@ -612,12 +620,12 @@ static void query_resolve_cb(struct sockaddr *addr, void *data) } } } - } // clean up close_and_free_query(EV_A_ query_ctx); } + #endif static void remote_recv_cb(EV_P_ ev_io *w, int revents) @@ -705,12 +713,12 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) rx += buf->len; char addr_header_buf[256]; - char *addr_header = remote_ctx->addr_header; + char *addr_header = remote_ctx->addr_header; int addr_header_len = remote_ctx->addr_header_len; if (remote_ctx->af == AF_INET || remote_ctx->af == AF_INET6) { addr_header_len = construct_udprealy_header(&src_addr, addr_header_buf); - addr_header = addr_header_buf; + addr_header = addr_header_buf; } // Construct packet @@ -775,7 +783,7 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) // triger the timer ev_timer_again(EV_A_ & remote_ctx->watcher); - CLEAN_UP: +CLEAN_UP: bfree(buf); free(buf); @@ -791,7 +799,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) balloc(buf, BUF_SIZE); socklen_t src_addr_len = sizeof(struct sockaddr_storage); - unsigned int offset = 0; + unsigned int offset = 0; #ifdef UDPRELAY_REDIR char control_buffer[64] = { 0 }; @@ -800,15 +808,15 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) struct sockaddr_storage dst_addr; memset(&dst_addr, 0, sizeof(struct sockaddr_storage)); - msg.msg_name = &src_addr; - msg.msg_namelen = src_addr_len; - msg.msg_control = control_buffer; + msg.msg_name = &src_addr; + msg.msg_namelen = src_addr_len; + msg.msg_control = control_buffer; msg.msg_controllen = sizeof(control_buffer); iov[0].iov_base = buf->array; - iov[0].iov_len = BUF_SIZE; - msg.msg_iov = iov; - msg.msg_iovlen = 1; + iov[0].iov_len = BUF_SIZE; + msg.msg_iov = iov; + msg.msg_iovlen = 1; ssize_t buf_len = recvmsg(server_ctx->fd, &msg, 0); if (buf_len == -1) { @@ -824,7 +832,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) src_addr_len = msg.msg_namelen; #else buf->len = recvfrom(server_ctx->fd, buf->array, BUF_SIZE, - 0, (struct sockaddr *)&src_addr, &src_addr_len); + 0, (struct sockaddr *)&src_addr, &src_addr_len); if (buf->len == -1) { // error on recv @@ -907,7 +915,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) #ifdef UDPRELAY_REDIR char addr_header[256] = { 0 }; - int addr_header_len = construct_udprealy_header(&dst_addr, addr_header); + int addr_header_len = construct_udprealy_header(&dst_addr, addr_header); if (addr_header_len == 0) { LOGE("[udp] failed to parse tproxy addr"); @@ -925,11 +933,11 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) #elif UDPRELAY_TUNNEL char addr_header[256] = { 0 }; - char *host = server_ctx->tunnel_addr.host; - char *port = server_ctx->tunnel_addr.port; - uint16_t port_num = (uint16_t)atoi(port); + char *host = server_ctx->tunnel_addr.host; + char *port = server_ctx->tunnel_addr.port; + uint16_t port_num = (uint16_t)atoi(port); uint16_t port_net_num = htons(port_num); - int addr_header_len = 0; + int addr_header_len = 0; struct cork_ip ip; if (cork_ip_init(&ip, host) != -1) { @@ -981,7 +989,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) #else char host[256] = { 0 }; - char port[64] = { 0 }; + char port[64] = { 0 }; struct sockaddr_storage dst_addr; memset(&dst_addr, 0, sizeof(struct sockaddr_storage)); @@ -994,7 +1002,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } char *addr_header = buf->array + offset; - char *key = hash_key(dst_addr.ss_family, &src_addr); + char *key = hash_key(dst_addr.ss_family, &src_addr); #endif struct cache *conn_cache = server_ctx->conn_cache; @@ -1051,7 +1059,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) #endif const struct sockaddr *remote_addr = server_ctx->remote_addr; - const int remote_addr_len = server_ctx->remote_addr_len; + const int remote_addr_len = server_ctx->remote_addr_len; if (remote_ctx == NULL) { // Bind to any port @@ -1082,9 +1090,9 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) #endif // Init remote_ctx - remote_ctx = new_remote(remotefd, server_ctx); - remote_ctx->src_addr = src_addr; - remote_ctx->af = remote_addr->sa_family; + remote_ctx = new_remote(remotefd, server_ctx); + remote_ctx->src_addr = src_addr; + remote_ctx->af = remote_addr->sa_family; remote_ctx->addr_header_len = addr_header_len; memcpy(remote_ctx->addr_header, addr_header, addr_header_len); @@ -1094,7 +1102,6 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) // Start remote io ev_io_start(EV_A_ & remote_ctx->io); ev_timer_start(EV_A_ & remote_ctx->watcher); - } if (offset > 0) { @@ -1121,7 +1128,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) #else - int cache_hit = 0; + int cache_hit = 0; int need_query = 0; if (remote_ctx != NULL) { @@ -1149,9 +1156,9 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) setinterface(remotefd, server_ctx->iface); } #endif - remote_ctx = new_remote(remotefd, server_ctx); - remote_ctx->src_addr = src_addr; - remote_ctx->server_ctx = server_ctx; + remote_ctx = new_remote(remotefd, server_ctx); + remote_ctx->src_addr = src_addr; + remote_ctx->server_ctx = server_ctx; remote_ctx->addr_header_len = addr_header_len; memcpy(remote_ctx->addr_header, addr_header, addr_header_len); } else { @@ -1163,9 +1170,9 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) if (remote_ctx != NULL && !need_query) { size_t addr_len = get_sockaddr_len((struct sockaddr *)&dst_addr); - int s = sendto(remote_ctx->fd, buf->array + addr_header_len, - buf->len - addr_header_len, 0, - (struct sockaddr *)&dst_addr, addr_len); + int s = sendto(remote_ctx->fd, buf->array + addr_header_len, + buf->len - addr_header_len, 0, + (struct sockaddr *)&dst_addr, addr_len); if (s == -1) { ERROR("[udp] sendto_remote"); @@ -1186,15 +1193,15 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } else { struct addrinfo hints; memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; + hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; struct query_ctx *query_ctx = new_query_ctx(buf->array + addr_header_len, buf->len - addr_header_len); - query_ctx->server_ctx = server_ctx; + query_ctx->server_ctx = server_ctx; query_ctx->addr_header_len = addr_header_len; - query_ctx->src_addr = src_addr; + query_ctx->src_addr = src_addr; memcpy(query_ctx->addr_header, addr_header, addr_header_len); if (need_query) { @@ -1212,7 +1219,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) } #endif - CLEAN_UP: +CLEAN_UP: bfree(buf); free(buf); } @@ -1258,13 +1265,13 @@ int init_udprelay(const char *server_host, const char *server_port, #ifdef UDPRELAY_REMOTE server_ctx->loop = loop; #endif - server_ctx->auth = auth; - server_ctx->timeout = max(timeout, MIN_UDP_TIMEOUT); - server_ctx->method = method; - server_ctx->iface = iface; + server_ctx->auth = auth; + server_ctx->timeout = max(timeout, MIN_UDP_TIMEOUT); + server_ctx->method = method; + server_ctx->iface = iface; server_ctx->conn_cache = conn_cache; #ifdef UDPRELAY_LOCAL - server_ctx->remote_addr = remote_addr; + server_ctx->remote_addr = remote_addr; server_ctx->remote_addr_len = remote_addr_len; #ifdef UDPRELAY_TUNNEL server_ctx->tunnel_addr = tunnel_addr; diff --git a/src/uthash.h b/src/uthash.h index d136b741..1d9d0d79 100644 --- a/src/uthash.h +++ b/src/uthash.h @@ -1,24 +1,24 @@ /* - Copyright (c) 2003-2013, Troy D. Hanson http://troydhanson.github.com/uthash/ - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - + * Copyright (c) 2003-2013, Troy D. Hanson http://troydhanson.github.com/uthash/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER - OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * notice, this list of conditions and the following disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef UTHASH_H @@ -29,9 +29,9 @@ #include /* exit() */ /* These macros use decltype or the earlier __typeof GNU extension. - As decltype is only available in newer compilers (VS2010 or gcc 4.3+ - when compiling c++ source) this code uses whatever method is needed - or, for VS2008 where neither is available, uses casting workarounds. */ + * As decltype is only available in newer compilers (VS2010 or gcc 4.3+ + * when compiling c++ source) this code uses whatever method is needed + * or, for VS2008 where neither is available, uses casting workarounds. */ #ifdef _MSC_VER /* MS compiler */ #if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */ #define DECLTYPE(x) (decltype(x)) @@ -100,7 +100,7 @@ typedef unsigned char uint8_t; _hf_bkt); \ if (HASH_BLOOM_TEST((head)->hh.tbl, _hf_hashv)) { \ HASH_FIND_IN_BKT((head)->hh.tbl, hh, \ - (head)->hh.tbl->buckets[ _hf_bkt ], \ + (head)->hh.tbl->buckets[_hf_bkt], \ keyptr, keylen, out); \ } \ } \ @@ -114,8 +114,8 @@ typedef unsigned char uint8_t; #define HASH_BLOOM_MAKE(tbl) \ do { \ (tbl)->bloom_nbits = HASH_BLOOM; \ - (tbl)->bloom_bv = (uint8_t *)uthash_malloc(HASH_BLOOM_BYTELEN); \ - if (!((tbl)->bloom_bv)) { uthash_fatal( "out of memory"); } \ + (tbl)->bloom_bv = (uint8_t *)uthash_malloc(HASH_BLOOM_BYTELEN); \ + if (!((tbl)->bloom_bv)) { uthash_fatal("out of memory"); } \ memset((tbl)->bloom_bv, 0, HASH_BLOOM_BYTELEN); \ (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \ } while (0) @@ -148,15 +148,15 @@ typedef unsigned char uint8_t; do { \ (head)->hh.tbl = (UT_hash_table *)uthash_malloc( \ sizeof(UT_hash_table)); \ - if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \ + if (!((head)->hh.tbl)) { uthash_fatal("out of memory"); } \ memset((head)->hh.tbl, 0, sizeof(UT_hash_table)); \ - (head)->hh.tbl->tail = &((head)->hh); \ - (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \ + (head)->hh.tbl->tail = &((head)->hh); \ + (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \ (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \ - (head)->hh.tbl->hho = (char *)(&(head)->hh) - (char *)(head); \ - (head)->hh.tbl->buckets = (UT_hash_bucket *)uthash_malloc( \ + (head)->hh.tbl->hho = (char *)(&(head)->hh) - (char *)(head); \ + (head)->hh.tbl->buckets = (UT_hash_bucket *)uthash_malloc( \ HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \ - if (!(head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \ + if (!(head)->hh.tbl->buckets) { uthash_fatal("out of memory"); } \ memset((head)->hh.tbl->buckets, 0, \ HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \ HASH_BLOOM_MAKE((head)->hh.tbl); \ @@ -179,17 +179,17 @@ typedef unsigned char uint8_t; #define HASH_ADD_KEYPTR(hh, head, keyptr, keylen_in, add) \ do { \ unsigned _ha_bkt; \ - (add)->hh.next = NULL; \ - (add)->hh.key = (char *)keyptr; \ + (add)->hh.next = NULL; \ + (add)->hh.key = (char *)keyptr; \ (add)->hh.keylen = (unsigned)keylen_in; \ if (!(head)) { \ - head = (add); \ + head = (add); \ (head)->hh.prev = NULL; \ HASH_MAKE_TABLE(hh, head); \ } else { \ (head)->hh.tbl->tail->next = (add); \ - (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \ - (head)->hh.tbl->tail = &((add)->hh); \ + (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \ + (head)->hh.tbl->tail = &((add)->hh); \ } \ (head)->hh.tbl->num_items++; \ (add)->hh.tbl = (head)->hh.tbl; \ @@ -201,7 +201,7 @@ typedef unsigned char uint8_t; HASH_FSCK(hh, head); \ } while (0) -#define HASH_TO_BKT( hashv, num_bkts, bkt ) \ +#define HASH_TO_BKT(hashv, num_bkts, bkt) \ do { \ bkt = ((hashv) & ((num_bkts) - 1)); \ } while (0) @@ -222,10 +222,10 @@ typedef unsigned char uint8_t; do { \ unsigned _hd_bkt; \ struct UT_hash_handle *_hd_hh_del; \ - if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \ + if (((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL)) { \ uthash_free((head)->hh.tbl->buckets, \ (head)->hh.tbl->num_buckets * \ - sizeof(struct UT_hash_bucket) ); \ + sizeof(struct UT_hash_bucket)); \ HASH_BLOOM_FREE((head)->hh.tbl); \ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ head = NULL; \ @@ -249,15 +249,14 @@ typedef unsigned char uint8_t; (head)->hh.tbl->hho))->prev = \ _hd_hh_del->prev; \ } \ - HASH_TO_BKT( _hd_hh_del->hashv, (head)->hh.tbl->num_buckets, \ - _hd_bkt); \ + HASH_TO_BKT(_hd_hh_del->hashv, (head)->hh.tbl->num_buckets, \ + _hd_bkt); \ HASH_DEL_IN_BKT(hh, (head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \ (head)->hh.tbl->num_items--; \ } \ HASH_FSCK(hh, head); \ } while (0) - /* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */ #define HASH_FIND_STR(head, findstr, out) \ HASH_FIND(hh, head, findstr, strlen(findstr), out) @@ -295,16 +294,16 @@ typedef unsigned char uint8_t; _count = 0; \ for (_bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; _bkt_i++) { \ _bkt_count = 0; \ - _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \ - _prev = NULL; \ + _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \ + _prev = NULL; \ while (_thh) { \ if (_prev != (char *)(_thh->hh_prev)) { \ HASH_OOPS("invalid hh_prev %p, actual %p\n", \ - _thh->hh_prev, _prev ); \ + _thh->hh_prev, _prev); \ } \ _bkt_count++; \ _prev = (char *)(_thh); \ - _thh = _thh->hh_next; \ + _thh = _thh->hh_next; \ } \ _count += _bkt_count; \ if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \ @@ -315,26 +314,26 @@ typedef unsigned char uint8_t; } \ if (_count != (head)->hh.tbl->num_items) { \ HASH_OOPS("invalid hh item count %d, actual %d\n", \ - (head)->hh.tbl->num_items, _count ); \ + (head)->hh.tbl->num_items, _count); \ } \ /* traverse hh in app order; check next/prev integrity, count */ \ _count = 0; \ - _prev = NULL; \ - _thh = &(head)->hh; \ + _prev = NULL; \ + _thh = &(head)->hh; \ while (_thh) { \ _count++; \ if (_prev != (char *)(_thh->prev)) { \ HASH_OOPS("invalid prev %p, actual %p\n", \ - _thh->prev, _prev ); \ + _thh->prev, _prev); \ } \ _prev = (char *)ELMT_FROM_HH((head)->hh.tbl, _thh); \ - _thh = ( _thh->next ? (UT_hash_handle *)((char *)(_thh->next) + \ - (head)->hh.tbl->hho) : \ - NULL ); \ + _thh = (_thh->next ? (UT_hash_handle *)((char *)(_thh->next) + \ + (head)->hh.tbl->hho) : \ + NULL); \ } \ if (_count != (head)->hh.tbl->num_items) { \ HASH_OOPS("invalid app item count %d, actual %d\n", \ - (head)->hh.tbl->num_items, _count ); \ + (head)->hh.tbl->num_items, _count); \ } \ } \ } while (0) @@ -367,13 +366,12 @@ typedef unsigned char uint8_t; #define HASH_BER(key, keylen, num_bkts, hashv, bkt) \ do { \ unsigned _hb_keylen = keylen; \ - char *_hb_key = (char *)(key); \ + char *_hb_key = (char *)(key); \ (hashv) = 0; \ while (_hb_keylen--) { (hashv) = ((hashv) * 33) + *_hb_key++; } \ bkt = (hashv) & (num_bkts - 1); \ } while (0) - /* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */ #define HASH_SAX(key, keylen, num_bkts, hashv, bkt) \ @@ -409,20 +407,20 @@ typedef unsigned char uint8_t; hashv += (hashv << 3); \ hashv ^= (hashv >> 11); \ hashv += (hashv << 15); \ - bkt = hashv & (num_bkts - 1); \ + bkt = hashv & (num_bkts - 1); \ } while (0) #define HASH_JEN_MIX(a, b, c) \ do { \ - a -= b; a -= c; a ^= ( c >> 13 ); \ - b -= c; b -= a; b ^= ( a << 8 ); \ - c -= a; c -= b; c ^= ( b >> 13 ); \ - a -= b; a -= c; a ^= ( c >> 12 ); \ - b -= c; b -= a; b ^= ( a << 16 ); \ - c -= a; c -= b; c ^= ( b >> 5 ); \ - a -= b; a -= c; a ^= ( c >> 3 ); \ - b -= c; b -= a; b ^= ( a << 10 ); \ - c -= a; c -= b; c ^= ( b >> 15 ); \ + a -= b; a -= c; a ^= (c >> 13); \ + b -= c; b -= a; b ^= (a << 8); \ + c -= a; c -= b; c ^= (b >> 13); \ + a -= b; a -= c; a ^= (c >> 12); \ + b -= c; b -= a; b ^= (a << 16); \ + c -= a; c -= b; c ^= (b >> 5); \ + a -= b; a -= c; a ^= (c >> 3); \ + b -= c; b -= a; b ^= (a << 10); \ + c -= a; c -= b; c ^= (b >> 15); \ } while (0) #define HASH_JEN(key, keylen, num_bkts, hashv, bkt) \ @@ -433,34 +431,45 @@ typedef unsigned char uint8_t; _hj_i = _hj_j = 0x9e3779b9; \ _hj_k = (unsigned)keylen; \ while (_hj_k >= 12) { \ - _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \ - + ( (unsigned)_hj_key[2] << 16 ) \ - + ( (unsigned)_hj_key[3] << 24 ) ); \ - _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \ - + ( (unsigned)_hj_key[6] << 16 ) \ - + ( (unsigned)_hj_key[7] << 24 ) ); \ - hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \ - + ( (unsigned)_hj_key[10] << 16 ) \ - + ( (unsigned)_hj_key[11] << 24 ) ); \ + _hj_i += (_hj_key[0] + ((unsigned)_hj_key[1] << 8) \ + + ((unsigned)_hj_key[2] << 16) \ + + ((unsigned)_hj_key[3] << 24)); \ + _hj_j += (_hj_key[4] + ((unsigned)_hj_key[5] << 8) \ + + ((unsigned)_hj_key[6] << 16) \ + + ((unsigned)_hj_key[7] << 24)); \ + hashv += (_hj_key[8] + ((unsigned)_hj_key[9] << 8) \ + + ((unsigned)_hj_key[10] << 16) \ + + ((unsigned)_hj_key[11] << 24)); \ \ HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ \ _hj_key += 12; \ - _hj_k -= 12; \ + _hj_k -= 12; \ } \ hashv += keylen; \ switch (_hj_k) { \ - case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); \ - case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); \ - case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); \ - case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); \ - case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); \ - case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); \ - case 5: _hj_j += _hj_key[4]; \ - case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); \ - case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); \ - case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); \ - case 1: _hj_i += _hj_key[0]; \ + case 11: \ + hashv += ((unsigned)_hj_key[10] << 24); \ + case 10: \ + hashv += ((unsigned)_hj_key[9] << 16); \ + case 9: \ + hashv += ((unsigned)_hj_key[8] << 8); \ + case 8: \ + _hj_j += ((unsigned)_hj_key[7] << 24); \ + case 7: \ + _hj_j += ((unsigned)_hj_key[6] << 16); \ + case 6: \ + _hj_j += ((unsigned)_hj_key[5] << 8); \ + case 5: \ + _hj_j += _hj_key[4]; \ + case 4: \ + _hj_i += ((unsigned)_hj_key[3] << 24); \ + case 3: \ + _hj_i += ((unsigned)_hj_key[2] << 16); \ + case 2: \ + _hj_i += ((unsigned)_hj_key[1] << 8); \ + case 1: \ + _hj_i += _hj_key[0]; \ } \ HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ bkt = hashv & (num_bkts - 1); \ @@ -476,7 +485,7 @@ typedef unsigned char uint8_t; #if !defined (get16bits) #define get16bits(d) \ ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \ - + (uint32_t)(((const uint8_t *)(d))[0]) ) + + (uint32_t)(((const uint8_t *)(d))[0])) #endif #define HASH_SFH(key, keylen, num_bkts, hashv, bkt) \ do { \ @@ -485,29 +494,32 @@ typedef unsigned char uint8_t; \ int _sfh_rem = _sfh_len & 3; \ _sfh_len >>= 2; \ - hashv = 0xcafebabe; \ + hashv = 0xcafebabe; \ \ /* Main loop */ \ for (; _sfh_len > 0; _sfh_len--) { \ - hashv += get16bits(_sfh_key); \ - _sfh_tmp = (uint32_t)(get16bits(_sfh_key + 2)) << 11 ^ hashv; \ - hashv = (hashv << 16) ^ _sfh_tmp; \ + hashv += get16bits(_sfh_key); \ + _sfh_tmp = (uint32_t)(get16bits(_sfh_key + 2)) << 11 ^ hashv; \ + hashv = (hashv << 16) ^ _sfh_tmp; \ _sfh_key += 2 * sizeof(uint16_t); \ - hashv += hashv >> 11; \ + hashv += hashv >> 11; \ } \ \ /* Handle end cases */ \ switch (_sfh_rem) { \ - case 3: hashv += get16bits(_sfh_key); \ + case 3: \ + hashv += get16bits(_sfh_key); \ hashv ^= hashv << 16; \ hashv ^= (uint32_t)(_sfh_key[sizeof(uint16_t)] << 18); \ hashv += hashv >> 11; \ break; \ - case 2: hashv += get16bits(_sfh_key); \ + case 2: \ + hashv += get16bits(_sfh_key); \ hashv ^= hashv << 11; \ hashv += hashv >> 17; \ break; \ - case 1: hashv += *_sfh_key; \ + case 1: \ + hashv += *_sfh_key; \ hashv ^= hashv << 10; \ hashv += hashv >> 1; \ } \ @@ -519,7 +531,7 @@ typedef unsigned char uint8_t; hashv += hashv >> 17; \ hashv ^= hashv << 25; \ hashv += hashv >> 6; \ - bkt = hashv & (num_bkts - 1); \ + bkt = hashv & (num_bkts - 1); \ } while (0) #ifdef HASH_USING_NO_STRICT_ALIASING @@ -581,40 +593,43 @@ typedef unsigned char uint8_t; #define HASH_MUR(key, keylen, num_bkts, hashv, bkt) \ do { \ const uint8_t *_mur_data = (const uint8_t *)(key); \ - const int _mur_nblocks = (keylen) / 4; \ - uint32_t _mur_h1 = 0xf88D5353; \ - uint32_t _mur_c1 = 0xcc9e2d51; \ - uint32_t _mur_c2 = 0x1b873593; \ - uint32_t _mur_k1 = 0; \ + const int _mur_nblocks = (keylen) / 4; \ + uint32_t _mur_h1 = 0xf88D5353; \ + uint32_t _mur_c1 = 0xcc9e2d51; \ + uint32_t _mur_c2 = 0x1b873593; \ + uint32_t _mur_k1 = 0; \ const uint8_t *_mur_tail; \ const uint32_t *_mur_blocks = \ (const uint32_t *)(_mur_data + _mur_nblocks * 4); \ int _mur_i; \ for (_mur_i = -_mur_nblocks; _mur_i; _mur_i++) { \ - _mur_k1 = MUR_GETBLOCK(_mur_blocks, _mur_i); \ + _mur_k1 = MUR_GETBLOCK(_mur_blocks, _mur_i); \ _mur_k1 *= _mur_c1; \ - _mur_k1 = MUR_ROTL32(_mur_k1, 15); \ + _mur_k1 = MUR_ROTL32(_mur_k1, 15); \ _mur_k1 *= _mur_c2; \ \ _mur_h1 ^= _mur_k1; \ - _mur_h1 = MUR_ROTL32(_mur_h1, 13); \ - _mur_h1 = _mur_h1 * 5 + 0xe6546b64; \ + _mur_h1 = MUR_ROTL32(_mur_h1, 13); \ + _mur_h1 = _mur_h1 * 5 + 0xe6546b64; \ } \ _mur_tail = (const uint8_t *)(_mur_data + _mur_nblocks * 4); \ - _mur_k1 = 0; \ + _mur_k1 = 0; \ switch ((keylen) & 3) { \ - case 3: _mur_k1 ^= _mur_tail[2] << 16; \ - case 2: _mur_k1 ^= _mur_tail[1] << 8; \ - case 1: _mur_k1 ^= _mur_tail[0]; \ + case 3: \ + _mur_k1 ^= _mur_tail[2] << 16; \ + case 2: \ + _mur_k1 ^= _mur_tail[1] << 8; \ + case 1: \ + _mur_k1 ^= _mur_tail[0]; \ _mur_k1 *= _mur_c1; \ - _mur_k1 = MUR_ROTL32(_mur_k1, 15); \ + _mur_k1 = MUR_ROTL32(_mur_k1, 15); \ _mur_k1 *= _mur_c2; \ _mur_h1 ^= _mur_k1; \ } \ _mur_h1 ^= (keylen); \ MUR_FMIX(_mur_h1); \ hashv = _mur_h1; \ - bkt = hashv & (num_bkts - 1); \ + bkt = hashv & (num_bkts - 1); \ } while (0) #endif /* HASH_USING_NO_STRICT_ALIASING */ @@ -704,7 +719,7 @@ typedef unsigned char uint8_t; UT_hash_bucket *_he_new_buckets, *_he_newbkt; \ _he_new_buckets = (UT_hash_bucket *)uthash_malloc( \ 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ - if (!_he_new_buckets) { uthash_fatal( "out of memory"); } \ + if (!_he_new_buckets) { uthash_fatal("out of memory"); } \ memset(_he_new_buckets, 0, \ 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ tbl->ideal_chain_maxlen = \ @@ -713,11 +728,11 @@ typedef unsigned char uint8_t; tbl->nonideal_items = 0; \ for (_he_bkt_i = 0; _he_bkt_i < tbl->num_buckets; _he_bkt_i++) \ { \ - _he_thh = tbl->buckets[ _he_bkt_i ].hh_head; \ + _he_thh = tbl->buckets[_he_bkt_i].hh_head; \ while (_he_thh) { \ _he_hh_nxt = _he_thh->hh_next; \ - HASH_TO_BKT( _he_thh->hashv, tbl->num_buckets * 2, _he_bkt); \ - _he_newbkt = &(_he_new_buckets[ _he_bkt ]); \ + HASH_TO_BKT(_he_thh->hashv, tbl->num_buckets * 2, _he_bkt); \ + _he_newbkt = &(_he_new_buckets[_he_bkt]); \ if (++(_he_newbkt->count) > tbl->ideal_chain_maxlen) { \ tbl->nonideal_items++; \ _he_newbkt->expand_mult = _he_newbkt->count / \ @@ -728,14 +743,14 @@ typedef unsigned char uint8_t; if (_he_newbkt->hh_head) { _he_newbkt->hh_head->hh_prev = \ _he_thh; } \ _he_newbkt->hh_head = _he_thh; \ - _he_thh = _he_hh_nxt; \ + _he_thh = _he_hh_nxt; \ } \ } \ - uthash_free( tbl->buckets, tbl->num_buckets * \ - sizeof(struct UT_hash_bucket) ); \ + uthash_free(tbl->buckets, tbl->num_buckets * \ + sizeof(struct UT_hash_bucket)); \ tbl->num_buckets *= 2; \ tbl->log2_num_buckets++; \ - tbl->buckets = _he_new_buckets; \ + tbl->buckets = _he_new_buckets; \ tbl->ineff_expands = (tbl->nonideal_items > (tbl->num_items >> 1)) ? \ (tbl->ineff_expands + 1) : 0; \ if (tbl->ineff_expands > 1) { \ @@ -745,7 +760,6 @@ typedef unsigned char uint8_t; uthash_expand_fyi(tbl); \ } while (0) - /* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */ /* Note that HASH_SORT assumes the hash handle name to be hh. * HASH_SRT was added to allow the hash handle name to be passed in. */ @@ -756,17 +770,17 @@ typedef unsigned char uint8_t; unsigned _hs_looping, _hs_nmerges, _hs_insize, _hs_psize, _hs_qsize; \ struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \ if (head) { \ - _hs_insize = 1; \ + _hs_insize = 1; \ _hs_looping = 1; \ - _hs_list = &((head)->hh); \ + _hs_list = &((head)->hh); \ while (_hs_looping) { \ - _hs_p = _hs_list; \ - _hs_list = NULL; \ - _hs_tail = NULL; \ + _hs_p = _hs_list; \ + _hs_list = NULL; \ + _hs_tail = NULL; \ _hs_nmerges = 0; \ while (_hs_p) { \ _hs_nmerges++; \ - _hs_q = _hs_p; \ + _hs_q = _hs_p; \ _hs_psize = 0; \ for (_hs_i = 0; _hs_i < _hs_insize; _hs_i++) { \ _hs_psize++; \ @@ -775,10 +789,10 @@ typedef unsigned char uint8_t; next) + \ (head)->hh.tbl->hho)) \ : NULL); \ - if (!(_hs_q) ) { break; } \ + if (!(_hs_q)) { break; } \ } \ _hs_qsize = _hs_insize; \ - while ((_hs_psize > 0) || ((_hs_qsize > 0) && _hs_q )) { \ + while ((_hs_psize > 0) || ((_hs_qsize > 0) && _hs_q)) { \ if (_hs_psize == 0) { \ _hs_e = _hs_q; \ _hs_q = (UT_hash_handle *)((_hs_q->next) ? \ @@ -788,7 +802,7 @@ typedef unsigned char uint8_t; (head)->hh.tbl \ ->hho)) : NULL); \ _hs_qsize--; \ - } else if ( (_hs_qsize == 0) || !(_hs_q) ) { \ + } else if ((_hs_qsize == 0) || !(_hs_q)) { \ _hs_e = _hs_p; \ if (_hs_p) { \ _hs_p = (UT_hash_handle *)((_hs_p->next) ? \ @@ -851,7 +865,7 @@ typedef unsigned char uint8_t; _hs_tail->next = NULL; \ } \ if (_hs_nmerges <= 1) { \ - _hs_looping = 0; \ + _hs_looping = 0; \ (head)->hh.tbl->tail = _hs_tail; \ DECLTYPE_ASSIGN(head, \ ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \ @@ -881,12 +895,12 @@ typedef unsigned char uint8_t; _src_hh = _src_hh->hh_next) { \ _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \ if (cond(_elt)) { \ - _dst_hh = (UT_hash_handle *)(((char *)_elt) + _dst_hho); \ - _dst_hh->key = _src_hh->key; \ + _dst_hh = (UT_hash_handle *)(((char *)_elt) + _dst_hho); \ + _dst_hh->key = _src_hh->key; \ _dst_hh->keylen = _src_hh->keylen; \ - _dst_hh->hashv = _src_hh->hashv; \ - _dst_hh->prev = _last_elt; \ - _dst_hh->next = NULL; \ + _dst_hh->hashv = _src_hh->hashv; \ + _dst_hh->prev = _last_elt; \ + _dst_hh->next = NULL; \ if (_last_elt_hh) { _last_elt_hh->next = _elt; } \ if (!dst) { \ DECLTYPE_ASSIGN(dst, _elt); \ @@ -899,7 +913,7 @@ typedef unsigned char uint8_t; HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt], \ _dst_hh); \ (dst)->hh_dst.tbl->num_items++; \ - _last_elt = _elt; \ + _last_elt = _elt; \ _last_elt_hh = _dst_hh; \ } \ } \ @@ -960,7 +974,6 @@ typedef struct UT_hash_bucket { * value, than to do an O(n) bucket expansion too often. */ unsigned expand_mult; - } UT_hash_bucket; /* random signature used only to find hash tables in external analysis */ @@ -997,7 +1010,6 @@ typedef struct UT_hash_table { uint8_t *bloom_bv; char bloom_nbits; #endif - } UT_hash_table; typedef struct UT_hash_handle { diff --git a/src/utils.c b/src/utils.c index e3b1f740..98b3c063 100644 --- a/src/utils.c +++ b/src/utils.c @@ -45,7 +45,7 @@ #define INT_DIGITS 19 /* enough for 64 bit integer */ #ifdef LIB_ONLY -FILE * logfile; +FILE *logfile; #endif #ifdef HAS_SYSLOG @@ -57,8 +57,8 @@ void ERROR(const char *s) { char *msg = strerror(errno); LOGE("%s: %s", s, msg); - } + #endif int use_tty = 1; @@ -71,13 +71,13 @@ char *ss_itoa(int i) if (i >= 0) { do { *--p = '0' + (i % 10); - i /= 10; + i /= 10; } while (i != 0); return p; } else { /* i < 0 */ do { *--p = '0' - (i % 10); - i /= 10; + i /= 10; } while (i != 0); *--p = '-'; } @@ -100,8 +100,8 @@ int run_as(const char *user) char buf[buflen]; /* variable length array */ /* Note that we use getpwnam_r() instead of getpwnam(), - which returns its result in a statically allocated buffer and - cannot be considered thread safe. */ + * which returns its result in a statically allocated buffer and + * cannot be considered thread safe. */ err = getpwnam_r(user, &pwdbuf, buf, buflen, &pwd); if (err == 0 && pwd) { @@ -130,7 +130,7 @@ int run_as(const char *user) return 0; } else if (buflen >= 16 * 1024) { /* If getpwnam_r() seems defective, call it quits rather than - keep on allocating ever larger buffers until we crash. */ + * keep on allocating ever larger buffers until we crash. */ LOGE( "getpwnam_r() requires more than %u bytes of buffer space.", (unsigned)buflen); @@ -160,12 +160,10 @@ int run_as(const char *user) #endif } -#endif //__MINGW32__ +#endif // __MINGW32__ return 1; } - - char *ss_strndup(const char *s, size_t n) { size_t len = strlen(s); @@ -290,7 +288,7 @@ void usage() printf("\n"); } -void daemonize(const char * path) +void daemonize(const char *path) { #ifndef __MINGW32__ /* Our process ID and Session ID */ @@ -303,7 +301,7 @@ void daemonize(const char * path) } /* If we got a good PID, then - we can exit the parent process. */ + * we can exit the parent process. */ if (pid > 0) { FILE *file = fopen(path, "w"); if (file == NULL) { @@ -365,5 +363,5 @@ int set_nofile(int nofile) return 0; } -#endif +#endif diff --git a/src/utils.h b/src/utils.h index 6a7653f2..8eeb138d 100644 --- a/src/utils.h +++ b/src/utils.h @@ -44,12 +44,12 @@ #else -#define STR(x) #x +#define STR(x) # x #define TOSTR(x) STR(x) #ifdef LIB_ONLY -extern FILE * logfile; +extern FILE *logfile; #define TIME_FORMAT "%Y-%m-%d %H:%M:%S" @@ -196,7 +196,7 @@ char *ss_itoa(int i); int run_as(const char *user); void FATAL(const char *msg); void usage(void); -void daemonize(const char * path); +void daemonize(const char *path); char *ss_strndup(const char *s, size_t n); #ifdef HAVE_SETRLIMIT int set_nofile(int nofile); diff --git a/src/win32.c b/src/win32.c index c6050d0b..db9480f2 100644 --- a/src/win32.c +++ b/src/win32.c @@ -33,7 +33,7 @@ void winsock_init(void) WSADATA wsaData; int ret; wVersionRequested = MAKEWORD(1, 1); - ret = WSAStartup(wVersionRequested, &wsaData); + ret = WSAStartup(wVersionRequested, &wsaData); if (ret != 0) { FATAL("Could not initialize winsock"); } diff --git a/src/win32.h b/src/win32.h index 4ffcb7c5..661652e9 100644 --- a/src/win32.h +++ b/src/win32.h @@ -60,7 +60,6 @@ #define IPV6_V6ONLY 27 // Treat wildcard bind as AF_INET6-only. #endif - #define EWOULDBLOCK WSAEWOULDBLOCK #define errno WSAGetLastError() #define close(fd) closesocket(fd)