gitk: force focus to main window
[git/platforms.git] / connect.c
blobc699688edd55d794cf76c4f392d28ff55602bc15
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
473 char *get_port(char *host)
475 char *end;
476 char *p = strchr(host, ':');
478 if (p) {
479 strtol(p+1, &end, 10);
480 if (*end == '\0') {
481 *p = '\0';
482 return p+1;
486 return NULL;
490 * This returns 0 if the transport protocol does not need fork(2),
491 * or a process id if it does. Once done, finish the connection
492 * with finish_connect() with the value returned from this function
493 * (it is safe to call finish_connect() with 0 to support the former
494 * case).
496 * Does not return a negative value on error; it just dies.
498 pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
500 char *host, *path = url;
501 char *end;
502 int c;
503 int pipefd[2][2];
504 pid_t pid;
505 enum protocol protocol = PROTO_LOCAL;
506 int free_path = 0;
507 char *port = NULL;
509 /* Without this we cannot rely on waitpid() to tell
510 * what happened to our children.
512 signal(SIGCHLD, SIG_DFL);
514 host = strstr(url, "://");
515 if(host) {
516 *host = '\0';
517 protocol = get_protocol(url);
518 host += 3;
519 c = '/';
520 } else {
521 host = url;
522 c = ':';
525 if (host[0] == '[') {
526 end = strchr(host + 1, ']');
527 if (end) {
528 *end = 0;
529 end++;
530 host++;
531 } else
532 end = host;
533 } else
534 end = host;
536 path = strchr(end, c);
537 if (path) {
538 if (c == ':') {
539 #ifdef __MINGW32__
540 /* host must have at least 2 chars to
541 * catch DOS C:/path */
542 if (path - end > 1) {
543 #endif
544 protocol = PROTO_SSH;
545 *path++ = '\0';
546 #ifdef __MINGW32__
547 } else
548 path = end;
549 #endif
551 } else
552 path = end;
554 if (!path || !*path)
555 die("No path specified. See 'man git-pull' for valid url syntax");
558 * null-terminate hostname and point path to ~ for URL's like this:
559 * ssh://host.xz/~user/repo
561 if (protocol != PROTO_LOCAL && host != url) {
562 char *ptr = path;
563 if (path[1] == '~')
564 path++;
565 else {
566 path = xstrdup(ptr);
567 free_path = 1;
570 *ptr = '\0';
574 * Add support for ssh port: ssh://host.xy:<port>/...
576 if (protocol == PROTO_SSH && host != url)
577 port = get_port(host);
579 if (protocol == PROTO_GIT) {
580 /* These underlying connection commands die() if they
581 * cannot connect.
583 char *target_host = xstrdup(host);
584 if (git_use_proxy(host))
585 git_proxy_connect(fd, host);
586 else
587 git_tcp_connect(fd, host, flags);
589 * Separate original protocol components prog and path
590 * from extended components with a NUL byte.
592 packet_write(fd[1],
593 "%s %s%chost=%s%c",
594 prog, path, 0,
595 target_host, 0);
596 free(target_host);
597 if (free_path)
598 free(path);
599 return 0;
602 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
603 die("unable to create pipe pair for communication");
605 char command[MAX_CMD_LEN];
606 char *posn = command;
607 int size = MAX_CMD_LEN;
608 int of = 0;
610 of |= add_to_string(&posn, &size, prog, 0);
611 of |= add_to_string(&posn, &size, " ", 0);
612 of |= add_to_string(&posn, &size, path, 1);
614 if (of)
615 die("command line too long");
617 if (protocol == PROTO_SSH) {
618 const char *argv[] = { NULL, host, command, NULL, NULL, NULL };
619 const char *ssh = getenv("GIT_SSH");
620 if (!ssh) ssh = "ssh";
621 if (port) {
622 argv[2] = "-p";
623 argv[3] = port;
624 argv[4] = command;
626 pid = spawnvpe_pipe(ssh, argv, (const char**) environ, pipefd[1], pipefd[0]);
628 else {
629 const char *argv[] = { NULL, "-c", command, NULL };
630 const char **env = copy_environ();
631 env_unsetenv(env, ALTERNATE_DB_ENVIRONMENT);
632 env_unsetenv(env, DB_ENVIRONMENT);
633 env_unsetenv(env, GIT_DIR_ENVIRONMENT);
634 env_unsetenv(env, GIT_WORK_TREE_ENVIRONMENT);
635 env_unsetenv(env, GRAFT_ENVIRONMENT);
636 env_unsetenv(env, INDEX_ENVIRONMENT);
637 pid = spawnvpe_pipe("sh", argv, env, pipefd[1], pipefd[0]);
639 if (pid < 0)
640 die("unable to fork");
641 fd[0] = pipefd[0][0];
642 fd[1] = pipefd[1][1];
643 if (free_path)
644 free(path);
645 return pid;
648 int finish_connect(pid_t pid)
650 if (pid == 0)
651 return 0;
653 while (waitpid(pid, NULL, 0) < 0) {
654 if (errno != EINTR)
655 return -1;
657 return 0;