bool readConfig(cfg_t **cfg) { // All the const_cast keywords is to remove the compiler warnings generated by the C++-compiler. cfg_opt_t controller_opts[] = { CFG_INT(const_cast<char *>("id"), -1, CFGF_NONE), CFG_STR(const_cast<char *>("name"), const_cast<char *>(""), CFGF_NONE), CFG_INT(const_cast<char *>("type"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("serial"), const_cast<char *>(""), CFGF_NONE), CFG_END() }; cfg_opt_t device_parameter_opts[] = { // Groups CFG_STR(const_cast<char *>("devices"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("house"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("unit"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("code"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("system"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("units"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("fade"), 0, CFGF_NONE), CFG_END() }; cfg_opt_t device_opts[] = { CFG_INT(const_cast<char *>("id"), -1, CFGF_NONE), CFG_STR(const_cast<char *>("name"), const_cast<char *>("Unnamed"), CFGF_NONE), CFG_INT(const_cast<char *>("controller"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("protocol"), const_cast<char *>("arctech"), CFGF_NONE), CFG_STR(const_cast<char *>("model"), const_cast<char *>(""), CFGF_NONE), CFG_SEC(const_cast<char *>("parameters"), device_parameter_opts, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_STR(const_cast<char *>("user"), const_cast<char *>("nobody"), CFGF_NONE), CFG_STR(const_cast<char *>("group"), const_cast<char *>("plugdev"), CFGF_NONE), CFG_STR(const_cast<char *>("deviceNode"), const_cast<char *>("/dev/tellstick"), CFGF_NONE), CFG_STR(const_cast<char *>("ignoreControllerConfirmation"), const_cast<char *>("false"), CFGF_NONE), CFG_SEC(const_cast<char *>("device"), device_opts, CFGF_MULTI), CFG_SEC(const_cast<char *>("controller"), controller_opts, CFGF_MULTI), CFG_END() }; FILE *fp = fopen(CONFIG_FILE, "re"); // e for setting O_CLOEXEC on the file handle if (!fp) { return false; } (*cfg) = cfg_init(opts, CFGF_NOCASE); if (cfg_parse_fp((*cfg), fp) == CFG_PARSE_ERROR) { (*cfg) = 0; fclose(fp); return false; } fclose(fp); return true; }
void validate_setup(void) { cfg_opt_t *opt = 0; static cfg_opt_t action_opts[] = { CFG_INT("speed", 0, CFGF_NONE), CFG_STR("name", 0, CFGF_NONE), CFG_INT("xspeed", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t multi_opts[] = { CFG_INT_LIST("speeds", 0, CFGF_NONE), CFG_SEC("options", action_opts, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_STR_LIST("ip-address", 0, CFGF_NONE), CFG_INT_CB("action", ACTION_NONE, CFGF_NONE, parse_action), CFG_SEC("options", action_opts, CFGF_NONE), CFG_SEC("multi_options", multi_opts, CFGF_MULTI), CFG_END() }; cfg = cfg_init(opts, 0); cfg_set_validate_func(cfg, "ip-address", validate_ip); fail_unless(cfg_set_validate_func(cfg, "ip-address", validate_ip) == validate_ip); opt = cfg_getopt(cfg, "ip-address"); fail_unless(opt != 0); fail_unless(opt->validcb == validate_ip); cfg_set_validate_func(cfg, "options", validate_action); fail_unless(cfg_set_validate_func(cfg, "options", validate_action) == validate_action); opt = cfg_getopt(cfg, "options"); fail_unless(opt != 0); fail_unless(opt->validcb == validate_action); cfg_set_validate_func(cfg, "options|speed", validate_speed); fail_unless(cfg_set_validate_func(cfg, "options|speed", validate_speed) == validate_speed); opt = cfg_getopt(cfg, "options|speed"); fail_unless(opt != 0); fail_unless(opt->validcb == validate_speed); cfg_set_validate_func(cfg, "multi_options|speeds", validate_speed); fail_unless(cfg_set_validate_func(cfg, "multi_options|speeds", validate_speed) == validate_speed); cfg_set_validate_func(cfg, "multi_options|options|xspeed", validate_speed); fail_unless(cfg_set_validate_func(cfg, "multi_options|options|xspeed", validate_speed) == validate_speed); /* Validate callbacks for *set*() functions, i.e. not when parsing file content */ cfg_set_validate_func2(cfg, "multi_options|speed", validate_speed2); cfg_set_validate_func2(cfg, "multi_options|options|name", validate_name2); }
void read_config(void) { static cfg_opt_t arg_opts[] = { CFG_STR("value", "default", CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_INT("delay", 3, CFGF_NONE), CFG_STR("message", "This is a message", CFGF_NONE), CFG_SEC("argument", arg_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; char *buf = "" \ " delay = 3\n" \ "# message = \"asdfasfasfd tersf\"\n" \ " argument one { value = 1 }\n" \ " argument two { value=foo}\n"; cfg_free(cfg); cfg = cfg_init(opts, 0); cfg_parse_buf(cfg, buf); cfg_parse(cfg, config_filename); }
bool readVarConfig(cfg_t **cfg) { cfg_opt_t device_opts[] = { CFG_INT(const_cast<char *>("state"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("stateValue"), const_cast<char *>(""), CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC(const_cast<char *>("device"), device_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; FILE *fp = fopen(VAR_CONFIG_FILE, "re"); // e for setting O_CLOEXEC on the file handle if (!fp) { Log::warning("Unable to open var config file, %s", VAR_CONFIG_FILE); return false; } (*cfg) = cfg_init(opts, CFGF_NOCASE); if (cfg_parse_fp((*cfg), fp) == CFG_PARSE_ERROR) { (*cfg) = 0; fclose(fp); Log::warning("Unable to parse var config file, %s", VAR_CONFIG_FILE); return false; } fclose(fp); return true; }
cfg_opt_t * build_section(section_t *section, section_ptrs &ptrs) { static const char *funcname = "conf::build_section"; cfg_opt_t *ptr = new cfg_opt_t[section->size() + 1]; ptrs.push_back(std::unique_ptr<cfg_opt_t []>(ptr)); for (auto &entry : *section) { char *name = const_cast<char *>(entry.first.c_str()); switch (entry.second.what_type()) { case val_type::integer: *ptr = CFG_INT(name, 0, CFGF_NODEFAULT); break; case val_type::string: *ptr = CFG_STR(name, 0, CFGF_NODEFAULT); break; case val_type::multistring: *ptr = CFG_STR_LIST(name, 0, CFGF_NODEFAULT); break; case val_type::section: *ptr = CFG_SEC(name, build_section(entry.second.get<conf::section_t *>(), ptrs), CFGF_NONE); break; case val_type::unknown: throw logging::error(funcname, "Val with unknown type in section: %s", name); } ptr++; } *ptr = CFG_END(); return ptrs.back().get(); }
int main(void) { static cfg_opt_t section_opts[] = { CFG_STR("prop", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("section", section_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; const char *config_data = "section title_one { prop = 'value_one' }\n" "section title_two { prop = 'value_two' }\n"; int rc; cfg_t *cfg = cfg_init(opts, CFGF_NONE); fail_unless(cfg); rc = cfg_parse_buf(cfg, config_data); fail_unless(rc == CFG_SUCCESS); fail_unless(cfg_addtsec(cfg, "section", "title_three")); fail_unless(cfg_size(cfg, "section") == 3); fail_unless(cfg_title(cfg_gettsec(cfg, "section", "title_three"))); /* attempt to add a pre-existing section should fail */ fail_unless(!cfg_addtsec(cfg, "section", "title_three")); cfg_free(cfg); return 0; }
bool SyncLogger::ParseFile(const char* pszHash) { if (strcmp(m_szCurShare, pszHash) == 0) { // File is currently parsed, there is no need to parse it again. return true; } cfg_opt_t modEntry[] = { // Parses within the group. CFG_STR(FILE_PATH_VARNAME, FILE_PATH_DEFAULT, CFGF_NONE), CFG_STR(MOD_TIME_VARNAME, MOD_TIME_DEFAULT, CFGF_NONE), CFG_STR(MOD_TYPE_VARNAME, MOD_TYPE_DEFAULT, CFGF_NONE), CFG_END() }; cfg_opt_t entries[] = { // Parses the single groups. CFG_SEC(MOD_NUMBER_VARNAME, modEntry, CFGF_TITLE | CFGF_MULTI), CFG_END() }; // Initializes the parser. m_pCFG = cfg_init(entries, CFGF_NONE); // Parses the file. char szLogName[MAX_PATH]; CalcLogFileName(pszHash, szLogName); if (cfg_parse(m_pCFG, szLogName) == CFG_PARSE_ERROR) return false; return true; }
cfg_t *parse_conf(const char *filename) { cfg_opt_t process_opts[] = { CFG_STR("comm", 0, CFGF_NODEFAULT), CFG_STR("args", 0, CFGF_NODEFAULT), CFG_STR("pre", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("process", process_opts, CFGF_MULTI | CFGF_TITLE), CFG_INT("frequency", 10, CFGF_NONE), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); //cfg_set_validate_func(cfg, "bookmark|port", conf_validate_port); //cfg_set_validate_func(cfg, "bookmark", conf_validate_bookmark); switch(cfg_parse(cfg, filename)) { case CFG_FILE_ERROR: printf("warning: configuration file '%s' could not be read: %s\n", filename, strerror(errno)); printf("continuing with default values...\n\n"); case CFG_SUCCESS: break; case CFG_PARSE_ERROR: return 0; } return cfg; }
cfg_t *parse_conf(char *conf) { cfg_opt_t provider_opts[] = { CFG_STR ("username", 0, CFGF_NONE), CFG_STR ("password", 0, CFGF_NONE), CFG_STR_LIST("alias", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_BOOL("syslog", cfg_false, CFGF_NONE), CFG_BOOL("wildcard", cfg_false, CFGF_NONE), CFG_STR ("bind", 0, CFGF_NONE), CFG_INT ("period", 60, CFGF_NONE), CFG_INT ("startup-delay", 0, CFGF_NONE), CFG_INT ("forced-update", 720000, CFGF_NONE), CFG_SEC ("provider", provider_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); switch (cfg_parse(cfg, conf)) { case CFG_FILE_ERROR: fprintf(stderr, "Cannot read configuration file %s: %s\n", conf, strerror(errno)); case CFG_PARSE_ERROR: return NULL; case CFG_SUCCESS: break; } return cfg; }
int main(void) { cfg_opt_t sub_opts[] = { CFG_BOOL("bool", cfg_false, CFGF_NONE), CFG_STR("string", NULL, CFGF_NONE), CFG_INT("int", 0, CFGF_NONE), CFG_FLOAT("float", 0.0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_BOOL_LIST("bool", cfg_false, CFGF_NONE), CFG_STR_LIST("string", NULL, CFGF_NONE), CFG_INT_LIST("int", 0, CFGF_NONE), CFG_FLOAT_LIST("float", "0.0", CFGF_NONE), CFG_SEC("sub", sub_opts, CFGF_MULTI | CFGF_TITLE | CFGF_NO_TITLE_DUPES), CFG_END() }; char *cmd = NULL; const char *reply; int res; int i; cfg = cfg_init(opts, CFGF_NONE); for (;;) { printf("cli> "); fflush(stdout); if (cmd) free(cmd); cmd = input_cmd(); if (!cmd) exit(0); res = split_cmd(cmd); if (res < 0) { printf("Parse error\n"); continue; } if (cmdc == 0) continue; for (i = 0; cmds[i].cmd; ++i) { if (strcmp(cmdv[0], cmds[i].cmd)) continue; reply = cmds[i].handler(cmdc, cmdv); if (!reply) exit(0); printf("%s", reply); break; } if (!cmds[i].cmd) printf("Unknown command\n"); } cfg_free(cfg); return 0; }
/** * Parses the config file * * @return a pointer to the configuration data structure, NULL on failure */ static cfg_t * parse_config(void) { cfg_t * cfg = NULL; cfg_opt_t target_opts[] = { CFG_STR("comment", 0, CFGF_NONE), CFG_INT("strip", 0, CFGF_NONE), CFG_STR("rewrite_prefix", 0, CFGF_NONE), CFG_FLOAT("prob", 0, CFGF_NONE), CFG_INT("hash_index", 0, CFGF_NONE), CFG_STR("rewrite_suffix", 0, CFGF_NONE), CFG_INT("status", 1, CFGF_NONE), CFG_INT_LIST("backed_up", NULL, CFGF_NONE), CFG_INT("backup", -1, CFGF_NONE), CFG_END() }; cfg_opt_t prefix_opts[] = { CFG_SEC("target", target_opts, CFGF_MULTI | CFGF_TITLE), CFG_INT("max_targets", -1, CFGF_NONE), CFG_END() }; cfg_opt_t domain_opts[] = { CFG_SEC("prefix", prefix_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("domain", domain_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; cfg = cfg_init(opts, CFGF_NONE); cfg_set_error_function(cfg, conf_error); switch (cfg_parse(cfg, config_file)) { case CFG_FILE_ERROR: LM_ERR("file not found: %s\n", config_file); return NULL; case CFG_PARSE_ERROR: LM_ERR("error while parsing %s in line %i, section %s\n", cfg->filename, cfg->line, cfg->name); return NULL; case CFG_SUCCESS: break; } return cfg; }
int main(void) { cfg_opt_t group_opts[] = { CFG_INT("number", 0, CFGF_NONE), CFG_INT("total", 0, CFGF_NONE), CFG_END() }; cfg_opt_t groups_opts[] = { CFG_STR("name", "Esmé", CFGF_NONE), CFG_SEC("group", group_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("groups", groups_opts, CFGF_NONE), CFG_END() }; cfg_t *cfg, *sec; size_t i, j; cfg = cfg_init(opts, CFGF_NONE); if (!cfg || cfg_parse(cfg, CONF) == CFG_PARSE_ERROR) { perror("Failed parsing " CONF); return 1; } /* Iterate over the sections and print fields from each section. */ for (i = 0; i < cfg_size(cfg, "groups"); i++) { sec = cfg_getnsec(cfg, "groups", i); for (j = 0; j < cfg_size(sec, "group"); j++) { cfg_t *opt = cfg_getnsec(sec, "group", j); printf("group title: '%s'\n", cfg_title(opt)); printf("group number: %ld\n", cfg_getint(opt, "number")); printf("group total: %ld\n", cfg_getint(opt, "total")); printf("\n"); } } cfg_free(cfg); return 0; }
static cfg_t *create_config(void) { static cfg_opt_t sec_opts[] = { CFG_INT("a", 1, CFGF_NONE), CFG_INT("b", 2, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("sec", sec_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; return cfg_init(opts, 0); }
int main(void) { char *comment; char *expect = "Now, is it this comment that goes with the option?"; cfg_t *cfg; cfg_opt_t *opt; cfg_opt_t section_opts[] = { CFG_INT("key", 0, CFGF_NONE), CFG_BOOL("bool", 0, CFGF_NONE), CFG_STR("option", NULL, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_STR("option", NULL, CFGF_NONE), CFG_SEC("section", section_opts, CFGF_MULTI), CFG_END() }; cfg = cfg_init(opts, CFGF_COMMENTS); fail_unless(cfg != NULL); fail_unless(cfg_parse(cfg, SRC_DIR "/annotate.conf") == CFG_SUCCESS); /* Verify the parser read the correct comment for this tricky option */ opt = cfg_getopt(cfg, "section|option"); fail_unless(opt != NULL); comment = cfg_opt_getcomment(opt); fail_unless(comment != NULL); fail_unless(strcmp(comment, expect) == 0); expect = "But what's the worst poetry in the universe?"; fail_unless(cfg_opt_setcomment(opt, expect) == CFG_SUCCESS); cfg_opt_setnstr(opt, "Paula Nancy Millstone Jennings was a poet who wrote the worst poetry in " "the universe. In fact, her poetry is still considered to be the worst in " "the Galaxy, closely followed by that of the Azgoths of Kria and the " "Vogons, in that order.", 0); /* Verify that the comment is not reset when changing option value */ comment = cfg_opt_getcomment(opt); fail_unless(comment != NULL); fail_unless(strcmp(comment, expect) == 0); cfg_print(cfg, stdout); fail_unless(cfg_free(cfg) == CFG_SUCCESS); return 0; }
int main(void) { static cfg_opt_t section_opts[] = { CFG_STR("prop", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("section", section_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; const char *config_data = "section title_one { prop = 'value_one' }\n" "section title_two { prop = 'value_two' }\n" "section title_one { prop = 'value_one' }\n"; int rc; cfg_t *cfg = cfg_init(opts, CFGF_NONE); fail_unless(cfg); rc = cfg_parse_buf(cfg, config_data); fail_unless(rc == CFG_SUCCESS); cfg_rmtsec(cfg, "section", "title_two"); fail_unless(cfg_size(cfg, "section") == 1); fail_unless(strcmp(cfg_title(cfg_getnsec(cfg, "section", 0)), "title_one") == 0); cfg_free(cfg); cfg = cfg_init(opts, CFGF_NONE); fail_unless(cfg); rc = cfg_parse_buf(cfg, config_data); fail_unless(rc == CFG_SUCCESS); cfg_rmsec(cfg, "section"); fail_unless(cfg_size(cfg, "section") == 1); fail_unless(strcmp(cfg_title(cfg_getnsec(cfg, "section", 0)), "title_two") == 0); cfg_free(cfg); return 0; }
cfg_t *parse_conf(const char *filename) { cfg_opt_t bookmark_opts[] = { CFG_STR("host", 0, CFGF_NODEFAULT), CFG_INT("port", 21, CFGF_NONE), CFG_STR("login", "anonymous", CFGF_NONE), CFG_STR("password", "anonymous@", CFGF_NONE), CFG_STR("directory", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE), CFG_BOOL("reverse-dns", cfg_true, CFGF_NONE), CFG_BOOL("passive-mode", cfg_false, CFGF_NONE), CFG_BOOL("remote-completion", cfg_true, CFGF_NONE), CFG_FUNC("alias", conf_alias), CFG_STR_LIST("xterm-terminals", "{xterm, rxvt}", CFGF_NONE), CFG_INT_CB("auto-create-bookmark", ACB_YES, CFGF_NONE, conf_parse_acb), CFG_FUNC("include-file", cfg_include), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "bookmark|port", conf_validate_port); cfg_set_validate_func(cfg, "bookmark", conf_validate_bookmark); switch(cfg_parse(cfg, filename)) { case CFG_FILE_ERROR: printf("warning: configuration file '%s' could not be read: %s\n", filename, strerror(errno)); printf("continuing with default values...\n\n"); case CFG_SUCCESS: break; case CFG_PARSE_ERROR: return 0; } return cfg; }
int main(void) { cfg_opt_t greet_opts[] = { CFG_STR_LIST("targets", "{World}", CFGF_NONE), CFG_INT("repeat", 1, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("greeting", greet_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; cfg_t *cfg, *cfg_greet; int repeat; int i, j; cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "greeting|repeat", validate_unsigned_int); if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR) return 1; for(j = 0; j < cfg_size(cfg, "greeting"); j++) { cfg_greet = cfg_getnsec(cfg, "greeting", j); repeat = cfg_getint(cfg_greet, "repeat"); while(repeat--) { printf("%s", cfg_title(cfg_greet)); for(i = 0; i < cfg_size(cfg_greet, "targets"); i++) printf(", %s", cfg_getnstr(cfg_greet, "targets", i)); printf("!\n"); } } cfg_free(cfg); return 0; }
int handle_lispd_config_file() { cfg_t *cfg = 0; unsigned int i = 0; unsigned n = 0; int ret = 0; static cfg_opt_t map_server_opts[] = { CFG_STR("address", 0, CFGF_NONE), CFG_INT("key-type", 0, CFGF_NONE), CFG_STR("key", 0, CFGF_NONE), CFG_BOOL("proxy-reply", cfg_false, CFGF_NONE), CFG_BOOL("verify", cfg_false, CFGF_NONE), CFG_END() }; static cfg_opt_t db_mapping_opts[] = { CFG_STR("eid-prefix", 0, CFGF_NONE), CFG_INT("iid", -1, CFGF_NONE), CFG_STR("interface", 0, CFGF_NONE), CFG_INT("priority_v4", 0, CFGF_NONE), CFG_INT("weight_v4", 0, CFGF_NONE), CFG_INT("priority_v6", 0, CFGF_NONE), CFG_INT("weight_v6", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t mc_mapping_opts[] = { CFG_STR("eid-prefix", 0, CFGF_NONE), CFG_INT("iid", 0, CFGF_NONE), CFG_STR("rloc", 0, CFGF_NONE), CFG_INT("priority", 0, CFGF_NONE), CFG_INT("weight", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t petr_mapping_opts[] = { CFG_STR("address", 0, CFGF_NONE), CFG_INT("priority", 255, CFGF_NONE), CFG_INT("weight", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("database-mapping", db_mapping_opts, CFGF_MULTI), CFG_SEC("static-map-cache", mc_mapping_opts, CFGF_MULTI), CFG_SEC("map-server", map_server_opts, CFGF_MULTI), CFG_SEC("proxy-etr", petr_mapping_opts, CFGF_MULTI), CFG_INT("map-request-retries", 0, CFGF_NONE), CFG_INT("control-port", 0, CFGF_NONE), CFG_BOOL("debug", cfg_false, CFGF_NONE), CFG_STR("map-resolver", 0, CFGF_NONE), CFG_STR_LIST("proxy-itrs", 0, CFGF_NONE), CFG_END() }; /* * parse config_file */ cfg = cfg_init(opts, CFGF_NOCASE); ret = cfg_parse(cfg, config_file); if (ret == CFG_FILE_ERROR) { syslog(LOG_DAEMON, "Couldn't find config file %s, exiting...", config_file); exit(EXIT_FAILURE); } else if(ret == CFG_PARSE_ERROR) { syslog(LOG_DAEMON, "NOTE: Version 0.2.4 changed the format of the 'proxy-etr' element."); syslog(LOG_DAEMON, " Check the 'lispd.conf.example' file for an example entry in"); syslog(LOG_DAEMON, " the new format."); syslog(LOG_DAEMON, "Parse error in file %s, exiting...", config_file); exit(EXIT_FAILURE); } /* * lispd config options */ ret = cfg_getint(cfg, "map-request-retries"); if (ret != 0) map_request_retries = ret; cfg_getbool(cfg, "debug") ? (debug = 1) : (debug = 0); /* * LISP config options */ /* * handle map-resolver config */ map_resolver = cfg_getstr(cfg, "map-resolver"); if (!add_server(map_resolver, &map_resolvers)) return(0); #ifdef DEBUG syslog(LOG_DAEMON, "Added %s to map-resolver list", map_resolver); #endif /* * handle proxy-etr config */ n = cfg_size(cfg, "proxy-etr"); for(i = 0; i < n; i++) { cfg_t *petr = cfg_getnsec(cfg, "proxy-etr", i); if (!add_proxy_etr_entry(petr, &proxy_etrs)) { syslog(LOG_DAEMON, "Can't add proxy-etr %d (%s)", i, cfg_getstr(petr, "address")); } } if (!proxy_etrs){ syslog(LOG_DAEMON, "WARNING: No Proxy-ETR defined. Packets to non-LISP destinations will be forwarded natively (no LISP encapsulation). This may prevent mobility in some scenarios."); sleep(3); } /* * handle proxy-itr config */ n = cfg_size(cfg, "proxy-itrs"); for(i = 0; i < n; i++) { if ((proxy_itr = cfg_getnstr(cfg, "proxy-itrs", i)) != NULL) { if (!add_server(proxy_itr, &proxy_itrs)) continue; #ifdef DEBUG syslog(LOG_DAEMON, "Added %s to proxy-itr list", proxy_itr); #endif } } /* * handle database-mapping config */ n = cfg_size(cfg, "database-mapping"); for(i = 0; i < n; i++) { cfg_t *dm = cfg_getnsec(cfg, "database-mapping", i); if (!add_database_mapping(dm)) { syslog(LOG_DAEMON, "Can't add database-mapping %d (%s->%s)", i, cfg_getstr(dm, "eid-prefix"), cfg_getstr(dm, "interface")); } } /* * handle map-server config */ n = cfg_size(cfg, "map-server"); for(i = 0; i < n; i++) { cfg_t *ms = cfg_getnsec(cfg, "map-server", i); if (!add_map_server(cfg_getstr(ms, "address"), cfg_getint(ms, "key-type"), cfg_getstr(ms, "key"), (cfg_getbool(ms, "proxy-reply") ? 1:0), (cfg_getbool(ms, "verify") ? 1:0))) return(0); #ifdef DEBUG syslog(LOG_DAEMON, "Added %s to map-server list", cfg_getstr(ms, "address")); #endif } /* * handle static-map-cache config */ n = cfg_size(cfg, "static-map-cache"); for(i = 0; i < n; i++) { cfg_t *smc = cfg_getnsec(cfg, "static-map-cache", i); if (!add_static_map_cache_entry(smc)) { syslog(LOG_DAEMON,"Can't add static-map-cache %d (EID:%s -> RLOC:%s)", i, cfg_getstr(smc, "eid-prefix"), cfg_getstr(smc, "rloc")); } } #if (DEBUG > 3) dump_tree(AF_INET,AF4_database); dump_tree(AF_INET6,AF6_database); dump_database(); dump_map_servers(); dump_servers(map_resolvers, "map-resolvers"); dump_servers(proxy_etrs, "proxy-etrs"); dump_servers(proxy_itrs, "proxy-itrs"); dump_map_cache(); #endif cfg_free(cfg); return(0); }
CFG_BOOL("tcp_buffer_userspace", cfg_true, CFGF_NONE), CFG_INT("tcp_buffer_size", 1500, CFGF_NONE), CFG_INT("restricted_bandwidth", 10, CFGF_NONE), CFG_BOOL("daemon", cfg_false, CFGF_NONE), CFG_STR("unpriv_user", NULL, CFGF_NONE), CFG_INT("max_client", 50, CFGF_NONE), CFG_INT("max_relay_per_username", 10, CFGF_NONE), CFG_INT("allocation_lifetime", 1800, CFGF_NONE), CFG_STR("nonce_key", NULL, CFGF_NONE), CFG_STR("ca_file", NULL, CFGF_NONE), CFG_STR("cert_file", NULL, CFGF_NONE), CFG_STR("private_key_file", NULL, CFGF_NONE), CFG_STR("realm", "domain.org", CFGF_NONE), CFG_STR("account_method", "file", CFGF_NONE), CFG_STR("account_file", "users.txt", CFGF_NONE), CFG_SEC("denied_address", denied_address_opts, CFGF_MULTI), CFG_INT("bandwidth_per_allocation", 0, CFGF_NONE), CFG_BOOL("mod_tmpuser", cfg_false, CFGF_NONE), /* the following attributes are not used for the moment */ CFG_STR("account_db_login", "anonymous", CFGF_NONE), CFG_STR("account_db_password", "anonymous", CFGF_NONE), CFG_STR("account_db_name", "turnserver", CFGF_NONE), CFG_STR("account_db_address", "127.0.0.1", CFGF_NONE), CFG_INT("account_db_port", 3306, CFGF_NONE), /*oap configure*/ CFG_STR("oap_server","http://oap.91.com",CFGF_NONE), CFG_INT("inside_passport",1,CFGF_NONE), CFG_END() }; /**
int configuration(struct app_parent *app_p, const char *configfile, char **policy_file, char **syslog_ident, int *syslog_flags, int *syslog_facility) { unsigned int i, j; struct tq_listener_s *p_listener = NULL; struct tq_service_s *p_service = NULL; char *buf = NULL; cfg_t *cfg; cfg_t *syslog_sec; unsigned n_listener, n_services; int ret; static cfg_opt_t syslog_opts[] = { CFG_STR("ident", NULL, CFGF_NONE), CFG_INT_CB("facility", NONE, CFGF_NONE, &cb_syslog_facility), CFG_INT_LIST_CB("options", NULL, CFGF_NONE, &cb_syslog_options), CFG_END() }; static cfg_opt_t service_opts[] = { CFG_INT_CB("type", NONE, CFGF_NONE, &cb_service_type), CFG_STR("uri", 0, CFGF_NONE), CFG_INT("threads", 4, CFGF_NONE), CFG_END() }; static cfg_opt_t listener_opts[] = { CFG_STR("bindaddress", 0, CFGF_NONE), CFG_INT("port", 9001, CFGF_NONE), CFG_INT("backlog", 1024, CFGF_NONE), CFG_STR("cert", 0, CFGF_NONE), CFG_STR("key", 0, CFGF_NONE), CFG_STR("cafile", 0, CFGF_NONE), CFG_STR("capath", 0, CFGF_NONE), CFG_STR("crlpath", 0, CFGF_NONE), CFG_STR("password", 0, CFGF_NONE), CFG_STR("cipherlist", 0, CFGF_NONE), CFG_INT_CB("clientauth", NONE, CFGF_NONE, &cb_answer), CFG_INT_CB("rfc3820", NONE, CFGF_NONE, &cb_answer), CFG_STR("whitelist", 0, CFGF_NONE), CFG_STR("blacklist", 0, CFGF_NONE), CFG_SEC("service", service_opts, CFGF_MULTI), CFG_END() }; cfg_opt_t opts[] = { CFG_INT_CB("debug", NONE, CFGF_NONE, &cb_answer), CFG_STR("policyfile", 0, CFGF_NONE), CFG_SEC("syslog", syslog_opts, CFGF_NONE), CFG_SEC("listener", listener_opts, CFGF_MULTI), CFG_END() }; cfg = cfg_init(opts, CFGF_NOCASE); /* set a validating callback function for bookmark sections */ /* cfg_set_validate_func(cfg, "bookmark", &cb_validate_bookmark); */ ret = cfg_parse(cfg, configfile); if (ret == CFG_FILE_ERROR) { fprintf(stderr, "Error: could not open or read the configuration file " "\"%s\".\n", configfile); return GA_BAD; } else if (ret == CFG_PARSE_ERROR) { fprintf(stderr, "Error: parse error in the configuration file " "\"%s\".\n", configfile); return GA_BAD; } /* Generic */ app_p->debug = cfg_getint(cfg, "debug"); if (app_p->debug == MAYBE || app_p->debug == OPTIONAL) { printf("Overriding debug setting to 'yes'\n"); app_p->debug = YES; } else if (app_p->debug == YES) { printf("= Service running in DEBUG mode =\n"); } /* XACML Rules file */ *policy_file = strdup(cfg_getstr(cfg, "policyfile")); if (*policy_file == NULL) { fprintf(stderr, "Error: no \"policyfile\" set in the configuration file\n"); return GA_BAD; } else { if (app_p->verbose) { printf("Using XACML Policy file: \"%s\"\n", *policy_file); } } /* Syslog */ syslog_sec = cfg_getsec(cfg, "syslog"); if (syslog_sec == NULL) { fprintf(stderr, "Error: no \"syslog\" section found in the " "configuration file \"%s\"\n", configfile); } else { *syslog_ident = strdup(cfg_getstr(syslog_sec, "ident")); *syslog_facility = cfg_getint(syslog_sec, "facility"); if (app_p->debug == YES) { printf("found syslog\n"); printf(" ident = %s\n", cfg_getstr(syslog_sec, "ident")); printf(" facility = %ld\n", cfg_getint(syslog_sec, "facility")); printf("BUG\n"); for (i = 0; i < cfg_size(syslog_sec, "options"); i++) { printf("options[%d] == %ld\n", i, cfg_getnint(syslog_sec, "options", i)); } printf("BUG\n"); } /* Hardwired the settings */ *syslog_flags = LOG_PID|LOG_NDELAY; if (app_p->verbose > 1) { *syslog_flags |= LOG_PERROR; } } /* Listeners */ n_listener = cfg_size(cfg, "listener"); if (app_p->verbose) { printf("%d configured listeners:\n", n_listener); } for (i = 0; i < n_listener; i++) { cfg_t *ls = cfg_getnsec(cfg, "listener", i); if (ls == NULL) { goto cleanup; } p_listener = malloc(sizeof(struct tq_listener_s)); if (p_listener == NULL) { fprintf(stderr, "Error: memory allocation problem, couldn't allocate %lu bytes\n", sizeof(struct tq_listener_s)); goto cleanup; } memset(p_listener, 0, sizeof(struct tq_listener_s)); TAILQ_INIT(&(p_listener->services_head)); /* Settings */ STRDUP_OR_GOTO_CLEANUP(p_listener->bindip, cfg_getstr(ls, "bindaddress")); p_listener->port = (short)cfg_getint(ls, "port"); p_listener->backlog = (short)cfg_getint(ls, "backlog"); STRDUP_OR_GOTO_CLEANUP(p_listener->cert, cfg_getstr(ls, "cert")); STRDUP_OR_GOTO_CLEANUP(p_listener->key, cfg_getstr(ls, "key")); STRDUP_OR_GOTO_CLEANUP(p_listener->cafile, cfg_getstr(ls, "cafile")); STRDUP_OR_GOTO_CLEANUP(p_listener->capath, cfg_getstr(ls, "capath")); STRDUP_OR_GOTO_CLEANUP(p_listener->crlpath, cfg_getstr(ls, "crlpath")); STRDUP_OR_GOTO_CLEANUP(p_listener->cipherlist, cfg_getstr(ls, "cipherlist") ? cfg_getstr(ls, "cipherlist") : "HIGH"); STRDUP_OR_GOTO_CLEANUP(p_listener->cert_password, cfg_getstr(ls, "password")); STRDUP_OR_GOTO_CLEANUP(p_listener->whitelist_path, cfg_getstr(ls, "whitelist")); STRDUP_OR_GOTO_CLEANUP(p_listener->blacklist_path, cfg_getstr(ls, "blacklist")); p_listener->clientauth = (short)cfg_getint(ls, "clientauth"); p_listener->rfc3820 = (short)cfg_getint(ls, "rfc3820"); /* Normalizer */ if (p_listener->clientauth == MAYBE) p_listener->clientauth = OPTIONAL; if (p_listener->rfc3820 == MAYBE || p_listener->rfc3820 == OPTIONAL) p_listener->rfc3820 = YES; /* Create evhtp_ssl_cfg_t */ if (create_evhtp_ssl_cfg_from_tq_listener(p_listener) == GA_BAD) { goto cleanup; } /* Services per listener */ n_services = cfg_size(ls, "service"); if (app_p->verbose) { printf(" %d\n", n_services); } for (j = 0; j < n_services; j++) { cfg_t *serv = cfg_getnsec(ls, "service", j); if (serv == NULL) { goto cleanup; } p_service = malloc(sizeof(struct tq_service_s)); if (p_service == NULL) { fprintf(stderr, "Error: memory allocation problem, couldn't allocate %lu bytes\n", sizeof(struct tq_service_s)); goto cleanup; } memset(p_service, 0, sizeof(struct tq_service_s)); p_service->parent_listener = p_listener; /* Thread count override in Debug mode - max is 1 worker thread */ if (app_p->debug == YES) { p_service->thread_cnt = 1; } else { p_service->thread_cnt = (short)cfg_getint(serv, "threads"); } p_service->ltype = cfg_getint(serv, "type"); p_service->uri = cfg_getstr(serv, "uri"); if (p_service->uri && p_service->uri[0] == '/') { p_service->uri = strdup(p_service->uri); } else { buf = malloc(strlen(p_service->uri) + 2); if (buf == NULL) { goto cleanup; } snprintf(buf, strlen(p_service->uri) + 2, "/%s", p_service->uri); p_service->uri = buf; } if (app_p->verbose) { printf(" uri = %s\n", p_service->uri); printf(" type = %s\n", p_service->ltype == PDP ? "PDP" : p_service->ltype == PAP ? "PAP" : p_service->ltype == PEP ? "PEP" : "unknown"); } TAILQ_INSERT_TAIL(&(p_listener->services_head), p_service, next); } TAILQ_INSERT_TAIL(&(app_p->listener_head), p_listener, next); } cfg_free(cfg); return GA_GOOD; cleanup: cfg_free(cfg); return GA_BAD; }
/** * @brief 根据 conf 文件加载业务名称与gameid的对应关系; * @load_count 本次被加载的条数; * @return -1: failed, 0: succ */ int load_svc_gameid_map(const char *conf_path, uint32_t *load_count) { uint32_t n, last; int i, ret; cfg_t *si; struct svc_gameid_map_t *sg; static cfg_opt_t svcinfo_opts[] = { CFG_STR("name", 0, CFGF_NONE), CFG_INT("gameid", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("svcinfo", svcinfo_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); /* set a validating callback function for svcinfo sections */ cfg_set_validate_func(cfg, "svcinfo", &cb_validate_svcinfo); ret = cfg_parse(cfg, conf_path ? conf_path : DEF_SVC_CFG_PATH); if(ret == CFG_FILE_ERROR) { fprintf(stderr, "Access error, conf: %s, err: %s\n", conf_path ? conf_path : DEF_SVC_CFG_PATH, strerror(errno)); return -1; } else if(ret == CFG_PARSE_ERROR) { fprintf(stderr, "Parse error, conf: %s\n", conf_path ? conf_path : DEF_SVC_CFG_PATH); return -1; } memset(sg_map_shadow, 0, sizeof(sg_map_shadow)); n = cfg_size(cfg, "svcinfo"); for(i = 0; i < n; i++) { if (i == MAX_SVC_NUM) break; si = cfg_getnsec(cfg, "svcinfo", i); sg = &(sg_map_shadow[i]); snprintf(sg->name, sizeof(sg->name), "%s", cfg_getstr(si, "name")); sg->gameid = cfg_getint(si, "gameid"); cur_svcinfo_count_shadow++; } cfg_free(cfg); /* sort sg */ qsort(sg_map_shadow, cur_svcinfo_count_shadow, sizeof(sg_map_shadow[0]), svcinfocmp); last = 0; for (i = 0; i < cur_svcinfo_count_shadow; i++) { sg = &(sg_map_shadow[i]); if (sg->gameid == last) { fprintf(stderr, "dup gameid: %u in conf: %s\n", last, conf_path ? conf_path : DEF_SVC_CFG_PATH); return -1; } if (sg->gameid > MAX_SVC_NUM) { fprintf(stderr, "gameid: %u > max(%u) in conf: %s\n", sg->gameid, MAX_SVC_NUM, conf_path ? conf_path : DEF_SVC_CFG_PATH); return -1; } last = sg->gameid; } svcinfo_enable = 0; memcpy(sg_map, sg_map_shadow, sizeof(sg_map)); cur_svcinfo_count = cur_svcinfo_count_shadow; if (load_count) { *load_count = cur_svcinfo_count; } svcinfo_enable = 1; return 0; }
static cfg_opt_t attr_opts[] = { CFG_STR("description", "", CFGF_NONE), CFG_STR("unit", "", CFGF_NONE), CFG_STR("type", "counter", CFGF_NONE), CFG_BOOL("history", cfg_false, CFGF_NONE), CFG_END() }; static cfg_opt_t variant_opts[] = { CFG_FLOAT_LIST("div", "{}", CFGF_NONE), CFG_STR_LIST("txt", "", CFGF_NONE), CFG_END() }; static cfg_opt_t unit_opts[] = { CFG_SEC("variant", variant_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; static cfg_opt_t global_opts[] = { CFG_FLOAT("read_interval", 1.0f, CFGF_NONE), CFG_FLOAT("rate_interval", 1.0f, CFGF_NONE), CFG_FLOAT("lifetime", 30.0f, CFGF_NONE), CFG_FLOAT("history_variance", 0.1f, CFGF_NONE), CFG_FLOAT("variance", 0.1f, CFGF_NONE), CFG_BOOL("show_all", cfg_false, CFGF_NONE), CFG_INT("unit_exp", -1, CFGF_NONE), CFG_INT("sleep_time", 20000UL, CFGF_NONE), CFG_BOOL("use_si", 0, CFGF_NONE), CFG_BOOL("daemon", 0, CFGF_NONE), CFG_STR("uid", NULL, CFGF_NONE),
/* * read configuration file */ int read_config(struct program_params *pp) { static cfg_opt_t pv_opts[] = { CFG_INT("tier", 0, CFGF_NONE), CFG_FLOAT("pinningScore", 0, CFGF_NONE), CFG_STR("path", NULL, CFGF_NONE), CFG_INT_CB("maxUsedSpace", -1, CFGF_NONE, parse_size_value), CFG_END() }; static cfg_opt_t volume_opts[] = { CFG_STR("LogicalVolume", NULL, CFGF_NONE), CFG_STR("VolumeGroup", NULL, CFGF_NONE), CFG_FLOAT("timeExponent", 1.0/(2<<14), CFGF_NONE), CFG_FLOAT("hitScore", 16, CFGF_NONE), CFG_FLOAT("readMultiplier", 1, CFGF_NONE), CFG_FLOAT("writeMultiplier", 4, CFGF_NONE), CFG_INT_CB("pvmoveWait", 5*60, CFGF_NONE, parse_time_value), CFG_INT_CB("checkWait", 15*60, CFGF_NONE, parse_time_value), CFG_SEC("pv", pv_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; cfg_t *cfg; cfg = cfg_init(opts, CFGF_NONE); assert(cfg); // add verification functions cfg_set_validate_func(cfg, "volume|timeExponent", validate_require_positive); cfg_set_validate_func(cfg, "volume|hitScore", validate_require_positive); cfg_set_validate_func(cfg, "volume|readMultiplier", validate_require_nonnegative); cfg_set_validate_func(cfg, "volume|writeMultiplier", validate_require_nonnegative); cfg_set_validate_func(cfg, "volume|pv|pinningScore", validate_require_nonnegative); cfg_set_validate_func(cfg, "volume|pv|tier", validate_require_nonnegative); cfg_set_validate_func(cfg, "volume|pv|maxUsedSpace", validate_require_nonnegative); // TODO cfg_set_validate_func(cfg, "volume", validate_pv); // do they belong to volume, is there enough space switch(cfg_parse(cfg, pp->conf_file_path)) { case CFG_FILE_ERROR: fprintf(stderr, "Configuration file \"%s\" could not be read: %s\n", pp->conf_file_path, strerror(errno)); return 1; case CFG_SUCCESS: break; case CFG_PARSE_ERROR: fprintf(stderr, "Configuration file errors, aborting\n"); return 1; default: fprintf(stderr, "Internal error"); assert(0); } pp->cfg = cfg; return 0; }
/** * Parse config file options */ static gboolean parse_config_file(void) { cfg_t *cfg, *sec_as, *sec_mpd; cfg_opt_t mpd_opts[] = { CFG_STR("host", "localhost", CFGF_NONE), CFG_INT("port", 6600, CFGF_NONE), CFG_INT("timeout", 5, CFGF_NONE), CFG_INT("interval", 10, CFGF_NONE), CFG_STR("password", "", CFGF_NONE), CFG_END() }; cfg_opt_t as_opts[] = { CFG_STR("username", "", CFGF_NONE), CFG_STR("password", "", CFGF_NONE), CFG_STR("password_hash", "", CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_INT_CB("log_level", G_LOG_LEVEL_ERROR, CFGF_NONE, &cf_log_level), CFG_STR("log_file", "/var/log/scmpc.log", CFGF_NONE), CFG_STR("pid_file", "/var/run/scmpc.pid", CFGF_NONE), CFG_STR("cache_file", "/var/lib/scmpc/scmpc.cache", CFGF_NONE), CFG_INT("queue_length", 500, CFGF_NONE), CFG_INT("cache_interval", 10, CFGF_NONE), CFG_SEC("mpd", mpd_opts, CFGF_NONE), CFG_SEC("audioscrobbler", as_opts, CFGF_NONE), CFG_END() }; cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "queue_length", &cf_validate_num); cfg_set_validate_func(cfg, "cache_interval", &cf_validate_num); cfg_set_validate_func(cfg, "mpd|port", &cf_validate_num); cfg_set_validate_func(cfg, "mpd|timeout", &cf_validate_num); if (parse_files(cfg) == FALSE) { cfg_free(cfg); return FALSE; } g_free(prefs.log_file); g_free(prefs.pid_file); g_free(prefs.cache_file); g_free(prefs.mpd_hostname); g_free(prefs.mpd_password); g_free(prefs.as_username); g_free(prefs.as_password); g_free(prefs.as_password_hash); prefs.log_level = cfg_getint(cfg, "log_level"); prefs.log_file = expand_tilde(cfg_getstr(cfg, "log_file")); prefs.pid_file = expand_tilde(cfg_getstr(cfg, "pid_file")); prefs.cache_file = expand_tilde(cfg_getstr(cfg, "cache_file")); prefs.queue_length = cfg_getint(cfg, "queue_length"); prefs.cache_interval = cfg_getint(cfg, "cache_interval"); sec_mpd = cfg_getsec(cfg, "mpd"); prefs.mpd_hostname = g_strdup(cfg_getstr(sec_mpd, "host")); prefs.mpd_port = cfg_getint(sec_mpd, "port"); prefs.mpd_timeout = cfg_getint(sec_mpd, "timeout"); prefs.mpd_password = g_strdup(cfg_getstr(sec_mpd, "password")); sec_as = cfg_getsec(cfg, "audioscrobbler"); prefs.as_username = g_strdup(cfg_getstr(sec_as, "username")); prefs.as_password = g_strdup(cfg_getstr(sec_as, "password")); prefs.as_password_hash = g_strdup(cfg_getstr(sec_as, "password_hash")); prefs.fork = TRUE; cfg_free(cfg); return TRUE; }
CFG_STR("override_hostname", NULL, CFGF_NONE), CFG_STR("override_ip", NULL, CFGF_NONE), CFG_STR("tags", NULL, CFGF_NONE), CFG_END() }; static cfg_opt_t access_opts[] = { CFG_STR("action", NULL, CFGF_NONE), CFG_STR("ip", NULL, CFGF_NONE), CFG_STR("mask", NULL, CFGF_NONE), CFG_END() }; static cfg_opt_t acl_opts[] = { CFG_STR("default","allow", CFGF_NONE), CFG_SEC("access", access_opts, CFGF_MULTI ), CFG_END() }; static cfg_opt_t udp_send_channel_opts[] = { CFG_STR("mcast_join", NULL, CFGF_NONE), CFG_STR("mcast_if", NULL, CFGF_NONE), CFG_STR("host", NULL, CFGF_NONE ), CFG_INT("port", -1, CFGF_NONE ), CFG_INT("ttl", 1, CFGF_NONE ), CFG_STR("bind", NULL, CFGF_NONE), CFG_BOOL("bind_hostname", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t udp_recv_channel_opts[] = {
/** * Main function of the user app that parses configuration file and sends it to * NAT64 kernel module. * * @param argc Qty of arguments in command line call. * @param argv Array of arguments in command line call. */ int main(int argc, char *argv[]) { struct stat sb; // To check if config file exist. struct in_addr iaddrn, iaddrf, iaddrl; // To validate IP addresses struct in6_addr i6addrf; // also cfg_t *cfg, *cfg_ipv4, *cfg_ipv6; const char *sect_name; char *addr_first, *addr_last; unsigned char addr_maskbits; int port_first, port_last; char which[sizeof("ABC")]; char str[INET_ADDRSTRLEN]; struct config_struct cs; struct nl_sock *nls; int ret; int i = 0; struct ipv6_prefixes **ipv6_pref = NULL; unsigned char ipv6_pref_qty; char ipv6_def_prefix64[sizeof("1111:2222:3333:4444:5555:6666::/128")]; char *ipv6_buf; char *ipv6_check_addr; char *ipv6_check_maskbits; if (argc != 2) { printf("Usage: %s <config-file>\n", argv[0]); exit(EXIT_FAILURE); } if ( stat(argv[1], &sb) == -1 ) { printf("Error: Can not open configuration file: %s\n", argv[1]); exit(EXIT_FAILURE); } // Load default configuration values for each config option, just in case // they were not included in the config file. cfg_opt_t ipv4_opts[] = { CFG_STR("ipv4_addr_net", IPV4_DEF_NET, CFGF_NONE), CFG_INT("ipv4_addr_net_mask_bits", IPV4_DEF_MASKBITS, CFGF_NONE), CFG_STR("ipv4_pool_range_first", IPV4_DEF_POOL_FIRST, CFGF_NONE), CFG_STR("ipv4_pool_range_last", IPV4_DEF_POOL_LAST, CFGF_NONE), CFG_INT("ipv4_tcp_port_range_first", IPV4_DEF_TCP_PORTS_FIRST, CFGF_NONE), CFG_INT("ipv4_tcp_port_range_last", IPV4_DEF_TCP_PORTS_LAST, CFGF_NONE), CFG_INT("ipv4_udp_port_range_first", IPV4_DEF_UDP_PORTS_FIRST, CFGF_NONE), CFG_INT("ipv4_udp_port_range_last", IPV4_DEF_UDP_PORTS_LAST, CFGF_NONE), CFG_END() }; // Load default configuration values for each config option, just in case // they were not included in the config file. sprintf(ipv6_def_prefix64, "%s/%d", IPV6_DEF_PREFIX, IPV6_DEF_MASKBITS ); cfg_opt_t ipv6_opts[] = { CFG_STR_LIST("ipv6_net_prefixes", ipv6_def_prefix64, CFGF_NONE), CFG_INT("ipv6_tcp_port_range_first", IPV6_DEF_TCP_PORTS_FIRST, CFGF_NONE), CFG_INT("ipv6_tcp_port_range_last", IPV6_DEF_TCP_PORTS_LAST, CFGF_NONE), CFG_INT("ipv6_udp_port_range_first", IPV6_DEF_UDP_PORTS_FIRST, CFGF_NONE), CFG_INT("ipv6_udp_port_range_last", IPV6_DEF_UDP_PORTS_LAST, CFGF_NONE), CFG_END() }; // Define two sections in config file cfg_opt_t opts[] = { CFG_SEC("ipv4", ipv4_opts, CFGF_NONE), CFG_SEC("ipv6", ipv6_opts, CFGF_NONE), CFG_END() }; // Parse config file cfg = cfg_init(opts, CFGF_NONE); if(cfg_parse(cfg, argv[1]) == CFG_PARSE_ERROR) { printf("Error parsing configuration file: %s\n", argv[1]); exit_error_conf(cfg); } /* * Loading IPv4 configuration * */ { cfg_ipv4 = cfg_getsec(cfg, "ipv4"); sect_name = cfg_name(cfg_ipv4); printf ("Section: %s\n", sect_name); // Validate IPv4 pool address addr_first = cfg_getstr(cfg_ipv4, "ipv4_addr_net"); if ( convert_ipv4_addr(addr_first, &iaddrn) == EXIT_FAILURE ) { printf("Error: Invalid IPv4 address net: %s\n", addr_first); exit_error_conf(cfg); } // Validate netmask bits addr_maskbits = cfg_getint(cfg_ipv4, "ipv4_addr_net_mask_bits"); if ( validate_ipv4_netmask_bits(addr_maskbits) == EXIT_FAILURE ) { printf("Error: Bad IPv4 network mask bits value: %d\n", addr_maskbits); exit_error_conf(cfg); } // Store values in config struct cs.ipv4_addr_net = iaddrn; cs.ipv4_addr_net_mask_bits = addr_maskbits; printf("\tPool Network: %s/%d\n", addr_first, addr_maskbits); // Validate pool addresses range addr_first = cfg_getstr(cfg_ipv4, "ipv4_pool_range_first"); addr_last = cfg_getstr(cfg_ipv4, "ipv4_pool_range_last"); if ( convert_ipv4_addr(addr_first, &iaddrf) == EXIT_FAILURE ) // Validate ipv4 addr { printf("Error: Malformed ipv4_pool_range_first: %s\n", addr_first); exit_error_conf(cfg); } if ( convert_ipv4_addr(addr_last, &iaddrl) == EXIT_FAILURE ) // Validate ipv4 addr { printf("Error: Malformed ipv4_pool_range_last: %s\n", addr_last); exit_error_conf(cfg); } if ( validate_ipv4_pool_range(&iaddrn, addr_maskbits, &iaddrf, &iaddrl) == EXIT_FAILURE ) // Validate that: first < last { printf("Error: Pool addresses badly defined.\n"); exit_error_conf(cfg); } // Store values in config struct cs.ipv4_pool_range_first = iaddrf; cs.ipv4_pool_range_last = iaddrl; inet_ntop(AF_INET, &(iaddrf.s_addr), str, INET_ADDRSTRLEN); printf("\t\t- First address: %s\n", str); inet_ntop(AF_INET, &(iaddrl.s_addr), str, INET_ADDRSTRLEN); printf("\t\t- Last address: %s\n", str); // Validate port ranges port_first = cfg_getint(cfg_ipv4, "ipv4_tcp_port_range_first"); port_last = cfg_getint(cfg_ipv4, "ipv4_tcp_port_range_last"); sprintf(which, "TCP"); if ( validate_ports_range(port_first, port_last) == EXIT_FAILURE ) { //~ printf("Error: Invalid first %s port: %d\n", which, port_first); printf("Error: Invalid %s ports range.\n", which); exit_error_conf(cfg); } cs.ipv4_tcp_port_first = port_first; cs.ipv4_tcp_port_last = port_last; printf("\t%s pool port range: %d-%d\n", which, port_first, port_last); // port_first = cfg_getint(cfg_ipv4, "ipv4_udp_port_range_first"); port_last = cfg_getint(cfg_ipv4, "ipv4_udp_port_range_last"); sprintf(which, "UDP"); if ( validate_ports_range(port_first, port_last) == EXIT_FAILURE ) { printf("Error: Invalid %s ports range.\n", which); exit_error_conf(cfg); } cs.ipv4_udp_port_first = port_first; cs.ipv4_udp_port_last = port_last; printf("\t%s pool port range: %d-%d\n", which, port_first, port_last); printf ("\n" ); } /* * Loading IPv6 configuration * */ { cfg_ipv6 = cfg_getsec(cfg, "ipv6"); sect_name = cfg_name(cfg_ipv6); printf ("Section: %s\n", sect_name ); // Get number of IPv6 prefixes. ipv6_pref_qty = cfg_size(cfg_ipv6, "ipv6_net_prefixes"); // Allocate memory for the array of prefixes. ipv6_pref = (struct ipv6_prefixes **) malloc(ipv6_pref_qty * sizeof(struct ipv6_prefixes *)); for(i = 0; i < ipv6_pref_qty; i++) { // Split prefix and netmask bits ipv6_buf = cfg_getnstr(cfg_ipv6, "ipv6_net_prefixes", i); ipv6_check_addr = strtok(ipv6_buf, "/"); ipv6_check_maskbits = strtok(NULL, "/"); // Validate IPv6 addr if ( convert_ipv6_addr(ipv6_check_addr, &i6addrf) == EXIT_FAILURE ) { printf("Error: Invalid IPv6 address net: %s\n", ipv6_check_addr); exit_error_conf(cfg); } // Validate netmask bits addr_maskbits = atoi(ipv6_check_maskbits); if ( validate_ipv6_netmask_bits(addr_maskbits) == EXIT_FAILURE ) { printf("Error: Bad IPv6 network mask bits value: %d\n", addr_maskbits); exit_error_conf(cfg); } // Allocate memory for each IPv6 prefix ipv6_pref[i] = (struct ipv6_prefixes *) malloc(sizeof(struct ipv6_prefixes)); ipv6_pref[i]->addr = (i6addrf); ipv6_pref[i]->maskbits = addr_maskbits; } // Store prefixes in the config struct cs.ipv6_net_prefixes = ipv6_pref; cs.ipv6_net_prefixes_qty = ipv6_pref_qty; // Validate port ranges port_first = cfg_getint(cfg_ipv6, "ipv6_tcp_port_range_first"); port_last = cfg_getint(cfg_ipv6, "ipv6_tcp_port_range_last"); sprintf(which, "TCP"); if ( validate_ports_range(port_first, port_last) == EXIT_FAILURE ) { printf("Error: Invalid %s ports range.\n", which); exit_error_conf(cfg); } cs.ipv6_tcp_port_range_first = port_first; cs.ipv6_tcp_port_range_last = port_last; printf("\t%s pool port range: %d-%d\n", which, port_first, port_last); // port_first = cfg_getint(cfg_ipv6, "ipv6_udp_port_range_first"); port_last = cfg_getint(cfg_ipv6, "ipv6_udp_port_range_last"); sprintf(which, "UDP"); if ( validate_ports_range(port_first, port_last) == EXIT_FAILURE ) { printf("Error: Invalid %s ports range.\n", which); exit_error_conf(cfg); } cs.ipv6_udp_port_range_first = port_first; cs.ipv6_udp_port_range_last = port_last; printf("\t%s pool port range: %d-%d\n", which, port_first, port_last); printf ("\n" ); } cfg_free(cfg); /* We got the configuration structure, now send it to the module * using netlink sockets. */ // Reserve memory for netlink socket nls = nl_socket_alloc(); if (!nls) { printf("bad nl_socket_alloc\n"); return EXIT_FAILURE; } // Bind and connect the socket to kernel ret = nl_connect(nls, NETLINK_USERSOCK); if (ret < 0) { nl_perror(ret, "nl_connect"); nl_socket_free(nls); return EXIT_FAILURE; } // Send socket to module ret = nl_send_simple(nls, MSG_TYPE_CONF, 0, &(cs), sizeof(cs)); if (ret < 0) { nl_perror(ret, "nl_send_simple"); printf("Error sending message, is module loaded?\n"); nl_close(nls); nl_socket_free(nls); return EXIT_FAILURE; } else { printf("Message sent (%d bytes):\n", ret); //print_nat64_run_conf(nrc); } nl_close(nls); nl_socket_free(nls); exit(EXIT_SUCCESS); }
int handle_config_file(char **oor_conf_file) { int ret; cfg_t *cfg; char *mode; char *log_file; /* xTR specific */ static cfg_opt_t map_server_opts[] = { CFG_STR("address", 0, CFGF_NONE), CFG_INT("key-type", 0, CFGF_NONE), CFG_STR("key", 0, CFGF_NONE), CFG_BOOL("proxy-reply", cfg_false, CFGF_NONE), CFG_END() }; static cfg_opt_t rloc_address_opts[] = { CFG_STR("address", 0, CFGF_NONE), CFG_INT("priority", 0, CFGF_NONE), CFG_INT("weight", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t rloc_iface_opts[] = { CFG_STR("interface", 0, CFGF_NONE), CFG_INT("ip_version", 0, CFGF_NONE), CFG_INT("priority", 0, CFGF_NONE), CFG_INT("weight", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t db_mapping_opts[] = { CFG_STR("eid-prefix", 0, CFGF_NONE), CFG_INT("iid", 0, CFGF_NONE), CFG_SEC("rloc-address", rloc_address_opts, CFGF_MULTI), CFG_SEC("rloc-iface", rloc_iface_opts, CFGF_MULTI), CFG_END() }; static cfg_opt_t map_cache_mapping_opts[] = { CFG_STR("eid-prefix", 0, CFGF_NONE), CFG_INT("iid", 0, CFGF_NONE), CFG_SEC("rloc-address", rloc_address_opts, CFGF_MULTI), CFG_END() }; static cfg_opt_t petr_mapping_opts[] = { CFG_STR("address", 0, CFGF_NONE), CFG_INT("priority", 255, CFGF_NONE), CFG_INT("weight", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t rtr_iface_opts[] = { CFG_STR("iface", 0, CFGF_NONE), CFG_INT("ip_version", 0, CFGF_NONE), CFG_INT("priority", 255, CFGF_NONE), CFG_INT("weight", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t rtr_ifaces_opts[] = { CFG_SEC("rtr-iface", rtr_iface_opts, CFGF_MULTI), CFG_END() }; static cfg_opt_t nat_traversal_opts[] = { CFG_BOOL("nat_aware", cfg_false, CFGF_NONE), CFG_STR("site_ID", 0, CFGF_NONE), CFG_STR("xTR_ID", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t rloc_probing_opts[] = { CFG_INT("rloc-probe-interval", 0, CFGF_NONE), CFG_INT("rloc-probe-retries", 0, CFGF_NONE), CFG_INT("rloc-probe-retries-interval", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t elp_node_opts[] = { CFG_STR("address", 0, CFGF_NONE), CFG_BOOL("strict", cfg_false, CFGF_NONE), CFG_BOOL("probe", cfg_false, CFGF_NONE), CFG_BOOL("lookup", cfg_false, CFGF_NONE), CFG_END() }; static cfg_opt_t elp_opts[] = { CFG_STR("elp-name", 0, CFGF_NONE), CFG_SEC("elp-node", elp_node_opts, CFGF_MULTI), CFG_END() }; static cfg_opt_t rle_node_opts[] = { CFG_STR("address", 0, CFGF_NONE), CFG_INT("level", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t rle_opts[] = { CFG_STR("rle-name", 0, CFGF_NONE), CFG_SEC("rle-node", rle_node_opts, CFGF_MULTI), CFG_END() }; static cfg_opt_t mc_info_opts[] = { CFG_STR("mc-info-name", 0, CFGF_NONE), CFG_STR("source", 0, CFGF_NONE), CFG_INT("source-mask-length", 0, CFGF_NONE), CFG_STR("group", 0, CFGF_NONE), CFG_INT("group-mask-length", 0, CFGF_NONE), CFG_INT("iid", 0, CFGF_NONE), CFG_END() }; /* Map-Server specific */ static cfg_opt_t lisp_site_opts[] = { CFG_STR("eid-prefix", 0, CFGF_NONE), CFG_INT("iid", 0, CFGF_NONE), CFG_INT("key-type", 0, CFGF_NONE), CFG_STR("key", 0, CFGF_NONE), CFG_BOOL("accept-more-specifics", cfg_false, CFGF_NONE), CFG_BOOL("proxy-reply", cfg_false, CFGF_NONE), CFG_BOOL("merge", cfg_false, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("database-mapping", db_mapping_opts, CFGF_MULTI), CFG_SEC("ms-static-registered-site", db_mapping_opts, CFGF_MULTI), CFG_SEC("rtr-database-mapping", db_mapping_opts, CFGF_MULTI), CFG_SEC("static-map-cache", map_cache_mapping_opts, CFGF_MULTI), CFG_SEC("map-server", map_server_opts, CFGF_MULTI), CFG_SEC("rtr-ifaces", rtr_ifaces_opts, CFGF_MULTI), CFG_SEC("proxy-etr", petr_mapping_opts, CFGF_MULTI), CFG_SEC("nat-traversal", nat_traversal_opts, CFGF_MULTI), CFG_STR("encapsulation", 0, CFGF_NONE), CFG_SEC("rloc-probing", rloc_probing_opts, CFGF_MULTI), CFG_INT("map-request-retries", 0, CFGF_NONE), CFG_INT("control-port", 0, CFGF_NONE), CFG_INT("debug", 0, CFGF_NONE), CFG_STR("log-file", 0, CFGF_NONE), CFG_INT("rloc-probing-interval",0, CFGF_NONE), CFG_STR_LIST("map-resolver", 0, CFGF_NONE), CFG_STR_LIST("proxy-itrs", 0, CFGF_NONE), #ifdef ANDROID CFG_BOOL("override-dns", cfg_false, CFGF_NONE), CFG_STR("override-dns-primary", 0, CFGF_NONE), CFG_STR("override-dns-secondary", 0, CFGF_NONE), #endif CFG_STR("operating-mode", 0, CFGF_NONE), CFG_STR("control-iface", 0, CFGF_NONE), CFG_STR("rtr-data-iface", 0, CFGF_NONE), CFG_SEC("lisp-site", lisp_site_opts, CFGF_MULTI), CFG_SEC("explicit-locator-path", elp_opts, CFGF_MULTI), CFG_SEC("replication-list", rle_opts, CFGF_MULTI), CFG_SEC("multicast-info", mc_info_opts, CFGF_MULTI), CFG_END() }; if (*oor_conf_file == NULL){ *oor_conf_file = strdup("/etc/oor.conf"); } /* * parse config_file */ cfg = cfg_init(opts, CFGF_NOCASE); ret = cfg_parse(cfg, *oor_conf_file); if (ret == CFG_FILE_ERROR) { OOR_LOG(LCRIT, "Couldn't find config file %s, exiting...", config_file); exit_cleanup(); } else if(ret == CFG_PARSE_ERROR) { OOR_LOG(LCRIT, "Parse error in file %s, exiting. Check conf file (see oor.conf.example)", config_file); exit_cleanup(); } /* * oor config options */ /* Debug level */ if (debug_level == -1){ ret = cfg_getint(cfg, "debug"); if (ret > 0) debug_level = ret; else debug_level = 0; if (debug_level > 3) debug_level = 3; } if (debug_level == 1){ OOR_LOG (LINF, "Log level: Low debug"); }else if (debug_level == 2){ OOR_LOG (LINF, "Log level: Medium debug"); }else if (debug_level == 3){ OOR_LOG (LINF, "Log level: High Debug"); } /* * Log file */ log_file = cfg_getstr(cfg, "log-file"); if (daemonize == TRUE){ open_log_file(log_file); } mode = cfg_getstr(cfg, "operating-mode"); if (mode) { if (strcmp(mode, "xTR") == 0) { ret=configure_xtr(cfg); } else if (strcmp(mode, "MS") == 0) { ret=configure_ms(cfg); } else if (strcmp(mode, "RTR") == 0) { ret=configure_rtr(cfg); }else if (strcmp(mode, "MN") == 0) { ret=configure_mn(cfg); } } cfg_free(cfg); return(GOOD); }
int main(int argc, char *argv[]) { struct stat sb; // To check if config file exist. struct in_addr iaddrf, iaddrl; // To validate IP addresses struct in6_addr i6addrf; // also cfg_t *cfg, *cfg_ipv4, *cfg_ipv6; const char *sect_name; char *addr_first, *addr_last; unsigned char addr_maskbits; int port_first, port_last; char which[sizeof("ABC")]; struct config_struct cs; struct nl_sock *nls; int ret; if (argc != 2) { printf("Usage: %s <config-file>\n", argv[0]); exit(EXIT_FAILURE); } if ( stat(argv[1], &sb) == -1 ) { printf("Error: Can not open configuration file: %s\n", argv[1]); exit(EXIT_FAILURE); } cfg_opt_t ipv4_opts[] = { CFG_STR("ipv4_addr_net", IPV4_DEF_NET, CFGF_NONE), CFG_INT("ipv4_addr_net_mask_bits", IPV4_DEF_MASKBITS, CFGF_NONE), CFG_STR("ipv4_pool_range_first", IPV4_DEF_POOL_FIRST, CFGF_NONE), CFG_STR("ipv4_pool_range_last", IPV4_DEF_POOL_LAST, CFGF_NONE), CFG_INT("ipv4_tcp_port_range_first", IPV4_DEF_TCP_POOL_FIRST, CFGF_NONE), CFG_INT("ipv4_tcp_port_range_last", IPV4_DEF_TCP_POOL_LAST, CFGF_NONE), CFG_INT("ipv4_udp_port_range_first", IPV4_DEF_UDP_POOL_FIRST, CFGF_NONE), CFG_INT("ipv4_udp_port_range_last", IPV4_DEF_UDP_POOL_LAST, CFGF_NONE), CFG_END() }; cfg_opt_t ipv6_opts[] = { CFG_STR("ipv6_net_prefix", IPV6_DEF_PREFIX, CFGF_NONE), CFG_INT("ipv6_net_mask_bits", IPV6_DEF_MASKBITS, CFGF_NONE), CFG_INT("ipv6_tcp_port_range_first", IPV6_DEF_TCP_POOL_FIRST, CFGF_NONE), CFG_INT("ipv6_tcp_port_range_last", IPV6_DEF_TCP_POOL_LAST, CFGF_NONE), CFG_INT("ipv6_udp_port_range_first", IPV6_DEF_UDP_POOL_FIRST, CFGF_NONE), CFG_INT("ipv6_udp_port_range_last", IPV6_DEF_UDP_POOL_LAST, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("ipv4", ipv4_opts, CFGF_NONE), CFG_SEC("ipv6", ipv6_opts, CFGF_NONE), CFG_END() }; cfg = cfg_init(opts, CFGF_NONE); if(cfg_parse(cfg, argv[1]) == CFG_PARSE_ERROR) { printf("Error parsing configuration file: %s\n", argv[1]); exit_error_conf(cfg); } /* Loading IPv4 configuration */ { cfg_ipv4 = cfg_getsec(cfg, "ipv4"); sect_name = cfg_name(cfg_ipv4); printf ("Section: %s\n", sect_name); addr_first = cfg_getstr(cfg_ipv4, "ipv4_addr_net"); if ( inet_aton(addr_first, &iaddrf) == 0 ) // Validate ipv4 addr { printf("Error: Invalid IPv4 address net: %s\n", addr_first); exit_error_conf(cfg); } addr_maskbits = cfg_getint(cfg_ipv4, "ipv4_addr_net_mask_bits"); if (addr_maskbits > 32 || addr_maskbits < 0) { printf("Error: Bad IPv4 network mask bits value: %d\n", addr_maskbits); exit_error_conf(cfg); } cs.ipv4_addr_net = iaddrf; cs.ipv4_addr_net_mask_bits = addr_maskbits; printf("\tPool Network: %s/%d\n", addr_first, addr_maskbits); // addr_first = cfg_getstr(cfg_ipv4, "ipv4_pool_range_first"); addr_last = cfg_getstr(cfg_ipv4, "ipv4_pool_range_last"); if ( inet_aton(addr_first, &iaddrf) == 0 ) // Validate ipv4 addr { printf("Error: Malformed ipv4_pool_range_first: %s\n", addr_first); exit_error_conf(cfg); } if ( inet_aton(addr_last, &iaddrl) == 0 ) // Validate ipv4 addr { printf("Error: Malformed ipv4_pool_range_last: %s\n", addr_last); exit_error_conf(cfg); } if (iaddrf.s_addr > iaddrl.s_addr) // Validate that: first < last { printf("Error: First pool address is greater than last pool address.\n"); exit_error_conf(cfg); } cs.ipv4_pool_range_first = iaddrf; cs.ipv4_pool_range_last = iaddrl; printf("\t\t- First address: %s\n", inet_ntoa(iaddrf)); printf("\t\t- Last address: %s\n", inet_ntoa(iaddrl)); // port_first = cfg_getint(cfg_ipv4, "ipv4_tcp_port_range_first"); port_last = cfg_getint(cfg_ipv4, "ipv4_tcp_port_range_last"); sprintf(which, "TCP"); if (port_first < 0 || port_first > 65535) { printf("Error: Invalid first %s port: %d\n", which, port_first); exit_error_conf(cfg); } if (port_last < 0 || port_last > 65535) { printf("Error: Invalid last %s port: %d\n", which, port_last); exit_error_conf(cfg); } if (port_first > port_last) { printf("Error: First %s port is greater than last port.\n", which); exit_error_conf(cfg); } cs.ipv4_tcp_port_first = port_first; cs.ipv4_tcp_port_last = port_last; printf("\t%s pool port range: %d-%d\n", which, port_first, port_last); // port_first = cfg_getint(cfg_ipv4, "ipv4_udp_port_range_first"); port_last = cfg_getint(cfg_ipv4, "ipv4_udp_port_range_last"); sprintf(which, "UDP"); if (port_first < 0 || port_first > 65535) { printf("Error: Invalid first %s port: %d\n", which, port_first); exit_error_conf(cfg); } if (port_last < 0 || port_last > 65535) { printf("Error: Invalid last %s port: %d\n", which, port_last); exit_error_conf(cfg); } if (port_first > port_last) { printf("Error: First %s port is greater than last port.\n", which); exit_error_conf(cfg); } cs.ipv4_udp_port_first = port_first; cs.ipv4_udp_port_last = port_last; printf("\t%s pool port range: %d-%d\n", which, port_first, port_last); printf ("\n" ); } /* Loading IPv6 configuration */ { cfg_ipv6 = cfg_getsec(cfg, "ipv6"); sect_name = cfg_name(cfg_ipv6); printf ("Section: %s\n", sect_name ); addr_first = cfg_getstr(cfg_ipv6, "ipv6_net_prefix"); if ( inet_pton(AF_INET6, addr_first, &i6addrf) < 1 ) // Validate ipv6 addr { printf("Error: Invalid IPv6 address net: %s\n", addr_first); exit_error_conf(cfg); } addr_maskbits = cfg_getint(cfg_ipv6, "ipv6_net_mask_bits"); if (addr_maskbits > 128 || addr_maskbits < 0) { printf("Error: Bad IPv6 network mask bits value: %d\n", addr_maskbits); exit_error_conf(cfg); } cs.ipv6_net_prefix = i6addrf; cs.ipv6_net_mask_bits = addr_maskbits; printf("\tPrefix: %s/%d\n", addr_first, addr_maskbits); // port_first = cfg_getint(cfg_ipv6, "ipv6_tcp_port_range_first"); port_last = cfg_getint(cfg_ipv6, "ipv6_tcp_port_range_last"); sprintf(which, "TCP"); if (port_first < 0 || port_first > 65535) { printf("Error: Invalid first %s port: %d\n", which, port_first); exit_error_conf(cfg); } if (port_last < 0 || port_last > 65535) { printf("Error: Invalid last %s port: %d\n", which, port_last); exit_error_conf(cfg); } if (port_first > port_last) { printf("Error: First %s port is greater than last port.\n", which); exit_error_conf(cfg); } cs.ipv6_tcp_port_range_first = port_first; cs.ipv6_tcp_port_range_last = port_last; printf("\t%s pool port range: %d-%d\n", which, port_first, port_last); // port_first = cfg_getint(cfg_ipv6, "ipv6_udp_port_range_first"); port_last = cfg_getint(cfg_ipv6, "ipv6_udp_port_range_last"); sprintf(which, "UDP"); if (port_first < 0 || port_first > 65535) { printf("Error: Invalid first %s port: %d\n", which, port_first); exit_error_conf(cfg); } if (port_last < 0 || port_last > 65535) { printf("Error: Invalid last %s port: %d\n", which, port_last); exit_error_conf(cfg); } if (port_first > port_last) { printf("Error: First %s port is greater than last port.\n", which); exit_error_conf(cfg); } cs.ipv6_udp_port_range_first = port_first; cs.ipv6_udp_port_range_last = port_last; printf("\t%s pool port range: %d-%d\n", which, port_first, port_last); printf ("\n" ); } cfg_free(cfg); /* We got the configuration structure, now send it to the module * using netlink sockets. */ // Reserve memory for netlink socket nls = nl_socket_alloc(); if (!nls) { printf("bad nl_socket_alloc\n"); return EXIT_FAILURE; } // Bind and connect the socket to a protocol ret = nl_connect(nls, NETLINK_USERSOCK); if (ret < 0) { nl_perror(ret, "nl_connect"); nl_socket_free(nls); return EXIT_FAILURE; } ret = nl_send_simple(nls, MY_MSG_TYPE, 0, &(cs), sizeof(cs)); if (ret < 0) { nl_perror(ret, "nl_send_simple"); printf("Error sending message, is module loaded?\n"); nl_close(nls); nl_socket_free(nls); return EXIT_FAILURE; } else { printf("Message sent (%d bytes):\n", ret); //print_nat64_run_conf(nrc); } nl_close(nls); nl_socket_free(nls); exit(EXIT_SUCCESS); }
CFG_END() }; /* MPD section structure */ static cfg_opt_t sec_mpd[] = { CFG_INT("port", 6600, CFGF_NONE), CFG_INT("http_port", 0, CFGF_NONE), CFG_BOOL("clear_queue_on_stop_disable", cfg_false, CFGF_NONE), CFG_END() }; /* Config file structure */ static cfg_opt_t toplvl_cfg[] = { CFG_SEC("general", sec_general, CFGF_NONE), CFG_SEC("library", sec_library, CFGF_NONE), CFG_SEC("audio", sec_audio, CFGF_NONE), CFG_SEC("airplay", sec_airplay, CFGF_MULTI | CFGF_TITLE), CFG_SEC("spotify", sec_spotify, CFGF_NONE), CFG_SEC("sqlite", sec_sqlite, CFGF_NONE), CFG_SEC("mpd", sec_mpd, CFGF_NONE), CFG_END() }; cfg_t *cfg; uint64_t libhash; uid_t runas_uid; gid_t runas_gid;
int main(int argc, char *argv[]) { try { cfg_opt_t opts_sensor[] = { CFG_INT(const_cast<char *>("crate"), 0xff, CFGF_NONE), CFG_INT(const_cast<char *>("fru"), 0xff, CFGF_NONE), CFG_STR(const_cast<char *>("card"), const_cast<char *>(""), CFGF_NONE), CFG_STR(const_cast<char *>("sensor"), const_cast<char *>(""), CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC(const_cast<char *>("sensor"), opts_sensor, CFGF_MULTI), CFG_STR(const_cast<char *>("outfile"), const_cast<char *>("data.csv"), CFGF_NONE), CFG_STR(const_cast<char *>("host"), const_cast<char *>("localhost"), CFGF_NONE), CFG_STR(const_cast<char *>("password"), const_cast<char *>(""), CFGF_NONE), CFG_INT(const_cast<char *>("port"), 4681, CFGF_NONE), CFG_INT(const_cast<char *>("interval"), 5, CFGF_NONE), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "host", &cfg_validate_hostname); cfg_set_validate_func(cfg, "port", &cfg_validate_port); if (argc < 2) { printf("%s sensor-spec.conf\n", argv[0]); exit(1); } if (cfg_parse(cfg, argv[1]) == CFG_PARSE_ERROR) exit(1); const char *host = cfg_getstr(cfg, "host"); const char *pass = cfg_getstr(cfg, "password"); const char *outfile = cfg_getstr(cfg, "outfile"); uint16_t port = cfg_getint(cfg, "port"); uint32_t interval = cfg_getint(cfg, "interval"); sysmgr::sysmgr sm(host, pass, port); try { sm.connect(); } catch (sysmgr::sysmgr_exception &e) { printf("Unable to connect to system manager: %s\n", e.message.c_str()); exit(2); } std::vector<Sensor> sensors; std::vector<sysmgr::crate_info> sm_crates = sm.list_crates(); for (std::vector<sysmgr::crate_info>::iterator it = sm_crates.begin(); it != sm_crates.end(); it++) { if (!it->connected) printf("Warning: Crate %hhu is not connected at this time. Discarding all data related to it.\n", it->crateno); } for(unsigned int i = 0; i < cfg_size(cfg, "sensor"); i++) { cfg_t *cfgsensor = cfg_getnsec(cfg, "sensor", i); uint8_t crate = cfg_getint(cfgsensor, "crate"); uint8_t fru = cfg_getint(cfgsensor, "fru"); const char *card = cfg_getstr(cfgsensor, "card"); const char *sensor = cfg_getstr(cfgsensor, "sensor"); bool found = false; for (std::vector<sysmgr::crate_info>::iterator c_it = sm_crates.begin(); c_it != sm_crates.end(); c_it++) { if (!c_it->connected) continue; if (crate != 0xff && c_it->crateno != crate) continue; std::vector<sysmgr::card_info> sm_frus = sm.list_cards(c_it->crateno); for (std::vector<sysmgr::card_info>::iterator f_it = sm_frus.begin(); f_it != sm_frus.end(); f_it++) { if (fru != 0xff && f_it->fru != fru) continue; if (card[0] != '\0' && f_it->name != card) continue; std::vector<sysmgr::sensor_info> sm_sensors = sm.list_sensors(c_it->crateno, f_it->fru); for (std::vector<sysmgr::sensor_info>::iterator s_it = sm_sensors.begin(); s_it != sm_sensors.end(); s_it++) { if (s_it->type == 'E' || s_it->type == 'O') continue; // We don't know how to read these. if (sensor[0] != '\0' && s_it->name != sensor) continue; sensors.push_back(Sensor(c_it->crateno, f_it->fru, s_it->name, stdsprintf("C%hhu %s (%s) %s", c_it->crateno, sysmgr::sysmgr::get_slotstring(f_it->fru).c_str(), f_it->name.c_str(), s_it->name.c_str()))); found = true; } } } if (!found) printf("Warning: Unable to find a match for %hhu %hhu \"%s\" \"%s\"\n", crate, fru, card, sensor); } FILE *fd = fopen(outfile, "w"); if (!fd) { printf("Unable to open %s for writing\n", outfile); exit(1); } cfg_free(cfg); printf("Sensors indexed. Now polling %d sensors.\n", (int)sensors.size()); fprintf(fd, "Time"); for (std::vector<Sensor>::iterator it = sensors.begin(); it != sensors.end(); it++) { fprintf(fd, CSV_COMMA "\"%s\"", it->name.c_str()); } fprintf(fd, "\n"); fflush(fd); time_t now; char timestamp[32]; while (1) { time(&now); strftime(timestamp, 32, "%Y-%m-%d %H:%M:%S", localtime(&now)); fprintf(fd, "%s", timestamp); for (std::vector<Sensor>::iterator it = sensors.begin(); it != sensors.end(); it++) { fprintf(fd, CSV_COMMA); try { sysmgr::sensor_reading reading = sm.sensor_read(it->crate, it->fru, it->sensor); if (reading.threshold_set) fprintf(fd, "%f", reading.threshold); else fprintf(fd, "0x%04hx", reading.eventmask); } catch (sysmgr::sysmgr_exception &e) { //printf("Error reading sensor \"%s\": %s\n", it->name.c_str(), e.message.c_str()); } //usleep(100000); } fprintf(fd, "\n"); fflush(fd); sleep(interval); if (!sm.connected()) break; } } catch (sysmgr::sysmgr_exception &e) { printf("Caught fatal exception: %s\n", e.message.c_str()); printf("Goodbye.\n"); } }