From 13fd8f76079e7e9a0529754eb834fd93549d5a6b Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 4 Nov 2013 11:19:30 +0100 Subject: add configuration file for pacredir This allows to ignore hosts on specified interfaces and add static hosts. --- pacredir.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 3 deletions(-) (limited to 'pacredir.c') diff --git a/pacredir.c b/pacredir.c index aef0fba..d889309 100644 --- a/pacredir.c +++ b/pacredir.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -22,6 +23,7 @@ #include #include +#include #include #include @@ -46,8 +48,17 @@ struct hosts { struct hosts * next; }; +/* ignore interfaces */ +struct ignore_interfaces { + /* interface name */ + char * interface; + /* pointer to next struct element */ + struct ignore_interfaces * next; +}; + /* global variables */ struct hosts * hosts = NULL; +struct ignore_interfaces * ignore_interfaces = NULL; static AvahiSimplePoll *simple_poll = NULL; /*** write_log ***/ @@ -130,6 +141,8 @@ int remove_host(const char * host, const char * type) { static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, void* userdata) { char * host; + char intname[IFNAMSIZ]; + struct ignore_interfaces * tmp_ignore_interfaces = ignore_interfaces; assert(b); @@ -143,13 +156,23 @@ static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, Avah case AVAHI_BROWSER_NEW: host = get_fqdn(name, domain); + if (flags & AVAHI_LOOKUP_RESULT_LOCAL) + goto out; + + /* check whether to ignore the interface */ + if_indextoname(interface, intname); + while (tmp_ignore_interfaces->next != NULL) { + if (strcmp(intname, tmp_ignore_interfaces->interface) == 0) { + write_log(stdout, "Ignoring service '%s' of type '%s' in domain '%s' on interface '%s'\n", name, type, domain, intname); + goto out; + } + tmp_ignore_interfaces = tmp_ignore_interfaces->next; + } + # if defined DEBUG write_log(stdout, "NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain); # endif - if (flags & AVAHI_LOOKUP_RESULT_LOCAL) - goto out; - add_host(host, type); out: free(host); @@ -377,6 +400,8 @@ static int ahc_echo(void * cls, struct MHD_Connection * connection, const char * /*** sigterm_callback ***/ void sigterm_callback(int signal) { + write_log(stdout, "Received SIGTERM, quitting.\n"); + avahi_simple_poll_quit(simple_poll); } @@ -395,6 +420,9 @@ void sighup_callback(int signal) { /*** main ***/ int main(int argc, char ** argv) { + dictionary * ini; + char * values, * value; + struct ignore_interfaces * tmp_ignore_interfaces; AvahiClient *client = NULL; AvahiServiceBrowser *pacserve = NULL, *pacdbserve = NULL; int error; @@ -414,6 +442,62 @@ int main(int argc, char ** argv) { hosts->pacdbserve.bad = 0; hosts->next = NULL; + ignore_interfaces = malloc(sizeof(struct ignore_interfaces)); + ignore_interfaces->interface = NULL; + ignore_interfaces->next = NULL; + + + /* parse config file */ + if ((ini = iniparser_load(CONFIGFILE)) == NULL) { + write_log(stderr, "cannot parse file: " CONFIGFILE "\n"); + return EXIT_FAILURE ; + } + + /* store interfaces to ignore */ + if ((values = iniparser_getstring(ini, "general:ignore interfaces", NULL)) != NULL) { +# if defined DEBUG + write_log(stdout, "Ignore interface: [%s]\n", values); +# endif + tmp_ignore_interfaces = ignore_interfaces; + + value = strtok(values, DELIMITER); + while (value != NULL) { + tmp_ignore_interfaces->interface = strdup(value); + tmp_ignore_interfaces->next = malloc(sizeof(struct ignore_interfaces)); + tmp_ignore_interfaces = tmp_ignore_interfaces->next; + value = strtok(NULL, DELIMITER); + } + tmp_ignore_interfaces->interface = NULL; + tmp_ignore_interfaces->next = NULL; + } + + /* add static pacserve hosts */ + if ((values = iniparser_getstring(ini, "general:pacserve hosts", NULL)) != NULL) { +# if defined DEBUG + write_log(stdout, "pacserve hosts: [%s]\n", values); +# endif + value = strtok(values, DELIMITER); + while (value != NULL) { + add_host(value, PACSERVE); + value = strtok(NULL, DELIMITER); + } + } + + /* add static pacdbserve hosts */ + if ((values = iniparser_getstring(ini, "general:pacdbserve hosts", NULL)) != NULL) { +# if defined DEBUG + write_log(stdout, "pacdbserve hosts: [%s]\n", values); +# endif + value = strtok(values, DELIMITER); + while (value != NULL) { + add_host(value, PACDBSERVE); + value = strtok(NULL, DELIMITER); + } + } + + /* done reading config file, free */ + iniparser_freedict(ini); + /* allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { write_log(stderr, "Failed to create simple poll object.\n"); @@ -473,6 +557,13 @@ fail: hosts = tmphosts; } + while(ignore_interfaces->interface != NULL) { + free(ignore_interfaces->interface); + tmp_ignore_interfaces = ignore_interfaces->next; + free(ignore_interfaces); + ignore_interfaces = tmp_ignore_interfaces; + } + if (pacdbserve) avahi_service_browser_free(pacdbserve); -- cgit v1.2.3-54-g00ecf