EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
Macros | Functions
dnsmasq.c File Reference

File containing the implementation of dnsmasq service configuration utilities. More...

#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "dhcp_config.h"
#include "../utils/allocs.h"
#include "../utils/log.h"
#include "../utils/os.h"
#include "../utils/squeue.h"
Include dependency graph for dnsmasq.c:

Macros

#define PROCESS_RESTART_TIME   5
 
#define MAX_DHCP_CHECK_COUNT   100
 
#define DNSMASQ_BIND_INTERFACE_OPTION   "--bind-interfaces"
 
#define DNSMASQ_BIND_DYNAMIC_OPTION   "--bind-dynamic"
 
#define DNSMASQ_NO_DAEMON_OPTION   "--no-daemon"
 
#define DNSMASQ_LOG_QUERIES_OPTION   "--log-queries"
 
#define DNSMASQ_CONF_FILE_OPTION   "-C"
 
#define DNSMASQ_SCRIPT_STR
 

Functions

int define_dhcp_interface_name (const struct dhcp_conf *dconf, uint16_t vlanid, char *ifname)
 Creates the DHCP interface name for the given vlan id. More...
 
int generate_dnsmasq_conf (struct dhcp_conf *dconf, UT_array *dns_server_array)
 Generates the dnsmasq configuration file. More...
 
int generate_dnsmasq_script (char *dhcp_script_path, char *supervisor_control_path)
 Generates the dnsmasq executable script for DHCP requests. More...
 
void get_dnsmasq_args (const char *dnsmasq_bin_path, const char *dnsmasq_conf_path, const char *argv[static 6])
 Builds the argv for calling dnsmasq. More...
 
int check_dhcp_running (char *name, int wait_time)
 
char * run_dhcp_process (const char *dhcp_bin_path, const char *dhcp_conf_path)
 Execute the DHCP server. More...
 
bool kill_dhcp_process (void)
 Terminate the DHCP server. More...
 
int signal_dhcp_process (const char *dhcp_bin_path)
 Signal the DHCP process to reload the config. More...
 
int clear_dhcp_lease_entry (char *mac_addr, char *dhcp_leasefile_path)
 Clear the DHCP lease entry for a MAC addrress. More...
 

Detailed Description

File containing the implementation of dnsmasq service configuration utilities.

Author
Alexandru Mereacre
Date
2021

Macro Definition Documentation

◆ DNSMASQ_BIND_DYNAMIC_OPTION

#define DNSMASQ_BIND_DYNAMIC_OPTION   "--bind-dynamic"

◆ DNSMASQ_BIND_INTERFACE_OPTION

#define DNSMASQ_BIND_INTERFACE_OPTION   "--bind-interfaces"

◆ DNSMASQ_CONF_FILE_OPTION

#define DNSMASQ_CONF_FILE_OPTION   "-C"

◆ DNSMASQ_LOG_QUERIES_OPTION

#define DNSMASQ_LOG_QUERIES_OPTION   "--log-queries"

◆ DNSMASQ_NO_DAEMON_OPTION

#define DNSMASQ_NO_DAEMON_OPTION   "--no-daemon"

◆ DNSMASQ_SCRIPT_STR

#define DNSMASQ_SCRIPT_STR
Value:
"#!/bin/sh\n" \
"sockpath=\"%s\"\n" \
"str=\"SET_IP $1 $2 $3\"\n" \
"\n" \
"nccheck=`nc -help 2>&1 >/dev/null | grep 'OpenBSD netcat'`\n" \
"if [ -z \"$nccheck\" ]\n" \
"then\n" \
"\techo \"Using socat\"\n" \
"\tcommand=\"socat - UNIX-CLIENT:$sockpath\"\n" \
"else\n" \
"\techo \"Using netcat\"\n" \
"\tcommand=\"nc -uU $sockpath -w2 -W1\"\n" \
"fi\n" \
"\n" \
"echo \"Sending $str ...\"\n" \
"echo $str | $command\n"

◆ MAX_DHCP_CHECK_COUNT

#define MAX_DHCP_CHECK_COUNT   100

◆ PROCESS_RESTART_TIME

#define PROCESS_RESTART_TIME   5

Function Documentation

◆ check_dhcp_running()

int check_dhcp_running ( char *  name,
int  wait_time 
)

◆ clear_dhcp_lease_entry()

int clear_dhcp_lease_entry ( char *  mac_addr,
char *  dhcp_leasefile_path 
)

Clear the DHCP lease entry for a MAC addrress.

Parameters
mac_addrThe MAC address string
dhcp_leasefile_pathThe DHCP file path
Returns
int 0 on success, -1 on failure

◆ define_dhcp_interface_name()

int define_dhcp_interface_name ( const struct dhcp_conf dconf,
uint16_t  vlanid,
char *  ifname 
)

Creates the DHCP interface name for the given vlan id.

Parameters
dconfThe dhcp configuration structure.
vlanidThe vlan id.
[out]ifnameThe DHCP interface name.
Precondition
vlanid must be less than 4095 chars
ifname must point to at least #IF_NAMESIZE bytes.
Return values
0Success
-1Error (invalid args)

◆ generate_dnsmasq_conf()

int generate_dnsmasq_conf ( struct dhcp_conf dconf,
UT_array *  dns_server_array 
)

Generates the dnsmasq configuration file.

Parameters
dconfThe dhcp configuration structure.
dns_server_arrayThe array including the DNS servers IP addresses.
Returns
0 on success, -1 otherwise

◆ generate_dnsmasq_script()

int generate_dnsmasq_script ( char *  dhcp_script_path,
char *  supervisor_control_path 
)

Generates the dnsmasq executable script for DHCP requests.

Parameters
dhcp_script_pathThe dhcp executable script path string.
supervisor_control_pathThe UNIX domains supervisor control path.
Returns
0 on success, -1 otherwise

◆ get_dnsmasq_args()

void get_dnsmasq_args ( const char *  dnsmasq_bin_path,
const char *  dnsmasq_conf_path,
const char *  argv[static 6] 
)

Builds the argv for calling dnsmasq.

Parameters
dnsmasq_bin_pathThe path to the dnsmasq binary.
dnsmasq_conf_pathThe path to the dnsmasq config file.
[in,out]argvThe array to store the args warning.
Precondition
argv must have space for at least 6 pointers.

◆ kill_dhcp_process()

bool kill_dhcp_process ( void  )

Terminate the DHCP server.

Returns
bool true on success, false otherwise

◆ run_dhcp_process()

char* run_dhcp_process ( const char *  dhcp_bin_path,
const char *  dhcp_conf_path 
)

Execute the DHCP server.

This function returns a pointer to statically allocated memory, which may be overwritten by subsequent calls.

Parameters
dhcp_bin_pathThe DHCP server binary path
dhcp_conf_pathThe DHCP server config path
Returns
The pointer to the statically allocated process name, NULL on failure. Do not pass this pointer to free()

◆ signal_dhcp_process()

int signal_dhcp_process ( const char *  dhcp_bin_path)

Signal the DHCP process to reload the config.

Parameters
dhcp_bin_pathThe DHCP server binary path
Returns
int 0 on success, -1 on failure