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>,
Ramkumar Ramachandra <artagnon@gmail.com>
Subject: [PATCH 1/5] transport: expose git_tcp_connect() and friends in new tcp.h
Date: Thu, 8 Mar 2012 07:03:04 -0600 [thread overview]
Message-ID: <20120308130304.GA9426@burratino> (raw)
In-Reply-To: <20120308124857.GA7666@burratino>
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.
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
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
diff --git a/Makefile b/Makefile
index e4f8e0ef..0d0ac31d 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/connect.c b/connect.c
index 912cddee..962dc030 100644
--- a/connect.c
+++ b/connect.c
@@ -5,6 +5,7 @@
#include "refs.h"
#include "run-command.h"
#include "remote.h"
+#include "tcp.h"
#include "url.h"
static char *server_capabilities;
@@ -145,282 +146,6 @@ static enum protocol get_protocol(const char *name)
die("I don't handle protocol '%s'", name);
}
-#define STR_(s) # s
-#define STR(s) STR_(s)
-
-static void get_host_and_port(char **host, const char **port)
-{
- char *colon, *end;
-
- if (*host[0] == '[') {
- end = strchr(*host + 1, ']');
- if (end) {
- *end = 0;
- end++;
- (*host)++;
- } else
- end = *host;
- } else
- end = *host;
- colon = strchr(end, ':');
-
- if (colon) {
- *colon = 0;
- *port = colon + 1;
- }
-}
-
-static void enable_keepalive(int sockfd)
-{
- int ka = 1;
-
- if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
- fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
- 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;
-}
-
-/*
- * 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);
- struct addrinfo hints, *ai0, *ai;
- int gai;
- int cnt = 0;
-
- 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 (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)) {
- strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
- host, cnt, ai_name(ai), strerror(errno));
- if (0 <= sockfd)
- close(sockfd);
- sockfd = -1;
- continue;
- }
- if (flags & CONNECT_VERBOSE)
- fprintf(stderr, "%s ", ai_name(ai));
- break;
- }
-
- freeaddrinfo(ai0);
-
- 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");
-
- strbuf_release(&error_message);
-
- return sockfd;
-}
-
-#else /* NO_IPV6 */
-
-/*
- * 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 */
-
-
-static void git_tcp_connect(int fd[2], char *host, int flags)
-{
- int sockfd = git_tcp_connect_sock(host, flags);
-
- fd[0] = sockfd;
- fd[1] = dup(sockfd);
-}
-
-
-static char *git_proxy_command;
-
-static int git_proxy_command_options(const char *var, const char *value,
- void *cb)
-{
- if (!strcmp(var, "core.gitproxy")) {
- const char *for_pos;
- int matchlen = -1;
- int hostlen;
- const char *rhost_name = cb;
- int rhost_len = strlen(rhost_name);
-
- if (git_proxy_command)
- return 0;
- if (!value)
- return config_error_nonbool(var);
- /* [core]
- * ;# matches www.kernel.org as well
- * gitproxy = netcatter-1 for kernel.org
- * gitproxy = netcatter-2 for sample.xz
- * gitproxy = netcatter-default
- */
- for_pos = strstr(value, " for ");
- if (!for_pos)
- /* matches everybody */
- matchlen = strlen(value);
- else {
- hostlen = strlen(for_pos + 5);
- if (rhost_len < hostlen)
- matchlen = -1;
- else if (!strncmp(for_pos + 5,
- rhost_name + rhost_len - hostlen,
- hostlen) &&
- ((rhost_len == hostlen) ||
- rhost_name[rhost_len - hostlen -1] == '.'))
- matchlen = for_pos - value;
- else
- matchlen = -1;
- }
- if (0 <= matchlen) {
- /* core.gitproxy = none for kernel.org */
- if (matchlen == 4 &&
- !memcmp(value, "none", 4))
- matchlen = 0;
- git_proxy_command = xmemdupz(value, matchlen);
- }
- return 0;
- }
-
- return git_default_config(var, value, cb);
-}
-
-static int git_use_proxy(const char *host)
-{
- git_proxy_command = getenv("GIT_PROXY_COMMAND");
- git_config(git_proxy_command_options, (void*)host);
- return (git_proxy_command && *git_proxy_command);
-}
-
-static struct child_process *git_proxy_connect(int fd[2], char *host)
-{
- const char *port = STR(DEFAULT_GIT_PORT);
- const char **argv;
- struct child_process *proxy;
-
- get_host_and_port(&host, &port);
-
- argv = xmalloc(sizeof(*argv) * 4);
- argv[0] = git_proxy_command;
- argv[1] = host;
- argv[2] = port;
- argv[3] = NULL;
- proxy = xcalloc(1, sizeof(*proxy));
- proxy->argv = argv;
- proxy->in = -1;
- proxy->out = -1;
- if (start_command(proxy))
- die("cannot start proxy %s", argv[0]);
- fd[0] = proxy->out; /* read from proxy stdout */
- fd[1] = proxy->in; /* write to proxy stdin */
- return proxy;
-}
-
#define MAX_CMD_LEN 1024
static char *get_port(char *host)
diff --git a/tcp.c b/tcp.c
new file mode 100644
index 00000000..f5e1ab37
--- /dev/null
+++ b/tcp.c
@@ -0,0 +1,278 @@
+#include "cache.h"
+#include "run-command.h"
+
+#define STR_(s) # s
+#define STR(s) STR_(s)
+
+static void get_host_and_port(char **host, const char **port)
+{
+ char *colon, *end;
+
+ if (*host[0] == '[') {
+ end = strchr(*host + 1, ']');
+ if (end) {
+ *end = 0;
+ end++;
+ (*host)++;
+ } else
+ end = *host;
+ } else
+ end = *host;
+ colon = strchr(end, ':');
+
+ if (colon) {
+ *colon = 0;
+ *port = colon + 1;
+ }
+}
+
+static void enable_keepalive(int sockfd)
+{
+ int ka = 1;
+
+ if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
+ fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
+ 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;
+}
+
+/*
+ * 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);
+ struct addrinfo hints, *ai0, *ai;
+ int gai;
+ int cnt = 0;
+
+ 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 (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)) {
+ strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
+ host, cnt, ai_name(ai), strerror(errno));
+ if (0 <= sockfd)
+ close(sockfd);
+ sockfd = -1;
+ continue;
+ }
+ if (flags & CONNECT_VERBOSE)
+ fprintf(stderr, "%s ", ai_name(ai));
+ break;
+ }
+
+ freeaddrinfo(ai0);
+
+ 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");
+
+ strbuf_release(&error_message);
+
+ return sockfd;
+}
+
+#else /* NO_IPV6 */
+
+/*
+ * 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);
+
+ fd[0] = sockfd;
+ fd[1] = dup(sockfd);
+}
+
+
+static char *git_proxy_command;
+
+static int git_proxy_command_options(const char *var, const char *value,
+ void *cb)
+{
+ if (!strcmp(var, "core.gitproxy")) {
+ const char *for_pos;
+ int matchlen = -1;
+ int hostlen;
+ const char *rhost_name = cb;
+ int rhost_len = strlen(rhost_name);
+
+ if (git_proxy_command)
+ return 0;
+ if (!value)
+ return config_error_nonbool(var);
+ /* [core]
+ * ;# matches www.kernel.org as well
+ * gitproxy = netcatter-1 for kernel.org
+ * gitproxy = netcatter-2 for sample.xz
+ * gitproxy = netcatter-default
+ */
+ for_pos = strstr(value, " for ");
+ if (!for_pos)
+ /* matches everybody */
+ matchlen = strlen(value);
+ else {
+ hostlen = strlen(for_pos + 5);
+ if (rhost_len < hostlen)
+ matchlen = -1;
+ else if (!strncmp(for_pos + 5,
+ rhost_name + rhost_len - hostlen,
+ hostlen) &&
+ ((rhost_len == hostlen) ||
+ rhost_name[rhost_len - hostlen -1] == '.'))
+ matchlen = for_pos - value;
+ else
+ matchlen = -1;
+ }
+ if (0 <= matchlen) {
+ /* core.gitproxy = none for kernel.org */
+ if (matchlen == 4 &&
+ !memcmp(value, "none", 4))
+ matchlen = 0;
+ git_proxy_command = xmemdupz(value, matchlen);
+ }
+ return 0;
+ }
+
+ return git_default_config(var, value, cb);
+}
+
+int git_use_proxy(const char *host)
+{
+ git_proxy_command = getenv("GIT_PROXY_COMMAND");
+ git_config(git_proxy_command_options, (void*)host);
+ return (git_proxy_command && *git_proxy_command);
+}
+
+struct child_process *git_proxy_connect(int fd[2], char *host)
+{
+ const char *port = STR(DEFAULT_GIT_PORT);
+ const char **argv;
+ struct child_process *proxy;
+
+ get_host_and_port(&host, &port);
+
+ argv = xmalloc(sizeof(*argv) * 4);
+ argv[0] = git_proxy_command;
+ argv[1] = host;
+ argv[2] = port;
+ argv[3] = NULL;
+ proxy = xcalloc(1, sizeof(*proxy));
+ proxy->argv = argv;
+ proxy->in = -1;
+ proxy->out = -1;
+ if (start_command(proxy))
+ die("cannot start proxy %s", argv[0]);
+ fd[0] = proxy->out; /* read from proxy stdout */
+ fd[1] = proxy->in; /* write to proxy stdin */
+ return proxy;
+}
diff --git a/tcp.h b/tcp.h
new file mode 100644
index 00000000..4de5f712
--- /dev/null
+++ b/tcp.h
@@ -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
--
1.7.9.2
next prev parent reply other threads:[~2012-03-08 13:03 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 ` Jonathan Nieder [this message]
2012-03-08 15:28 ` [PATCH 1/5] transport: expose git_tcp_connect() and friends in new tcp.h Erik Faye-Lund
2012-03-08 13:05 ` [PATCH 2/5] daemon: make host resolution a separate function Jonathan Nieder
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120308130304.GA9426@burratino \
--to=jrnieder@gmail.com \
--cc=artagnon@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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).