Revert fa807fcb: 'Revert "Makefile: remove $foo when $foo.exe...'
[git/mingw/4msysgit/wingit-dll.git] / connect.c
blob9f51e8dcc85cf3831fa5d1b3538f723ef5ea494e
1 #include "git-compat-util.h"
2 #include "cache.h"
3 #include "pkt-line.h"
4 #include "quote.h"
5 #include "refs.h"
6 #include "run-command.h"
7 #include "remote.h"
8 #include "spawn-pipe.h"
10 static char *server_capabilities;
12 static int check_ref(const char *name, int len, unsigned int flags)
14 if (!flags)
15 return 1;
17 if (len < 5 || memcmp(name, "refs/", 5))
18 return 0;
20 /* Skip the "refs/" part */
21 name += 5;
22 len -= 5;
24 /* REF_NORMAL means that we don't want the magic fake tag refs */
25 if ((flags & REF_NORMAL) && check_ref_format(name) < 0)
26 return 0;
28 /* REF_HEADS means that we want regular branch heads */
29 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
30 return 1;
32 /* REF_TAGS means that we want tags */
33 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
34 return 1;
36 /* All type bits clear means that we are ok with anything */
37 return !(flags & ~REF_NORMAL);
41 * Read all the refs from the other end
43 struct ref **get_remote_heads(int in, struct ref **list,
44 int nr_match, char **match,
45 unsigned int flags)
47 *list = NULL;
48 for (;;) {
49 struct ref *ref;
50 unsigned char old_sha1[20];
51 static char buffer[1000];
52 char *name;
53 int len, name_len;
55 len = packet_read_line(in, buffer, sizeof(buffer));
56 if (!len)
57 break;
58 if (buffer[len-1] == '\n')
59 buffer[--len] = 0;
61 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
62 die("protocol error: expected sha/ref, got '%s'", buffer);
63 name = buffer + 41;
65 name_len = strlen(name);
66 if (len != name_len + 41) {
67 if (server_capabilities)
68 free(server_capabilities);
69 server_capabilities = xstrdup(name + name_len + 1);
72 if (!check_ref(name, name_len, flags))
73 continue;
74 if (nr_match && !path_match(name, nr_match, match))
75 continue;
76 ref = alloc_ref(len - 40);
77 hashcpy(ref->old_sha1, old_sha1);
78 memcpy(ref->name, buffer + 41, len - 40);
79 *list = ref;
80 list = &ref->next;
82 return list;
85 int server_supports(const char *feature)
87 return server_capabilities &&
88 strstr(server_capabilities, feature) != NULL;
91 int get_ack(int fd, unsigned char *result_sha1)
93 static char line[1000];
94 int len = packet_read_line(fd, line, sizeof(line));
96 if (!len)
97 die("git-fetch-pack: expected ACK/NAK, got EOF");
98 if (line[len-1] == '\n')
99 line[--len] = 0;
100 if (!strcmp(line, "NAK"))
101 return 0;
102 if (!prefixcmp(line, "ACK ")) {
103 if (!get_sha1_hex(line+4, result_sha1)) {
104 if (strstr(line+45, "continue"))
105 return 2;
106 return 1;
109 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
112 int path_match(const char *path, int nr, char **match)
114 int i;
115 int pathlen = strlen(path);
117 for (i = 0; i < nr; i++) {
118 char *s = match[i];
119 int len = strlen(s);
121 if (!len || len > pathlen)
122 continue;
123 if (memcmp(path + pathlen - len, s, len))
124 continue;
125 if (pathlen > len && path[pathlen - len - 1] != '/')
126 continue;
127 *s = 0;
128 return (i + 1);
130 return 0;
133 enum protocol {
134 PROTO_LOCAL = 1,
135 PROTO_SSH,
136 PROTO_GIT,
139 static enum protocol get_protocol(const char *name)
141 if (!strcmp(name, "ssh"))
142 return PROTO_SSH;
143 if (!strcmp(name, "git"))
144 return PROTO_GIT;
145 if (!strcmp(name, "git+ssh"))
146 return PROTO_SSH;
147 if (!strcmp(name, "ssh+git"))
148 return PROTO_SSH;
149 if (!strcmp(name, "file"))
150 return PROTO_LOCAL;
151 die("I don't handle protocol '%s'", name);
154 #define STR_(s) # s
155 #define STR(s) STR_(s)
157 #ifndef NO_IPV6
159 static const char *ai_name(const struct addrinfo *ai)
161 static char addr[INET_ADDRSTRLEN];
162 if ( AF_INET == ai->ai_family ) {
163 struct sockaddr_in *in;
164 in = (struct sockaddr_in *)ai->ai_addr;
165 inet_ntop(ai->ai_family, &in->sin_addr, addr, sizeof(addr));
166 } else if ( AF_INET6 == ai->ai_family ) {
167 struct sockaddr_in6 *in;
168 in = (struct sockaddr_in6 *)ai->ai_addr;
169 inet_ntop(ai->ai_family, &in->sin6_addr, addr, sizeof(addr));
170 } else {
171 strcpy(addr, "(unknown)");
173 return addr;
177 * Returns a connected socket() fd, or else die()s.
179 static int git_tcp_connect_sock(char *host, int flags)
181 int sockfd = -1, saved_errno = 0;
182 char *colon, *end;
183 const char *port = STR(DEFAULT_GIT_PORT);
184 struct addrinfo hints, *ai0, *ai;
185 int gai;
186 int cnt = 0;
188 if (host[0] == '[') {
189 end = strchr(host + 1, ']');
190 if (end) {
191 *end = 0;
192 end++;
193 host++;
194 } else
195 end = host;
196 } else
197 end = host;
198 colon = strchr(end, ':');
200 if (colon) {
201 *colon = 0;
202 port = colon + 1;
203 if (!*port)
204 port = "<none>";
207 memset(&hints, 0, sizeof(hints));
208 hints.ai_socktype = SOCK_STREAM;
209 hints.ai_protocol = IPPROTO_TCP;
211 if (flags & CONNECT_VERBOSE)
212 fprintf(stderr, "Looking up %s ... ", host);
214 gai = getaddrinfo(host, port, &hints, &ai);
215 if (gai)
216 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
218 if (flags & CONNECT_VERBOSE)
219 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
221 for (ai0 = ai; ai; ai = ai->ai_next) {
222 sockfd = socket(ai->ai_family,
223 ai->ai_socktype, ai->ai_protocol);
224 if (sockfd < 0) {
225 saved_errno = errno;
226 continue;
228 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
229 saved_errno = errno;
230 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
231 host,
232 cnt,
233 ai_name(ai),
234 strerror(saved_errno));
235 close(sockfd);
236 sockfd = -1;
237 continue;
239 if (flags & CONNECT_VERBOSE)
240 fprintf(stderr, "%s ", ai_name(ai));
241 break;
244 freeaddrinfo(ai0);
246 if (sockfd < 0)
247 die("unable to connect a socket (%s)", strerror(saved_errno));
249 if (flags & CONNECT_VERBOSE)
250 fprintf(stderr, "done.\n");
252 return sockfd;
255 #else /* NO_IPV6 */
258 * Returns a connected socket() fd, or else die()s.
260 static int git_tcp_connect_sock(char *host, int flags)
262 int sockfd = -1, saved_errno = 0;
263 char *colon, *end;
264 char *port = STR(DEFAULT_GIT_PORT), *ep;
265 struct hostent *he;
266 struct sockaddr_in sa;
267 char **ap;
268 unsigned int nport;
269 int cnt;
271 if (host[0] == '[') {
272 end = strchr(host + 1, ']');
273 if (end) {
274 *end = 0;
275 end++;
276 host++;
277 } else
278 end = host;
279 } else
280 end = host;
281 colon = strchr(end, ':');
283 if (colon) {
284 *colon = 0;
285 port = colon + 1;
288 if (flags & CONNECT_VERBOSE)
289 fprintf(stderr, "Looking up %s ... ", host);
291 he = gethostbyname(host);
292 if (!he)
293 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
294 nport = strtoul(port, &ep, 10);
295 if ( ep == port || *ep ) {
296 /* Not numeric */
297 struct servent *se = getservbyname(port,"tcp");
298 if ( !se )
299 die("Unknown port %s\n", port);
300 nport = se->s_port;
303 if (flags & CONNECT_VERBOSE)
304 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
306 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
307 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
308 if (sockfd < 0) {
309 saved_errno = errno;
310 continue;
313 memset(&sa, 0, sizeof sa);
314 sa.sin_family = he->h_addrtype;
315 sa.sin_port = htons(nport);
316 memcpy(&sa.sin_addr, *ap, he->h_length);
318 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
319 saved_errno = errno;
320 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
321 host,
322 cnt,
323 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
324 strerror(saved_errno));
325 close(sockfd);
326 sockfd = -1;
327 continue;
329 if (flags & CONNECT_VERBOSE)
330 fprintf(stderr, "%s ",
331 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
332 break;
335 if (sockfd < 0)
336 die("unable to connect a socket (%s)", strerror(saved_errno));
338 if (flags & CONNECT_VERBOSE)
339 fprintf(stderr, "done.\n");
341 return sockfd;
344 #endif /* NO_IPV6 */
347 static void git_tcp_connect(int fd[2], char *host, int flags)
349 #ifndef __MINGW32__
350 int sockfd = git_tcp_connect_sock(host, flags);
351 #else
352 int sockfd;
353 WSADATA wsa;
355 if (WSAStartup(MAKEWORD(2,2), &wsa))
356 die("unable to initialize winsock subsystem, error %d",
357 WSAGetLastError());
358 atexit((void(*)(void)) WSACleanup);
360 sockfd = git_tcp_connect_sock(host, flags);
361 /* convert into a file descriptor */
362 if ((sockfd = _open_osfhandle(sockfd, O_RDWR|O_BINARY)) < 0)
363 die("unable to make a socket file descriptor: %s",
364 strerror(errno));
365 #endif
367 fd[0] = sockfd;
368 fd[1] = dup(sockfd);
372 static char *git_proxy_command;
373 static const char *rhost_name;
374 static int rhost_len;
376 static int git_proxy_command_options(const char *var, const char *value)
378 if (!strcmp(var, "core.gitproxy")) {
379 const char *for_pos;
380 int matchlen = -1;
381 int hostlen;
383 if (git_proxy_command)
384 return 0;
385 /* [core]
386 * ;# matches www.kernel.org as well
387 * gitproxy = netcatter-1 for kernel.org
388 * gitproxy = netcatter-2 for sample.xz
389 * gitproxy = netcatter-default
391 for_pos = strstr(value, " for ");
392 if (!for_pos)
393 /* matches everybody */
394 matchlen = strlen(value);
395 else {
396 hostlen = strlen(for_pos + 5);
397 if (rhost_len < hostlen)
398 matchlen = -1;
399 else if (!strncmp(for_pos + 5,
400 rhost_name + rhost_len - hostlen,
401 hostlen) &&
402 ((rhost_len == hostlen) ||
403 rhost_name[rhost_len - hostlen -1] == '.'))
404 matchlen = for_pos - value;
405 else
406 matchlen = -1;
408 if (0 <= matchlen) {
409 /* core.gitproxy = none for kernel.org */
410 if (matchlen == 4 &&
411 !memcmp(value, "none", 4))
412 matchlen = 0;
413 git_proxy_command = xmalloc(matchlen + 1);
414 memcpy(git_proxy_command, value, matchlen);
415 git_proxy_command[matchlen] = 0;
417 return 0;
420 return git_default_config(var, value);
423 static int git_use_proxy(const char *host)
425 rhost_name = host;
426 rhost_len = strlen(host);
427 git_proxy_command = getenv("GIT_PROXY_COMMAND");
428 git_config(git_proxy_command_options);
429 rhost_name = NULL;
430 return (git_proxy_command && *git_proxy_command);
433 static void git_proxy_connect(int fd[2], char *host)
435 const char *port = STR(DEFAULT_GIT_PORT);
436 char *colon, *end;
437 const char *argv[4];
438 struct child_process proxy;
440 if (host[0] == '[') {
441 end = strchr(host + 1, ']');
442 if (end) {
443 *end = 0;
444 end++;
445 host++;
446 } else
447 end = host;
448 } else
449 end = host;
450 colon = strchr(end, ':');
452 if (colon) {
453 *colon = 0;
454 port = colon + 1;
457 argv[0] = git_proxy_command;
458 argv[1] = host;
459 argv[2] = port;
460 argv[3] = NULL;
461 memset(&proxy, 0, sizeof(proxy));
462 proxy.argv = argv;
463 proxy.in = -1;
464 proxy.out = -1;
465 if (start_command(&proxy))
466 die("cannot start proxy %s", argv[0]);
467 fd[0] = proxy.out; /* read from proxy stdout */
468 fd[1] = proxy.in; /* write to proxy stdin */
471 #define MAX_CMD_LEN 1024
474 * This returns 0 if the transport protocol does not need fork(2),
475 * or a process id if it does. Once done, finish the connection
476 * with finish_connect() with the value returned from this function
477 * (it is safe to call finish_connect() with 0 to support the former
478 * case).
480 * Does not return a negative value on error; it just dies.
482 pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
484 char *host, *path = url;
485 char *end;
486 int c;
487 int pipefd[2][2];
488 pid_t pid;
489 enum protocol protocol = PROTO_LOCAL;
490 int free_path = 0;
492 /* Without this we cannot rely on waitpid() to tell
493 * what happened to our children.
495 signal(SIGCHLD, SIG_DFL);
497 host = strstr(url, "://");
498 if(host) {
499 *host = '\0';
500 protocol = get_protocol(url);
501 host += 3;
502 c = '/';
503 } else {
504 host = url;
505 c = ':';
508 if (host[0] == '[') {
509 end = strchr(host + 1, ']');
510 if (end) {
511 *end = 0;
512 end++;
513 host++;
514 } else
515 end = host;
516 } else
517 end = host;
519 path = strchr(end, c);
520 if (path) {
521 if (c == ':'
522 #ifdef __MINGW32__
523 && path - end > 1 /* host must have at least 2 chars to catch DOS C:/path */
524 #endif
526 protocol = PROTO_SSH;
527 *path++ = '\0';
529 } else
530 path = end;
532 if (!path || !*path)
533 die("No path specified. See 'man git-pull' for valid url syntax");
536 * null-terminate hostname and point path to ~ for URL's like this:
537 * ssh://host.xz/~user/repo
539 if (protocol != PROTO_LOCAL && host != url) {
540 char *ptr = path;
541 if (path[1] == '~')
542 path++;
543 else {
544 path = xstrdup(ptr);
545 free_path = 1;
548 *ptr = '\0';
551 if (protocol == PROTO_GIT) {
552 /* These underlying connection commands die() if they
553 * cannot connect.
555 char *target_host = xstrdup(host);
556 if (git_use_proxy(host))
557 git_proxy_connect(fd, host);
558 else
559 git_tcp_connect(fd, host, flags);
561 * Separate original protocol components prog and path
562 * from extended components with a NUL byte.
564 packet_write(fd[1],
565 "%s %s%chost=%s%c",
566 prog, path, 0,
567 target_host, 0);
568 free(target_host);
569 if (free_path)
570 free(path);
571 return 0;
574 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
575 die("unable to create pipe pair for communication");
577 char command[MAX_CMD_LEN];
578 char *posn = command;
579 int size = MAX_CMD_LEN;
580 int of = 0;
582 of |= add_to_string(&posn, &size, prog, 0);
583 of |= add_to_string(&posn, &size, " ", 0);
584 of |= add_to_string(&posn, &size, path, 1);
586 if (of)
587 die("command line too long");
589 if (protocol == PROTO_SSH) {
590 const char *argv[] = { NULL, host, command, NULL };
591 const char *ssh = getenv("GIT_SSH");
592 if (!ssh) ssh = "ssh";
593 pid = spawnvpe_pipe(ssh, argv, (const char**) environ, pipefd[1], pipefd[0]);
595 else {
596 const char *argv[] = { NULL, "-c", command, NULL };
597 const char **env = copy_environ();
598 env_unsetenv(env, ALTERNATE_DB_ENVIRONMENT);
599 env_unsetenv(env, DB_ENVIRONMENT);
600 env_unsetenv(env, GIT_DIR_ENVIRONMENT);
601 env_unsetenv(env, GIT_WORK_TREE_ENVIRONMENT);
602 env_unsetenv(env, GRAFT_ENVIRONMENT);
603 env_unsetenv(env, INDEX_ENVIRONMENT);
604 pid = spawnvpe_pipe("sh", argv, env, pipefd[1], pipefd[0]);
606 if (pid < 0)
607 die("unable to fork");
608 fd[0] = pipefd[0][0];
609 fd[1] = pipefd[1][1];
610 if (free_path)
611 free(path);
612 return pid;
615 int finish_connect(pid_t pid)
617 if (pid == 0)
618 return 0;
620 while (waitpid(pid, NULL, 0) < 0) {
621 if (errno != EINTR)
622 return -1;
624 return 0;