aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2018-07-05 11:58:24 +0200
committerGravatar Christian Hesse <mail@eworm.de>2018-07-05 11:58:24 +0200
commitb8aeafb0377321d8443fc36a7a1ae660f407cfd2 (patch)
tree1fa776df51ae5b41f2dcd65472458fa806f4d12e
parenta834db5dc933a4b48c8323ae3e490448a86b448f (diff)
downloadpacredir-b8aeafb0377321d8443fc36a7a1ae660f407cfd2.tar.gz
pacredir-b8aeafb0377321d8443fc36a7a1ae660f407cfd2.tar.zst
drop 'struct services'
Now that we have just one service this can be merged with 'struct hosts'.
-rw-r--r--pacredir.c50
-rw-r--r--pacredir.h28
2 files changed, 34 insertions, 44 deletions
diff --git a/pacredir.c b/pacredir.c
index 76dc52d..05caf1d 100644
--- a/pacredir.c
+++ b/pacredir.c
@@ -95,10 +95,10 @@ int add_host(const char * host, AvahiProtocol proto, const char * address, const
tmphosts->proto = AVAHI_PROTO_UNSPEC;
*tmphosts->address = 0;
- tmphosts->pacserve.port = 0;
- tmphosts->pacserve.online = 0;
- tmphosts->pacserve.badtime = 0;
- tmphosts->pacserve.badcount = 0;
+ tmphosts->port = 0;
+ tmphosts->online = 0;
+ tmphosts->badtime = 0;
+ tmphosts->badcount = 0;
tmphosts->next = malloc(sizeof(struct hosts));
tmphosts->next->host = NULL;
@@ -109,13 +109,12 @@ update:
if (address != NULL)
memcpy(tmphosts->address, address, AVAHI_ADDRESS_STR_MAX);
- tmphosts->pacserve.online = 1;
- tmphosts->pacserve.port = port;
- request.service = &tmphosts->pacserve;
+ tmphosts->online = 1;
+ tmphosts->port = port;
/* do a first request and let get_http_code() set the bad status */
- request.host = tmphosts->host;
- request.url = get_url(request.host, tmphosts->proto, tmphosts->address, request.service->port, 0, "");
+ request.host = tmphosts;
+ request.url = get_url(request.host->host, request.host->proto, request.host->address, request.host->port, 0, "");
request.http_code = 0;
request.last_modified = 0;
get_http_code(&request);
@@ -133,7 +132,7 @@ int remove_host(const char * host, AvahiProtocol proto, const char * type) {
if (verbose > 0)
write_log(stdout, "Marking service %s on host %s (%s) offline\n",
type, host, avahi_proto_to_string(proto));
- tmphosts->pacserve.online = 0;
+ tmphosts->online = 0;
break;
}
tmphosts = tmphosts->next;
@@ -303,16 +302,16 @@ static void * get_http_code(void * data) {
/* perform the request */
if ((res = curl_easy_perform(curl)) != CURLE_OK) {
write_log(stderr, "Could not connect to server %s on port %d: %s\n",
- request->host, request->service->port,
+ request->host->host, request->host->port,
*errbuf != 0 ? errbuf : curl_easy_strerror(res));
request->http_code = 0;
request->last_modified = 0;
- request->service->badtime = tv.tv_sec;
- request->service->badcount++;
+ request->host->badtime = tv.tv_sec;
+ request->host->badcount++;
return NULL;
} else {
- request->service->badtime = 0;
- request->service->badcount = 0;
+ request->host->badtime = 0;
+ request->host->badcount = 0;
}
/* get http status code */
@@ -426,11 +425,11 @@ static int ahc_echo(void * cls,
/* try to find a server with most recent file */
while (tmphosts->host != NULL) {
- struct services *service = &tmphosts->pacserve;
- time_t badtime = service->badtime + service->badcount * BADTIME;
+ struct hosts * host = tmphosts;
+ time_t badtime = host->badtime + host->badcount * BADTIME;
/* skip host if offline or had a bad request within last BADTIME seconds */
- if (service->online == 0) {
+ if (host->online == 0) {
if (verbose > 0)
write_log(stdout, "Service %s on host %s is offline, skipping\n",
PACSERVE, tmphosts->host);
@@ -472,9 +471,8 @@ static int ahc_echo(void * cls,
request = requests[req_count];
/* prepare request struct */
- request->host = tmphosts->host;
- request->service = &(tmphosts->pacserve);
- request->url = get_url(tmphosts->host, tmphosts->proto, tmphosts->address, request->service->port, dbfile, basename);
+ request->host = tmphosts;
+ request->url = get_url(request->host->host, request->host->proto, request->host->address, request->host->port, dbfile, basename);
request->http_code = 0;
request->last_modified = 0;
@@ -521,7 +519,7 @@ static int ahc_echo(void * cls,
if (url != NULL)
free(url);
url = request->url;
- host = request->host;
+ host = request->host->host;
http_code = MHD_HTTP_OK;
last_modified = request->last_modified;
time_total = request->time_total;
@@ -592,8 +590,8 @@ void sighup_callback(int signal) {
write_log(stdout, "Received SIGHUP, resetting bad status for hosts.\n");
while (tmphosts->host != NULL) {
- tmphosts->pacserve.badtime = 0;
- tmphosts->pacserve.badcount = 0;
+ tmphosts->badtime = 0;
+ tmphosts->badcount = 0;
tmphosts = tmphosts->next;
}
}
@@ -655,8 +653,8 @@ int main(int argc, char ** argv) {
/* allocate first struct element as dummy */
hosts = malloc(sizeof(struct hosts));
hosts->host = NULL;
- hosts->pacserve.online = 0;
- hosts->pacserve.badtime = 0;
+ hosts->online = 0;
+ hosts->badtime = 0;
hosts->next = NULL;
ignore_interfaces = malloc(sizeof(struct ignore_interfaces));
diff --git a/pacredir.h b/pacredir.h
index bddab74..75017b7 100644
--- a/pacredir.h
+++ b/pacredir.h
@@ -44,18 +44,6 @@
#define PROGNAME "pacredir"
-/* services */
-struct services {
- /* network port */
- uint16_t port;
- /* true if host/service is online */
- uint8_t online;
- /* unix timestamp of last bad request */
- __time_t badtime;
- /* count the number of bad requests */
- unsigned int badcount;
-};
-
/* hosts */
struct hosts {
/* host name */
@@ -64,8 +52,14 @@ struct hosts {
AvahiProtocol proto;
/* resolved address */
char address[AVAHI_ADDRESS_STR_MAX];
- /* online status and bad time for services */
- struct services pacserve;
+ /* network port */
+ uint16_t port;
+ /* true if host/service is online */
+ uint8_t online;
+ /* unix timestamp of last bad request */
+ __time_t badtime;
+ /* count the number of bad requests */
+ unsigned int badcount;
/* pointer to next struct element */
struct hosts * next;
};
@@ -80,10 +74,8 @@ struct ignore_interfaces {
/* request */
struct request {
- /* host name */
- const char * host;
- /* pointer to service */
- struct services * service;
+ /* host infos */
+ struct hosts * host;
/* url */
char * url;
/* HTTP status code */