Alter 5 files

Add `.clang-format`
Update `checkPacket.c`
Update `parser.c`
Update `readFirewall.c`
Update `types.c`
This commit is contained in:
akp 2023-10-08 19:35:23 +01:00
parent c9e2c11fb3
commit be3eece61c
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
5 changed files with 294 additions and 95 deletions

191
.clang-format Normal file
View file

@ -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
...

View file

@ -1,22 +1,27 @@
#include <stdlib.h>
#include <stdio.h>
#include "parser.c"
#include <stdio.h>
#include <stdlib.h>
#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];
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;
}

View file

@ -1,10 +1,11 @@
//
// Created by akp on 05/10/23.
//
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include "types.c"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum ParseState {
ParseState_ip1,
@ -13,8 +14,8 @@ 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.
// 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;
@ -39,8 +40,8 @@ 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.
// 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);
@ -50,8 +51,9 @@ 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.
// 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;
@ -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;

View file

@ -1,7 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
#include "parser.c"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
@ -9,7 +8,6 @@ int main(int argc, char* argv[]) {
return 1;
}
size_t num_parsed_rules;
struct Rule **parsed_rules = parse_file(argv[1], &num_parsed_rules);
@ -17,7 +15,8 @@ int main(int argc, char* argv[]) {
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];

View file

@ -2,6 +2,9 @@
// Created by akp on 06/10/23.
//
#include "akpa_util.c"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
struct IP {
unsigned char val[4];
@ -60,9 +63,7 @@ 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) {
print_ip(&r->ip.start);