]> git.proxmox.com Git - libgit2.git/blob - src/transports/smart_pkt.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / transports / smart_pkt.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "common.h"
9
10 #include "smart.h"
11 #include "util.h"
12 #include "netops.h"
13 #include "posix.h"
14 #include "str.h"
15
16 #include "git2/types.h"
17 #include "git2/errors.h"
18 #include "git2/refs.h"
19 #include "git2/revwalk.h"
20
21 #include <ctype.h>
22
23 #define PKT_LEN_SIZE 4
24 static const char pkt_done_str[] = "0009done\n";
25 static const char pkt_flush_str[] = "0000";
26 static const char pkt_have_prefix[] = "0032have ";
27 static const char pkt_want_prefix[] = "0032want ";
28
29 static int flush_pkt(git_pkt **out)
30 {
31 git_pkt *pkt;
32
33 pkt = git__malloc(sizeof(git_pkt));
34 GIT_ERROR_CHECK_ALLOC(pkt);
35
36 pkt->type = GIT_PKT_FLUSH;
37 *out = pkt;
38
39 return 0;
40 }
41
42 /* the rest of the line will be useful for multi_ack and multi_ack_detailed */
43 static int ack_pkt(git_pkt **out, const char *line, size_t len)
44 {
45 git_pkt_ack *pkt;
46
47 pkt = git__calloc(1, sizeof(git_pkt_ack));
48 GIT_ERROR_CHECK_ALLOC(pkt);
49 pkt->type = GIT_PKT_ACK;
50
51 if (git__prefixncmp(line, len, "ACK "))
52 goto out_err;
53 line += 4;
54 len -= 4;
55
56 if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->oid, line) < 0)
57 goto out_err;
58 line += GIT_OID_HEXSZ;
59 len -= GIT_OID_HEXSZ;
60
61 if (len && line[0] == ' ') {
62 line++;
63 len--;
64
65 if (!git__prefixncmp(line, len, "continue"))
66 pkt->status = GIT_ACK_CONTINUE;
67 else if (!git__prefixncmp(line, len, "common"))
68 pkt->status = GIT_ACK_COMMON;
69 else if (!git__prefixncmp(line, len, "ready"))
70 pkt->status = GIT_ACK_READY;
71 else
72 goto out_err;
73 }
74
75 *out = (git_pkt *) pkt;
76
77 return 0;
78
79 out_err:
80 git_error_set(GIT_ERROR_NET, "error parsing ACK pkt-line");
81 git__free(pkt);
82 return -1;
83 }
84
85 static int nak_pkt(git_pkt **out)
86 {
87 git_pkt *pkt;
88
89 pkt = git__malloc(sizeof(git_pkt));
90 GIT_ERROR_CHECK_ALLOC(pkt);
91
92 pkt->type = GIT_PKT_NAK;
93 *out = pkt;
94
95 return 0;
96 }
97
98 static int comment_pkt(git_pkt **out, const char *line, size_t len)
99 {
100 git_pkt_comment *pkt;
101 size_t alloclen;
102
103 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_comment), len);
104 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
105 pkt = git__malloc(alloclen);
106 GIT_ERROR_CHECK_ALLOC(pkt);
107
108 pkt->type = GIT_PKT_COMMENT;
109 memcpy(pkt->comment, line, len);
110 pkt->comment[len] = '\0';
111
112 *out = (git_pkt *) pkt;
113
114 return 0;
115 }
116
117 static int err_pkt(git_pkt **out, const char *line, size_t len)
118 {
119 git_pkt_err *pkt = NULL;
120 size_t alloclen;
121
122 /* Remove "ERR " from the line */
123 if (git__prefixncmp(line, len, "ERR "))
124 goto out_err;
125 line += 4;
126 len -= 4;
127
128 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len);
129 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
130 pkt = git__malloc(alloclen);
131 GIT_ERROR_CHECK_ALLOC(pkt);
132 pkt->type = GIT_PKT_ERR;
133 pkt->len = len;
134
135 memcpy(pkt->error, line, len);
136 pkt->error[len] = '\0';
137
138 *out = (git_pkt *) pkt;
139
140 return 0;
141
142 out_err:
143 git_error_set(GIT_ERROR_NET, "error parsing ERR pkt-line");
144 git__free(pkt);
145 return -1;
146 }
147
148 static int data_pkt(git_pkt **out, const char *line, size_t len)
149 {
150 git_pkt_data *pkt;
151 size_t alloclen;
152
153 line++;
154 len--;
155
156 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len);
157 pkt = git__malloc(alloclen);
158 GIT_ERROR_CHECK_ALLOC(pkt);
159
160 pkt->type = GIT_PKT_DATA;
161 pkt->len = len;
162 memcpy(pkt->data, line, len);
163
164 *out = (git_pkt *) pkt;
165
166 return 0;
167 }
168
169 static int sideband_progress_pkt(git_pkt **out, const char *line, size_t len)
170 {
171 git_pkt_progress *pkt;
172 size_t alloclen;
173
174 line++;
175 len--;
176
177 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len);
178 pkt = git__malloc(alloclen);
179 GIT_ERROR_CHECK_ALLOC(pkt);
180
181 pkt->type = GIT_PKT_PROGRESS;
182 pkt->len = len;
183 memcpy(pkt->data, line, len);
184
185 *out = (git_pkt *) pkt;
186
187 return 0;
188 }
189
190 static int sideband_error_pkt(git_pkt **out, const char *line, size_t len)
191 {
192 git_pkt_err *pkt;
193 size_t alloc_len;
194
195 line++;
196 len--;
197
198 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(git_pkt_err), len);
199 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1);
200 pkt = git__malloc(alloc_len);
201 GIT_ERROR_CHECK_ALLOC(pkt);
202
203 pkt->type = GIT_PKT_ERR;
204 pkt->len = (int)len;
205 memcpy(pkt->error, line, len);
206 pkt->error[len] = '\0';
207
208 *out = (git_pkt *)pkt;
209
210 return 0;
211 }
212
213 /*
214 * Parse an other-ref line.
215 */
216 static int ref_pkt(git_pkt **out, const char *line, size_t len)
217 {
218 git_pkt_ref *pkt;
219 size_t alloclen;
220
221 pkt = git__calloc(1, sizeof(git_pkt_ref));
222 GIT_ERROR_CHECK_ALLOC(pkt);
223 pkt->type = GIT_PKT_REF;
224
225 if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->head.oid, line) < 0)
226 goto out_err;
227 line += GIT_OID_HEXSZ;
228 len -= GIT_OID_HEXSZ;
229
230 if (git__prefixncmp(line, len, " "))
231 goto out_err;
232 line++;
233 len--;
234
235 if (!len)
236 goto out_err;
237
238 if (line[len - 1] == '\n')
239 --len;
240
241 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1);
242 pkt->head.name = git__malloc(alloclen);
243 GIT_ERROR_CHECK_ALLOC(pkt->head.name);
244
245 memcpy(pkt->head.name, line, len);
246 pkt->head.name[len] = '\0';
247
248 if (strlen(pkt->head.name) < len)
249 pkt->capabilities = strchr(pkt->head.name, '\0') + 1;
250
251 *out = (git_pkt *)pkt;
252 return 0;
253
254 out_err:
255 git_error_set(GIT_ERROR_NET, "error parsing REF pkt-line");
256 if (pkt)
257 git__free(pkt->head.name);
258 git__free(pkt);
259 return -1;
260 }
261
262 static int ok_pkt(git_pkt **out, const char *line, size_t len)
263 {
264 git_pkt_ok *pkt;
265 size_t alloc_len;
266
267 pkt = git__malloc(sizeof(*pkt));
268 GIT_ERROR_CHECK_ALLOC(pkt);
269 pkt->type = GIT_PKT_OK;
270
271 if (git__prefixncmp(line, len, "ok "))
272 goto out_err;
273 line += 3;
274 len -= 3;
275
276 if (len && line[len - 1] == '\n')
277 --len;
278
279 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, len, 1);
280 pkt->ref = git__malloc(alloc_len);
281 GIT_ERROR_CHECK_ALLOC(pkt->ref);
282
283 memcpy(pkt->ref, line, len);
284 pkt->ref[len] = '\0';
285
286 *out = (git_pkt *)pkt;
287 return 0;
288
289 out_err:
290 git_error_set(GIT_ERROR_NET, "error parsing OK pkt-line");
291 git__free(pkt);
292 return -1;
293 }
294
295 static int ng_pkt(git_pkt **out, const char *line, size_t len)
296 {
297 git_pkt_ng *pkt;
298 const char *ptr, *eol;
299 size_t alloclen;
300
301 pkt = git__malloc(sizeof(*pkt));
302 GIT_ERROR_CHECK_ALLOC(pkt);
303
304 pkt->ref = NULL;
305 pkt->type = GIT_PKT_NG;
306
307 eol = line + len;
308
309 if (git__prefixncmp(line, len, "ng "))
310 goto out_err;
311 line += 3;
312
313 if (!(ptr = memchr(line, ' ', eol - line)))
314 goto out_err;
315 len = ptr - line;
316
317 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1);
318 pkt->ref = git__malloc(alloclen);
319 GIT_ERROR_CHECK_ALLOC(pkt->ref);
320
321 memcpy(pkt->ref, line, len);
322 pkt->ref[len] = '\0';
323
324 line = ptr + 1;
325 if (line >= eol)
326 goto out_err;
327
328 if (!(ptr = memchr(line, '\n', eol - line)))
329 goto out_err;
330 len = ptr - line;
331
332 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1);
333 pkt->msg = git__malloc(alloclen);
334 GIT_ERROR_CHECK_ALLOC(pkt->msg);
335
336 memcpy(pkt->msg, line, len);
337 pkt->msg[len] = '\0';
338
339 *out = (git_pkt *)pkt;
340 return 0;
341
342 out_err:
343 git_error_set(GIT_ERROR_NET, "invalid packet line");
344 git__free(pkt->ref);
345 git__free(pkt);
346 return -1;
347 }
348
349 static int unpack_pkt(git_pkt **out, const char *line, size_t len)
350 {
351 git_pkt_unpack *pkt;
352
353 pkt = git__malloc(sizeof(*pkt));
354 GIT_ERROR_CHECK_ALLOC(pkt);
355 pkt->type = GIT_PKT_UNPACK;
356
357 if (!git__prefixncmp(line, len, "unpack ok"))
358 pkt->unpack_ok = 1;
359 else
360 pkt->unpack_ok = 0;
361
362 *out = (git_pkt *)pkt;
363 return 0;
364 }
365
366 static int parse_len(size_t *out, const char *line, size_t linelen)
367 {
368 char num[PKT_LEN_SIZE + 1];
369 int i, k, error;
370 int32_t len;
371 const char *num_end;
372
373 /* Not even enough for the length */
374 if (linelen < PKT_LEN_SIZE)
375 return GIT_EBUFS;
376
377 memcpy(num, line, PKT_LEN_SIZE);
378 num[PKT_LEN_SIZE] = '\0';
379
380 for (i = 0; i < PKT_LEN_SIZE; ++i) {
381 if (!isxdigit(num[i])) {
382 /* Make sure there are no special characters before passing to error message */
383 for (k = 0; k < PKT_LEN_SIZE; ++k) {
384 if(!isprint(num[k])) {
385 num[k] = '.';
386 }
387 }
388
389 git_error_set(GIT_ERROR_NET, "invalid hex digit in length: '%s'", num);
390 return -1;
391 }
392 }
393
394 if ((error = git__strntol32(&len, num, PKT_LEN_SIZE, &num_end, 16)) < 0)
395 return error;
396
397 if (len < 0)
398 return -1;
399
400 *out = (size_t) len;
401 return 0;
402 }
403
404 /*
405 * As per the documentation, the syntax is:
406 *
407 * pkt-line = data-pkt / flush-pkt
408 * data-pkt = pkt-len pkt-payload
409 * pkt-len = 4*(HEXDIG)
410 * pkt-payload = (pkt-len -4)*(OCTET)
411 * flush-pkt = "0000"
412 *
413 * Which means that the first four bytes are the length of the line,
414 * in ASCII hexadecimal (including itself)
415 */
416
417 int git_pkt_parse_line(
418 git_pkt **pkt, const char **endptr, const char *line, size_t linelen)
419 {
420 int error;
421 size_t len;
422
423 if ((error = parse_len(&len, line, linelen)) < 0) {
424 /*
425 * If we fail to parse the length, it might be
426 * because the server is trying to send us the
427 * packfile already or because we do not yet have
428 * enough data.
429 */
430 if (error == GIT_EBUFS)
431 ;
432 else if (!git__prefixncmp(line, linelen, "PACK"))
433 git_error_set(GIT_ERROR_NET, "unexpected pack file");
434 else
435 git_error_set(GIT_ERROR_NET, "bad packet length");
436 return error;
437 }
438
439 /*
440 * Make sure there is enough in the buffer to satisfy
441 * this line.
442 */
443 if (linelen < len)
444 return GIT_EBUFS;
445
446 /*
447 * The length has to be exactly 0 in case of a flush
448 * packet or greater than PKT_LEN_SIZE, as the decoded
449 * length includes its own encoded length of four bytes.
450 */
451 if (len != 0 && len < PKT_LEN_SIZE)
452 return GIT_ERROR;
453
454 line += PKT_LEN_SIZE;
455 /*
456 * The Git protocol does not specify empty lines as part
457 * of the protocol. Not knowing what to do with an empty
458 * line, we should return an error upon hitting one.
459 */
460 if (len == PKT_LEN_SIZE) {
461 git_error_set_str(GIT_ERROR_NET, "Invalid empty packet");
462 return GIT_ERROR;
463 }
464
465 if (len == 0) { /* Flush pkt */
466 *endptr = line;
467 return flush_pkt(pkt);
468 }
469
470 len -= PKT_LEN_SIZE; /* the encoded length includes its own size */
471
472 if (*line == GIT_SIDE_BAND_DATA)
473 error = data_pkt(pkt, line, len);
474 else if (*line == GIT_SIDE_BAND_PROGRESS)
475 error = sideband_progress_pkt(pkt, line, len);
476 else if (*line == GIT_SIDE_BAND_ERROR)
477 error = sideband_error_pkt(pkt, line, len);
478 else if (!git__prefixncmp(line, len, "ACK"))
479 error = ack_pkt(pkt, line, len);
480 else if (!git__prefixncmp(line, len, "NAK"))
481 error = nak_pkt(pkt);
482 else if (!git__prefixncmp(line, len, "ERR"))
483 error = err_pkt(pkt, line, len);
484 else if (*line == '#')
485 error = comment_pkt(pkt, line, len);
486 else if (!git__prefixncmp(line, len, "ok"))
487 error = ok_pkt(pkt, line, len);
488 else if (!git__prefixncmp(line, len, "ng"))
489 error = ng_pkt(pkt, line, len);
490 else if (!git__prefixncmp(line, len, "unpack"))
491 error = unpack_pkt(pkt, line, len);
492 else
493 error = ref_pkt(pkt, line, len);
494
495 *endptr = line + len;
496
497 return error;
498 }
499
500 void git_pkt_free(git_pkt *pkt)
501 {
502 if (pkt == NULL) {
503 return;
504 }
505 if (pkt->type == GIT_PKT_REF) {
506 git_pkt_ref *p = (git_pkt_ref *) pkt;
507 git__free(p->head.name);
508 git__free(p->head.symref_target);
509 }
510
511 if (pkt->type == GIT_PKT_OK) {
512 git_pkt_ok *p = (git_pkt_ok *) pkt;
513 git__free(p->ref);
514 }
515
516 if (pkt->type == GIT_PKT_NG) {
517 git_pkt_ng *p = (git_pkt_ng *) pkt;
518 git__free(p->ref);
519 git__free(p->msg);
520 }
521
522 git__free(pkt);
523 }
524
525 int git_pkt_buffer_flush(git_str *buf)
526 {
527 return git_str_put(buf, pkt_flush_str, strlen(pkt_flush_str));
528 }
529
530 static int buffer_want_with_caps(const git_remote_head *head, transport_smart_caps *caps, git_str *buf)
531 {
532 git_str str = GIT_STR_INIT;
533 char oid[GIT_OID_HEXSZ +1] = {0};
534 size_t len;
535
536 /* Prefer multi_ack_detailed */
537 if (caps->multi_ack_detailed)
538 git_str_puts(&str, GIT_CAP_MULTI_ACK_DETAILED " ");
539 else if (caps->multi_ack)
540 git_str_puts(&str, GIT_CAP_MULTI_ACK " ");
541
542 /* Prefer side-band-64k if the server supports both */
543 if (caps->side_band_64k)
544 git_str_printf(&str, "%s ", GIT_CAP_SIDE_BAND_64K);
545 else if (caps->side_band)
546 git_str_printf(&str, "%s ", GIT_CAP_SIDE_BAND);
547
548 if (caps->include_tag)
549 git_str_puts(&str, GIT_CAP_INCLUDE_TAG " ");
550
551 if (caps->thin_pack)
552 git_str_puts(&str, GIT_CAP_THIN_PACK " ");
553
554 if (caps->ofs_delta)
555 git_str_puts(&str, GIT_CAP_OFS_DELTA " ");
556
557 if (git_str_oom(&str))
558 return -1;
559
560 len = strlen("XXXXwant ") + GIT_OID_HEXSZ + 1 /* NUL */ +
561 git_str_len(&str) + 1 /* LF */;
562
563 if (len > 0xffff) {
564 git_error_set(GIT_ERROR_NET,
565 "tried to produce packet with invalid length %" PRIuZ, len);
566 return -1;
567 }
568
569 git_str_grow_by(buf, len);
570 git_oid_fmt(oid, &head->oid);
571 git_str_printf(buf,
572 "%04xwant %s %s\n", (unsigned int)len, oid, git_str_cstr(&str));
573 git_str_dispose(&str);
574
575 GIT_ERROR_CHECK_ALLOC_STR(buf);
576
577 return 0;
578 }
579
580 /*
581 * All "want" packets have the same length and format, so what we do
582 * is overwrite the OID each time.
583 */
584
585 int git_pkt_buffer_wants(
586 const git_remote_head * const *refs,
587 size_t count,
588 transport_smart_caps *caps,
589 git_str *buf)
590 {
591 size_t i = 0;
592 const git_remote_head *head;
593
594 if (caps->common) {
595 for (; i < count; ++i) {
596 head = refs[i];
597 if (!head->local)
598 break;
599 }
600
601 if (buffer_want_with_caps(refs[i], caps, buf) < 0)
602 return -1;
603
604 i++;
605 }
606
607 for (; i < count; ++i) {
608 char oid[GIT_OID_HEXSZ];
609
610 head = refs[i];
611 if (head->local)
612 continue;
613
614 git_oid_fmt(oid, &head->oid);
615 git_str_put(buf, pkt_want_prefix, strlen(pkt_want_prefix));
616 git_str_put(buf, oid, GIT_OID_HEXSZ);
617 git_str_putc(buf, '\n');
618 if (git_str_oom(buf))
619 return -1;
620 }
621
622 return git_pkt_buffer_flush(buf);
623 }
624
625 int git_pkt_buffer_have(git_oid *oid, git_str *buf)
626 {
627 char oidhex[GIT_OID_HEXSZ + 1];
628
629 memset(oidhex, 0x0, sizeof(oidhex));
630 git_oid_fmt(oidhex, oid);
631 return git_str_printf(buf, "%s%s\n", pkt_have_prefix, oidhex);
632 }
633
634 int git_pkt_buffer_done(git_str *buf)
635 {
636 return git_str_puts(buf, pkt_done_str);
637 }