From: Jonathan Nieder <jrnieder@gmail.com>
To: Julien Cristau <jcristau@debian.org>
Cc: git@vger.kernel.org, Jeff King <peff@peff.net>,
Ilari Liusvaara <ilari.liusvaara@elisanet.fi>,
"Shawn O. Pearce" <spearce@spearce.org>
Subject: [PATCH 2/6] daemon: make host resolution into a separate function
Date: Mon, 6 Jun 2011 04:38:45 -0500 [thread overview]
Message-ID: <20110606093845.GG8015@elie> (raw)
In-Reply-To: <20110606093019.GD8015@elie>
For each incoming connection, the git daemon runs a DNS query using
the client-supplied hostname parameter, to determine the canonical
hostname and IP address in case it is needed by the
--interpolated-path feature (to distinguish between virtual hosts).
Put the code for this in a separate function so the details of this
lookup can be easily tweaked.
Signed-off-by; Jonathan Nieder <jrnieder@gmail.com>
---
daemon.c | 112 +++++++++++++++++++++++++++++++++++---------------------------
1 files changed, 63 insertions(+), 49 deletions(-)
diff --git a/daemon.c b/daemon.c
index 4c8346d..3958cb6 100644
--- a/daemon.c
+++ b/daemon.c
@@ -432,6 +432,65 @@ 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);
+
+ 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.
*/
@@ -467,56 +526,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);
-
- 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);
}
--
1.7.5.3
next prev parent reply other threads:[~2011-06-06 9:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20110524202249.GA5889@elie>
[not found] ` <20110524230900.GA9440@radis.liafa.jussieu.fr>
2011-06-06 9:30 ` [RFC/PATCH 0/6] git: please honor DNS SRV records Jonathan Nieder
2011-06-06 9:37 ` [PATCH 1/6] transport: expose git_tcp_connect and friends in new tcp.h Jonathan Nieder
2011-06-06 9:38 ` Jonathan Nieder [this message]
2011-06-06 9:39 ` [PATCH 3/6] daemon: move locate_host to tcp.c Jonathan Nieder
2011-06-06 9:40 ` [PATCH 4/6] transport: fix index in ipv6 connection failed message Jonathan Nieder
2011-06-06 9:41 ` [PATCH 5/6] tcp: unify ipv4 and ipv6 code paths Jonathan Nieder
2011-06-06 10:01 ` Jonathan Nieder
2011-06-06 9:46 ` [PATCH 6/6] transport: learn to honor DNS SRV records Jonathan Nieder
2011-06-06 9:49 ` [RFC/PATCH 0/6] git: please " Jonathan Nieder
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110606093845.GG8015@elie \
--to=jrnieder@gmail.com \
--cc=git@vger.kernel.org \
--cc=ilari.liusvaara@elisanet.fi \
--cc=jcristau@debian.org \
--cc=peff@peff.net \
--cc=spearce@spearce.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).