[PATCH] GIT: Try all addresses for given remote name
[git/git.git] / connect.c
CommitLineData
f7192598 1#include "cache.h"
41cb7488 2#include "pkt-line.h"
b10d0ec7 3#include "quote.h"
f7192598 4#include <sys/wait.h>
2386d658
LT
5#include <sys/socket.h>
6#include <netinet/in.h>
7#include <arpa/inet.h>
8#include <netdb.h>
f7192598 9
d1c133f5
LT
10/*
11 * Read all the refs from the other end
12 */
13struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match)
14{
15 *list = NULL;
16 for (;;) {
17 struct ref *ref;
18 unsigned char old_sha1[20];
19 static char buffer[1000];
20 char *name;
21 int len;
22
23 len = packet_read_line(in, buffer, sizeof(buffer));
24 if (!len)
25 break;
26 if (buffer[len-1] == '\n')
27 buffer[--len] = 0;
28
29 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
30 die("protocol error: expected sha/ref, got '%s'", buffer);
31 name = buffer + 41;
32 if (nr_match && !path_match(name, nr_match, match))
33 continue;
34 ref = xmalloc(sizeof(*ref) + len - 40);
35 memcpy(ref->old_sha1, old_sha1, 20);
36 memset(ref->new_sha1, 0, 20);
37 memcpy(ref->name, buffer + 41, len - 40);
38 ref->next = NULL;
39 *list = ref;
40 list = &ref->next;
41 }
42 return list;
43}
44
41cb7488
LT
45int get_ack(int fd, unsigned char *result_sha1)
46{
47 static char line[1000];
48 int len = packet_read_line(fd, line, sizeof(line));
49
50 if (!len)
51 die("git-fetch-pack: expected ACK/NAK, got EOF");
52 if (line[len-1] == '\n')
53 line[--len] = 0;
54 if (!strcmp(line, "NAK"))
55 return 0;
56 if (!strncmp(line, "ACK ", 3)) {
57 if (!get_sha1_hex(line+4, result_sha1))
58 return 1;
59 }
60 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
61}
62
013e7c7f
LT
63int path_match(const char *path, int nr, char **match)
64{
65 int i;
66 int pathlen = strlen(path);
67
68 for (i = 0; i < nr; i++) {
69 char *s = match[i];
70 int len = strlen(s);
71
72 if (!len || len > pathlen)
73 continue;
74 if (memcmp(path + pathlen - len, s, len))
75 continue;
76 if (pathlen > len && path[pathlen - len - 1] != '/')
77 continue;
78 *s = 0;
79 return 1;
80 }
81 return 0;
82}
83
2386d658
LT
84enum protocol {
85 PROTO_LOCAL = 1,
86 PROTO_SSH,
87 PROTO_GIT,
88};
89
90static enum protocol get_protocol(const char *name)
91{
92 if (!strcmp(name, "ssh"))
93 return PROTO_SSH;
94 if (!strcmp(name, "git"))
95 return PROTO_GIT;
96 die("I don't handle protocol '%s'", name);
97}
98
5ba88448
YH
99#define STR_(s) # s
100#define STR(s) STR_(s)
2386d658
LT
101
102static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
103{
5ba88448
YH
104 int sockfd = -1;
105 char *colon, *end;
106 char *port = STR(DEFAULT_GIT_PORT);
107 struct addrinfo hints, *ai0, *ai;
108 int gai;
109
110 if (host[0] == '[') {
111 end = strchr(host + 1, ']');
112 if (end) {
113 *end = 0;
114 end++;
115 host++;
116 } else
117 end = host;
118 } else
119 end = host;
120 colon = strchr(end, ':');
121
122 if (colon)
123 port = colon + 1;
124
125 memset(&hints, 0, sizeof(hints));
126 hints.ai_socktype = SOCK_STREAM;
127 hints.ai_protocol = IPPROTO_TCP;
128
129 gai = getaddrinfo(host, port, &hints, &ai);
130 if (gai)
131 die("Unable to look up %s (%s)", host, gai_strerror(gai));
132
133 for (ai0 = ai; ai; ai = ai->ai_next) {
134 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
135 if (sockfd < 0)
136 continue;
137 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
138 close(sockfd);
139 sockfd = -1;
140 continue;
2386d658 141 }
5ba88448 142 break;
2386d658
LT
143 }
144
5ba88448 145 freeaddrinfo(ai0);
2386d658 146
2386d658 147 if (sockfd < 0)
5ba88448
YH
148 die("unable to connect a socket (%s)", strerror(errno));
149
2386d658
LT
150 fd[0] = sockfd;
151 fd[1] = sockfd;
152 packet_write(sockfd, "%s %s\n", prog, path);
153 return 0;
154}
155
f7192598
LT
156/*
157 * Yeah, yeah, fixme. Need to pass in the heads etc.
158 */
159int git_connect(int fd[2], char *url, const char *prog)
160{
161 char command[1024];
2386d658 162 char *host, *path;
f7192598
LT
163 char *colon;
164 int pipefd[2][2];
165 pid_t pid;
2386d658 166 enum protocol protocol;
f7192598 167
f7192598
LT
168 host = NULL;
169 path = url;
170 colon = strchr(url, ':');
2386d658 171 protocol = PROTO_LOCAL;
f7192598
LT
172 if (colon) {
173 *colon = 0;
174 host = url;
175 path = colon+1;
2386d658
LT
176 protocol = PROTO_SSH;
177 if (!memcmp(path, "//", 2)) {
178 char *slash = strchr(path + 2, '/');
179 if (slash) {
180 int nr = slash - path - 2;
181 memmove(path, path+2, nr);
182 path[nr] = 0;
183 protocol = get_protocol(url);
184 host = path;
185 path = slash;
186 }
187 }
f7192598 188 }
2386d658
LT
189
190 if (protocol == PROTO_GIT)
191 return git_tcp_connect(fd, prog, host, path);
192
f7192598
LT
193 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
194 die("unable to create pipe pair for communication");
195 pid = fork();
196 if (!pid) {
b10d0ec7
JH
197 snprintf(command, sizeof(command), "%s %s", prog,
198 sq_quote(path));
f7192598
LT
199 dup2(pipefd[1][0], 0);
200 dup2(pipefd[0][1], 1);
201 close(pipefd[0][0]);
202 close(pipefd[0][1]);
203 close(pipefd[1][0]);
204 close(pipefd[1][1]);
2386d658 205 if (protocol == PROTO_SSH)
f7192598
LT
206 execlp("ssh", "ssh", host, command, NULL);
207 else
208 execlp("sh", "sh", "-c", command, NULL);
209 die("exec failed");
210 }
211 fd[0] = pipefd[0][0];
212 fd[1] = pipefd[1][1];
213 close(pipefd[0][1]);
214 close(pipefd[1][0]);
215 return pid;
216}
217
218int finish_connect(pid_t pid)
219{
220 int ret;
221
222 for (;;) {
223 ret = waitpid(pid, NULL, 0);
224 if (!ret)
225 break;
226 if (errno != EINTR)
227 break;
228 }
229 return ret;
230}