Alter 5 files
Add `.clang-format` Update `checkPacket.c` Update `parser.c` Update `readFirewall.c` Update `types.c`
This commit is contained in:
parent
c9e2c11fb3
commit
be3eece61c
5 changed files with 294 additions and 95 deletions
191
.clang-format
Normal file
191
.clang-format
Normal 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
|
||||||
|
...
|
|
@ -1,22 +1,27 @@
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "parser.c"
|
#include "parser.c"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define TRUE 1
|
#define TRUE 1
|
||||||
#define FALSE 0
|
#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) {
|
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 ip_matches = FALSE;
|
||||||
int port_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;
|
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;
|
port_matches = TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,26 +33,26 @@ int check_rules(struct Rule** rules, size_t num_rules, struct IP* ip, Port* port
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc < 4) {
|
if (argc < 4) {
|
||||||
fprintf(stderr, "Usage: %s FILENAME IP_ADDR PORT\n", argv[0]);
|
fprintf(stderr, "Usage: %s FILENAME IP_ADDR PORT\n", argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t num_parsed_rules;
|
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) {
|
if (parsed_rules == NULL) {
|
||||||
return 1;
|
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) {
|
if (parse_ip(argv[2], ip) == NULL) {
|
||||||
fprintf(stderr, "Failed to parse IP\n");
|
fprintf(stderr, "Failed to parse IP\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Port* port = (Port*) malloc(sizeof(Port));
|
Port *port = (Port *)malloc(sizeof(Port));
|
||||||
if (parse_port(argv[3], port) == NULL) {
|
if (parse_port(argv[3], port) == NULL) {
|
||||||
fprintf(stderr, "Failed to parse port\n");
|
fprintf(stderr, "Failed to parse port\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
//
|
//
|
||||||
// Created by akp on 05/10/23.
|
// Created by akp on 05/10/23.
|
||||||
//
|
//
|
||||||
#include <stdlib.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "types.c"
|
#include "types.c"
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
enum ParseState {
|
enum ParseState {
|
||||||
ParseState_ip1,
|
ParseState_ip1,
|
||||||
|
@ -13,10 +14,10 @@ enum ParseState {
|
||||||
ParseState_port2,
|
ParseState_port2,
|
||||||
};
|
};
|
||||||
|
|
||||||
// parse_ip parses an IP address line starting at startptr. Returns the ending position of the pointer in line.
|
// parse_ip parses an IP address line starting at startptr. Returns the ending
|
||||||
// Returns NULL on failure.
|
// position of the pointer in line. Returns NULL on failure.
|
||||||
char* parse_ip(char* startptr, struct IP* ip) {
|
char *parse_ip(char *startptr, struct IP *ip) {
|
||||||
char* endptr = startptr;
|
char *endptr = startptr;
|
||||||
|
|
||||||
for (int i = 0; i < 4; i += 1) {
|
for (int i = 0; i < 4; i += 1) {
|
||||||
long n = strtol(endptr, &endptr, 10);
|
long n = strtol(endptr, &endptr, 10);
|
||||||
|
@ -26,7 +27,7 @@ char* parse_ip(char* startptr, struct IP* ip) {
|
||||||
ip->val[i] = n;
|
ip->val[i] = n;
|
||||||
|
|
||||||
if (i != 3) {
|
if (i != 3) {
|
||||||
if (*endptr != '.' || *(endptr+1) == '\0') {
|
if (*endptr != '.' || *(endptr + 1) == '\0') {
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
endptr += 1;
|
endptr += 1;
|
||||||
|
@ -39,10 +40,10 @@ char* parse_ip(char* startptr, struct IP* ip) {
|
||||||
return endptr;
|
return endptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse_port parses a port starting at startptr. Returns the ending position of the pointer in line.
|
// parse_port parses a port starting at startptr. Returns the ending position of
|
||||||
// Returns NULL on failure.
|
// the pointer in line. Returns NULL on failure.
|
||||||
char* parse_port(char* startptr, Port* port) {
|
char *parse_port(char *startptr, Port *port) {
|
||||||
char* end;
|
char *end;
|
||||||
long n = strtol(startptr, &end, 10);
|
long n = strtol(startptr, &end, 10);
|
||||||
if (n > USHRT_MAX || n < 0)
|
if (n > USHRT_MAX || n < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -50,9 +51,10 @@ char* parse_port(char* startptr, Port* port) {
|
||||||
return end;
|
return end;
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse_rule parses a rule out a string, line. The result is saved to result, which must be allocated by the caller.
|
// parse_rule parses a rule out a string, line. The result is saved to result,
|
||||||
// The return value is the ending position of the rule in the string.
|
// which must be allocated by the caller. The return value is the ending
|
||||||
char* parse_rule(char line[], struct Rule* result) {
|
// position of the rule in the string.
|
||||||
|
char *parse_rule(char line[], struct Rule *result) {
|
||||||
enum ParseState state = ParseState_ip1;
|
enum ParseState state = ParseState_ip1;
|
||||||
|
|
||||||
result->ip.end = NULL;
|
result->ip.end = NULL;
|
||||||
|
@ -61,65 +63,65 @@ char* parse_rule(char line[], struct Rule* result) {
|
||||||
int ptr = 0;
|
int ptr = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case ParseState_ip1: {
|
case ParseState_ip1: {
|
||||||
char *end = parse_ip(line + ptr, &result->ip.start);
|
char *end = parse_ip(line + ptr, &result->ip.start);
|
||||||
if (end == NULL) {
|
if (end == NULL) {
|
||||||
goto fail;
|
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_ip2: {
|
ptr = (int)(end - line);
|
||||||
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);
|
|
||||||
|
|
||||||
|
switch (line[ptr - 1]) {
|
||||||
|
case '-':
|
||||||
|
state = ParseState_ip2;
|
||||||
|
break;
|
||||||
|
case ' ':
|
||||||
state = ParseState_port1;
|
state = ParseState_port1;
|
||||||
break;
|
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] == '-') {
|
break;
|
||||||
ptr += 1;
|
}
|
||||||
state = ParseState_port2;
|
case ParseState_ip2: {
|
||||||
} else {
|
result->ip.end = (struct IP *)malloc(sizeof(struct IP));
|
||||||
goto finished_parsing;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
char *end = parse_ip(line + ptr, result->ip.end);
|
||||||
|
if (end == NULL) {
|
||||||
|
goto fail;
|
||||||
}
|
}
|
||||||
case ParseState_port2: {
|
ptr = (int)(end - line);
|
||||||
result->port.end = (Port*) malloc(sizeof(Port));
|
|
||||||
|
|
||||||
char* end = parse_port(line + ptr, result->port.end);
|
state = ParseState_port1;
|
||||||
if (end == NULL) {
|
break;
|
||||||
goto fail;
|
}
|
||||||
}
|
case ParseState_port1: {
|
||||||
ptr = (int)(end - line);
|
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;
|
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;
|
return line + ptr;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAX_RULES 128
|
#define MAX_RULES 128
|
||||||
|
|
||||||
// parse_file parses a set of rules from the file named fname. The memory used
|
// 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.
|
// 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;
|
int is_stdin = strcmp(fname, "--") == 0;
|
||||||
|
|
||||||
FILE *fp = NULL;
|
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;
|
*num_parsed_rules = 0;
|
||||||
|
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
|
@ -173,8 +175,8 @@ struct Rule** parse_file(char fname[], size_t* num_parsed_rules) {
|
||||||
fprintf(stderr, "Too many rules\n");
|
fprintf(stderr, "Too many rules\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
struct Rule* rule = (struct Rule*) malloc(sizeof(struct Rule));
|
struct Rule *rule = (struct Rule *)malloc(sizeof(struct Rule));
|
||||||
char* endptr = parse_rule(line, rule);
|
char *endptr = parse_rule(line, rule);
|
||||||
if (endptr == NULL || *endptr != '\n') {
|
if (endptr == NULL || *endptr != '\n') {
|
||||||
fprintf(stderr, "Ill formed rule: %s\n", line);
|
fprintf(stderr, "Ill formed rule: %s\n", line);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -182,7 +184,8 @@ struct Rule** parse_file(char fname[], size_t* num_parsed_rules) {
|
||||||
parsed_rules[*num_parsed_rules] = rule;
|
parsed_rules[*num_parsed_rules] = rule;
|
||||||
(*num_parsed_rules) += 1;
|
(*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.
|
// If it hasn't reached EOF, an error was thrown.
|
||||||
perror("Read line");
|
perror("Read line");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -1,26 +1,25 @@
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "parser.c"
|
#include "parser.c"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
int main(int argc, char* argv[]) {
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
|
fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
size_t num_parsed_rules;
|
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) {
|
if (parsed_rules == NULL) {
|
||||||
return 1;
|
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) {
|
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);
|
print_rule(r);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
free_rule(r);
|
free_rule(r);
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
// Created by akp on 06/10/23.
|
// Created by akp on 06/10/23.
|
||||||
//
|
//
|
||||||
#include "akpa_util.c"
|
#include "akpa_util.c"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct IP {
|
struct IP {
|
||||||
unsigned char val[4];
|
unsigned char val[4];
|
||||||
|
@ -32,7 +35,7 @@ struct Rule {
|
||||||
struct PortRange port;
|
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);
|
int result = compare_ip(&a->ip.start, &b->ip.start);
|
||||||
|
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
|
@ -42,11 +45,11 @@ int compare_rule(struct Rule* a, struct Rule* b) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int compare_rule_v(const void* a, const void* b) {
|
int compare_rule_v(const void *a, const void *b) {
|
||||||
return compare_rule(*((struct Rule**) a), *((struct Rule**) 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) {
|
if (r->ip.end != NULL) {
|
||||||
free(r->ip.end);
|
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]);
|
printf("%d.%d.%d.%d", ip->val[0], ip->val[1], ip->val[2], ip->val[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_port(Port port) {
|
void print_port(Port port) { printf("%d", port); }
|
||||||
printf("%d", port);
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_rule(struct Rule* r) {
|
void print_rule(struct Rule *r) {
|
||||||
print_ip(&r->ip.start);
|
print_ip(&r->ip.start);
|
||||||
if (r->ip.end != NULL) {
|
if (r->ip.end != NULL) {
|
||||||
printf("-");
|
printf("-");
|
||||||
|
|
Reference in a new issue