Skip to content

Commit edef953

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c84ac8 commit edef953

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

daemon.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ static char *canon_hostname;
6161
static char *ip_address;
6262
static char *tcp_port;
6363

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+
6480
static void logreport(int priority, const char *err, va_list params)
6581
{
6682
if (log_syslog) {
@@ -147,8 +163,8 @@ static const char *path_ok(const char *directory)
147163
struct strbuf_expand_dict_entry dict[6];
148164

149165
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();
152168
dict[3].placeholder = "P"; dict[3].value = tcp_port;
153169
dict[4].placeholder = "D"; dict[4].value = directory;
154170
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
271287
*arg++ = service->name;
272288
*arg++ = path;
273289
*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());
276292
*arg++ = STRARG(tcp_port);
277293
*arg = NULL;
278294
#undef STRARG
@@ -529,6 +545,7 @@ static void parse_host_arg(char *extra_args, int buflen)
529545
}
530546
free(hostname);
531547
hostname = xstrdup_tolower(host);
548+
hostname_lookup_done = 0;
532549
}
533550

534551
/* On to the next one */
@@ -537,11 +554,14 @@ static void parse_host_arg(char *extra_args, int buflen)
537554
if (extra_args < end && *extra_args)
538555
die("Invalid request");
539556
}
557+
}
540558

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) {
545565
#ifndef NO_IPV6
546566
struct addrinfo hints;
547567
struct addrinfo *ai;
@@ -589,6 +609,7 @@ static void parse_host_arg(char *extra_args, int buflen)
589609
ip_address = xstrdup(addrbuf);
590610
}
591611
#endif
612+
hostname_lookup_done = 1;
592613
}
593614
}
594615

0 commit comments

Comments
 (0)