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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
From aea828f4cb26d3ddc4ff49308e8b33c09effc66f Mon Sep 17 00:00:00 2001
From: Jonathan Nieder <jrnieder@gmail.com>
Date: Mon, 6 Jun 2011 04:41:28 -0500
Subject: [PATCH 08/12] tcp: unify ipv4 and ipv6 code paths
In order to support old ipv4- and current mixed ipv4/ipv6 systems, git
code that makes use of DNS facilities currently involves implementing
each DNS-heavy block of code twice, once using the gethostbyname() API
and again using getaddrinfo(), choosing between the two
implementations with "#ifdef NO_IPV6".
The result is that we maintain two variants that are supposed to
accomplish the same thing of certain code paths:
- connection setup in core git (git_tcp_connect_sock)
- connection setup in git daemon
- %IP and %CH substitution in git daemon
- connection setup in git imap-send
Any change to these procedures has to be made in both places, which
makes enhancements and cleanups there more painful than necessary.
Th new DNS API is meant to solve that by providing a thin abstraction
on top of the usual DNS resolution interfaces that both
gethostbyname()- and getaddrinfo()-centric interfaces and can easily
implement. Using this API means you do not have to implement your
higher-level logic twice just because it uses DNS.
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
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
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) {
...
}
Two functions in the API are a little strange and probably need more
work: dns_name and dns_ip_address both return the IP address
corresponding to a resolved address. The former returns its result
in a static buffer and is meant for use in error messages, while the
latter returns its result in a malloc()ed buffer and is meant to
support virtual hosting.
This patch introduces the DNS API and translates git_locate_host() and
git_tcp_connect_sock() to use it as an example. No functional change
intended.
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.
The NO_IPV6 codepath assumes 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. If gethostbyname() errors out with h_errno == 0 on some
platform, die with a message indicating a BUG so the bad assumption
can be corrected.
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Improved-by: Erik Faye-Lund <kusmabite@gmail.com>
---
Makefile | 5 ++
dns-ipv4.c | 36 ++++++++++++
dns-ipv4.h | 74 ++++++++++++++++++++++++
dns-ipv6.c | 49 ++++++++++++++++
dns-ipv6.h | 32 +++++++++++
tcp.c | 182 +++++++++++-------------------------------------------------
6 files changed, 227 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
diff --git a/Makefile b/Makefile
index d321ad4..ea44b91 100644
--- a/Makefile
+++ b/Makefile
@@ -1618,6 +1618,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
diff --git a/dns-ipv4.c b/dns-ipv4.c
new file mode 100644
index 0000000..8820c9e
--- /dev/null
+++ b/dns-ipv4.c
@@ -0,0 +1,36 @@
+#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)) {
+ 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));
+
+ 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;
+}
diff --git a/dns-ipv4.h b/dns-ipv4.h
new file mode 100644
index 0000000..c63558b
--- /dev/null
+++ b/dns-ipv4.h
@@ -0,0 +1,74 @@
+#ifndef DNS_IPV4_H
+#define DNS_IPV4_H
+
+#ifndef HOST_NAME_MAX
+#define HOST_NAME_MAX 256
+#endif
+
+struct ipv4_address {
+ char **ap;
+ struct sockaddr_in sa;
+};
+
+struct ipv4_addrinfo {
+ struct hostent *he;
+ unsigned int port;
+};
+
+typedef struct ipv4_addrinfo resolver_result;
+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,
+ resolver_result *res);
+
+static inline const char *dns_name(const resolved_address *addr)
+{
+ return inet_ntoa(*(struct in_addr *)&addr->sa.sin_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));
+ return xstrdup(addrbuf);
+}
+
+static inline int dns_fill_sockaddr_(char *ap,
+ const struct ipv4_addrinfo *ai, struct sockaddr_in *sa)
+{
+ if (!ap) /* done. */
+ return -1;
+
+ memset(sa, 0, sizeof(*sa));
+ sa->sin_family = ai->he->h_addrtype;
+ sa->sin_port = htons(ai->port);
+ memcpy(&sa->sin_addr, ap, ai->he->h_length);
+ return 0;
+}
+
+#define for_each_address(addr, ai) \
+ for ((addr).ap = (ai).he->h_addr_list; \
+ !dns_fill_sockaddr_(*(addr).ap, &(ai), &(addr).sa); \
+ (addr).ap++)
+
+#define dns_family(addr, ai) ((ai).he->h_addrtype)
+#define dns_socktype(addr, ai) SOCK_STREAM
+#define dns_protocol(addr, ai) 0
+#define dns_addr(addr, ai) ((struct sockaddr *) &(addr).sa)
+#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
diff --git a/dns-ipv6.c b/dns-ipv6.c
new file mode 100644
index 0000000..2129136
--- /dev/null
+++ b/dns-ipv6.c
@@ -0,0 +1,49 @@
+#include "cache.h"
+#include "dns-ipv6.h"
+
+#ifndef HOST_NAME_MAX
+#define HOST_NAME_MAX 256
+#endif
+
+const char *dns_name(const resolved_address *i)
+{
+ const struct addrinfo *ai = *i;
+ static char addr[NI_MAXHOST];
+ if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
+ NI_NUMERICHOST) != 0)
+ strcpy(addr, "(unknown)");
+
+ return addr;
+}
+
+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);
+}
+
+int dns_resolve(const char *host, const char *port, int flags,
+ resolver_result *res)
+{
+ struct addrinfo hints;
+ int gai;
+
+ memset(&hints, 0, sizeof(hints));
+ if (flags & RESOLVE_CANONNAME)
+ hints.ai_flags = AI_CANONNAME;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+
+ gai = getaddrinfo(host, port, &hints, res);
+ if (gai && (flags & RESOLVE_FAIL_QUIETLY))
+ return gai;
+ if (gai)
+ die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
+
+ return 0;
+}
diff --git a/dns-ipv6.h b/dns-ipv6.h
new file mode 100644
index 0000000..4211c9e
--- /dev/null
+++ b/dns-ipv6.h
@@ -0,0 +1,32 @@
+#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_strerror(gai) gai_strerror(gai)
+#define dns_free(ai) freeaddrinfo(ai)
+
+#endif
diff --git a/tcp.c b/tcp.c
index 9263e0d..4239daf 100644
--- a/tcp.c
+++ b/tcp.c
@@ -1,8 +1,10 @@
#include "cache.h"
#include "run-command.h"
-#ifndef HOST_NAME_MAX
-#define HOST_NAME_MAX 256
+#ifndef NO_IPV6
+#include "dns-ipv6.h"
+#else
+#include "dns-ipv4.h"
#endif
#define STR_(s) # s
@@ -39,44 +41,27 @@ static void enable_keepalive(int sockfd)
strerror(errno));
}
-#ifndef NO_IPV6
-
-static const char *ai_name(const struct addrinfo *ai)
-{
- static char addr[NI_MAXHOST];
- if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
- NI_NUMERICHOST) != 0)
- strcpy(addr, "(unknown)");
-
- return addr;
-}
-
void git_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;
+ resolver_result ai;
+ resolved_address i;
- gai = getaddrinfo(hostname, NULL, &hints, &ai);
- if (gai)
+ if (dns_resolve(hostname, NULL,
+ RESOLVE_CANONNAME | RESOLVE_FAIL_QUIETLY, &ai))
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);
+ for_each_address(i, ai) {
+ free(*ip_address);
+ *ip_address = dns_ip_address(&i, &ai);
- free(*canon_hostname);
- *canon_hostname = xstrdup(ai->ai_canonname ?
- ai->ai_canonname : *ip_address);
+ free(*canon_hostname);
+ *canon_hostname = xstrdup(dns_canonname(i, ai) ?
+ dns_canonname(i, ai) : *ip_address);
+ break;
+ }
- freeaddrinfo(ai);
+ dns_free(ai);
}
/*
@@ -87,46 +72,42 @@ static int git_tcp_connect_sock(char *host, int flags)
struct strbuf error_message = STRBUF_INIT;
int sockfd = -1;
const char *port = STR(DEFAULT_GIT_PORT);
- struct addrinfo hints, *ai0, *ai;
- int gai;
- int cnt = 0;
+ resolver_result ai;
+ resolved_address i;
+ int cnt = -1;
get_host_and_port(&host, &port);
if (!*port)
port = "<none>";
- memset(&hints, 0, sizeof(hints));
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
-
if (flags & CONNECT_VERBOSE)
fprintf(stderr, "Looking up %s ... ", host);
- gai = getaddrinfo(host, port, &hints, &ai);
- if (gai)
- die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
+ if (dns_resolve(host, port, 0, &ai))
+ die("BUG: dns_resolve returned error?");
if (flags & CONNECT_VERBOSE)
fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
- for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
- sockfd = socket(ai->ai_family,
- ai->ai_socktype, ai->ai_protocol);
- if ((sockfd < 0) ||
- (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
+ for_each_address(i, ai) {
+ cnt++;
+ sockfd = socket(dns_family(i, ai),
+ dns_socktype(i, ai), dns_protocol(i, ai));
+ if (sockfd < 0 ||
+ connect(sockfd, dns_addr(i, ai), dns_addrlen(i, ai)) < 0) {
strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
- host, cnt, ai_name(ai), strerror(errno));
+ host, cnt, dns_name(&i), strerror(errno));
if (0 <= sockfd)
close(sockfd);
sockfd = -1;
continue;
}
if (flags & CONNECT_VERBOSE)
- fprintf(stderr, "%s ", ai_name(ai));
+ fprintf(stderr, "%s ", dns_name(&i));
break;
}
- freeaddrinfo(ai0);
+ dns_free(ai);
if (sockfd < 0)
die("unable to connect to %s:\n%s", host, error_message.buf);
@@ -141,107 +122,6 @@ static int git_tcp_connect_sock(char *host, int flags)
return sockfd;
}
-#else /* NO_IPV6 */
-
-void git_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);
-}
-
-/*
- * Returns a connected socket() fd, or else die()s.
- */
-static int git_tcp_connect_sock(char *host, int flags)
-{
- struct strbuf error_message = STRBUF_INIT;
- int sockfd = -1;
- const char *port = STR(DEFAULT_GIT_PORT);
- char *ep;
- struct hostent *he;
- struct sockaddr_in sa;
- char **ap;
- unsigned int nport;
- int cnt;
-
- get_host_and_port(&host, &port);
-
- if (flags & CONNECT_VERBOSE)
- fprintf(stderr, "Looking up %s ... ", host);
-
- he = gethostbyname(host);
- if (!he)
- die("Unable to look up %s (%s)", host, hstrerror(h_errno));
- 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;
- }
-
- if (flags & CONNECT_VERBOSE)
- fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
-
- for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
- memset(&sa, 0, sizeof sa);
- sa.sin_family = he->h_addrtype;
- sa.sin_port = htons(nport);
- memcpy(&sa.sin_addr, *ap, he->h_length);
-
- sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
- if ((sockfd < 0) ||
- connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
- strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
- host,
- cnt,
- inet_ntoa(*(struct in_addr *)&sa.sin_addr),
- strerror(errno));
- if (0 <= sockfd)
- close(sockfd);
- sockfd = -1;
- continue;
- }
- if (flags & CONNECT_VERBOSE)
- fprintf(stderr, "%s ",
- inet_ntoa(*(struct in_addr *)&sa.sin_addr));
- break;
- }
-
- if (sockfd < 0)
- die("unable to connect to %s:\n%s", host, error_message.buf);
-
- enable_keepalive(sockfd);
-
- if (flags & CONNECT_VERBOSE)
- fprintf(stderr, "done.\n");
-
- return sockfd;
-}
-
-#endif /* NO_IPV6 */
-
-
void git_tcp_connect(int fd[2], char *host, int flags)
{
int sockfd = git_tcp_connect_sock(host, flags);
--
1.7.10
|