From 7bcf3ff3bd4006943839f560724d347b85df2cc7 Mon Sep 17 00:00:00 2001
From: Jan Vcelak <jan.vcelak@nic.cz>
Date: Wed, 5 Nov 2014 19:09:56 +0100
Subject: [PATCH] log: cache hostname lookup for nsid and identity

---
 src/knot/conf/cf-parse.y | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/knot/conf/cf-parse.y b/src/knot/conf/cf-parse.y
index 135a86acfc..95eeaa5171 100644
--- a/src/knot/conf/cf-parse.y
+++ b/src/knot/conf/cf-parse.y
@@ -54,6 +54,8 @@ static conf_log_t *this_log = 0;
 static conf_log_map_t *this_logmap = 0;
 //#define YYERROR_VERBOSE 1
 
+static char *cache_hostname = NULL;
+
 #define SET_NUM(out, in, min, max, name)				\
 {									\
 	if (in < min || in > max) {					\
@@ -68,6 +70,11 @@ static conf_log_map_t *this_logmap = 0;
 #define SET_INT(out, in, name) SET_NUM(out, in, 0, INT_MAX, name);
 #define SET_SIZE(out, in, name) SET_NUM(out, in, 0, SIZE_MAX, name);
 
+static void conf_start(void *scanner)
+{
+	cache_hostname = NULL;
+}
+
 static void conf_init_iface(void *scanner, char* ifname)
 {
 	this_iface = malloc(sizeof(conf_iface_t));
@@ -422,12 +429,18 @@ static void opt_replace(char **opt, char *new_opt, bool val)
 
 static char *get_hostname(void *scanner)
 {
+	if (cache_hostname) {
+		return strdup(cache_hostname);
+	}
+
 	char *fqdn = sockaddr_hostname();
 	if (!fqdn) {
 		cf_warning(scanner, "cannot retrieve host FQDN");
 		return NULL;
 	}
 
+	cache_hostname = fqdn;
+
 	return fqdn;
 }
 
@@ -531,7 +544,7 @@ static void ident_auto(void *scanner, int tok, conf_t *conf, bool val)
 
 %%
 
-config: conf_entries END { return 0; } ;
+config: { conf_start(scanner); } conf_entries END { return 0; } ;
 
 conf_entries:
  /* EMPTY */
-- 
GitLab