Smart Home firewall
Profile-based Smart Home firewall
Classes | Macros | Typedefs | Enumerations | Functions
rule_utils.h File Reference

Interface to nftables counters. More...

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
#include <errno.h>
#include <nftables/libnftables.h>
Include dependency graph for rule_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  packet_count_t
 
struct  duration_init_t
 
struct  counters_t
 
struct  periodic_policy_t
 

Macros

#define INET_MIN_ADDRSTRLEN   1
 
#define INET6_MIN_ADDRSTRLEN   2
 

Typedefs

typedef struct nft_ctx nft_ctx_t
 

Enumerations

enum  counter_type_t { PACKETS = 1 , BYTES = 2 }
 

Functions

uint64_t counter_read_microseconds ()
 Read the current microseconds value. More...
 
duration_init_t counter_duration_init ()
 Initialize the values of a duration_t structure. More...
 
bool exec_nft_cmd (char *cmd)
 Execute an nftables command. More...
 
char * exec_nft_cmd_verbose (char *cmd)
 Execute an nftables command and return its output. More...
 
int16_t get_nft_handle (char *buf)
 Get an nftables handle from a command output. More...
 
bool delete_nft_rule_by_handle (char *nft_table, char *nft_chain, int16_t handle)
 Delete an nftables rule, by specifying its handle. More...
 
bool delete_nft_rule (char *nft_table, char *nft_chain, char *nft_rule)
 Delete an nftables rule. More...
 
int32_t counter_read_packets (char *table_name, char *counter_name)
 Read the packet count value of an nftables counter. More...
 
int32_t counter_read_bytes (char *table_name, char *counter_name)
 Read the bytes value of an nftables counter. More...
 

Detailed Description

Interface to nftables counters.

Date
2022-11-02

Function Documentation

◆ counter_duration_init()

duration_init_t counter_duration_init ( )

Initialize the values of a duration_t structure.

Returns
duration_init_t struct containing the initial duration value

Initialize the values of a duration_t structure.

Parameters
nft_table_namename of the nftables table containing the associated nftables counter
nft_counter_namename of the associated nftables counter
Returns
duration_init_t struct containing the initial duration value

◆ counter_read_bytes()

int32_t counter_read_bytes ( char *  table_name,
char *  counter_name 
)

Read the bytes value of an nftables counter.

Parameters
table_namename of the nftables table containing the counter
counter_namename of the nftables counter to read
Returns
bytes value of the counter

◆ counter_read_microseconds()

uint64_t counter_read_microseconds ( )

Read the current microseconds value.

Returns
current microseconds value

◆ counter_read_packets()

int32_t counter_read_packets ( char *  table_name,
char *  counter_name 
)

Read the packet count value of an nftables counter.

Parameters
table_namename of the nftables table containing the counter
counter_namename of the nftables counter to read
Returns
packet count value of the counter

◆ delete_nft_rule()

bool delete_nft_rule ( char *  nft_table,
char *  nft_chain,
char *  nft_rule 
)

Delete an nftables rule.

Parameters
nft_tablenftables table containing the rule
nft_chainnftables chain containing the rule
nft_rulenftables rule to delete
Returns
true if the rule was correctly deleted, false otherwise

Retrieve the rule handle, then delete the rule.

Parameters
nft_tablenftables table containing the rule
nft_chainnftables chain containing the rule
nft_rulenftables rule to delete
Returns
true if the rule was correctly deleted, false otherwise

◆ delete_nft_rule_by_handle()

bool delete_nft_rule_by_handle ( char *  nft_table,
char *  nft_chain,
int16_t  handle 
)

Delete an nftables rule, by specifying its handle.

Parameters
nft_tablenftables table containing the rule
nft_chainnftables chain containing the rule
handlehandle of the nftables rule to delete
Returns
true if the rule was correctly deleted, false otherwise

◆ exec_nft_cmd()

bool exec_nft_cmd ( char *  cmd)

Execute an nftables command.

Parameters
cmdnftables command to be executed
Returns
true if the command was correctly executed, false otherwise

◆ exec_nft_cmd_verbose()

char* exec_nft_cmd_verbose ( char *  cmd)

Execute an nftables command and return its output.

Uses libnftables to execute the command.

Parameters
cmdnftables command to be executed
Returns
buffer containing the command output, or NULL in case of error

◆ get_nft_handle()

int16_t get_nft_handle ( char *  buf)

Get an nftables handle from a command output.

Parameters
bufnftables command output to search for the handle
Returns
nftables handle, or -1 in case of error