Select Git revision
loop-detector.c
loop-detector.c 3.49 KiB
/*
* This is the main file of loop_detection application.
*
* @Company: PivaSoftware, Inteno
* @author: Omar Kallel
*/
#include <stdio.h>
#include <uci.h>
#include <stdlib.h> /* for atoi() and exit() */
#include <unistd.h> /* for close() */
#include "ethernetThreadPool.h"
#include "mac_ethernet.h"
#include <sys/time.h>
#include <sys/resource.h>
#include <signal.h>
char **portsNames=NULL;
uint8_t portsnumber=0;
uint8_t send_loop_discovery_period = 10;
uint8_t send_discovery_numbertimes = 5;
/*
* getAppUciParams permits to get parameters values from /etc/config/loopDetect file using uci.
* It gets send_loop_discovery_period and send_discovery_numbertimes parameters.
*/
void getAppUciParams(void){
static struct uci_context *uci_ctx = NULL;
static struct uci_package *uci_config_package = NULL;
struct uci_element *e1, *e2, *e3;
struct uci_section *s;
struct uci_option *opt;
uint8_t k =0;
/*
* init uci
*/
if (!uci_ctx) {
uci_ctx = uci_alloc_context();
if (!uci_ctx) fprintf(stdout, "uci_ctx alloc failed\n");
}
if (uci_load(uci_ctx, "loopDetect", &uci_config_package)) {
uci_free_context(uci_ctx);
uci_ctx = NULL;
return;
}
send_loop_discovery_period = 10;
send_discovery_numbertimes = 5;
uci_foreach_element(&uci_config_package->sections, e2) {
s = uci_to_section(e2);
uci_foreach_element(&s->options, e3) {
opt = uci_to_option(e3);
if(!strcmp(opt->e.name, "send_loop_discovery_period")) {
send_loop_discovery_period = atoi(opt->v.string);
}
if(!strcmp(opt->e.name, "send_discovery_numbertimes")) {
send_discovery_numbertimes = atoi(opt->v.string);
}
}
}
/*
* close uci Section
*/
if (uci_ctx) {
if (uci_config_package) {
uci_unload(uci_ctx, uci_config_package);
uci_config_package = NULL;
}
uci_free_context(uci_ctx);
uci_ctx = NULL;
}
}
/*
* findPortsNumberNames permits to find the number of ethernet ports of the equipment and the name of each one using uci.
*/
void findPortsNumberNames(void){
static struct uci_context *uci_ctx = NULL;
static struct uci_package *uci_config_package = NULL;
struct uci_element *e1, *e2, *e3;
struct uci_section *s;
struct uci_option *opt;
uint8_t k =0;
/*
* init uci
*/
if (!uci_ctx) {
uci_ctx = uci_alloc_context();
if (!uci_ctx) fprintf(stdout, "uci_ctx alloc failed\n");
}
if (uci_load(uci_ctx, "ports", &uci_config_package)) {
uci_free_context(uci_ctx);
uci_ctx = NULL;
}
/*
* find ports number
*/
uci_foreach_element(&uci_config_package->sections, e1) {
portsnumber++;
}
/*
* find ports names
*/
portsNames = (char **)malloc((portsnumber+1) * sizeof(char*));
uci_foreach_element(&uci_config_package->sections, e2) {
s = uci_to_section(e2);
uci_foreach_element(&s->options, e3) {
opt = uci_to_option(e3);
if(!strcmp(opt->e.name, "ifname")) {
portsNames[k]=(char*)malloc(6*sizeof(char));
sprintf(portsNames[k++], "%s", opt->v.string);
break;
}
}
}
portsNames[k]=NULL;
/*
* close uci Section
*/
if (uci_ctx) {
if (uci_config_package) {
uci_unload(uci_ctx, uci_config_package);
uci_config_package = NULL;
}
uci_free_context(uci_ctx);
uci_ctx = NULL;
}
}
int main(int argc, char *argv[]) {
if(!findOwnMacAddress()){
fprintf(stderr, "MAC address not found\n");
return 0;
}
getAppUciParams();
findPortsNumberNames();
buildL2LoopDiscoveryFrame();
init_l2loop_send_discovery_thread();
init_ethernet_thread_pool();
join_ports_threads();
return 0;
}