git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] daemon: look up client-supplied hostname lazily

From
René Scharfe <l.s.r@web.de>
Date
Feb 15, 2015, 18:31 UTC
Message-ID
<54E0E60D.6000305@web.de>

Look up canonical hostname and IP address using getaddrinfo(3) or gethostbyname(3) only if --interpolated-path or --access-hook were specified.

Do that by introducing getter functions for canon_hostname and ip_address and using them for all read accesses. These wrappers call the new helper lookup_hostname(), which sets the variables only at its first call.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 daemon.c | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)
diff --git a/daemon.c b/daemon.c
index 54a03bd..ef41943 100644
--- a/daemon.c
+++ b/daemon.c
@@ -61,6 +61,22 @@ static char *canon_hostname;
 static char *ip_address;
 static char *tcp_port;
 
+static int hostname_lookup_done;
+
+static void lookup_hostname(void);
+
+static const char *get_canon_hostname(void)
+{
+	lookup_hostname();
+	return canon_hostname;
+}
+
+static const char *get_ip_address(void)
+{
+	lookup_hostname();
+	return ip_address;
+}
+
 static void logreport(int priority, const char *err, va_list params)
 {
 	if (log_syslog) {
@@ -147,8 +163,8 @@ static const char *path_ok(const char *directory)
 		struct strbuf_expand_dict_entry dict[6];
 
 		dict[0].placeholder = "H"; dict[0].value = hostname;
-		dict[1].placeholder = "CH"; dict[1].value = canon_hostname;
-		dict[2].placeholder = "IP"; dict[2].value = ip_address;
+		dict[1].placeholder = "CH"; dict[1].value = get_canon_hostname();
+		dict[2].placeholder = "IP"; dict[2].value = get_ip_address();
 		dict[3].placeholder = "P"; dict[3].value = tcp_port;
 		dict[4].placeholder = "D"; dict[4].value = directory;
 		dict[5].placeholder = NULL; dict[5].value = NULL;
@@ -254,8 +270,8 @@ static int run_access_hook(struct daemon_service *service, const char *dir, cons
 	*arg++ = service->name;
 	*arg++ = path;
 	*arg++ = STRARG(hostname);
-	*arg++ = STRARG(canon_hostname);
-	*arg++ = STRARG(ip_address);
+	*arg++ = STRARG(get_canon_hostname());
+	*arg++ = STRARG(get_ip_address());
 	*arg++ = STRARG(tcp_port);
 	*arg = NULL;
 #undef STRARG
@@ -509,6 +525,7 @@ static void parse_host_arg(char *extra_args, int buflen)
 				}
 				free(hostname);
 				hostname = xstrdup_tolower(host);
+				hostname_lookup_done = 0;
 			}
 
 			/* On to the next one */
@@ -517,11 +534,14 @@ static void parse_host_arg(char *extra_args, int buflen)
 		if (extra_args < end && *extra_args)
 			die("Invalid request");
 	}
+}
 
-	/*
-	 * Locate canonical hostname and its IP address.
-	 */
-	if (hostname) {
+/*
+ * Locate canonical hostname and its IP address.
+ */
+static void lookup_hostname(void)
+{
+	if (!hostname_lookup_done && hostname) {
 #ifndef NO_IPV6
 		struct addrinfo hints;
 		struct addrinfo *ai;
@@ -569,6 +589,7 @@ static void parse_host_arg(char *extra_args, int buflen)
 			ip_address = xstrdup(addrbuf);
 		}
 #endif
+		hostname_lookup_done = 1;
 	}
 }
 
-- 
2.3.0
Next: René Scharfe
Message 1 of 2 in “daemon: look up client-supplied hostname lazily”
  1. 1/2 daemon: look up client-supplied hostname lazilyRené Scharfe, Feb 15, 2015
  2. 2/2 daemon: use callback to build interpolated pathRené Scharfe, Feb 15, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.