File: 0003-daemon-make-host-resolution-into-a-separate-function.diff

package info (click to toggle)
git 1%3A2.1.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 28,832 kB
  • sloc: ansic: 154,058; sh: 131,926; perl: 28,828; tcl: 21,269; python: 5,337; makefile: 3,509; lisp: 1,786; php: 120; asm: 98; csh: 45
file content (161 lines) | stat: -rw-r--r-- 4,166 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
From 9007fdecdd52d6f95d6da29a869f7cbf918f3d71 Mon Sep 17 00:00:00 2001
From: Jonathan Nieder <jrnieder@gmail.com>
Date: Mon, 6 Jun 2011 04:38:45 -0500
Subject: daemon: make host resolution into a separate function

The locate_host() function looks up the IP address and canonical
hostname of the host named by its argument.  If it succeeds,
*ip_address and *canon_hostname are freed and replaced by the hosts'
IP address and canonical hostname, respectively, as strings.  If it
fails, *ip_address and *canon_hostname are left alone.

The git daemon uses this functionality to support the %IP and %CH
placeholders for its --interpolated-path feature.  Splitting it out as
a separate function would make it easier to tweak, for example to
unify the ipv6 and ipv4 code paths or to share code with other parts
of git that make DNS queries.

No functional change intended.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 daemon.c | 115 ++++++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 65 insertions(+), 50 deletions(-)

diff --git a/daemon.c b/daemon.c
index 4b02184..66c2545 100644
--- a/daemon.c
+++ b/daemon.c
@@ -504,6 +504,67 @@ static void parse_host_and_port(char *hostport, char **host,
 	}
 }
 
+#ifndef NO_IPV6
+
+static void locate_host(const char *hostname, char **ip_address,
+						char **canon_hostname)
+{
+	struct addrinfo hints;
+	struct addrinfo *ai;
+	int gai;
+	static char addrbuf[HOST_NAME_MAX + 1];
+	struct sockaddr_in *sin_addr;
+
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_flags = AI_CANONNAME;
+
+	gai = getaddrinfo(hostname, NULL, &hints, &ai);
+	if (gai)
+		return;
+
+	sin_addr = (void *)ai->ai_addr;
+	inet_ntop(AF_INET, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
+	free(*ip_address);
+	*ip_address = xstrdup(addrbuf);
+
+	free(*canon_hostname);
+	*canon_hostname = xstrdup(ai->ai_canonname ?
+				  ai->ai_canonname : *ip_address);
+
+	freeaddrinfo(ai);
+}
+
+#else
+
+static void locate_host(const char *hostname, char **ip_address,
+						char **canon_hostname)
+{
+	struct hostent *hent;
+	struct sockaddr_in sa;
+	char **ap;
+	static char addrbuf[HOST_NAME_MAX + 1];
+
+	hent = gethostbyname(hostname);
+	if (!hent)
+		return;
+
+	ap = hent->h_addr_list;
+	memset(&sa, 0, sizeof sa);
+	sa.sin_family = hent->h_addrtype;
+	sa.sin_port = htons(0);
+	memcpy(&sa.sin_addr, *ap, hent->h_length);
+
+	inet_ntop(hent->h_addrtype, &sa.sin_addr,
+		  addrbuf, sizeof(addrbuf));
+
+	free(*canon_hostname);
+	*canon_hostname = xstrdup(hent->h_name);
+	free(*ip_address);
+	*ip_address = xstrdup(addrbuf);
+}
+
+#endif
+
 /*
  * Read the host as supplied by the client connection.
  */
@@ -539,57 +600,11 @@ static void parse_host_arg(char *extra_args, int buflen)
 	}
 
 	/*
-	 * Locate canonical hostname and its IP address.
+	 * Locate canonical hostname and its IP address,
+	 * if possible.
 	 */
-	if (hostname) {
-#ifndef NO_IPV6
-		struct addrinfo hints;
-		struct addrinfo *ai;
-		int gai;
-		static char addrbuf[HOST_NAME_MAX + 1];
-
-		memset(&hints, 0, sizeof(hints));
-		hints.ai_flags = AI_CANONNAME;
-
-		gai = getaddrinfo(hostname, NULL, &hints, &ai);
-		if (!gai) {
-			struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
-
-			inet_ntop(AF_INET, &sin_addr->sin_addr,
-				  addrbuf, sizeof(addrbuf));
-			free(ip_address);
-			ip_address = xstrdup(addrbuf);
-
-			free(canon_hostname);
-			canon_hostname = xstrdup(ai->ai_canonname ?
-						 ai->ai_canonname : ip_address);
-
-			freeaddrinfo(ai);
-		}
-#else
-		struct hostent *hent;
-		struct sockaddr_in sa;
-		char **ap;
-		static char addrbuf[HOST_NAME_MAX + 1];
-
-		hent = gethostbyname(hostname);
-		if (hent) {
-			ap = hent->h_addr_list;
-			memset(&sa, 0, sizeof sa);
-			sa.sin_family = hent->h_addrtype;
-			sa.sin_port = htons(0);
-			memcpy(&sa.sin_addr, *ap, hent->h_length);
-
-			inet_ntop(hent->h_addrtype, &sa.sin_addr,
-				  addrbuf, sizeof(addrbuf));
-
-			free(canon_hostname);
-			canon_hostname = xstrdup(hent->h_name);
-			free(ip_address);
-			ip_address = xstrdup(addrbuf);
-		}
-#endif
-	}
+	if (hostname)
+		locate_host(hostname, &ip_address, &canon_hostname);
 }
 
 
-- 
2.1.0.rc2.206.gedb03e5