From 0db83386f2effba1be59ed301d9b400b57207b0a Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 15 Mar 2016 15:40:02 +0100 Subject: add built in config for fallback --- config.def.h | 14 ++++++++++++++ dyndhcpd.c | 39 +++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/config.def.h b/config.def.h index 0195c03..43cddc3 100644 --- a/config.def.h +++ b/config.def.h @@ -22,6 +22,20 @@ #define PIDFILE "/run/dhcpd-%s.pid" #define LEASESFILE "/var/lib/dhcp/dhcp-%s.leases" +#define FALLBACKCONFIG \ + "# fallback dhcpd.conf for interface __INTERFACE__\n" \ + "# generated by dyndhcpd/__VERSION__\n" \ + "\n" \ + "authoritative;\n" \ + "option domain-name \"__DOMAINNAME__\";\n" \ + "\n" \ + "subnet __NETADDRESS__ netmask __NETMASK__ {\n" \ + "\toption broadcast-address __BROADCAST__;\n" \ + "\toption routers __ADDRESS__;\n" \ + "\toption domain-name-servers __ADDRESS__;\n" \ + "\trange __MINDHCP__ __MAXDHCP__;\n" \ + "}" + #endif /* _CONFIG_H */ // vim: set syntax=c: diff --git a/dyndhcpd.c b/dyndhcpd.c index 5cbfb79..134e845 100644 --- a/dyndhcpd.c +++ b/dyndhcpd.c @@ -47,7 +47,7 @@ int main(int argc, char ** argv) { char * template = NULL; FILE * templatefile; - const char * templatefilename = NULL; + const char * templatefilename = CONFIG_TEMPLATE; char * config = NULL; FILE * configfile; char * configfilename = NULL; @@ -209,26 +209,25 @@ int main(int argc, char ** argv) { } /* open the template for reading */ - if (templatefilename == NULL) - templatefilename = CONFIG_TEMPLATE; - if ((templatefile = fopen(templatefilename, "r")) == NULL) { - fprintf(stderr, "Failed opening config template for reading.\n"); - goto out; - } - - /* seek to the and so we know the file size */ - fseek(templatefile, 0, SEEK_END); - fsize = ftell(templatefile); - fseek(templatefile, 0, SEEK_SET); - - /* allocate memory and read file */ - template = malloc(fsize + 1); - if ((fread(template, fsize, 1, templatefile)) != 1) { - fprintf(stderr, "Failed reading config template.\n"); - goto out; + if ((templatefile = fopen(templatefilename, "r")) != NULL) { + /* seek to the and so we know the file size */ + fseek(templatefile, 0, SEEK_END); + fsize = ftell(templatefile); + fseek(templatefile, 0, SEEK_SET); + + /* allocate memory and read file */ + template = malloc(fsize + 1); + if ((fread(template, fsize, 1, templatefile)) != 1) { + fprintf(stderr, "Failed reading config template.\n"); + goto out; + } + fclose(templatefile); + template[fsize] = 0; + } else { + fprintf(stderr, "Failed opening config template for reading.\n" + "Using fallback to built in defaults, functionality is limited.\n"); + template = strdup(FALLBACKCONFIG); } - fclose(templatefile); - template[fsize] = 0; /* replace strings with real values */ for (tmp = template; *tmp;) { -- cgit v1.2.3-54-g00ecf