daemon: look up client-supplied hostname lazily

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Feb 15, 2015 at 19:31 UTC edef953e482ce212b7d68bbe0f59e34b5552db3e
1 file changed +29 -8
daemon.c
+29 -8
@@ -61,6 +61,22 @@ static char *canon_hostname;
61 static char *ip_address;
62 static char *tcp_port;
63
64 +static int hostname_lookup_done;
65 +
66 +static void lookup_hostname(void);
67 +
68 +static const char *get_canon_hostname(void)
69 +{
70 + lookup_hostname();
71 + return canon_hostname;
72 +}
73 +
74 +static const char *get_ip_address(void)
75 +{
76 + lookup_hostname();
77 + return ip_address;
78 +}
79 +
80 static void logreport(int priority, const char *err, va_list params)
81 {
82 if (log_syslog) {
@@ -147,8 +163,8 @@ static const char *path_ok(const char *directory)
163 struct strbuf_expand_dict_entry dict[6];
164
165 dict[0].placeholder = "H"; dict[0].value = hostname;
150 - dict[1].placeholder = "CH"; dict[1].value = canon_hostname;
151 - dict[2].placeholder = "IP"; dict[2].value = ip_address;
166 + dict[1].placeholder = "CH"; dict[1].value = get_canon_hostname();
167 + dict[2].placeholder = "IP"; dict[2].value = get_ip_address();
168 dict[3].placeholder = "P"; dict[3].value = tcp_port;
169 dict[4].placeholder = "D"; dict[4].value = directory;
170 dict[5].placeholder = NULL; dict[5].value = NULL;
@@ -271,8 +287,8 @@ static int run_access_hook(struct daemon_service *service, const char *dir, cons
287 *arg++ = service->name;
288 *arg++ = path;
289 *arg++ = STRARG(hostname);
274 - *arg++ = STRARG(canon_hostname);
275 - *arg++ = STRARG(ip_address);
290 + *arg++ = STRARG(get_canon_hostname());
291 + *arg++ = STRARG(get_ip_address());
292 *arg++ = STRARG(tcp_port);
293 *arg = NULL;
294 #undef STRARG
@@ -529,6 +545,7 @@ static void parse_host_arg(char *extra_args, int buflen)
545 }
546 free(hostname);
547 hostname = xstrdup_tolower(host);
548 + hostname_lookup_done = 0;
549 }
550
551 /* On to the next one */
@@ -537,11 +554,14 @@ static void parse_host_arg(char *extra_args, int buflen)
554 if (extra_args < end && *extra_args)
555 die("Invalid request");
556 }
557 +}
558
541 - /*
542 - * Locate canonical hostname and its IP address.
543 - */
544 - if (hostname) {
559 +/*
560 + * Locate canonical hostname and its IP address.
561 + */
562 +static void lookup_hostname(void)
563 +{
564 + if (!hostname_lookup_done && hostname) {
565 #ifndef NO_IPV6
566 struct addrinfo hints;
567 struct addrinfo *ai;
@@ -589,6 +609,7 @@ static void parse_host_arg(char *extra_args, int buflen)
609 ip_address = xstrdup(addrbuf);
610 }
611 #endif
612 + hostname_lookup_done = 1;
613 }
614 }
615