diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e067dd9 --- /dev/null +++ b/.clang-format @@ -0,0 +1,191 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveMacros: None +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: None +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortEnumsOnASingleLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: true +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +QualifierAlignment: Leave +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +PackConstructorInitializers: BinPack +BasedOnStyle: '' +ConstructorInitializerAllOnOneLineOrOnePerLine: false +AllowAllConstructorInitializersOnNextLine: true +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseLabels: false +IndentCaseBlocks: false +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentRequires: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PenaltyIndentedWhitespace: 0 +PointerAlignment: Right +PPIndentWidth: -1 +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + BeforeNonEmptyParentheses: false +SpaceAroundPointerQualifiers: Default +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +BitFieldColonSpacing: Both +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseCRLF: false +UseTab: Never +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE + - NS_SWIFT_NAME + - CF_SWIFT_NAME +... diff --git a/firewall-rules/checkPacket.c b/firewall-rules/checkPacket.c index 0e965ad..6f71826 100644 --- a/firewall-rules/checkPacket.c +++ b/firewall-rules/checkPacket.c @@ -1,22 +1,27 @@ -#include -#include #include "parser.c" +#include +#include #define TRUE 1 #define FALSE 0 -int check_rules(struct Rule** rules, size_t num_rules, struct IP* ip, Port* port) { +int check_rules(struct Rule **rules, size_t num_rules, struct IP *ip, + Port *port) { for (size_t i = 0; i < num_rules; i += 1) { - struct Rule* r = rules[i]; + struct Rule *r = rules[i]; int ip_matches = FALSE; int port_matches = FALSE; - if ((r->ip.end != NULL && compare_ip(ip, &r->ip.start) > -1 && compare_ip(ip, r->ip.end) < 1) || compare_ip(ip, &r->ip.start) == 0) { + if ((r->ip.end != NULL && compare_ip(ip, &r->ip.start) > -1 && + compare_ip(ip, r->ip.end) < 1) || + compare_ip(ip, &r->ip.start) == 0) { ip_matches = TRUE; } - if ((r->port.end != NULL && akpa_numcmp(*port, r->port.start) > -1 && akpa_numcmp(*port, *r->port.end) < 1) || akpa_numcmp(*port, r->port.start) == 0) { + if ((r->port.end != NULL && akpa_numcmp(*port, r->port.start) > -1 && + akpa_numcmp(*port, *r->port.end) < 1) || + akpa_numcmp(*port, r->port.start) == 0) { port_matches = TRUE; } @@ -28,26 +33,26 @@ int check_rules(struct Rule** rules, size_t num_rules, struct IP* ip, Port* port return FALSE; } -int main(int argc, char* argv[]) { +int main(int argc, char *argv[]) { if (argc < 4) { fprintf(stderr, "Usage: %s FILENAME IP_ADDR PORT\n", argv[0]); return 1; } size_t num_parsed_rules; - struct Rule** parsed_rules = parse_file(argv[1], &num_parsed_rules); + struct Rule **parsed_rules = parse_file(argv[1], &num_parsed_rules); if (parsed_rules == NULL) { return 1; } - struct IP* ip = (struct IP*) malloc(sizeof(struct IP)); + struct IP *ip = (struct IP *)malloc(sizeof(struct IP)); if (parse_ip(argv[2], ip) == NULL) { fprintf(stderr, "Failed to parse IP\n"); return 1; } - Port* port = (Port*) malloc(sizeof(Port)); + Port *port = (Port *)malloc(sizeof(Port)); if (parse_port(argv[3], port) == NULL) { fprintf(stderr, "Failed to parse port\n"); return 1; diff --git a/firewall-rules/parser.c b/firewall-rules/parser.c index 5e3122a..40d30ff 100644 --- a/firewall-rules/parser.c +++ b/firewall-rules/parser.c @@ -1,10 +1,11 @@ // // Created by akp on 05/10/23. // -#include -#include -#include #include "types.c" +#include +#include +#include +#include enum ParseState { ParseState_ip1, @@ -13,10 +14,10 @@ enum ParseState { ParseState_port2, }; -// parse_ip parses an IP address line starting at startptr. Returns the ending position of the pointer in line. -// Returns NULL on failure. -char* parse_ip(char* startptr, struct IP* ip) { - char* endptr = startptr; +// parse_ip parses an IP address line starting at startptr. Returns the ending +// position of the pointer in line. Returns NULL on failure. +char *parse_ip(char *startptr, struct IP *ip) { + char *endptr = startptr; for (int i = 0; i < 4; i += 1) { long n = strtol(endptr, &endptr, 10); @@ -26,7 +27,7 @@ char* parse_ip(char* startptr, struct IP* ip) { ip->val[i] = n; if (i != 3) { - if (*endptr != '.' || *(endptr+1) == '\0') { + if (*endptr != '.' || *(endptr + 1) == '\0') { return NULL; } else { endptr += 1; @@ -39,10 +40,10 @@ char* parse_ip(char* startptr, struct IP* ip) { return endptr; } -// parse_port parses a port starting at startptr. Returns the ending position of the pointer in line. -// Returns NULL on failure. -char* parse_port(char* startptr, Port* port) { - char* end; +// parse_port parses a port starting at startptr. Returns the ending position of +// the pointer in line. Returns NULL on failure. +char *parse_port(char *startptr, Port *port) { + char *end; long n = strtol(startptr, &end, 10); if (n > USHRT_MAX || n < 0) return NULL; @@ -50,9 +51,10 @@ char* parse_port(char* startptr, Port* port) { return end; } -// parse_rule parses a rule out a string, line. The result is saved to result, which must be allocated by the caller. -// The return value is the ending position of the rule in the string. -char* parse_rule(char line[], struct Rule* result) { +// parse_rule parses a rule out a string, line. The result is saved to result, +// which must be allocated by the caller. The return value is the ending +// position of the rule in the string. +char *parse_rule(char line[], struct Rule *result) { enum ParseState state = ParseState_ip1; result->ip.end = NULL; @@ -61,65 +63,65 @@ char* parse_rule(char line[], struct Rule* result) { int ptr = 0; for (;;) { switch (state) { - case ParseState_ip1: { - char *end = parse_ip(line + ptr, &result->ip.start); - if (end == NULL) { - goto fail; - } - ptr = (int)(end - line); - - switch (line[ptr-1]) { - case '-': - state = ParseState_ip2; - break; - case ' ': - state = ParseState_port1; - break; - default: - goto fail; - } - - break; + case ParseState_ip1: { + char *end = parse_ip(line + ptr, &result->ip.start); + if (end == NULL) { + goto fail; } - case ParseState_ip2: { - result->ip.end = (struct IP *) malloc(sizeof(struct IP)); - - char *end = parse_ip(line + ptr, result->ip.end); - if (end == NULL) { - goto fail; - } - ptr = (int) (end - line); + ptr = (int)(end - line); + switch (line[ptr - 1]) { + case '-': + state = ParseState_ip2; + break; + case ' ': state = ParseState_port1; break; + default: + goto fail; } - case ParseState_port1: { - char* end = parse_port(line + ptr, &result->port.start); - if (end == NULL) { - goto fail; - } - ptr = (int)(end - line); - if (line[ptr] == '-') { - ptr += 1; - state = ParseState_port2; - } else { - goto finished_parsing; - } + break; + } + case ParseState_ip2: { + result->ip.end = (struct IP *)malloc(sizeof(struct IP)); - break; + char *end = parse_ip(line + ptr, result->ip.end); + if (end == NULL) { + goto fail; } - case ParseState_port2: { - result->port.end = (Port*) malloc(sizeof(Port)); + ptr = (int)(end - line); - char* end = parse_port(line + ptr, result->port.end); - if (end == NULL) { - goto fail; - } - ptr = (int)(end - line); + state = ParseState_port1; + break; + } + case ParseState_port1: { + char *end = parse_port(line + ptr, &result->port.start); + if (end == NULL) { + goto fail; + } + ptr = (int)(end - line); + if (line[ptr] == '-') { + ptr += 1; + state = ParseState_port2; + } else { goto finished_parsing; } + + break; + } + case ParseState_port2: { + result->port.end = (Port *)malloc(sizeof(Port)); + + char *end = parse_port(line + ptr, result->port.end); + if (end == NULL) { + goto fail; + } + ptr = (int)(end - line); + + goto finished_parsing; + } } } @@ -139,16 +141,16 @@ finished_parsing: return line + ptr; - fail: +fail: return NULL; } #define MAX_RULES 128 // parse_file parses a set of rules from the file named fname. The memory used -// by the return value should be freed by the caller. If the return value is +// by the return value should be freed by the caller. If the return value is // NULL, an error was encoutered and echo'd. -struct Rule** parse_file(char fname[], size_t* num_parsed_rules) { +struct Rule **parse_file(char fname[], size_t *num_parsed_rules) { int is_stdin = strcmp(fname, "--") == 0; FILE *fp = NULL; @@ -163,7 +165,7 @@ struct Rule** parse_file(char fname[], size_t* num_parsed_rules) { } } - struct Rule** parsed_rules = malloc(sizeof(struct Rule*) * MAX_RULES); + struct Rule **parsed_rules = malloc(sizeof(struct Rule *) * MAX_RULES); *num_parsed_rules = 0; char *line = NULL; @@ -173,8 +175,8 @@ struct Rule** parse_file(char fname[], size_t* num_parsed_rules) { fprintf(stderr, "Too many rules\n"); return NULL; } - struct Rule* rule = (struct Rule*) malloc(sizeof(struct Rule)); - char* endptr = parse_rule(line, rule); + struct Rule *rule = (struct Rule *)malloc(sizeof(struct Rule)); + char *endptr = parse_rule(line, rule); if (endptr == NULL || *endptr != '\n') { fprintf(stderr, "Ill formed rule: %s\n", line); return NULL; @@ -182,7 +184,8 @@ struct Rule** parse_file(char fname[], size_t* num_parsed_rules) { parsed_rules[*num_parsed_rules] = rule; (*num_parsed_rules) += 1; } - if (!feof(fp)) { // When we get here, getline has always returned -1 but may or may not have reached EOF. + if (!feof(fp)) { // When we get here, getline has always returned -1 but may + // or may not have reached EOF. // If it hasn't reached EOF, an error was thrown. perror("Read line"); return NULL; diff --git a/firewall-rules/readFirewall.c b/firewall-rules/readFirewall.c index ef3df10..00e0009 100644 --- a/firewall-rules/readFirewall.c +++ b/firewall-rules/readFirewall.c @@ -1,26 +1,25 @@ -#include -#include #include "parser.c" +#include +#include - -int main(int argc, char* argv[]) { +int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s FILENAME\n", argv[0]); return 1; } - size_t num_parsed_rules; - struct Rule** parsed_rules = parse_file(argv[1], &num_parsed_rules); + struct Rule **parsed_rules = parse_file(argv[1], &num_parsed_rules); if (parsed_rules == NULL) { return 1; } - qsort(parsed_rules, num_parsed_rules, sizeof(struct Rule*), &compare_rule_v); + qsort(parsed_rules, num_parsed_rules, sizeof(struct Rule *), + &compare_rule_v); for (int i = 0; i < num_parsed_rules; i += 1) { - struct Rule* r = parsed_rules[i]; + struct Rule *r = parsed_rules[i]; print_rule(r); printf("\n"); free_rule(r); diff --git a/firewall-rules/types.c b/firewall-rules/types.c index 8c77761..ef7c373 100644 --- a/firewall-rules/types.c +++ b/firewall-rules/types.c @@ -2,6 +2,9 @@ // Created by akp on 06/10/23. // #include "akpa_util.c" +#include +#include +#include struct IP { unsigned char val[4]; @@ -32,7 +35,7 @@ struct Rule { struct PortRange port; }; -int compare_rule(struct Rule* a, struct Rule* b) { +int compare_rule(struct Rule *a, struct Rule *b) { int result = compare_ip(&a->ip.start, &b->ip.start); if (result == 0) { @@ -42,11 +45,11 @@ int compare_rule(struct Rule* a, struct Rule* b) { return result; } -int compare_rule_v(const void* a, const void* b) { - return compare_rule(*((struct Rule**) a), *((struct Rule**) b)); +int compare_rule_v(const void *a, const void *b) { + return compare_rule(*((struct Rule **)a), *((struct Rule **)b)); } -void free_rule(struct Rule* r) { +void free_rule(struct Rule *r) { if (r->ip.end != NULL) { free(r->ip.end); } @@ -60,11 +63,9 @@ void print_ip(struct IP *ip) { printf("%d.%d.%d.%d", ip->val[0], ip->val[1], ip->val[2], ip->val[3]); } -void print_port(Port port) { - printf("%d", port); -} +void print_port(Port port) { printf("%d", port); } -void print_rule(struct Rule* r) { +void print_rule(struct Rule *r) { print_ip(&r->ip.start); if (r->ip.end != NULL) { printf("-");