transport: unify ipv4 and ipv6 code paths
cover transport: unify ipv4 and ipv6 code paths
Hi,
These patches eliminate some ifdef-ery concerning NO_IPV6. I used them when writing the SRV patch, which applies on top, but it's probably best to think of it as an independent topic.
Patch 4 is the heart of the series. It provides an interface similar to getaddrinfo that can be implemented on top of either gethostbyname or getaddrinfo and puts each implementation in a separate file. This way, callers can just use the common API and they do not need to have two copies of their code, one for each host resolution API.
Patches 1-3 move code around until all the code that patch 4 touches is in one place.
Patches 5 is a potential error handling improvement noticed while writing patches 1-4. It's probably not actually needed but it was a comfort to me.
These patches have been in use in Debian since June of last year. I'd like to see this in mainline early in the 1.7.11 cycle to make coding that touches this area during that cycle more pleasant. Thoughts of all kinds welcome.
Jonathan Nieder (5):
transport: expose git_tcp_connect and friends in new tcp.h
daemon: make host resolution into a separate function
daemon: move locate_host() to tcp.c
tcp: unify ipv4 and ipv6 code paths
daemon: check for errors retrieving IP address
Makefile | 7 ++ connect.c | 277 +----------------------------------------------------------- daemon.c | 58 ++----------- dns-ipv4.c | 33 ++++++++ dns-ipv4.h | 68 +++++++++++++++ dns-ipv6.c | 49 +++++++++++ dns-ipv6.h | 31 +++++++ tcp.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++ tcp.h | 11 +++ 9 files changed, 422 insertions(+), 329 deletions(-) create mode 100644 dns-ipv4.c create mode 100644 dns-ipv4.h create mode 100644 dns-ipv6.c create mode 100644 dns-ipv6.h create mode 100644 tcp.c create mode 100644 tcp.h
PATCH 1/5 transport: expose git_tcp_connect() and friends in new tcp.h
Date: Mon, 6 Jun 2011 04:37:14 -0500
Split off a new tcp.c with the functions git_tcp_connect() and git_proxy_connect() that resolve and connect to a host.
Part of a series to teach git to respect DNS SRV records when making new connections. This is a preliminary step to make the connection library easier to understand before changing it.
---
From http://thread.gmane.org/gmane.comp.version-control.git/175106/focus=175107
Reviewing mass code migration is a little tricky. My current favorite strategy is
git show connect.c Makefile tcp.h
git diff HEAD^:connect.c HEAD:tcp.c
to make sure nothing important was added or dropped.
Makefile | 2 + connect.c | 277 +----------------------------------------------------------- tcp.c | 278 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tcp.h | 8 ++ 4 files changed, 289 insertions(+), 276 deletions(-) create mode 100644 tcp.c create mode 100644 tcp.h
Makefile +2 -0
@@ -736,6 +736,7 @@ LIB_OBJS += string-list.o LIB_OBJS += submodule.o LIB_OBJS += symlinks.o LIB_OBJS += tag.o +LIB_OBJS += tcp.o LIB_OBJS += trace.o LIB_OBJS += transport.o LIB_OBJS += transport-helper.o
@@ -2148,6 +2149,7 @@ builtin/prune.o builtin/reflog.o reachable.o: reachable.h builtin/commit.o builtin/revert.o wt-status.o: wt-status.h builtin/tar-tree.o archive-tar.o: tar.h connect.o transport.o url.o http-backend.o: url.h +connect.o tcp.o: tcp.h http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h
connect.c +1 -276
289 lines not on this page · raw message
tcp.c new file +278 -0
278 lines not on this page · raw message
tcp.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifndef TCP_H +#define TCP_H + +extern int git_use_proxy (const char *host); +extern void git_tcp_connect (int fd[2 ],char *host,int flags); +extern struct child_process *git_proxy_connect (int fd[2 ],char *host); + +#endif
Signature · 1 line
1.7.9.2
Re: [PATCH 1/5] transport: expose git_tcp_connect() and friends in new tcp.h
Quoted 10 lines · Jonathan Nieder
Date: Mon, 6 Jun 2011 04:37:14 -0500
Split off a new
tcp.cwith the functionsgit_tcp_connect()andgit_proxy_connect()that resolve and connect to a host.Part of a series to teach git to respect DNS SRV records when making new connections. This is a preliminary step to make the connection library easier to understand before changing it.
Signed-off-by Jonathan Nieder <[email protected]>
Nice. Now that this has been libified, someone can probably get rid of some code in imap-send.c (specifically in the imap_open_store function) on top of your topic...
PATCH 2/5 daemon: make host resolution a separate function
In reply to Jonathan NiederDate: 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 <[email protected]>
---
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(-)
daemon.c +63 -49
@@ -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); }
Signature · 1 line
1.7.9.2
PATCH 3/5 daemon: move locate_host() to tcp lib
In reply to Jonathan NiederDate: Mon, 6 Jun 2011 04:39:29 -0500
Keep the different name resolution functions close together so they can learn from each other and perhaps share code in the future.
---
From http://thread.gmane.org/gmane.comp.version-control.git/175106/focus=175108
The original forgot to move HOST_NAME_MAX, causing build failures on systems that lack it.
Makefile | 2 +- daemon.c | 66 ++------------------------------------------------------------ tcp.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tcp.h | 3 +++ 4 files changed, 63 insertions(+), 65 deletions(-)
Makefile +1 -1
@@ -2149,7 +2149,7 @@ builtin/prune.o builtin/reflog.o reachable.o: reachable.h builtin/commit.o builtin/revert.o wt-status.o: wt-status.h builtin/tar-tree.o archive-tar.o: tar.h connect.o transport.o url.o http-backend.o: url.h -connect.o tcp.o: tcp.h +connect.o daemon.o tcp.o: tcp.h http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h
daemon.c +2 -64
88 lines not on this page · raw message
tcp.c +57 -0
75 lines not on this page · raw message
tcp.h +3 -0
@@ -1,6 +1,9 @@#ifndef TCP_H #define TCP_H +extern void git_locate_host (const char *hostname, +char **ip_address,char **canon_hostname); +extern int git_use_proxy (const char *host);extern void git_tcp_connect (int fd[2 ],char *host,int flags);extern struct child_process *git_proxy_connect (int fd[2 ],char *host);
Signature · 1 line
1.7.9.2
PATCH 4/5 tcp: unify ipv4 and ipv6 code paths
In reply to Jonathan NiederDate: Mon, 6 Jun 2011 04:41:28 -0500
The new DNS API abstracts away differences between the gethostbyname- and getaddrinfo-centric interfaces for looking up a host, making the code to use them in connect.c a little easier to read.
To make a lookup:
resolver_result ai; dns_resolve(host, port, 0, &ai); ... dns_free(ai);
To iterate over responses:
resolved_address i;
for_each_address(i, ai) {
...
}In the !NO_IPV6 codepath, the git_locate_host function that is used to find the canonical IP and hostname for a git server's public address (for virtual hosting) tells getaddrinfo to restrict attention to TCP services after this patch. That should make no difference because the service parameter is NULL.
No functional change intended.
---
This is the title feature, corresponding to
http://thread.gmane.org/gmane.comp.version-control.git/175106/focus=175111
It needed changes to adjust to released changes in the code it touches, but nothing is fundamentally different from v1.
Makefile | 5 ++ dns-ipv4.c | 33 +++++++++++ dns-ipv4.h | 69 +++++++++++++++++++++++ dns-ipv6.c | 49 ++++++++++++++++ dns-ipv6.h | 31 +++++++++++ tcp.c | 182 +++++++++++------------------------------------------------- 6 files changed, 218 insertions(+), 151 deletions(-) create mode 100644 dns-ipv4.c create mode 100644 dns-ipv4.h create mode 100644 dns-ipv6.c create mode 100644 dns-ipv6.h
Makefile +5 -0
@@ -1612,6 +1612,11 @@ ifdef NO_TRUSTABLE_FILEMODE endif ifdef NO_IPV6 BASIC_CFLAGS += -DNO_IPV6 + LIB_OBJS += dns-ipv4.o + LIB_H += dns-ipv4.h +else + LIB_OBJS += dns-ipv6.o + LIB_H += dns-ipv6.h endif ifdef NO_UINTMAX_T BASIC_CFLAGS += -Duintmax_t=uint32_t
dns-ipv4.c new file +33 -0
@@ -0,0 +1,33 @@ +#include "cache.h" +#include "dns-ipv4.h" + +int dns_resolve (const char *host,const char *port,int flags, +resolver_result *res) +{ +char *ep; +struct hostent *he; +unsigned int nport; + + he =gethostbyname (host); +if (!he && (flags &RESOLVE_FAIL_QUIETLY )) +return -1 ; +if (!he) +die ("Unable to look up %s (%s)" , host,hstrerror (h_errno)); + +if (!port) { + nport =0 ; + goto done; + } + nport =strtoul (port, &ep,10 ); +if ( ep == port || *ep ) { +/* Not numeric */ +struct servent *se =getservbyname (port,"tcp" ); +if ( !se ) +die ("Unknown port %s" , port); + nport = se->s_port; + } +done: + res->he = he; + res->port = nport; +return 0 ; +}
dns-ipv4.h new file +69 -0
69 lines not on this page · raw message
dns-ipv6.c new file +49 -0
49 lines not on this page · raw message
dns-ipv6.h new file +31 -0
@@ -0,0 +1,31 @@ +#ifndef DNS_IPV6_H +#define DNS_IPV6_H + +typedef struct addrinfo *resolver_result ; +typedef const struct addrinfo *resolved_address ; + +enum { +RESOLVE_CANONNAME =1 , +RESOLVE_FAIL_QUIETLY =2 +}; +extern int dns_resolve (const char *host,const char *port,int flags, +resolver_result *res); +/* result is in static buffer */ +extern const char *dns_name (const resolved_address *i); +/* result is in malloc'ed buffer */ +extern char *dns_ip_address (const resolved_address *i, +const resolver_result *ai); + +#define for_each_address (i, ai) \ + for (i = ai; i; i = (i)->ai_next) + +#define dns_family (i, ai) ((i)->ai_family) +#define dns_socktype (i, ai) ((i)->ai_socktype) +#define dns_protocol (i, ai) ((i)->ai_protocol) +#define dns_addr (i, ai) ((i)->ai_addr) +#define dns_addrlen (i, ai) ((i)->ai_addrlen) +#define dns_canonname (i, ai) ((i)->ai_canonname) + +#define dns_free (ai) freeaddrinfo(ai) + +#endif
tcp.c +31 -151
236 lines not on this page · raw message
Signature · 1 line
1.7.9.2
Re: [PATCH 4/5] tcp: unify ipv4 and ipv6 code paths
Quoted 36 lines · Jonathan Nieder
Date: Mon, 6 Jun 2011 04:41:28 -0500
The new DNS API abstracts away differences between the gethostbyname- and getaddrinfo-centric interfaces for looking up a host, making the code to use them in
connect.ca little easier to read.To make a lookup:
resolver_result ai;
dns_resolve(host, port, 0, &ai);
...
dns_free(ai);To iterate over responses:
resolved_address i;
for_each_address(i, ai) {
...
}In the !NO_IPV6 codepath, the
git_locate_hostfunction that is used to find the canonical IP and hostname for a git server's public address (for virtual hosting) tells getaddrinfo to restrict attention to TCP services after this patch. That should make no difference because the service parameter is NULL.No functional change intended.
Signed-off-by Jonathan Nieder <[email protected]>---
This is the title feature, corresponding to
http://thread.gmane.org/gmane.comp.version-control.git/175106/focus=175111It needed changes to adjust to released changes in the code it touches, but nothing is fundamentally different from v1.
Quoted 7 lines
Makefile | 5 ++
dns-ipv4.c | 33 +++++++++++ dns-ipv4.h | 69 +++++++++++++++++++++++ dns-ipv6.c | 49 ++++++++++++++++ dns-ipv6.h | 31 +++++++++++ tcp.c | 182 +++++++++++------------------------------------------------- 6 files changed, 218 insertions(+), 151 deletions(-)
I'm not entirely sure I understand the motivation here. We already had well-tested, implementations of IPv4 and IPv6 tcp-socket setup. Here you unify the code by adding abstraction, but it ends up amounting to more lines of code, with the details scattered around in different source files.
For me, this means that I have to learn a new API, and to see what really happens when something goes wrong, I have to jump between multiple source files.
And I'm not entirely sure what this patch actually improves. If it was likely that we'd get support for yet another IP-stack version, then this would probably be a win. But that's not likely, is it?
Re: [PATCH 4/5] tcp: unify ipv4 and ipv6 code paths
Quoted 5 lines · Erik Faye-Lund
I'm not entirely sure I understand the motivation here. We already had well-tested, implementations of IPv4 and IPv6 tcp-socket setup. Here you unify the code by adding abstraction, but it ends up amounting to more lines of code, with the details scattered around in different source files.
Thanks. It's true: I didn't convey the motivation well at all.
Before this patch, git_tcp_connect_sock() looks roughly like this:
#ifndef NO_IPV6
static int git_tcp_connect_sock(char *host, int flags)
{
get_host_and_port(&host, &port);
memcpy(&hints, tcp_stream_hints, sizeof(hints));
gai = getaddrinfo(host, port, &hints, &ai);
for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
... socket(), connect(), etc ...
}
freeaddrinfo(ai0);
enable_keepalive(sockfd);
return sockfd;
}
#else
static int git_tcp_connect_sock(char *host, int flags)
{
get_host_and_port(&host, &port);
he = gethostbyname(host);
for (ap = he->h_addr_list; *ap; ap++, cnt++) {
... socket(), connect(), etc ...
}
enable_keepalive(sockfd);
return sockfd;
}
#endifAny change to this procedure has to be made in both places, and in the past, that has caused some minor bugs that were unnoticed for a while, but no big deal --- it's not that complicated, so the code duplication is tolerable.
After this patch and the next two, it looks like so:
static int git_tcp_connect_sock(char *host, int flags)
{
get_host_and_port(&host, &port);
gai = dns_resolve(host, port, 0, &ai);
for_each_address(i, ai) {
... socket(), connect(), etc ...
}
dns_free(ai0);
enable_keepalive(sockfd);
return sockfd;
}That is, it is almost identical to the getaddrinfo version.
So it is not about making the code shorter, but about not repeating ourselves.
I did this for my sanity while implementing the SRV code: if my changes work with the gethostbyname API, this way it is much more likely this way that they will still work with getaddrinfo, too. So basically, the point is that I did not want to have to think about the !NO_IPV6 codepath while writing patches (and others might not want to have to think about the NO_IPV6 codepath at all --- the same advantages apply for them, too).
API dictionary in the !NO_IPV6 case (see dns-ipv6.h and dns-ipv6.c for details):
resolver_result = struct addrinfo *
resolved_address = const struct addrinfo *
dns_resolve = getaddrinfo
dns_name = getnameinfo, use in error messages dns_ip_address = getnameinfo, git-daemon uses for %IP substitution for_each_address = for (ai0 = ai; ai; ai = ai->ai_next)
dns_family = ->ai_family
dns_socktype = ->ai_socktype
etc
dns_strerror = gai_strerror
dns_free = freeaddrinfo
The dns_name()/dns_ip_address() pair is a little silly. The former returns its result in a static buffer and the latter uses malloc. Improvements welcome.
Of course, git_tcp_connect_sock() is not the only function that uses host resolution facilities. The benefit scales as more functions get converted.
Hope that helps,
Jonathan
PATCH 5/5 daemon: check for errors retrieving IP address
In reply to Jonathan NiederDate: Fri, 17 Jun 2011 05:26:05 -0500
To retrieve a canonical IP address for possible use in a --interpolated-path=%IP string, the git daemon calls inet_ntop to fill a buffer of size HOST_NAME_MAX+1, ignoring errors, and copies the result. If the address has length > HOST_NAME_MAX (which was as low as 8 in ancient times) then inet_ntop could error out, leading the git daemon to copy uninitialized data.
This probably never happens in practice because modern systems allow such long hostnames as 64 chars (POSIX requires at least 255) or have no compile-time maximum at all and let git fall back to a buffer size of 256. But it is more comforting not to rely on that. So:
- in "ipv4" code, which uses inet_ntop, use a buffer size of 64 (long enough to hold an ipv6 address if needed).
- in ipv6 code, use the "getnameinfo" function that is already being used to convert addresses to text in error messages. It uses a buffer of length NI_MAXHOST.
- check for errors and make dns_ip_address() and git_locate_host() return NULL when they occur. strbuf_expand_dict_cb treats NULL as an empty substitution string.
As a nice side effect, the fallback definition of HOST_NAME_MAX for platforms that don't define it is no longer needed.
---
This patch allows getnameinfo and inet_ntop to use a larger buffer or fail. (I was mostly nervous because HOST_NAME_MAX is not guaranteed to be large enough to hold an IP address.)
That's the end of the series. Thanks for reading.
dns-ipv4.h | 11 +++++------ dns-ipv6.c | 16 ++++++++-------- tcp.c | 6 ++++-- 3 files changed, 17 insertions(+), 16 deletions(-)
dns-ipv4.h +5 -6
@@ -1,9 +1,7 @@ #ifndef DNS_IPV4_H #define DNS_IPV4_H -#ifndef HOST_NAME_MAX -#define HOST_NAME_MAX 256 -#endif +#define ADDRBUFLEN 64 /* 46 for an ipv6 address, plus a little extra */ struct ipv4_address { char **ap;
@@ -33,9 +31,10 @@ static inline const char *dns_name(const resolved_address *addr)static inline char *dns_ip_address (const resolved_address *addr,const resolver_result *ai) { -char addrbuf[HOST_NAME_MAX +1 ]; -inet_ntop (ai->he->h_addrtype, &addr->sa.sin_addr, - addrbuf,sizeof (addrbuf)); +char addrbuf[ADDRBUFLEN ]; +if (!inet_ntop (ai->he->h_addrtype, &addr->sa.sin_addr, + addrbuf,sizeof (addrbuf))) +return NULL ;return xstrdup (addrbuf); }
dns-ipv6.c +8 -8
@@ -1,8 +1,9 @@#include "cache.h" #include "dns-ipv6.h" -#ifndef HOST_NAME_MAX -#define HOST_NAME_MAX 256 +/* from RFC 2553 */ +#ifndef NI_MAXHOST +#define NI_MAXHOST 1025#endif const char *dns_name (const resolved_address *i)
@@ -19,12 +20,11 @@ const char *dns_name(const resolved_address *i)char *dns_ip_address (const resolved_address *i,const resolver_result *ai0) {const struct addrinfo *ai = *i; -char addrbuf[HOST_NAME_MAX +1 ]; -struct sockaddr_in *sin_addr; - - sin_addr = (void *)ai->ai_addr; -inet_ntop (AF_INET , &sin_addr->sin_addr, addrbuf,sizeof (addrbuf)); -return xstrdup (addrbuf); +char addr[NI_MAXHOST ]; +if (getnameinfo (ai->ai_addr, ai->ai_addrlen, addr,sizeof (addr), +NULL ,0 ,NI_NUMERICHOST )) +return NULL ; +return xstrdup (addr); } int dns_resolve(const char *host, const char *port, int flags,
tcp.c +4 -2
@@ -56,8 +56,10 @@ void git_locate_host(const char *hostname, char **ip_address, *ip_address =dns_ip_address (&i, &ai);free (*canon_hostname); - *canon_hostname =xstrdup (dns_canonname (i, ai) ? -dns_canonname (i, ai) : *ip_address); + *canon_hostname = +dns_canonname (i, ai) ?xstrdup (dns_canonname (i, ai)) : + *ip_address ?xstrdup (*ip_address) : +NULL ;break ; }
Signature · 1 line
1.7.9.2
[PATCH 6/5] tcp: make dns_resolve() return an error code
In reply to Jonathan NiederDate: Thu, 23 Jun 2011 05:23:57 -0500
getaddrinfo() and gethostbyname() use error codes to describe their errors. Pass that error code back to the caller when dns_resolve() fails and the RESOLVE_FAIL_QUIETLY flag is set, so callers can save the diagnosis and print it later:
int saved_rerrno = dns_resolve(...);
...
if (saved_rerrno)
die("resolver failed: %s", dns_strerror(saved_rerrno));In the ipv4 codepath, we assume that h_errno is never 0 on error. POSIX.1-2004 does not specify whether 0 is a valid value for h_errno, but luckily common practice is for h_errno to be a strictly positive integer (HOST_NOT_FOUND = 1, NO_DATA = 2, NO_RECOVERY = 3, or TRY_AGAIN = 4). If gethostbyname errors out with h_errno == 0 on some platform, just let git die with a message indicating a BUG so the bad assumption can be corrected.
---
This makes saving up errors from a host resolution failure as in v1.7.7-rc0~40^2 (connect: only log if all attempts failed, 2011-08-01) possible. By the way, this series is currently against "maint" for no particular reason and presumably it would conflict with that patch. ;-)
I'll be happy to rebase against "master" some time soon.
dns-ipv4.c | 7 +++++-- dns-ipv4.h | 5 +++++ dns-ipv6.c | 2 +- dns-ipv6.h | 1 + 4 files changed, 12 insertions(+), 3 deletions(-)
dns-ipv4.c +5 -2
@@ -9,8 +9,11 @@ int dns_resolve(const char *host, const char *port, int flags,unsigned int nport; he =gethostbyname (host); -if (!he && (flags &RESOLVE_FAIL_QUIETLY )) -return -1 ; +if (!he && (flags &RESOLVE_FAIL_QUIETLY )) { +if (!h_errno) +die ("BUG: gethostbyname failed but h_errno == 0" ); +return h_errno; + }if (!he)die ("Unable to look up %s (%s)" , host,hstrerror (h_errno));
dns-ipv4.h +5 -0
@@ -18,6 +18,10 @@ typedef struct ipv4_address resolved_address;enum {RESOLVE_CANONNAME =1 , +/* +* Quietly return an error code instead of exiting on error. +* Callers can use dns_strerror() to get an error string. +*/ RESOLVE_FAIL_QUIETLY =2 }; extern int dns_resolve(const char *host, const char *port, int flags,
@@ -63,6 +67,7 @@ static inline int dns_fill_sockaddr_(char *ap,#define dns_addrlen (addr, ai) sizeof((addr).sa)#define dns_canonname (addr, ai) ((ai).he->h_name) +#define dns_strerror (n) hstrerror(n) #define dns_free(ai) do { /* nothing */ } while (0)#endif
dns-ipv6.c +1 -1
@@ -41,7 +41,7 @@ int dns_resolve(const char *host, const char *port, int flags, gai =getaddrinfo (host, port, &hints, res);if (gai && (flags &RESOLVE_FAIL_QUIETLY )) -return -1 ; +return gai;if (gai)die ("Unable to look up %s (port %s) (%s)" , host, port,gai_strerror (gai));
dns-ipv6.h +1 -0
@@ -26,6 +26,7 @@ extern char *dns_ip_address(const resolved_address *i,#define dns_addrlen (i, ai) ((i)->ai_addrlen)#define dns_canonname (i, ai) ((i)->ai_canonname) +#define dns_strerror (gai) gai_strerror(gai)#define dns_free (ai) freeaddrinfo(ai)#endif
Signature · 1 line
1.7.9.2
[PATCH 7/5] transport: optionally honor DNS SRV records
In reply to Jonathan NiederDate: Mon, 6 Jun 2011 04:46:20 -0500
SRV records are an extra layer of indirection on top of A/AAAA records mapping from domain names and service types to the hostnames of machines that offer that service. That has a couple of nice effects:
- a single domain can use different hosts for different services
- a single domain can use multiple hosts for one service
Teach git to perform a SRV lookup whenever resolving a git:// URL. This means:
- if your git server was previously the same machine as your wesnoth server, you can move it to a separate machine without forcing everyone to update their links to the old URLs
- if you have a primary git server and a backup machine that should be used when the primary server goes down, the client can automatically take care of it
- if you have multiple git servers and would like to spread load between them, the client can automatically take care of it
That is, SRV records let us ask the client to carry out various tasks that would require a proxy on the server side with traditional DNS. The client performs a SRV query to _git._tcp.<domain name> to receive its instructions. RFC 2782 has details.
Ideally reaping these benefits would just involve passing a special flag to getaddrinfo(). Since we don't live in such a world, this patch uses the BIND 8 API provided by libresolv to parse the response for ourselves.
RFC 2782 requires some non-determinism in the order of hosts contacted; this patch uses drand48() for that. To avoid causing trouble for platforms that lack the libbind ns_* functions or drand48(), the SRV support is only provided when requested by setting the USE_SRV_RR compile-time option.
git servers must ensure that they can also be reached by a plain A/AAAA lookup to support git clients without SRV support, for example by proxying connections to an appropriate server:
# in inetd.conf git stream tcp nowait.400 nobody /usr/sbin/tcpd \ /bin/nc -q0 gitserver.example.com git
Regression: this uglifies error messages for connection errors a little. It would probably be better to leave out the connection count when we are not trying more than one server.
Based on a patch by Julien Cristau <[email protected]>.
---
As I mentioned at the gittogether, I think this would be a valuable feature in git. You can test it out (though this is only the simple case, no load balancing or failover) by doing
git clone git://git.debian.org/~jrnieder-guest/git.git
and watching what happens with wireshark.
The patch needs documentation. Maybe a howto and an addendum to the protocol docs would do. Anyway, I hope it's at least entertaining in the current state.
Makefile | 10 ++ srv.c | 321 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ srv.h | 15 +++ tcp.c | 100 ++++++++++++++------ 4 files changed, 418 insertions(+), 28 deletions(-) create mode 100644 srv.c create mode 100644 srv.h
Makefile +10 -0
@@ -148,6 +148,10 @@ all:: # Notably on Solaris hstrerror resides in libresolv and on Solaris 7 # inet_ntop and inet_pton additionally reside there. # +# Define USE_SRV_RR if you want git to pay attention to SRV resource records +# when looking up servers to contact over git protocol. This implies +# NEEDS_RESOLV. +# # Define NO_MMAP if you want to avoid mmap. # # Define NO_PTHREADS if you do not have or do not want to use Pthreads.
@@ -1490,6 +1494,11 @@ endif ifdef NEEDS_NSL EXTLIBS += -lnsl endif +ifdef USE_SRV_RR + BASIC_CFLAGS += -DUSE_SRV_RR + LIB_OBJS += srv.o + NEEDS_RESOLV = YesPlease +endif ifdef NEEDS_RESOLV EXTLIBS += -lresolv endif
@@ -2155,6 +2164,7 @@ builtin/commit.o builtin/revert.o wt-status.o: wt-status.h builtin/tar-tree.o archive-tar.o: tar.h connect.o transport.o url.o http-backend.o: url.h connect.o daemon.o tcp.o: tcp.h +tcp.o srv.o: srv.h http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h
srv.c new file +321 -0
321 lines not on this page · raw message
srv.h new file +15 -0
@@ -0,0 +1,15 @@ +#ifndef SRV_H +#define SRV_H + +struct host { +char *hostname; +char *port; +}; + +#ifndef USE_SRV_RR +#define get_srv (host, hosts) 0 +#else +extern int get_srv (const char *host,struct host **hosts); +#endif + +#endif
tcp.c +72 -28
131 lines not on this page · raw message
Signature · 1 line
1.7.9.2
Re: [PATCH 7/5] transport: optionally honor DNS SRV records
Quoted 59 lines · Jonathan Nieder
Date: Mon, 6 Jun 2011 04:46:20 -0500
SRV records are an extra layer of indirection on top of A/AAAA records mapping from domain names and service types to the hostnames of machines that offer that service. That has a couple of nice effects:
- a single domain can use different hosts for different services
- a single domain can use multiple hosts for one serviceTeach git to perform a SRV lookup whenever resolving a
git://URL. This means:- if your git server was previously the same machine as your wesnoth server, you can move it to a separate machine without forcing everyone to update their links to the old URLs
- if you have a primary git server and a backup machine that should be used when the primary server goes down, the client can automatically take care of it
- if you have multiple git servers and would like to spread load between them, the client can automatically take care of it
That is, SRV records let us ask the client to carry out various tasks that would require a proxy on the server side with traditional DNS. The client performs a SRV query to _git._tcp.<domain name> to receive its instructions. RFC 2782 has details.
Ideally reaping these benefits would just involve passing a special flag to
getaddrinfo(). Since we don't live in such a world, this patch uses the BIND 8 API provided by libresolv to parse the response for ourselves.RFC 2782 requires some non-determinism in the order of hosts contacted; this patch uses
drand48()for that. To avoid causing trouble for platforms that lack the libbind ns_* functions ordrand48(), the SRV support is only provided when requested by setting theUSE_SRV_RRcompile-time option.git servers must ensure that they can also be reached by a plain A/AAAA lookup to support git clients without SRV support, for example by proxying connections to an appropriate server:
# in inetd.conf git stream tcp nowait.400 nobody /usr/sbin/tcpd \ /bin/nc -q0 gitserver.example.com gitRegression: this uglifies error messages for connection errors a little. It would probably be better to leave out the connection count when we are not trying more than one server.
Based on a patch by Julien Cristau <[email protected]>.
Signed-off-by Jonathan Nieder <[email protected]>---
As I mentioned at the gittogether, I think this would be a valuable feature in git. You can test it out (though this is only the simple case, no load balancing or failover) by doing
Quoted 7 lines
git clone
git://git.debian.org/~jrnieder-guest/git.gitand watching what happens with wireshark.
The patch needs documentation. Maybe a howto and an addendum to the protocol docs would do. Anyway, I hope it's at least entertaining in the current state.
It's an interesting feature, but I'm a little bit worried if this promotes non-portable setups; won't these repos be unreachable (at least without manually redirecting or also keeping a copy on the advertised URL) on machines where libresolv is unavailable? I'm mainly thinking about the "a single domain can use different hosts for different services"-benefit you mentioned. Multiple hosts for one service would probably be done by simply advertising one of the URLs, and get some load-balancing from the clients that DOES have libresolv...
Re: [PATCH 7/5] transport: optionally honor DNS SRV records
Quoted 6 lines · Erik Faye-Lund
It's an interesting feature, but I'm a little bit worried if this promotes non-portable setups; won't these repos be unreachable (at least without manually redirecting or also keeping a copy on the advertised URL) on machines where libresolv is unavailable? I'm mainly thinking about the "a single domain can use different hosts for different services"-benefit you mentioned.
Yes. I think of redirecting with SRV records without a fallback as a misconfiguration ("don't do that, then"). Note that it would not only affect people without drand48 but people with broken DNS servers.
In the case of Alioth, what they do is let inetd listen on git.debian.org = vasks.debian.org, using netcat to forward connections to wagner.debian.org. So clients with and without SRV support end up finding their bits shuttled to the same place, with the only difference being a little wasted load on vasks in the no-SRV case.
This means the Alioth admins don't get the benefit of not having to set up a proxy, but they do get the benefit of clients taking on more of the work and the service continuing to be available for some clients if vasks goes down.
Maybe adding an envvar to disable the SRV handling would make it easier for server admins to check the fallback. Can we do more?
(By the way, what platforms don't support BIND and a random number generator?)
Thanks,
Jonathan
Re: [PATCH 7/5] transport: optionally honor DNS SRV records
Am 3/8/2012 22:35, schrieb Jonathan Nieder:
Quoted 2 lines
(By the way, what platforms don't support BIND and a random number generator?)
MinGW, for example:
D:\Src\mingw-git>cat foo.c && gcc -c foo.c
#include <arpa/nameser.h>
#include <resolv.h>
#include <stdlib.h>
void (*x)(long int) = srand48;
foo.c:1:26: error: arpa/nameser.h: No such file or directory foo.c:2:20: error: resolv.h: No such file or directory foo.c:5: error: 'srand48' undeclared here (not in a function)
-- Hannes
Re: [PATCH 7/5] transport: optionally honor DNS SRV records
Hi,
Quoted 6 lines · Johannes Sixt
Am 3/8/2012 22:35, schrieb Jonathan Nieder:
Quoted 2 lines
(By the way, what platforms don't support BIND and a random number generator?)
MinGW, for example:
The ISC seems to provide BIND source code[*] and binaries for Windows, though I'm not sure how well they could fit into a typical development environment for git's MinGW port. A good approach for drand48 is harder to imagine, mostly because there are too many choices for a free PRNG to use to replace it. Not a bad problem to have. ;-)
Though all of that would only come up once someone wants to use features requiring these facilities on Windows. Thanks for the hints and sorry for the distraction.
Ciao,
Jonathan
[PATCH 8/5] srv: tolerate broken DNS replies
In reply to Jonathan NiederAt a hotel with a very broken Wi-Fi setup, Richard found his copy of git unable to cope:
% git clone git://git.kitenet.net/mr
Cloning into 'mr'...
error: cannot initialize DNS parser: Message too long
fatal: Unable to look up git.kitenet.net
Other programs gave some warnings but otherwise worked fine. From a packet capture, it seems that the response to a SRV query for _git._tcp.git.kitenet.net in this setup was a single A resource record pointing to the link-local address 169.254.1.1, followed by two trailing bytes: c0 1a. The trailing bytes cause the underlying parser to fail.
It would not be good to silently tolerate this and similar kinds of brokenness, but working around it would help people on affected systems to recover. Luckily RFC2782 gives us enough leeway to act as we please for this particular kind of error, so give a warning and fall back to an A/AAAA query (which should work).
Similarly, if we receive non-SRV RRs in response to a SRV query, RFC2782 does not say to error out, so in the spirit of graceful degradation let's warn and skip those records.
---
Thanks for reading. That's the end of the series.
Good night,
Jonathan
srv.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-)
srv.c +26 -14
@@ -83,7 +83,6 @@ static int srv_parse(ns_msg *msg, struct parsed_srv_rr **res) {struct parsed_srv_rr *rrs =NULL ;int nr_parsed =0 ; -int cnames =0 ;int i, n; n =ns_msg_count (*msg, ns_s_an);
@@ -98,30 +97,33 @@ static int srv_parse(ns_msg *msg, struct parsed_srv_rr **res)if (ns_rr_type (rr) != ns_t_cname)break ; } - cnames = i; - n -= cnames; - rrs =xmalloc (n *sizeof (*rrs)); -for (i =0 ; i < n; i++) { + rrs =xmalloc ((n - i) *sizeof (*rrs)); +for (; i < n; i++) {ns_rr rr; -if (ns_parserr (msg, ns_s_an, cnames + i, &rr)) { +if (ns_parserr (msg, ns_s_an, i, &rr)) {error ("cannot parse DNS RR: %s" ,strerror (errno)); goto fail; }if (ns_rr_type (rr) != ns_t_srv) { -error ("expected SRV RR, found RR type %d" , +/* +* Maybe the server is playing tricks and returned +* an A record. Let it pass and if we don't get +* any SRV RRs, we can fall back to an A lookup. +*/ +warning ("expected SRV RR, found RR type %d" , (int )ns_rr_type (rr)); - goto fail; +continue ; } -if (srv_parse_rr (msg, &rr, rrs + i)) +if (srv_parse_rr (msg, &rr, rrs + nr_parsed))/* srv_parse_rr writes a message */ goto fail; nr_parsed++; } *res = rrs; -return n; +return nr_parsed; fail:for (i =0 ; i < nr_parsed; i++)free (rrs[i].target);
@@ -274,13 +276,23 @@ int get_srv(const char *host, struct host **hosts)if (len <0 ) goto out; +/* +* If the reply to a SRV query is malformed, fall back to an +* A query. +* +* The RFC2782 usage rules don't say anything about this, but +* in practice, it seems that some firewalls or DNS servers +* (think: captive portal) handle A queries sensibly and +* provide malformed replies in response to SRV queries. +*/ +if (ns_initparse (buf, len, &msg)) { +warning ("cannot parse SRV response: %s" ,strerror (errno)); + goto out; + } +/* If a SRV RR cannot be parsed, give up. */ ret =-1 ; -if (ns_initparse (buf, len, &msg)) { -error ("cannot initialize DNS parser: %s" ,strerror (errno)); - goto out; - } n =srv_parse (&msg, &rrs);if (n <0 )/* srv_parse writes a message */
Signature · 1 line
1.7.9.2
Re: [PATCH 8/5] srv: tolerate broken DNS replies
Quoted 1 line · Jonathan Nieder
Other programs gave some warnings but otherwise worked fine.
Just ftr, Chrome/Chromium ignores this quickly and loads pages while Konqueror takes ages to get around this. Not related to git, but maybe an interesting data point in the future. If not, disregard.
Thanks for hunting and killing this one.
Richard
Quoted 15 lines · Jonathan Nieder
Hi,
These patches eliminate some ifdef-ery concerning
NO_IPV6. I used them when writing the SRV patch, which applies on top, but it's probably best to think of it as an independent topic.Patch 4 is the heart of the series. It provides an interface similar to getaddrinfo that can be implemented on top of either gethostbyname or getaddrinfo and puts each implementation in a separate file. This way, callers can just use the common API and they do not need to have two copies of their code, one for each host resolution API.
Patches 1-3 move code around until all the code that patch 4 touches is in one place.
Quoted 8 lines
Patches 5 is a potential error handling improvement noticed while writing patches 1-4. It's probably not actually needed but it was a comfort to me.
These patches have been in use in Debian since June of last year. I'd like to see this in mainline early in the 1.7.11 cycle to make coding that touches this area during that cycle more pleasant. Thoughts of all kinds welcome.
What happened to this series?
Quoted 20 lines · Erik Faye-Lund
Quoted 19 lines · Jonathan Nieder
Hi,
These patches eliminate some ifdef-ery concerning
NO_IPV6. I used them when writing the SRV patch, which applies on top, but it's probably best to think of it as an independent topic.Patch 4 is the heart of the series. It provides an interface similar to getaddrinfo that can be implemented on top of either gethostbyname or getaddrinfo and puts each implementation in a separate file. This way, callers can just use the common API and they do not need to have two copies of their code, one for each host resolution API.
Patches 1-3 move code around until all the code that patch 4 touches is in one place.
Patches 5 is a potential error handling improvement noticed while writing patches 1-4. It's probably not actually needed but it was a comfort to me.
Quoted 6 lines
Quoted 4 lines
These patches have been in use in Debian since June of last year. I'd like to see this in mainline early in the 1.7.11 cycle to make coding that touches this area during that cycle more pleasant. Thoughts of all kinds welcome.
What happened to this series?
Yeah, what happened to it? I was looking at the diff files Ubuntu applies when generating its recent .deb files, and noticed these patches today, and was wondering what was going on.
Quoted 7 lines · Erik Faye-Lund
Quoted 5 lines · Jonathan Nieder
Patch 4 is the heart of the series. It provides an interface similar to getaddrinfo that can be implemented on top of either gethostbyname or getaddrinfo and puts each implementation in a separate file. This way, callers can just use the common API and they do not need to have two copies of their code, one for each host resolution API.
[...]
Quoted 1 line
What happened to this series?
It seemed nice in principle, but if I understood correctly then the API was too complicated for anyone but me to like it. Instead of an interface similar to getaddrinfo, it would probably be better to implement an interface _identical_ to getaddrinfo for the the subset of functionality that git uses.
See [1] for example for proof that that's possible.
I would make some different choices from Russ if doing it myself --- e.g., I would want to unconditionally use
#define getaddrinfo git_getaddrinfo
when NO_IPV6 is set so testing these code paths would be as simple as enabling NO_IPV6, even on systems that also provide the modern API in libc.
Hoping that clarifies,
Jonathan
[1] http://git.eyrie.org/?p=devel/rra-c-util.git;a=tree;f=portable