flesh out yaml parser.
This commit is contained in:
parent
0485a7242d
commit
71385a1fd5
8 changed files with 250 additions and 6 deletions
|
|
@ -16,6 +16,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 4.0)
|
||||||
PROJECT(nfgen LANGUAGES C)
|
PROJECT(nfgen LANGUAGES C)
|
||||||
|
|
||||||
SET(SOURCE_FILES
|
SET(SOURCE_FILES
|
||||||
|
config.c
|
||||||
main.c
|
main.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -24,5 +25,7 @@ ADD_EXECUTABLE(nfgen ${SOURCE_FILES})
|
||||||
SET_PROPERTY(TARGET nfgen PROPERTY C_STANDARD 11)
|
SET_PROPERTY(TARGET nfgen PROPERTY C_STANDARD 11)
|
||||||
|
|
||||||
TARGET_COMPILE_OPTIONS(nfgen PRIVATE
|
TARGET_COMPILE_OPTIONS(nfgen PRIVATE
|
||||||
$<$<C_COMPILER_ID:AppleClang,Clang,GNU>: -Wall>
|
$<$<C_COMPILER_ID:AppleClang,Clang,GNU>: -Wall -Wextra -Wpedantic -Wformat=2 -Wuseless-cast -Wshadow>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
TARGET_COMPILE_OPTIONS(nfgen PRIVATE $<$<NOT:$<CONFIG:Release,RelMinSize>>: -g>)
|
||||||
|
|
|
||||||
2
LICENSE
2
LICENSE
|
|
@ -209,7 +209,7 @@ If you develop a new program, and you want it to be of the greatest possible use
|
||||||
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
|
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
nfgen
|
nfgen
|
||||||
Copyright (C) 2026 jsvcycling
|
Copyright (C) 2026 Joshua Vega
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
|
|
||||||
32
README.md
32
README.md
|
|
@ -1,3 +1,33 @@
|
||||||
# nfgen
|
# nfgen
|
||||||
|
|
||||||
Generate realistic NetFlow v5 and v9 packets.
|
Generate realistic NetFlow (v5 and/or v9) traffic.
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ cmake -DCMAKE_BUILD_TYPE=Release .
|
||||||
|
$ make
|
||||||
|
```
|
||||||
|
|
||||||
|
To build the executable with debugging symbols, remove the `-DCMAKE_BUILD_TYPE`
|
||||||
|
argument or set it to `-DCMAKE_BUILD_TYPE=Debug`.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
```
|
||||||
|
Copyright (C) 2026 Joshua Vega
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify it under
|
||||||
|
the terms of the GNU General Public License as published by the Free Software
|
||||||
|
Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with
|
||||||
|
this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
```
|
||||||
|
|
||||||
|
Please see `LICENSE` for the complete license details.
|
||||||
|
|
|
||||||
30
config.c
Normal file
30
config.c
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2026 Joshua Vega
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
* version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with
|
||||||
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#define YAML_IMPLEMENTATION
|
||||||
|
#include "yaml.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int loadConfigFromFile(const char *path, size_t pathLength, Config **config) {
|
||||||
|
yaml_value_t *configRoot = yaml_parse(path, pathLength);
|
||||||
|
if (configRoot == NULL) {
|
||||||
|
fprintf(stderr, "%s", yaml_error());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
27
config.h
Normal file
27
config.h
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2026 Joshua Vega
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
* version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with
|
||||||
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#ifndef CONFIG_H
|
||||||
|
#define CONFIG_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct Config {
|
||||||
|
/* TODO */
|
||||||
|
} Config;
|
||||||
|
|
||||||
|
int loadConfigFromFile(const char *path, size_t pathLength, Config **config);
|
||||||
|
|
||||||
|
#endif /* CONFIG_H */
|
||||||
3
format.sh
Executable file
3
format.sh
Executable file
|
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
find . -regex '.*\.\(c\|h\)' -exec clang-format -i --sort-includes {} \;
|
||||||
11
main.c
11
main.c
|
|
@ -18,14 +18,15 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
#define ARG_L_CONFIG_PATH "--config"
|
#define ARG_L_CONFIG_PATH "--config"
|
||||||
#define ARG_S_CONFIG_PATH "-c"
|
#define ARG_S_CONFIG_PATH "-c"
|
||||||
|
|
||||||
#define ARG_LEN_L_CONFIG_PATH 8
|
#define ARG_LEN_L_CONFIG_PATH 8
|
||||||
#define ARG_LEN_S_CONFIG_PATH 2
|
#define ARG_LEN_S_CONFIG_PATH 2
|
||||||
|
|
||||||
char configPath[1024];
|
Config *config = NULL;
|
||||||
size_t configPathLength = 0;
|
|
||||||
|
|
||||||
int parseArguments(int, char**);
|
int parseArguments(int, char**);
|
||||||
void printHelp(void);
|
void printHelp(void);
|
||||||
|
|
@ -42,6 +43,8 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
int parseArguments(int argc, char** argv)
|
int parseArguments(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
char configPath[1024];
|
||||||
|
size_t configPathLength = 0;
|
||||||
bool expectConfigPath = false;
|
bool expectConfigPath = false;
|
||||||
|
|
||||||
for (int argIndex = 1; argIndex < argc; argIndex++) {
|
for (int argIndex = 1; argIndex < argc; argIndex++) {
|
||||||
|
|
@ -70,6 +73,10 @@ int parseArguments(int argc, char** argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (loadConfigFromFile(configPath, configPathLength, &config) != 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
144
yaml.h
Normal file
144
yaml.h
Normal file
|
|
@ -0,0 +1,144 @@
|
||||||
|
#ifndef YAML_H
|
||||||
|
#define YAML_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
YAML_TYPE_STRING, /**< A value that represents a string. */
|
||||||
|
YAML_TYPE_NUMBER, /**< A value that represents a number. */
|
||||||
|
YAML_TYPE_OBJECT, /**< A value that represents an object. */
|
||||||
|
YAML_TYPE_ARRAY, /**< A value that represents an array. */
|
||||||
|
YAML_TYPE_TRUE, /**< A value that represents true. */
|
||||||
|
YAML_TYPE_FALSE, /**< A value that represents false. */
|
||||||
|
YAML_TYPE_NULL, /**< A value that represents null. */
|
||||||
|
} yaml_type_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *data;
|
||||||
|
yaml_type_t type;
|
||||||
|
} yaml_value_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char *data;
|
||||||
|
size_t length;
|
||||||
|
} yaml_string_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char *data;
|
||||||
|
size_t length;
|
||||||
|
} yaml_number_t;
|
||||||
|
|
||||||
|
typedef struct yaml_object_field_s {
|
||||||
|
yaml_string_t *name;
|
||||||
|
yaml_value_t *value;
|
||||||
|
|
||||||
|
struct yaml_object_field_s *next;
|
||||||
|
} yaml_object_field_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
yaml_object_field_t *head;
|
||||||
|
size_t count;
|
||||||
|
} yaml_object_t;
|
||||||
|
|
||||||
|
typedef struct yaml_array_element_s {
|
||||||
|
yaml_value_t *value;
|
||||||
|
|
||||||
|
struct yaml_array_element_s *next;
|
||||||
|
} yaml_array_element_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
yaml_array_element_t *head;
|
||||||
|
size_t count;
|
||||||
|
} yaml_array_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a UTF-8 encoded string into a value object.
|
||||||
|
*
|
||||||
|
* Returns `NULL` if parsing fails.
|
||||||
|
*/
|
||||||
|
yaml_value_t *yaml_parse(const void *, size_t);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Traverse the tree to retrieve a specific sub-value from a root value.
|
||||||
|
*
|
||||||
|
* Returns `NULL` if the requested sub-value does not exist.
|
||||||
|
*/
|
||||||
|
yaml_value_t *yaml_get(const yaml_value_t *, const char *);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access a value object as a string.
|
||||||
|
*
|
||||||
|
* Returns `NULL` if the value is not a string.
|
||||||
|
*/
|
||||||
|
yaml_string_t *yaml_value_as_string(const yaml_value_t *);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access a value object as a number.
|
||||||
|
*
|
||||||
|
* Returns `NULL` if the value is not a number.
|
||||||
|
*/
|
||||||
|
yaml_number_t *yaml_value_as_number(const yaml_value_t *);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access a value object as an object.
|
||||||
|
*
|
||||||
|
* Returns `NULL` if the value is not an object.
|
||||||
|
*/
|
||||||
|
yaml_object_t *yaml_value_as_object(const yaml_value_t *);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access a value object as an array.
|
||||||
|
*
|
||||||
|
* Returns `NULL` if the value is not an array.
|
||||||
|
*/
|
||||||
|
yaml_array_t *yaml_value_as_array(const yaml_value_t *);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a string describing the most recently encountered error.
|
||||||
|
*
|
||||||
|
* Returns `NULL` if no error has been encountered.
|
||||||
|
*/
|
||||||
|
const char *yaml_error(void);
|
||||||
|
|
||||||
|
#endif /* YAML_H */
|
||||||
|
|
||||||
|
#ifdef YAML_IMPLEMENTATION
|
||||||
|
|
||||||
|
char error_message[4096];
|
||||||
|
|
||||||
|
yaml_value_t *yaml_parse(const void *content, size_t length) {
|
||||||
|
/* TODO */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
yaml_value_t *yaml_get(const yaml_value_t *root, const char *path) {
|
||||||
|
/* TODO */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
yaml_string_t *yaml_value_as_string(const yaml_value_t *value) {
|
||||||
|
/* TODO */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
yaml_number_t *yaml_value_as_number(const yaml_value_t *value) {
|
||||||
|
/* TODO */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
yaml_object_t *yaml_object_as_object(const yaml_value_t *value) {
|
||||||
|
/* TODO */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
yaml_array_t *yaml_object_as_array(const yaml_value_t *value) {
|
||||||
|
/* TODO */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *yaml_error(void) {
|
||||||
|
if (error_message[0] == '\0') return NULL;
|
||||||
|
return error_message;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* YAML_IMPLEMENTATION */
|
||||||
Loading…
Add table
Add a link
Reference in a new issue