#include "config.h"
#include "util/log.h"
#include "daemon/worker.h"
+#include "util/config_file.h"
/** buffer size for network connections */
#define BUFSZ 65552
printf("usage: unbound [options]\n");
printf(" start unbound daemon DNS resolver.\n");
printf("-h this help\n");
+ printf("-c file config file to read, unbound.conf(5).\n");
printf("-p port the port to listen on\n");
printf("-v verbose (multiple times increase verbosity)\n");
printf("-f ip set forwarder address\n");
int c;
const char* fwd = "127.0.0.1";
const char* fwdport = UNBOUND_DNS_PORT;
+ const char* cfgfile = NULL;
+ struct config_file *cfg = NULL;
log_init();
- log_info("Start of %s.", PACKAGE_STRING);
/* parse the options */
- while( (c=getopt(argc, argv, "f:hvp:z:")) != -1) {
+ while( (c=getopt(argc, argv, "c:f:hvp:z:")) != -1) {
switch(c) {
+ case 'c':
+ cfgfile = optarg;
+ break;
case 'f':
fwd = optarg;
break;
return 1;
}
+ if(!(cfg = config_create())) {
+ fprintf(stderr, "Could not init config defaults.");
+ return 1;
+ }
+ if(cfgfile) {
+ if(!config_read(cfg, cfgfile)) {
+ config_delete(cfg);
+ return 1;
+ }
+ }
+ log_info("Start of %s.", PACKAGE_STRING);
+
/* setup */
worker = worker_init(port, do_ip4, do_ip6, do_udp, do_tcp, BUFSZ,
numports, baseport);
return cfg;
}
+/** initialize the global cfg_parser object. */
+static void
+create_cfg_parser(struct config_file* cfg, char* filename)
+{
+ static struct config_parser_state st;
+ cfg_parser = &st;
+ cfg_parser->filename = filename;
+ cfg_parser->line = 1;
+ cfg_parser->errors = 0;
+ cfg_parser->cfg = cfg;
+}
+
int
config_read(struct config_file* cfg, const char* filename)
{
log_err("Could not open %s: %s", filename, strerror(errno));
return 0;
}
+ create_cfg_parser(cfg, (char*)filename);
+ ub_c_in = in;
+ ub_c_parse();
fclose(in);
+
+ if(cfg_parser->errors != 0) {
+ fprintf(stderr, "read %s failed: %d errors in configuration file\n",
+ cfg_parser->filename, cfg_parser->errors);
+ return 0;
+ }
return 1;
}