summaryrefslogtreecommitdiffstats
path: root/pacredir.c
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2013-11-04 09:30:17 +0100
committerGravatar Christian Hesse <mail@eworm.de>2013-11-04 09:30:17 +0100
commit3ea4fd22575e1d7f081b1777809a4aa9b6af8a0c (patch)
tree54963184b844618f8d996f37e17d465a1640730a /pacredir.c
parent4c50bfc7c8f6c1575586dde1d5053d72dc0b4937 (diff)
downloadpacredir-3ea4fd22575e1d7f081b1777809a4aa9b6af8a0c.tar.gz
pacredir-3ea4fd22575e1d7f081b1777809a4aa9b6af8a0c.tar.zst
introduce add_host() and remove_host()
Diffstat (limited to 'pacredir.c')
-rw-r--r--pacredir.c98
1 files changed, 57 insertions, 41 deletions
diff --git a/pacredir.c b/pacredir.c
index 3429d44..aef0fba 100644
--- a/pacredir.c
+++ b/pacredir.c
@@ -70,11 +70,65 @@ char * get_fqdn(const char * hostname, const char * domainname) {
return name;
}
+/*** add_host ***/
+int add_host(const char * host, const char * type) {
+ struct hosts * tmphosts = hosts;
+
+ while (tmphosts->host != NULL) {
+ if (strcmp(tmphosts->host, host) == 0) {
+ /* host already exists */
+ write_log(stdout, "Updating service %s on %s\n", type, host);
+ goto update;
+ }
+ tmphosts = tmphosts->next;
+ }
+ /* host not found, adding a new one */
+ write_log(stdout, "Adding host %s with service %s\n", host, type);
+ tmphosts->host = strdup(host);
+ tmphosts->pacserve.online = 0;
+ tmphosts->pacserve.bad = 0;
+ tmphosts->pacdbserve.online = 0;
+ tmphosts->pacdbserve.bad = 0;
+ tmphosts->next = realloc(tmphosts->next, sizeof(struct hosts));
+ tmphosts->next->host = NULL;
+ tmphosts->next->next = NULL;
+
+update:
+ if (strcmp(type, PACSERVE) == 0) {
+ tmphosts->pacserve.online = 1;
+ tmphosts->pacserve.bad = 0;
+ } else if (strcmp(type, PACDBSERVE) == 0) {
+ tmphosts->pacdbserve.online = 1;
+ tmphosts->pacdbserve.bad = 0;
+ }
+
+ return EXIT_SUCCESS;
+}
+
+/*** remove_host ***/
+int remove_host(const char * host, const char * type) {
+ struct hosts * tmphosts = hosts;
+
+ while (tmphosts->host != NULL) {
+ if (strcmp(tmphosts->host, host) == 0) {
+ write_log(stdout, "Marking service %s on host %s offline\n", type, host);
+ if (strcmp(type, PACSERVE) == 0) {
+ tmphosts->pacserve.online = 0;
+ } else if (strcmp(type, PACDBSERVE) == 0) {
+ tmphosts->pacdbserve.online = 0;
+ }
+ break;
+ }
+ tmphosts = tmphosts->next;
+ }
+
+ return EXIT_SUCCESS;
+}
+
/*** browse_callback ***
* Called whenever a new services becomes available on the LAN or is removed from the LAN */
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) {
- struct hosts * tmphosts = hosts;
char * host;
assert(b);
@@ -96,34 +150,7 @@ static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, Avah
if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
goto out;
- while (tmphosts->host != NULL) {
- if (strcmp(tmphosts->host, host) == 0) {
- /* host already exists */
- write_log(stdout, "Updating service %s on %s\n", type, host);
- goto update;
- }
- tmphosts = tmphosts->next;
- }
- /* host not found, adding a new one */
- write_log(stdout, "Adding host %s with service %s\n", host, type);
- tmphosts->host = strdup(host);
- tmphosts->pacserve.online = 0;
- tmphosts->pacserve.bad = 0;
- tmphosts->pacdbserve.online = 0;
- tmphosts->pacdbserve.bad = 0;
- tmphosts->next = realloc(tmphosts->next, sizeof(struct hosts));
- tmphosts->next->host = NULL;
- tmphosts->next->next = NULL;
-
-update:
- if (strcmp(type, PACSERVE) == 0) {
- tmphosts->pacserve.online = 1;
- tmphosts->pacserve.bad = 0;
- } else if (strcmp(type, PACDBSERVE) == 0) {
- tmphosts->pacdbserve.online = 1;
- tmphosts->pacdbserve.bad = 0;
- }
-
+ add_host(host, type);
out:
free(host);
@@ -136,18 +163,7 @@ out:
write_log(stdout, "REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
# endif
- while (tmphosts->host != NULL) {
- if (strcmp(tmphosts->host, host) == 0) {
- write_log(stdout, "Marking service %s on host %s offline\n", type, host);
- if (strcmp(type, PACSERVE) == 0) {
- tmphosts->pacserve.online = 0;
- } else if (strcmp(type, PACDBSERVE) == 0) {
- tmphosts->pacdbserve.online = 0;
- }
- break;
- }
- tmphosts = tmphosts->next;
- }
+ remove_host(host, type);
free(host);