main.c (2362B)
1 #include <stdint.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 #include <libgen.h> 7 8 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 #include "cofyc/argparse.h" 13 14 #include "util/config.h" 15 16 #include "daemon/bond/setup.h" 17 #include "daemon/router/setup.h" 18 19 static const char *const usage[] = { 20 __NAME " [options]", 21 __NAME " --help", 22 NULL, 23 }; 24 25 int main(int argc, const char **argv) { 26 char *config_file="/etc/network/pmlag"; 27 FILE *config_fd; 28 29 // Seed random 30 uint32_t seed; 31 FILE* urandom = fopen("/dev/urandom", "r"); 32 fread(&seed, sizeof(uint32_t), 1, urandom); 33 fclose(urandom); 34 srand(seed); 35 36 // Define which options we support 37 struct argparse_option options[] = { 38 OPT_HELP(), 39 OPT_STRING('c', "config", &config_file, "Config file to use", NULL, 0, 0), 40 OPT_END(), 41 }; 42 43 // Initialize components 44 daemon_bond_setup(); 45 daemon_router_setup(); 46 47 // Parse command line arguments 48 struct argparse argparse; 49 argparse_init(&argparse, options, usage, 0); 50 argparse_describe(&argparse, NULL, 51 // TODO: format to terminal width 52 "\n" 53 __NAME " is a tool for bonding network interfaces together when the hardware\n" 54 "on the other side of the cable(s) doesn't support it.\n" 55 ); 56 argc = argparse_parse(&argparse, argc, argv); 57 58 // Check the config exists 59 char *config_file_real = realpath(config_file, NULL); 60 if (!config_file_real) { 61 fprintf(stderr, "Could not resolve config file `%s`\n", config_file); 62 return 1; 63 } 64 config_fd = fopen(config_file_real, "r"); 65 if (!config_fd) { 66 perror("Could not open config file"); 67 return 1; 68 } 69 70 // Start the actual parse 71 char *config_dirname = calloc(strlen(config_file_real)+1, 1); 72 strcpy(config_dirname, config_file_real); 73 dirname(config_dirname); 74 if (cfg_parse(config_dirname, config_fd, NULL) < 0) { 75 fprintf(stderr, "Error during reading configuration from %s\n", config_file_real); 76 return 1; 77 } 78 79 // struct pmlag_configuration *config = calloc(1, sizeof(struct pmlag_configuration)); 80 81 // printf("Config file: %s\n", config_file); 82 // config_file = realpath(config_file, NULL); 83 // printf("Realpath : %s\n", config_file); 84 // printf("dirname : %s\n", dirname(config_file)); 85 // // cfg_parse( 86 // // config_load(config_file, config); 87 88 // return 0; 89 return 42; 90 } 91 92 #ifdef __cplusplus 93 } // extern "C" 94 #endif