From: Jonathan Nieder <jrnieder@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>,
Eric Wong <normalperson@yhbt.net>,
Erik Faye-Lund <kusmabite@gmail.com>
Subject: [PATCH 2/5] daemon: make host resolution a separate function
Date: Thu, 8 Mar 2012 07:05:05 -0600 [thread overview]
Message-ID: <20120308130505.GB9426@burratino> (raw)
In-Reply-To: <20120308124857.GA7666@burratino>
Date: Mon, 6 Jun 2011 04:38:45 -0500
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.
Signed-off-by; Jonathan Nieder <jrnieder@gmail.com>
---
From http://thread.gmane.org/gmane.comp.version-control.git/175106/focus=175108
The commit message was tweaked, but the patch is the same as before.
daemon.c | 112 +++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 63 insertions(+), 49 deletions(-)
diff --git a/daemon.c b/daemon.c
index 15ce918a..2a9dfea0 100644
--- a/daemon.c
+++ b/daemon.c
@@ -441,6 +441,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.
*/
@@ -476,56 +535,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.9.2
next prev parent reply other threads:[~2012-03-08 13:05 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-08 12:48 [PATCH 0/5] transport: unify ipv4 and ipv6 code paths Jonathan Nieder
2012-03-08 13:03 ` [PATCH 1/5] transport: expose git_tcp_connect() and friends in new tcp.h Jonathan Nieder
2012-03-08 15:28 ` Erik Faye-Lund
2012-03-08 13:05 ` Jonathan Nieder [this message]
2012-03-08 13:06 ` [PATCH 3/5] daemon: move locate_host() to tcp lib Jonathan Nieder
2012-03-08 13:09 ` [PATCH 4/5] tcp: unify ipv4 and ipv6 code paths Jonathan Nieder
2012-03-08 15:39 ` Erik Faye-Lund
2012-03-08 21:10 ` Jonathan Nieder
2012-03-08 13:11 ` [PATCH 5/5] daemon: check for errors retrieving IP address Jonathan Nieder
2012-03-08 13:16 ` [PATCH 6/5] tcp: make dns_resolve() return an error code Jonathan Nieder
2012-03-08 13:21 ` [PATCH 7/5] transport: optionally honor DNS SRV records Jonathan Nieder
2012-03-08 16:18 ` Erik Faye-Lund
2012-03-08 21:35 ` Jonathan Nieder
2012-03-09 7:07 ` Johannes Sixt
2012-03-09 8:00 ` Jonathan Nieder
2012-03-08 13:23 ` [PATCH 8/5] srv: tolerate broken DNS replies Jonathan Nieder
2012-03-08 22:28 ` Richard Hartmann
2012-06-11 18:12 ` [PATCH 0/5] transport: unify ipv4 and ipv6 code paths Erik Faye-Lund
2012-06-11 18:59 ` Junio C Hamano
2012-06-14 5:02 ` 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=20120308130505.GB9426@burratino \
--to=jrnieder@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=kusmabite@gmail.com \
--cc=normalperson@yhbt.net \
--cc=peff@peff.net \
/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).